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