[or-cvs] Cache running-routers; compress running-routers; serve comp...

Nick Mathewson nickm at seul.org
Mon Nov 15 04:04:23 UTC 2004


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

Modified Files:
	directory.c dirserv.c or.h routerparse.c 
Log Message:
Cache running-routers; compress running-routers; serve compressed running-routers.z

Index: directory.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/or/directory.c,v
retrieving revision 1.167
retrieving revision 1.168
diff -u -d -r1.167 -r1.168
--- directory.c	15 Nov 2004 03:53:03 -0000	1.167
+++ directory.c	15 Nov 2004 04:04:20 -0000	1.168
@@ -780,23 +780,26 @@
     return 0;
   }
 
-  if(!strcmp(url,"/tor/running-routers")) { /* running-routers fetch */
+  if(!strcmp(url,"/tor/running-routers") ||
+     !strcmp(url,"/tor/running-routers.z")) { /* running-routers fetch */
+    int deflated = !strcmp(url,"/tor/dir.z");
     tor_free(url);
     if(!authdir_mode(get_options())) {
       /* For now, we don't cache running-routers. Reject. */
       connection_write_to_buf(answer400, strlen(answer400), conn);
       return 0;
     }
-    dlen = dirserv_get_runningrouters(&cp);
-    if(!dlen) { /* we failed to create cp */
+    dlen = dirserv_get_runningrouters(&cp, deflated);
+    if(!dlen) { /* we failed to create/cache cp */
       connection_write_to_buf(answer503, strlen(answer503), conn);
       return 0;
     }
 
     format_rfc1123_time(date, time(NULL));
-    tor_snprintf(tmp, sizeof(tmp), "HTTP/1.0 200 OK\r\nDate: %s\r\nContent-Length: %d\r\nContent-Type: text/plain\r\n\r\n",
-             date,
-             (int)dlen);
+    tor_snprintf(tmp, sizeof(tmp), "HTTP/1.0 200 OK\r\nDate: %s\r\nContent-Length: %d\r\nContent-Type: text/plain\r\nContent-Encoding: %s\r\n\r\n",
+                 date,
+                 (int)dlen,
+                 deflated?"deflate":"identity");
     connection_write_to_buf(tmp, strlen(tmp), conn);
     connection_write_to_buf(cp, strlen(cp), conn);
     return 0;

Index: dirserv.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/or/dirserv.c,v
retrieving revision 1.118
retrieving revision 1.119
diff -u -d -r1.118 -r1.119
--- dirserv.c	9 Nov 2004 20:04:00 -0000	1.118
+++ dirserv.c	15 Nov 2004 04:04:20 -0000	1.119
@@ -693,53 +693,65 @@
 static char *the_directory_z = NULL;
 static size_t the_directory_z_len = 0;
 
-static char *cached_directory = NULL; /* used only by non-auth dirservers */
-static size_t cached_directory_len = 0;
-static char *cached_directory_z = NULL;
-static size_t cached_directory_z_len = 0;
-static time_t cached_directory_published = 0;
+typedef struct cached_dir_t {
+  char *dir;
+  char *dir_z;
+  size_t dir_len;
+  size_t dir_z_len;
+  time_t published;
+} cached_dir_t;
+
+/* used only by non-auth dirservers */
+static cached_dir_t cached_directory = { NULL, NULL, 0, 0, 0 };
+static cached_dir_t cached_runningrouters = { NULL, NULL, 0, 0, 0 };
 
 /** If we have no cached directory, or it is older than <b>when</b>, then
  * replace it with <b>directory</b>, published at <b>when</b>.
  */
-void dirserv_set_cached_directory(const char *directory, time_t when)
+void dirserv_set_cached_directory(const char *directory, time_t when,
+                                  int is_running_routers)
 {
   time_t now;
-  char filename[512];
+  cached_dir_t *d;
   tor_assert(!get_options()->AuthoritativeDir);
   now = time(NULL);
-  if (when<=cached_directory_published) {
+  d = is_running_routers ? &cached_runningrouters : &cached_directory;
+  if (when<=d->published) {
     log_fn(LOG_INFO, "Ignoring old directory; not caching.");
   } else if (when>=now+ROUTER_ALLOW_SKEW) {
     log_fn(LOG_INFO, "Ignoring future directory; not caching.");
-  } else if (when>cached_directory_published &&
+  } else if (when>d->published &&
         when<now+ROUTER_ALLOW_SKEW) {
     log_fn(LOG_DEBUG, "Caching directory.");
-    tor_free(cached_directory);
-    cached_directory = tor_strdup(directory);
-    cached_directory_len = strlen(cached_directory);
-    tor_free(cached_directory_z);
-    if (tor_gzip_compress(&cached_directory_z, &cached_directory_z_len,
-                          cached_directory, cached_directory_len,
+    tor_free(d->dir);
+    d->dir = tor_strdup(directory);
+    d->dir_len = strlen(directory);
+    tor_free(d->dir_z);
+    if (tor_gzip_compress(&(d->dir_z), &(d->dir_z_len), d->dir, d->dir_len,
                           ZLIB_METHOD)) {
       log_fn(LOG_WARN,"Error compressing cached directory");
     }
-    cached_directory_published = when;
-    tor_snprintf(filename,sizeof(filename),"%s/cached-directory", get_options()->DataDirectory);
-    if(write_str_to_file(filename,cached_directory,0) < 0) {
-      log_fn(LOG_WARN, "Couldn't write cached directory to disk. Ignoring.");
+    d->published = when;
+    if (!is_running_routers) {
+      char filename[512];
+      tor_snprintf(filename,sizeof(filename),"%s/cached-directory", get_options()->DataDirectory);
+      if(write_str_to_file(filename,cached_directory.dir,0) < 0) {
+        log_fn(LOG_WARN, "Couldn't write cached directory to disk. Ignoring.");
+      }
     }
   }
 }
 
 /** Set *<b>directory</b> to the most recently generated encoded signed
- * directory, generating a new one as necessary. */
+ * directory, generating a new one as necessary.  If not an authoritative
+ * directory may return 0 if no directory is yet cached.*/
 size_t dirserv_get_directory(const char **directory, int compress)
 {
   if (!get_options()->AuthoritativeDir) {
-    if (compress?cached_directory_z:cached_directory) {
-      *directory = compress?cached_directory_z:cached_directory;
-      return compress?cached_directory_z_len:cached_directory_len;
+    cached_dir_t *d = &cached_directory;
+    *directory = compress ? d->dir_z : d->dir;
+    if (*directory) {
+      return compress ? d->dir_z_len : d->dir_len;
     } else {
       /* no directory yet retrieved */
       return 0;
@@ -799,8 +811,10 @@
   return 0;
 }
 
-static char *runningrouters_string=NULL;
-static size_t runningrouters_len=0;
+static char *the_runningrouters=NULL;
+static size_t the_runningrouters_len=0;
+static char *the_runningrouters_z=NULL;
+static size_t the_runningrouters_z_len=0;
 
 /** Replace the current running-routers list with a newly generated one. */
 static int generate_runningrouters(crypto_pk_env_t *private_key)
@@ -867,9 +881,16 @@
     goto err;
   }
 
-  tor_free(runningrouters_string);
-  runningrouters_string = s;
-  runningrouters_len = strlen(s);
+  tor_free(the_runningrouters);
+  the_runningrouters = s;
+  the_runningrouters_len = strlen(s);
+  tor_free(the_runningrouters_z);
+  if (tor_gzip_compress(&the_runningrouters_z, &the_runningrouters_z_len,
+                        the_runningrouters, the_runningrouters_len,
+                        ZLIB_METHOD)) {
+    log_fn(LOG_WARN, "Error gzipping runningrouters");
+    return -1;
+  }
   runningrouters_is_dirty = 0;
   return 0;
  err:
@@ -881,8 +902,18 @@
 /** Set *<b>rr</b> to the most recently generated encoded signed
  * running-routers list, generating a new one as necessary.  Return the
  * size of the directory on success, and 0 on failure. */
-size_t dirserv_get_runningrouters(const char **rr)
+size_t dirserv_get_runningrouters(const char **rr, int compress)
 {
+  if (!get_options()->AuthoritativeDir) {
+    cached_dir_t *d = &cached_runningrouters;
+    *rr = compress ? d->dir_z : d->dir;
+    if (*rr) {
+      return compress ? d->dir_z_len : d->dir_len;
+    } else {
+      /* no directory yet retrieved */
+      return 0;
+    }
+  }
   if (runningrouters_is_dirty &&
       runningrouters_is_dirty + DIR_REGEN_SLACK_TIME < time(NULL)) {
     if(generate_runningrouters(get_identity_key())) {
@@ -890,8 +921,8 @@
       return 0;
     }
   }
-  *rr = runningrouters_string;
-  return runningrouters_len;
+  *rr = compress ? the_runningrouters_z : the_runningrouters;
+  return compress ? the_runningrouters_z_len : the_runningrouters_len;
 }
 
 /*

Index: or.h
===================================================================
RCS file: /home/or/cvsroot/tor/src/or/or.h,v
retrieving revision 1.484
retrieving revision 1.485
diff -u -d -r1.484 -r1.485
--- or.h	15 Nov 2004 04:01:31 -0000	1.484
+++ or.h	15 Nov 2004 04:04:20 -0000	1.485
@@ -1307,8 +1307,9 @@
                                      crypto_pk_env_t *private_key);
 void directory_set_dirty(void);
 size_t dirserv_get_directory(const char **cp, int compress);
-size_t dirserv_get_runningrouters(const char **rr);
-void dirserv_set_cached_directory(const char *directory, time_t when);
+size_t dirserv_get_runningrouters(const char **rr, int compress);
+void dirserv_set_cached_directory(const char *directory, time_t when,
+                                  int is_running_routers);
 
 /********************************* dns.c ***************************/
 

Index: routerparse.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/or/routerparse.c,v
retrieving revision 1.78
retrieving revision 1.79
diff -u -d -r1.78 -r1.79
--- routerparse.c	14 Nov 2004 21:35:30 -0000	1.78
+++ routerparse.c	15 Nov 2004 04:04:20 -0000	1.79
@@ -390,7 +390,7 @@
   if(!get_options()->AuthoritativeDir) {
     /* Now that we know the signature is okay, and we have a
      * publication time, cache the directory. */
-    dirserv_set_cached_directory(str, published_on);
+    dirserv_set_cached_directory(str, published_on, 0);
   }
 
   if (!(tok = find_first_by_keyword(tokens, K_RECOMMENDED_SOFTWARE))) {
@@ -475,6 +475,7 @@
   return r;
 }
 
+/* DOCDOC */
 running_routers_t *
 router_parse_runningrouters(const char *str)
 {
@@ -514,6 +515,12 @@
      goto err;
   }
 
+  if(!get_options()->AuthoritativeDir) {
+    /* Now that we know the signature is okay, and we have a
+     * publication time, cache the list. */
+    dirserv_set_cached_directory(str, published_on, 1);
+  }
+
   if (!(tok = find_first_by_keyword(tokens, K_ROUTER_STATUS))) {
     if (!(tok = find_first_by_keyword(tokens, K_RUNNING_ROUTERS))) {
       log_fn(LOG_WARN,



More information about the tor-commits mailing list