changeset 2234:790e447ef39c draft

We're not extracting the address from proc, so convert our match address to the same string format and compare instead.
author Roy Marples <roy@marples.name>
date Tue, 21 Jan 2014 09:58:55 +0000
parents 6229a6424748
children c6ef4773e366
files common.h if-linux.c
diffstat 2 files changed, 17 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/common.h	Mon Jan 20 16:16:54 2014 +0000
+++ b/common.h	Tue Jan 21 09:58:55 2014 +0000
@@ -39,6 +39,8 @@
 #endif
 
 #define UNCONST(a)		((void *)(unsigned long)(const void *)(a))
+#define STRINGIFY(a)		#a
+#define TOSTRING(a)		STRINGIFY(a)
 
 #define timeval_to_double(tv) ((tv)->tv_sec * 1.0 + (tv)->tv_usec * 1.0e-6)
 #define timernorm(tv) do {						\
--- a/if-linux.c	Mon Jan 20 16:16:54 2014 +0000
+++ b/if-linux.c	Tue Jan 21 09:58:55 2014 +0000
@@ -836,35 +836,37 @@
 in6_addr_flags(const char *ifname, const struct in6_addr *addr)
 {
 	FILE *fp;
-	char *ent, address[33], name[IF_NAMESIZE + 1];
+	char *p, ifaddress[33], address[33], name[IF_NAMESIZE + 1];
 	unsigned int ifindex;
 	int prefix, scope, flags, i;
-	struct in6_addr addr6;
-	static const char *hex = "0123456789abcdef";
 
 	fp = fopen("/proc/net/if_inet6", "r");
 	if (fp == NULL)
 		return -1;
-	while ((ent = get_line(fp))) {
-		i = sscanf(ent, "%32[a-f0-9] %x %x %x %x %16s\n",
+
+	p = ifaddress;
+	for (i = 0; i < (int)sizeof(addr->s6_addr); i++) {
+		p += snprintf(p, 3, "%.2x", addr->s6_addr[i]);
+	}
+	*p = '\0';
+
+	while ((p = get_line(fp))) {
+		i = sscanf(p, "%32[a-f0-9] %x %x %x %x"
+		    " %"TOSTRING(IF_NAMESIZE)"s\n",
 		    address, &ifindex, &prefix, &scope, &flags, name);
 		if (i != 6 || strlen(address) != 32) {
 			fclose(fp);
 			errno = ENOTSUP;
 			return -1;
 		}
-		if (strcmp(ifname, name))
-			continue;
-		for (i = 0; i < 16; i++) {
-			addr6.s6_addr[i] =
-			    ((strchr(hex, address[i * 2]) - hex) << 4) |
-			    (strchr(hex, address[i * 2 + 1]) - hex);
-		}
-		if (IN6_ARE_ADDR_EQUAL(addr, &addr6)) {
+		if (strcmp(ifname, name) == 0 &&
+		    strcmp(ifaddress, address) == 0)
+		{
 			fclose(fp);
 			return flags;
 		}
 	}
+
 	fclose(fp);
 	errno = ESRCH;
 	return -1;