[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[or-cvs] getrouters() changed so that a router ignores its own entry...



Update of /home/or/cvsroot/src/or
In directory moria.seul.org:/tmp/cvs-serv29651

Modified Files:
	main.c or.h routers.c 
Log Message:
getrouters() changed so that a router ignores its own entry in the router list 


Index: main.c
===================================================================
RCS file: /home/or/cvsroot/src/or/main.c,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- main.c	8 Jul 2002 08:59:15 -0000	1.6
+++ main.c	10 Jul 2002 12:35:59 -0000	1.7
@@ -302,7 +302,7 @@
   int i;
 
   /* load the routers file */
-  router_array = getrouters(options[RouterFile].r.str,&rarray_len);
+  router_array = getrouters(options[RouterFile].r.str,&rarray_len, options[ORPort].r.i);
   if (!router_array)
   {
     log(LOG_ERR,"Error loading router list.");

Index: or.h
===================================================================
RCS file: /home/or/cvsroot/src/or/or.h,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- or.h	8 Jul 2002 08:59:15 -0000	1.6
+++ or.h	10 Jul 2002 12:35:59 -0000	1.7
@@ -549,7 +549,7 @@
 
 /********************************* routers.c ***************************/
 
-routerinfo_t **getrouters(char *routerfile, size_t *listlenp);
+routerinfo_t **getrouters(char *routerfile, size_t *listlenp, uint16_t or_listenport);
 void delete_routerlist(routerinfo_t *list);
 /* create an NULL-terminated array of pointers pointing to elements of a router list */
 routerinfo_t **make_rarray(routerinfo_t* list, size_t *listlenp);

Index: routers.c
===================================================================
RCS file: /home/or/cvsroot/src/or/routers.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- routers.c	2 Jul 2002 09:36:58 -0000	1.2
+++ routers.c	10 Jul 2002 12:35:59 -0000	1.3
@@ -10,6 +10,42 @@
 
 #include "or.h"
 
+/* private function, to determine whether the current entry in the router list is actually us */
+static int routers_is_us(uint32_t or_address, uint16_t or_listenport, uint16_t my_or_listenport)
+{
+  /* local host information */
+  char localhostname[512];
+  struct hostent *localhost;
+  
+  char *addr = NULL;
+  int i = 0;
+  
+  /* obtain local host information */
+  if (gethostname(localhostname,512) < 0) {
+    log(LOG_ERR,"Error obtaining local hostname.");
+    return -1;
+  }
+  localhost = gethostbyname(localhostname);
+  if (!localhost) {
+    log(LOG_ERR,"Error obtaining local host info.");
+    return -1;
+  }
+  
+  /* check host addresses for a match with or_address above */
+  addr = localhost->h_addr_list[i++]; /* set to the first local address */
+  while(addr)
+  {
+    if (!memcmp((void *)&or_address, (void *)addr, sizeof(uint32_t))) { /* addresses match */
+      if (or_listenport == htons(my_or_listenport)) /* ports also match */
+	      return 1;
+    }
+    
+    addr = localhost->h_addr_list[i++];
+  }
+  
+  return 0;
+}
+
 /* delete a list of routers from memory */
 void delete_routerlist(routerinfo_t *list)
 {
@@ -76,7 +112,7 @@
 }
 
 /* load the router list */
-routerinfo_t **getrouters(char *routerfile, size_t *lenp)
+routerinfo_t **getrouters(char *routerfile, size_t *lenp, uint16_t or_listenport)
 {
   int retval = 0;
   char *retp = NULL;
@@ -167,8 +203,8 @@
 	  if ((*token != '\0') && (*errtest == '\0')) /* conversion was successful */
 	  {
 /* FIXME patch from RD. We should make it actually read these. */
-            router->op_port = htons(router->or_port + 10);
-            router->ap_port = htons(router->or_port + 20);
+	    router->op_port = htons(router->or_port + 10);
+	    router->ap_port = htons(router->or_port + 20);
 	    /* convert port to network format */
 	    router->or_port = htons(router->or_port);
 	    
@@ -208,7 +244,7 @@
 			  if (!retp)
 			  {
 			    log(LOG_ERR,"Could not find a public key entry for router %s:%u.",
-			        router->address,router->or_port);
+				router->address,router->or_port);
 			    free((void *)router->address);
 			    free((void *)router);
 			    fclose(rf);
@@ -273,13 +309,34 @@
 			    delete_routerlist(routerlist);
 			    return NULL;
 			  }
-			  router->next = NULL;
-			  /* save the entry into the routerlist linked list */
-			  if (!routerlist) /* this is the first entry */
-			    routerlist = router;
-			  else
-			    lastrouter->next = (void *)router;
-			  lastrouter = router;
+			  
+			  /* check that this router doesn't actually represent us */
+			  retval = routers_is_us(router->addr, router->or_port, or_listenport);
+			  if (!retval) { /* this isn't us, continue */
+			    router->next = NULL;
+			    /* save the entry into the routerlist linked list */
+			    if (!routerlist) /* this is the first entry */
+				    routerlist = router;
+			    else
+				    lastrouter->next = (void *)router;
+			    lastrouter = router;
+			  }
+			  else if (retval == 1) /* this is us, ignore */
+			  {
+			    log(LOG_DEBUG,"getrouters(): This entry is actually me. Ignoring.");
+			    free((void *)router->address);
+			    RSA_free(router->pkey);
+			    free((void *)router);
+			  }
+			  else /* routers_is_us() returned an error */
+			  {
+			    free((void *)router->address);
+			    RSA_free(router->pkey);
+			    free((void *)router);
+			    fclose(rf);
+			    delete_routerlist(routerlist);
+			    return NULL;
+			  }
 			}
 		      }
 		      else /* maximum link utilisation is zero */