1 diff -up dhcp-4.3.5b1/common/discover.c.getifaddrs dhcp-4.3.5b1/common/discover.c
2 --- dhcp-4.3.5b1/common/discover.c.getifaddrs 2016-09-12 17:10:39.585374741 +0200
3 +++ dhcp-4.3.5b1/common/discover.c 2016-09-12 17:15:56.160628636 +0200
4 @@ -373,392 +373,13 @@ end_iface_scan(struct iface_conf_list *i
8 -#elif __linux /* !HAVE_SIOCGLIFCONF */
13 - * In Linux, we use the /proc pseudo-filesystem to get information
14 - * about interfaces, along with selected ioctl() calls.
16 - * Linux low level access is documented in the netdevice man page.
20 - * Structure holding state about the scan.
22 -struct iface_conf_list {
23 - int sock; /* file descriptor used to get information */
24 - FILE *fp; /* input from /proc/net/dev */
26 - FILE *fp6; /* input from /proc/net/if_inet6 */
31 - * Structure used to return information about a specific interface.
34 - char name[IFNAMSIZ]; /* name of the interface, e.g. "eth0" */
35 - struct sockaddr_storage addr; /* address information */
36 - isc_uint64_t flags; /* interface flags, e.g. IFF_LOOPBACK */
40 - * Start a scan of interfaces.
42 - * The iface_conf_list structure maintains state for this process.
45 -begin_iface_scan(struct iface_conf_list *ifaces) {
46 - char buf[IF_LINE_LENGTH];
50 - ifaces->fp = fopen("/proc/net/dev", "r");
51 - if (ifaces->fp == NULL) {
52 - log_error("Error opening '/proc/net/dev' to list interfaces");
57 - * The first 2 lines are header information, so read and ignore them.
59 - for (i=0; i<2; i++) {
60 - if (fgets(buf, sizeof(buf), ifaces->fp) == NULL) {
61 - log_error("Error reading headers from '/proc/net/dev'");
67 - if ((len <= 0) || (buf[len-1] != '\n')) {
68 - log_error("Bad header line in '/proc/net/dev'");
75 - ifaces->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
76 - if (ifaces->sock < 0) {
77 - log_error("Error creating socket to list interfaces; %m");
84 - if (local_family == AF_INET6) {
85 - ifaces->fp6 = fopen("/proc/net/if_inet6", "r");
86 - if (ifaces->fp6 == NULL) {
87 - log_error("Error opening '/proc/net/if_inet6' to "
88 - "list IPv6 interfaces; %m");
89 - close(ifaces->sock);
102 - * Read our IPv4 interfaces from /proc/net/dev.
104 - * The file looks something like this:
106 - * Inter-| Receive ...
107 - * face |bytes packets errs drop fifo frame ...
108 - * lo: 1580562 4207 0 0 0 0 ...
109 - * eth0: 0 0 0 0 0 0 ...
110 - * eth1:1801552440 37895 0 14 0 ...
112 - * We only care about the interface name, which is at the start of
115 - * We use an ioctl() to get the address and flags for each interface.
118 -next_iface4(struct iface_info *info, int *err, struct iface_conf_list *ifaces) {
119 - char buf[IF_LINE_LENGTH];
126 - * Loop exits when we find an interface that has an address, or
127 - * when we run out of interfaces.
132 - * Read the next line in the file.
134 - if (fgets(buf, sizeof(buf), ifaces->fp) == NULL) {
135 - if (ferror(ifaces->fp)) {
137 - log_error("Error reading interface "
146 - * Make sure the line is a nice,
147 - * newline-terminated line.
150 - if ((len <= 0) || (buf[len-1] != '\n')) {
151 - log_error("Bad line reading interface "
158 - * Figure out our name.
160 - p = strrchr(buf, ':');
162 - log_error("Bad line reading interface "
163 - "information (no colon)");
169 - while (isspace(*name)) {
174 - * Copy our name into our interface structure.
177 - if (len >= sizeof(info->name)) {
179 - log_error("Interface name '%s' too long", name);
182 - strncpy(info->name, name, sizeof(info->name) - 1);
184 -#ifdef ALIAS_NAMED_PERMUTED
185 - /* interface aliases look like "eth0:1" or "wlan1:3" */
186 - s = strchr(info->name, ':');
192 -#ifdef SKIP_DUMMY_INTERFACES
193 - } while (strncmp(info->name, "dummy", 5) == 0);
198 - memset(&tmp, 0, sizeof(tmp));
199 - strncpy(tmp.ifr_name, name, sizeof(tmp.ifr_name) - 1);
200 - if (ioctl(ifaces->sock, SIOCGIFADDR, &tmp) < 0) {
201 - if (errno == EADDRNOTAVAIL) {
204 - log_error("Error getting interface address "
205 - "for '%s'; %m", name);
209 - memcpy(&info->addr, &tmp.ifr_addr, sizeof(tmp.ifr_addr));
211 - memset(&tmp, 0, sizeof(tmp));
212 - strncpy(tmp.ifr_name, name, sizeof(tmp.ifr_name) - 1);
213 - if (ioctl(ifaces->sock, SIOCGIFFLAGS, &tmp) < 0) {
214 - log_error("Error getting interface flags for '%s'; %m",
219 - info->flags = tmp.ifr_flags;
228 - * Read our IPv6 interfaces from /proc/net/if_inet6.
230 - * The file looks something like this:
232 - * fe80000000000000025056fffec00008 05 40 20 80 vmnet8
233 - * 00000000000000000000000000000001 01 80 10 80 lo
234 - * fe80000000000000025056fffec00001 06 40 20 80 vmnet1
235 - * 200108881936000202166ffffe497d9b 03 40 00 00 eth1
236 - * fe8000000000000002166ffffe497d9b 03 40 20 80 eth1
238 - * We get IPv6 address from the start, the interface name from the end,
239 - * and ioctl() to get flags.
242 -next_iface6(struct iface_info *info, int *err, struct iface_conf_list *ifaces) {
243 - char buf[IF_LINE_LENGTH];
248 - struct sockaddr_in6 addr;
253 - * Read the next line in the file.
255 - if (fgets(buf, sizeof(buf), ifaces->fp6) == NULL) {
256 - if (ferror(ifaces->fp6)) {
258 - log_error("Error reading IPv6 "
259 - "interface information");
267 - * Make sure the line is a nice, newline-terminated line.
270 - if ((len <= 0) || (buf[len-1] != '\n')) {
271 - log_error("Bad line reading IPv6 "
272 - "interface information");
278 - * Figure out our name.
281 - p = strrchr(buf, ' ');
283 - log_error("Bad line reading IPv6 interface "
284 - "information (no space)");
291 - * Copy our name into our interface structure.
293 - len = strlen(name);
294 - if (len >= sizeof(info->name)) {
296 - log_error("IPv6 interface name '%s' too long", name);
299 - strncpy(info->name, name, sizeof(info->name) - 1);
301 -#ifdef SKIP_DUMMY_INTERFACES
302 - } while (strncmp(info->name, "dummy", 5) == 0);
308 - * Double-check we start with the IPv6 address.
310 - for (i=0; i<32; i++) {
311 - if (!isxdigit(buf[i]) || isupper(buf[i])) {
313 - log_error("Bad line reading IPv6 interface address "
320 - * Load our socket structure.
322 - memset(&addr, 0, sizeof(addr));
323 - addr.sin6_family = AF_INET6;
324 - for (i=0; i<16; i++) {
325 - unsigned char byte;
326 - static const char hex[] = "0123456789abcdef";
327 - byte = ((index(hex, buf[i * 2]) - hex) << 4) |
328 - (index(hex, buf[i * 2 + 1]) - hex);
329 - addr.sin6_addr.s6_addr[i] = byte;
331 - memcpy(&info->addr, &addr, sizeof(addr));
336 - memset(&tmp, 0, sizeof(tmp));
337 - strncpy(tmp.ifr_name, name, sizeof(tmp.ifr_name) - 1);
338 - if (ioctl(ifaces->sock, SIOCGIFFLAGS, &tmp) < 0) {
339 - log_error("Error getting interface flags for '%s'; %m", name);
343 - info->flags = tmp.ifr_flags;
351 - * Retrieve the next interface.
353 - * Returns information in the info structure.
354 - * Sets err to 1 if there is an error, otherwise 0.
357 -next_iface(struct iface_info *info, int *err, struct iface_conf_list *ifaces) {
358 - memset(info, 0, sizeof(struct iface_info));
359 - if (next_iface4(info, err, ifaces)) {
364 - if (local_family == AF_INET6)
365 - return next_iface6(info, err, ifaces);
372 - * End scan of interfaces.
375 -end_iface_scan(struct iface_conf_list *ifaces) {
376 - fclose(ifaces->fp);
378 - close(ifaces->sock);
381 - if (local_family == AF_INET6) {
382 - fclose(ifaces->fp6);
383 - ifaces->fp6 = NULL;
393 - * FreeBSD, NetBSD, OpenBSD, and OS X all have the getifaddrs()
394 + * FreeBSD, NetBSD, OpenBSD, OS X and Linux all have the getifaddrs()
397 * The getifaddrs() man page describes the use.
398 @@ -806,6 +427,8 @@ begin_iface_scan(struct iface_conf_list
401 next_iface(struct iface_info *info, int *err, struct iface_conf_list *ifaces) {
404 if (ifaces->next == NULL) {
407 @@ -818,8 +441,20 @@ next_iface(struct iface_info *info, int
409 memset(info, 0, sizeof(struct iface_info));
410 strncpy(info->name, ifaces->next->ifa_name, sizeof(info->name) - 1);
411 - memcpy(&info->addr, ifaces->next->ifa_addr,
412 - ifaces->next->ifa_addr->sa_len);
414 + memset(&info->addr, 0 , sizeof(info->addr));
416 + if (ifaces->next->ifa_addr != NULL) {
418 + sa_len = ifaces->next->ifa_addr->sa_len;
420 + if (ifaces->next->ifa_addr->sa_family == AF_INET)
421 + sa_len = sizeof(struct sockaddr_in);
422 + else if (ifaces->next->ifa_addr->sa_family == AF_INET6)
423 + sa_len = sizeof(struct sockaddr_in6);
425 + memcpy(&info->addr, ifaces->next->ifa_addr, sa_len);
427 info->flags = ifaces->next->ifa_flags;
428 ifaces->next = ifaces->next->ifa_next;