/* * tftp_server_init * @tftp_server: TFTP server instance to initialize. * @socket_address: Socket address at which TFTP server will be listening on. * @name: Name of the TFTP server. * This function will initialize TFTP server. */ void tftp_server_init(TFTP_SERVER *tftp_server, SOCKET_ADDRESS *socket_address, char *name) { SYS_LOG_FUNCTION_ENTRY(TFTPS); /* Clear the given server structure. */ memset(tftp_server, 0, sizeof(TFTP_SERVER)); /* Use buffered mode for this UDP port. */ tftp_server->port.console.fs.flags = FS_BUFFERED; /* As we will be using net condition to process data on this port so it * would not be okay to suspend for buffers. */ tftp_server->port.flags = UDP_FLAG_THR_BUFFERS; /* Register the UDP port. */ udp_register((FD)&tftp_server->port, name, socket_address); /* Get read condition for UDP port. */ fs_condition_get((FD)&tftp_server->port, &tftp_server->port_condition, &tftp_server->port_suspend, &tftp_server->port_fs_param, FS_BLOCK_READ); /* For now disable the timer. */ tftp_server->port_suspend.timeout_enabled = FALSE; tftp_server->port_suspend.priority = NET_SOCKET_PRIORITY; tftp_server->port_suspend.status = SUCCESS; /* Lets never block on this socket. */ tftp_server->port.console.fs.flags &= (uint16_t)~(FS_BLOCK); /* Add a networking condition for this UDP port. */ net_condition_add(tftp_server->port_condition, &tftp_server->port_suspend, &tftp_server_process, (void *)tftp_server); } /* tftp_server_init */
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; }
/*------------------------------------------------------------------------ * xsh_udpeserver - shell command that acts as a UDP echo server (is * usually run in background) *------------------------------------------------------------------------ */ shellcmd xsh_udpeserver(int nargs, char *args[]) { int32 retval; /* return value */ uint32 localip; /* local IP address */ uint32 remip; /* remote sender's IP address */ uint16 remport; /* remote sender's UDP port */ char buff[1500]; /* buffer for incoming reply */ int32 msglen; /* length of outgoing message */ uint16 echoserverport= 7; /* port number for UDP echo */ /* For argument '--help', emit a help message */ if (nargs == 2 && strncmp(args[1], "--help", 7) == 0) { printf("Use: %s\n\n", args[0]); printf("Description:\n"); printf("\tBecome a UDP echo server\n"); printf("Options:\n"); printf("\t--help\t display this help and exit\n"); return 0; } /* Check for valid IP address argument */ if (nargs != 1) { fprintf(stderr, "%s: no arguments expected\n", args[0]); fprintf(stderr, "Try '%s --help' for more information\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(0, 0, echoserverport); if (retval == SYSERR) { fprintf(stderr, "%s: could not reserve UDP port %d\n", args[0], echoserverport); return 1; } /* Do forever: read an incoming datagram and send it back */ while (TRUE) { retval = udp_recvaddr(&remip, &remport, echoserverport, buff, sizeof(buff), 600000); if (retval == TIMEOUT) { continue; } else if (retval == SYSERR) { fprintf(stderr, "%s: error receiving UDP\n", args[0]); return 1; } msglen = retval; retval = udp_send(remip, remport, localip, echoserverport, buff, msglen); if (retval == SYSERR) { fprintf(stderr, "%s: udp_send failed\n", args[0]); return 1; } } return 0; }
/*------------------------------------------------------------------------ * rdscomm - handle communication with a remote disk server (send a * request and receive a reply, including sequencing and * retries) *------------------------------------------------------------------------ */ status rdscomm ( struct rd_msg_hdr *msg, /* message to send */ int32 mlen, /* message length */ struct rd_msg_hdr *reply, /* buffer for reply */ int32 rlen, /* size of reply buffer */ struct rdscblk *rdptr /* ptr to device control block */ ) { int32 i; /* counts retries */ int32 retval; /* return value */ int32 seq; /* sequence for this exchange */ uint32 localip; /* local IP address */ int16 rtype; /* reply type in host byte order*/ bool8 xmit; /* Should we transmit again? */ /* For the first time after reboot, register the server port */ if ( ! rdptr->rd_registered ) { retval = udp_register(0, rdptr->rd_ser_port, rdptr->rd_loc_port); rdptr->rd_registered = TRUE; } if ( (int32)(localip = getlocalip()) == SYSERR ) { return SYSERR; } /* Assign message next sequence number */ seq = rdptr->rd_seq++; msg->rd_seq = htonl(seq); /* Repeat RD_RETRIES times: send message and receive reply */ xmit = TRUE; for (i=0; i<RD_RETRIES; i++) { if (xmit) { /* Send a copy of the message */ retval = udp_send(rdptr->rd_ser_ip, rdptr->rd_ser_port, localip, rdptr->rd_loc_port, (char *)msg, mlen); if (retval == SYSERR) { kprintf("Cannot send to remote disk server\n\r"); return SYSERR; } } else { xmit = TRUE; } /* Receive a reply */ retval = udp_recv(0, rdptr->rd_ser_port, rdptr->rd_loc_port, (char *)reply, rlen, RD_TIMEOUT); if (retval == TIMEOUT) { continue; } else if (retval == SYSERR) { kprintf("Error reading remote disk reply\n\r"); return SYSERR; } /* Verify that sequence in reply matches request */ if (ntohl(reply->rd_seq) < seq) { xmit = FALSE; } else if (ntohl(reply->rd_seq) != seq) { continue; } /* Verify the type in the reply matches the request */ rtype = ntohs(reply->rd_type); if (rtype != ( ntohs(msg->rd_type) | RD_MSG_RESPONSE) ) { continue; } /* Check the status */ if (ntohs(reply->rd_status) != 0) { return SYSERR; } return OK; } /* Retries exhausted without success */ kprintf("Timeout on exchange with remote disk server\n\r"); return TIMEOUT; }
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; }
/*------------------------------------------------------------------------ * rfscomm - Handle communication with RFS server (send request and * receive a reply, including sequencing and retries) *------------------------------------------------------------------------ */ int32 rfscomm ( struct rf_msg_hdr *msg, /* Message to send */ int32 mlen, /* Message length */ struct rf_msg_hdr *reply, /* Buffer for reply */ int32 rlen /* Size of reply buffer */ ) { int32 i; /* Counts retries */ int32 retval; /* Return value */ int32 seq; /* Sequence for this exchange */ int16 rtype; /* Reply type in host byte order*/ int32 slot; /* UDP slot */ /* For the first time after reboot, register the server port */ if ( ! Rf_data.rf_registered ) { if ( (slot = udp_register(Rf_data.rf_ser_ip, Rf_data.rf_ser_port, Rf_data.rf_loc_port)) == SYSERR) { return SYSERR; } Rf_data.rf_udp_slot = slot; Rf_data.rf_registered = TRUE; } /* Assign message next sequence number */ seq = Rf_data.rf_seq++; msg->rf_seq = htonl(seq); /* Repeat RF_RETRIES times: send message and receive reply */ for (i=0; i<RF_RETRIES; i++) { /* Send a copy of the message */ retval = udp_send(Rf_data.rf_udp_slot, (char *)msg, mlen); if (retval == SYSERR) { kprintf("Cannot send to remote file server\n"); return SYSERR; } /* Receive a reply */ retval = udp_recv(Rf_data.rf_udp_slot, (char *)reply, rlen, RF_TIMEOUT); if (retval == TIMEOUT) { continue; } else if (retval == SYSERR) { kprintf("Error reading remote file reply\n"); return SYSERR; } /* Verify that sequence in reply matches request */ if (ntohl(reply->rf_seq) != seq) { continue; } /* Verify the type in the reply matches the request */ rtype = ntohs(reply->rf_type); if (rtype != ( ntohs(msg->rf_type) | RF_MSG_RESPONSE) ) { continue; } return retval; /* Return length to caller */ } /* Retries exhausted without success */ kprintf("Timeout on exchange with remote file server\n"); return TIMEOUT; }
/*------------------------------------------------------------------------ * 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; }