[tor-commits] [tor/master] Pass OR address to PT proxy, even with IPv6 or ORListenAddress.

nickm at torproject.org nickm at torproject.org
Fri May 11 15:57:21 UTC 2012


commit 10232dc0423792a8fb61b9ec136d7ffd5484216b
Author: George Kadianakis <desnacked at riseup.net>
Date:   Sat Mar 31 14:04:58 2012 +0200

    Pass OR address to PT proxy, even with IPv6 or ORListenAddress.
    
    Introduce get_first_listener_addrport_for_pt() which returns a string
    containing the addrport of the first listener we could find. Use it to
    form the TOR_PT_ORPORT managed proxy protocol line.
---
 src/or/config.c     |   53 +++++++++++++++++++++++++++++++++++++++++++++++++++
 src/or/config.h     |    2 +
 src/or/transports.c |    9 ++++---
 3 files changed, 60 insertions(+), 4 deletions(-)

diff --git a/src/or/config.c b/src/or/config.c
index 2a8c540..5ea1f5e 100644
--- a/src/or/config.c
+++ b/src/or/config.c
@@ -6026,6 +6026,59 @@ get_configured_ports(void)
   return configured_ports;
 }
 
+/** Return an <address>:<port> string representation of the address
+ *  where the first <b>listener_type</b> listener waits for
+ *  connections. Return NULL if we couldn't find a listener. The
+ *  string is allocated on the heap and it's the responsibility of the
+ *  caller to free it after use.
+ *
+ *  This function is meant to be used by the pluggable transport proxy
+ *  spawning code. */
+char *
+get_first_listener_addrport_for_pt(int listener_type)
+{
+  static const char *ipv4_localhost = "127.0.0.1";
+  static const char *ipv6_localhost = "[::1]";
+  const char *address;
+  uint16_t port;
+  char *string = NULL;
+
+  if (!configured_ports)
+    return NULL;
+
+  SMARTLIST_FOREACH_BEGIN(configured_ports, const port_cfg_t *, cfg) {
+
+    if (cfg->type == listener_type &&
+        tor_addr_family(&cfg->addr) != AF_UNSPEC) {
+
+      /* We found the first listener of the type we are interested in! */
+
+      /* If a listener is listening on INADDR_ANY, assume that it's
+         also listening on 127.0.0.1, and point the transport proxy
+         there: */
+      if (tor_addr_is_null(&cfg->addr))
+        address = tor_addr_is_v4(&cfg->addr) ? ipv4_localhost : ipv6_localhost;
+      else
+        address = fmt_and_decorate_addr(&cfg->addr);
+
+      /* If a listener is configured with port 'auto', we are forced
+         to iterate all listener connections and find out in which
+         port it ended up listening: */
+      if (cfg->port == CFG_AUTO_PORT)
+        port = router_get_active_listener_port_by_type(listener_type);
+      else
+        port = cfg->port;
+
+      tor_asprintf(&string, "%s:%u", address, port);
+
+      return string;
+    }
+
+  } SMARTLIST_FOREACH_END(cfg);
+
+  return NULL;
+}
+
 /** Return the first advertised port of type <b>listener_type</b> in
     <b>address_family</b>.  */
 int
diff --git a/src/or/config.h b/src/or/config.h
index 0f7c618..fd84d9b 100644
--- a/src/or/config.h
+++ b/src/or/config.h
@@ -72,6 +72,8 @@ int get_first_advertised_port_by_type_af(int listener_type,
 #define get_primary_dir_port() \
   (get_first_advertised_port_by_type_af(CONN_TYPE_DIR_LISTENER, AF_INET))
 
+char *get_first_listener_addrport_for_pt(int listener_type);
+
 int options_need_geoip_info(const or_options_t *options,
                             const char **reason_out);
 
diff --git a/src/or/transports.c b/src/or/transports.c
index 564603e..1f21493 100644
--- a/src/or/transports.c
+++ b/src/or/transports.c
@@ -957,8 +957,6 @@ get_bindaddr_for_server_proxy(const managed_proxy_t *mp)
 static process_environment_t *
 create_managed_proxy_environment(const managed_proxy_t *mp)
 {
-  const or_options_t *options = get_options();
-
   /* Environment variables to be added to or set in mp's environment. */
   smartlist_t *envs = smartlist_new();
   /* XXXX The next time someone touches this code, shorten the name of
@@ -993,8 +991,11 @@ create_managed_proxy_environment(const managed_proxy_t *mp)
   }
 
   if (mp->is_server) {
-    smartlist_add_asprintf(envs, "TOR_PT_ORPORT=127.0.0.1:%s",
-                           options->ORPort->value);
+    {
+      char *orport_tmp = get_first_listener_addrport_for_pt(CONN_TYPE_OR_LISTENER);
+      smartlist_add_asprintf(envs, "TOR_PT_ORPORT=%s", orport_tmp);
+      tor_free(orport_tmp);
+    }
 
     {
       char *bindaddr_tmp = get_bindaddr_for_server_proxy(mp);





More information about the tor-commits mailing list