[tor-commits] [torsocks/master] Fix: socks5 connect use connection domain and correct len

dgoulet at torproject.org dgoulet at torproject.org
Fri Apr 4 22:40:27 UTC 2014


commit 1e3c81ef2471bf3bfde2701107f9c099c08f1dbf
Author: David Goulet <dgoulet at ev0ke.net>
Date:   Sat Feb 8 15:15:43 2014 -0500

    Fix: socks5 connect use connection domain and correct len
    
    The connection we are trying to do tells us if we need to connect to Tor
    in ipv6 or ipv4 since the socket that we send back needs to be connected
    with the right socket family.
    
    Signed-off-by: David Goulet <dgoulet at ev0ke.net>
---
 src/common/socks5.c |   12 ++++++++++--
 src/lib/torsocks.c  |    2 ++
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/src/common/socks5.c b/src/common/socks5.c
index ede3594..81806c6 100644
--- a/src/common/socks5.c
+++ b/src/common/socks5.c
@@ -123,17 +123,25 @@ ATTR_HIDDEN
 int socks5_connect(struct connection *conn)
 {
 	int ret;
+	socklen_t len;
 	struct sockaddr *socks5_addr = NULL;
 
 	assert(conn);
 	assert(conn->fd >= 0);
 
-	switch (tsocks_config.socks5_addr.domain) {
+	/*
+	 * We use the connection domain here since the connect() call MUST match
+	 * the right socket family. Thus, trying to establish a connection to a
+	 * remote IPv6, we have to connect to the Tor daemon in v6.
+	 */
+	switch (conn->dest_addr.domain) {
 	case CONNECTION_DOMAIN_INET:
 		socks5_addr = (struct sockaddr *) &tsocks_config.socks5_addr.u.sin;
+		len = sizeof(tsocks_config.socks5_addr.u.sin);
 		break;
 	case CONNECTION_DOMAIN_INET6:
 		socks5_addr = (struct sockaddr *) &tsocks_config.socks5_addr.u.sin6;
+		len = sizeof(tsocks_config.socks5_addr.u.sin6);
 		break;
 	default:
 		ERR("Socks5 connect domain unknown %d",
@@ -145,7 +153,7 @@ int socks5_connect(struct connection *conn)
 
 	do {
 		/* Use the original libc connect() to the Tor. */
-		ret = tsocks_libc_connect(conn->fd, socks5_addr, sizeof(*socks5_addr));
+		ret = tsocks_libc_connect(conn->fd, socks5_addr, len);
 	} while (ret < 0 &&
 			(errno == EINTR || errno == EINPROGRESS || errno == EALREADY));
 	if (ret < 0) {
diff --git a/src/lib/torsocks.c b/src/lib/torsocks.c
index fbb091e..8d83bcd 100644
--- a/src/lib/torsocks.c
+++ b/src/lib/torsocks.c
@@ -398,6 +398,7 @@ int tsocks_tor_resolve(const char *hostname, uint32_t *ip_addr)
 		ret = -errno;
 		goto error;
 	}
+	conn.dest_addr.domain = CONNECTION_DOMAIN_INET;
 
 	ret = setup_tor_connection(&conn);
 	if (ret < 0) {
@@ -446,6 +447,7 @@ int tsocks_tor_resolve_ptr(const char *addr, char **ip, int af)
 		ret = -errno;
 		goto error;
 	}
+	conn.dest_addr.domain = CONNECTION_DOMAIN_INET;
 
 	ret = setup_tor_connection(&conn);
 	if (ret < 0) {





More information about the tor-commits mailing list