From: Ted Lemon Date: Wed, 28 Aug 1996 01:37:06 +0000 (+0000) Subject: Add code to check interface type and skip point-to-point and loopback interfaces... X-Git-Tag: BETA_5~20 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=4593073fc8cdc8a3fe5cb2337b8e0508fe4c7fb1;p=thirdparty%2Fdhcp.git Add code to check interface type and skip point-to-point and loopback interfaces; copy ifreq structure out of ifconf structure so as to avoid unaligned pointer dereferences --- diff --git a/common/dispatch.c b/common/dispatch.c index b013d76d8..dbe34ead0 100644 --- a/common/dispatch.c +++ b/common/dispatch.c @@ -42,7 +42,7 @@ #ifndef lint static char copyright[] = -"$Id: dispatch.c,v 1.19 1996/08/27 09:48:40 mellon Exp $ Copyright (c) 1995, 1996 The Internet Software Consortium. All rights reserved.\n"; +"$Id: dispatch.c,v 1.20 1996/08/28 01:37:06 mellon Exp $ Copyright (c) 1995, 1996 The Internet Software Consortium. All rights reserved.\n"; #endif /* not lint */ #include "dhcpd.h" @@ -63,12 +63,13 @@ void discover_interfaces () struct interface_info *last; static char buf [8192]; struct ifconf ic; + struct ifreq ifr; int i; int sock; int address_count = 0; struct subnet *subnet; struct shared_network *share; - struct sockaddr_in *foo; + struct sockaddr_in foo; int ir; char *s; #ifdef USE_FALLBACK @@ -83,7 +84,7 @@ void discover_interfaces () ic.ifc_len = sizeof buf; ic.ifc_ifcu.ifcu_buf = (caddr_t)buf; i = ioctl(sock, SIOCGIFCONF, &ic); - close (sock); + if (i < 0) error ("ioctl: SIOCGIFCONF: %m"); @@ -110,6 +111,19 @@ void discover_interfaces () #endif + /* See if this is the sort of interface we want to + deal with. */ + strcpy (ifr.ifr_name, ifp -> ifr_name); + if (ioctl (sock, SIOCGIFFLAGS, &ifr) < 0) + error ("Can't get interface flags for %s: %m", + ifr.ifr_name); + + /* Skip loopback and point-to-point interfaces. */ + if ((ifr.ifr_flags & IFF_LOOPBACK) || + (ifr.ifr_flags & IFF_POINTOPOINT)) + continue; + + /* See if we've seen an interface that matches this one. */ for (tmp = interfaces; tmp; tmp = tmp -> next) if (!strcmp (tmp -> name, ifp -> ifr_name)) @@ -147,12 +161,14 @@ void discover_interfaces () struct iaddr addr; /* Get a pointer to the address... */ - foo = (struct sockaddr_in *)(&ifp -> ifr_addr); + memcpy (&foo, &ifp -> ifr_addr, + sizeof ifp -> ifr_addr); /* We don't want the loopback interface. */ - if (foo -> sin_addr.s_addr == htonl (INADDR_LOOPBACK)) + if (foo.sin_addr.s_addr == htonl (INADDR_LOOPBACK)) continue; + /* If this is the first real IP address we've found, keep a pointer to ifreq structure in which we found it. */ @@ -161,7 +177,7 @@ void discover_interfaces () /* Grab the address... */ addr.len = 4; - memcpy (addr.iabuf, &foo -> sin_addr.s_addr, + memcpy (addr.iabuf, &foo.sin_addr.s_addr, addr.len); /* If this address matches the server identifier, @@ -222,7 +238,8 @@ void discover_interfaces () } last = tmp; - foo = (struct sockaddr_in *)(&tmp -> tif -> ifr_addr); + memcpy (&foo, &tmp -> tif -> ifr_addr, + sizeof tmp -> tif -> ifr_addr); /* Find subnets that don't have valid interface addresses... */ @@ -238,7 +255,7 @@ void discover_interfaces () to the first address we found. */ subnet -> interface_address.len = 4; memcpy (subnet -> interface_address.iabuf, - &foo -> sin_addr.s_addr, 4); + &foo.sin_addr.s_addr, 4); } } @@ -249,7 +266,7 @@ void discover_interfaces () warn ("no server identifier specified."); server_identifier.len = 4; memcpy (server_identifier.iabuf, - &foo -> sin_addr.s_addr, 4); + &foo.sin_addr.s_addr, 4); /* Flag the server identifier as having matched, so we don't generate a spurious warning. */ server_identifier_matched = 1; @@ -265,6 +282,8 @@ void discover_interfaces () if (!server_identifier_matched) warn ("no interface address matches server identifier"); + close (sock); + #ifdef USE_FALLBACK strcpy (fallback_interface.name, "fallback"); fallback_interface.shared_network = &fallback_network; diff --git a/dispatch.c b/dispatch.c index b013d76d8..dbe34ead0 100644 --- a/dispatch.c +++ b/dispatch.c @@ -42,7 +42,7 @@ #ifndef lint static char copyright[] = -"$Id: dispatch.c,v 1.19 1996/08/27 09:48:40 mellon Exp $ Copyright (c) 1995, 1996 The Internet Software Consortium. All rights reserved.\n"; +"$Id: dispatch.c,v 1.20 1996/08/28 01:37:06 mellon Exp $ Copyright (c) 1995, 1996 The Internet Software Consortium. All rights reserved.\n"; #endif /* not lint */ #include "dhcpd.h" @@ -63,12 +63,13 @@ void discover_interfaces () struct interface_info *last; static char buf [8192]; struct ifconf ic; + struct ifreq ifr; int i; int sock; int address_count = 0; struct subnet *subnet; struct shared_network *share; - struct sockaddr_in *foo; + struct sockaddr_in foo; int ir; char *s; #ifdef USE_FALLBACK @@ -83,7 +84,7 @@ void discover_interfaces () ic.ifc_len = sizeof buf; ic.ifc_ifcu.ifcu_buf = (caddr_t)buf; i = ioctl(sock, SIOCGIFCONF, &ic); - close (sock); + if (i < 0) error ("ioctl: SIOCGIFCONF: %m"); @@ -110,6 +111,19 @@ void discover_interfaces () #endif + /* See if this is the sort of interface we want to + deal with. */ + strcpy (ifr.ifr_name, ifp -> ifr_name); + if (ioctl (sock, SIOCGIFFLAGS, &ifr) < 0) + error ("Can't get interface flags for %s: %m", + ifr.ifr_name); + + /* Skip loopback and point-to-point interfaces. */ + if ((ifr.ifr_flags & IFF_LOOPBACK) || + (ifr.ifr_flags & IFF_POINTOPOINT)) + continue; + + /* See if we've seen an interface that matches this one. */ for (tmp = interfaces; tmp; tmp = tmp -> next) if (!strcmp (tmp -> name, ifp -> ifr_name)) @@ -147,12 +161,14 @@ void discover_interfaces () struct iaddr addr; /* Get a pointer to the address... */ - foo = (struct sockaddr_in *)(&ifp -> ifr_addr); + memcpy (&foo, &ifp -> ifr_addr, + sizeof ifp -> ifr_addr); /* We don't want the loopback interface. */ - if (foo -> sin_addr.s_addr == htonl (INADDR_LOOPBACK)) + if (foo.sin_addr.s_addr == htonl (INADDR_LOOPBACK)) continue; + /* If this is the first real IP address we've found, keep a pointer to ifreq structure in which we found it. */ @@ -161,7 +177,7 @@ void discover_interfaces () /* Grab the address... */ addr.len = 4; - memcpy (addr.iabuf, &foo -> sin_addr.s_addr, + memcpy (addr.iabuf, &foo.sin_addr.s_addr, addr.len); /* If this address matches the server identifier, @@ -222,7 +238,8 @@ void discover_interfaces () } last = tmp; - foo = (struct sockaddr_in *)(&tmp -> tif -> ifr_addr); + memcpy (&foo, &tmp -> tif -> ifr_addr, + sizeof tmp -> tif -> ifr_addr); /* Find subnets that don't have valid interface addresses... */ @@ -238,7 +255,7 @@ void discover_interfaces () to the first address we found. */ subnet -> interface_address.len = 4; memcpy (subnet -> interface_address.iabuf, - &foo -> sin_addr.s_addr, 4); + &foo.sin_addr.s_addr, 4); } } @@ -249,7 +266,7 @@ void discover_interfaces () warn ("no server identifier specified."); server_identifier.len = 4; memcpy (server_identifier.iabuf, - &foo -> sin_addr.s_addr, 4); + &foo.sin_addr.s_addr, 4); /* Flag the server identifier as having matched, so we don't generate a spurious warning. */ server_identifier_matched = 1; @@ -265,6 +282,8 @@ void discover_interfaces () if (!server_identifier_matched) warn ("no interface address matches server identifier"); + close (sock); + #ifdef USE_FALLBACK strcpy (fallback_interface.name, "fallback"); fallback_interface.shared_network = &fallback_network;