[tor-commits] [obfsproxy/master] Set peername in open_outbound_hostname().

nickm at torproject.org nickm at torproject.org
Mon Jan 16 20:35:53 UTC 2012


commit 64af18df7dc39915be9fb9a5f22ad1d8ab76d4ca
Author: George Kadianakis <desnacked at riseup.net>
Date:   Mon Jan 16 19:20:17 2012 +0200

    Set peername in open_outbound_hostname().
---
 src/network.c |   38 ++++++++++++++++++++++++++++++++++++--
 1 files changed, 36 insertions(+), 2 deletions(-)

diff --git a/src/network.c b/src/network.c
index 86d46e7..bb9ce83 100644
--- a/src/network.c
+++ b/src/network.c
@@ -563,6 +563,7 @@ open_outbound_hostname(conn_t *conn, int af, const char *addr, int port)
   struct event_base *base = bufferevent_get_base(conn->buffer);
   struct bufferevent *buf;
   conn_t *newconn;
+  obfs_assert((port >= 0) && (port <= 65535));
 
   buf = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE);
   if (!buf) {
@@ -576,7 +577,29 @@ open_outbound_hostname(conn_t *conn, int af, const char *addr, int port)
     bufferevent_free(buf);
     return NULL;
   }
+
+  /* associate bufferevent with the new connection. */
   newconn->buffer = buf;
+
+  { /* set up temporary FQDN peername. When we connect to the host, we
+        will replace this peername with the IP address we connected
+        to. */
+
+    /* '6' is strlen(":65535")
+       '1' is the ending NUL. */
+    const size_t peername_len = strlen(addr) + 6 + 1;
+    newconn->peername = xzalloc(peername_len);
+    if (obfs_snprintf(newconn->peername, peername_len, "%s:%d",
+                      addr, port) < 0) {
+      log_warn("Failed while writing peername (%s:%u)! This might be a bug.",
+               addr, port);
+      free(newconn->peername);
+      newconn->peername = NULL;
+      conn_free(newconn);
+      return NULL;
+    }
+  }
+
   bufferevent_setcb(buf, downstream_read_cb, NULL, pending_socks_cb, newconn);
   if (bufferevent_socket_connect_hostname(buf, get_evdns_base(),
                                           af, addr, port) < 0) {
@@ -923,9 +946,20 @@ pending_socks_cb(struct bufferevent *bev, short what, void *arg)
 
     /* Figure out where we actually connected to, and tell the socks client */
     if (getpeername(bufferevent_getfd(bev), sa, &slen) == 0) {
+      char *peername = printable_address(sa, slen);
       socks_state_set_address(socks, sa);
-      if (!down->peername)
-        down->peername = printable_address(sa, slen);
+
+      /* We have to update our connection's peername. If it was an
+         FQDN SOCKS request, the current peername contains the FQDN
+         string, and we want to replace it with the actual IP address
+         we connected to. */
+      if (down->peername) {
+        log_debug("We connected to our SOCKS destination! "
+                  "Replacing peername '%s' with '%s'",
+                  down->peername, peername);
+        free(down->peername);
+      }
+      down->peername = peername;
     }
     socks_send_reply(socks, bufferevent_get_output(up->buffer), 0);
 





More information about the tor-commits mailing list