54343e
From c09dd24a7d63988e0acef7d033bd3e088fc005c0 Mon Sep 17 00:00:00 2001
54343e
From: Jiri Popelka <jpopelka@redhat.com>
54343e
Date: Thu, 24 Jan 2013 12:39:50 +0100
54343e
Subject: [PATCH] Linux interface discovery
54343e
54343e
Use the same discovery code as for *BSD and OS X,
54343e
i.e. the getifaddrs() function.
54343e
---
54343e
 common/discover.c | 398 +++---------------------------------------------------
54343e
 1 file changed, 17 insertions(+), 381 deletions(-)
54343e
54343e
diff --git a/common/discover.c b/common/discover.c
54343e
index 1d84219..f2a8f6d 100644
54343e
--- a/common/discover.c
54343e
+++ b/common/discover.c
54343e
@@ -379,391 +379,13 @@ end_iface_scan(struct iface_conf_list *ifaces) {
54343e
 	ifaces->sock = -1;
54343e
 }
54343e
 
54343e
-#elif __linux /* !HAVE_SIOCGLIFCONF */
54343e
-/* 
54343e
- * Linux support
54343e
- * -------------
54343e
- *
54343e
- * In Linux, we use the /proc pseudo-filesystem to get information
54343e
- * about interfaces, along with selected ioctl() calls.
54343e
- *
54343e
- * Linux low level access is documented in the netdevice man page.
54343e
- */
54343e
-
54343e
-/* 
54343e
- * Structure holding state about the scan.
54343e
- */
54343e
-struct iface_conf_list {
54343e
-	int sock;	/* file descriptor used to get information */
54343e
-	FILE *fp;	/* input from /proc/net/dev */
54343e
-#ifdef DHCPv6
54343e
-	FILE *fp6;	/* input from /proc/net/if_inet6 */
54343e
-#endif
54343e
-};
54343e
-
54343e
-/* 
54343e
- * Structure used to return information about a specific interface.
54343e
- */
54343e
-struct iface_info {
54343e
-	char name[IFNAMSIZ];		/* name of the interface, e.g. "eth0" */
54343e
-	struct sockaddr_storage addr;	/* address information */
54343e
-	isc_uint64_t flags;		/* interface flags, e.g. IFF_LOOPBACK */
54343e
-};
54343e
-
54343e
-/* 
54343e
- * Start a scan of interfaces.
54343e
- *
54343e
- * The iface_conf_list structure maintains state for this process.
54343e
- */
54343e
-int 
54343e
-begin_iface_scan(struct iface_conf_list *ifaces) {
54343e
-	char buf[256];
54343e
-	int len;
54343e
-	int i;
54343e
-
54343e
-	ifaces->fp = fopen("/proc/net/dev", "r");
54343e
-	if (ifaces->fp == NULL) {
54343e
-		log_error("Error opening '/proc/net/dev' to list interfaces");
54343e
-		return 0;
54343e
-	}
54343e
-
54343e
-	/*
54343e
-	 * The first 2 lines are header information, so read and ignore them.
54343e
-	 */
54343e
-	for (i=0; i<2; i++) {
54343e
-		if (fgets(buf, sizeof(buf), ifaces->fp) == NULL) {
54343e
-			log_error("Error reading headers from '/proc/net/dev'");
54343e
-			fclose(ifaces->fp);
54343e
-			ifaces->fp = NULL;
54343e
-			return 0;
54343e
-		}
54343e
-		len = strlen(buf);
54343e
-		if ((len <= 0) || (buf[len-1] != '\n')) { 
54343e
-			log_error("Bad header line in '/proc/net/dev'");
54343e
-			fclose(ifaces->fp);
54343e
-			ifaces->fp = NULL;
54343e
-			return 0;
54343e
-		}
54343e
-	}
54343e
-
54343e
-	ifaces->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
54343e
-	if (ifaces->sock < 0) {
54343e
-		log_error("Error creating socket to list interfaces; %m");
54343e
-		fclose(ifaces->fp);
54343e
-		ifaces->fp = NULL;
54343e
-		return 0;
54343e
-	}
54343e
-
54343e
-#ifdef DHCPv6
54343e
-	if (local_family == AF_INET6) {
54343e
-		ifaces->fp6 = fopen("/proc/net/if_inet6", "r");
54343e
-		if (ifaces->fp6 == NULL) {
54343e
-			log_error("Error opening '/proc/net/if_inet6' to "
54343e
-				  "list IPv6 interfaces; %m");
54343e
-			close(ifaces->sock);
54343e
-			ifaces->sock = -1;
54343e
-			fclose(ifaces->fp);
54343e
-			ifaces->fp = NULL;
54343e
-			return 0;
54343e
-		}
54343e
-	}
54343e
-#endif
54343e
-
54343e
-	return 1;
54343e
-}
54343e
-
54343e
-/*
54343e
- * Read our IPv4 interfaces from /proc/net/dev.
54343e
- *
54343e
- * The file looks something like this:
54343e
- *
54343e
- * Inter-|   Receive ...
54343e
- *  face |bytes    packets errs drop fifo frame ...
54343e
- *     lo: 1580562    4207    0    0    0     0 ...
54343e
- *   eth0:       0       0    0    0    0     0 ...
54343e
- *   eth1:1801552440   37895    0   14    0     ...
54343e
- *
54343e
- * We only care about the interface name, which is at the start of 
54343e
- * each line.
54343e
- *
54343e
- * We use an ioctl() to get the address and flags for each interface.
54343e
- */
54343e
-static int
54343e
-next_iface4(struct iface_info *info, int *err, struct iface_conf_list *ifaces) {
54343e
-	char buf[256];
54343e
-	int len;
54343e
-	char *p;
54343e
-	char *name;
54343e
-	struct ifreq tmp;
54343e
-
54343e
-	/*
54343e
-	 * Loop exits when we find an interface that has an address, or 
54343e
-	 * when we run out of interfaces.
54343e
-	 */
54343e
-	for (;;) {
54343e
-		do {
54343e
-			/*
54343e
-	 		 *  Read the next line in the file.
54343e
-	 		 */
54343e
-			if (fgets(buf, sizeof(buf), ifaces->fp) == NULL) {
54343e
-				if (ferror(ifaces->fp)) {
54343e
-					*err = 1;
54343e
-					log_error("Error reading interface "
54343e
-					  	"information");
54343e
-				} else {
54343e
-					*err = 0;
54343e
-				}
54343e
-				return 0;
54343e
-			}
54343e
-
54343e
-			/*
54343e
-	 		 * Make sure the line is a nice, 
54343e
-			 * newline-terminated line.
54343e
-	 		 */
54343e
-			len = strlen(buf);
54343e
-			if ((len <= 0) || (buf[len-1] != '\n')) { 
54343e
-				log_error("Bad line reading interface "
54343e
-					  "information");
54343e
-				*err = 1;
54343e
-				return 0;
54343e
-			}
54343e
-
54343e
-			/*
54343e
-	 		 * Figure out our name.
54343e
-	 		 */
54343e
-			p = strrchr(buf, ':');
54343e
-			if (p == NULL) {
54343e
-				log_error("Bad line reading interface "
54343e
-					  "information (no colon)");
54343e
-				*err = 1;
54343e
-				return 0;
54343e
-			}
54343e
-			*p = '\0';
54343e
-			name = buf;
54343e
-			while (isspace(*name)) {
54343e
-				name++;
54343e
-			}
54343e
-
54343e
-			/* 
54343e
-		 	 * Copy our name into our interface structure.
54343e
-		 	 */
54343e
-			len = p - name;
54343e
-			if (len >= sizeof(info->name)) {
54343e
-				*err = 1;
54343e
-				log_error("Interface name '%s' too long", name);
54343e
-				return 0;
54343e
-			}
54343e
-			strcpy(info->name, name);
54343e
-
54343e
-#ifdef ALIAS_NAMED_PERMUTED
54343e
-			/* interface aliases look like "eth0:1" or "wlan1:3" */
54343e
-			s = strchr(info->name, ':');
54343e
-			if (s != NULL) {
54343e
-				*s = '\0';
54343e
-			}
54343e
-#endif
54343e
-
54343e
-#ifdef SKIP_DUMMY_INTERFACES
54343e
-		} while (strncmp(info->name, "dummy", 5) == 0);
54343e
-#else
54343e
-		} while (0);
54343e
-#endif
54343e
-
54343e
-		memset(&tmp, 0, sizeof(tmp));
54343e
-		strcpy(tmp.ifr_name, name);
54343e
-		if (ioctl(ifaces->sock, SIOCGIFADDR, &tmp) < 0) {
54343e
-			if (errno == EADDRNOTAVAIL) {
54343e
-				continue;
54343e
-			}
54343e
-			log_error("Error getting interface address "
54343e
-				  "for '%s'; %m", name);
54343e
-			*err = 1;
54343e
-			return 0;
54343e
-		}
54343e
-		memcpy(&info->addr, &tmp.ifr_addr, sizeof(tmp.ifr_addr));
54343e
-
54343e
-		memset(&tmp, 0, sizeof(tmp));
54343e
-		strcpy(tmp.ifr_name, name);
54343e
-		if (ioctl(ifaces->sock, SIOCGIFFLAGS, &tmp) < 0) {
54343e
-			log_error("Error getting interface flags for '%s'; %m", 
54343e
-			  	name);
54343e
-			*err = 1;
54343e
-			return 0;
54343e
-		}
54343e
-		info->flags = tmp.ifr_flags;
54343e
-
54343e
-		*err = 0;
54343e
-		return 1;
54343e
-	}
54343e
-}
54343e
-
54343e
-#ifdef DHCPv6
54343e
-/*
54343e
- * Read our IPv6 interfaces from /proc/net/if_inet6.
54343e
- *
54343e
- * The file looks something like this:
54343e
- *
54343e
- * fe80000000000000025056fffec00008 05 40 20 80   vmnet8
54343e
- * 00000000000000000000000000000001 01 80 10 80       lo
54343e
- * fe80000000000000025056fffec00001 06 40 20 80   vmnet1
54343e
- * 200108881936000202166ffffe497d9b 03 40 00 00     eth1
54343e
- * fe8000000000000002166ffffe497d9b 03 40 20 80     eth1
54343e
- *
54343e
- * We get IPv6 address from the start, the interface name from the end, 
54343e
- * and ioctl() to get flags.
54343e
- */
54343e
-static int
54343e
-next_iface6(struct iface_info *info, int *err, struct iface_conf_list *ifaces) {
54343e
-	char buf[256];
54343e
-	int len;
54343e
-	char *p;
54343e
-	char *name;
54343e
-	int i;
54343e
-	struct sockaddr_in6 addr;
54343e
-	struct ifreq tmp;
54343e
-
54343e
-	do {
54343e
-		/*
54343e
-		 *  Read the next line in the file.
54343e
-		 */
54343e
-		if (fgets(buf, sizeof(buf), ifaces->fp6) == NULL) {
54343e
-			if (ferror(ifaces->fp6)) {
54343e
-				*err = 1;
54343e
-				log_error("Error reading IPv6 "
54343e
-					  "interface information");
54343e
-			} else {
54343e
-				*err = 0;
54343e
-			}
54343e
-			return 0;
54343e
-		}
54343e
-
54343e
-		/*
54343e
-		 * Make sure the line is a nice, newline-terminated line.
54343e
-		 */
54343e
-		len = strlen(buf);
54343e
-		if ((len <= 0) || (buf[len-1] != '\n')) { 
54343e
-			log_error("Bad line reading IPv6 "
54343e
-				  "interface information");
54343e
-			*err = 1;
54343e
-			return 0;
54343e
-		}
54343e
-
54343e
-		/*
54343e
- 		 * Figure out our name.
54343e
- 		 */
54343e
-		buf[--len] = '\0';
54343e
-		p = strrchr(buf, ' ');
54343e
-		if (p == NULL) {
54343e
-			log_error("Bad line reading IPv6 interface "
54343e
-			          "information (no space)");
54343e
-			*err = 1;
54343e
-			return 0;
54343e
-		}
54343e
-		name = p+1;
54343e
-
54343e
-		/* 
54343e
- 		 * Copy our name into our interface structure.
54343e
- 		 */
54343e
-		len = strlen(name);
54343e
-		if (len >= sizeof(info->name)) {
54343e
-			*err = 1;
54343e
-			log_error("IPv6 interface name '%s' too long", name);
54343e
-			return 0;
54343e
-		}
54343e
-		strcpy(info->name, name);
54343e
-
54343e
-#ifdef SKIP_DUMMY_INTERFACES
54343e
-	} while (strncmp(info->name, "dummy", 5) == 0);
54343e
-#else
54343e
-	} while (0);
54343e
-#endif
54343e
-
54343e
-	/*
54343e
-	 * Double-check we start with the IPv6 address.
54343e
-	 */
54343e
-	for (i=0; i<32; i++) {
54343e
-		if (!isxdigit(buf[i]) || isupper(buf[i])) {
54343e
-			*err = 1;
54343e
-			log_error("Bad line reading IPv6 interface address "
54343e
-				  "for '%s'", name);
54343e
-			return 0;
54343e
-		}
54343e
-	}
54343e
-
54343e
-	/* 
54343e
-	 * Load our socket structure.
54343e
-	 */
54343e
-	memset(&addr, 0, sizeof(addr));
54343e
-	addr.sin6_family = AF_INET6;
54343e
-	for (i=0; i<16; i++) {
54343e
-		unsigned char byte;
54343e
-                static const char hex[] = "0123456789abcdef";
54343e
-                byte = ((index(hex, buf[i * 2]) - hex) << 4) |
54343e
-			(index(hex, buf[i * 2 + 1]) - hex);
54343e
-		addr.sin6_addr.s6_addr[i] = byte;
54343e
-	}
54343e
-	memcpy(&info->addr, &addr, sizeof(addr));
54343e
-
54343e
-	/*
54343e
-	 * Get our flags.
54343e
-	 */
54343e
-	memset(&tmp, 0, sizeof(tmp));
54343e
-	strcpy(tmp.ifr_name, name);
54343e
-	if (ioctl(ifaces->sock, SIOCGIFFLAGS, &tmp) < 0) {
54343e
-		log_error("Error getting interface flags for '%s'; %m", name);
54343e
-		*err = 1;
54343e
-		return 0;
54343e
-	}
54343e
-	info->flags = tmp.ifr_flags;
54343e
-
54343e
-	*err = 0;
54343e
-	return 1;
54343e
-}
54343e
-#endif /* DHCPv6 */
54343e
-
54343e
-/*
54343e
- * Retrieve the next interface.
54343e
- *
54343e
- * Returns information in the info structure. 
54343e
- * Sets err to 1 if there is an error, otherwise 0.
54343e
- */
54343e
-int
54343e
-next_iface(struct iface_info *info, int *err, struct iface_conf_list *ifaces) {
54343e
-	if (next_iface4(info, err, ifaces)) {
54343e
-		return 1;
54343e
-	}
54343e
-#ifdef DHCPv6
54343e
-	if (!(*err)) {
54343e
-		if (local_family == AF_INET6)
54343e
-			return next_iface6(info, err, ifaces);
54343e
-	}
54343e
-#endif
54343e
-	return 0;
54343e
-}
54343e
-
54343e
-/*
54343e
- * End scan of interfaces.
54343e
- */
54343e
-void
54343e
-end_iface_scan(struct iface_conf_list *ifaces) {
54343e
-	fclose(ifaces->fp);
54343e
-	ifaces->fp = NULL;
54343e
-	close(ifaces->sock);
54343e
-	ifaces->sock = -1;
54343e
-#ifdef DHCPv6
54343e
-	if (local_family == AF_INET6) {
54343e
-		fclose(ifaces->fp6);
54343e
-		ifaces->fp6 = NULL;
54343e
-	}
54343e
-#endif
54343e
-}
54343e
 #else
54343e
 
54343e
 /* 
54343e
  * BSD support
54343e
  * -----------
54343e
  *
54343e
- * FreeBSD, NetBSD, OpenBSD, and OS X all have the getifaddrs() 
54343e
+ * FreeBSD, NetBSD, OpenBSD, OS X and Linux all have the getifaddrs()
54343e
  * function.
54343e
  *
54343e
  * The getifaddrs() man page describes the use.
54343e
@@ -811,6 +433,8 @@ begin_iface_scan(struct iface_conf_list *ifaces) {
54343e
  */
54343e
 int
54343e
 next_iface(struct iface_info *info, int *err, struct iface_conf_list *ifaces) {
54343e
+	size_t sa_len = 0;
54343e
+
54343e
 	if (ifaces->next == NULL) {
54343e
 		*err = 0;
54343e
 		return 0;
54343e
@@ -822,8 +446,20 @@ next_iface(struct iface_info *info, int *err, struct iface_conf_list *ifaces) {
54343e
 		return 0;
54343e
 	}
54343e
 	strcpy(info->name, ifaces->next->ifa_name);
54343e
-	memcpy(&info->addr, ifaces->next->ifa_addr, 
54343e
-	       ifaces->next->ifa_addr->sa_len);
54343e
+
54343e
+	memset(&info->addr, 0 , sizeof(info->addr));
54343e
+
54343e
+	if (ifaces->next->ifa_addr != NULL) {
54343e
+#ifdef HAVE_SA_LEN
54343e
+		sa_len = ifaces->next->ifa_addr->sa_len;
54343e
+#else
54343e
+		if (ifaces->next->ifa_addr->sa_family == AF_INET)
54343e
+			sa_len = sizeof(struct sockaddr_in);
54343e
+		else if (ifaces->next->ifa_addr->sa_family == AF_INET6)
54343e
+			sa_len = sizeof(struct sockaddr_in6);
54343e
+#endif
54343e
+		memcpy(&info->addr, ifaces->next->ifa_addr, sa_len);
54343e
+	}
54343e
 	info->flags = ifaces->next->ifa_flags;
54343e
 	ifaces->next = ifaces->next->ifa_next;
54343e
 	*err = 0;
54343e
-- 
54343e
1.8.1
54343e