[tor-commits] [obfsproxy/master] Return an evutil_addrinfo from resolve_address_port rather than copying out the address.

nickm at torproject.org nickm at torproject.org
Fri Sep 9 17:08:56 UTC 2011


commit b06baa42832cd05576dc32b9d113aef31d3c9b55
Author: Zack Weinberg <zackw at panix.com>
Date:   Sun Jul 24 13:03:34 2011 -0700

    Return an evutil_addrinfo from resolve_address_port rather than copying out the address.
---
 src/network.c         |   14 +++++++-------
 src/protocol.c        |    8 ++++----
 src/protocol.h        |    9 +++------
 src/protocols/dummy.c |    7 ++-----
 src/protocols/obfs2.c |   10 ++++------
 src/util.c            |   47 ++++++++++++++++++++++++-----------------------
 src/util.h            |    9 ++++-----
 7 files changed, 48 insertions(+), 56 deletions(-)

diff --git a/src/network.c b/src/network.c
index 7d05b91..75d90ef 100644
--- a/src/network.c
+++ b/src/network.c
@@ -117,11 +117,10 @@ listener_new(struct event_base *base,
 
   lsn->proto_params = proto_params;
 
-  lsn->listener = evconnlistener_new_bind(base, simple_listener_cb, lsn,
-                                          flags,
-                                          -1,
-                                          proto_params->listen_address,
-                                          proto_params->listen_address_len);
+  lsn->listener =
+    evconnlistener_new_bind(base, simple_listener_cb, lsn, flags, -1,
+                            proto_params->listen_addr->ai_addr,
+                            proto_params->listen_addr->ai_addrlen);
 
   if (!lsn->listener) {
     log_warn("Failed to create listener!");
@@ -246,8 +245,9 @@ simple_listener_cb(struct evconnlistener *evcl,
   if (conn->mode == LSN_SIMPLE_SERVER || conn->mode == LSN_SIMPLE_CLIENT) {
     /* Launch the connect attempt. */
     if (bufferevent_socket_connect(conn->output,
-                                   lsn->proto_params->target_address,
-                                   lsn->proto_params->target_address_len)<0)
+                                   lsn->proto_params->target_addr->ai_addr,
+                                   lsn->proto_params->target_addr->ai_addrlen)
+        < 0)
       goto err;
 
     bufferevent_enable(conn->output, EV_READ|EV_WRITE);
diff --git a/src/protocol.c b/src/protocol.c
index 6abf7f1..8cd069e 100644
--- a/src/protocol.c
+++ b/src/protocol.c
@@ -53,10 +53,10 @@ proto_params_free(protocol_params_t *params)
 {
   obfs_assert(params);
 
-  if (params->target_address)
-    free(params->target_address);
-  if (params->listen_address)
-    free(params->listen_address);
+  if (params->target_addr)
+    evutil_freeaddrinfo(params->target_addr);
+  if (params->listen_addr)
+    evutil_freeaddrinfo(params->listen_addr);
   if (params->shared_secret)
     free(params->shared_secret);
   free(params);
diff --git a/src/protocol.h b/src/protocol.h
index 06dec08..6fa3eb4 100644
--- a/src/protocol.h
+++ b/src/protocol.h
@@ -5,11 +5,10 @@
 #ifndef PROTOCOL_H
 #define PROTOCOL_H
 
-#include <stddef.h>   /* for size_t */
 #include "network.h"  /* for recv_ret */
+#include <event2/util.h> /* for evutil_addrinfo */
 
 struct evbuffer;
-struct sockaddr;
 
 /**
   This struct defines parameters of a protocol on a per-listener basis.
@@ -20,12 +19,10 @@ struct sockaddr;
 */
 typedef struct protocol_params_t {
   const struct protocol_vtable *vtable;
-  struct sockaddr *target_address;
-  struct sockaddr *listen_address;
+  struct evutil_addrinfo *target_addr;
+  struct evutil_addrinfo *listen_addr;
   char *shared_secret;
   size_t shared_secret_len;
-  size_t target_address_len;
-  size_t listen_address_len;
   int mode;
 } protocol_params_t;
 
diff --git a/src/protocols/dummy.c b/src/protocols/dummy.c
index 578182a..c12fe01 100644
--- a/src/protocols/dummy.c
+++ b/src/protocols/dummy.c
@@ -63,12 +63,9 @@ parse_and_set_options(int n_options, const char *const *options,
   } else
     return -1;
 
-  if (resolve_address_port(options[1], 1, 1,
-                           &params->listen_address,
-                           &params->listen_address_len, defport) < 0) {
-    log_warn("addr");
+  params->listen_addr = resolve_address_port(options[1], 1, 1, defport);
+  if (!params->listen_addr)
     return -1;
-  }
 
   params->vtable = &dummy_vtable;
   return 0;
diff --git a/src/protocols/obfs2.c b/src/protocols/obfs2.c
index 88d750f..42d30c1 100644
--- a/src/protocols/obfs2.c
+++ b/src/protocols/obfs2.c
@@ -67,9 +67,8 @@ parse_and_set_options(int n_options, const char *const *options,
       if (!strncmp(*options,"--dest=",7)) {
         if (got_dest)
           return -1;
-        if (resolve_address_port(*options+7, 1, 0,
-                                 &params->target_address,
-                                 &params->target_address_len, NULL) < 0)
+        params->target_addr = resolve_address_port(*options+7, 1, 0, NULL);
+        if (!params->target_addr)
           return -1;
         got_dest=1;
       } else if (!strncmp(*options,"--shared-secret=",16)) {
@@ -102,9 +101,8 @@ parse_and_set_options(int n_options, const char *const *options,
     }
     options++;
 
-    if (resolve_address_port(*options, 1, 1,
-                             &params->listen_address,
-                             &params->listen_address_len, defport) < 0)
+    params->listen_addr = resolve_address_port(*options, 1, 1, defport);
+    if (!params->listen_addr)
       return -1;
 
     /* Validate option selection. */
diff --git a/src/util.c b/src/util.c
index a4801a0..87087c2 100644
--- a/src/util.c
+++ b/src/util.c
@@ -141,7 +141,7 @@ ui64_log2(uint64_t u64)
 
 /**
    Accepts a string 'address' of the form ADDRESS:PORT and attempts to
-   parse it into 'addr_out' and put it's length into 'addrlen_out'.
+   parse it into an 'evutil_addrinfo' structure.
 
    If 'nodns' is set it means that 'address' was an IP address.
    If 'passive' is set it means that the address is destined for
@@ -150,16 +150,13 @@ ui64_log2(uint64_t u64)
    If no port was given in 'address', we set 'default_port' as the
    port.
 */
-int
-resolve_address_port(const char *address,
-                     int nodns, int passive,
-                     struct sockaddr **addr_out,
-                     size_t *addrlen_out,
+struct evutil_addrinfo *
+resolve_address_port(const char *address, int nodns, int passive,
                      const char *default_port)
 {
   struct evutil_addrinfo *ai = NULL;
   struct evutil_addrinfo ai_hints;
-  int result = -1, ai_res;
+  int ai_res, ai_errno;
   char *a = xstrdup(address), *cp;
   const char *portstr;
 
@@ -170,7 +167,8 @@ resolve_address_port(const char *address,
     portstr = default_port;
   } else {
     log_debug("Error in address %s: port required.", address);
-    goto done;
+    free(a);
+    return NULL;
   }
 
   memset(&ai_hints, 0, sizeof(ai_hints));
@@ -182,24 +180,27 @@ resolve_address_port(const char *address,
   if (nodns)
     ai_hints.ai_flags |= EVUTIL_AI_NUMERICHOST;
 
-  if ((ai_res = evutil_getaddrinfo(a, portstr, &ai_hints, &ai))) {
-    log_warn("Error resolving %s (%s) (%s): %s",
-             address,  a, portstr, evutil_gai_strerror(ai_res));
-    goto done;
-  }
-  if (ai == NULL) {
+  ai_res = evutil_getaddrinfo(a, portstr, &ai_hints, &ai);
+  ai_errno = errno;
+
+  free(a);
+
+  if (ai_res) {
+    if (ai_res == EAI_SYSTEM)
+      log_warn("Error resolving %s: %s [%s]",
+               address, evutil_gai_strerror(ai_res), strerror(ai_errno));
+    else
+      log_warn("Error resolving %s: %s", address, evutil_gai_strerror(ai_res));
+
+    if (ai) {
+      evutil_freeaddrinfo(ai);
+      ai = NULL;
+    }
+  } else if (ai == NULL) {
     log_warn("No result for address %s", address);
-    goto done;
   }
-  *addrlen_out = ai->ai_addrlen;
-  *addr_out = xmemdup(ai->ai_addr, ai->ai_addrlen);
-  result = 0;
 
- done:
-  free(a);
-  if (ai)
-    evutil_freeaddrinfo(ai);
-  return result;
+  return ai;
 }
 
 static struct evdns_base *the_evdns_base = NULL;
diff --git a/src/util.h b/src/util.h
index a88d06c..cb98fbd 100644
--- a/src/util.h
+++ b/src/util.h
@@ -9,6 +9,7 @@
 #include <stdarg.h> /* va_list */
 #include <stddef.h> /* size_t, ptrdiff_t, offsetof, NULL */
 #include <stdint.h> /* intN_t, uintN_t */
+#include <event2/util.h> /* evutil_addrinfo */
 
 struct sockaddr;
 struct event_base;
@@ -50,11 +51,9 @@ unsigned int ui64_log2(uint64_t u64);
 
 /***** Network functions. *****/
 
-int resolve_address_port(const char *address,
-                         int nodns, int passive,
-                         struct sockaddr **addr_out,
-                         size_t *addrlen_out,
-                         const char *default_port);
+struct evutil_addrinfo *resolve_address_port(const char *address,
+                                             int nodns, int passive,
+                                             const char *default_port);
 
 struct evdns_base *get_evdns_base(void);
 int init_evdns_base(struct event_base *base);





More information about the tor-commits mailing list