[or-cvs] Make directory functions update routerlist, not replace it....

Nick Mathewson nickm at seul.org
Mon May 17 20:31:04 UTC 2004


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

Modified Files:
	config.c directory.c dirserv.c main.c or.h routerlist.c 
Log Message:
Make directory functions update routerlist, not replace it.  Add notion of OR-is-trusted-to-be-a-dirserver. Arma, please review: does this handle being a dirserver right?

Index: config.c
===================================================================
RCS file: /home/or/cvsroot/src/or/config.c,v
retrieving revision 1.112
retrieving revision 1.113
diff -u -d -r1.112 -r1.113
--- config.c	10 May 2004 07:37:10 -0000	1.112
+++ config.c	17 May 2004 20:31:01 -0000	1.113
@@ -362,7 +362,7 @@
 ;
 
 int config_assign_default_dirservers(void) {
-  if(router_set_routerlist_from_string(default_dirservers_string) < 0) {
+  if(router_load_routerlist_from_string(default_dirservers_string, 1) < 0) {
     log_fn(LOG_WARN,"Bug: the default dirservers internal string is corrupt.");
     return -1;
   }

Index: directory.c
===================================================================
RCS file: /home/or/cvsroot/src/or/directory.c,v
retrieving revision 1.105
retrieving revision 1.106
diff -u -d -r1.105 -r1.106
--- directory.c	12 May 2004 23:48:57 -0000	1.105
+++ directory.c	17 May 2004 20:31:01 -0000	1.106
@@ -336,7 +336,7 @@
         connection_mark_for_close(conn);
         return -1;
       }
-      if(router_set_routerlist_from_directory(body, conn->identity_pkey) < 0){
+      if(router_load_routerlist_from_directory(body, conn->identity_pkey) < 0){
         log_fn(LOG_INFO,"...but parsing failed. Ignoring.");
       } else {
         log_fn(LOG_INFO,"updated routers.");

Index: dirserv.c
===================================================================
RCS file: /home/or/cvsroot/src/or/dirserv.c,v
retrieving revision 1.46
retrieving revision 1.47
diff -u -d -r1.46 -r1.47
--- dirserv.c	10 May 2004 17:30:51 -0000	1.46
+++ dirserv.c	17 May 2004 20:31:01 -0000	1.47
@@ -566,7 +566,7 @@
      * necessary, but safe is better than sorry. */
     new_directory = tor_strdup(the_directory);
     /* use a new copy of the dir, since get_dir_from_string scribbles on it */
-    if (router_set_routerlist_from_directory(new_directory, get_identity_key())) {
+    if (router_load_routerlist_from_directory(new_directory, get_identity_key())) {
       log_fn(LOG_ERR, "We just generated a directory we can't parse. Dying.");
       exit(0);
     }

Index: main.c
===================================================================
RCS file: /home/or/cvsroot/src/or/main.c,v
retrieving revision 1.267
retrieving revision 1.268
diff -u -d -r1.267 -r1.268
--- main.c	12 May 2004 23:48:57 -0000	1.267
+++ main.c	17 May 2004 20:31:01 -0000	1.268
@@ -668,10 +668,12 @@
   }
 
   /* load the routers file */
-  if(options.RouterFile &&
-     router_set_routerlist_from_file(options.RouterFile) < 0) {
-    log_fn(LOG_ERR,"Error loading router list.");
-    return -1;
+  if(options.RouterFile) {
+    routerlist_clear_trusted_directories();
+    if (router_load_routerlist_from_file(options.RouterFile, 1) < 0) {
+      log_fn(LOG_ERR,"Error loading router list.");
+      return -1;
+    }
   }
 
   if(options.DirPort) { /* the directory is already here, run startup things */

Index: or.h
===================================================================
RCS file: /home/or/cvsroot/src/or/or.h,v
retrieving revision 1.348
retrieving revision 1.349
diff -u -d -r1.348 -r1.349
--- or.h	15 May 2004 07:21:25 -0000	1.348
+++ or.h	17 May 2004 20:31:01 -0000	1.349
@@ -542,7 +542,6 @@
   crypto_pk_env_t *onion_pkey; /**< Public RSA key for onions. */
   crypto_pk_env_t *identity_pkey;  /**< Public RSA key for signing. */
 
-  int is_running; /**< As far as we know, is this OR currently running? */
 
   char *platform; /**< What software/operating system is this OR using? */
 
@@ -552,6 +551,10 @@
   uint32_t bandwidthburst; /**< How large is this OR's token bucket? */
   struct exit_policy_t *exit_policy; /**< What streams will this OR permit
                                       * to exit? */
+  /* local info */
+  int is_running; /**< As far as we know, is this OR currently running? */
+  int is_trusted_dir; /**< Do we trust this OR as a directory server? */
+
 } routerinfo_t;
 
 #define MAX_ROUTERS_IN_DIR 1024
@@ -561,7 +564,9 @@
   smartlist_t *routers;
   /** Which versions of tor are recommended by this directory? */
   char *software_versions;
-  /** When was this directory published? */
+  /** When was the most recent directory that contributed to this list
+   * published?
+   */
   time_t published_on;
 } routerlist_t;
 
@@ -1284,12 +1289,13 @@
 routerinfo_t *router_get_by_nickname(char *nickname);
 void router_get_routerlist(routerlist_t **prouterlist);
 void routerlist_free(routerlist_t *routerlist);
+void routerlist_clear_trusted_directories(void);
 void routerinfo_free(routerinfo_t *router);
 routerinfo_t *routerinfo_copy(const routerinfo_t *router);
 void router_mark_as_down(char *nickname);
-int router_set_routerlist_from_file(char *routerfile);
-int router_set_routerlist_from_string(const char *s);
-int router_set_routerlist_from_directory(const char *s, crypto_pk_env_t *pkey);
+int router_load_routerlist_from_file(char *routerfile, int trusted);
+int router_load_routerlist_from_string(const char *s, int trusted);
+int router_load_routerlist_from_directory(const char *s,crypto_pk_env_t *pkey);
 int router_compare_addr_to_exit_policy(uint32_t addr, uint16_t port,
                                        struct exit_policy_t *policy);
 #define ADDR_POLICY_ACCEPTED 0

Index: routerlist.c
===================================================================
RCS file: /home/or/cvsroot/src/or/routerlist.c,v
retrieving revision 1.75
retrieving revision 1.76
diff -u -d -r1.75 -r1.76
--- routerlist.c	12 May 2004 19:49:48 -0000	1.75
+++ routerlist.c	17 May 2004 20:31:01 -0000	1.76
@@ -44,8 +44,9 @@
   if(!choice) {
     log_fn(LOG_WARN,"No dirservers known. Reloading and trying again.");
     has_fetched_directory=0; /* reset it */
+    routerlist_clear_trusted_directories();
     if(options.RouterFile) {
-      if(router_set_routerlist_from_file(options.RouterFile) < 0)
+      if(router_load_routerlist_from_file(options.RouterFile, 1) < 0)
         return NULL;
     } else {
       if(config_assign_default_dirservers() < 0)
@@ -71,7 +72,7 @@
   sl = smartlist_create();
   for(i=0;i< smartlist_len(routerlist->routers); i++) {
     router = smartlist_get(routerlist->routers, i);
-    if(router->dir_port > 0 && router->is_running)
+    if(router->dir_port > 0 && router->is_running && router->is_trusted_dir)
       smartlist_add(sl, router);
   }
 
@@ -86,7 +87,7 @@
    * so we cycle through the list again. */
   for(i=0; i < smartlist_len(routerlist->routers); i++) {
     router = smartlist_get(routerlist->routers, i);
-    if(router->dir_port > 0) {
+    if(router->dir_port > 0 && router->is_trusted_dir) {
       router->is_running = 1;
       dirserver = router;
     }
@@ -300,13 +301,68 @@
   router->is_running = 0;
 }
 
+/** Add <b>router</b> to the routerlist, if we don't already have it.  Replace
+ * older entries (if any) with the same name.  Note: Callers should not hold
+ * their pointers to <b>router</b> after invoking this function; <b>router</b>
+ * will either be inserted into the routerlist or freed.  Returns 0 if the
+ * router was added; -1 if it was not.
+ */
+int router_add_to_routerlist(routerinfo_t *router) {
+  int i;
+  routerinfo_t *r;
+  /* If we have a router with this name, and the identity key is the same,
+   * choose the newer one. If the identity key has changed, drop the router.
+   */
+  for (i = 0; i < smartlist_len(routerlist->routers); ++i) {
+    r = smartlist_get(routerlist->routers, i);
+    if (!strcasecmp(router->nickname, r->nickname)) {
+      if (!crypto_pk_cmp_keys(router->identity_pkey, r->identity_pkey)) {
+        if (router->published_on > r->published_on) {
+          log_fn(LOG_DEBUG, "Replacing entry for router '%s'",
+                 router->nickname);
+          /* Remember whether we trust this router as a dirserver. */
+          if (r->is_trusted_dir)
+            router->is_trusted_dir = 1;
+          /* If the adress hasn't changed; no need to re-resolve. */
+          if (!strcasecmp(r->address, router->address))
+            router->addr = r->addr;
+          routerinfo_free(r);
+          smartlist_set(routerlist->routers, i, router);
+          return 0;
+        } else {
+          log_fn(LOG_DEBUG, "Skipping old entry for router '%s'",
+                 router->nickname);
+          /* If we now trust 'router', then we trust the one in the routerlist
+           * too. */
+          if (router->is_trusted_dir)
+            r->is_trusted_dir = 1;
+          /* Update the is_running status to whatever we were told. */
+          r->is_running = router->is_running;
+          routerinfo_free(router);
+          return -1;
+        }
+      } else {
+        log_fn(LOG_WARN, "Identity key mismatch for router '%s'",
+               router->nickname);
+        routerinfo_free(router);
+        return -1;
+      }
+    }
+  }
+  /* We haven't seen a router with this name before.  Add it to the end of
+   * the list. */
+  smartlist_add(routerlist->routers, router);
+  return 0;
+}
+
 /*
  * Code to parse router descriptors and directories.
  */
 
-/** Replace the current router list with the one stored in
- * <b>routerfile</b>. */
-int router_set_routerlist_from_file(char *routerfile)
+/** Update the current router list with the one stored in
+ * <b>routerfile</b>. If <b>trusted</b> is true, then we'll use
+ * directory servers from the file. */
+int router_load_routerlist_from_file(char *routerfile, int trusted)
 {
   char *string;
 
@@ -316,7 +372,7 @@
     return -1;
   }
 
-  if(router_set_routerlist_from_string(string) < 0) {
+  if(router_load_routerlist_from_string(string, trusted) < 0) {
     log_fn(LOG_WARN,"The routerfile itself was corrupt.");
     free(string);
     return -1;
@@ -327,15 +383,35 @@
   return 0;
 }
 
+/** Mark all directories in the routerlist as nontrusted. */
+void routerlist_clear_trusted_directories(void)
+{
+  if (!routerlist) return;
+  SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, r,
+                    r->is_trusted_dir = 0);
+}
+
 /** Helper function: read routerinfo elements from s, and throw out the
- * ones that don't parse and resolve.  Replace the current
- * routerlist. */
-int router_set_routerlist_from_string(const char *s)
+ * ones that don't parse and resolve.  Add all remaining elements to the
+ * routerlist.  If <b>trusted</b> is true, then we'll use
+ * directory servers from the string
+ */
+int router_load_routerlist_from_string(const char *s, int trusted)
 {
-  if (router_parse_list_from_string(&s, &routerlist, -1, NULL)) {
+  routerlist_t *new_list=NULL;
+
+  if (router_parse_list_from_string(&s, &new_list, -1, NULL)) {
     log(LOG_WARN, "Error parsing router file");
     return -1;
   }
+  if (trusted) {
+    SMARTLIST_FOREACH(new_list->routers, routerinfo_t *, r,
+                      if (r->dir_port) r->is_trusted_dir = 1);
+  }
+  SMARTLIST_FOREACH(new_list->routers, routerinfo_t *, r,
+                    router_add_to_routerlist(r));
+  smartlist_clear(new_list->routers);
+  routerlist_free(new_list);
   if (router_resolve_routerlist(routerlist)) {
     log(LOG_WARN, "Error resolving routerlist");
     return -1;
@@ -370,16 +446,25 @@
 /** Replace the current routerlist with the routers stored in the
  * signed directory <b>s</b>.  If pkey is provided, make sure that <b>s</b> is
  * signed with pkey. */
-int router_set_routerlist_from_directory(const char *s, crypto_pk_env_t *pkey)
+int router_load_routerlist_from_directory(const char *s, crypto_pk_env_t *pkey)
 {
-  if (router_parse_routerlist_from_directory(s, &routerlist, pkey)) {
+  routerlist_t *new_list = NULL;
+  if (router_parse_routerlist_from_directory(s, &new_list, pkey)) {
     log_fn(LOG_WARN, "Couldn't parse directory.");
     return -1;
   }
+  SMARTLIST_FOREACH(new_list->routers, routerinfo_t *, r,
+                    router_add_to_routerlist(r));
+  smartlist_clear(new_list->routers);
   if (router_resolve_routerlist(routerlist)) {
     log_fn(LOG_WARN, "Error resolving routerlist");
     return -1;
   }
+  routerlist->published_on = new_list->published_on;
+  tor_free(routerlist->software_versions);
+  routerlist->software_versions = new_list->software_versions;
+  new_list->software_versions = NULL;
+  routerlist_free(new_list);
   if (!is_recommended_version(VERSION, routerlist->software_versions)) {
     log(options.IgnoreVersion ? LOG_WARN : LOG_ERR,
      "You are running Tor version %s, which will not work with this network.\n"
@@ -432,7 +517,9 @@
     r = smartlist_get(rl->routers,i);
     if (router_is_me(r)) {
       remove = 1;
-    } else if (router_resolve(r)) {
+    } else if (r->addr) {
+      /* already resolved. */
+    } else if (!router_resolve(r)) {
       log_fn(LOG_WARN, "Couldn't resolve router %s; not using", r->address);
       remove = 1;
     }



More information about the tor-commits mailing list