[or-cvs] Backport directory connection retry code

Nick Mathewson nickm at seul.org
Mon Jan 3 21:24:09 UTC 2005


Update of /home/or/cvsroot/tor/src/or
In directory moria.mit.edu:/tmp/cvs-serv2389/or

Modified Files:
      Tag: tor-0_0_9-patches
	connection.c directory.c main.c or.h rendclient.c routerlist.c 
Log Message:
Backport directory connection retry code

Index: connection.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/or/connection.c,v
retrieving revision 1.310.2.1
retrieving revision 1.310.2.2
diff -u -d -r1.310.2.1 -r1.310.2.2
--- connection.c	3 Jan 2005 19:53:35 -0000	1.310.2.1
+++ connection.c	3 Jan 2005 21:24:06 -0000	1.310.2.2
@@ -208,13 +208,7 @@
       if (conn->state == DIR_CONN_STATE_CONNECTING) {
         /* it's a directory server and connecting failed: forget about
            this router */
-        router_mark_as_down(conn->identity_digest);
-        if (conn->purpose == DIR_PURPOSE_FETCH_DIR &&
-            !all_trusted_directory_servers_down()) {
-          log_fn(LOG_INFO,"Giving up on dirserver %s; trying another.",
-                 conn->address);
-         directory_get_from_dirserver(DIR_PURPOSE_FETCH_DIR, NULL);
-        }
+        connection_dir_connect_failed(conn);
       }
       if (conn->purpose == DIR_PURPOSE_FETCH_RENDDESC)
         rend_client_desc_fetched(conn->rend_query, 0);

Index: directory.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/or/directory.c,v
retrieving revision 1.181.2.1
retrieving revision 1.181.2.2
diff -u -d -r1.181.2.1 -r1.181.2.2
--- directory.c	3 Jan 2005 18:44:29 -0000	1.181.2.1
+++ directory.c	3 Jan 2005 21:24:06 -0000	1.181.2.2
@@ -143,9 +143,12 @@
  * connection purpose 'purpose' requesting 'resource'.  The purpose
  * should be one of 'DIR_PURPOSE_FETCH_DIR',
  * 'DIR_PURPOSE_FETCH_RENDDESC', 'DIR_PURPOSE_FETCH_RUNNING_LIST.'
+ * If <b>retry_if_no_servers</b>, then if all the possible servers seem
+ * down, mark them up and try again.
  */
 void
-directory_get_from_dirserver(uint8_t purpose, const char *resource)
+directory_get_from_dirserver(uint8_t purpose, const char *resource,
+                             int retry_if_no_servers)
 {
   routerinfo_t *r = NULL;
   trusted_dir_server_t *ds = NULL;
@@ -155,30 +158,34 @@
       purpose == DIR_PURPOSE_FETCH_RUNNING_LIST) {
     if (advertised_server_mode()) {
       /* only ask authdirservers, and don't ask myself */
-      ds = router_pick_trusteddirserver(1, fascistfirewall);
+      ds = router_pick_trusteddirserver(1, fascistfirewall,
+                                        retry_if_no_servers);
     } else {
       /* anybody with a non-zero dirport will do */
       r = router_pick_directory_server(1, fascistfirewall,
-                                purpose==DIR_PURPOSE_FETCH_RUNNING_LIST);
+                                purpose==DIR_PURPOSE_FETCH_RUNNING_LIST,
+                                       retry_if_no_servers);
       if (!r) {
         log_fn(LOG_INFO, "No router found for %s; falling back to dirserver list",
                purpose == DIR_PURPOSE_FETCH_RUNNING_LIST
                ? "status list" : "directory");
-        ds = router_pick_trusteddirserver(1, fascistfirewall);
+        ds = router_pick_trusteddirserver(1, fascistfirewall,
+                                          retry_if_no_servers);
       }
     }
   } else { // (purpose == DIR_PURPOSE_FETCH_RENDDESC)
     /* only ask authdirservers, any of them will do */
     /* Never use fascistfirewall; we're going via Tor. */
-    ds = router_pick_trusteddirserver(0, 0);
+    ds = router_pick_trusteddirserver(0, 0, retry_if_no_servers);
   }
 
   if (r)
     directory_initiate_command_router(r, purpose, resource, NULL, 0);
   else if (ds)
     directory_initiate_command_trusted_dir(ds, purpose, resource, NULL, 0);
-  else
+  else {
     log_fn(LOG_WARN,"No running dirservers known. Not trying. (purpose %d)", purpose);
+  }
 }
 
 /** Launch a new connection to the directory server <b>router</b> to upload or
@@ -213,6 +220,22 @@
                NULL, dirserv->digest, purpose, resource, payload, payload_len);
 }
 
+/** Called when we are unable to complete our connection to a
+ * directory server: Mark the router as down and try again if possible.
+ */
+int
+connection_dir_connect_failed(connection_t *conn)
+{
+  router_mark_as_down(conn->identity_digest); /* don't try him again */
+  if (conn->purpose == DIR_PURPOSE_FETCH_DIR ||
+      conn->purpose == DIR_PURPOSE_FETCH_RUNNING_LIST) {
+    log_fn(LOG_INFO, "Giving up on directory server at '%s'; retrying",
+           conn->address);
+    directory_get_from_dirserver(conn->purpose, NULL,
+                                 0 /* don't retry_if_no_servers */);
+  }
+}
+
 /** Helper for directory_initiate_command(router|trusted_dir): send the
  * command to a server whose address is <b>address</b>, whose IP is
  * <b>addr</b>, whose directory port is <b>dir_port</b>, whose tor version is
@@ -282,12 +305,7 @@
     /* then we want to connect directly */
     switch (connection_connect(conn, conn->address, addr, dir_port)) {
       case -1:
-        router_mark_as_down(conn->identity_digest); /* don't try him again */
-        if (purpose == DIR_PURPOSE_FETCH_DIR &&
-            !all_trusted_directory_servers_down()) {
-          log_fn(LOG_INFO,"Giving up on dirserver '%s'; trying another.", conn->address);
-          directory_get_from_dirserver(purpose, NULL);
-        }
+        connection_dir_connect_failed(conn);
         connection_free(conn);
         return;
       case 1:

Index: main.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/or/main.c,v
retrieving revision 1.407.2.1
retrieving revision 1.407.2.2
diff -u -d -r1.407.2.1 -r1.407.2.2
--- main.c	25 Dec 2004 06:42:47 -0000	1.407.2.1
+++ main.c	3 Jan 2005 21:24:06 -0000	1.407.2.2
@@ -594,7 +594,7 @@
       router_retry_connections();
     }
 
-    directory_get_from_dirserver(DIR_PURPOSE_FETCH_DIR, NULL);
+    directory_get_from_dirserver(DIR_PURPOSE_FETCH_DIR, NULL, 1);
     time_to_fetch_directory = now + options->DirFetchPeriod;
     if (time_to_fetch_running_routers < now + options->StatusFetchPeriod) {
       time_to_fetch_running_routers = now + options->StatusFetchPeriod;
@@ -606,7 +606,7 @@
 
   if (time_to_fetch_running_routers < now) {
     if (!authdir_mode(options)) {
-      directory_get_from_dirserver(DIR_PURPOSE_FETCH_RUNNING_LIST, NULL);
+      directory_get_from_dirserver(DIR_PURPOSE_FETCH_RUNNING_LIST, NULL, 1);
     }
     time_to_fetch_running_routers = now + options->StatusFetchPeriod;
   }
@@ -768,7 +768,7 @@
     }
   }
   /* Fetch a new directory. Even authdirservers do this. */
-  directory_get_from_dirserver(DIR_PURPOSE_FETCH_DIR, NULL);
+  directory_get_from_dirserver(DIR_PURPOSE_FETCH_DIR, NULL, 1);
   if (server_mode(options)) {
     /* Restart cpuworker and dnsworker processes, so they get up-to-date
      * configuration options. */

Index: or.h
===================================================================
RCS file: /home/or/cvsroot/tor/src/or/or.h,v
retrieving revision 1.508.2.2
retrieving revision 1.508.2.3
diff -u -d -r1.508.2.2 -r1.508.2.3
--- or.h	24 Dec 2004 02:19:29 -0000	1.508.2.2
+++ or.h	3 Jan 2005 21:24:06 -0000	1.508.2.3
@@ -1313,11 +1313,13 @@
 int dir_policy_permits_address(uint32_t addr);
 void directory_post_to_dirservers(uint8_t purpose, const char *payload,
                                   size_t payload_len);
-void directory_get_from_dirserver(uint8_t purpose, const char *resource);
+void directory_get_from_dirserver(uint8_t purpose, const char *resource,
+                                  int retry_if_no_server);
 int connection_dir_reached_eof(connection_t *conn);
 int connection_dir_process_inbuf(connection_t *conn);
 int connection_dir_finished_flushing(connection_t *conn);
 int connection_dir_finished_connecting(connection_t *conn);
+int connection_dir_connect_failed(connection_t *conn);
 void parse_dir_policy(void);
 
 /********************************* dirserv.c ***************************/
@@ -1571,8 +1573,13 @@
 
 int router_reload_router_list(void);
 void router_get_trusted_dir_servers(smartlist_t **outp);
-routerinfo_t *router_pick_directory_server(int requireothers, int fascistfirewall, int for_running_routers);
-trusted_dir_server_t *router_pick_trusteddirserver(int requireothers, int fascistfirewall);
+routerinfo_t *router_pick_directory_server(int requireothers,
+                                           int fascistfirewall,
+                                           int for_running_routers,
+                                           int retry_if_no_servers);
+trusted_dir_server_t *router_pick_trusteddirserver(int requireothers,
+                                                   int fascistfirewall,
+                                                   int retry_if_no_servers);
 int all_trusted_directory_servers_down(void);
 struct smartlist_t;
 void routerlist_add_family(struct smartlist_t *sl, routerinfo_t *router);
@@ -1698,4 +1705,3 @@
 void assert_addr_policy_ok(addr_policy_t *t);
 
 #endif
-

Index: rendclient.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/or/rendclient.c,v
retrieving revision 1.71
retrieving revision 1.71.2.1
diff -u -d -r1.71 -r1.71.2.1
--- rendclient.c	7 Dec 2004 19:42:45 -0000	1.71
+++ rendclient.c	3 Jan 2005 21:24:06 -0000	1.71.2.1
@@ -250,7 +250,7 @@
     log_fn(LOG_INFO,"Would fetch a new renddesc here (for %s), but one is already in progress.", query);
   } else {
     /* not one already; initiate a dir rend desc lookup */
-    directory_get_from_dirserver(DIR_PURPOSE_FETCH_RENDDESC, query);
+    directory_get_from_dirserver(DIR_PURPOSE_FETCH_RENDDESC, query, 1);
   }
 }
 

Index: routerlist.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/or/routerlist.c,v
retrieving revision 1.200
retrieving revision 1.200.2.1
diff -u -d -r1.200 -r1.200.2.1
--- routerlist.c	7 Dec 2004 08:51:10 -0000	1.200
+++ routerlist.c	3 Jan 2005 21:24:07 -0000	1.200.2.1
@@ -95,7 +95,8 @@
  */
 routerinfo_t *router_pick_directory_server(int requireothers,
                                            int fascistfirewall,
-                                           int for_runningrouters) {
+                                           int for_runningrouters,
+                                           int retry_if_no_servers) {
   routerinfo_t *choice;
 
   if (!routerlist)
@@ -103,7 +104,7 @@
 
   choice = router_pick_directory_server_impl(requireothers, fascistfirewall,
                                              for_runningrouters);
-  if (choice)
+  if (choice || !retry_if_no_servers)
     return choice;
 
   log_fn(LOG_INFO,"No reachable router entries for dirservers. Trying them all again.");
@@ -128,11 +129,12 @@
 }
 
 trusted_dir_server_t *router_pick_trusteddirserver(int requireothers,
-                                                   int fascistfirewall) {
+                                                   int fascistfirewall,
+                                                   int retry_if_no_servers) {
   trusted_dir_server_t *choice;
 
   choice = router_pick_trusteddirserver_impl(requireothers, fascistfirewall);
-  if (choice)
+  if (choice || !retry_if_no_servers)
     return choice;
 
   log_fn(LOG_INFO,"No trusted dirservers are reachable. Trying them all again.");



More information about the tor-commits mailing list