commit 8495857ce613023e75bd3f29da860952e793cd7f Author: David Goulet dgoulet@ev0ke.net Date: Thu Feb 20 15:08:09 2014 +0000
Fix: make tsock_tor_resolve support IPv6
Note that the tor daemon does not support IPv6 DNS resolution through the SOCKS port thus for now it is denied.
Signed-off-by: David Goulet dgoulet@ev0ke.net --- src/lib/getaddrinfo.c | 2 +- src/lib/gethostbyname.c | 4 ++-- src/lib/torsocks.c | 25 +++++++++++++++++++------ src/lib/torsocks.h | 2 +- 4 files changed, 23 insertions(+), 10 deletions(-)
diff --git a/src/lib/getaddrinfo.c b/src/lib/getaddrinfo.c index da9d4ed..003e51f 100644 --- a/src/lib/getaddrinfo.c +++ b/src/lib/getaddrinfo.c @@ -85,7 +85,7 @@ LIBC_GETADDRINFO_RET_TYPE tsocks_getaddrinfo(LIBC_GETADDRINFO_SIG) ret = inet_pton(af, node, addr); if (ret == 0) { /* The node most probably is a DNS name. */ - ret = tsocks_tor_resolve(node, (uint32_t *) addr); + ret = tsocks_tor_resolve(af, node, addr); if (ret < 0) { ret = EAI_FAIL; goto error; diff --git a/src/lib/gethostbyname.c b/src/lib/gethostbyname.c index 1b9d00e..6b4ea21 100644 --- a/src/lib/gethostbyname.c +++ b/src/lib/gethostbyname.c @@ -72,7 +72,7 @@ LIBC_GETHOSTBYNAME_RET_TYPE tsocks_gethostbyname(LIBC_GETHOSTBYNAME_SIG) }
/* Resolve the given hostname through Tor. */ - ret = tsocks_tor_resolve(name, &ip); + ret = tsocks_tor_resolve(AF_INET, name, &ip); if (ret < 0) { goto error; } @@ -341,7 +341,7 @@ LIBC_GETHOSTBYNAME_R_RET_TYPE tsocks_gethostbyname_r(LIBC_GETHOSTBYNAME_R_SIG) }
/* Resolve the given hostname through Tor. */ - ret = tsocks_tor_resolve(name, &ip); + ret = tsocks_tor_resolve(AF_INET, name, &ip); if (ret < 0) { goto error; } diff --git a/src/lib/torsocks.c b/src/lib/torsocks.c index bb7773c..9b3100c 100644 --- a/src/lib/torsocks.c +++ b/src/lib/torsocks.c @@ -367,16 +367,30 @@ error: * * Return 0 on success else a negative value and the result addr is untouched. */ -int tsocks_tor_resolve(const char *hostname, uint32_t *ip_addr) +int tsocks_tor_resolve(int af, const char *hostname, void *ip_addr) { int ret; + size_t addr_len; struct connection conn;
assert(hostname); assert(ip_addr);
- ret = utils_localhost_resolve(hostname, AF_INET, ip_addr, - sizeof(uint32_t)); + if (af == AF_INET) { + addr_len = sizeof(uint32_t); + conn.dest_addr.domain = CONNECTION_DOMAIN_INET; + } else if (af == AF_INET6) { + addr_len = 16; + conn.dest_addr.domain = CONNECTION_DOMAIN_INET6; + /* Tor daemon does not support IPv6 DNS resolution yet. */ + ret = -ENOSYS; + goto error; + } else { + ret = -EINVAL; + goto error; + } + + ret = utils_localhost_resolve(hostname, af, ip_addr, addr_len); if (ret) { /* Found to be a localhost name. */ ret = 0; @@ -401,13 +415,12 @@ int tsocks_tor_resolve(const char *hostname, uint32_t *ip_addr) } }
- conn.fd = tsocks_libc_socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); + conn.fd = tsocks_libc_socket(af, SOCK_STREAM, IPPROTO_TCP); if (conn.fd < 0) { PERROR("socket"); ret = -errno; goto error; } - conn.dest_addr.domain = CONNECTION_DOMAIN_INET;
ret = setup_tor_connection(&conn); if (ret < 0) { @@ -420,7 +433,7 @@ int tsocks_tor_resolve(const char *hostname, uint32_t *ip_addr) }
/* Force IPv4 resolution for now. */ - ret = socks5_recv_resolve_reply(&conn, ip_addr, sizeof(uint32_t)); + ret = socks5_recv_resolve_reply(&conn, ip_addr, addr_len); if (ret < 0) { goto end_close; } diff --git a/src/lib/torsocks.h b/src/lib/torsocks.h index 01c25b0..1b95cb9 100644 --- a/src/lib/torsocks.h +++ b/src/lib/torsocks.h @@ -383,7 +383,7 @@ extern unsigned int tsocks_cleaned_up; int tsocks_connect_to_tor(struct connection *conn); void *tsocks_find_libc_symbol(const char *symbol, enum tsocks_sym_action action); -int tsocks_tor_resolve(const char *hostname, uint32_t *ip_addr); +int tsocks_tor_resolve(int af, const char *hostname, void *ip_addr); int tsocks_tor_resolve_ptr(const char *addr, char **ip, int af); void tsocks_cleanup(void);
tor-commits@lists.torproject.org