philipp / rpms / dhcp

Forked from rpms/dhcp 4 years ago
Clone

Blame SOURCES/dhcp-4.2.4-getifaddrs.patch

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