int netvalue() { //int returnval = -1; int returnval; uint32 remoteip; dot2ip("192.168.1.100",&remoteip); /* remote IP address to use */ int i; /* index into buffer */ int retval; /* return value */ char msg[] = "Xinu waiting for value from Linux"; /* message to send */ char inbuf[1500]; /* buffer for incoming reply */ int32 slot; /* UDP slot to use */ int32 msglen; /* length of outgoing message */ uint16 echoport= 8888; /* port number for UDP echo */ uint16 locport = 52743; /* local port to use */ int32 retries = 6; /* number of retries */ int32 delay = 2000; /* reception delay in ms */ /* register local UDP port */ slot = udp_register(remoteip, echoport, locport); if (slot == SYSERR) { printf("could not reserve UDP port \n"); return 1; } /* Retry sending outgoing datagram and getting response */ msglen = strnlen(msg, 1200); for (i=0; i<retries; i++) { retval = udp_send(slot, msg, msglen+50); if (retval == SYSERR) { printf("error sending UDP \n"); return 1; } retval = udp_recv(slot, inbuf, sizeof(inbuf), delay); if (retval == TIMEOUT) { printf("timeout...\n"); continue; } else if (retval == SYSERR) { printf("error from udp_recv \n"); udp_release(slot); return 1; } break; } udp_release(slot); if (retval == TIMEOUT) { printf("retry limit exceeded\n"); return SYSERR; } returnval = atoi(inbuf); //printf("UDP echo test was successful\n"); printf("Received value is %d\n", returnval); return returnval; }
int netserver(void){ int32 delay = 5000; uint32 mask; uint32 rmtIp; int retries = 3; uint16 locport = 5444; uint16 remotePort = 5443; dot2ip("192.168.1.100",&mask); dot2ip("192.168.1.101",&rmtIp); int32 slot; int retval; int i; char msg[] = "Message From XINU"; int msglen; int val; msglen = string_length(msg); slot = udp_register(mask, remotePort, locport); if(slot == SYSERR){ printf("Error getting UDP slot.\n"); return SYSERR; } for (i=0; i<retries; i++) { printf("Requesting value from linux.....\n"); udp_send(slot, msg, msglen+10); printf("Waiting for message from linux.\n"); retval = udp_recv(slot, msg, sizeof(msg), delay); if (retval == TIMEOUT) { continue; } else if (retval == SYSERR) { udp_release(slot); return 1; }else{ break; } } printf("Received message %s", msg); val = atoi(msg); udp_release(slot); return val; }
void termination_handler(int sig) { PRINT_DEBUG("**********Terminating *******"); //shutdown all module threads in backwards order of startup //rtm_shutdown(); udp_shutdown(); tcp_shutdown(); icmp_shutdown(); ipv4_shutdown(); arp_shutdown(); interface_shutdown(); //TODO finish daemon_shutdown(); //TODO finish switch_shutdown(); //TODO finish //have each module free data & que/sem //TODO finish each of these //rtm_release(); udp_release(); tcp_release(); icmp_release(); ipv4_release(); arp_release(); interface_release(); daemon_release(); switch_release(); PRINT_DEBUG("FIN"); exit(-1); }
shellcmd xsh_chat(int nargs, char *args[]) { char vmip[]="192.168.1.100"; char message[50]; char message1[50]; uint32 vmipnew; uint32 server,client; uint32 rsend,rrecv,rip; uint16 rport; int i; if (nargs == 2 && strncmp(args[1], "--help", 7) == 0) { printf("Usage: %s <name>\n\n", args[0]); printf("Description:\n"); printf("\tInitiates a Chat Conversation with \n"); printf("Options (one per invocation):\n"); printf("\t--help\tdisplay this help and exit\n"); return 0; } else if (nargs == 2) { dot2ip(vmip,&vmipnew); server=udp_register(0,0,1026); client=udp_register(vmipnew,1025,1026); printf("XINU :%s",args[1]); udp_send(client,args[1],strlen(message)); while(1) { rrecv=udp_recv(server,message1,50,10000); if(rport==1026) { rsend=udp_send(client,"Bye",strlen(message)); break; } else if(rrecv==SYSERR) { //printf("error"); exit(); break; } else if(rrecv==TIMEOUT) { printf("Timeout"); break; } else { printf("\VM :"); for(i=0;i<strlen(message1);i++) { printf("%c",message1[i]); } break; } } printf("\n"); udp_release(server); udp_release(client); return 0; }
/*------------------------------------------------------------------------ * xsh_udpecho - shell command that can send a message to a remote UDP * echo server and receive a reply *------------------------------------------------------------------------ */ shellcmd xsh_udpecho(int nargs, char *args[]) { int i; /* index into buffer */ int retval; /* return value */ char msg[] = "Xinu testing UDP echo"; /* message to send */ char inbuf[1500]; /* buffer for incoming reply */ int32 msglen; /* length of outgoing message */ uint32 remoteip; /* remote IP address to use */ uint32 localip; /* local IP address to use */ uint16 echoport= 7; /* port number for UDP echo */ uint16 locport = 52743; /* local port to use */ int32 retries = 3; /* number of retries */ int32 delay = 2000; /* reception delay in ms */ /* For argument '--help', emit help about the 'udpecho' command */ if (nargs == 2 && strncmp(args[1], "--help", 7) == 0) { printf("Use: %s REMOTEIP\n\n", args[0]); printf("Description:\n"); printf("\tBounce a message off a remote UDP echo server\n"); printf("Options:\n"); printf("\tREMOTEIP:\tIP address in dotted decimal\n"); printf("\t--help\t display this help and exit\n"); return 0; } /* Check for valid IP address argument */ if (nargs != 2) { fprintf(stderr, "%s: invalid argument(s)\n", args[0]); fprintf(stderr, "Try '%s --help' for more information\n", args[0]); return 1; } if (dot2ip(args[1], &remoteip) == SYSERR) { fprintf(stderr, "%s: invalid IP address argument\r\n", args[0]); return 1; } localip = getlocalip(); if (localip == SYSERR) { fprintf(stderr, "%s: could not obtain a local IP address\n", args[0]); return 1; } /* register local UDP port */ retval = udp_register(remoteip, echoport, locport); if (retval == SYSERR) { fprintf(stderr, "%s: could not reserve UDP port %d\n", args[0], locport); return 1; } /* Retry sending outgoing datagram and getting response */ for (i=0; i<retries; i++) { msglen = strnlen(msg, 1500); retval = udp_send(remoteip, echoport, localip, locport, msg, msglen); if (retval == SYSERR) { fprintf(stderr, "%s: error sending UDP \n", args[0]); return 1; } retval = udp_recv(remoteip, echoport, locport, inbuf, sizeof(inbuf), delay); if (retval == TIMEOUT) { fprintf(stderr, "%s: timeout...\n", args[0]); continue; } else if (retval == SYSERR) { fprintf(stderr, "%s: error from udp_recv \n", args[0]); udp_release(remoteip, echoport, locport); return 1; } break; } udp_release(remoteip, echoport, locport); if (retval == TIMEOUT) { fprintf(stderr, "%s: retry limit exceeded\n", args[0]); return 1; } /* Response received - check contents */ if (retval != msglen) { fprintf(stderr, "%s: sent %d bytes and received %d\n", args[0], msglen, retval); return 1; } for (i = 0; i < msglen; i++) { if (msg[i] != inbuf[i]) { fprintf(stderr, "%s: reply differs at byte %d\n", args[0], i); return 1; } } printf("UDP echo test was successful\n"); return 0; }
/*------------------------------------------------------------------------ * getlocalip - use DHCP to obtain an IP address *------------------------------------------------------------------------ */ uint32 getlocalip(void) { struct dhcpmsg dmsg; /* holds outgoing DHCP discover */ /* message */ struct dhcpmsg dmsg2; /* holds incoming DHCP offer */ /* and outgoing request message */ uint32 xid; /* xid used for the exchange */ int32 i; /* retry counter */ int32 len; /* length of data read */ char *optptr; /* pointer to options area */ char *eop; /* address of end of packet */ int32 msgtype; /* type of DCHP message */ uint32 addrmask; /* address mask for network */ uint32 routeraddr; /* default router address */ if (NetData.ipvalid) { /* already have an IP address */ return NetData.ipaddr; } udp_register(0, UDP_DHCP_SPORT, UDP_DHCP_CPORT); memcpy(&xid, NetData.ethaddr, 4); /* use 4 bytes from MAC as XID */ /* handcraft a DHCP Discover message in dmsg */ dmsg.dc_bop = 0x01; /* Outgoing request */ dmsg.dc_htype = 0x01; /* hardware type is Ethernet */ dmsg.dc_hlen = 0x06; /* hardware address length */ dmsg.dc_hops = 0x00; /* Hop count */ dmsg.dc_xid = xid; /* xid (unique ID) */ dmsg.dc_secs = 0x0000; /* seconds */ dmsg.dc_flags = 0x0000; /* flags */ dmsg.dc_cip = 0x00000000; /* Client IP address */ dmsg.dc_yip = 0x00000000; /* Your IP address */ dmsg.dc_sip = 0x00000000; /* Server IP address */ dmsg.dc_gip = 0x00000000; /* Gateway IP address */ memset(&dmsg.dc_chaddr,'\0',16);/* Client hardware address */ memcpy(&dmsg.dc_chaddr, NetData.ethaddr, ETH_ADDR_LEN); memset(&dmsg.dc_bootp,'\0',192);/* zero the bootp area */ dmsg.dc_cookie = 0x63825363; /* Magic cookie for DHCP */ dmsg.dc_opt[0] = 0xff & 53; /* DHCP message type option */ dmsg.dc_opt[1] = 0xff & 1; /* option length */ dmsg.dc_opt[2] = 0xff & 1; /* DHCP Dicover message */ dmsg.dc_opt[3] = 0xff & 0; /* Options padding */ dmsg.dc_opt[4] = 0xff & 55; /* DHCP parameter request list */ dmsg.dc_opt[5] = 0xff & 2; /* option length */ dmsg.dc_opt[6] = 0xff & 1; /* request subnet mask */ dmsg.dc_opt[7] = 0xff & 3; /* request default router addr. */ dmsg.dc_opt[8] = 0xff & 0; /* options padding */ dmsg.dc_opt[9] = 0xff & 0; /* options padding */ dmsg.dc_opt[10]= 0xff & 0; /* options padding */ dmsg.dc_opt[11]= 0xff & 0; /* options padding */ len = (char *)&dmsg.dc_opt[11] - (char *)&dmsg + 1; udp_send(IP_BCAST, UDP_DHCP_SPORT, IP_THIS, UDP_DHCP_CPORT, (char *)&dmsg, len); /* Read 3 incoming DHCP messages and check for an offer or */ /* wait for three timeout periods if no message arrives. */ for (i=0; i<3; i++) { if ((len=udp_recv(0, UDP_DHCP_SPORT, UDP_DHCP_CPORT, (char *)&dmsg2, sizeof(struct dhcpmsg), 3000)) == TIMEOUT) { continue; } /* Check that incoming message is a valid response (ID */ /* matches our request) */ if ( (dmsg2.dc_xid != xid) ) { continue; } eop = (char *)&dmsg2 + len - 1; optptr = (char *)&dmsg2.dc_opt; msgtype = addrmask = routeraddr = 0; while (optptr < eop) { switch (*optptr) { case 53: /* message type */ msgtype = 0xff & *(optptr+2); break; case 1: /* subnet mask */ memcpy(&addrmask, optptr+2, 4); break; case 3: /* router address */ memcpy(&routeraddr, optptr+2, 4); break; } optptr++; /* move to length octet */ optptr += (0xff & *optptr) + 1; } if (msgtype == 0x02) { /* offer - send request */ dmsg2.dc_opt[0] = 0xff & 53; dmsg2.dc_opt[1] = 0xff & 1; dmsg2.dc_opt[2] = 0xff & 3; dmsg2.dc_bop = 0x01; udp_send(IP_BCAST, UDP_DHCP_SPORT, IP_THIS, UDP_DHCP_CPORT, (char *)&dmsg2, sizeof(struct dhcpmsg) - 4); } else if (dmsg2.dc_opt[2] != 0x05) { /* if not an ack*/ continue; /* skip it */ } if (addrmask != 0) { NetData.addrmask = addrmask; } if (routeraddr != 0) { NetData.routeraddr = routeraddr; } NetData.ipaddr = dmsg2.dc_yip; NetData.ipvalid = TRUE; udp_release(0, UDP_DHCP_SPORT, UDP_DHCP_CPORT); return NetData.ipaddr; } kprintf("DHCP failed to get response\r\n"); udp_release(0, UDP_DHCP_SPORT, UDP_DHCP_CPORT); return (uint32)SYSERR; }
/*------------------------------------------------------------------------ * getlocalip - use DHCP to obtain an IP address *------------------------------------------------------------------------ */ uint32 getlocalip(void) { int32 slot; /* UDP slot to use */ struct dhcpmsg dmsg; /* holds outgoing DHCP discover */ /* message */ struct dhcpmsg dmsg2; /* holds incoming DHCP offer */ /* and outgoing request message */ uint32 xid; /* xid used for the exchange */ int32 i, j; /* retry counters */ int32 len; /* length of data sent */ int32 inlen; /* length of data received */ char *optptr; /* pointer to options area */ char *eop; /* address of end of packet */ int32 msgtype; /* type of DCHP message */ uint32 addrmask; /* address mask for network */ uint32 routeraddr; /* default router address */ uint32 tmp; /* used for byte conversion */ struct ifentry *ifptr; /* ptr to interface */ /* Select interface and obtain a pointer to the table entry */ ifptr = &if_tab[0]; /* Use interface 0 for DHCP */ if (ifptr->if_ipvalid) { /* already have an IP address */ return ifptr->if_ipucast; } slot = udp_register(0, 0, UDP_DHCP_SPORT, UDP_DHCP_CPORT); if (slot == SYSERR) { kprintf("getlocalip: cannot register with UDP\r\n"); return SYSERR; } memcpy(&xid, ifptr->if_macucast, 4); /* use 4 bytes from MAC as XID */ /* handcraft a DHCP Discover message in dmsg */ dmsg.dc_bop = 0x01; /* Outgoing request */ dmsg.dc_htype = 0x01; /* hardware type is Ethernet */ dmsg.dc_hlen = 0x06; /* hardware address length */ dmsg.dc_hops = 0x00; /* Hop count */ dmsg.dc_xid = htonl(xid); /* xid (unique ID) */ dmsg.dc_secs = 0x0000; /* seconds */ dmsg.dc_flags = 0x0000; /* flags */ dmsg.dc_cip = 0x00000000; /* Client IP address */ dmsg.dc_yip = 0x00000000; /* Your IP address */ dmsg.dc_sip = 0x00000000; /* Server IP address */ dmsg.dc_gip = 0x00000000; /* Gateway IP address */ memset(&dmsg.dc_chaddr,'\0',16);/* Client hardware address */ memcpy(&dmsg.dc_chaddr, ifptr->if_macucast, ETH_ADDR_LEN); memset(&dmsg.dc_bootp,'\0',192);/* zero the bootp area */ dmsg.dc_cookie = htonl(0x63825363); /* Magic cookie for DHCP */ dmsg.dc_opt[0] = 0xff & 53; /* DHCP message type option */ dmsg.dc_opt[1] = 0xff & 1; /* option length */ dmsg.dc_opt[2] = 0xff & 1; /* DHCP Dicover message */ dmsg.dc_opt[3] = 0xff & 0; /* Options padding */ dmsg.dc_opt[4] = 0xff & 55; /* DHCP parameter request list */ dmsg.dc_opt[5] = 0xff & 2; /* option length */ dmsg.dc_opt[6] = 0xff & 1; /* request subnet mask */ dmsg.dc_opt[7] = 0xff & 3; /* request default router addr. */ dmsg.dc_opt[8] = 0xff & 0; /* options padding */ dmsg.dc_opt[9] = 0xff & 0; /* options padding */ dmsg.dc_opt[10]= 0xff & 0; /* options padding */ dmsg.dc_opt[11]= 0xff & 0; /* options padding */ len = (char *)&dmsg.dc_opt[11] - (char *)&dmsg + 1; for (i = 0; i < DHCP_RETRY; i++) { udp_sendto(slot, IP_BCAST, UDP_DHCP_SPORT, (char *)&dmsg, len); /* Read 3 incoming DHCP messages and check for an offer */ /* or wait for three timeout periods if no message */ /* arrives. */ for (j=0; j<3; j++) { inlen = udp_recv(slot, (char *)&dmsg2, sizeof(struct dhcpmsg),2000); if (inlen == TIMEOUT) { continue; } else if (inlen == SYSERR) { return SYSERR; } /* Check that incoming message is a valid */ /* response (ID matches our request) */ if ( (ntohl(dmsg2.dc_xid) != xid) ) { continue; } eop = (char *)&dmsg2 + inlen - 1; optptr = (char *)&dmsg2.dc_opt; msgtype = addrmask = routeraddr = 0; while (optptr < eop) { switch (*optptr) { case 53: /* message type */ msgtype = 0xff & *(optptr+2); break; case 1: /* subnet mask */ memcpy((void *)&tmp, optptr+2, 4); addrmask = ntohl(tmp); break; case 3: /* router address */ memcpy((void *)&tmp, optptr+2, 4); routeraddr = ntohl(tmp); break; } optptr++; /* move to length octet */ optptr += (0xff & *optptr) + 1; } if (msgtype == 0x02) { /* offer - send request */ dmsg2.dc_opt[0] = 0xff & 53; dmsg2.dc_opt[1] = 0xff & 1; dmsg2.dc_opt[2] = 0xff & 3; dmsg2.dc_bop = 0x01; udp_sendto(slot, IP_BCAST, UDP_DHCP_SPORT, (char *)&dmsg2, sizeof(struct dhcpmsg) - 4); } else if (dmsg2.dc_opt[2] != 0x05) { /* if not an ack skip it */ continue; } if (addrmask != 0) { ifptr->if_ipmask = addrmask; } if (routeraddr != 0) { ifptr->if_iprouter = routeraddr; } ifptr->if_ipucast = ntohl(dmsg2.dc_yip); ifptr->if_ipprefix = ifptr->if_ipucast & ifptr->if_ipmask; ifptr->if_ipbcast = ifptr->if_ipprefix | ~ifptr->if_ipmask; ifptr->if_ipvalid = TRUE; udp_release(slot); return ifptr->if_ipucast; } } kprintf("DHCP failed to get response\r\n"); udp_release(slot); return (uint32)SYSERR; }
int echo_client() { int i; int retval; char msg[] = "Hello"; char inbuf[1500]; int32 slot; int32 msglen; uint32 remoteip; uint16 echoport= 1025; uint16 locport = 1026; int32 retries = 3; int32 delay = 5000; char args[] = "192.168.1.100"; char prog[] = "udpclient"; udp_init(); if (dot2ip(args, &remoteip) == SYSERR) { fprintf(stderr, "%s: invalid IP address argument\r\n", prog); return 1; } slot = udp_register(remoteip, echoport, locport); if (slot == SYSERR) { fprintf(stderr, "%s: could not reserve UDP port %d\n", prog, locport); return 1; } msglen = strnlen(msg, 1200); for (i=0; i<retries; i++) { retval = udp_send(slot, msg, msglen); if (retval == SYSERR) { fprintf(stderr, "%s: error sending UDP \n",prog); return 1; } retval = udp_recv(slot, inbuf, sizeof(inbuf), delay); if (retval == TIMEOUT) { fprintf(stderr, "%s: timeout...\n", prog); continue; } else if (retval == SYSERR) { fprintf(stderr, "%s: error from udp_recv \n",prog); udp_release(slot); return 1; } break; } udp_release(slot); if (retval == TIMEOUT) { fprintf(stderr, "%s: retry limit exceeded\n",prog); return 1; } if (retval != msglen) { fprintf(stderr, "%s: sent %d bytes and received %d\n",prog, msglen, retval); return 1; } for (i = 0; i < msglen; i++) { if (msg[i] != inbuf[i]) { fprintf(stderr, "%s: reply differs at byte %d\n",prog, i); return 1; } } int msgnum = (int)msg; kprintf("\n %s \n",inbuf); return msgnum; }
/*------------------------------------------------------------------------ * getutime - Obtain time in seconds past Jan 1, 1970, UCT (GMT) *------------------------------------------------------------------------ */ status getutime( uint32 *timvar /* Location to store the result */ ) { uint32 now; /* Current time in xinu format */ int32 retval; /* Return value from call */ uid32 slot; /* Slot in UDP table */ struct ntp { /* Format of an NTP message */ byte livn; /* LI:2 VN:3 and mode:3 fields */ byte strat; /* Stratum */ byte poll; /* Poll interval */ byte precision; /* Precision */ uint32 rootdelay; /* Root delay */ uint32 rootdisp; /* Root dispersion */ uint32 refid; /* Reference identifier */ uint32 reftimestamp[2];/* Reference timestamp */ uint32 oritimestamp[2];/* Originate timestamp */ uint32 rectimestamp[2];/* Receive timestamp */ uint32 trntimestamp[2];/* Transmit timestamp */ } ntpmsg; if (Date.dt_bootvalid) { /* Return time from local info */ *timvar = Date.dt_boot + clktime; return OK; } /* Verify that we have obtained an IP address */ if (getlocalip() == SYSERR) { return SYSERR; } /* If the DHCP response did not contain an NTP server address */ /* use the default server */ if (NetData.ntpserver == 0) { if (dot2ip(TIMESERVER, &NetData.ntpserver) == SYSERR) { return SYSERR; } } /* Contact the time server to get the date and time */ slot = udp_register(NetData.ntpserver, TIMERPORT, TIMELPORT); if (slot == SYSERR) { fprintf(stderr,"getutime: cannot register a udp port %d\n", TIMERPORT); return SYSERR; } /* Send a request message to the NTP server */ memset((char *)&ntpmsg, 0x00, sizeof(ntpmsg)); ntpmsg.livn = 0x1b; /* Client request, protocol version 3 */ retval = udp_send(slot, (char *)&ntpmsg, sizeof(ntpmsg)); if (retval == SYSERR) { fprintf(stderr,"getutime: cannot send to the server\n"); udp_release(slot); return SYSERR; } /* Read the response from the NTP server */ retval = udp_recv(slot, (char *) &ntpmsg, sizeof(ntpmsg), TIMETIMEOUT); if ( (retval == SYSERR) || (retval == TIMEOUT) ) { udp_release(slot); return SYSERR; } udp_release(slot); /* Extract the seconds since Jan 1900 and convert */ now = ntim2xtim( ntohl(ntpmsg.trntimestamp[0]) ); Date.dt_boot = now - clktime; Date.dt_bootvalid = TRUE; *timvar = now; return OK; }