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; }
/*------------------------------------------------------------------------ * rfsInit - initialize the remote file system master device *------------------------------------------------------------------------ */ devcall rfsInit( struct dentry *devptr /* entry in device switch table */ ) { /* Choose an initial message sequence number */ Rf_data.rf_seq = 1; /* Set the server IP address, server port, and local port */ if ( dot2ip(RF_SERVER_IP, &Rf_data.rf_ser_ip) == SYSERR ) { panic("invalid IP address for remote file server"); } Rf_data.rf_ser_port = RF_SERVER_PORT; Rf_data.rf_loc_port = RF_LOC_PORT; /* Create a mutual exclusion semaphore */ if ( (Rf_data.rf_mutex = semcreate(1)) == SYSERR ) { panic("Cannot create remote file system semaphore"); } /* Specify that the server port is not yet registered */ Rf_data.rf_registered = FALSE; return OK; }
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; }
/*------------------------------------------------------------------------ * name2ip - return the IP address for a given DNS name *------------------------------------------------------------------------ */ IPaddr name2ip(char *nam) { Bool isnum; char *p; isnum = TRUE; for (p=nam; *p; ++p) { isnum &= ((*p >= '0' && *p <= '9') || *p == '.'); if (!isnum) break; } if (isnum) return dot2ip(nam); return resolve(nam); }
/*------------------------------------------------------------------------ * parsevalue - parse the type and value of variable to set *------------------------------------------------------------------------ */ LOCAL int parsevalue(char **word, struct snbentry *bl) { if (strequ(*word, "int")) SVTYPE(bl) = ASN1_INT; else if (strequ(*word, "counter")) SVTYPE(bl) = ASN1_COUNTER; else if (strequ(*word, "gauge")) SVTYPE(bl) = ASN1_GAUGE; else if (strequ(*word, "timeticks")) SVTYPE(bl) = ASN1_TIMETICKS; else if (strequ(*word, "str")) SVTYPE(bl) = ASN1_OCTSTR; else if (strequ(*word, "objid")) SVTYPE(bl) = ASN1_OBJID; else if (strequ(*word, "ipaddr")) SVTYPE(bl) = ASN1_IPADDR; else return SYSERR; getword(word); if (**word == NULLCH) return SYSERR; switch (SVTYPE(bl)) { case ASN1_INT: case ASN1_COUNTER: case ASN1_GAUGE: case ASN1_TIMETICKS: SVINT(bl) = atoi(*word); break; case ASN1_OCTSTR: SVSTRLEN(bl) = strlen(*word); SVSTR(bl) = (char *) getmem(SVSTRLEN(bl)); memcpy(SVSTR(bl), *word, SVSTRLEN(bl)); break; case ASN1_OBJID: SVOIDLEN(bl) = dot2oid(SVOID(bl), *word); break; case ASN1_IPADDR: SVIPADDR(bl) = dot2ip(*word); break; default: return SYSERR; } return OK; }
int main(int argc, char **argv) { uint32 retval; kprintf("main: calling getlocalip\n"); retval = getlocalip(); if (retval == SYSERR) { kprintf("I'm here to stop!\n"); } else { kprintf("I'm here to continue!\n"); kprintf("\n\n###########################################################\n\n"); kprintf("IP address is %d.%d.%d.%d %08x\n\r", (retval>>24)&0xff, (retval>>16)&0xff, (retval>>8)&0xff, retval&0xff,retval); kprintf("Subnet mask is %d.%d.%d.%d and router is %d.%d.%d.%d\n\r", (NetData.addrmask>>24)&0xff, (NetData.addrmask>>16)&0xff, (NetData.addrmask>> 8)&0xff, NetData.addrmask&0xff, (NetData.routeraddr>>24)&0xff, (NetData.routeraddr>>16)&0xff, (NetData.routeraddr>> 8)&0xff, NetData.routeraddr&0xff); } dot2ip(RPL_FORWARDING_GATEWAY, &retval); if(retval == NetData.ipaddr){ kprintf("I am the simulator gateway\r\n"); } rpl_init(); resume(create(shell, 4096, INITPRIO, "Shell", 1, CONSOLE)); receive(); getlocalip(); resume(create(shell, 4096, INITPRIO, "Shell", 1, CONSOLE)); return OK; }
/*------------------------------------------------------------------------ * rdsinit - Initialize the remote disk system device *------------------------------------------------------------------------ */ devcall rdsinit ( struct dentry *devptr /* Entry in device switch table */ ) { struct rdscblk *rdptr; /* Ptr to device contol block */ struct rdbuff *bptr; /* Ptr to buffer in memory */ /* used to form linked list */ struct rdbuff *pptr; /* Ptr to previous buff on list */ struct rdbuff *buffend; /* Last address in buffer memory*/ uint32 size; /* Total size of memory needed */ /* buffers */ /* Obtain address of control block */ rdptr = &rdstab[devptr->dvminor]; /* Set control block to unused */ rdptr->rd_state = RD_FREE; rdptr->rd_id[0] = NULLCH; /* Set initial message sequence number */ rdptr->rd_seq = 1; /* Initialize request queue and cache to empty */ rdptr->rd_rhnext = (struct rdbuff *) &rdptr->rd_rtnext; rdptr->rd_rhprev = (struct rdbuff *)NULL; rdptr->rd_rtnext = (struct rdbuff *)NULL; rdptr->rd_rtprev = (struct rdbuff *) &rdptr->rd_rhnext; rdptr->rd_chnext = (struct rdbuff *) &rdptr->rd_ctnext; rdptr->rd_chprev = (struct rdbuff *)NULL; rdptr->rd_ctnext = (struct rdbuff *)NULL; rdptr->rd_ctprev = (struct rdbuff *) &rdptr->rd_chnext; /* Allocate memory for a set of buffers (actually request */ /* blocks and link them to form the initial free list */ size = sizeof(struct rdbuff) * RD_BUFFS; bptr = (struct rdbuff *)getmem(size); rdptr->rd_free = bptr; if ((int32)bptr == SYSERR) { panic("Cannot allocate memory for remote disk buffers"); } buffend = (struct rdbuff *) ((char *)bptr + size); while (bptr < buffend) { /* walk through memory */ pptr = bptr; bptr = (struct rdbuff *) (sizeof(struct rdbuff)+ (char *)bptr); pptr->rd_status = RD_INVALID; /* Buffer is empty */ pptr->rd_next = bptr; /* Point to next buffer */ } pptr->rd_next = (struct rdbuff *) NULL; /* Last buffer on list */ /* Create the request list and available buffer semaphores */ rdptr->rd_availsem = semcreate(RD_BUFFS); rdptr->rd_reqsem = semcreate(0); /* Set the server IP address, server port, and local port */ if ( dot2ip(RD_SERVER_IP, &rdptr->rd_ser_ip) == SYSERR ) { panic("invalid IP address for remote disk server"); } /* Set the port numbers */ rdptr->rd_ser_port = RD_SERVER_PORT; rdptr->rd_loc_port = RD_LOC_PORT + devptr->dvminor; /* Specify that the server port is not yet registered */ rdptr->rd_registered = FALSE; /* Create a communication process */ rdptr->rd_comproc = -1; /* rdptr->rd_comproc = create(rdsprocess, RD_STACK, RD_PRIO, "rdsproc", 1, rdptr); if (rdptr->rd_comproc == SYSERR) { panic("Cannot create remote disk process"); } resume(rdptr->rd_comproc); */ return OK; }
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; }
/*------------------------------------------------------------------------ * netstart - initialize net *------------------------------------------------------------------------ */ int netstart(int userpid) { char str[128]; char *str1 = "sleeping 30 secs to get routes..."; char *str2 = "\ndone.\n"; unsigned long now; int i; if (clkruns == FALSE) panic("net: no clock"); /* initialize ports */ for (i=0 ; i<UPPS ; i++) upqs[i].up_valid = FALSE; udpmutex = screate(1); arpinit(); ipfinit(); /* init the IP frag queue */ /* these need a command to set/clear them. FIX ME!!! */ gateway = 1; IpForwarding = 1; /* == 2 if not a gateway */ IfNumber = Net.nif - 1; /* bsdbrc = 1; */ /* uncomment to use 0's broadcast */ if (gateway) { IPaddr gw; initgate(); gw = dot2ip(DEFRTR); rtadd(RT_DEFAULT, RT_DEFAULT, gw, RTM_INF-1, 1, RT_INF); } else inithost(); rtadd(RT_LOOPBACK, ip_maskall, RT_LOOPBACK, 0, NI_LOCAL, RT_INF); for (i=0; i<Net.nif; ++i) { nif[i].ni_ipinq = newq(NI_INQSZ, QF_NOWAIT); nif[i].ni_hwtype = AR_HARDWARE; /* for ARP */ /* no OTHER's for now */ if (i < 2) nif[i].ni_state = NIS_UP; } /* * wait()'s synchronize to insure initialization is done * before proceeding. */ resume(create(slowtimer, STSTK, STPRI, STNAM, STARGC)); wait(Net.sema); resume(create(ipproc, IPSTK, IPPRI, IPNAM, IPARGC)); wait(Net.sema); resume(create(tcptimer, TMSTK, TMPRI, TMNAM, TMARGC)); wait(Net.sema); resume(create(tcpinp, TCPISTK, TCPIPRI, TCPINAM, TCPIARGC)); wait(Net.sema); resume(create(tcpout, TCPOSTK, TCPOPRI, TCPONAM, TCPOARGC)); wait(Net.sema); if (!gateway) { write(CONSOLE, str1, strlen(str1)); sleep(30); write(CONSOLE, str2, strlen(str2)); } /* get addrs & names */ for (i=0; i<Net.nif; ++i) { IPaddr junk; char junk2[64]; if (i == NI_LOCAL) continue; if (nif[i].ni_state != NIS_UP) continue; junk = getiaddr(i); sprintf(str, "if%d IP %s ha %02x:%02x:%02x:%02x:%02x:%02x ", i, ip2dot(junk2, nif[i].ni_ip), nif[i].ni_hwa.ha_addr[0]&0xff, nif[i].ni_hwa.ha_addr[1]&0xff, nif[i].ni_hwa.ha_addr[2]&0xff, nif[i].ni_hwa.ha_addr[3]&0xff, nif[i].ni_hwa.ha_addr[4]&0xff, nif[i].ni_hwa.ha_addr[5]&0xff); write(CONSOLE, str, strlen(str)); sprintf(str, "br %02x:%02x:%02x:%02x:%02x:%02x\n", nif[i].ni_hwb.ha_addr[0]&0xff, nif[i].ni_hwb.ha_addr[1]&0xff, nif[i].ni_hwb.ha_addr[2]&0xff, nif[i].ni_hwb.ha_addr[3]&0xff, nif[i].ni_hwb.ha_addr[4]&0xff, nif[i].ni_hwb.ha_addr[5]&0xff); write(CONSOLE, str, strlen(str)); icmp(ICT_MASKRQ, 0, nif[i].ni_brc, 0, 0); recvtim(30); /* wait for an answer */ getiname(i, junk2); } getutim(&now); #ifdef MULTICAST hginit(); #endif /* MULTICAST */ #ifdef RIP resume(create(rip, RIPISTK, RIPPRI, RIPNAM, RIPARGC)); #endif /* RIP */ #ifdef OSPF resume(create(ospf, OSPFSTK, OSPFPRI, OSPFNAM, 0)); #endif /* OSPF */ #ifdef SNMP resume(create(snmpd, SNMPSTK, SNMPPRI, SNMPDNAM, 0)); #endif /* SNMP */ rwho(); resume(create(FINGERD, FNGDSTK, FNGDPRI, FNGDNAM, FNGDARGC)); resume(create(ECHOD, ECHOSTK, ECHOPRI, ECHODNAM, 0)); resume(create(udpecho, 1000, 50, "udpecho", 0)); resume(userpid); }
status rpl_send(char * node_phy_addr, char *src_node, byte msg_type, char *msg, uint32 msg_len){ struct eth_packet pkt; struct rpl_sim_packet *rpl_sim_pkt = NULL; uint32 remip; byte ethbcast[] = {0xff,0xff,0xff,0xff,0xff,0xff}; dot2ip(RPL_FORWARDING_GATEWAY, &remip); #ifdef DEBUG kprintf("Simulator is : %04x dest : %04x source : %04x my addr : %04x\r\n", remip, *((uint32*)node_phy_addr), *((uint32*)src_node), NetData.ipaddr); #endif if ( ! NetData.ipvalid){ getlocalip(); } if(msg_len > 1500-ETH_HDR_LEN-RPL_SIM_HDR_LEN){ kprintf("WARN : Simulator : Message too big \r\n"); return SYSERR; } memcpy(pkt.net_ethsrc, NetData.ethaddr, ETH_ADDR_LEN); //kprintf("Copied ethernet source : %06x\r\n", *(pkt.net_ethsrc+4)); /* FIXME : Needs to be changed to something that is valid */ pkt.net_ethtype = 0x1000; //kprintf("The eth type in current packet is : %02x\r\n", pkt.net_ethtype); if (remip == IP_BCAST) { /* set mac address to b-cast */ memcpy(pkt.net_ethdst, ethbcast, ETH_ADDR_LEN); /* If destination isn't on the same subnet, send to router */ } else if ((NetData.ipaddr & NetData.addrmask) != (remip & NetData.addrmask)) { if (arp_resolve(NetData.routeraddr, pkt.net_ethdst)!=OK) { kprintf("rpl_send: cannot resolve router %08x\n\r", NetData.routeraddr); return SYSERR; } } else { /* Destination is on local subnet - get MAC address */ if (arp_resolve(remip, pkt.net_ethdst) != OK) { kprintf("rpl_send: cannot resolve %08x\n\r",remip); return SYSERR; } } rpl_sim_pkt = (struct rpl_sim_packet *)pkt.net_ethdata; memcpy((char *)rpl_sim_pkt->dest_node, (char *)node_phy_addr, RPL_NODE_PHY_ADDR_LEN); /* * FIXME Change this to my_phsical_address which is 64 bits */ memcpy((char *)rpl_sim_pkt->src_node, (char *)(src_node), RPL_NODE_PHY_ADDR_LEN); rpl_sim_pkt->msg_type = msg_type; rpl_sim_pkt->msg_len = msg_len; memcpy(rpl_sim_pkt->data, msg, msg_len); /* * FIXME : Perform host to network order translations */ kprintf("The packet is destined for : %06x with length : %d\r\n", *(pkt.net_ethdst+4), msg_len); eth_hton(&pkt); write(ETHER0, (char *)&pkt, ETH_HDR_LEN + RPL_SIM_HDR_LEN + msg_len); return OK; }
status rpl_receive(){ int j = 19990; int i = 0; struct rpl_sim_packet * pkt = NULL; uint32 remip ; while(1){ remip = 0xffffffff; wait(rpl_sim_read_sem); #ifdef DEBUG kprintf("Resuming in rpl_receive\r\n"); kprintf("After resuming in rpl_receive i is [%d] j is [%d]\r\n", i, j); #endif pkt = &sim_queue[i]; /* * Lookup map and see if we have a mapping * Send the packet * Use the rpl_sim_packet header as such */ dot2ip(RPL_FORWARDING_GATEWAY, &remip); if(remip == NetData.ipaddr){ #ifdef DEBUG kprintf("Simulator working with iter [%d] and j [%d] message type : %d dest : %04x source : %04x\r\n",i,j, pkt->msg_type, *((uint32 *)(pkt->dest_node)), *((uint32 *)(pkt->src_node))); #endif remip = *((uint32 *)(pkt->dest_node)); if(remip == 0xffffffff || remip == 0x00000000){ kprintf("WARN : Could not find a mapping for the given 64bit physical address\r\n"); //return SYSERR; } else{ #ifdef DEBUG kprintf("Forwarding rpl message to : [%04x] and i is : %d\r\n", remip, i); #endif rpl_send_with_ip((char *)pkt->dest_node, (char *)pkt->src_node, pkt->msg_type, (char *)pkt->data, pkt->msg_len, remip); //kprintf("The value of i after rpl_send_with_ip is : %d\r\n", i); } } else{ struct icmpv6_sim_packet rpkt; byte parent_changed = 0; switch(pkt->msg_type){ case RPL_DIS_MSGTYPE: #ifdef DEBUG kprintf("DIS Received\r\n"); #endif #ifdef LOWPAN_NODE if(RPL_MYINFO.parent_index < 0){ kprintf("WARN : Ignoring DIS Message since I don't know my parent\r\n"); break; } #endif decodedis((struct icmpv6_sim_packet *)(pkt->data)); encodedio(&rpkt); rpl_send((char *)(pkt->src_node), (char *)(&NetData.ipaddr), RPL_DIO_MSGTYPE, (char *)(&rpkt), 1500-ETH_HDR_LEN- RPL_SIM_HDR_LEN); break; case RPL_DIO_MSGTYPE: #ifdef DEBUG kprintf("DIO Received\r\n"); #endif parent_changed = 0; processdio((struct icmpv6_sim_packet *)(pkt->data), *((uint32 *)(pkt->src_node)), &parent_changed); if(parent_changed != 0){ encodedao(&rpkt); #ifdef DEBUG kprintf("Sending DAO to [%04x]\r\n", *((uint32 *)(RPL_MYINFO.dodagid))); #endif if(RPL_MYINFO.parent_index > -1 && RPL_MYINFO.parent_index < LOWPAN_MAX_NODES){ rpl_send((char *)(&(rpl_link_local_neighbors[RPL_MYINFO.parent_index].iface_addr)), (char *)(&NetData.ipaddr), RPL_DAO_MSGTYPE, (char *)(&rpkt), 1500-ETH_HDR_LEN- RPL_SIM_HDR_LEN); } else{ kprintf("WARN : Cannot send the DAO message after receiving DIO message since we do not know the parent\r\n"); } } else{ #ifdef DEBUG kprintf("INFO : Parent did not change so not doing anything\r\n"); #endif } break; case RPL_DAO_MSGTYPE: #ifdef DEBUG kprintf("DAO Received\r\n"); #endif #ifdef LOWPAN_BORDER_ROUTER #ifdef DEBUG kprintf("**********DAO RECEIVED from %04x ", *((uint32 *)(pkt->src_node))); #endif processdao((struct icmpv6_sim_packet *)(pkt->data)); #endif #ifdef LOWPAN_NODE if(RPL_MYINFO.parent_index > -1 && RPL_MYINFO.parent_index < LOWPAN_MAX_NODES){ #ifdef DEBUG kprintf("**********FORWARDING DAO MESSAGE TO %04x from %04x\r\n",rpl_link_local_neighbors[RPL_MYINFO.parent_index].iface_addr, *((uint32 *)(pkt->src_node))); #endif rpl_send((char *)(&(rpl_link_local_neighbors[RPL_MYINFO.parent_index].iface_addr)), (char *)(&NetData.ipaddr), RPL_DAO_MSGTYPE, (char *)(pkt->data), 1500-ETH_HDR_LEN- RPL_SIM_HDR_LEN); } else{ #ifdef DEBUG kprintf("WARN : Cannot forward the DAO message since we do not know the parent\r\n"); #endif } #endif break; default: #ifdef DEBUG kprintf("Received an unknown RPL message type\r\n"); #endif break; } } //kprintf("Before signal i is [%d] j is [%d]\r\n", i, j); signal(rpl_sim_write_sem); //kprintf("Before incrementing i is [%d] j is [%d]\r\n", i, j); i = (i+1)%RPL_SIM_RECV_QUEUE_LEN; //kprintf("After handling packet i is [%d] j is [%d]\r\n", i, j); } return OK; }
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; }
/*------------------------------------------------------------------------ * xsh_ping - shell command to ping a remote host *------------------------------------------------------------------------ */ shellcmd xsh_ping(int nargs, char *args[]) { uint32 ipaddr; /* IP address in binary */ int32 retval; /* return value */ int32 icmpid; /* ICMP ID to use */ static int32 seq = 0; /* sequence number */ char buf[56]; /* buffer of chars */ int32 i; /* index into buffer */ int32 nextval; /* next value to use */ /* For argument '--help', emit help about the 'ping' command */ if (nargs == 2 && strncmp(args[1], "--help", 7) == 0) { printf("Use: %s address\n\n", args[0]); printf("Description:\n"); printf("\tUse ICMP Echo to ping a remote host\n"); printf("Options:\n"); printf("\t--help\t display this help and exit\n"); printf("\taddress\t an IP address in dotted decimal\n"); return 0; } /* Check for valid number of arguments */ if (nargs != 2) { fprintf(stderr, "%s: invalid arguments\n", args[0]); fprintf(stderr, "Try '%s --help' for more information\n", args[0]); return 1; } /* convert argument to binary */ retval = dot2ip(args[1], &ipaddr); if ((int32)retval == SYSERR) { fprintf(stderr, "%s: invalid IP address\n", args[0]); return 1; } /* Register to receive an ICMP Echo Reply */ icmpid = icmp_register(ipaddr); if (icmpid == SYSERR) { fprintf(stderr, "%s: ICMP registration fails\n", args[0]); return 1; } /* Fill the buffer with values - start with low-order byte of */ /* the sequence number and increment */ nextval = seq; for (i = 0; i<sizeof(buf); i++) { buf[i] = 0xff & nextval++; } /* Send an ICMP Echo Request */ retval = icmp_send(ipaddr, ICMP_ECHOREQST, icmpid, seq++, buf, sizeof(buf)); if (retval == SYSERR) { fprintf(stderr, "%s: cannot send a ping\n", args[0]); icmp_release(icmpid); return 1; } /* Read a reply */ retval = icmp_recv(icmpid, buf, sizeof(buf), 3000); icmp_release(icmpid); if (retval == TIMEOUT) { fprintf(stderr, "%s: no response from %s\n\n", args[0], args[1]); return 1; } fprintf(stderr, "host %s is alive\n", args[1]); return 0; }