commit b3469e4207d12821993f1d2d381c5d27918a4c01 Author: Nick Mathewson nickm@torproject.org Date: Sat Apr 5 14:41:37 2014 -0400
Make tor_addr_port_parse handle portless IPv6 addresses correctly.
(Not a bugfix on any Tor release; before 10801_024, it didn't handle portless addresses at all.) --- src/common/address.c | 9 +++++++++ src/test/test_addr.c | 25 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+)
diff --git a/src/common/address.c b/src/common/address.c index 3e89861..8456558 100644 --- a/src/common/address.c +++ b/src/common/address.c @@ -1488,9 +1488,18 @@ int tor_addr_port_split(int severity, const char *addrport, char **address_out, uint16_t *port_out) { + tor_addr_t a_tmp; tor_assert(addrport); tor_assert(address_out); tor_assert(port_out); + /* We need to check for IPv6 manually because addr_port_lookup() doesn't + * do a good job on IPv6 addresses that lack a port. */ + if (tor_addr_parse(&a_tmp, addrport) == AF_INET6) { + *port_out = 0; + *address_out = tor_strdup(addrport); + return 0; + } + return addr_port_lookup(severity, addrport, address_out, NULL, port_out); }
diff --git a/src/test/test_addr.c b/src/test/test_addr.c index 6228fab..c75f8e6 100644 --- a/src/test/test_addr.c +++ b/src/test/test_addr.c @@ -741,6 +741,14 @@ test_addr_parse(void) test_streq(buf, "192.0.2.1"); test_eq(port, 1234);
+ r= tor_addr_port_parse(LOG_DEBUG, + "[::1]:1234", + &addr, &port, -1); + test_assert(r == 0); + tor_addr_to_str(buf, &addr, sizeof(buf), 0); + test_streq(buf, "::1"); + test_eq(port, 1234); + /* Domain name. */ r= tor_addr_port_parse(LOG_DEBUG, "torproject.org:1234", @@ -759,6 +767,17 @@ test_addr_parse(void) test_assert(r == 0); tt_int_op(port,==,200);
+ r= tor_addr_port_parse(LOG_DEBUG, + "[::1]", + &addr, &port, -1); + test_assert(r == -1); + + r= tor_addr_port_parse(LOG_DEBUG, + "[::1]", + &addr, &port, 400); + test_assert(r == 0); + tt_int_op(port,==,400); + /* Bad port. */ r= tor_addr_port_parse(LOG_DEBUG, "192.0.2.2:66666", @@ -793,6 +812,12 @@ test_addr_parse(void) test_assert(r == 0); tt_int_op(port,==,1337);
+ r= tor_addr_port_parse(LOG_DEBUG, + "[::1]:1369", + &addr, &port, 200); + test_assert(r == 0); + tt_int_op(port,==,1369); + done: ; }
tor-commits@lists.torproject.org