[or-cvs] choose randomly from running dirservers, not always the fir...

Roger Dingledine arma at seul.org
Wed Dec 3 10:39:30 UTC 2003


Update of /home/or/cvsroot/src/or
In directory moria.mit.edu:/home2/arma/work/onion/cvs/src/or

Modified Files:
	routers.c 
Log Message:
choose randomly from running dirservers, not always the first one


Index: routers.c
===================================================================
RCS file: /home/or/cvsroot/src/or/routers.c,v
retrieving revision 1.99
retrieving revision 1.100
diff -u -d -r1.99 -r1.100
--- routers.c	3 Dec 2003 10:28:51 -0000	1.99
+++ routers.c	3 Dec 2003 10:39:27 -0000	1.100
@@ -50,31 +50,48 @@
 }
 
 routerinfo_t *router_pick_directory_server(void) {
-  /* pick the first running router with a positive dir_port */
-  int i;
+  /* pick a random running router with a positive dir_port */
+  int i,j;
   routerinfo_t *router, *dirserver=NULL;
-  
+  int num_dirservers=0;
+
   if(!directory)
     return NULL;
 
   for(i=0;i<directory->n_routers;i++) {
     router = directory->routers[i];
     if(router->dir_port > 0 && router->is_running)
-      return router;
+      num_dirservers++;
   }
 
-  log_fn(LOG_INFO,"No dirservers are reachable. Trying them all again.");
-  /* no running dir servers found? go through and mark them all as up,
-   * and we'll cycle through the list again. */
-  for(i=0;i<directory->n_routers;i++) {
-    router = directory->routers[i];
-    if(router->dir_port > 0) {
-      router->is_running = 1;
-      dirserver = router;
+  if(!num_dirservers) {
+    log_fn(LOG_INFO,"No dirservers are reachable. Trying them all again.");
+    /* no running dir servers found? go through and mark them all as up,
+     * and we'll cycle through the list again. */
+    for(i=0;i<directory->n_routers;i++) {
+      router = directory->routers[i];
+      if(router->dir_port > 0) {
+        router->is_running = 1;
+        dirserver = router;
+      }
     }
+    return dirserver;
   }
 
-  return dirserver;
+  j = crypto_pseudo_rand_int(num_dirservers);
+  for (i=0;i<directory->n_routers;i++) {
+    router = directory->routers[i];
+    if (router->dir_port > 0 && router->is_running) {
+      if (j)
+        --j;
+      else {
+        log_fn(LOG_DEBUG, "Chose server '%s'", router->nickname);
+        return router;
+      }
+    }
+  }
+  assert(0);
+  return NULL;
 }
 
 routerinfo_t *router_pick_randomly_from_running(void) {



More information about the tor-commits mailing list