[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] r10294: Extract common code from networkstatus_getinfo_helper_single (in tor/trunk: . doc src/or)



Author: nickm
Date: 2007-05-22 13:58:30 -0400 (Tue, 22 May 2007)
New Revision: 10294

Modified:
   tor/trunk/
   tor/trunk/doc/TODO
   tor/trunk/src/or/dirserv.c
   tor/trunk/src/or/or.h
   tor/trunk/src/or/router.c
   tor/trunk/src/or/routerlist.c
Log:
 r12899@catbus:  nickm | 2007-05-22 13:51:26 -0400
 Extract common code from networkstatus_getinfo_helper_single() and generate_v2_networkstatus().



Property changes on: tor/trunk
___________________________________________________________________
 svk:merge ticket from /tor/trunk [r12899] on 8246c3cf-6607-4228-993b-4d95d33730f1

Modified: tor/trunk/doc/TODO
===================================================================
--- tor/trunk/doc/TODO	2007-05-22 17:58:25 UTC (rev 10293)
+++ tor/trunk/doc/TODO	2007-05-22 17:58:30 UTC (rev 10294)
@@ -188,7 +188,7 @@
       - Detect correct version of libraries.
       o Run autoupdate
     - Refactor networkstatus generation:
-      - Use networkstatus_getinfo_helper_single() as base of
+      o Use networkstatus_getinfo_helper_single() as base of
         networkstatus generation; eliminate duplicate code.
       - Include "v" line in getinfo values.
   - Features:

Modified: tor/trunk/src/or/dirserv.c
===================================================================
--- tor/trunk/src/or/dirserv.c	2007-05-22 17:58:25 UTC (rev 10293)
+++ tor/trunk/src/or/dirserv.c	2007-05-22 17:58:30 UTC (rev 10294)
@@ -1570,6 +1570,77 @@
   smartlist_free(bandwidths_excluding_exits);
 }
 
+/** DOCDOC */
+int
+routerstatus_format_entry(char *buf, size_t buf_len,
+                          routerstatus_t *rs, const char *platform)
+{
+  int r;
+  struct in_addr in;
+
+  int f_authority;
+  char published[ISO_TIME_LEN+1];
+  char ipaddr[INET_NTOA_BUF_LEN];
+  char identity64[BASE64_DIGEST_LEN+1];
+  char digest64[BASE64_DIGEST_LEN+1];
+
+  format_iso_time(published, rs->published_on);
+  digest_to_base64(identity64, rs->identity_digest);
+  digest_to_base64(digest64, rs->descriptor_digest);
+  in.s_addr = htonl(rs->addr);
+  tor_inet_ntoa(&in, ipaddr, sizeof(ipaddr));
+
+  f_authority = router_digest_is_trusted_dir(rs->identity_digest);
+
+  r = tor_snprintf(buf, buf_len,
+                   "r %s %s %s %s %s %d %d\n"
+                   "s%s%s%s%s%s%s%s%s%s%s\n",
+                   rs->nickname,
+                   identity64,
+                   digest64,
+                   published,
+                   ipaddr,
+                   (int)rs->or_port,
+                   (int)rs->dir_port,
+
+                   f_authority?" Authority":"",
+                   rs->is_bad_exit?" BadExit":"",
+                   rs->is_exit?" Exit":"",
+                   rs->is_fast?" Fast":"",
+                   rs->is_possible_guard?" Guard":"",
+                   rs->is_named?" Named":"",
+                   rs->is_stable?" Stable":"",
+                   rs->is_running?" Running":"",
+                   rs->is_valid?" Valid":"",
+                   rs->is_v2_dir?" V2Dir":"");
+  if (r<0) {
+    log_warn(LD_BUG, "Not enough space in buffer.");
+    return -1;
+  }
+  if (platform && !strcmpstart(platform, "Tor ")) {
+    const char *eos = find_whitespace(platform+4);
+    if (eos && !strcmpstart(eos, " (r")) {
+      /* XXXX020 Unify this logic with the other version extraction
+       * logic */
+      eos = find_whitespace(eos+1);
+    }
+    if (eos) {
+      char *p = tor_strndup(platform, eos-platform);
+      size_t ln = strlen(buf);
+      buf += ln;
+      buf_len -= ln;
+      if (tor_snprintf(buf, buf_len, "opt v %s\n", p)<0) {
+        tor_free(p);
+        log_warn(LD_BUG, "Unable to print router version.");
+        return -1;
+      }
+      tor_free(p);
+    }
+  }
+
+  return 0;
+}
+
 /** For v2 authoritative directories only: replace the contents of
  * <b>the_v2_networkstatus</b> with a newly generated network status
  * object. */
@@ -1684,91 +1755,56 @@
     if (ri->cache_info.published_on >= cutoff) {
       /* Already set by compute_performance_thresholds. */
       int f_exit = ri->is_exit;
+      routerstatus_t rs;
       /* These versions dump connections with idle live circuits
          sometimes. D'oh!*/
       int unstable_version =
         tor_version_as_new_as(ri->platform,"0.1.1.10-alpha") &&
         !tor_version_as_new_as(ri->platform,"0.1.1.16-rc-cvs");
-      int f_stable = ri->is_stable =
+      memset(&rs, 0, sizeof(rs));
+
+      rs.is_stable = ri->is_stable =
         router_is_active(ri, now) &&
         !dirserv_thinks_router_is_unreliable(now, ri, 1, 0) &&
         !unstable_version;
-      int f_fast = ri->is_fast =
+      rs.is_fast = ri->is_fast =
         router_is_active(ri, now) &&
         !dirserv_thinks_router_is_unreliable(now, ri, 0, 1);
-      int f_running = ri->is_running; /* computed above */
+      rs.is_running = ri->is_running; /* computed above */
+      /*
       int f_authority = router_digest_is_trusted_dir(
                                       ri->cache_info.identity_digest);
-      int f_named = naming && ri->is_named;
-      int f_valid = ri->is_valid;
-      int f_guard = f_fast && f_stable &&
+      */
+      rs.is_named = naming && ri->is_named;
+      rs.is_valid = ri->is_valid;
+      rs.is_possible_guard = rs.is_fast && rs.is_stable &&
         (!f_exit || exits_can_be_guards) &&
         router_get_advertised_bandwidth(ri) >=
           (exits_can_be_guards ? guard_bandwidth_including_exits :
                                  guard_bandwidth_excluding_exits);
-      int f_bad_exit = listbadexits && ri->is_bad_exit;
+      rs.is_bad_exit = listbadexits && ri->is_bad_exit;
       /* 0.1.1.9-alpha is the first version to support fetch by descriptor
        * hash. */
-      int f_v2_dir = ri->dir_port &&
+      rs.is_v2_dir = ri->dir_port &&
         tor_version_as_new_as(ri->platform,"0.1.1.9-alpha");
-      char identity64[BASE64_DIGEST_LEN+1];
-      char digest64[BASE64_DIGEST_LEN+1];
 
       if (!strcasecmp(ri->nickname, UNNAMED_ROUTER_NICKNAME))
-        f_named = 0;
+        rs.is_named = 0;
 
-      format_iso_time(published, ri->cache_info.published_on);
+      rs.published_on = ri->cache_info.published_on;
+      memcpy(rs.identity_digest, ri->cache_info.identity_digest, DIGEST_LEN);
+      memcpy(rs.descriptor_digest, ri->cache_info.signed_descriptor_digest,
+             DIGEST_LEN);
+      rs.addr = ri->addr;
+      strlcpy(rs.nickname, ri->nickname, sizeof(rs.nickname));
+      rs.or_port = ri->or_port;
+      rs.dir_port = ri->dir_port;
 
-      digest_to_base64(identity64, ri->cache_info.identity_digest);
-      digest_to_base64(digest64, ri->cache_info.signed_descriptor_digest);
-
-      in.s_addr = htonl(ri->addr);
-      tor_inet_ntoa(&in, ipaddr, sizeof(ipaddr));
-
-      if (tor_snprintf(outp, endp-outp,
-                       "r %s %s %s %s %s %d %d\n"
-                       "s%s%s%s%s%s%s%s%s%s%s\n",
-                       ri->nickname,
-                       identity64,
-                       digest64,
-                       published,
-                       ipaddr,
-                       ri->or_port,
-                       ri->dir_port,
-                       f_authority?" Authority":"",
-                       f_bad_exit?" BadExit":"",
-                       f_exit?" Exit":"",
-                       f_fast?" Fast":"",
-                       f_guard?" Guard":"",
-                       f_named?" Named":"",
-                       f_stable?" Stable":"",
-                       f_running?" Running":"",
-                       f_valid?" Valid":"",
-                       f_v2_dir?" V2Dir":"")<0) {
-                       /* when adding more flags, remember to change
-                        * the #defines at the top of this function. */
+      if (routerstatus_format_entry(outp, endp-outp, &rs, ri->platform) < 0) {
         log_warn(LD_BUG, "Unable to print router status.");
         goto done;
       }
       outp += strlen(outp);
-      if (ri->platform && !strcmpstart(ri->platform, "Tor ")) {
-        const char *eos = find_whitespace(ri->platform+4);
-        if (eos && !strcmpstart(eos, " (r")) {
-          /* XXXX020 Unify this logic with the other version extraction
-           * logic */
-          eos = find_whitespace(eos+1);
-        }
-        if (eos) {
-          char *platform = tor_strndup(ri->platform, eos-(ri->platform));
-          if (tor_snprintf(outp, endp-outp,
-                           "opt v %s\n", platform)<0) {
-            log_warn(LD_BUG, "Unable to print router version.");
-            goto done;
-          }
-          tor_free(platform);
-          outp += strlen(outp);
-        }
-      }
     }
   });
 

Modified: tor/trunk/src/or/or.h
===================================================================
--- tor/trunk/src/or/or.h	2007-05-22 17:58:25 UTC (rev 10293)
+++ tor/trunk/src/or/or.h	2007-05-22 17:58:30 UTC (rev 10294)
@@ -1204,7 +1204,6 @@
                                * an exit node. */
   unsigned int is_bad_directory:1; /**< Do we think this directory is junky,
                                     * underpowered, or otherwise useless? */
-
   /** True iff we know version info for this router. (i.e., a "v" entry was
    * included.)  We'll replace all these with a big tor_version_t or a char[]
    * if the number of traits we care about ever becomes incredibly big. */
@@ -2636,6 +2635,8 @@
 int dirserv_would_reject_router(routerstatus_t *rs);
 size_t dirserv_estimate_data_size(smartlist_t *fps, int is_serverdescs,
                                   int compressed);
+int routerstatus_format_entry(char *buf, size_t buf_len,
+                              routerstatus_t *rs, const char *platform);
 void dirserv_free_all(void);
 void cached_dir_decref(cached_dir_t *d);
 

Modified: tor/trunk/src/or/router.c
===================================================================
--- tor/trunk/src/or/router.c	2007-05-22 17:58:25 UTC (rev 10293)
+++ tor/trunk/src/or/router.c	2007-05-22 17:58:30 UTC (rev 10294)
@@ -296,7 +296,6 @@
     authority_cert_free(parsed);
 }
 
-
 /** Initialize all OR private keys, and the TLS context, as necessary.
  * On OPs, this only initializes the tls context. Return 0 on success,
  * or -1 if Tor should die.

Modified: tor/trunk/src/or/routerlist.c
===================================================================
--- tor/trunk/src/or/routerlist.c	2007-05-22 17:58:25 UTC (rev 10293)
+++ tor/trunk/src/or/routerlist.c	2007-05-22 17:58:30 UTC (rev 10294)
@@ -5019,52 +5019,11 @@
 /** Generate networkstatus lines for a single routerstatus_t object, and
  * return the result in a newly allocated string.  Used only by controller
  * interface (for now.) */
-/* XXXX This should eventually merge into generate_v2_networkstatus() */
 char *
 networkstatus_getinfo_helper_single(routerstatus_t *rs)
 {
-  char buf[192];
-  int r;
-  struct in_addr in;
-
-  int f_authority;
-  char published[ISO_TIME_LEN+1];
-  char ipaddr[INET_NTOA_BUF_LEN];
-  char identity64[BASE64_DIGEST_LEN+1];
-  char digest64[BASE64_DIGEST_LEN+1];
-
-  format_iso_time(published, rs->published_on);
-  digest_to_base64(identity64, rs->identity_digest);
-  digest_to_base64(digest64, rs->descriptor_digest);
-  in.s_addr = htonl(rs->addr);
-  tor_inet_ntoa(&in, ipaddr, sizeof(ipaddr));
-
-  f_authority = router_digest_is_trusted_dir(rs->identity_digest);
-
-  r = tor_snprintf(buf, sizeof(buf),
-                   "r %s %s %s %s %s %d %d\n"
-                   "s%s%s%s%s%s%s%s%s%s%s\n",
-                   rs->nickname,
-                   identity64,
-                   digest64,
-                   published,
-                   ipaddr,
-                   (int)rs->or_port,
-                   (int)rs->dir_port,
-
-                   f_authority?" Authority":"",
-                   rs->is_bad_exit?" BadExit":"",
-                   rs->is_exit?" Exit":"",
-                   rs->is_fast?" Fast":"",
-                   rs->is_possible_guard?" Guard":"",
-                   rs->is_named?" Named":"",
-                   rs->is_stable?" Stable":"",
-                   rs->is_running?" Running":"",
-                   rs->is_valid?" Valid":"",
-                   rs->is_v2_dir?" V2Dir":"");
-  if (r<0)
-    log_warn(LD_BUG, "Not enough space in buffer.");
-
+  char buf[256];
+  routerstatus_format_entry(buf, sizeof(buf), rs, NULL);
   return tor_strdup(buf);
 }