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.)
This commit is contained in:
Nick Mathewson 2014-04-05 14:41:37 -04:00
parent eefa3ebc78
commit b3469e4207
2 changed files with 34 additions and 0 deletions

View File

@ -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);
}

View File

@ -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:
;
}