Example #1
0
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(example_psock_client_process, ev, data)
{
  uip_ipaddr_t addr;

  printf("%d\n", TEST);
  
  PROCESS_BEGIN();

  uip_ipaddr(addr, 192,168,2,1);
  tcp_connect(addr, UIP_HTONS(80), NULL);

  printf("Connecting...\n");
  PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);

  if(uip_aborted() || uip_timedout() || uip_closed()) {
    printf("Could not establish connection\n");
  } else if(uip_connected()) {
    printf("Connected\n");
    
    PSOCK_INIT(&ps, buffer, sizeof(buffer));

    do {
      handle_connection(&ps);
      PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);
    } while(!(uip_closed() || uip_aborted() || uip_timedout()));

    printf("\nConnection closed.\n");
  }
  PROCESS_END();
}
Example #2
0
PROCESS_THREAD(server_process, ev, data)
{
  PROCESS_BEGIN();

  set_global_address();
  leds_init();
  print_local_addresses();
  printf("Starting TCP server on port=%d\n", PORT);
  tcp_listen(UIP_HTONS(PORT));

  while(1) {
    PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);

    if(uip_aborted() )
	printf("TCP aborted\n");
    if(uip_timedout() )
	printf("TCP timeoutn\n");
    if(uip_closed() )
	printf("TCP closed\n");

    if(uip_connected()) {
      printf("TCP Connected\n\r");
      PSOCK_INIT(&ps, buf, sizeof(buf));

      while(!(uip_aborted() || uip_closed() || uip_timedout())) {
	PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);
	handle_connection(&ps);
      }
    }
  }
  PROCESS_END();
}
Example #3
0
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(server_process, ev, data)
{
  PROCESS_BEGIN();

  u16_t hostport = 7777;

  printf("TCP listen on %d.%d.%d.%d:%u\n", 
    uip_ipaddr_to_quad(&uip_hostaddr), hostport);

  tcp_listen(uip_htons(hostport));

  while(1) {
    PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);
    if (uip_connected()) {
      printf("server: client arrived!\n");
      connected = 1;
      while (!(uip_aborted() || uip_closed() || uip_timedout())) {
        PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);
      }
      if (uip_aborted() || uip_closed()) {
        connected = 0;
      }
    } else {
      printf("server: client not accepted!\n");
      KLEENET_ASSERT(0 && "server: client not accepted!\n");
    }
  }

  PROCESS_END();
}
Example #4
0
/*---------------------------------------------------------------------------*/
void
httpd_appcall(void *state)
{
  struct httpd_state *s = (struct httpd_state *)state;

  if(uip_closed() || uip_aborted() || uip_timedout()) {
    if(s != NULL) {
      memb_free(&conns, s);
    }
  } else if(uip_connected()) {
    s = (struct httpd_state *)memb_alloc(&conns);
    if(s == NULL) {
      uip_abort();
      return;
    }
    tcp_markconn(uip_conn, s);
    PSOCK_INIT(&s->sin, s->inputbuf, sizeof(s->inputbuf) - 1);
    PSOCK_INIT(&s->sout, s->inputbuf, sizeof(s->inputbuf) - 1);
    PT_INIT(&s->outputpt);
    s->state = STATE_WAITING;
    timer_set(&s->timer, CLOCK_SECOND * 10);
    handle_connection(s);
  } else if(s != NULL) {
    if(uip_poll()) {
      if(timer_expired(&s->timer)) {
	uip_abort();
      }
    } else {
      timer_reset(&s->timer);
    }
    handle_connection(s);
  } else {
    uip_abort();
  }
}
Example #5
0
/*
 * The definition of the process.
 */
PROCESS_THREAD(sensor_psock_server_process, ev, data)
{
    PROCESS_BEGIN();

    tcp_listen(UIP_HTONS(1010));

    while(1) {

        PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);

        if(uip_connected()) {

            PSOCK_INIT(&ps, buffer, sizeof(buffer));

            while(!(uip_aborted() || uip_closed() || uip_timedout())) {

                PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);
                handle_connection(&ps);
            }
            PSOCK_CLOSE(&ps);
        }
    }

    PSOCK_CLOSE_EXIT(&ps);
    PROCESS_END();
}
//这是一个TCP 服务器应用回调函数。
//该函数通过UIP_APPCALL(tcp_demo_appcall)调用,实现Web Server的功能.
//当uip事件发生时,UIP_APPCALL函数会被调用,根据所属端口(1200),确定是否执行该函数。
//例如 : 当一个TCP连接被创建时、有新的数据到达、数据已经被应答、数据需要重发等事件
void tcp_server_demo_appcall(void)
{
 	struct tcp_demo_appstate *s = (struct tcp_demo_appstate *)&uip_conn->appstate;
	if(uip_aborted())tcp_server_aborted();		//连接终止
 	if(uip_timedout())tcp_server_timedout();	//连接超时   
	if(uip_closed())tcp_server_closed();		//连接关闭	   
 	if(uip_connected())tcp_server_connected();	//连接成功	    
	if(uip_acked())tcp_server_acked();			//发送的数据成功送达 
	//接收到一个新的TCP数据包 
	if (uip_newdata())//收到客户端发过来的数据
	{
		if((tcp_server_sta&(1<<6))==0)//还未收到数据
		{
			if(uip_len>199)
			{		   
				((uint8_t*)uip_appdata)[199]=0;
			}		    
	    	strcpy((char*)tcp_server_databuf,uip_appdata);				   	  		  
			tcp_server_sta|=1<<6;//表示收到客户端数据
		}
	}else if(tcp_server_sta&(1<<5))//有数据需要发送
	{
		s->textptr=tcp_server_databuf;
		s->textlen=strlen((const char*)tcp_server_databuf);
		tcp_server_sta&=~(1<<5);//清除标记
	}   
	//当需要重发、新数据到达、数据包送达、连接建立时,通知uip发送数据 
	if(uip_rexmit()||uip_newdata()||uip_acked()||uip_connected()||uip_poll())
	{
		tcp_server_senddata();
	}
}	  
void uip_task_appcall()
{  
	if(uip_connected()) 
	{
		socket_process_new_connect_();
	}else
	if(uip_poll())
	{
		if(uip_stopped(uip_conn))
		{
			socket_process_try_restart_();
		}
		
		socket_process_write_();
	}else
	if(uip_newdata())
	{
		socket_process_new_data_();
		socket_process_write_();
	}else
	if(uip_aborted() || uip_closed())
	{
		socket_process_close_();
	}else
	if(uip_timedout())
	{
		socket_process_timeout_();
		uip_close();
	}else
	if(uip_acked() || uip_rexmit())
	{
		socket_process_write_();
	}
}
Example #8
0
void tcp_client_appcall_user(void)
{
    uint8 buffer[ELEMENT_SIZE];
    if (uip_aborted() || uip_timedout() || uip_closed())
    {
        tcp_client_reconnect();
        return;
    }

    if (uip_rexmit())
    {
        _uip_send_rexmit_http();
        return;
    }

    if (QueueLength(&queue) != 0)
    {
        FreeQueuePop(&queue, buffer);
        _uip_send_http(buffer);
    }
    else
    {
        if (nv_read_msg(buffer))
        {
            _uip_send_http(buffer);
        }
    }

    if (uip_newdata())
    {
        process_ip_command(uip_datalen(), (uint8*)uip_appdata);
    }
}
Example #9
0
/*---------------------------------------------------------------------------*/
void
httpd_appcall(void)
{
  struct httpd_state *s = (struct httpd_state *)&(uip_conn->appstate);

  if(uip_closed() || uip_aborted() || uip_timedout()) {
  } else if(uip_connected()) {
    PSOCK_INIT(&s->sin, s->inputbuf, sizeof(s->inputbuf) - 1);
    PSOCK_INIT(&s->sout, s->inputbuf, sizeof(s->inputbuf) - 1);
    PT_INIT(&s->outputpt);
    s->state = STATE_WAITING;
    /*    timer_set(&s->timer, CLOCK_SECOND * 100);*/
    s->timer = 0;
    handle_connection(s);
  } else if(s != NULL) {
    if(uip_poll()) {
      ++s->timer;
      if(s->timer >= 20) {
	uip_abort();
      }
    } else {
      s->timer = 0;
    }
    handle_connection(s);
  } else {
    uip_abort();
  }
}
Example #10
0
/*---------------------------------------------------------------------*/
static void
uipcall(void *state)
{
  if(uip_udpconnection()) {
    recv_udpthread(&s.recv_udpthread_pt);
    send_udpthread(&s.udpthread_pt);
  } else {
    if(uip_conn->lport == HTONS(CODEPROP_DATA_PORT)) {
      if(uip_connected()) {

	if(state == NULL) {
	  s.addr = 0;
	  s.count = 0;
	  PT_INIT(&s.tcpthread_pt);
	  process_poll(&codeprop_process);
	  tcp_markconn(uip_conn, &s);
/* 	  process_post(PROCESS_BROADCAST, codeprop_event_quit, */
/* 		       (process_data_t)NULL); */
	} else {
	  PRINTF(("codeprop: uip_connected() and state != NULL\n"));
	  uip_abort();
	}
      }
      recv_tcpthread(&s.tcpthread_pt);


      if(uip_closed() || uip_aborted() || uip_timedout()) {
	PRINTF(("codeprop: connection down\n"));
	tcp_markconn(uip_conn, NULL);
      }
    }
  }
}
Example #11
0
PROCESS_THREAD(tcp_process, ev, data)
{
  static char incoming[10];
  static struct psock ps;

  PROCESS_BEGIN();

  uart0_set_br(1000000);
  tcp_listen(UIP_HTONS(2020));

  while(1) {
    PROCESS_YIELD();
  }

  {
    /* wait for tcp connection */
    PROCESS_WAIT_EVENT_UNTIL(ev==tcpip_event && uip_connected());

    /* start estimator process and init psock connection handler */
    process_start(&ahrs_process, NULL);
    PSOCK_INIT(&ps,incoming,sizeof(incoming));

    /* loop until connection is closed */
    while (!(uip_aborted() || uip_closed() || uip_timedout())) {
      PROCESS_YIELD_UNTIL(ev==tcpip_event);
      handle_connection(&ps);
    }

    /* stop ahrs process */
    process_exit(&ahrs_process);

  }

  PROCESS_END();
}
Example #12
0
/*---------------------------------------------------------------------------*/
void
telnetd_appcall(void *ts)
{
  if(uip_connected()) {
    tcp_markconn(uip_conn, &s);
    buf_init(&buf);
    s.bufptr = 0;
    s.state = STATE_NORMAL;
    shell_start();
  }

  if(s.state == STATE_CLOSE) {
    s.state = STATE_NORMAL;
    uip_close();
    return;
  }
  if(uip_closed() ||
     uip_aborted() ||
     uip_timedout()) {
    closed();
  }
  if(uip_acked()) {
    acked();
  }
  if(uip_newdata()) {
    newdata();
  }
  if(uip_rexmit() ||
     uip_newdata() ||
     uip_acked() ||
     uip_connected() ||
     uip_poll()) {
    senddata();
  }
}
Example #13
0
/*-----------------------------------------------------------------------------------*/
void
ctk_vncserver_appcall(void *state)
{
  static struct vnc_server_state *vs;

  vs = (struct vnc_server_state *)(state);

  if(uip_connected()) {

    /* Since we've just been connected, the state pointer should be
       NULL and we need to allocate a new state object. If we have run
       out of memory for state objects, we'll have to abort the
       connection and return. */
    if(vs == NULL) {
      vs = alloc_state();
      if(vs == NULL) {
	uip_close();
	return;
      }
      tcp_markconn(uip_conn, (void *)vs);
    }
  } else if(uip_closed() || uip_aborted()) {
    if(vs != NULL) {
      dealloc_state(vs);
    }
    return;
  }
  vnc_server_appcall(vs);
}
Example #14
0
/*---------------------------------------------------------------------*/
PROCESS_THREAD(tcp_loader_process, ev, data)
{
  PROCESS_BEGIN();
  
  tcp_listen(HTONS(CODEPROP_DATA_PORT));
  
  while(1) {
    PROCESS_YIELD();
    if(ev == tcpip_event && uip_conn->lport == HTONS(CODEPROP_DATA_PORT)) {
      if(uip_connected()) {	/* Really uip_connecting()!!! */
	if(data == NULL) {
	  PT_INIT(&s.tcpthread_pt);
	  process_poll(&tcp_loader_process);
	  tcp_markconn(uip_conn, &s);
	} else {
	  PRINTF(("codeprop: uip_connected() and data != NULL\n"));
	  uip_abort();
	}
      }
      recv_tcpthread(&s.tcpthread_pt); /* Run thread */

      if(uip_closed() || uip_aborted() || uip_timedout()) {
	PRINTF(("codeprop: connection down\n"));
	tcp_markconn(uip_conn, NULL);
      }
    }
  }

  PROCESS_END();
}
Example #15
0
void uip_modbus_appcall(void)
{
    if(uip_connected())
    {
        PRINTF("connected!\r\n");
    }

    if(uip_closed())
    {
        PRINTF("closed\r\n");
    }

    if(uip_newdata())
    {
        PRINTF("request!\r\n");
        // 获得modbus请求
        memcpy(ucTCPRequestFrame, uip_appdata, uip_len );
        ucTCPRequestLen = uip_len;
        // 向 modbus poll发送消息
        xMBPortEventPost( EV_FRAME_RECEIVED );
    }

    if(uip_poll())
    {
        if(bFrameSent)
        {
            bFrameSent = FALSE;
            // uIP发送Modbus应答数据包
            uip_send( ucTCPResponseFrame , ucTCPResponseLen );
        }
    }
}
Example #16
0
/* -------------------------------------------------------------------------- */
PROCESS_THREAD(example_psock_server_process, ev, data)
{
    // The Start...
    PROCESS_BEGIN();

    // So, here we configure the network listening at a TCP port.
    tcp_listen(UIP_HTONS(12345));

    while (1) {
        // Wait a TCP / IP event...
        PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);

        // If someone has connected, let's init the connection
        if (uip_connected()) {
            // Bind the buffer to the socket
            PSOCK_INIT(&socket, buffer, sizeof(buffer));

            // So now, let's keep the connection open waiting for something
            while (!(uip_aborted() || uip_closed() || uip_timedout())) {
                // Wait for an TCIP / IP event
                PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);

                // And then, let's send to handle the data
                handle_connection(&socket);
            }
        }
    }

    // ... the End
    PROCESS_END();
}
static void contiki_appcall(void *data)
{
	contiki_data_t *s = (contiki_data_t *)data;

	if (uip_closed() || uip_aborted() || uip_timedout()) {
		//xprintf("closed or aborted or timedout\n");
		if (s->state == READING || s->state == WRITTING) {
			s->state = ERROR;
		} else {
			s->state = CLOSED;
		}
		process_post_synch(s->process, xively_event, s);
	} else if (uip_connected()) {
		s->state = CONNECTED;
		PSOCK_INIT(&s->p, NULL, 0);
		process_post_synch(s->process, xively_event, s);
	} else if (uip_newdata()) {
		if (s->state == READING) {
			s->state = READ_END;
			process_post_synch(s->process, xively_event, s);
		}
	}
	if (s->state == CLOSING) {
		uip_close();
	}
	handle_output(s);
}
Example #18
0
void
httpd_main(void)
{
  if (uip_aborted() || uip_timedout())
  {
    printf("httpd: connection aborted\n");
    httpd_cleanup();
    return;
  }

  if (uip_closed())
  {
    printf("httpd: connection closed\n");
    httpd_cleanup();
    return;
  }

  if (uip_connected())
  {
    printf("httpd: new connection\n");

    /* initialize struct */
    STATE->handler = NULL;
    STATE->header_acked = 0;
    STATE->eof = 0;
    STATE->header_reparse = 0;
#ifdef HTTPD_AUTH_SUPPORT
    STATE->auth_state = PAM_UNKOWN;
#endif
  }

  if (uip_newdata() && (!STATE->handler || STATE->header_reparse))
  {
    printf("httpd: new data\n");
    httpd_handle_input();
  }

#ifdef HTTPD_AUTH_SUPPORT
  if (STATE->auth_state == PAM_DENIED && STATE->handler != httpd_handle_401)
  {
    httpd_cleanup();
    STATE->handler = httpd_handle_401;
    STATE->header_reparse = 0;
    printf("httpd: auth failed\n");
  }
  else if (STATE->auth_state == PAM_PENDING)
    return;                     /* Waiting for the PAM Layer */
#endif


  if (uip_rexmit() ||
      uip_newdata() || uip_acked() || uip_poll() || uip_connected())
  {

    /* Call associated handler, if set already. */
    if (STATE->handler && (!STATE->header_reparse))
      STATE->handler();
  }
}
Example #19
0
static void
irc_main(void)
{
    if (uip_aborted() || uip_timedout()) {
	IRCDEBUG ("connection aborted\n");
	irc_conn = NULL;
    }

    if (uip_closed()) {
	IRCDEBUG ("connection closed\n");
	irc_conn = NULL;
    }

    if (uip_connected()) {
	IRCDEBUG ("new connection\n");
	STATE->stage = IRC_SEND_USERNICK;
	STATE->sent = IRC_SEND_INIT;
#ifdef ECMD_IRC_SUPPORT
	STATE->reparse = 0;
#endif
	*STATE->outbuf = 0;
    }

    if (STATE->stage == IRC_SEND_JOIN
	&& uip_acked ())
	STATE->stage ++;

    else if (STATE->stage == IRC_CONNECTED
	     && uip_acked ()) {
	*STATE->outbuf = 0;

#ifdef ECMD_IRC_SUPPORT
	if (STATE->reparse)
	    irc_handle_ecmd ();
#endif
    }

    else if (uip_newdata() && uip_len) {
	((char *) uip_appdata)[uip_len] = 0;
	IRCDEBUG ("received data: %s\n", uip_appdata);

	if (irc_parse ()) {
	    uip_close ();	/* Parse error */
	    return;
	}
    }

    if (uip_rexmit ())
	irc_send_data (STATE->sent);

    else if ((STATE->stage > STATE->sent || STATE->stage == IRC_CONNECTED)
	     && (uip_newdata()
		 || uip_acked()
		 || uip_connected()))
	irc_send_data (STATE->stage);

    else if (STATE->stage == IRC_CONNECTED && uip_poll() && *STATE->outbuf)
	irc_send_data (STATE->stage);
}
Example #20
0
/*-----------------------------------------------------------------------------------*/
void
webclient_appcall(void)
{
  if(uip_connected()) {
    s.timer = 0;
    s.state = WEBCLIENT_STATE_STATUSLINE;
    senddata();
    webclient_connected();
    return;
  }

  if(s.state == WEBCLIENT_STATE_CLOSE) {
    webclient_closed();
    uip_abort();
    return;
  }

  if(uip_aborted()) {
    webclient_aborted();
  }
  if(uip_timedout()) {
    webclient_timedout();
  }

  
  if(uip_acked()) {
    s.timer = 0;
    acked();
  }
  if(uip_newdata()) {
    s.timer = 0;
    newdata();
  }
  if(uip_rexmit() ||
     uip_newdata() ||
     uip_acked()) {
    senddata();
  } else if(uip_poll()) {
    ++s.timer;
    if(s.timer == WEBCLIENT_TIMEOUT) {
      webclient_timedout();
      uip_abort();
      return;
    }
        /*    senddata();*/
  }

  if(uip_closed()) {
    if(s.httpflag != HTTPFLAG_MOVED) {
      /* Send NULL data to signal EOF. */
      webclient_datahandler(NULL, 0);
    } else {
      if(resolv_lookup(s.host) == NULL) {
	resolv_query(s.host);
      }
      webclient_get(s.host, s.port, s.file);
    }
  }
}
Example #21
0
void yport_net_main(void)
{
  if(uip_connected()) {
    if (yport_conn == NULL) {
      yport_conn = uip_conn;
      uip_conn->wnd = YPORT_BUFFER_LEN - 1;
    }
    else 
      /* if we have already an connection, send an error */
      uip_send("ERROR: Connection blocked\n", 27);
  } else if (uip_acked()) {
    /* If the peer is not our connection, close it */
    if (yport_conn != uip_conn) 
      uip_close();
    else {
      /* Some data we have sent was acked, jipphie */
      /* disable interrupts */
      uint8_t sreg = SREG; cli();
      yport_recv_buffer.len -= yport_recv_buffer.sent;
      /* We should use memmove, because the data may overlap */
      memmove(yport_recv_buffer.data, 
              yport_recv_buffer.data + yport_recv_buffer.sent,
              yport_recv_buffer.len);
      /* enable interrupts again */
      SREG = sreg;
    }
  } else if (uip_closed() || uip_aborted() || uip_timedout()) {
    /* if the closed connection was our connection, clean yport_conn */
    if (yport_conn == uip_conn)
      yport_conn = NULL;
  } else if (uip_newdata()) {
    if (uip_len <= YPORT_BUFFER_LEN && yport_rxstart(uip_appdata, uip_len) != 0) {
      /* Prevent the other side from sending more data */
      uip_stop();
    }
  } 
  if (uip_poll() 
      && uip_conn == yport_conn 
      && uip_stopped(yport_conn)
      && yport_send_buffer.sent == yport_send_buffer.len)
    uip_restart();
  /* Send data */
  if ((uip_poll() 
       || uip_acked()
       || uip_rexmit())
      && yport_conn == uip_conn 
      && yport_recv_buffer.len > 0) {
    /* We have recieved data, lets propagade it */
    /* disable interrupts */
    uint8_t sreg = SREG; cli();
    /* Send the data */
    uip_send(yport_recv_buffer.data, yport_recv_buffer.len);
    /* so many data was send */
    yport_recv_buffer.sent = yport_recv_buffer.len;
    /* enable interrupts again */
    SREG = sreg;
  }
}
Example #22
0
/*---------------------------------------------------------------------*/
static
PT_THREAD(recv_tcpthread(struct pt *pt))
{
  PT_BEGIN(pt);

  /* Read the header. */
  PT_WAIT_UNTIL(pt, uip_newdata() && uip_datalen() > 0);

  if(uip_datalen() < sizeof(struct codeprop_tcphdr)) {
    PRINTF(("codeprop: header not found in first tcp segment\n"));
    uip_abort();
    goto thread_done;
  }

  s.len = uip_htons(((struct codeprop_tcphdr *)uip_appdata)->len);
  s.addr = 0;
  uip_appdata += sizeof(struct codeprop_tcphdr);
  uip_len -= sizeof(struct codeprop_tcphdr);

  xmem_erase(XMEM_ERASE_UNIT_SIZE, EEPROMFS_ADDR_CODEPROP);

  /* Read the rest of the data. */
  do {      
    if(uip_len > 0) {
      xmem_pwrite(uip_appdata, uip_len, EEPROMFS_ADDR_CODEPROP + s.addr);
      s.addr += uip_len;
    }
    if(s.addr < s.len) {
      PT_YIELD_UNTIL(pt, uip_newdata());
    }	
  } while(s.addr < s.len);

  /* Kill old program. */
  elfloader_unload();

  /* Link, load, and start new program. */
  int s;
  static char msg[30 + 10];
  s = elfloader_load(EEPROMFS_ADDR_CODEPROP);
  if (s == ELFLOADER_OK)
    sprintf(msg, "ok\n");
  else
    sprintf(msg, "err %d %s\n", s, elfloader_unknown);
    
  /* Return "ok" message. */
  do {
    s = strlen(msg);
    uip_send(msg, s);
    PT_WAIT_UNTIL(pt, uip_acked() || uip_rexmit() || uip_closed());
  } while(uip_rexmit());

  /* Close the connection. */
  uip_close();
    
 thread_done:;
  PT_END(pt);
}
Example #23
0
/*---------------------------------------------------------------------------*/
void
telnetd_appcall(void *ts)
{
  if(uip_connected()) {
    if(!connected) {
      buf_init(&buf);
      s.bufptr = 0;
      s.state = STATE_NORMAL;
      connected = 1;
      shell_start();
      timer_set(&s.silence_timer, MAX_SILENCE_TIME);
      ts = (char *)0;
    } else {
      uip_send(telnetd_reject_text, strlen(telnetd_reject_text));
      ts = (char *)1;
    }
    tcp_markconn(uip_conn, ts);
  }

  if(!ts) {
    if(s.state == STATE_CLOSE) {
      s.state = STATE_NORMAL;
      uip_close();
      return;
    }
    if(uip_closed() ||
       uip_aborted() ||
       uip_timedout()) {
      shell_stop();
      connected = 0;
    }
    if(uip_acked()) {
      timer_set(&s.silence_timer, MAX_SILENCE_TIME);
      acked();
    }
    if(uip_newdata()) {
      timer_set(&s.silence_timer, MAX_SILENCE_TIME);
      newdata();
    }
    if(uip_rexmit() ||
       uip_newdata() ||
       uip_acked() ||
       uip_connected() ||
       uip_poll()) {
      senddata();
      if(s.numsent > 0) {
	timer_set(&s.silence_timer, MAX_SILENCE_TIME);
      }
    }
    if(uip_poll()) {
      if(timer_expired(&s.silence_timer)) {
        uip_close();
        tcp_markconn(uip_conn, NULL);
      }
    }
  }
}
Example #24
0
static void
jabber_main(void)
{
  if (uip_aborted() || uip_timedout())
  {
    JABDEBUG("connection aborted\n");
    jabber_conn = NULL;
  }

  if (uip_closed())
  {
    JABDEBUG("connection closed\n");
    jabber_conn = NULL;
  }

  if (uip_connected())
  {
    JABDEBUG("new connection\n");
    STATE->stage = JABBER_OPEN_STREAM;
    STATE->sent = JABBER_INIT;

#ifdef JABBER_STARTUP_MESSAGE_SUPPORT
    strncpy_P(STATE->target, PSTR(CONF_JABBER_BUDDY), sizeof(STATE->target));
    strncpy_P(STATE->outbuf, jabber_startup_text, sizeof(STATE->outbuf));
    STATE->action = JABBER_ACTION_MESSAGE;
#endif /* JABBER_STARTUP_MESSAGE_SUPPORT */
  }

  if (uip_acked() && STATE->stage == JABBER_CONNECTED)
  {
    STATE->action = JABBER_ACTION_NONE;
    *STATE->outbuf = 0;
  }

  if (uip_newdata() && uip_len)
  {
    /* Zero-terminate */
    ((char *) uip_appdata)[uip_len] = 0;
    JABDEBUG("received data: %s\n", uip_appdata);

    if (jabber_parse())
    {
      uip_close();              /* Parse error */
      return;
    }
  }

  if (uip_rexmit())
    jabber_send_data(STATE->stage, STATE->action);

  else if ((STATE->stage > STATE->sent || STATE->stage == JABBER_CONNECTED)
           && (uip_newdata() || uip_acked() || uip_connected()))
    jabber_send_data(STATE->stage, STATE->action);
  else if (STATE->stage == JABBER_CONNECTED && uip_poll() && STATE->action)
    jabber_send_data(STATE->stage, STATE->action);

}
Example #25
0
/*---------------------------------------------------------------------------*/
static void
appcall(void *state)
{
  if(uip_closed() || uip_aborted() || uip_timedout()) {
    ctkmode();
  } else if(uip_connected()) {
  } else {
    handle_input();
    handle_output();   
  }
}
Example #26
0
/*---------------------------------------------------------------------------*/
void
httpd_appcall(void *state)
{
#if DEBUGLOGIC
  struct httpd_state *s;   //Enter here for debugging with output directed to TCPBUF
  s = sg = (struct httpd_state *)memb_alloc(&conns);
  if (1) {
#else
  struct httpd_state *s = (struct httpd_state *)state;
  if(uip_closed() || uip_aborted() || uip_timedout()) {
    if(s != NULL) {
      memb_free(&conns, s);
    }
  } else if(uip_connected()) {
    s = (struct httpd_state *)memb_alloc(&conns);
    if(s == NULL) {
      uip_abort();
      return;
    }
#endif
    tcp_markconn(uip_conn, s);
    PSOCK_INIT(&s->sin, (uint8_t *)s->inputbuf, sizeof(s->inputbuf) - 1);
    PSOCK_INIT(&s->sout, (uint8_t *)s->inputbuf, sizeof(s->inputbuf) - 1);
    PT_INIT(&s->outputpt);
    s->state = STATE_WAITING;
    /*    timer_set(&s->timer, CLOCK_SECOND * 100);*/
    s->timer = 0;
    handle_connection(s);
  } else if(s != NULL) {
    if(uip_poll()) {
      ++s->timer;
      if(s->timer >= 20) {
        uip_abort();
        memb_free(&conns, s);
      }
    } else {
      s->timer = 0;
    }
    handle_connection(s);
  } else {
    uip_abort();
  }
}
/*---------------------------------------------------------------------------*/
void
httpd_init(void)
{
  tcp_listen(UIP_HTONS(80));
  memb_init(&conns);
  httpd_cgi_init();
}
Example #27
0
/******************************************************************************
* void exosite_appcall(void)
*
* This function is uIP's application function.  It is called whenever a uIP 
* event occurs (e.g. when a new connection is established, new data arrives, 
* sent data is acknowledged, data needs to be retransmitted, etc.).
* 
******************************************************************************/ 
void exosite_appcall(void)
{
  if(uip_connected()) {
    s.timer = 0;
    senddata();
    return;
  }

  if(s.state == EXO_STATE_CLOSE) {
    uip_abort();
    return;
  }
  
  if(uip_aborted()) {
  }
  if(uip_timedout()) {
  }

  if(uip_acked()) {
    s.timer = 0;
    acked();
  }

  if(uip_newdata()) {
    s.timer = 0;
    newdata();
  }
 
  if(uip_rexmit() || uip_newdata() || uip_acked()) {
    senddata();
  } else if(uip_poll()) {
    ++s.timer;
    if(s.timer == EXO_TIMEOUT) {
      uip_abort();
      return;
    }
  }
#if 0  
  if(uip_closed()) {
    if(s.httpflag != HTTPFLAG_MOVED) {
      memset(s.rxdata,0,sizeof(s.rxdata));
    } else {
      if(resolv_lookup(s.host) == NULL) {
	      resolv_query(s.host);
      }
    }
  }
#endif

}
Example #28
0
/*---------------------------------------------------------------------------*/
void
smtp_appcall(void)
{
  if(uip_closed()) {
    s.connected = 0;
    return;
  }
  if(uip_aborted() || uip_timedout()) {
    s.connected = 0;
    smtp_done(1);
    return;
  }
  smtp_thread();
}
Example #29
0
static void
mysql_main(void)
{
    if (uip_aborted() || uip_timedout()) {
	MYDEBUG ("connection aborted\n");
	mysql_conn = NULL;
    }

    if (uip_closed()) {
	MYDEBUG ("connection closed\n");
	mysql_conn = NULL;
    }

    if (uip_connected()) {
	MYDEBUG ("new connection\n");
	STATE->stage = MYSQL_WAIT_GREETING;
	STATE->sent = MYSQL_WAIT_GREETING;
	STATE->packetid = 0;
    }

    if (uip_newdata() && uip_len) {
#ifdef DEBUG_MYSQL
	MYDEBUG ("received data: %s\n", uip_appdata);
	for (uint16_t i = 0; i < uip_len; i ++)
	    debug_putchar (((unsigned char *) uip_appdata)[i]);
	debug_putchar (10);
#endif

	if (mysql_parse ()) {
	    uip_close ();		/* Parse error */
	    return;
	}
    }

    if (uip_rexmit()) {
	STATE->packetid --;
	mysql_send_data (STATE->sent);
    }
    else if ((STATE->stage > STATE->sent || STATE->stage == MYSQL_CONNECTED)
	     && (uip_newdata()
		 || uip_acked()
		 || uip_connected()))
	mysql_send_data (STATE->stage);
    else if (STATE->stage == MYSQL_CONNECTED
	     && uip_poll() && *STATE->u.stmtbuf)
	mysql_send_data(STATE->stage);

}
Example #30
0
/*---------------------------------------------------------------------------*/
void
httpd_appcall(void *state)
{
  struct httpd_state *s = (struct httpd_state *)state;

  if(uip_closed() || uip_aborted() || uip_timedout()) {
    if(s != NULL) {
      if(s->fd >= 0) {
        cfs_close(s->fd);
	s->fd = -1;
      }
      memb_free(&conns, s);
    }
  } else if(uip_connected()) {
    s = (struct httpd_state *)memb_alloc(&conns);
    if(s == NULL) {
      uip_abort();
      webserver_log_file(&uip_conn->ripaddr, "reset (no memory block)");
      return;
    }
    tcp_markconn(uip_conn, s);
    PSOCK_INIT(&s->sin, (uint8_t *)s->inputbuf, sizeof(s->inputbuf) - 1);
    PSOCK_INIT(&s->sout, (uint8_t *)s->inputbuf, sizeof(s->inputbuf) - 1);
    PT_INIT(&s->outputpt);
    s->fd = -1;
    s->state = STATE_WAITING;
    timer_set(&s->timer, CLOCK_SECOND * 10);
    handle_connection(s);
  } else if(s != NULL) {
    if(uip_poll()) {
      if(timer_expired(&s->timer)) {
	uip_abort();
	if(s->fd >= 0) {
	  cfs_close(s->fd);
	  s->fd = -1;
	}
        memb_free(&conns, s);
        webserver_log_file(&uip_conn->ripaddr, "reset (timeout)");
      }
    } else {
      timer_restart(&s->timer);
    }
    handle_connection(s);
  } else {
    uip_abort();
  }
}