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

[tor-commits] [tor/master] addr: Refactor last resolved address cache accessors



commit 7795dd7ef6fa01202b91d20a0ac82eda1456cef8
Author: David Goulet <dgoulet@xxxxxxxxxxxxxx>
Date:   Tue Jun 23 10:06:19 2020 -0400

    addr: Refactor last resolved address cache accessors
    
    Series of things done in this commit:
    
      1. Rename the functions to better reflect the namespace of the file.
    
      2. Make both reset and get function to operate on the last_resolved_addrs
         cache that is per family.
    
      3. Make the get function to take a tor_addr_t.
    
      4. Change all callsite to use the new convention.
    
    Part of #33233
    
    Signed-off-by: David Goulet <dgoulet@xxxxxxxxxxxxxx>
---
 src/app/config/resolve_addr.c       | 19 ++++++++++++-------
 src/app/config/resolve_addr.h       |  4 ++--
 src/core/mainloop/connection.c      |  2 +-
 src/feature/relay/relay_find_addr.c | 26 +++++++++++++++++++-------
 4 files changed, 34 insertions(+), 17 deletions(-)

diff --git a/src/app/config/resolve_addr.c b/src/app/config/resolve_addr.c
index 9a7fbde16..1e861217d 100644
--- a/src/app/config/resolve_addr.c
+++ b/src/app/config/resolve_addr.c
@@ -47,18 +47,23 @@ static tor_addr_t last_resolved_addrs[IDX_SIZE];
 /** Last value actually set by resolve_my_address. */
 static uint32_t last_resolved_addr_v4 = 0;
 
-/** Accessor for last_resolved_addr_v4 from outside this file. */
-uint32_t
-get_last_resolved_addr_v4(void)
+/** Copy the last resolved address of family into addr_out.
+ *
+ * If not last resolved address existed, the addr_out is a null address (use
+ * tor_addr_is_null()). */
+void
+resolved_addr_get_last(int family, tor_addr_t *addr_out)
 {
-  return last_resolved_addr_v4;
+  tor_addr_copy(addr_out, &last_resolved_addrs[family]);
 }
 
-/** Reset last_resolved_addr_v4 from outside this file. */
+/** Reset the last resolved address of family.
+ *
+ * This makes it null address. */
 void
-reset_last_resolved_addr_v4(void)
+resolved_addr_reset_last(int family)
 {
-  last_resolved_addr_v4 = 0;
+  tor_addr_make_null(&last_resolved_addrs[family], family);
 }
 
 /** @brief Return true iff the given IP address can be used as a valid
diff --git a/src/app/config/resolve_addr.h b/src/app/config/resolve_addr.h
index 2cd27d170..17e940203 100644
--- a/src/app/config/resolve_addr.h
+++ b/src/app/config/resolve_addr.h
@@ -19,8 +19,8 @@ bool find_my_address(const or_options_t *options, int family,
                      int warn_severity, tor_addr_t *addr_out,
                      const char **method_out, char **hostname_out);
 
-uint32_t get_last_resolved_addr_v4(void);
-void reset_last_resolved_addr_v4(void);
+void resolved_addr_get_last(int family, tor_addr_t *addr_out);
+void resolved_addr_reset_last(int family);
 
 MOCK_DECL(int, is_local_addr, (const tor_addr_t *addr));
 
diff --git a/src/core/mainloop/connection.c b/src/core/mainloop/connection.c
index 9fff71ee0..3ebe18cd3 100644
--- a/src/core/mainloop/connection.c
+++ b/src/core/mainloop/connection.c
@@ -4870,7 +4870,7 @@ client_check_address_changed(tor_socket_t sock)
     smartlist_clear(outgoing_addrs);
     smartlist_add(outgoing_addrs, tor_memdup(&out_addr, sizeof(tor_addr_t)));
     /* We'll need to resolve ourselves again. */
-    reset_last_resolved_addr_v4();
+    resolved_addr_reset_last(AF_INET);
     /* Okay, now change our keys. */
     ip_address_changed(1);
   }
diff --git a/src/feature/relay/relay_find_addr.c b/src/feature/relay/relay_find_addr.c
index cc08914f6..70cb77d4b 100644
--- a/src/feature/relay/relay_find_addr.c
+++ b/src/feature/relay/relay_find_addr.c
@@ -45,7 +45,7 @@ void
 router_new_address_suggestion(const char *suggestion,
                               const dir_connection_t *d_conn)
 {
-  tor_addr_t addr;
+  tor_addr_t addr, last_resolved_addr;
   uint32_t cur = 0;             /* Current IPv4 address.  */
   const or_options_t *options = get_options();
 
@@ -64,14 +64,22 @@ router_new_address_suggestion(const char *suggestion,
   }
 
   /* XXXX ipv6 */
-  cur = get_last_resolved_addr_v4();
-  if (cur ||
-      resolve_my_address_v4(LOG_INFO, options, &cur, NULL, NULL) >= 0) {
+  resolved_addr_get_last(AF_INET, &last_resolved_addr);
+  if (!tor_addr_is_null(&last_resolved_addr)) {
+    /* Lets use this one. */
+    tor_addr_copy(&last_guessed_ip, &last_resolved_addr);
+    return;
+  }
+
+  /* Attempt to find our address. */
+  if (resolve_my_address_v4(LOG_INFO, options, &cur, NULL, NULL) >= 0) {
     /* We're all set -- we already know our address. Great. */
     tor_addr_from_ipv4h(&last_guessed_ip, cur); /* store it in case we
                                                    need it later */
     return;
   }
+
+  /* Consider the suggestion from the directory. */
   if (tor_addr_is_internal(&addr, 0)) {
     /* Don't believe anybody who says our IP is, say, 127.0.0.1. */
     return;
@@ -111,10 +119,14 @@ MOCK_IMPL(int,
 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_v4();
-  if (*addr)
+  tor_addr_t last_resolved_addr;
+
+  /* First, check the cached output from resolve_my_address_v4(). */
+  resolved_addr_get_last(AF_INET, &last_resolved_addr);
+  if (!tor_addr_is_null(&last_resolved_addr)) {
+    *addr = tor_addr_to_ipv4h(&last_resolved_addr);
     return 0;
+  }
 
   /* Second, consider doing a resolve attempt right here. */
   if (!cache_only) {



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