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

[tor-commits] [tor/master] refactor router_pick_published_address to have another arg



commit 3bb40b213b26467df2604023ae29d915dc740128
Author: Roger Dingledine <arma@xxxxxxxxxxxxxx>
Date:   Wed Nov 16 16:13:03 2016 -0500

    refactor router_pick_published_address to have another arg
    
    no change in behavior except fewer log entries in the case where we use
    a cached result.
---
 src/or/control.c       |  2 +-
 src/or/dirserv.c       |  3 ++-
 src/or/router.c        | 39 +++++++++++++++++++++++++--------------
 src/or/router.h        |  3 ++-
 src/or/transports.c    |  2 +-
 src/test/test_config.c |  4 +++-
 6 files changed, 34 insertions(+), 19 deletions(-)

diff --git a/src/or/control.c b/src/or/control.c
index a4f8e37..c8c5062 100644
--- a/src/or/control.c
+++ b/src/or/control.c
@@ -1704,7 +1704,7 @@ getinfo_helper_misc(control_connection_t *conn, const char *question,
     *answer = tor_strdup("VERBOSE_NAMES EXTENDED_EVENTS");
   } else if (!strcmp(question, "address")) {
     uint32_t addr;
-    if (router_pick_published_address(get_options(), &addr) < 0) {
+    if (router_pick_published_address(get_options(), &addr, 0) < 0) {
       *errmsg = "Address unknown";
       return -1;
     }
diff --git a/src/or/dirserv.c b/src/or/dirserv.c
index 41945fe..fd92522 100644
--- a/src/or/dirserv.c
+++ b/src/or/dirserv.c
@@ -1033,7 +1033,8 @@ directory_fetches_from_authorities(const or_options_t *options)
     return 1;
   if (options->BridgeRelay == 1)
     return 0;
-  if (server_mode(options) && router_pick_published_address(options, &addr)<0)
+  if (server_mode(options) &&
+      router_pick_published_address(options, &addr, 0) < 0)
     return 1; /* we don't know our IP address; ask an authority. */
   refuseunknown = ! router_my_exit_policy_is_reject_star() &&
     should_refuse_unknown_exits(options);
diff --git a/src/or/router.c b/src/or/router.c
index b968072..6d3a32a 100644
--- a/src/or/router.c
+++ b/src/or/router.c
@@ -1961,23 +1961,34 @@ static int router_guess_address_from_dir_headers(uint32_t *guess);
 /** Make a current best guess at our address, either because
  * it's configured in torrc, or because we've learned it from
  * dirserver headers. Place the answer in *<b>addr</b> and return
- * 0 on success, else return -1 if we have no guess. */
+ * 0 on success, else return -1 if we have no guess.
+ *
+ * If <b>cache_only</b> is true, just return any cached answers, and
+ * don't try to get any new answers.
+ */
 MOCK_IMPL(int,
-router_pick_published_address,(const or_options_t *options, uint32_t *addr))
+router_pick_published_address,(const or_options_t *options, uint32_t *addr,
+                               int cache_only))
 {
+  /* First, check the cached output from resolve_my_address(). */
   *addr = get_last_resolved_addr();
-  if (!*addr &&
-      resolve_my_address(LOG_INFO, options, addr, NULL, NULL) < 0) {
-    log_info(LD_CONFIG, "Could not determine our address locally. "
-             "Checking if directory headers provide any hints.");
-    if (router_guess_address_from_dir_headers(addr) < 0) {
-      log_info(LD_CONFIG, "No hints from directory headers either. "
-               "Will try again later.");
-      return -1;
+  if (*addr)
+    return 0;
+
+  /* Second, consider doing a resolve attempt right here. */
+  if (!cache_only) {
+    if (resolve_my_address(LOG_INFO, options, addr, NULL, NULL) >= 0) {
+      log_info(LD_CONFIG,"Success: chose address '%s'.", fmt_addr32(*addr));
+      return 0;
     }
   }
-  log_info(LD_CONFIG,"Success: chose address '%s'.", fmt_addr32(*addr));
-  return 0;
+
+  /* Third, check the cached output from router_new_address_suggestion(). */
+  if (router_guess_address_from_dir_headers(addr) >= 0)
+    return 0;
+
+  /* We have no useful cached answers. Return failure. */
+  return -1;
 }
 
 /* Like router_check_descriptor_address_consistency, but specifically for the
@@ -2074,7 +2085,7 @@ router_build_fresh_descriptor(routerinfo_t **r, extrainfo_t **e)
   int hibernating = we_are_hibernating();
   const or_options_t *options = get_options();
 
-  if (router_pick_published_address(options, &addr) < 0) {
+  if (router_pick_published_address(options, &addr, 0) < 0) {
     log_warn(LD_CONFIG, "Don't know my address while generating descriptor");
     return -1;
   }
@@ -2323,7 +2334,7 @@ router_rebuild_descriptor(int force)
   if (desc_clean_since && !force)
     return 0;
 
-  if (router_pick_published_address(options, &addr) < 0 ||
+  if (router_pick_published_address(options, &addr, 0) < 0 ||
       router_get_advertised_or_port(options) == 0) {
     /* Stop trying to rebuild our descriptor every second. We'll
      * learn that it's time to try again when ip_address_changed()
diff --git a/src/or/router.h b/src/or/router.h
index 73bfea1..c30a030 100644
--- a/src/or/router.h
+++ b/src/or/router.h
@@ -91,7 +91,8 @@ const uint8_t *router_get_my_id_digest(void);
 int router_extrainfo_digest_is_me(const char *digest);
 int router_is_me(const routerinfo_t *router);
 MOCK_DECL(int,router_pick_published_address,(const or_options_t *options,
-                                             uint32_t *addr));
+                                             uint32_t *addr,
+                                             int cache_only));
 int router_build_fresh_descriptor(routerinfo_t **r, extrainfo_t **e);
 int router_rebuild_descriptor(int force);
 char *router_dump_router_to_string(routerinfo_t *router,
diff --git a/src/or/transports.c b/src/or/transports.c
index be77c87..7a52b73 100644
--- a/src/or/transports.c
+++ b/src/or/transports.c
@@ -1611,7 +1611,7 @@ pt_get_extra_info_descriptor_string(void)
       uint32_t external_ip_address = 0;
       if (tor_addr_is_null(&t->addr) &&
           router_pick_published_address(get_options(),
-                                        &external_ip_address) >= 0) {
+                                        &external_ip_address, 0) >= 0) {
         tor_addr_t addr;
         tor_addr_from_ipv4h(&addr, external_ip_address);
         addrport = fmt_addrport(&addr, t->port);
diff --git a/src/test/test_config.c b/src/test/test_config.c
index 384bff4..2fc37b0 100644
--- a/src/test/test_config.c
+++ b/src/test/test_config.c
@@ -3480,10 +3480,12 @@ test_config_default_dir_servers(void *arg)
 static int mock_router_pick_published_address_result = 0;
 
 static int
-mock_router_pick_published_address(const or_options_t *options, uint32_t *addr)
+mock_router_pick_published_address(const or_options_t *options,
+                                   uint32_t *addr, int cache_only)
 {
   (void)options;
   (void)addr;
+  (void)cache_only;
   return mock_router_pick_published_address_result;
 }
 



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits