unix, windows: fix parsing scoped IPv6 addresses

Signed-off-by: Saúl Ibarra Corretgé <saghul@gmail.com>
This commit is contained in:
Saúl Ibarra Corretgé 2014-05-23 09:25:23 +02:00
parent e7b3c3fb6b
commit 4fac9427e4
2 changed files with 22 additions and 16 deletions

View File

@ -159,11 +159,27 @@ static int inet_ntop6(const unsigned char *src, char *dst, size_t size) {
int uv_inet_pton(int af, const char* src, void* dst) {
if (src == NULL || dst == NULL)
return UV_EINVAL;
switch (af) {
case AF_INET:
return (inet_pton4(src, dst));
case AF_INET6:
return (inet_pton6(src, dst));
case AF_INET6: {
int len;
char tmp[UV__INET6_ADDRSTRLEN], *s, *p;
s = (char*) src;
p = strchr(src, '%');
if (p != NULL) {
s = tmp;
len = p - src;
if (len > UV__INET6_ADDRSTRLEN-1)
return UV_EINVAL;
memcpy(s, src, len);
s[len] = '\0';
}
return inet_pton6(s, dst);
}
default:
return UV_EAFNOSUPPORT;
}
@ -228,7 +244,7 @@ static int inet_pton6(const char *src, unsigned char *dst) {
curtok = src;
seen_xdigits = 0;
val = 0;
while ((ch = *src++) != '\0' && ch != '%') {
while ((ch = *src++) != '\0') {
const char *pch;
if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
@ -259,19 +275,7 @@ static int inet_pton6(const char *src, unsigned char *dst) {
continue;
}
if (ch == '.' && ((tp + sizeof(struct in_addr)) <= endp)) {
int err;
/* Scope id present, parse ipv4 addr without it */
pch = strchr(curtok, '%');
if (pch != NULL) {
char tmp[sizeof "255.255.255.255"];
memcpy(tmp, curtok, pch - curtok);
curtok = tmp;
src = pch;
}
err = inet_pton4(curtok, tp);
int err = inet_pton4(curtok, tp);
if (err == 0) {
tp += sizeof(struct in_addr);
seen_xdigits = 0;

View File

@ -103,6 +103,7 @@ TEST_IMPL(ip6_addr_link_local) {
X("fe80::2acf:daff:fedd:342a") \
X("fe80:0:0:0:2acf:daff:fedd:342a") \
X("fe80:0:0:0:2acf:daff:1.2.3.4") \
X("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255") \
#define BAD_ADDR_LIST(X) \
X(":::1") \
@ -110,6 +111,7 @@ TEST_IMPL(ip6_addr_link_local) {
X("fe80:0:0:0:2acf:daff:fedd:342a:5678") \
X("fe80:0:0:0:2acf:daff:abcd:1.2.3.4") \
X("fe80:0:0:2acf:daff:1.2.3.4.5") \
X("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255.255") \
#define TEST_GOOD(ADDR) \
ASSERT(0 == uv_inet_pton(AF_INET6, ADDR, &addr)); \