about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2014-06-04 02:24:38 -0400
committerRich Felker <dalias@aerifal.cx>2014-06-04 02:24:38 -0400
commitbdad2fefb206d9727d4a3254f7883b8455452d89 (patch)
treef700c40bc61308cf7ac1a66d63f5bb16e1aca6eb
parent8041af59881219c32267c3491bee43591d3c3fe6 (diff)
downloadmusl-bdad2fefb206d9727d4a3254f7883b8455452d89.tar.gz
musl-bdad2fefb206d9727d4a3254f7883b8455452d89.tar.xz
musl-bdad2fefb206d9727d4a3254f7883b8455452d89.zip
add support for ipv6 scope_id to getaddrinfo and getnameinfo
for all address types, a scope_id specified as a decimal value is
accepted. for addresses with link-local scope, a string containing the
interface name is also accepted.

some changes are made to error handling to avoid unwanted fallbacks in
the case where the scope_id is invalid: if an earlier name lookup
backend fails with an error rather than simply "0 results", this
failure now suppresses any later attempts with other backends.

in getnameinfo, a light "itoa" type function is added for generating
decimal scope_id results, and decimal port strings for services are
also generated using this function now so as not to pull in the
dependency on snprintf.

in netdb.h, a definition for the NI_NUMERICSCOPE flag is added. this
is required by POSIX (it was previously missing) and needed to allow
callers to suppress interface-name lookups.
-rw-r--r--include/netdb.h2
-rw-r--r--src/network/getaddrinfo.c1
-rw-r--r--src/network/getnameinfo.c32
-rw-r--r--src/network/lookup_name.c31
4 files changed, 57 insertions, 9 deletions
diff --git a/include/netdb.h b/include/netdb.h
index dfc70e2b..703a4b26 100644
--- a/include/netdb.h
+++ b/include/netdb.h
@@ -41,7 +41,7 @@ struct addrinfo
 #define NI_NOFQDN       0x04
 #define NI_NAMEREQD     0x08
 #define NI_DGRAM        0x10
-/*#define NI_NUMERICSCOPE */
+#define NI_NUMERICSCOPE 0x100
 
 #define EAI_BADFLAGS   -1
 #define EAI_NONAME     -2
diff --git a/src/network/getaddrinfo.c b/src/network/getaddrinfo.c
index 70b6cfda..d9913445 100644
--- a/src/network/getaddrinfo.c
+++ b/src/network/getaddrinfo.c
@@ -105,6 +105,7 @@ int getaddrinfo(const char *restrict host, const char *restrict serv, const stru
 		case AF_INET6:
 			out[k].sa.sin6.sin6_family = AF_INET6;
 			out[k].sa.sin6.sin6_port = htons(ports[j].port);
+			out[k].sa.sin6.sin6_scope_id = addrs[i].scopeid;
 			memcpy(&out[k].sa.sin6.sin6_addr, &addrs[i].addr, 16);
 			break;			
 		}
diff --git a/src/network/getnameinfo.c b/src/network/getnameinfo.c
index dfcf6eda..6962ed1a 100644
--- a/src/network/getnameinfo.c
+++ b/src/network/getnameinfo.c
@@ -5,6 +5,7 @@
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
+#include <net/if.h>
 
 int __dns_parse(const unsigned char *, int, int (*)(void *, int, const void *, int, const void *), void *);
 int __dn_expand(const unsigned char *, const unsigned char *, const unsigned char *, char *, int);
@@ -14,6 +15,16 @@ int __res_send(const unsigned char *, int, unsigned char *, int);
 #define PTR_MAX (64 + sizeof ".in-addr.arpa")
 #define RR_PTR 12
 
+static char *itoa(char *p, unsigned x) {
+	p += 3*sizeof(int);
+	*--p = 0;
+	do {
+		*--p = '0' + x % 10;
+		x /= 10;
+	} while (x);
+	return p;
+}
+
 static void mkptr4(char *s, const unsigned char *ip)
 {
 	sprintf(s, "%d.%d.%d.%d.in-addr.arpa",
@@ -48,9 +59,10 @@ int getnameinfo(const struct sockaddr *restrict sa, socklen_t sl,
 	int flags)
 {
 	char ptr[PTR_MAX];
-	char buf[256];
+	char buf[256], num[3*sizeof(int)+1];
 	int af = sa->sa_family;
 	unsigned char *a;
+	unsigned x;
 
 	switch (af) {
 	case AF_INET:
@@ -84,16 +96,28 @@ int getnameinfo(const struct sockaddr *restrict sa, socklen_t sl,
 		if (!*buf) {
 			if (flags & NI_NAMEREQD) return EAI_NONAME;
 			inet_ntop(af, a, buf, sizeof buf);
+			if (af == AF_INET6 &&
+			    (x = ((struct sockaddr_in6 *)sa)->sin6_scope_id)) {
+				char *p = 0, tmp[IF_NAMESIZE+1];
+				if (!(flags & NI_NUMERICSCOPE) &&
+				    (IN6_IS_ADDR_LINKLOCAL(a) ||
+				     IN6_IS_ADDR_MC_LINKLOCAL(a)))
+					p = if_indextoname(x, tmp+1);
+				if (!p)
+					p = itoa(num, x);
+				*--p = '%';
+				strcat(buf, p);
+			}
 		}
 		if (strlen(buf) >= nodelen) return EAI_OVERFLOW;
 		strcpy(node, buf);
 	}
 
 	if (serv && servlen) {
-		if (snprintf(buf, sizeof buf, "%d",
-			ntohs(((struct sockaddr_in *)sa)->sin_port))>=servlen)
+		char *p = itoa(num, ntohs(((struct sockaddr_in *)sa)->sin_port));
+		if (strlen(p) >= servlen)
 			return EAI_OVERFLOW;
-		strcpy(serv, buf);
+		strcpy(serv, p);
 	}
 
 	return 0;
diff --git a/src/network/lookup_name.c b/src/network/lookup_name.c
index 02920930..492e932c 100644
--- a/src/network/lookup_name.c
+++ b/src/network/lookup_name.c
@@ -1,6 +1,7 @@
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <netdb.h>
+#include <net/if.h>
 #include <arpa/inet.h>
 #include <ctype.h>
 #include <stdlib.h>
@@ -47,9 +48,31 @@ static int name_from_numeric(struct address buf[static 1], const char *name, int
 		buf[0].family = AF_INET;
 		return 1;
 	}
-	if (family != AF_INET && inet_pton(AF_INET6, name, &a6)>0) {
+	if (family != AF_INET) {
+		char tmp[64];
+		char *p = strchr(name, '%'), *z;
+		unsigned long long scopeid;
+		if (p && p-name < 64) {
+			memcpy(tmp, name, p-name);
+			tmp[p-name] = 0;
+			name = tmp;
+		}
+		if (inet_pton(AF_INET6, name, &a6)<=0) return 0;
 		memcpy(&buf[0].addr, &a6, sizeof a6);
 		buf[0].family = AF_INET6;
+		if (p) {
+			if (isdigit(*++p)) scopeid = strtoull(p, &z, 10);
+			else z = p-1;
+			if (*z) {
+				if (!IN6_IS_ADDR_LINKLOCAL(&a6) &&
+				    !IN6_IS_ADDR_MC_LINKLOCAL(&a6))
+					return EAI_NONAME;
+				scopeid = if_nametoindex(p);
+				if (!scopeid) return EAI_NONAME;
+			}
+			if (scopeid > UINT_MAX) return EAI_NONAME;
+			buf[0].scopeid = scopeid;
+		}
 		return 1;
 	}
 	return 0;
@@ -179,10 +202,10 @@ int __lookup_name(struct address buf[static MAXADDRS], char canon[static 256], c
 
 	/* Try each backend until there's at least one result. */
 	cnt = name_from_null(buf, name, family, flags);
-	if (cnt<=0) cnt = name_from_numeric(buf, name, family);
-	if (cnt<=0 && !(flags & AI_NUMERICHOST)) {
+	if (!cnt) cnt = name_from_numeric(buf, name, family);
+	if (!cnt && !(flags & AI_NUMERICHOST)) {
 		cnt = name_from_hosts(buf, canon, name, family);
-		if (cnt<=0) cnt = name_from_dns(buf, canon, name, family);
+		if (!cnt) cnt = name_from_dns(buf, canon, name, family);
 	}
 	if (cnt<=0) return cnt ? cnt : EAI_NONAME;