diff --git a/discvr/cli.c b/discvr/cli.c index 1958e7182f526a5ac581ebaaad65d5b84a8a74e0..744fd98dc90d5e9dce40451fc0050cbde74a075b 100644 --- a/discvr/cli.c +++ b/discvr/cli.c @@ -18,7 +18,7 @@ * * --------------------------- * - * $Id: cli.c,v 1.3 2001-06-14 23:19:23 ikumar Exp $ + * $Id: cli.c,v 1.4 2001-06-18 01:41:19 ikumar Exp $ */ #include "discvr.h" @@ -116,6 +116,7 @@ cli(int sockfd, const struct sockaddr *pservaddr, socklen_t servlen, make_inquiry(&ti, ttl, factor); + printf("sending query to server:\n"); sendto(sockfd, &ti, TOPD_INQ_SIZ, 0, pservaddr, servlen); //print_tdinq((char *)&ti); n = recvfrom(sockfd, recvline, MAXLINE, 0, NULL, NULL); diff --git a/discvr/discvr.h b/discvr/discvr.h index e8028a91adca4fea4b46342d222551f1f6ec9479..17b8c98e9dc7b27fcbb91d9d63af4f16c9c9fe04 100644 --- a/discvr/discvr.h +++ b/discvr/discvr.h @@ -1,4 +1,4 @@ -/*$Id: discvr.h,v 1.4 2001-06-14 23:19:23 ikumar Exp $*/ +/*$Id: discvr.h,v 1.5 2001-06-18 01:41:19 ikumar Exp $*/ #ifndef _TOPD_DISCVR_H_ #define _TOPD_DISCVR_H_ @@ -54,7 +54,7 @@ struct ifi_info { struct sockaddr *ifi_dstaddr;/* destination address */ struct ifi_info *ifi_next; /* next of these structures */ struct topd_nborlist *ifi_nbors;/* neighbors */ - + int sock; /* socket associated with this interface */ }; /* Prototypes */ @@ -93,5 +93,11 @@ compose_reply(struct ifi_info *ifi, char *mesg, const int mesglen, int sendnbors int is_my_packet(struct sockaddr *pcliaddr, struct ifi_info *ifihead) ; +struct ifi_info * +get_ifi_struct(int sock, struct ifi_info * ifihead); + +void +addMyID(char* mesg, int size); + #endif /* _TOPD_DISCVR_H_ */ diff --git a/discvr/forw_requests.c b/discvr/forw_requests.c index 39d9a47468adba657f1d49bb2a22777e4975055b..d146aa33daec1f6564c4940d15d7b58828af3546 100644 --- a/discvr/forw_requests.c +++ b/discvr/forw_requests.c @@ -18,7 +18,7 @@ * * --------------------------- * - * $Id: forw_requests.c,v 1.6 2001-06-14 23:19:23 ikumar Exp $ + * $Id: forw_requests.c,v 1.7 2001-06-18 01:41:19 ikumar Exp $ */ #include @@ -31,7 +31,7 @@ * Send a request on to each interface */ char recvbuf[BUFSIZ]; - +extern u_char myNodeID[ETHADDRSIZ]; static double start = 0.0; double @@ -47,13 +47,36 @@ tod(void) } +struct ifi_info * +get_ifi_struct(int sock, struct ifi_info * ifihead) +{ + struct ifi_info * ifi=NULL; + + for (ifi = ifihead; ifi != NULL; ifi = ifi->ifi_next) + { + if(ifi->sock == sock) return ifi; + } + return NULL; +} + +void +addMyID(char* mesg, int size) +{ + struct topd_nbor *p; + p = (struct topd_nbor *) (mesg + sizeof(topd_inqid_t)); + + memcpy(p->tdnbor_dnode,myNodeID,ETHADDRSIZ); +} + + + struct ifi_info * forward_request(struct ifi_info *ifi, const struct in_pktinfo *pktinfo, const char *mesg, int mesglen) { int s, n; int more_time = 1; - fd_set rset; + fd_set rset,temp_rset; const int on = 1; char ifname[IFNAMSIZ]; struct topd_inqid *tdi; @@ -64,9 +87,31 @@ forward_request(struct ifi_info *ifi, const struct in_pktinfo *pktinfo, int selectReturn; struct sockaddr name; int namelen=sizeof(name); + int *sock_list=NULL, *temp_sock_list=NULL; + int sock_num = 0,i=0; + struct topd_inqid *temp_mesg; + int t_int; + FD_ZERO(&rset); + FD_ZERO(&temp_rset); printf("The interface on which I received: \"%s\"\n",if_indextoname(pktinfo->ipi_ifindex, ifname)); + + bzero(&tempAddr, sizeof(tempAddr)); + tempAddr.sin_family = AF_INET; + //inet_aton("192.168.2.2",&tempAddr.sin_addr.s_addr); + tempAddr.sin_addr.s_addr = htonl(INADDR_ANY); + tempAddr.sin_port = htons(0); + // Setting the deadline for select -ik + tdi = (struct topd_inqid *)mesg; + tv.tv_sec = ntohs(tdi->tdi_ttl) * ntohs(tdi->tdi_factor); + tv.tv_usec = 0; + + temp_mesg = (struct topd_inqid *)mesg; + t_int = ntohs(temp_mesg->tdi_ttl); + t_int=t_int-1; + temp_mesg->tdi_ttl = htons(t_int); + for (ifihead = ifi = get_ifi_info(AF_INET, 0); ifi != NULL; ifi = ifi->ifi_next) { @@ -90,15 +135,16 @@ forward_request(struct ifi_info *ifi, const struct in_pktinfo *pktinfo, if(strcmp(ifi->ifi_name,"fxp4")==0) continue; printf("Forwarding the query to interface: \"%s\"\n",ifi->ifi_name); - - bzero(&tempAddr, sizeof(tempAddr)); - tempAddr.sin_family = AF_INET; - //inet_aton("192.168.2.2",&tempAddr.sin_addr.s_addr); - tempAddr.sin_addr.s_addr = htonl(INADDR_ANY); - tempAddr.sin_port = htons(0); - - - s = socket(AF_INET, SOCK_DGRAM, 0); + temp_sock_list = sock_list; + sock_list = (int *)malloc(sizeof(int)*(sock_num+1)); + for(i=0;isock = s; if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) != 0) { perror("setsockopt died."); @@ -139,11 +187,11 @@ forward_request(struct ifi_info *ifi, const struct in_pktinfo *pktinfo, * 15:04:59.357528 eureka.cs.utah.edu.1192 > 155.99.212.255.9877: udp 15 * 15:20:02.075858 kamas.cs.utah.edu.9877 > eureka.cs.utah.edu.1214: udp 15 */ - fprintf(stderr, "$$$forwarding the enquiry: "); + printf("$$$forwarding the enquiry: "); print_tdinq(mesg); n = sendto(s, mesg, mesglen, 0, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)); - fprintf(stderr, "$$$after forwarding the enquiry: "); + printf("$$$after forwarding the enquiry: "); print_tdinq(mesg); if(getsockname(s, (struct sockaddr *)&name, &namelen)<0) @@ -157,98 +205,115 @@ forward_request(struct ifi_info *ifi, const struct in_pktinfo *pktinfo, perror("Didn't send all of packet"); exit(1); } + + FD_SET(s, &rset); + } - /* - * Can't wait forever because - * there may be either no nodes on this interface or - * the nodes that are there may not be responding. - * Our wait delay is equal to the "factor times the ttl". - * - * The packet size *might* exceed our buffer size. - * Put in some mechanism to check. -lkw + /* + * Can't wait forever because + * there may be either no nodes on this interface or + * the nodes that are there may not be responding. + * Our wait delay is equal to the "factor times the ttl". + * + * The packet size *might* exceed our buffer size. + * Put in some mechanism to check. -lkw + */ + + n = 0; + start = tod(); + //while ( more_time ) { + while (1) { + double begin, end, elapsed; + double deadline = tv.tv_sec + (tv.tv_usec * 1e-6); + + /* + * It's possible that one neighbor responded very quickly + * and other neighbors (perhaps because they have many more + * neighbors whose messages they had to parse) could respond + * slowly. To cover this case, we should continue to try to + * read from the socket until our delay is up. -lkw */ + begin = tod(); + temp_rset = rset; + printf("entering select **** sec:%d and usec:%d\n",tv.tv_sec,tv.tv_usec); + selectReturn = select(FD_SETSIZE, &temp_rset, NULL, NULL, &tv); + printf("select return: %d\n",selectReturn); + if( selectReturn < 0 ) { + printf("Error in Select call\n"); + perror("select"); + exit(1); + } + else if(selectReturn == 0) + { + printf("Select timed out****\n"); + break; + //more_time=0; + } - tdi = (struct topd_inqid *)mesg; - tv.tv_sec = ntohs(tdi->tdi_ttl) * ntohs(tdi->tdi_factor); - tv.tv_usec = 0; - FD_ZERO(&rset); - FD_SET(s, &rset); + end = tod(); + /* get ready for next select */ + elapsed = end - begin; + /* fprintf(stderr, "elapsed:%f ", elapsed); DEBUG */ + if (elapsed > deadline) { + break; + } + deadline -= elapsed; + tv.tv_sec = floor(deadline); + tv.tv_usec = (deadline - tv.tv_sec) * 1e+6; + printf("sec:%d usec:%d\n", tv.tv_sec, tv.tv_usec); - n = 0; - start = tod(); - while ( more_time ) { - double begin, end, elapsed; - double deadline = tv.tv_sec + (tv.tv_usec * 1e-6); - /* - * It's possible that one neighbor responded very quickly - * and other neighbors (perhaps because they have many more - * neighbors whose messages they had to parse) could respond - * slowly. To cover this case, we should continue to try to - * read from the socket until our delay is up. -lkw - */ - begin = tod(); + + for(i=0;i deadline) { - break; - } - deadline -= elapsed; - tv.tv_sec = floor(deadline); - tv.tv_usec = (deadline - tv.tv_sec) * 1e+6; - fprintf(stderr, "sec:%d usec:%d\n", tv.tv_sec, tv.tv_usec); - + //} + // receive one of my neighbors' list of neighbors through // "ifi" interface to which I just forwarded the request -ik - n = recvfrom(s, recvbuf, BUFSIZ, 0, NULL, NULL); + printf("Checking socket: %d\n",sock_list[i]); + + if (!FD_ISSET(sock_list[i], &temp_rset)) continue; + + n = recvfrom(sock_list[i], recvbuf, BUFSIZ, 0, NULL, NULL); //printf ("recvd %d\n", n); printf ("received:==>\n"); print_tdreply(recvbuf,n); - - - /* + + //addMyID(recvbuf,n); + /* * Be sure to malloc enough space for all of the mesg. * See note above about possible datagram truncation. -lkw */ - + + if((ifi=get_ifi_struct(sock_list[i],ifihead)) == NULL) + { + printf("Socket \"%d\" is not in any ifi struct\n",sock_list[i]); + } + + printf("The interface on which i received is \"%s\"",ifi->ifi_name); + // save the existing list of neighbors which I just received through // the "ifi" interface... -ik - save = ifi->ifi_nbors; + save = ifi->ifi_nbors; printf("The value of save: \"%d\"\n",save); // create a new node for the neighbor link list pointed by "ifi_nbors" // -ik ifi->ifi_nbors = (struct topd_nborlist *)malloc(sizeof(struct topd_nborlist)); if ( ifi->ifi_nbors == NULL ) { - perror("Not enough memory for neighbor list."); - exit(1); + perror("Not enough memory for neighbor list."); + exit(1); } /* skip inquiry ID */ ifi->ifi_nbors->tdnbl_nbors = (u_char *)malloc(n); if ( ifi->ifi_nbors->tdnbl_nbors == NULL ) { - perror("Not enough memory for neighbor list."); + perror("Not enough memory for neighbor list."); exit(1); } diff --git a/discvr/serv_listen.c b/discvr/serv_listen.c index 42187db0c365ed78331a06a4f9a0d21182deb592..77c6cc6e8c796028a3ff5a26eecca35ebc1616f8 100644 --- a/discvr/serv_listen.c +++ b/discvr/serv_listen.c @@ -18,7 +18,7 @@ * * --------------------------- * - * $Id: serv_listen.c,v 1.6 2001-06-14 23:19:23 ikumar Exp $ + * $Id: serv_listen.c,v 1.7 2001-06-18 01:41:19 ikumar Exp $ */ #include "discvr.h" @@ -65,7 +65,7 @@ int is_my_packet(struct sockaddr *pcliaddr, struct ifi_info *ifihead) for( ifi = ifihead; ifi != NULL; ifi = ifi->ifi_next) { name1=inet_ntoa(((struct sockaddr_in *)ifi->ifi_addr)->sin_addr); name2=inet_ntoa(((struct sockaddr_in *)pcliaddr)->sin_addr); - fprintf(stderr,"name1:\"%s\" name2:\"%s\"\n",name1,name2); + //printf("name1:\"%s\" name2:\"%s\"\n",name1,name2); if(strcmp(name1,name2)==0) return 1; } @@ -78,7 +78,11 @@ int is_my_packet(struct sockaddr *pcliaddr, struct ifi_info *ifihead) int inqid_cmp(struct topd_inqid *tid1, struct topd_inqid *tid2) { - return( bcmp((void *)tid1, (void*)tid2, sizeof(struct topd_inqid))); + //return( bcmp((void *)tid1, (void*)tid2, sizeof(struct topd_inqid))); + if((tid1->tdi_tv.tv_sec==tid2->tdi_tv.tv_sec) && (tid1->tdi_tv.tv_usec==tid2->tdi_tv.tv_usec) + && (strcmp(tid1->tdi_nodeID,tid2->tdi_nodeID)==0)) + return 1; + return 0; } @@ -124,6 +128,8 @@ serv_listen(int sockfd, struct sockaddr *pcliaddr, socklen_t clilen) struct in_pktinfo pktinfo; struct topd_inqnode *inqn, *inqhead = 0; int i=0,j=0; + struct topd_inqid *temp_inqid; + int t_int; /* * Use this option to specify that the receiving interface @@ -135,7 +141,7 @@ serv_listen(int sockfd, struct sockaddr *pcliaddr, socklen_t clilen) * is supported on FreeBSD. -lkw */ if (setsockopt(sockfd, IPPROTO_IP, IP_RECVIF, &on, sizeof(on)) < 0) { - fprintf(stderr, "setsockopt of IP_RECVIF"); + printf("setsockopt of IP_RECVIF"); return; } @@ -155,7 +161,7 @@ serv_listen(int sockfd, struct sockaddr *pcliaddr, socklen_t clilen) } } memcpy(&myNodeID, myNodeIDtmp, ETHADDRSIZ); - fprintf(stderr, "My node id:"); + printf("My node id:"); print_nodeID(myNodeID); print_ifi_info(ifihead); @@ -167,7 +173,7 @@ serv_listen(int sockfd, struct sockaddr *pcliaddr, socklen_t clilen) */ i=0; for ( ; ; ) { - fprintf(stderr,"**********for==>%d\n",i++); + printf("**********for==>%d\n",i++); len = clilen; flags = 0; n = recvfrom_flags(sockfd, mesg, MAXLINE, &flags, @@ -206,34 +212,33 @@ serv_listen(int sockfd, struct sockaddr *pcliaddr, socklen_t clilen) if(inqhead==NULL) class = NEW; j=0; for ( inqn = inqhead; inqn != NULL; inqn = inqn->inqn_next ) { - fflush(stdin); - fflush(stderr); - fprintf(stderr,"inquiry loop ##### %d\n",j++); - fprintf(stderr,"Comparing #### ==>\n\""); - print_tdinq((char*)inqn->inqn_inq); - fprintf(stderr,"\"and\n\""); - print_tdinq(mesg); - fprintf(stderr,"\"\n"); - if (inqid_cmp(inqn->inqn_inq, (struct topd_inqid *)mesg) == NULL) { + //printf("inquiry loop ##### %d\n",j++); + //printf("Comparing #### ==>\n\""); + //print_tdinq((char*)inqn->inqn_inq); + //printf("\"and\n\""); + //print_tdinq(mesg); + //printf("\"\n"); + if (inqid_cmp(inqn->inqn_inq, (struct topd_inqid *)mesg)) { /* Duplicate inquiry */ //if (from_us(&pktinfo, ifihead))... if(is_my_packet(pcliaddr, ifihead)) { - fprintf(stderr,"Duplicate inquiry from me\n"); + printf("Duplicate inquiry from me\n"); class = DUP; break; } else { - fprintf(stderr,"Duplicate inquiry from others\n"); + printf("Duplicate inquiry from others\n"); class = DUP_NBOR; break; } } else { /* New inquiry */ - fprintf(stderr,"Got a new inquiry\n"); class = NEW; } } if (class == DUP) continue; + if (class == NEW) + printf("Got a new inquiry\n"); /* * Save the inquiry ID into inqid_current. @@ -245,8 +250,14 @@ serv_listen(int sockfd, struct sockaddr *pcliaddr, socklen_t clilen) */ ifihead = get_ifi_info(AF_INET, 0); - if (class == NEW) { + temp_inqid = (struct topd_inqid *)mesg; + t_int = ntohs(temp_inqid->tdi_ttl); + if(t_int==0) + printf("The TTL is zero so i will not forward!!\n"); + if ((class == NEW) && (t_int!=0)) { struct topd_inqnode *save = inqhead; + //struct topd_inqid *temp; + //int t_int; inqhead = (struct topd_inqnode *)malloc(sizeof(struct topd_inqnode)); inqhead->inqn_inq = (struct topd_inqid *)malloc(sizeof(struct topd_inqid)); if (inqhead == NULL || inqhead->inqn_inq == NULL ) { @@ -255,6 +266,12 @@ serv_listen(int sockfd, struct sockaddr *pcliaddr, socklen_t clilen) } memcpy(inqhead->inqn_inq, mesg, sizeof(struct topd_inqid)); inqhead->inqn_next = save; + + //temp = (struct topd_inqid *)mesg; + //t_int = ntohs(temp->tdi_ttl); + //t_int=t_int-2; + //temp->tdi_ttl = htons(t_int); + ifihead=forward_request(ifihead, &pktinfo, mesg, n ); printf("printing after forward===>\n"); print_tdifinbrs(ifihead); @@ -266,9 +283,9 @@ serv_listen(int sockfd, struct sockaddr *pcliaddr, socklen_t clilen) exit(1); } n = compose_reply(ifihead, reply, BUFSIZ, class == NEW); - fprintf(stderr, "replying: "); + printf("replying: "); print_tdreply(reply, n); - fprintf(stderr,"done!\n"); + printf("done!\n"); free_ifi_info(ifihead); printf("I am sending the response to: %s\n",sock_ntop(pcliaddr,sizeof(struct sockaddr))); diff --git a/discvr/util.c b/discvr/util.c index 77ba97cf9bef38e4d45ac1eebe95186dde85c983..0c7180ebbaa3fac6198b065af808b408c086369e 100644 --- a/discvr/util.c +++ b/discvr/util.c @@ -18,7 +18,7 @@ * * --------------------------- * - * $Id: util.c,v 1.3 2001-06-14 23:19:23 ikumar Exp $ + * $Id: util.c,v 1.4 2001-06-18 01:41:19 ikumar Exp $ */ #include "discvr.h" @@ -34,10 +34,10 @@ println_haddr(u_char *haddr, u_short hlen) if ((i = hlen) > 0) { ptr = haddr; do { - fprintf(stderr, "%s%x", (i == hlen) ? " " : ":", *ptr++); + printf("%s%x", (i == hlen) ? " " : ":", *ptr++); } while (--i > 0); } - fprintf(stderr, "\n"); + printf("\n"); } void @@ -49,7 +49,7 @@ print_haddr(u_char *haddr, u_short hlen) if ((i = hlen) > 0) { ptr = haddr; do { - fprintf(stderr, "%s%x", (i == hlen) ? "" : ":", *ptr++); + printf("%s%x", (i == hlen) ? "" : ":", *ptr++); } while (--i > 0); } } @@ -83,17 +83,17 @@ print_tdreply(const char *mesg, size_t nbytes) while( (char *)p < mesg + nbytes ) { - fprintf(stderr, "ROUTE\t\t\t\tDEST\n"); - fprintf(stderr, "["); + printf("ROUTE\t\t\t\tDEST\n"); + printf("["); print_haddr(p->tdnbor_pnode, ETHADDRSIZ); - fprintf(stderr, "-"); + printf("-"); print_haddr(p->tdnbor_pif, ETHADDRSIZ); - fprintf(stderr, "] "); - fprintf(stderr, "["); + printf("] "); + printf("["); print_haddr(p->tdnbor_dnode, ETHADDRSIZ); - fprintf(stderr, "-"); + printf("-"); print_haddr(p->tdnbor_dif, ETHADDRSIZ); - fprintf(stderr, "]\n\n"); + printf("]\n\n"); p++; } @@ -108,17 +108,17 @@ print_tdpairs(const char *mesg, size_t nbytes) while( (char *)p < mesg + nbytes ) { - fprintf(stderr, "ROUTE\t\t\t\tDEST\n"); - fprintf(stderr, "["); + printf( "ROUTE\t\t\t\tDEST\n"); + printf( "["); print_haddr(p->tdnbor_pnode, ETHADDRSIZ); - fprintf(stderr, "-"); + printf( "-"); print_haddr(p->tdnbor_pif, ETHADDRSIZ); - fprintf(stderr, "] "); - fprintf(stderr, "["); + printf( "] "); + printf("["); print_haddr(p->tdnbor_dnode, ETHADDRSIZ); - fprintf(stderr, "-"); + printf("-"); print_haddr(p->tdnbor_dif, ETHADDRSIZ); - fprintf(stderr, "]\n\n"); + printf("]\n\n"); p++; } @@ -169,7 +169,7 @@ print_tdinq(const char *mesg) { topd_inqid_t *tip = (topd_inqid_t *)mesg; - fprintf(stderr, "\nINQ:%u.%u TTL:%d FACTOR:%d NODE:", + printf("\nINQ:%u.%u TTL:%d FACTOR:%d NODE:", ntohl(tip->tdi_tv.tv_sec), ntohl(tip->tdi_tv.tv_usec), ntohs(tip->tdi_ttl),