[tor-commits] [tor/master] addr: Remove resolve_my_address_v4()

dgoulet at torproject.org dgoulet at torproject.org
Wed Jun 24 17:58:40 UTC 2020


commit b76325190b3fb2d01fb0c9f0d2ffe76a284d1766
Author: David Goulet <dgoulet at torproject.org>
Date:   Tue Jun 23 11:19:50 2020 -0400

    addr: Remove resolve_my_address_v4()
    
    Replace it by find_my_address() everywhere. This changes many parts of the
    code that uses it to use a tor_addr_t instead of a plain uint32_t for IPv4.
    
    Many changes to the unit test to also use the new interface.
    
    Part #33233
    
    Signed-off-by: David Goulet <dgoulet at torproject.org>
---
 src/app/config/resolve_addr.c        |  68 +-----------------
 src/app/config/resolve_addr.h        |   4 --
 src/feature/dirauth/dirauth_config.c |   4 +-
 src/feature/dirauth/dirvote.c        |   8 +--
 src/feature/nodelist/dirlist.c       |  10 +--
 src/feature/relay/relay_find_addr.c  |  17 ++---
 src/feature/relay/router.c           |   5 +-
 src/test/test_config.c               | 136 +++++++++++++++++++----------------
 8 files changed, 100 insertions(+), 152 deletions(-)

diff --git a/src/app/config/resolve_addr.c b/src/app/config/resolve_addr.c
index c73519f14..56548e6e7 100644
--- a/src/app/config/resolve_addr.c
+++ b/src/app/config/resolve_addr.c
@@ -528,70 +528,6 @@ find_my_address(const or_options_t *options, int family, int warn_severity,
   return true;
 }
 
-/**
- * Attempt getting our non-local (as judged by tor_addr_is_internal()
- * function) IP address using following techniques, listed in
- * order from best (most desirable, try first) to worst (least
- * desirable, try if everything else fails).
- *
- * First, attempt using <b>options-\>Address</b> to get our
- * non-local IP address.
- *
- * If <b>options-\>Address</b> represents a non-local IP address,
- * consider it ours.
- *
- * If <b>options-\>Address</b> is a DNS name that resolves to
- * a non-local IP address, consider this IP address ours.
- *
- * If <b>options-\>Address</b> is NULL, fall back to getting local
- * hostname and using it in above-described ways to try and
- * get our IP address.
- *
- * In case local hostname cannot be resolved to a non-local IP
- * address, try getting an IP address of network interface
- * in hopes it will be non-local one.
- *
- * Fail if one or more of the following is true:
- *   - DNS name in <b>options-\>Address</b> cannot be resolved.
- *   - <b>options-\>Address</b> is a local host address.
- *   - Attempt at getting local hostname fails.
- *   - Attempt at getting network interface address fails.
- *
- * Return 0 if all is well, or -1 if we can't find a suitable
- * public IP address.
- *
- * If we are returning 0:
- *   - Put our public IP address (in host order) into *<b>addr_out</b>.
- *   - If <b>method_out</b> is non-NULL, set *<b>method_out</b> to a static
- *     string describing how we arrived at our answer.
- *      - "CONFIGURED" - parsed from IP address string in
- *        <b>options-\>Address</b>
- *      - "RESOLVED" - resolved from DNS name in <b>options-\>Address</b>
- *      - "GETHOSTNAME" - resolved from a local hostname.
- *      - "INTERFACE" - retrieved from a network interface.
- *   - If <b>hostname_out</b> is non-NULL, and we resolved a hostname to
- *     get our address, set *<b>hostname_out</b> to a newly allocated string
- *     holding that hostname. (If we didn't get our address by resolving a
- *     hostname, set *<b>hostname_out</b> to NULL.)
- *
- * XXXX ipv6
- */
-int
-resolve_my_address_v4(int warn_severity, const or_options_t *options,
-                      uint32_t *addr_out,
-                      const char **method_out, char **hostname_out)
-{
-  tor_addr_t my_addr;
-  bool ret = find_my_address(options, AF_INET, warn_severity, &my_addr,
-                             method_out, hostname_out);
-  if (!ret) {
-    return -1;
-  }
-
-  *addr_out = tor_addr_to_ipv4h(&my_addr);
-  return 0;
-}
-
 /** Return true iff <b>addr</b> is judged to be on the same network as us, or
  * on a private network.
  */
@@ -618,8 +554,8 @@ resolved_addr_is_local, (const tor_addr_t *addr))
      * can't use addrs_in_same_network_family(). */
 
     /* It's possible that this next check will hit before the first time
-     * resolve_my_address_v4 actually succeeds. For clients, it is likely that
-     * resolve_my_address_v4 will never be called at all. In those cases,
+     * find_my_address actually succeeds. For clients, it is likely that
+     * find_my_address will never be called at all. In those cases,
      * last_resolved_addr_v4 will be 0, and so checking to see whether ip is
      * on the same /24 as last_resolved_addrs[AF_INET] will be the same as
      * checking whether it was on net 0, which is already done by
diff --git a/src/app/config/resolve_addr.h b/src/app/config/resolve_addr.h
index f435ab29a..ff732561c 100644
--- a/src/app/config/resolve_addr.h
+++ b/src/app/config/resolve_addr.h
@@ -11,10 +11,6 @@
 
 #include "app/config/or_options_st.h"
 
-int resolve_my_address_v4(int warn_severity, const or_options_t *options,
-                          uint32_t *addr_out,
-                          const char **method_out, char **hostname_out);
-
 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);
diff --git a/src/feature/dirauth/dirauth_config.c b/src/feature/dirauth/dirauth_config.c
index 888b8e7d9..1ffd33e5f 100644
--- a/src/feature/dirauth/dirauth_config.c
+++ b/src/feature/dirauth/dirauth_config.c
@@ -77,8 +77,8 @@ options_validate_dirauth_mode(const or_options_t *old_options,
     return 0;
 
   /* confirm that our address isn't broken, so we can complain now */
-  uint32_t tmp;
-  if (resolve_my_address_v4(LOG_WARN, options, &tmp, NULL, NULL) < 0)
+  tor_addr_t tmp;
+  if (!find_my_address(options, AF_INET, LOG_WARN, &tmp, NULL, NULL))
     REJECT("Failed to resolve/guess local address. See logs for details.");
 
   if (!options->ContactInfo && !options->TestingTorNetwork)
diff --git a/src/feature/dirauth/dirvote.c b/src/feature/dirauth/dirvote.c
index 3030955fd..5fda84224 100644
--- a/src/feature/dirauth/dirvote.c
+++ b/src/feature/dirauth/dirvote.c
@@ -4463,7 +4463,7 @@ dirserv_generate_networkstatus_vote_obj(crypto_pk_t *private_key,
   const or_options_t *options = get_options();
   const dirauth_options_t *d_options = dirauth_get_options();
   networkstatus_t *v3_out = NULL;
-  uint32_t addr;
+  tor_addr_t addr;
   char *hostname = NULL, *client_versions = NULL, *server_versions = NULL;
   const char *contact;
   smartlist_t *routers, *routerstatuses;
@@ -4492,13 +4492,13 @@ dirserv_generate_networkstatus_vote_obj(crypto_pk_t *private_key,
     log_err(LD_BUG, "Error computing identity key digest");
     return NULL;
   }
-  if (resolve_my_address_v4(LOG_WARN, options, &addr, NULL, &hostname)<0) {
+  if (!find_my_address(options, AF_INET, LOG_WARN, &addr, NULL, &hostname)) {
     log_warn(LD_NET, "Couldn't resolve my hostname");
     return NULL;
   }
   if (!hostname || !strchr(hostname, '.')) {
     tor_free(hostname);
-    hostname = tor_dup_ip(addr);
+    hostname = tor_addr_to_str_dup(&addr);
   }
 
   if (!hostname) {
@@ -4722,7 +4722,7 @@ dirserv_generate_networkstatus_vote_obj(crypto_pk_t *private_key,
   memcpy(voter->identity_digest, identity_digest, DIGEST_LEN);
   voter->sigs = smartlist_new();
   voter->address = hostname;
-  voter->addr = addr;
+  voter->addr = tor_addr_to_ipv4h(&addr);
   voter->dir_port = router_get_advertised_dir_port(options, 0);
   voter->or_port = router_get_advertised_or_port(options);
   voter->contact = tor_strdup(contact);
diff --git a/src/feature/nodelist/dirlist.c b/src/feature/nodelist/dirlist.c
index 4481daba6..f49d991f9 100644
--- a/src/feature/nodelist/dirlist.c
+++ b/src/feature/nodelist/dirlist.c
@@ -343,25 +343,25 @@ trusted_dir_server_new(const char *nickname, const char *address,
                        const char *digest, const char *v3_auth_digest,
                        dirinfo_type_t type, double weight)
 {
-  uint32_t a;
   tor_addr_t addr;
   char *hostname=NULL;
   dir_server_t *result;
 
   if (!address) { /* The address is us; we should guess. */
-    if (resolve_my_address_v4(LOG_WARN, get_options(),
-                              &a, NULL, &hostname) < 0) {
+    if (!find_my_address(get_options(), AF_INET, LOG_WARN, &addr,
+                         NULL, &hostname)) {
       log_warn(LD_CONFIG,
                "Couldn't find a suitable address when adding ourself as a "
                "trusted directory server.");
       return NULL;
     }
     if (!hostname)
-      hostname = tor_dup_ip(a);
+      hostname = tor_addr_to_str_dup(&addr);
 
     if (!hostname)
       return NULL;
   } else {
+    uint32_t a;
     if (tor_lookup_hostname(address, &a)) {
       log_warn(LD_CONFIG,
                "Unable to lookup address for directory server at '%s'",
@@ -369,8 +369,8 @@ trusted_dir_server_new(const char *nickname, const char *address,
       return NULL;
     }
     hostname = tor_strdup(address);
+    tor_addr_from_ipv4h(&addr, a);
   }
-  tor_addr_from_ipv4h(&addr, a);
 
   result = dir_server_new(1, nickname, &addr, hostname,
                           dir_port, or_port,
diff --git a/src/feature/relay/relay_find_addr.c b/src/feature/relay/relay_find_addr.c
index 70cb77d4b..a51457ddb 100644
--- a/src/feature/relay/relay_find_addr.c
+++ b/src/feature/relay/relay_find_addr.c
@@ -45,8 +45,7 @@ void
 router_new_address_suggestion(const char *suggestion,
                               const dir_connection_t *d_conn)
 {
-  tor_addr_t addr, last_resolved_addr;
-  uint32_t cur = 0;             /* Current IPv4 address.  */
+  tor_addr_t addr, my_addr, last_resolved_addr;
   const or_options_t *options = get_options();
 
   /* first, learn what the IP address actually is */
@@ -72,10 +71,10 @@ router_new_address_suggestion(const char *suggestion,
   }
 
   /* Attempt to find our address. */
-  if (resolve_my_address_v4(LOG_INFO, options, &cur, NULL, NULL) >= 0) {
+  if (find_my_address(options, AF_INET, LOG_INFO, &my_addr, NULL, NULL)) {
     /* 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 */
+    tor_addr_copy(&last_guessed_ip, &my_addr); /* store it in case we
+                                                  need it later */
     return;
   }
 
@@ -121,7 +120,7 @@ router_pick_published_address, (const or_options_t *options, uint32_t *addr,
 {
   tor_addr_t last_resolved_addr;
 
-  /* First, check the cached output from resolve_my_address_v4(). */
+  /* First, check the cached output from find_my_address(). */
   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);
@@ -130,8 +129,10 @@ router_pick_published_address, (const or_options_t *options, uint32_t *addr,
 
   /* Second, consider doing a resolve attempt right here. */
   if (!cache_only) {
-    if (resolve_my_address_v4(LOG_INFO, options, addr, NULL, NULL) >= 0) {
-      log_info(LD_CONFIG,"Success: chose address '%s'.", fmt_addr32(*addr));
+    tor_addr_t my_addr;
+    if (find_my_address(options, AF_INET, LOG_INFO, &my_addr, NULL, NULL)) {
+      log_info(LD_CONFIG,"Success: chose address '%s'.", fmt_addr(&my_addr));
+      *addr = tor_addr_to_ipv4h(&my_addr);
       return 0;
     }
   }
diff --git a/src/feature/relay/router.c b/src/feature/relay/router.c
index 4d5ed3a3e..fd62a3073 100644
--- a/src/feature/relay/router.c
+++ b/src/feature/relay/router.c
@@ -2540,6 +2540,7 @@ void
 check_descriptor_ipaddress_changed(time_t now)
 {
   uint32_t prev, cur;
+  tor_addr_t addr;
   const or_options_t *options = get_options();
   const char *method = NULL;
   char *hostname = NULL;
@@ -2552,10 +2553,12 @@ check_descriptor_ipaddress_changed(time_t now)
 
   /* XXXX ipv6 */
   prev = my_ri->addr;
-  if (resolve_my_address_v4(LOG_INFO, options, &cur, &method, &hostname) < 0) {
+  if (!find_my_address(options, AF_INET, LOG_INFO, &addr, &method,
+                       &hostname)) {
     log_info(LD_CONFIG,"options->Address didn't resolve into an IP.");
     return;
   }
+  cur = tor_addr_to_ipv4h(&addr);
 
   if (prev != cur) {
     char *source;
diff --git a/src/test/test_config.c b/src/test/test_config.c
index a6b02713f..b082a4989 100644
--- a/src/test/test_config.c
+++ b/src/test/test_config.c
@@ -1170,14 +1170,14 @@ get_interface_address6_failure(int severity, sa_family_t family,
 }
 
 static void
-test_config_resolve_my_address_v4(void *arg)
+test_config_find_my_address(void *arg)
 {
   or_options_t *options;
-  uint32_t resolved_addr;
+  tor_addr_t resolved_addr, test_addr;
   char buf[1024];
   const char *method_used;
   char *hostname_out = NULL;
-  int retval;
+  bool retval;
   int prev_n_hostname_01010101;
   int prev_n_hostname_localhost;
   int prev_n_hostname_failure;
@@ -1200,20 +1200,21 @@ test_config_resolve_my_address_v4(void *arg)
   */
   strlcpy(buf, "Address 128.52.128.105\n", sizeof(buf));
   config_get_lines(buf, &(options->Address), 0);
+  tor_addr_parse(&test_addr, "128.52.128.105");
 
-  retval = resolve_my_address_v4(LOG_NOTICE,options,&resolved_addr,
-                              &method_used,&hostname_out);
+  retval = find_my_address(options, AF_INET, LOG_NOTICE, &resolved_addr,
+                           &method_used, &hostname_out);
 
-  tt_want(retval == 0);
+  tt_want(retval == true);
   tt_want_str_op(method_used,OP_EQ,"CONFIGURED");
   tt_want(hostname_out == NULL);
-  tt_u64_op(resolved_addr, OP_EQ, 0x80348069);
+  tt_int_op(tor_addr_eq(&resolved_addr, &test_addr), OP_EQ, 1);
 
   config_free_lines(options->Address);
 
 /*
  * CASE 2:
- * If options->Address is a valid DNS address, we want resolve_my_address_v4()
+ * If options->Address is a valid DNS address, we want find_my_address()
  * function to ask tor_addr_lookup() for help with resolving it
  * and return the address that was resolved (in host order).
  */
@@ -1222,17 +1223,18 @@ test_config_resolve_my_address_v4(void *arg)
 
   strlcpy(buf, "Address www.torproject.org\n", sizeof(buf));
   config_get_lines(buf, &(options->Address), 0);
+  tor_addr_parse(&test_addr, "1.1.1.1");
 
   prev_n_hostname_01010101 = n_hostname_01010101;
 
-  retval = resolve_my_address_v4(LOG_NOTICE,options,&resolved_addr,
-                              &method_used,&hostname_out);
+  retval = find_my_address(options, AF_INET, LOG_NOTICE, &resolved_addr,
+                           &method_used, &hostname_out);
 
-  tt_want(retval == 0);
+  tt_want(retval == true);
   tt_want(n_hostname_01010101 == prev_n_hostname_01010101 + 1);
   tt_want_str_op(method_used,OP_EQ,"RESOLVED");
   tt_want_str_op(hostname_out,OP_EQ,"www.torproject.org");
-  tt_u64_op(resolved_addr, OP_EQ, 0x01010101);
+  tt_int_op(tor_addr_eq(&resolved_addr, &test_addr), OP_EQ, 1);
 
   UNMOCK(tor_addr_lookup);
 
@@ -1241,13 +1243,14 @@ test_config_resolve_my_address_v4(void *arg)
 
 /*
  * CASE 3:
- * Given that options->Address is NULL, we want resolve_my_address_v4()
+ * Given that options->Address is NULL, we want find_my_address()
  * to try and use tor_gethostname() to get hostname AND use
  * tor_addr_lookup() to get IP address.
  */
 
-  resolved_addr = 0;
+  tor_addr_make_unspec(&resolved_addr);
   options->Address = NULL;
+  tor_addr_parse(&test_addr, "1.1.1.1");
 
   MOCK(tor_gethostname,tor_gethostname_replacement);
   MOCK(tor_addr_lookup,tor_addr_lookup_01010101);
@@ -1255,15 +1258,15 @@ test_config_resolve_my_address_v4(void *arg)
   prev_n_gethostname_replacement = n_gethostname_replacement;
   prev_n_hostname_01010101 = n_hostname_01010101;
 
-  retval = resolve_my_address_v4(LOG_NOTICE,options,&resolved_addr,
-                              &method_used,&hostname_out);
+  retval = find_my_address(options, AF_INET, LOG_NOTICE, &resolved_addr,
+                           &method_used, &hostname_out);
 
-  tt_want(retval == 0);
+  tt_want(retval == true);
   tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
   tt_want(n_hostname_01010101 == prev_n_hostname_01010101 + 1);
   tt_want_str_op(method_used,OP_EQ,"GETHOSTNAME");
   tt_want_str_op(hostname_out,OP_EQ,"onionrouter!");
-  tt_assert(resolved_addr == 0x01010101);
+  tt_int_op(tor_addr_eq(&resolved_addr, &test_addr), OP_EQ, 1);
 
   UNMOCK(tor_gethostname);
   UNMOCK(tor_addr_lookup);
@@ -1273,25 +1276,26 @@ test_config_resolve_my_address_v4(void *arg)
 /*
  * CASE 4:
  * Given that options->Address is a local host address, we want
- * resolve_my_address_v4() function to fail.
+ * find_my_address() function to fail.
  */
 
-  resolved_addr = 0;
+  tor_addr_make_unspec(&resolved_addr);
   strlcpy(buf, "Address 127.0.0.1\n", sizeof(buf));
   config_get_lines(buf, &(options->Address), 0);
+  tor_addr_parse(&test_addr, "127.0.0.1");
 
-  retval = resolve_my_address_v4(LOG_NOTICE,options,&resolved_addr,
-                              &method_used,&hostname_out);
+  retval = find_my_address(options, AF_INET, LOG_NOTICE, &resolved_addr,
+                           &method_used, &hostname_out);
 
-  tt_want(resolved_addr == 0);
-  tt_int_op(retval, OP_EQ, -1);
+  tt_want(tor_addr_is_null(&resolved_addr) == 1);
+  tt_want(retval == false);
 
   config_free_lines(options->Address);
   tor_free(hostname_out);
 
 /*
  * CASE 5:
- * We want resolve_my_address_v4() to fail if DNS address in options->Address
+ * We want find_my_address() to fail if DNS address in options->Address
  * cannot be resolved.
  */
 
@@ -1302,11 +1306,12 @@ test_config_resolve_my_address_v4(void *arg)
   strlcpy(buf, "Address www.tor-project.org\n", sizeof(buf));
   config_get_lines(buf, &(options->Address), 0);
 
-  retval = resolve_my_address_v4(LOG_NOTICE,options,&resolved_addr,
-                              &method_used,&hostname_out);
+  retval = find_my_address(options, AF_INET, LOG_NOTICE, &resolved_addr,
+                           &method_used, &hostname_out);
 
   tt_want(n_hostname_failure == prev_n_hostname_failure + 1);
-  tt_int_op(retval, OP_EQ, -1);
+  tt_want(tor_addr_is_null(&resolved_addr) == 1);
+  tt_want(retval == false);
 
   UNMOCK(tor_addr_lookup);
 
@@ -1317,25 +1322,26 @@ test_config_resolve_my_address_v4(void *arg)
 /*
  * CASE 6:
  * If options->Address is NULL AND gettting local hostname fails, we want
- * resolve_my_address_v4() to fail as well.
+ * find_my_address() to fail as well.
  */
 
   MOCK(tor_gethostname,tor_gethostname_failure);
 
   prev_n_gethostname_failure = n_gethostname_failure;
 
-  retval = resolve_my_address_v4(LOG_NOTICE,options,&resolved_addr,
-                              &method_used,&hostname_out);
+  retval = find_my_address(options, AF_INET, LOG_NOTICE, &resolved_addr,
+                           &method_used, &hostname_out);
 
   tt_want(n_gethostname_failure == prev_n_gethostname_failure + 1);
-  tt_int_op(retval, OP_EQ, -1);
+  tt_want(tor_addr_is_null(&resolved_addr) == 1);
+  tt_want(retval == false);
 
   UNMOCK(tor_gethostname);
   tor_free(hostname_out);
 
 /*
  * CASE 7:
- * We want resolve_my_address_v4() to try and get network interface address via
+ * We want find_my_address() to try and get network interface address via
  * get_interface_address() if hostname returned by tor_gethostname() cannot be
  * resolved into IP address.
  */
@@ -1344,20 +1350,22 @@ test_config_resolve_my_address_v4(void *arg)
   MOCK(tor_addr_lookup,tor_addr_lookup_failure);
   MOCK(get_interface_address6, get_interface_address6_08080808);
 
+  tor_addr_parse(&test_addr, "8.8.8.8");
+
   prev_n_gethostname_replacement = n_gethostname_replacement;
   prev_n_get_interface_address6 = n_get_interface_address6;
 
-  retval = resolve_my_address_v4(LOG_NOTICE,options,&resolved_addr,
-                                 &method_used,&hostname_out);
+  retval = find_my_address(options, AF_INET, LOG_NOTICE, &resolved_addr,
+                           &method_used, &hostname_out);
 
-  tt_want(retval == 0);
+  tt_want(retval == true);
   tt_want_int_op(n_gethostname_replacement, OP_EQ,
                  prev_n_gethostname_replacement + 1);
   tt_want_int_op(n_get_interface_address6, OP_EQ,
                  prev_n_get_interface_address6 + 1);
   tt_want_str_op(method_used,OP_EQ,"INTERFACE");
   tt_want(hostname_out == NULL);
-  tt_assert(resolved_addr == 0x08080808);
+  tt_int_op(tor_addr_eq(&resolved_addr, &test_addr), OP_EQ, 1);
 
   UNMOCK(get_interface_address);
   tor_free(hostname_out);
@@ -1365,7 +1373,7 @@ test_config_resolve_my_address_v4(void *arg)
 /*
  * CASE 8:
  * Suppose options->Address is NULL AND hostname returned by tor_gethostname()
- * is unresolvable. We want resolve_my_address_v4 to fail if
+ * is unresolvable. We want find_my_address to fail if
  * get_interface_address() fails.
  */
 
@@ -1374,14 +1382,14 @@ test_config_resolve_my_address_v4(void *arg)
   prev_n_get_interface_address6_failure = n_get_interface_address6_failure;
   prev_n_gethostname_replacement = n_gethostname_replacement;
 
-  retval = resolve_my_address_v4(LOG_NOTICE,options,&resolved_addr,
-                              &method_used,&hostname_out);
+  retval = find_my_address(options, AF_INET, LOG_NOTICE, &resolved_addr,
+                           &method_used, &hostname_out);
 
   tt_want(n_get_interface_address6_failure ==
           prev_n_get_interface_address6_failure + 1);
   tt_want(n_gethostname_replacement ==
           prev_n_gethostname_replacement + 1);
-  tt_int_op(retval, OP_EQ, -1);
+  tt_want(retval == false);
 
   UNMOCK(get_interface_address);
   tor_free(hostname_out);
@@ -1390,7 +1398,7 @@ test_config_resolve_my_address_v4(void *arg)
  * CASE 9:
  * Given that options->Address is NULL AND tor_addr_lookup()
  * fails AND hostname returned by gethostname() resolves
- * to local IP address, we want resolve_my_address_v4() function to
+ * to local IP address, we want find_my_address() function to
  * call get_interface_address6(.,AF_INET,.) and return IP address
  * the latter function has found.
  */
@@ -1399,20 +1407,22 @@ test_config_resolve_my_address_v4(void *arg)
   MOCK(tor_gethostname,tor_gethostname_replacement);
   MOCK(get_interface_address6,get_interface_address6_replacement);
 
+  tor_addr_parse(&test_addr, "9.9.9.9");
+
   prev_n_gethostname_replacement = n_gethostname_replacement;
   prev_n_hostname_failure = n_hostname_failure;
   prev_n_get_interface_address6 = n_get_interface_address6;
 
-  retval = resolve_my_address_v4(LOG_NOTICE,options,&resolved_addr,
-                              &method_used,&hostname_out);
+  retval = find_my_address(options, AF_INET, LOG_NOTICE, &resolved_addr,
+                           &method_used, &hostname_out);
 
   tt_want(last_address6_family == AF_INET);
   tt_want(n_get_interface_address6 == prev_n_get_interface_address6 + 1);
   tt_want(n_hostname_failure == prev_n_hostname_failure + 1);
   tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
-  tt_want(retval == 0);
+  tt_want(retval == true);
   tt_want_str_op(method_used,OP_EQ,"INTERFACE");
-  tt_assert(resolved_addr == 0x09090909);
+  tt_int_op(tor_addr_eq(&resolved_addr, &test_addr), OP_EQ, 1);
 
   UNMOCK(tor_addr_lookup);
   UNMOCK(tor_gethostname);
@@ -1421,7 +1431,7 @@ test_config_resolve_my_address_v4(void *arg)
   tor_free(hostname_out);
 
   /*
-   * CASE 10: We want resolve_my_address_v4() to fail if all of the following
+   * CASE 10: We want find_my_address() to fail if all of the following
    * are true:
    *   1. options->Address is not NULL
    *   2. ... but it cannot be converted to struct in_addr by
@@ -1437,11 +1447,11 @@ test_config_resolve_my_address_v4(void *arg)
   strlcpy(buf, "Address some_hostname\n", sizeof(buf));
   config_get_lines(buf, &(options->Address), 0);
 
-  retval = resolve_my_address_v4(LOG_NOTICE, options, &resolved_addr,
-                              &method_used,&hostname_out);
+  retval = find_my_address(options, AF_INET, LOG_NOTICE, &resolved_addr,
+                           &method_used, &hostname_out);
 
   tt_want(n_hostname_failure == prev_n_hostname_failure + 1);
-  tt_int_op(retval, OP_EQ, -1);
+  tt_want(retval == false);
 
   UNMOCK(tor_gethostname);
   UNMOCK(tor_addr_lookup);
@@ -1469,6 +1479,7 @@ test_config_resolve_my_address_v4(void *arg)
 
   config_free_lines(options->Address);
   options->Address = NULL;
+  tor_addr_parse(&test_addr, "9.9.9.9");
 
   MOCK(tor_gethostname,tor_gethostname_replacement);
   MOCK(tor_addr_lookup,tor_addr_lookup_localhost);
@@ -1478,8 +1489,8 @@ test_config_resolve_my_address_v4(void *arg)
   prev_n_hostname_localhost = n_hostname_localhost;
   prev_n_get_interface_address6 = n_get_interface_address6;
 
-  retval = resolve_my_address_v4(LOG_DEBUG, options, &resolved_addr,
-                              &method_used,&hostname_out);
+  retval = find_my_address(options, AF_INET, LOG_NOTICE, &resolved_addr,
+                           &method_used, &hostname_out);
 
   tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
   tt_want(n_hostname_localhost == prev_n_hostname_localhost + 1);
@@ -1487,14 +1498,15 @@ test_config_resolve_my_address_v4(void *arg)
 
   tt_str_op(method_used,OP_EQ,"INTERFACE");
   tt_ptr_op(hostname_out, OP_EQ, NULL);
-  tt_int_op(retval, OP_EQ, 0);
+  tt_want(retval == true);
+  tt_int_op(tor_addr_eq(&resolved_addr, &test_addr), OP_EQ, 1);
 
   /*
    * CASE 11b:
    *   1-5 as above.
    *   6. get_interface_address6() fails.
    *
-   *   In this subcase, we want resolve_my_address_v4() to fail.
+   *   In this subcase, we want find_my_address() to fail.
    */
 
   UNMOCK(get_interface_address6);
@@ -1504,15 +1516,15 @@ test_config_resolve_my_address_v4(void *arg)
   prev_n_hostname_localhost = n_hostname_localhost;
   prev_n_get_interface_address6_failure = n_get_interface_address6_failure;
 
-  retval = resolve_my_address_v4(LOG_DEBUG, options, &resolved_addr,
-                              &method_used,&hostname_out);
+  retval = find_my_address(options, AF_INET, LOG_DEBUG, &resolved_addr,
+                           &method_used, &hostname_out);
 
   tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
   tt_want(n_hostname_localhost == prev_n_hostname_localhost + 1);
   tt_want(n_get_interface_address6_failure ==
           prev_n_get_interface_address6_failure + 1);
 
-  tt_int_op(retval, OP_EQ, -1);
+  tt_want(retval == false);
 
   UNMOCK(tor_gethostname);
   UNMOCK(tor_addr_lookup);
@@ -1526,7 +1538,7 @@ test_config_resolve_my_address_v4(void *arg)
    *   4. into IPv4 address that tor_addr_is_inernal() considers to be
    *      internal.
    *
-   *  In this case, we want resolve_my_address_v4() to fail.
+   *  In this case, we want find_my_address() to fail.
    */
 
   tor_free(options->Address);
@@ -1537,11 +1549,11 @@ test_config_resolve_my_address_v4(void *arg)
 
   prev_n_gethostname_localhost = n_gethostname_localhost;
 
-  retval = resolve_my_address_v4(LOG_DEBUG, options, &resolved_addr,
-                              &method_used,&hostname_out);
+  retval = find_my_address(options, AF_INET, LOG_DEBUG, &resolved_addr,
+                           &method_used, &hostname_out);
 
   tt_want(n_gethostname_localhost == prev_n_gethostname_localhost + 1);
-  tt_int_op(retval, OP_EQ, -1);
+  tt_want(retval == false);
 
   UNMOCK(tor_gethostname);
 
@@ -6239,7 +6251,7 @@ struct testcase_t config_tests[] = {
   CONFIG_TEST(adding_dir_servers, TT_FORK),
   CONFIG_TEST(default_dir_servers, TT_FORK),
   CONFIG_TEST(default_fallback_dirs, 0),
-  CONFIG_TEST(resolve_my_address_v4, TT_FORK),
+  CONFIG_TEST(find_my_address, TT_FORK),
   CONFIG_TEST(addressmap, 0),
   CONFIG_TEST(parse_bridge_line, 0),
   CONFIG_TEST(parse_transport_options_line, 0),





More information about the tor-commits mailing list