diff options
author | Nick Mathewson <nickm@torproject.org> | 2008-07-30 13:04:26 +0000 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2008-07-30 13:04:26 +0000 |
commit | e5bc5f11b87cee1a8f0630f6527bf5a7b1726d44 (patch) | |
tree | 745aa76a74950aa9ab913abde1722ffde967839b | |
parent | c4bf882d437018d8230cd086ca3470dfd6df8283 (diff) | |
download | tor-e5bc5f11b87cee1a8f0630f6527bf5a7b1726d44.tar.gz tor-e5bc5f11b87cee1a8f0630f6527bf5a7b1726d44.zip |
r17434@tombo: nickm | 2008-07-29 10:58:36 -0400
Refactor tor_addr_from_string: it didnt need most of parse_addr_mask_port_range, and its dependence on that latter function made it less flexible.
svn:r16255
-rw-r--r-- | src/common/address.c | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/src/common/address.c b/src/common/address.c index b698eba7a3..fef1f80fea 100644 --- a/src/common/address.c +++ b/src/common/address.c @@ -838,15 +838,31 @@ tor_dup_addr(const tor_addr_t *addr) return tor_strdup(buf); } -/** Convert the string in <b>src</b> to a tor_addr_t <b>addr</b>. +/** Convert the string in <b>src</b> to a tor_addr_t <b>addr</b>. The string + * may be an IPv4 address, an IPv6 address, or an IPv6 address surrounded by + * square brackets. * * Return an address family on success, or -1 if an invalid address string is * provided. */ int tor_addr_from_str(tor_addr_t *addr, const char *src) { + char *tmp = NULL; /* Holds substring if we got a dotted quad. */ + int result; tor_assert(addr && src); - return tor_addr_parse_mask_ports(src, addr, NULL, NULL, NULL); + if (src[0] == '[' && src[1]) + src = tmp = tor_strndup(src+1, strlen(src)-2); + + if (tor_inet_pton(AF_INET6, src, &addr->addr.in6_addr) > 0) { + result = addr->family = AF_INET6; + } else if (tor_inet_pton(AF_INET, src, &addr->addr.in_addr) > 0) { + result = addr->family = AF_INET; + } else { + result = -1; + } + + tor_free(tmp); + return result; } /** Set *<b>addr</b> to the IP address (if any) of whatever interface |