[torsocks/master] Add IPv6 support to Tor resolve command

commit 6a0bc8b32f884a4ab12a4b5bf356c418c3072d95 Author: David Goulet <dgoulet@ev0ke.net> Date: Sun Jun 23 14:56:36 2013 -0400 Add IPv6 support to Tor resolve command Signed-off-by: David Goulet <dgoulet@ev0ke.net> --- src/common/socks5.c | 35 +++++++++++++++++++++++++++++------ src/common/socks5.h | 3 ++- src/lib/torsocks.c | 3 ++- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/common/socks5.c b/src/common/socks5.c index d007599..fcb30d7 100644 --- a/src/common/socks5.c +++ b/src/common/socks5.c @@ -449,20 +449,25 @@ error: * * Return 0 on success else a negative value. */ -int socks5_recv_resolve_reply(struct connection *conn, uint32_t *ip_addr) +int socks5_recv_resolve_reply(struct connection *conn, void *addr, + size_t addrlen) { int ret; + size_t recv_len; ssize_t ret_recv; struct { struct socks5_reply msg; - uint32_t addr; + union { + uint8_t ipv4[4]; + uint8_t ipv6[16]; + } addr; } buffer; assert(conn); assert(conn >= 0); - assert(ip_addr); + assert(addr); - ret_recv = recv_data(conn->fd, &buffer, sizeof(buffer)); + ret_recv = recv_data(conn->fd, &buffer, sizeof(buffer.msg)); if (ret_recv < 0) { ret = ret_recv; goto error; @@ -481,16 +486,34 @@ int socks5_recv_resolve_reply(struct connection *conn, uint32_t *ip_addr) } if (buffer.msg.atyp == SOCKS5_ATYP_IPV4) { - *ip_addr = buffer.addr; + /* Size of a binary IPv4 in bytes. */ + recv_len = sizeof(buffer.addr.ipv4); + } else if (buffer.msg.atyp == SOCKS5_ATYP_IPV6) { + /* Size of a binary IPv6 in bytes. */ + recv_len = sizeof(buffer.addr.ipv6); } else { ERR("Bad SOCKS5 atyp reply %d", buffer.msg.atyp); ret = -EINVAL; goto error; } + ret_recv = recv_data(conn->fd, &buffer.addr, recv_len); + if (ret_recv < 0) { + ret = ret_recv; + goto error; + } + + if (addrlen < recv_len) { + ERR("[socks5] Resolve destination buffer too small"); + ret = -EINVAL; + goto error; + } + + memcpy(addr, &buffer.addr, recv_len); + /* Everything went well and ip_addr has been populated. */ ret = 0; - DBG("[socks5] Resolve reply received: %" PRIu32, *ip_addr); + DBG("[socks5] Resolve reply received successfully"); error: return ret; diff --git a/src/common/socks5.h b/src/common/socks5.h index 9b47aa1..2699357 100644 --- a/src/common/socks5.h +++ b/src/common/socks5.h @@ -130,7 +130,8 @@ int socks5_recv_connect_reply(struct connection *conn); /* Tor DNS resolve. */ int socks5_send_resolve_request(const char *hostname, struct connection *conn); -int socks5_recv_resolve_reply(struct connection *conn, uint32_t *ip_addr); +int socks5_recv_resolve_reply(struct connection *conn, void *addr, + size_t addrlent); int socks5_recv_resolve_ptr_reply(struct connection *conn, char **_hostname); int socks5_send_resolve_ptr_request(const void *ip, struct connection *conn); diff --git a/src/lib/torsocks.c b/src/lib/torsocks.c index 28cd183..6b657ef 100644 --- a/src/lib/torsocks.c +++ b/src/lib/torsocks.c @@ -289,7 +289,8 @@ int tsocks_tor_resolve(const char *hostname, uint32_t *ip_addr) goto error; } - ret = socks5_recv_resolve_reply(&conn, ip_addr); + /* Force IPv4 resolution for now. */ + ret = socks5_recv_resolve_reply(&conn, ip_addr, sizeof(uint32_t)); if (ret < 0) { goto error; }
participants (1)
-
dgoulet@torproject.org