[tor-commits] [tor/master] This commit is an attempt to beautify the previous commit.

nickm at torproject.org nickm at torproject.org
Mon Jul 11 20:01:36 UTC 2011


commit 5b050a9b08fa81421d7e923ef04d06b569f5f742
Author: George Kadianakis <desnacked at gmail.com>
Date:   Tue Jun 14 04:28:36 2011 +0200

    This commit is an attempt to beautify the previous commit.
    
    It creates some helper functions that return the proxy type, proxy addr/port, etc.
---
 src/or/connection.c    |   98 +++++++++++++++++++++++++++++++++++------------
 src/or/connection.h    |    3 +
 src/or/connection_or.c |   34 ++++++-----------
 3 files changed, 88 insertions(+), 47 deletions(-)

diff --git a/src/or/connection.c b/src/or/connection.c
index 087ee29..f7ad84a 100644
--- a/src/or/connection.c
+++ b/src/or/connection.c
@@ -4101,37 +4101,82 @@ assert_connection_ok(connection_t *conn, time_t now)
   }
 }
 
-void
-log_failed_proxy_connection(connection_t *conn)
+/**
+   Fills <b>addr</b> and <b>port</b> with the details of the proxy
+   server of type 'proxy_type' we are using.
+   'conn' contains a connection_t and is used for finding pluggable
+   transports proxies.
+   
+   Returns 1 if we were successfull, 0 if we are not using a proxy
+   server and -1 if something went wrong.
+*/
+int
+get_proxy_addrport(int proxy_type, tor_addr_t *addr, uint16_t *port, 
+                   connection_t *conn)
 {
-  or_options_t *options = get_options();
-  int proxy_type;
-  tor_addr_t proxy_addr;
-  int proxy_port;
-
-  if (options->HTTPSProxy) {
-    tor_addr_copy(&proxy_addr, &options->HTTPSProxyAddr);
-    proxy_port = options->HTTPSProxyPort;
-    proxy_type = PROXY_CONNECT;
-  } else if (options->Socks4Proxy) {
-    tor_addr_copy(&proxy_addr, &options->Socks4ProxyAddr);
-    proxy_port = options->Socks4ProxyPort;
-    proxy_type = PROXY_SOCKS4;
-  } else if (options->Socks5Proxy) {
-    tor_addr_copy(&proxy_addr, &options->Socks5ProxyAddr);
-    proxy_port = options->Socks5ProxyPort;
-    proxy_type = PROXY_SOCKS5;
-  } else if (options->ClientTransportPlugin) {
+  or_options_t *options;
+
+  if (proxy_type == PROXY_NONE)
+    return 0;
+
+  options = get_options();
+
+  if (proxy_type == PROXY_CONNECT) {
+    tor_addr_copy(addr, &options->HTTPSProxyAddr);
+    *port = options->HTTPSProxyPort;
+  } else if (proxy_type == PROXY_SOCKS4) {
+    tor_addr_copy(addr, &options->Socks4ProxyAddr);
+    *port = options->Socks4ProxyPort;
+  } else if (proxy_type == PROXY_SOCKS5) {
+    tor_addr_copy(addr, &options->Socks5ProxyAddr);
+    *port = options->Socks5ProxyPort;
+  } else if (proxy_type == PROXY_PLUGGABLE) {
     transport_info_t *transport;
     transport = find_transport_by_bridge_addrport(&conn->addr, conn->port);
     if (transport) {
-      tor_addr_copy(&proxy_addr, &transport->addr);
-      proxy_port = transport->port;
-      proxy_type = PROXY_PLUGGABLE;
+      tor_addr_copy(addr, &transport->addr);
+      *port = transport->port;
     } else
-      return;
+      return -1;
   } else
-    tor_assert(0);
+    return -1;
+
+  return 1;
+}  
+
+/**
+   Returns the proxy type used by tor.
+*/
+int
+get_proxy_type(void)
+{
+  or_options_t *options = get_options();
+
+  if (options->HTTPSProxy)
+    return PROXY_CONNECT;
+  else if (options->Socks4Proxy)
+    return PROXY_SOCKS4;
+  else if (options->Socks5Proxy)
+    return PROXY_SOCKS5;
+  else if (options->ClientTransportPlugin)
+    return PROXY_PLUGGABLE;
+  else
+    return PROXY_NONE;
+}
+  
+/**
+   Log a failed connection to a proxy server.
+*/
+void
+log_failed_proxy_connection(connection_t *conn)
+{
+  int proxy_type;
+  tor_addr_t proxy_addr;
+  uint16_t proxy_port;
+
+  proxy_type = get_proxy_type();
+  if (get_proxy_addrport(proxy_type, &proxy_addr, &proxy_port, conn) <= 0)
+    return;
   
   log_warn(LD_NET,
            "The connection to the %s proxy server at %s:%u just failed. "
@@ -4140,6 +4185,9 @@ log_failed_proxy_connection(connection_t *conn)
            proxy_port);  
 }
 
+/**
+   Transforms 'proxy_type' to it's string representation/
+*/
 static const char *
 proxy_type_to_string(int proxy_type)
 {
diff --git a/src/or/connection.h b/src/or/connection.h
index ba6a258..544e939 100644
--- a/src/or/connection.h
+++ b/src/or/connection.h
@@ -58,6 +58,9 @@ int connection_connect(connection_t *conn, const char *address,
 int connection_proxy_connect(connection_t *conn, int type);
 int connection_read_proxy_handshake(connection_t *conn);
 void log_failed_proxy_connection(connection_t *conn);
+int get_proxy_addrport(int proxy_type, tor_addr_t *addr, uint16_t *port, connection_t *conn);
+int get_proxy_type(void);
+
 
 int retry_all_listeners(smartlist_t *replaced_conns,
                         smartlist_t *new_conns);
diff --git a/src/or/connection_or.c b/src/or/connection_or.c
index b72cd77..f111bc1 100644
--- a/src/or/connection_or.c
+++ b/src/or/connection_or.c
@@ -839,6 +839,11 @@ connection_or_connect(const tor_addr_t *_addr, uint16_t port,
   int socket_error = 0;
   tor_addr_t addr;
 
+  int r;
+  int proxy_type;
+  tor_addr_t proxy_addr;
+  uint16_t proxy_port;
+
   tor_assert(_addr);
   tor_assert(id_digest);
   tor_addr_copy(&addr, _addr);
@@ -855,29 +860,14 @@ connection_or_connect(const tor_addr_t *_addr, uint16_t port,
   conn->_base.state = OR_CONN_STATE_CONNECTING;
   control_event_or_conn_status(conn, OR_CONN_EVENT_LAUNCHED, 0);
 
-  /* use a proxy server if available */
-  if (options->HTTPSProxy) {
-    conn->_base.proxy_state = PROXY_INFANT;
-    tor_addr_copy(&addr, &options->HTTPSProxyAddr);
-    port = options->HTTPSProxyPort;
-  } else if (options->Socks4Proxy) {
-    conn->_base.proxy_state = PROXY_INFANT;
-    tor_addr_copy(&addr, &options->Socks4ProxyAddr);
-    port = options->Socks4ProxyPort;
-  } else if (options->Socks5Proxy) {
+  proxy_type = get_proxy_type();
+  r = get_proxy_addrport(proxy_type, &proxy_addr, &proxy_port, TO_CONN(conn));
+  if (r == 1) { /* proxy found. */
+    addr = proxy_addr;
+    port = proxy_port;
     conn->_base.proxy_state = PROXY_INFANT;
-    tor_addr_copy(&addr, &options->Socks5ProxyAddr);
-    port = options->Socks5ProxyPort;
-  } else if (options->ClientTransportPlugin) {
-    transport_info_t *transport;
-    transport = find_transport_by_bridge_addrport(&addr, port);
-    if (transport) {
-      log_debug(LD_GENERAL, "Found transport. Setting up proxying!");
-      conn->_base.proxy_state = PROXY_INFANT;
-      tor_addr_copy(&addr, &transport->addr);
-      port = transport->port;
-    }
-  }
+  } else if (r < 0)
+    return NULL;
 
   switch (connection_connect(TO_CONN(conn), conn->_base.address,
                              &addr, port, &socket_error)) {





More information about the tor-commits mailing list