aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRoger Dingledine <arma@torproject.org>2003-12-03 10:39:27 +0000
committerRoger Dingledine <arma@torproject.org>2003-12-03 10:39:27 +0000
commit4f2d5565087a05dce8eb8d2366d74f4c0726c501 (patch)
tree29d80b86d0fc91aa2fae024f0bb905ea7372553e /src
parent63f81bddae0e5b80d2702ee7780346d03bdad84f (diff)
downloadtor-4f2d5565087a05dce8eb8d2366d74f4c0726c501.tar
tor-4f2d5565087a05dce8eb8d2366d74f4c0726c501.tar.gz
choose randomly from running dirservers, not always the first one
svn:r881
Diffstat (limited to 'src')
-rw-r--r--src/or/routers.c43
1 files changed, 30 insertions, 13 deletions
diff --git a/src/or/routers.c b/src/or/routers.c
index 6d159016c..1c83120c1 100644
--- a/src/or/routers.c
+++ b/src/or/routers.c
@@ -50,31 +50,48 @@ void router_retry_connections(void) {
}
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++;
+ }
+
+ 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;
}
- 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++) {
+ 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 = 1;
- dirserver = router;
+ if (router->dir_port > 0 && router->is_running) {
+ if (j)
+ --j;
+ else {
+ log_fn(LOG_DEBUG, "Chose server '%s'", router->nickname);
+ return router;
+ }
}
}
-
- return dirserver;
+ assert(0);
+ return NULL;
}
routerinfo_t *router_pick_randomly_from_running(void) {