[tor-commits] [tor/master] Move dirserv_get_routerdescs() to control_getinfo.c

nickm at torproject.org nickm at torproject.org
Fri Jan 17 12:51:00 UTC 2020


commit fe8156dbc20870bb2e065aa10ca7f2e993958597
Author: Nick Mathewson <nickm at torproject.org>
Date:   Wed Jan 8 21:32:16 2020 -0500

    Move dirserv_get_routerdescs() to control_getinfo.c
    
    This function had some XXX comments indicating (correctly) that it
    was not actually used by the dirserver code, and that only the
    controller still used it.
---
 src/feature/control/control_getinfo.c | 83 ++++++++++++++++++++++++++++++++++-
 src/feature/dircache/dirserv.c        | 81 ----------------------------------
 src/feature/dircache/dirserv.h        |  2 -
 3 files changed, 82 insertions(+), 84 deletions(-)

diff --git a/src/feature/control/control_getinfo.c b/src/feature/control/control_getinfo.c
index 48c185494..6f30878d2 100644
--- a/src/feature/control/control_getinfo.c
+++ b/src/feature/control/control_getinfo.c
@@ -34,6 +34,7 @@
 #include "feature/dircache/dirserv.h"
 #include "feature/dirclient/dirclient.h"
 #include "feature/dirclient/dlstatus.h"
+#include "feature/dircommon/directory.h"
 #include "feature/hibernate/hibernate.h"
 #include "feature/hs/hs_cache.h"
 #include "feature/hs_common/shared_random_client.h"
@@ -361,6 +362,86 @@ getinfo_helper_current_consensus(consensus_flavor_t flavor,
   return 0;
 }
 
+/** Helper for getinfo_helper_dir.
+ *
+ * Add a signed_descriptor_t to <b>descs_out</b> for each router matching
+ * <b>key</b>.  The key should be either
+ *   - "/tor/server/authority" for our own routerinfo;
+ *   - "/tor/server/all" for all the routerinfos we have, concatenated;
+ *   - "/tor/server/fp/FP" where FP is a plus-separated sequence of
+ *     hex identity digests; or
+ *   - "/tor/server/d/D" where D is a plus-separated sequence
+ *     of server descriptor digests, in hex.
+ *
+ * Return 0 if we found some matching descriptors, or -1 if we do not
+ * have any descriptors, no matching descriptors, or if we did not
+ * recognize the key (URL).
+ * If -1 is returned *<b>msg</b> will be set to an appropriate error
+ * message.
+ */
+static int
+controller_get_routerdescs(smartlist_t *descs_out, const char *key,
+                        const char **msg)
+{
+  *msg = NULL;
+
+  if (!strcmp(key, "/tor/server/all")) {
+    routerlist_t *rl = router_get_routerlist();
+    SMARTLIST_FOREACH(rl->routers, routerinfo_t *, r,
+                      smartlist_add(descs_out, &(r->cache_info)));
+  } else if (!strcmp(key, "/tor/server/authority")) {
+    const routerinfo_t *ri = router_get_my_routerinfo();
+    if (ri)
+      smartlist_add(descs_out, (void*) &(ri->cache_info));
+  } else if (!strcmpstart(key, "/tor/server/d/")) {
+    smartlist_t *digests = smartlist_new();
+    key += strlen("/tor/server/d/");
+    dir_split_resource_into_fingerprints(key, digests, NULL,
+                                         DSR_HEX|DSR_SORT_UNIQ);
+    SMARTLIST_FOREACH(digests, const char *, d,
+       {
+         signed_descriptor_t *sd = router_get_by_descriptor_digest(d);
+         if (sd)
+           smartlist_add(descs_out,sd);
+       });
+    SMARTLIST_FOREACH(digests, char *, d, tor_free(d));
+    smartlist_free(digests);
+  } else if (!strcmpstart(key, "/tor/server/fp/")) {
+    smartlist_t *digests = smartlist_new();
+    time_t cutoff = time(NULL) - ROUTER_MAX_AGE_TO_PUBLISH;
+    key += strlen("/tor/server/fp/");
+    dir_split_resource_into_fingerprints(key, digests, NULL,
+                                         DSR_HEX|DSR_SORT_UNIQ);
+    SMARTLIST_FOREACH_BEGIN(digests, const char *, d) {
+         if (router_digest_is_me(d)) {
+           /* calling router_get_my_routerinfo() to make sure it exists */
+           const routerinfo_t *ri = router_get_my_routerinfo();
+           if (ri)
+             smartlist_add(descs_out, (void*) &(ri->cache_info));
+         } else {
+           const routerinfo_t *ri = router_get_by_id_digest(d);
+           /* Don't actually serve a descriptor that everyone will think is
+            * expired.  This is an (ugly) workaround to keep buggy 0.1.1.10
+            * Tors from downloading descriptors that they will throw away.
+            */
+           if (ri && ri->cache_info.published_on > cutoff)
+             smartlist_add(descs_out, (void*) &(ri->cache_info));
+         }
+    } SMARTLIST_FOREACH_END(d);
+    SMARTLIST_FOREACH(digests, char *, d, tor_free(d));
+    smartlist_free(digests);
+  } else {
+    *msg = "Key not recognized";
+    return -1;
+  }
+
+  if (!smartlist_len(descs_out)) {
+    *msg = "Servers unavailable";
+    return -1;
+  }
+  return 0;
+}
+
 /** Implementation helper for GETINFO: knows the answers for questions about
  * directory information. */
 STATIC int
@@ -590,7 +671,7 @@ getinfo_helper_dir(control_connection_t *control_conn,
     int res;
     char *cp;
     tor_asprintf(&url, "/tor/%s", question+4);
-    res = dirserv_get_routerdescs(descs, url, &msg);
+    res = controller_get_routerdescs(descs, url, &msg);
     if (res) {
       log_warn(LD_CONTROL, "getinfo '%s': %s", question, msg);
       smartlist_free(descs);
diff --git a/src/feature/dircache/dirserv.c b/src/feature/dircache/dirserv.c
index 738b1928a..721261479 100644
--- a/src/feature/dircache/dirserv.c
+++ b/src/feature/dircache/dirserv.c
@@ -363,87 +363,6 @@ dirserv_get_routerdesc_spool(smartlist_t *spool_out,
   return 0;
 }
 
-/** Add a signed_descriptor_t to <b>descs_out</b> for each router matching
- * <b>key</b>.  The key should be either
- *   - "/tor/server/authority" for our own routerinfo;
- *   - "/tor/server/all" for all the routerinfos we have, concatenated;
- *   - "/tor/server/fp/FP" where FP is a plus-separated sequence of
- *     hex identity digests; or
- *   - "/tor/server/d/D" where D is a plus-separated sequence
- *     of server descriptor digests, in hex.
- *
- * Return 0 if we found some matching descriptors, or -1 if we do not
- * have any descriptors, no matching descriptors, or if we did not
- * recognize the key (URL).
- * If -1 is returned *<b>msg</b> will be set to an appropriate error
- * message.
- *
- * XXXX rename this function.  It's only called from the controller.
- * XXXX in fact, refactor this function, merging as much as possible.
- */
-int
-dirserv_get_routerdescs(smartlist_t *descs_out, const char *key,
-                        const char **msg)
-{
-  *msg = NULL;
-
-  if (!strcmp(key, "/tor/server/all")) {
-    routerlist_t *rl = router_get_routerlist();
-    SMARTLIST_FOREACH(rl->routers, routerinfo_t *, r,
-                      smartlist_add(descs_out, &(r->cache_info)));
-  } else if (!strcmp(key, "/tor/server/authority")) {
-    const routerinfo_t *ri = router_get_my_routerinfo();
-    if (ri)
-      smartlist_add(descs_out, (void*) &(ri->cache_info));
-  } else if (!strcmpstart(key, "/tor/server/d/")) {
-    smartlist_t *digests = smartlist_new();
-    key += strlen("/tor/server/d/");
-    dir_split_resource_into_fingerprints(key, digests, NULL,
-                                         DSR_HEX|DSR_SORT_UNIQ);
-    SMARTLIST_FOREACH(digests, const char *, d,
-       {
-         signed_descriptor_t *sd = router_get_by_descriptor_digest(d);
-         if (sd)
-           smartlist_add(descs_out,sd);
-       });
-    SMARTLIST_FOREACH(digests, char *, d, tor_free(d));
-    smartlist_free(digests);
-  } else if (!strcmpstart(key, "/tor/server/fp/")) {
-    smartlist_t *digests = smartlist_new();
-    time_t cutoff = time(NULL) - ROUTER_MAX_AGE_TO_PUBLISH;
-    key += strlen("/tor/server/fp/");
-    dir_split_resource_into_fingerprints(key, digests, NULL,
-                                         DSR_HEX|DSR_SORT_UNIQ);
-    SMARTLIST_FOREACH_BEGIN(digests, const char *, d) {
-         if (router_digest_is_me(d)) {
-           /* calling router_get_my_routerinfo() to make sure it exists */
-           const routerinfo_t *ri = router_get_my_routerinfo();
-           if (ri)
-             smartlist_add(descs_out, (void*) &(ri->cache_info));
-         } else {
-           const routerinfo_t *ri = router_get_by_id_digest(d);
-           /* Don't actually serve a descriptor that everyone will think is
-            * expired.  This is an (ugly) workaround to keep buggy 0.1.1.10
-            * Tors from downloading descriptors that they will throw away.
-            */
-           if (ri && ri->cache_info.published_on > cutoff)
-             smartlist_add(descs_out, (void*) &(ri->cache_info));
-         }
-    } SMARTLIST_FOREACH_END(d);
-    SMARTLIST_FOREACH(digests, char *, d, tor_free(d));
-    smartlist_free(digests);
-  } else {
-    *msg = "Key not recognized";
-    return -1;
-  }
-
-  if (!smartlist_len(descs_out)) {
-    *msg = "Servers unavailable";
-    return -1;
-  }
-  return 0;
-}
-
 /* ==========
  * Spooling code.
  * ========== */
diff --git a/src/feature/dircache/dirserv.h b/src/feature/dircache/dirserv.h
index a4e10dd16..895cef948 100644
--- a/src/feature/dircache/dirserv.h
+++ b/src/feature/dircache/dirserv.h
@@ -101,8 +101,6 @@ int dirserv_get_routerdesc_spool(smartlist_t *spools_out, const char *key,
                                  dir_spool_source_t source,
                                  int conn_is_encrypted,
                                  const char **msg_out);
-int dirserv_get_routerdescs(smartlist_t *descs_out, const char *key,
-                            const char **msg);
 
 void dirserv_free_all(void);
 void cached_dir_decref(cached_dir_t *d);





More information about the tor-commits mailing list