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