/********************************************************************************* This function handles and controls the xml socket. ********************************************************************************/ void My_Handler(My_Socket_Type *my_sock) { static char ipaddr[25],DispStrOut[100]; switch(my_sock->nextstate) { case 0:/*INITIALIZATION*/ // listen for incoming connection inet_ntoa( ipaddr, my_ip_addr ); sprintf(DispStrOut,"OFFLINE\nCONNECT TO\nIP: %s\nPORT: %d", ipaddr, MY_PORT); while(!btnMsgBox(40,0,240,90,&fi14x16,DispStrOut,1,1)); if(tcp_listen(&my_sock->sock,MY_PORT,INCOMING_IP,INCOMING_PORT,NULL,0)) { my_sock->rxbytes = my_sock->txbytes = 0; my_sock->statetime = 0L; (my_sock->nextstate)++; // init complete move onto next state my_sock->statetime = MS_TIMER+TCP_TIMEOUT; // reset the statetime } else exit(0); break; case 1://LISTEN// if(sock_established(&my_sock->sock)) // check for a connection { inet_ntoa(ipaddr,my_sock->sock.hisaddr); sprintf(DispStrOut,"CONNECTED\nIP: %s",ipaddr); while(!btnMsgBox(40,0,240,90,&fi14x16,DispStrOut,1,0)); //printf("%s\nConnection Established.\n",BLACK); (my_sock->statetime) = MS_TIMER+TCP_TIMEOUT; // reset statetime for RECEIVE state (my_sock->nextstate)++; // we have connection so move on } else if ((long)(MS_TIMER-(my_sock->statetime)) > 0) // if X sec and no sock my_sock->nextstate = 4; // abort and re-init break; case 2://RECEIVE// my_sock->nextstate = My_Rcv_Pkt(my_sock); // see function for details if ((long)(MS_TIMER-(my_sock->statetime)) > 0) // if X sec and still waiting my_sock->nextstate = 4; // abort and re-init break; case 3://SEND// my_sock->nextstate = My_Snd_Pkt(my_sock); // see function for details if ((long)(MS_TIMER-(my_sock->statetime)) > 0) // if X sec and still waiting my_sock->nextstate = 4; // abort and re-init break; case 4://NO WAIT_CLOSE// sock_abort(&my_sock->sock); // close the socket my_sock->nextstate = 0; // go back to the INIT state } if(!tcp_tick(&my_sock->sock)) my_sock->nextstate = 0; }
void main() { int state,status; state = 0; sock_init(); while(1) { switch(state) { case 0:/*INITIALIZATION*/ // listen for incoming connection if(!tcp_listen(&sock,MY_PORT,INCOMING_IP,INCOMING_PORT,NULL,0)) printf("%sERROR OPENNING SOCKET!\n",RED); else { printf("%swaiting for incomming session\n",GREEN); statetime = MS_TIMER+TIME_OUT; // setup the time to be in LISTEN state state++; // init complete move onto next state } break; case 1:/*LISTEN*/ if(sock_established(&sock)) // check for a connection { printf("%ssocket established\n",BLUE); statetime = MS_TIMER+TIME_OUT; // reset time to be in the RECEIVE state state++; // we have connection so move on } else if ((long)(MS_TIMER-statetime) > 0) // if X millisecs and still waiting state = 4; // abort and re-init break; case 2:/*RECEIVE*/ state = receive_packet(); // see function for details if ((long)(MS_TIMER-statetime) > 0) // if X millisecs and still waiting state = 4; // abort and re-init break; case 3:/*SEND*/ state = send_packet(); // see function for details if ((long)(MS_TIMER-statetime) > 0) // if X millisecs and still waiting state = 4; // abort and re-init break; case 4:/*NO WAIT_CLOSE*/ printf("%sABORT SOCKET\n",RED); sock_abort(&sock); // close the socket state = 0; // go back to the INIT state } status = tcp_tick(&sock); } }
/** * Keep the socket open inspite of receiving "ICMP Unreachable" * For UDP or TCP socket. */ void sock_sturdy (sock_type *s, int level) { s->tcp.rigid = level; if (s->tcp.rigid < s->tcp.stress) sock_abort (s); }
void main(void) { // index is used to loop through the interfaces int index; // Initialize the TCP/IP stack sock_init(); // Initialize the state machine structure for (index = 0; index <= VIRTUAL_ETH; index++) { socks[index].state = LSTN_STATE; socks[index].iface = 0; memset(socks[index].buff, 0, sizeof(socks[index].buff)); socks[index].bytes = 0; } // Perform network configuration on the main (physical) Ethernet ineterface printf("Bringing up Main Interface %2d:\n", socks[0].iface); ifconfig(IF_ETH0, IFS_IPADDR, aton(LOCAL_IP), IFS_NETMASK, aton(LOCAL_NETMASK), IFS_ROUTER_SET,aton(LOCAL_GATEWAY), IFS_UP, IFS_END); // Wait for the interface to come up while (ifpending(IF_ETH0) == IF_COMING_UP) { tcp_tick(NULL); } printf("Main Interface %2d: is up!!\n", socks[0].iface); // Configure each of the virtual Ethernet interfaces for (index = 1; index <= VIRTUAL_ETH; index++) { // virtual_eth() creates a new virtual Ethernet interface and returns // the new interface number socks[index].iface = virtual_eth(IF_ETH0, aton(LOCAL_IP) + index, aton(LOCAL_NETMASK), NULL); if (socks[index].iface != -1) { printf("Created Virtual Interface %2d:\n", socks[index].iface); } else { exit(0); } // Wait for the virtual Ethernet interface to come up while (ifpending(socks[index].iface) == IF_COMING_UP) { tcp_tick(NULL); } printf("Virtual Interface %2d: is up!!\n", socks[index].iface); } // Print out information on the interfaces ip_print_ifs(); // Begin the main program loop while (1) { // Iterate over the Ethernet interfaces for (index = 0; index <= VIRTUAL_ETH; index++) { switch (socks[index].state) { // Listen on the socket case LSTN_STATE: // Note that the iface number is passed to tcp_extlisten() if (tcp_extlisten(&socks[index].s, socks[index].iface, LOCAL_PORT, 0, 0, NULL, 0, 0, 0)) { socks[index].state = ESTB_STATE; socks[index].timer = SEC_TIMER + TIME2WAIT; // reset the timer printf("Interface %2d: listening on port: %5d\n", socks[index].iface, LOCAL_PORT); } else { // tcp_extlisten() failed--let the user know printf("Interface %2d: tcp_extlisten failed\n", socks[index].iface); socks[index].state = CLSE_STATE; socks[index].timer = SEC_TIMER + TIME2WAIT; // reset the timer } break; // Check if a connection has been established case ESTB_STATE: if (sock_established(&socks[index].s) || sock_bytesready(&socks[index].s) >= 0) { socks[index].state = RECV_STATE; socks[index].timer = SEC_TIMER + TIME2WAIT; // reset the timer printf("Interface %2d: socket established.\n", socks[index].iface); } break; // Check if data has been received. If so, read it out. case RECV_STATE: // Read any incoming data socks[index].bytes = sock_fastread(&socks[index].s, socks[index].buff, sizeof(socks[index].buff)); if (socks[index].bytes == -1) { // sock_fastread() returned an error--means that the socket is // likely closed printf("Interface %2d: sock_fastread failed\n", socks[index].iface); socks[index].state = CLSE_STATE; socks[index].timer = SEC_TIMER + TIME2WAIT; // reset the timer } // Check if we received any data if (socks[index].bytes > 0) { printf("Interface %2d: revd: %2d bytes\n", socks[index].iface, socks[index].bytes); socks[index].state = SEND_STATE; // send the data back socks[index].timer = SEC_TIMER + TIME2WAIT; // reset the timer } break; // Echo back any received data case SEND_STATE: socks[index].bytes = sock_fastwrite(&socks[index].s, socks[index].buff, socks[index].bytes); if (socks[index].bytes == -1) { // sock_fastwrite() returned an error--means that the socket // is likely closed printf("Interface %2d: sock_fastwrite failed\n", socks[index].iface); socks[index].state = CLSE_STATE; socks[index].timer = SEC_TIMER + TIME2WAIT; // reset the timer } // Check how much data was written. Note that in this program, // if not all the data was written, the remaining data will be // dropped. A more realistic program would try sending the rest // of the data later, or using sock_awrite() until the data can // be sent. if (socks[index].bytes > 0) { printf("Interface %2d: sent: %2d bytes\n", socks[index].iface, socks[index].bytes); socks[index].state = RECV_STATE; socks[index].timer = SEC_TIMER + TIME2WAIT; // reset the timer } break; // Close the socket case CLSE_STATE: sock_close(&socks[index].s); socks[index].state = CLWT_STATE; socks[index].timer = SEC_TIMER + TIME2WAIT; // reset the timer break; // Wait for the socket to completely close case CLWT_STATE: if (!sock_alive(&socks[index].s)) { printf("Interface %2d: socket closed.\n", socks[index].iface); socks[index].state = LSTN_STATE; socks[index].timer = SEC_TIMER + TIME2WAIT; // reset the timer } break; // Abort the socket--used only if a socket has timed out in one of // the closing states case ABRT_STATE: sock_abort(&socks[index].s); // abort the socket socks[index].state = LSTN_STATE; // try to listen again socks[index].timer = SEC_TIMER + TIME2WAIT; // reset the timer break; } // Drive the TCP/IP stack tcp_tick(NULL); // Check the timeout on this socket, and close or abort the socket // if necessary timed_out(index); } } }