aboutsummaryrefslogtreecommitdiff
path: root/src/lib/net/address.c
diff options
context:
space:
mode:
authorteor <teor@torproject.org>2019-06-02 17:46:58 +1000
committerNick Mathewson <nickm@torproject.org>2019-06-26 09:55:36 -0400
commitcd1de99468aa4d7b48c9f3bcb7c7d4fb5b3bfe9a (patch)
treed866135a9c2d3f12aca4d792a03b2797397b251b /src/lib/net/address.c
parent180048e013c06ee67c053186aab46ff94cea0489 (diff)
downloadtor-cd1de99468aa4d7b48c9f3bcb7c7d4fb5b3bfe9a.tar.gz
tor-cd1de99468aa4d7b48c9f3bcb7c7d4fb5b3bfe9a.zip
resolve: consistently parse IP addresses in square brackets
When parsing addreses via Tor's internal DNS lookup API: * reject IPv4 addresses in square brackets (with or without a port), * accept IPv6 addresses in square brackets (with or without a port), and * accept IPv6 addresses without square brackets, as long as they have no port. This change completes the work started in 23082, making address parsing consistent between tor's internal DNS lookup and address parsing APIs. Fixes bug 30721; bugfix on 0.2.1.5-alpha.
Diffstat (limited to 'src/lib/net/address.c')
-rw-r--r--src/lib/net/address.c31
1 files changed, 26 insertions, 5 deletions
diff --git a/src/lib/net/address.c b/src/lib/net/address.c
index 546af800a9..4989e4ab2b 100644
--- a/src/lib/net/address.c
+++ b/src/lib/net/address.c
@@ -373,7 +373,8 @@ tor_addr_to_str(char *dest, const tor_addr_t *addr, size_t len, int decorate)
*
* If <b>accept_regular</b> is set and the address is in neither recognized
* reverse lookup hostname format, try parsing the address as a regular
- * IPv4 or IPv6 address too.
+ * IPv4 or IPv6 address too. This mode will accept IPv6 addresses with or
+ * without square brackets.
*/
int
tor_addr_parse_PTR_name(tor_addr_t *result, const char *address,
@@ -1204,6 +1205,10 @@ tor_addr_parse(tor_addr_t *addr, const char *src)
tor_assert(addr && src);
+ /* Clear the address before starting, to avoid returning uninitialised data.
+ */
+ memset(addr, 0, sizeof(tor_addr_t));
+
size_t len = strlen(src);
if (len && src[0] == '[' && src[len - 1] == ']') {
@@ -1718,6 +1723,11 @@ get_interface_address6_list,(int severity,
* form "ip" or "ip:0". Otherwise, accept those forms, and set
* *<b>port_out</b> to <b>default_port</b>.
*
+ * This function accepts:
+ * - IPv6 address and port, when the IPv6 address is in square brackets,
+ * - IPv6 address with square brackets,
+ * - IPv6 address without square brackets.
+ *
* Return 0 on success, -1 on failure. */
int
tor_addr_port_parse(int severity, const char *addrport,
@@ -1755,9 +1765,17 @@ tor_addr_port_parse(int severity, const char *addrport,
}
/** Given an address of the form "host[:port]", try to divide it into its host
- * and port portions, setting *<b>address_out</b> to a newly allocated string
- * holding the address portion and *<b>port_out</b> to the port (or 0 if no
- * port is given). Return 0 on success, -1 on failure. */
+ * and port portions.
+ *
+ * Like tor_addr_port_parse(), this function accepts:
+ * - IPv6 address and port, when the IPv6 address is in square brackets,
+ * - IPv6 address with square brackets,
+ * - IPv6 address without square brackets.
+ *
+ * Sets *<b>address_out</b> to a newly allocated string holding the address
+ * portion, and *<b>port_out</b> to the port (or 0 if no port is given).
+ *
+ * Return 0 on success, -1 on failure. */
int
tor_addr_port_split(int severity, const char *addrport,
char **address_out, uint16_t *port_out)
@@ -1766,8 +1784,11 @@ tor_addr_port_split(int severity, const char *addrport,
tor_assert(addrport);
tor_assert(address_out);
tor_assert(port_out);
+
/* We need to check for IPv6 manually because the logic below doesn't
- * do a good job on IPv6 addresses that lack a port. */
+ * do a good job on IPv6 addresses that lack a port.
+ * If an IPv6 address without square brackets is ambiguous, it gets parsed
+ * here as an address, rather than address:port. */
if (tor_addr_parse(&a_tmp, addrport) == AF_INET6) {
*port_out = 0;
*address_out = tor_strdup(addrport);