[or-cvs] r12621: start to refactor dirserver_mode() (tor/trunk/src/or)

arma at seul.org arma at seul.org
Sat Dec 1 04:58:54 UTC 2007


Author: arma
Date: 2007-11-30 23:58:53 -0500 (Fri, 30 Nov 2007)
New Revision: 12621

Modified:
   tor/trunk/src/or/config.c
   tor/trunk/src/or/connection_edge.c
   tor/trunk/src/or/control.c
   tor/trunk/src/or/directory.c
   tor/trunk/src/or/dirserv.c
   tor/trunk/src/or/main.c
   tor/trunk/src/or/networkstatus.c
   tor/trunk/src/or/or.h
   tor/trunk/src/or/router.c
   tor/trunk/src/or/routerlist.c
   tor/trunk/src/or/routerparse.c
Log:
start to refactor dirserver_mode()


Modified: tor/trunk/src/or/config.c
===================================================================
--- tor/trunk/src/or/config.c	2007-12-01 04:40:12 UTC (rev 12620)
+++ tor/trunk/src/or/config.c	2007-12-01 04:58:53 UTC (rev 12621)
@@ -1132,7 +1132,8 @@
   if (old_options) {
     if (authdir_mode_v3(options) && !authdir_mode_v3(old_options))
       dirvote_recalculate_timing(options, time(NULL));
-    if (!bool_eq(dirserver_mode(options), dirserver_mode(old_options))) {
+    if (!bool_eq(directory_caches_dir_info(options),
+                 directory_caches_dir_info(old_options))) {
       /* Make sure update_router_have_min_dir_info gets called. */
       router_dir_info_changed();
       /* We might need to download a new consensus status later or sooner than

Modified: tor/trunk/src/or/connection_edge.c
===================================================================
--- tor/trunk/src/or/connection_edge.c	2007-12-01 04:40:12 UTC (rev 12620)
+++ tor/trunk/src/or/connection_edge.c	2007-12-01 04:58:53 UTC (rev 12621)
@@ -2292,7 +2292,8 @@
     }
   } else if (rh.command == RELAY_COMMAND_BEGIN_DIR) {
     or_options_t *options = get_options();
-    if (!dirserver_mode(options) || circ->purpose != CIRCUIT_PURPOSE_OR) {
+    if (!directory_permits_begindir_requests(options) ||
+        circ->purpose != CIRCUIT_PURPOSE_OR) {
       end_payload[0] = END_STREAM_REASON_NOTDIRECTORY;
       relay_send_command_from_edge(rh.stream_id, circ, RELAY_COMMAND_END,
                                    end_payload, 1, NULL);

Modified: tor/trunk/src/or/control.c
===================================================================
--- tor/trunk/src/or/control.c	2007-12-01 04:40:12 UTC (rev 12620)
+++ tor/trunk/src/or/control.c	2007-12-01 04:58:53 UTC (rev 12621)
@@ -1382,7 +1382,7 @@
     tor_free(url);
     smartlist_free(descs);
   } else if (!strcmpstart(question, "dir/status/")) {
-    if (dirserver_mode(get_options())) {
+    if (directory_permits_controller_requests(get_options())) {
       size_t len=0;
       char *cp;
       smartlist_t *status_list = smartlist_create();

Modified: tor/trunk/src/or/directory.c
===================================================================
--- tor/trunk/src/or/directory.c	2007-12-01 04:40:12 UTC (rev 12620)
+++ tor/trunk/src/or/directory.c	2007-12-01 04:58:53 UTC (rev 12621)
@@ -282,7 +282,7 @@
 {
   routerstatus_t *rs = NULL;
   or_options_t *options = get_options();
-  int prefer_authority = server_mode(options) && dirserver_mode(options);
+  int prefer_authority = directory_fetches_from_authorities(options);
   int get_via_tor = purpose_needs_anonymity(dir_purpose, router_purpose);
   authority_type_t type;
   int flags = retry_if_no_servers ? PDS_RETRY_IF_NO_SERVERS : 0;
@@ -2982,8 +2982,7 @@
 {
   char digest[DIGEST_LEN];
   time_t now = time(NULL);
-  or_options_t *options = get_options();
-  int server = server_mode(options) && dirserver_mode(options);
+  int server = directory_fetches_from_authorities(get_options());
   if (!was_descriptor_digests) {
     if (router_purpose == ROUTER_PURPOSE_BRIDGE) {
       tor_assert(!was_extrainfo); /* not supported yet */

Modified: tor/trunk/src/or/dirserv.c
===================================================================
--- tor/trunk/src/or/dirserv.c	2007-12-01 04:40:12 UTC (rev 12620)
+++ tor/trunk/src/or/dirserv.c	2007-12-01 04:58:53 UTC (rev 12621)
@@ -1056,6 +1056,50 @@
   return -1;
 }
 
+/********************************************************************/
+
+/* A set of functions to answer questions about how we'd like to behave
+ * as a directory cache/client. */
+
+/** Return 1 if we want to keep descriptors, networkstatuses, etc around
+ * and serve them to others, or 0 otherwise.
+ * Also causes us to fetch new networkstatuses, descriptors, etc on the
+ * "mirror" schedule rather than the "client" schedule.
+ */
+int
+directory_caches_dir_info(or_options_t *options)
+{
+  return options->DirPort != 0;
+}
+
+/** Return 1 if we fetch our directory material directly from the
+ * authorities, rather than some other cache. */
+int
+directory_fetches_from_authorities(or_options_t *options)
+{
+  return server_mode(options) && options->DirPort != 0;
+}
+
+/** Return 1 if we want to allow remote people to ask us directory
+ * requests via the "begin_dir" interface, which doesn't require
+ * having any separate port open. */
+int
+directory_permits_begindir_requests(or_options_t *options)
+{
+  return options->DirPort != 0;
+}
+
+/** Return 1 if we want to allow controllers to ask us directory
+ * requests via the controller interface, which doesn't require
+ * having any separate port open. */
+int
+directory_permits_controller_requests(or_options_t *options)
+{
+  return options->DirPort != 0;
+}
+
+/********************************************************************/
+
 /* Used only by non-v1-auth dirservers: The v1 directory and
  * runningrouters we'll serve when requested. */
 static cached_dir_t *cached_directory = NULL;

Modified: tor/trunk/src/or/main.c
===================================================================
--- tor/trunk/src/or/main.c	2007-12-01 04:40:12 UTC (rev 12620)
+++ tor/trunk/src/or/main.c	2007-12-01 04:58:53 UTC (rev 12621)
@@ -950,7 +950,7 @@
    * (if we've passed our internal checks). */
   if (time_to_fetch_directory < now) {
     /* Only caches actually need to fetch directories now. */
-    if (dirserver_mode(options) && !authdir_mode_v1(options)) {
+    if (directory_caches_dir_info(options) && !authdir_mode_v1(options)) {
       /* XXX020 actually, we should only do this if we want to advertise
        * our dirport. not simply if we configured one. -RD */
       if (any_trusted_dir_is_v1_authority() &&
@@ -964,7 +964,8 @@
   }
 
   /* Caches need to fetch running_routers; directory clients don't. */
-  if (dirserver_mode(options) && time_to_fetch_running_routers < now) {
+  if (directory_caches_dir_info(options) &&
+      time_to_fetch_running_routers < now) {
     if (!authdir_mode_v1(options) && !should_delay_dir_fetches(options)) {
       directory_get_from_dirserver(DIR_PURPOSE_FETCH_RUNNING_LIST,
                                    ROUTER_PURPOSE_GENERAL, NULL, 1);

Modified: tor/trunk/src/or/networkstatus.c
===================================================================
--- tor/trunk/src/or/networkstatus.c	2007-12-01 04:40:12 UTC (rev 12620)
+++ tor/trunk/src/or/networkstatus.c	2007-12-01 04:58:53 UTC (rev 12621)
@@ -483,7 +483,7 @@
     tor_free(fn);
   }
 
-  if (dirserver_mode(get_options()))
+  if (directory_caches_dir_info(get_options()))
     dirserv_set_cached_networkstatus_v2(s,
                                         ns->identity_digest,
                                         ns->published_on);
@@ -528,7 +528,7 @@
   char fp[HEX_DIGEST_LEN+1];
   char published[ISO_TIME_LEN+1];
 
-  if (!dirserver_mode(get_options()))
+  if (!directory_caches_dir_info(get_options()))
     return 0; /* Don't bother storing it. */
 
   ns = networkstatus_v2_parse_from_string(s);
@@ -542,10 +542,6 @@
       !(trusted_dir->type & V2_AUTHORITY)) {
     log_info(LD_DIR, "Network status was signed, but not by an authoritative "
              "directory we recognize.");
-    if (!dirserver_mode(get_options())) {
-      networkstatus_v2_free(ns);
-      return 0;
-    }
     source_desc = fp;
   } else {
     source_desc = trusted_dir->description;
@@ -600,7 +596,7 @@
   }
 
   if (!trusted_dir) {
-    if (!skewed && dirserver_mode(get_options())) {
+    if (!skewed) {
       /* We got a non-trusted networkstatus, and we're a directory cache.
        * This means that we asked an authority, and it told us about another
        * authority we didn't recognize. */
@@ -713,7 +709,7 @@
       unlink(fname);
     }
     tor_free(fname);
-    if (dirserver_mode(get_options())) {
+    if (directory_caches_dir_info(get_options())) {
       dirserv_set_cached_networkstatus_v2(NULL, ns->identity_digest, 0);
     }
     networkstatus_v2_free(ns);
@@ -1055,7 +1051,7 @@
     long dl_interval;
     long interval = c->fresh_until - c->valid_after;
     time_t start;
-    if (dirserver_mode(options)) {
+    if (directory_caches_dir_info(options)) {
       /* We want to cache the next one at some point after this one
        * is no longer fresh... */
       start = c->fresh_until + CONSENSUS_MIN_SECONDS_BEFORE_CACHING;
@@ -1114,7 +1110,7 @@
   or_options_t *options = get_options();
   if (should_delay_dir_fetches(options))
     return;
-  if (dirserver_mode(options))
+  if (directory_caches_dir_info(options))
     update_v2_networkstatus_cache_downloads(now);
   update_consensus_networkstatus_downloads(now);
   update_certificate_downloads(now);
@@ -1351,7 +1347,7 @@
     write_str_to_file(consensus_fname, consensus, 0);
   }
 
-  if (dirserver_mode(get_options()))
+  if (directory_caches_dir_info(get_options()))
     dirserv_set_cached_networkstatus_v3(consensus,
                                         current_consensus->valid_after);
 

Modified: tor/trunk/src/or/or.h
===================================================================
--- tor/trunk/src/or/or.h	2007-12-01 04:40:12 UTC (rev 12620)
+++ tor/trunk/src/or/or.h	2007-12-01 04:58:53 UTC (rev 12621)
@@ -3001,6 +3001,12 @@
                        int for_controller);
 int dirserv_dump_directory_to_string(char **dir_out,
                                      crypto_pk_env_t *private_key);
+
+int directory_caches_dir_info(or_options_t *options);
+int directory_fetches_from_authorities(or_options_t *options);
+int directory_permits_begindir_requests(or_options_t *options);
+int directory_permits_controller_requests(or_options_t *options);
+
 void directory_set_dirty(void);
 cached_dir_t *dirserv_get_directory(void);
 cached_dir_t *dirserv_get_runningrouters(void);
@@ -3628,7 +3634,6 @@
 int authdir_mode_bridge(or_options_t *options);
 int authdir_mode_any_nonbridge(or_options_t *options);
 int clique_mode(or_options_t *options);
-int dirserver_mode(or_options_t *options);
 int server_mode(or_options_t *options);
 int advertised_server_mode(void);
 int proxy_mode(or_options_t *options);

Modified: tor/trunk/src/or/router.c
===================================================================
--- tor/trunk/src/or/router.c	2007-12-01 04:40:12 UTC (rev 12620)
+++ tor/trunk/src/or/router.c	2007-12-01 04:58:53 UTC (rev 12621)
@@ -884,14 +884,6 @@
   return (options->ORPort != 0 || options->ORListenAddress);
 }
 
-/** Return true iff we are trying to be a directory server */
-int
-dirserver_mode(or_options_t *options)
-{
-//  if (options->ClientOnly) return 0;
-  return options->DirPort != 0;
-}
-
 /** Remember if we've advertised ourselves to the dirservers. */
 static int server_is_advertised=0;
 

Modified: tor/trunk/src/or/routerlist.c
===================================================================
--- tor/trunk/src/or/routerlist.c	2007-12-01 04:40:12 UTC (rev 12620)
+++ tor/trunk/src/or/routerlist.c	2007-12-01 04:58:53 UTC (rev 12621)
@@ -2262,7 +2262,8 @@
   return r;
 }
 
-#define should_cache_old_descriptors() dirserver_mode(get_options())
+#define should_cache_old_descriptors() \
+  directory_caches_dir_info(get_options())
 
 /** If we're a directory cache and routerlist <b>rl</b> doesn't have
  * a copy of router <b>ri</b> yet, add it to the list of old (not
@@ -2835,7 +2836,7 @@
 
   /* Check whether we need to do anything at all. */
   {
-    int mdpr = dirserver_mode(get_options()) ? 5 : 2;
+    int mdpr = directory_caches_dir_info(get_options()) ? 5 : 2;
     if (n <= mdpr)
       return;
     n_extra = n - mdpr;
@@ -2908,7 +2909,7 @@
   routerinfo_t *router;
   signed_descriptor_t *sd;
   digestmap_t *retain;
-  int dirserv = dirserver_mode(get_options());
+  int dirserv = directory_caches_dir_info(get_options());
   const networkstatus_vote_t *consensus = networkstatus_get_latest_consensus();
   const smartlist_t *networkstatus_v2_list = networkstatus_get_v2_list();
 
@@ -2999,7 +3000,7 @@
    * total number doesn't approach max_descriptors_per_router()*len(router).
    */
   if (smartlist_len(routerlist->old_routers) <
-      smartlist_len(routerlist->routers) * (dirserver_mode(get_options())?4:2))
+      smartlist_len(routerlist->routers) * (dirserv?4:2))
     goto done;
 
   smartlist_sort(routerlist->old_routers, _compare_old_routers_by_identity);
@@ -3223,7 +3224,7 @@
 {
   routerstatus_t *rs;
   networkstatus_vote_t *consensus = networkstatus_get_latest_consensus();
-  int dirserv = dirserver_mode(get_options());
+  int dirserv = directory_caches_dir_info(get_options());
   const smartlist_t *networkstatus_v2_list = networkstatus_get_v2_list();
 
   if (consensus) {
@@ -3559,7 +3560,7 @@
   or_options_t *options = get_options();
 
   n_downloadable = smartlist_len(downloadable);
-  if (!dirserver_mode(options)) {
+  if (!directory_caches_dir_info(options)) {
     if (n_downloadable >= MAX_DL_TO_DELAY) {
       log_debug(LD_DIR,
              "There are enough downloadable routerdescs to launch requests.");
@@ -3623,7 +3624,7 @@
   or_options_t *options = get_options();
   const smartlist_t *networkstatus_v2_list = networkstatus_get_v2_list();
 
-  if (! dirserver_mode(options)) {
+  if (! directory_caches_dir_info(options)) {
     log_warn(LD_BUG, "Called update_router_descriptor_cache_downloads() "
              "on a non-dir-mirror?");
   }
@@ -3762,7 +3763,7 @@
   smartlist_t *no_longer_old = smartlist_create();
   smartlist_t *downloadable = smartlist_create();
   int authdir = authdir_mode(options);
-  int dirserver = dirserver_mode(options);
+  int dirserver = directory_caches_dir_info(options);
   networkstatus_vote_t *consensus =
     networkstatus_get_reasonably_live_consensus(now);
   int n_delayed=0, n_have=0, n_would_reject=0, n_wouldnt_use=0,
@@ -3858,7 +3859,7 @@
   or_options_t *options = get_options();
   if (should_delay_dir_fetches(options))
     return;
-  if (dirserver_mode(options)) {
+  if (directory_caches_dir_info(options)) {
     update_router_descriptor_cache_downloads(now);
   }
   update_consensus_router_descriptor_downloads(now);

Modified: tor/trunk/src/or/routerparse.c
===================================================================
--- tor/trunk/src/or/routerparse.c	2007-12-01 04:40:12 UTC (rev 12620)
+++ tor/trunk/src/or/routerparse.c	2007-12-01 04:58:53 UTC (rev 12621)
@@ -686,7 +686,8 @@
 
   /* Now that we know the signature is okay, and we have a
    * publication time, cache the directory. */
-  if (dirserver_mode(get_options()) && !authdir_mode_v1(get_options()))
+  if (directory_caches_dir_info(get_options()) &&
+      !authdir_mode_v1(get_options()))
     dirserv_set_cached_directory(str, published_on, 0);
 
   r = 0;



More information about the tor-commits mailing list