[tor-commits] [obfsproxy/master] Merge protocol_params_t into listener_t.

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


commit 07dd121cd4d49793df67b3e1a57e52e8bdf7f9a8
Author: Zack Weinberg <zackw at panix.com>
Date:   Tue Aug 2 10:16:31 2011 -0700

    Merge protocol_params_t into listener_t.
---
 src/network.c             |   68 +++++++++++++++++++++++---------------------
 src/network.h             |   24 ++++++++++++----
 src/protocol.c            |   53 +++++++++++++----------------------
 src/protocol.h            |   46 ++++++++++--------------------
 src/protocols/dummy.c     |   57 ++++++++++++++++++++-----------------
 src/protocols/dummy.h     |    7 ++--
 src/test/unittest_dummy.c |   34 +++++++++++-----------
 src/util.h                |    4 +-
 8 files changed, 144 insertions(+), 149 deletions(-)

diff --git a/src/network.c b/src/network.c
index c327403..b1d71dc 100644
--- a/src/network.c
+++ b/src/network.c
@@ -58,6 +58,8 @@ static smartlist_t *connections;
     connections and shutdowns when the last connection is closed. */
 static int shutting_down=0;
 
+static void listener_free(listener_t *lsn);
+
 static void simple_client_listener_cb(struct evconnlistener *evcl,
    evutil_socket_t fd, struct sockaddr *sourceaddr, int socklen, void *arg);
 static void socks_client_listener_cb(struct evconnlistener *evcl,
@@ -135,35 +137,34 @@ create_listener(struct event_base *base, int argc, const char *const *argv)
   const unsigned flags =
     LEV_OPT_CLOSE_ON_FREE|LEV_OPT_CLOSE_ON_EXEC|LEV_OPT_REUSEABLE;
   evconnlistener_cb callback;
-  listener_t *lsn;
-  protocol_params_t *params = proto_params_init(argc, argv);
-  if (!params)
+  listener_t *lsn = proto_listener_create(argc, argv);
+  if (!lsn)
     return 0;
 
-  switch (params->mode) {
+  switch (lsn->mode) {
   case LSN_SIMPLE_CLIENT: callback = simple_client_listener_cb; break;
   case LSN_SIMPLE_SERVER: callback = simple_server_listener_cb; break;
   case LSN_SOCKS_CLIENT:  callback = socks_client_listener_cb;  break;
   default: obfs_abort();
   }
-  lsn = xzalloc(sizeof(listener_t));
-  lsn->address = printable_address(params->listen_addr->ai_addr,
-                                   params->listen_addr->ai_addrlen);
-  lsn->proto_params = params;
+
+  lsn->listen_addr_str = printable_address(lsn->listen_addr->ai_addr,
+                                           lsn->listen_addr->ai_addrlen);
   lsn->listener =
     evconnlistener_new_bind(base, callback, lsn, flags, -1,
-                            params->listen_addr->ai_addr,
-                            params->listen_addr->ai_addrlen);
+                            lsn->listen_addr->ai_addr,
+                            lsn->listen_addr->ai_addrlen);
 
   if (!lsn->listener) {
-    log_warn("Failed to create listener!");
-    proto_params_free(params);
-    free(lsn);
+    log_warn("Could not begin listening on %s: %s",
+             lsn->listen_addr_str,
+             evutil_socket_error_to_string(EVUTIL_SOCKET_ERROR()));
+    listener_free(lsn);
     return 0;
   }
 
   log_debug("Now listening on %s in mode %d, protocol %s.",
-            lsn->address, params->mode, params->vtable->name);
+            lsn->listen_addr_str, lsn->mode, lsn->vtable->name);
 
   /* If we don't have a listener list, create one now. */
   if (!listeners)
@@ -179,13 +180,16 @@ create_listener(struct event_base *base, int argc, const char *const *argv)
 static void
 listener_free(listener_t *lsn)
 {
-  if (lsn->address)
-    free(lsn->address);
   if (lsn->listener)
     evconnlistener_free(lsn->listener);
-  if (lsn->proto_params)
-    proto_params_free(lsn->proto_params);
-  free(lsn);
+  if (lsn->listen_addr)
+    evutil_freeaddrinfo(lsn->listen_addr);
+  if (lsn->listen_addr_str)
+    free(lsn->listen_addr_str);
+  if (lsn->target_addr)
+    evutil_freeaddrinfo(lsn->target_addr);
+
+  proto_listener_free(lsn);
 }
 
 /**
@@ -219,12 +223,12 @@ simple_client_listener_cb(struct evconnlistener *evcl,
 
   conn->peername = printable_address(sourceaddr, socklen);
   log_debug("%s: connection to %s from %s", __func__,
-            lsn->address, conn->peername);
+            lsn->listen_addr_str, conn->peername);
 
-  conn->mode = lsn->proto_params->mode;
+  conn->mode = lsn->mode;
   obfs_assert(conn->mode == LSN_SIMPLE_CLIENT);
 
-  conn->proto = proto_create(lsn->proto_params);
+  conn->proto = proto_create(lsn);
   if (!conn->proto) {
     log_warn("Creation of protocol object failed! Closing connection.");
     goto err;
@@ -253,8 +257,8 @@ simple_client_listener_cb(struct evconnlistener *evcl,
 
   /* Launch the connect attempt. */
   if (bufferevent_socket_connect(conn->downstream,
-                                 lsn->proto_params->target_addr->ai_addr,
-                                 lsn->proto_params->target_addr->ai_addrlen)<0)
+                                 lsn->target_addr->ai_addr,
+                                 lsn->target_addr->ai_addrlen)<0)
     goto err;
 
   bufferevent_enable(conn->downstream, EV_READ|EV_WRITE);
@@ -290,12 +294,12 @@ socks_client_listener_cb(struct evconnlistener *evcl,
 
   conn->peername = printable_address(sourceaddr, socklen);
   log_debug("%s: connection to %s from %s", __func__,
-            lsn->address, conn->peername);
+            lsn->listen_addr_str, conn->peername);
 
-  conn->mode = lsn->proto_params->mode;
+  conn->mode = lsn->mode;
   obfs_assert(conn->mode == LSN_SOCKS_CLIENT);
 
-  conn->proto = proto_create(lsn->proto_params);
+  conn->proto = proto_create(lsn);
   if (!conn->proto) {
     log_warn("Creation of protocol object failed! Closing connection.");
     goto err;
@@ -348,12 +352,12 @@ simple_server_listener_cb(struct evconnlistener *evcl,
 
   conn->peername = printable_address(sourceaddr, socklen);
   log_debug("%s: connection to %s from %s", __func__,
-            lsn->address, conn->peername);
+            lsn->listen_addr_str, conn->peername);
 
-  conn->mode = lsn->proto_params->mode;
+  conn->mode = lsn->mode;
   obfs_assert(conn->mode == LSN_SIMPLE_SERVER);
 
-  conn->proto = proto_create(lsn->proto_params);
+  conn->proto = proto_create(lsn);
   if (!conn->proto) {
     log_warn("Creation of protocol object failed! Closing connection.");
     goto err;
@@ -382,8 +386,8 @@ simple_server_listener_cb(struct evconnlistener *evcl,
 
   /* Launch the connect attempt. */
   if (bufferevent_socket_connect(conn->upstream,
-                                 lsn->proto_params->target_addr->ai_addr,
-                                 lsn->proto_params->target_addr->ai_addrlen)<0)
+                                 lsn->target_addr->ai_addr,
+                                 lsn->target_addr->ai_addrlen) < 0)
     goto err;
 
   bufferevent_enable(conn->upstream, EV_READ|EV_WRITE);
diff --git a/src/network.h b/src/network.h
index 54684a6..dc099f6 100644
--- a/src/network.h
+++ b/src/network.h
@@ -11,13 +11,25 @@ void free_all_listeners(void);
 
 void start_shutdown(int barbaric);
 
-#ifdef NETWORK_PRIVATE
+/**
+  This struct defines the state of a listener on a particular address.
+  Each protocol may extend this structure with additional private data
+  by embedding it as the first member of a larger structure (standard
+  fake-inheritance-in-C technique).  The protocol's listener_create()
+  method is responsible for filling in the |vtable|, |listen_addr|,
+  |target_addr|, and |mode| fields of this structure, but should leave
+  the |listener| and |listen_addr_str| fields alone.
+ */
+struct listener_t {
+  const protocol_vtable  *vtable;
+  struct evconnlistener  *listener;
+  struct evutil_addrinfo *listen_addr;
+  char                   *listen_addr_str;
+  struct evutil_addrinfo *target_addr;
+  enum listen_mode        mode;
+};
 
-typedef struct listener_t {
-  char *address;
-  protocol_params_t *proto_params;
-  struct evconnlistener *listener;
-} listener_t;
+#ifdef NETWORK_PRIVATE
 
 typedef struct conn_t {
   char *peername;
diff --git a/src/protocol.c b/src/protocol.c
index 8366d58..7865aac 100644
--- a/src/protocol.c
+++ b/src/protocol.c
@@ -3,10 +3,11 @@
 */
 
 #include "util.h"
-
+#include "network.h"
 #include "protocol.h"
-/*#include "protocols/obfs2.h"*/
+
 #include "protocols/dummy.h"
+/*#include "protocols/obfs2.h"*/
 
 /**
     All supported protocols should be put in this array.
@@ -21,46 +22,32 @@ const size_t n_supported_protocols =
   sizeof(supported_protocols)/sizeof(supported_protocols[0]);
 
 /**
-   This function figures out which protocol we want to set up, and
-   gives 'n_options', 'options' and 'params' to the appropriate
-   protocol-specific initalization function.
-   This function is called once for every listener through the runtime
-   of obfsproxy.
-*/
-protocol_params_t *
-proto_params_init(int n_options, const char *const *options)
+   This function dispatches (by name) creation of a |listener_t|
+   to the appropriate protocol-specific initalization function.
+ */
+listener_t *
+proto_listener_create(int n_options, const char *const *options)
 {
   size_t i;
   for (i = 0; i < n_supported_protocols; i++)
     if (!strcmp(*options, supported_protocols[i]->name))
       /* Remove the first element of 'options' (which is always the
          protocol name) from the list passed to the init method. */
-      return supported_protocols[i]->init(n_options - 1, options + 1);
+      return supported_protocols[i]->listener_create(n_options - 1, options + 1);
 
   return NULL;
 }
 
 /**
-   This function destroys 'params'.
-   It's called everytime we free a listener.
+   This function destroys the protocol-specific part of a listener object.
 */
 void
-proto_params_free(protocol_params_t *params)
+proto_listener_free(listener_t *lsn)
 {
-  obfs_assert(params);
-  obfs_assert(params->vtable);
-  obfs_assert(params->vtable->fini);
-
-  if (params->target_addr) {
-    evutil_freeaddrinfo(params->target_addr);
-    params->target_addr = NULL;
-  }
-  if (params->listen_addr) {
-    evutil_freeaddrinfo(params->listen_addr);
-    params->listen_addr = NULL;
-  }
-
-  params->vtable->fini(params);
+  obfs_assert(lsn);
+  obfs_assert(lsn->vtable);
+  obfs_assert(lsn->vtable->listener_free);
+  lsn->vtable->listener_free(lsn);
 }
 
 /**
@@ -70,12 +57,12 @@ proto_params_free(protocol_params_t *params)
    Return a 'protocol_t' if successful, NULL otherwise.
 */
 protocol_t *
-proto_create(protocol_params_t *params)
+proto_create(listener_t *lsn)
 {
-  obfs_assert(params);
-  obfs_assert(params->vtable);
-  obfs_assert(params->vtable->create);
-  return params->vtable->create(params);
+  obfs_assert(lsn);
+  obfs_assert(lsn->vtable);
+  obfs_assert(lsn->vtable->create);
+  return lsn->vtable->create(lsn);
 }
 
 /**
diff --git a/src/protocol.h b/src/protocol.h
index 246ad1c..0f4f32d 100644
--- a/src/protocol.h
+++ b/src/protocol.h
@@ -6,20 +6,6 @@
 #define PROTOCOL_H
 
 /**
-  This struct defines the protocol-specific state for all connections
-  opened from a particular listener.  Each protocol may extend this
-  structure with additional private data by embedding it as the first
-  member of a larger structure (standard fake-inheritance-in-C
-  technique).
- */
-struct protocol_params_t {
-  const protocol_vtable *vtable;
-  struct evutil_addrinfo *target_addr;
-  struct evutil_addrinfo *listen_addr;
-  int mode;
-};
-
-/**
    This struct defines the protocol-specific state for a particular
    connection.  Again, each protocol may extend this structure with
    additional private data by embedding it as the first member of a
@@ -42,20 +28,20 @@ struct protocol_vtable
   /** The short name of this protocol. */
   const char *name;
 
-  /** Initialization: Allocate a 'protocol_params_t' object and fill
-      it in from the provided 'options' array. */
-  protocol_params_t *(*init)(int n_options, const char *const *options);
+  /** Allocate a 'listener_t' object and fill it in from the provided
+      'options' array. */
+  listener_t *(*listener_create)(int n_options, const char *const *options);
 
-  /** Finalization: Destroy the provided 'protocol_params_t' object.
-      This function is responsible for deallocating any data that the
-      protocol's extended structure points to, and deallocating the
-      object itself.  But it is *not* responsible for deallocating the
-      data pointed to by the generic 'protocol_params_t'; that's
-      already been done.  */
-  void (*fini)(protocol_params_t *params);
+  /** Destroy the provided 'listener_t' object.  This function is
+      responsible for deallocating any data that the protocol's
+      extended structure points to, and deallocating the object
+      itself.  But it is *not* responsible for deallocating the data
+      pointed to by the generic 'listener_t'; that's already been done
+      by generic code.  */
+  void (*listener_free)(listener_t *params);
 
   /** Constructor: Allocates per-connection, protocol-specific state. */
-  protocol_t *(*create)(protocol_params_t *params);
+  protocol_t *(*create)(listener_t *params);
 
   /** Destructor: Destroys per-connection, protocol-specific state.  */
   void (*destroy)(protocol_t *state);
@@ -83,16 +69,16 @@ struct protocol_vtable
 #define DEFINE_PROTOCOL_VTABLE(name)            \
   const protocol_vtable name##_vtable = {       \
     #name,                                      \
-    name##_init, name##_fini,                   \
+    name##_listener_create,                     \
+    name##_listener_free,                       \
     name##_create, name##_destroy,              \
     name##_handshake, name##_send, name##_recv  \
   }
 
-protocol_params_t *proto_params_init(int n_options,
-                                     const char *const *options);
-void proto_params_free(protocol_params_t *params);
+listener_t *proto_listener_create(int n_options, const char *const *options);
+void proto_listener_free(listener_t *params);
 
-protocol_t *proto_create(protocol_params_t *params);
+protocol_t *proto_create(listener_t *params);
 void proto_destroy(protocol_t *proto);
 
 int proto_handshake(protocol_t *proto, void *buf);
diff --git a/src/protocols/dummy.c b/src/protocols/dummy.c
index 8ecce8f..e78398f 100644
--- a/src/protocols/dummy.c
+++ b/src/protocols/dummy.c
@@ -10,10 +10,10 @@
 #include <event2/buffer.h>
 
 /* type-safe downcast wrappers */
-static inline dummy_params_t *
-downcast_params(protocol_params_t *p)
+static inline dummy_listener_t *
+downcast_listener(listener_t *p)
 {
-  return DOWNCAST(dummy_params_t, super, p);
+  return DOWNCAST(dummy_listener_t, super, p);
 }
 
 static inline dummy_protocol_t *
@@ -24,25 +24,30 @@ downcast_protocol(protocol_t *p)
 
 static int parse_and_set_options(int n_options,
                                  const char *const *options,
-                                 dummy_params_t *params);
+                                 dummy_listener_t *lsn);
 
 /**
-   This function populates 'params' according to 'options' and sets up
+   This function populates 'lsn' according to 'options' and sets up
    the protocol vtable.
 
    'options' is an array like this:
    {"dummy","socks","127.0.0.1:6666"}
 */
-static protocol_params_t *
-dummy_init(int n_options, const char *const *options)
+static listener_t *
+dummy_listener_create(int n_options, const char *const *options)
 {
-  dummy_params_t *params = xzalloc(sizeof(dummy_params_t));
-  params->super.vtable = &dummy_vtable;
+  dummy_listener_t *lsn = xzalloc(sizeof(dummy_listener_t));
+  lsn->super.vtable = &dummy_vtable;
 
-  if (parse_and_set_options(n_options, options, params) == 0)
-    return &params->super;
+  if (parse_and_set_options(n_options, options, lsn) == 0)
+    return &lsn->super;
+
+  if (lsn->super.listen_addr)
+    evutil_freeaddrinfo(lsn->super.listen_addr);
+  if (lsn->super.target_addr)
+    evutil_freeaddrinfo(lsn->super.target_addr);
+  free(lsn);
 
-  proto_params_free(&params->super);
   log_warn("dummy syntax:\n"
            "\tdummy <mode> <listen_address> [<target_address>]\n"
            "\t\tmode ~ server|client|socks\n"
@@ -57,11 +62,11 @@ dummy_init(int n_options, const char *const *options)
 }
 
 /**
-   Helper: Parses 'options' and fills 'params'.
+   Helper: Parses 'options' and fills 'lsn'.
 */
 static int
 parse_and_set_options(int n_options, const char *const *options,
-                      dummy_params_t *params)
+                      dummy_listener_t *lsn)
 {
   const char* defport;
 
@@ -70,26 +75,26 @@ parse_and_set_options(int n_options, const char *const *options,
 
   if (!strcmp(options[0], "client")) {
     defport = "48988"; /* bf5c */
-    params->super.mode = LSN_SIMPLE_CLIENT;
+    lsn->super.mode = LSN_SIMPLE_CLIENT;
   } else if (!strcmp(options[0], "socks")) {
     defport = "23548"; /* 5bf5 */
-    params->super.mode = LSN_SOCKS_CLIENT;
+    lsn->super.mode = LSN_SOCKS_CLIENT;
   } else if (!strcmp(options[0], "server")) {
     defport = "11253"; /* 2bf5 */
-    params->super.mode = LSN_SIMPLE_SERVER;
+    lsn->super.mode = LSN_SIMPLE_SERVER;
   } else
     return -1;
 
-  if (n_options != (params->super.mode == LSN_SOCKS_CLIENT ? 2 : 3))
+  if (n_options != (lsn->super.mode == LSN_SOCKS_CLIENT ? 2 : 3))
       return -1;
 
-  params->super.listen_addr = resolve_address_port(options[1], 1, 1, defport);
-  if (!params->super.listen_addr)
+  lsn->super.listen_addr = resolve_address_port(options[1], 1, 1, defport);
+  if (!lsn->super.listen_addr)
     return -1;
 
-  if (params->super.mode != LSN_SOCKS_CLIENT) {
-    params->super.target_addr = resolve_address_port(options[2], 1, 0, NULL);
-    if (!params->super.target_addr)
+  if (lsn->super.mode != LSN_SOCKS_CLIENT) {
+    lsn->super.target_addr = resolve_address_port(options[2], 1, 0, NULL);
+    if (!lsn->super.target_addr)
       return -1;
   }
 
@@ -97,9 +102,9 @@ parse_and_set_options(int n_options, const char *const *options,
 }
 
 static void
-dummy_fini(protocol_params_t *params)
+dummy_listener_free(listener_t *lsn)
 {
-  free(downcast_params(params));
+  free(downcast_listener(lsn));
 }
 
 /*
@@ -108,7 +113,7 @@ dummy_fini(protocol_params_t *params)
 */
 
 static protocol_t *
-dummy_create(protocol_params_t *params)
+dummy_create(listener_t *lsn)
 {
   dummy_protocol_t *proto = xzalloc(sizeof(dummy_protocol_t));
   proto->super.vtable = &dummy_vtable;
diff --git a/src/protocols/dummy.h b/src/protocols/dummy.h
index 308c4f0..b4e9c84 100644
--- a/src/protocols/dummy.h
+++ b/src/protocols/dummy.h
@@ -14,6 +14,7 @@ extern const protocol_vtable dummy_vtable;
    ==========
 */
 
+#include "../network.h"
 #include "../protocol.h"
 
 /* Dummy presently needs no extensions to the generic protocol
@@ -21,9 +22,9 @@ extern const protocol_vtable dummy_vtable;
    because, if you're using dummy as a template, you probably will
    want to extend the generic structures. */
 
-typedef struct dummy_params_t {
-  protocol_params_t super;
-} dummy_params_t;
+typedef struct dummy_listener_t {
+  listener_t super;
+} dummy_listener_t;
 
 typedef struct dummy_protocol_t {
   protocol_t super;
diff --git a/src/test/unittest_dummy.c b/src/test/unittest_dummy.c
index 6470528..2a55b9d 100644
--- a/src/test/unittest_dummy.c
+++ b/src/test/unittest_dummy.c
@@ -12,7 +12,7 @@ static void
 test_dummy_option_parsing(void *unused)
 {
   struct option_parsing_case {
-    protocol_params_t *result;
+    listener_t *result;
     short should_succeed;
     short n_opts;
     const char *const opts[4];
@@ -45,7 +45,7 @@ test_dummy_option_parsing(void *unused)
 
   struct option_parsing_case *c;
   for (c = cases; c->n_opts; c++) {
-    c->result = proto_params_init(c->n_opts, c->opts);
+    c->result = proto_listener_create(c->n_opts, c->opts);
     if (c->should_succeed)
       tt_ptr_op(c->result, !=, NULL);
     else
@@ -55,7 +55,7 @@ test_dummy_option_parsing(void *unused)
  end:
   for (c = cases; c->n_opts; c++)
     if (c->result)
-      proto_params_free(c->result);
+      proto_listener_free(c->result);
 
   /* Unsuspend logging */
   log_set_method(LOG_METHOD_STDERR, NULL);
@@ -64,8 +64,8 @@ test_dummy_option_parsing(void *unused)
 /* All the tests below use this test environment: */
 struct test_dummy_state
 {
-  protocol_params_t *proto_params_client;
-  protocol_params_t *proto_params_server;
+  listener_t *proto_lsn_client;
+  listener_t *proto_lsn_server;
   protocol_t *client_proto;
   protocol_t *server_proto;
   struct evbuffer *output_buffer;
@@ -82,10 +82,10 @@ cleanup_dummy_state(const struct testcase_t *unused, void *state)
   if (s->server_proto)
       proto_destroy(s->server_proto);
 
-  if (s->proto_params_client)
-    proto_params_free(s->proto_params_client);
-  if (s->proto_params_server)
-    proto_params_free(s->proto_params_server);
+  if (s->proto_lsn_client)
+    proto_listener_free(s->proto_lsn_client);
+  if (s->proto_lsn_server)
+    proto_listener_free(s->proto_lsn_server);
 
   if (s->output_buffer)
     evbuffer_free(s->output_buffer);
@@ -109,18 +109,18 @@ setup_dummy_state(const struct testcase_t *unused)
 {
   struct test_dummy_state *s = xzalloc(sizeof(struct test_dummy_state));
 
-  s->proto_params_client =
-    proto_params_init(ALEN(options_client), options_client);
-  tt_assert(s->proto_params_client);
+  s->proto_lsn_client =
+    proto_listener_create(ALEN(options_client), options_client);
+  tt_assert(s->proto_lsn_client);
 
-  s->proto_params_server =
-    proto_params_init(ALEN(options_server), options_server);
-  tt_assert(s->proto_params_server);
+  s->proto_lsn_server =
+    proto_listener_create(ALEN(options_server), options_server);
+  tt_assert(s->proto_lsn_server);
 
-  s->client_proto = proto_create(s->proto_params_client);
+  s->client_proto = proto_create(s->proto_lsn_client);
   tt_assert(s->client_proto);
 
-  s->server_proto = proto_create(s->proto_params_server);
+  s->server_proto = proto_create(s->proto_lsn_server);
   tt_assert(s->server_proto);
 
   s->output_buffer = evbuffer_new();
diff --git a/src/util.h b/src/util.h
index d2bbea9..d8c6bb2 100644
--- a/src/util.h
+++ b/src/util.h
@@ -21,8 +21,8 @@
 #endif
 
 struct bufferevent;
-struct evconnlistener;
 struct evbuffer;
+struct evconnlistener;
 struct evdns_base;
 struct event_base;
 
@@ -62,8 +62,8 @@ unsigned int ui64_log2(uint64_t u64);
 
 /***** Network types and functions. *****/
 
+typedef struct listener_t listener_t;
 typedef struct protocol_t protocol_t;
-typedef struct protocol_params_t protocol_params_t;
 typedef struct protocol_vtable protocol_vtable;
 typedef struct socks_state_t socks_state_t;
 





More information about the tor-commits mailing list