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

[tor-commits] [tor/master] resolve: refactor address family logic in tor_addr_lookup()



commit 29cf64c8389fd4de1e4419cb8ac30b2ca582ba5e
Author: teor <teor@xxxxxxxxxxxxxx>
Date:   Sun Jun 2 18:20:25 2019 +1000

    resolve: refactor address family logic in tor_addr_lookup()
    
    Cleanup after 30721.
---
 src/lib/net/resolve.c | 20 +++++++-------------
 1 file changed, 7 insertions(+), 13 deletions(-)

diff --git a/src/lib/net/resolve.c b/src/lib/net/resolve.c
index 71c033fe7..1b68a0b33 100644
--- a/src/lib/net/resolve.c
+++ b/src/lib/net/resolve.c
@@ -81,30 +81,24 @@ tor_addr_lookup,(const char *name, uint16_t family, tor_addr_t *addr))
   tor_assert(addr);
   tor_assert(family == AF_INET || family == AF_INET6 || family == AF_UNSPEC);
 
-  /* Clear address before starting, to avoid returning uninitialised data */
-  memset(addr, 0, sizeof(tor_addr_t));
-
   if (!*name) {
     /* Empty address is an error. */
+    memset(addr, 0, sizeof(tor_addr_t));
     return -1;
   }
 
   /* Is it an IP address? */
   parsed_family = tor_addr_parse(addr, name);
 
-  if (parsed_family == AF_INET) {
-    /* It's an IPv4 IP. */
-    if (family == AF_INET6) {
-      memset(addr, 0, sizeof(tor_addr_t));
-      return -1;
-    }
-    return 0;
-  } else if (parsed_family == AF_INET6) {
-    if (family == AF_INET) {
+  if (parsed_family >= 0) {
+    /* If the IP address family matches, or was unspecified */
+    if (parsed_family == family || family == AF_UNSPEC) {
+      return 0;
+    } else {
+      /* Clear the address before returning an error. */
       memset(addr, 0, sizeof(tor_addr_t));
       return -1;
     }
-    return 0;
   } else {
     /* Clear the address after a failed tor_addr_parse(). */
     memset(addr, 0, sizeof(tor_addr_t));



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