summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJun-ichiro itojun Hagino <itojun@cvs.openbsd.org>2003-08-19 22:19:09 +0000
committerJun-ichiro itojun Hagino <itojun@cvs.openbsd.org>2003-08-19 22:19:09 +0000
commitf0cf3adcad5d0247891eddb49c1e8ab30f13e513 (patch)
treeb38fd2a0d84ace93f340f64d6f2676d91900add3
parent97545fe4089995279b1270673dd8950368a985e5 (diff)
rewrite SIOCGIFCONF into getifaddrs. deraadt ok
-rw-r--r--usr.sbin/bootpd/getether.c45
-rw-r--r--usr.sbin/mopd/common/device.c104
-rw-r--r--usr.sbin/mopd/common/os.h5
-rw-r--r--usr.sbin/mrouted/cfparse.y57
-rw-r--r--usr.sbin/mtrace/mtrace.c35
-rw-r--r--usr.sbin/pppd/sys-bsd.c83
-rw-r--r--usr.sbin/pppoe/pppoe.c60
-rw-r--r--usr.sbin/rbootd/bpf.c59
-rw-r--r--usr.sbin/timed/timed/timed.c86
9 files changed, 165 insertions, 369 deletions
diff --git a/usr.sbin/bootpd/getether.c b/usr.sbin/bootpd/getether.c
index efcc3074eea..e03477da9af 100644
--- a/usr.sbin/bootpd/getether.c
+++ b/usr.sbin/bootpd/getether.c
@@ -111,52 +111,35 @@ getether(ifname, eap)
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_types.h>
+#include <ifaddrs.h>
int
getether(ifname, eap)
char *ifname; /* interface name from ifconfig structure */
char *eap; /* Ether address (output) */
{
- int fd, rc = -1;
- int n;
- struct ifreq ibuf[16], ifr;
- struct ifconf ifc;
- struct ifreq *ifrp, *ifend;
+ int rc = -1;
+ struct ifaddrs *ifap, *ifa;
/* Fetch the interface configuration */
- fd = socket(AF_INET, SOCK_DGRAM, 0);
- if (fd < 0) {
- report(LOG_ERR, "getether: socket %s: %s", ifname, get_errmsg());
- return (fd);
- }
- ifc.ifc_len = sizeof(ibuf);
- ifc.ifc_buf = (caddr_t) ibuf;
- if (ioctl(fd, SIOCGIFCONF, (char *) &ifc) < 0 ||
- ifc.ifc_len < sizeof(struct ifreq)) {
- report(LOG_ERR, "getether: SIOCGIFCONF: %s", get_errmsg());
- goto out;
+ if (getifaddrs(&ifap) != 0) {
+ report(LOG_ERR, "getether: getifaddrs %s: %s", ifname,
+ get_errmsg());
+ return (-1);
}
- /* Search interface configuration list for link layer address. */
- ifrp = ibuf;
- ifend = (struct ifreq *) ((char *) ibuf + ifc.ifc_len);
- while (ifrp < ifend) {
+
+ for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
/* Look for interface */
- if (strcmp(ifname, ifrp->ifr_name) == 0 &&
- ifrp->ifr_addr.sa_family == AF_LINK &&
- ((struct sockaddr_dl *) &ifrp->ifr_addr)->sdl_type == IFT_ETHER) {
- bcopy(LLADDR((struct sockaddr_dl *) &ifrp->ifr_addr), eap, EALEN);
+ if (strcmp(ifname, ifa->ifa_name) == 0 &&
+ ifa->ifa_addr->sa_family == AF_LINK &&
+ ((struct sockaddr_dl *)ifa->ifa_addr)->sdl_type == IFT_ETHER) {
+ bcopy(LLADDR((struct sockaddr_dl *)ifa->ifa_addr), eap, EALEN);
rc = 0;
break;
}
- /* Bump interface config pointer */
- n = ifrp->ifr_addr.sa_len + sizeof(ifrp->ifr_name);
- if (n < sizeof(*ifrp))
- n = sizeof(*ifrp);
- ifrp = (struct ifreq *) ((char *) ifrp + n);
}
- out:
- close(fd);
+ freeifaddrs(ifap);
return (rc);
}
diff --git a/usr.sbin/mopd/common/device.c b/usr.sbin/mopd/common/device.c
index 30e6b5b0c09..a6b289ac53e 100644
--- a/usr.sbin/mopd/common/device.c
+++ b/usr.sbin/mopd/common/device.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: device.c,v 1.7 2003/06/02 21:38:39 maja Exp $ */
+/* $OpenBSD: device.c,v 1.8 2003/08/19 22:19:08 itojun Exp $ */
/*
* Copyright (c) 1993-95 Mats O Jansson. All rights reserved.
@@ -25,7 +25,7 @@
*/
#ifndef LINT
-static char rcsid[] = "$OpenBSD: device.c,v 1.7 2003/06/02 21:38:39 maja Exp $";
+static char rcsid[] = "$OpenBSD: device.c,v 1.8 2003/08/19 22:19:08 itojun Exp $";
#endif
#include "os.h"
@@ -58,34 +58,21 @@ deviceEthAddr(ifname, eaddr)
struct sockaddr_dl *sdl;
int fd;
int i, len;
+ struct ifaddrs *ifap, *ifa;
- /* We cannot use SIOCGIFADDR on the BPF descriptor.
- We must instead get all the interfaces with SIOCGIFCONF
- and find the right one. */
-
- /* Use datagram socket to get Ethernet address. */
- if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
- syslog(LOG_ERR, "deviceEthAddr: socket: %m");
+ if (getifaddrs(&ifap) != 0) {
+ syslog(LOG_ERR, "deviceEthAddr: getifaddrs: %m");
exit(1);
}
- ifc.ifc_len = sizeof(inbuf);
- ifc.ifc_buf = inbuf;
- if (ioctl(fd, SIOCGIFCONF, (caddr_t)&ifc) < 0 ||
- ifc.ifc_len < sizeof(struct ifreq)) {
- syslog(LOG_ERR, "deviceEthAddr: SIOGIFCONF: %m");
- exit(1);
- }
- ifr = ifc.ifc_req;
- for (i = 0; i < ifc.ifc_len;
- i += len, ifr = (struct ifreq *)((caddr_t)ifr + len)) {
- len = sizeof(ifr->ifr_name) + ifr->ifr_addr.sa_len;
- sdl = (struct sockaddr_dl *)&ifr->ifr_addr;
+ for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
+ sdl = (struct sockaddr_dl *)ifa->ifa_addr;
if (sdl->sdl_family != AF_LINK || sdl->sdl_type != IFT_ETHER ||
sdl->sdl_alen != 6)
continue;
- if (!strncmp(ifr->ifr_name, ifname, sizeof(ifr->ifr_name))) {
+ if (!strcmp(ifa->ifa_name, ifname)) {
bcopy((caddr_t)LLADDR(sdl), (caddr_t)eaddr, 6);
+ freeifaddrs(ifap);
return;
}
}
@@ -262,74 +249,41 @@ deviceInitOne(ifname)
void
deviceInitAll()
{
-#ifdef DEV_NEW_CONF
- char inbuf[8192];
- struct ifconf ifc;
- struct ifreq *ifr;
+#ifdef DEV_NEW_CONF
struct sockaddr_dl *sdl;
- int fd;
- int i, len;
+ struct ifaddrs *ifap, *ifa;
- if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
- syslog(LOG_ERR, "deviceInitAll: socket: %m");
+ if (getifaddrs(&ifap) != 0) {
+ syslog(LOG_ERR, "deviceInitAll: getifaddrs: %m");
exit(1);
}
- ifc.ifc_len = sizeof(inbuf);
- ifc.ifc_buf = inbuf;
- if (ioctl(fd, SIOCGIFCONF, (caddr_t)&ifc) < 0 ||
- ifc.ifc_len < sizeof(struct ifreq)) {
- syslog(LOG_ERR, "deviceInitAll: SIOCGIFCONF: %m");
- exit(1);
- }
- ifr = ifc.ifc_req;
- for (i = 0; i < ifc.ifc_len;
- i += len, ifr = (struct ifreq *)((caddr_t)ifr + len)) {
- len = sizeof(ifr->ifr_name) + ifr->ifr_addr.sa_len;
- sdl = (struct sockaddr_dl *)&ifr->ifr_addr;
+ for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
+ sdl = (struct sockaddr_dl *)ifa->ifa_next;
if (sdl->sdl_family != AF_LINK || sdl->sdl_type != IFT_ETHER ||
sdl->sdl_alen != 6)
continue;
- if (ioctl(fd, SIOCGIFFLAGS, (caddr_t)ifr) < 0) {
- syslog(LOG_ERR, "deviceInitAll: SIOCGIFFLAGS: %m");
- continue;
- }
- if ((ifr->ifr_flags &
+ if ((ifa->ifa_flags &
(IFF_UP | IFF_LOOPBACK | IFF_POINTOPOINT)) != IFF_UP)
continue;
- deviceInitOne(ifr->ifr_name);
+ deviceInitOne(ifa->ifa_name);
}
- (void) close(fd);
+ freeifaddrs(ifap);
#else
- int fd;
- int n;
- struct ifreq ibuf[8], *ifrp;
- struct ifconf ifc;
+ struct ifaddrs *ifap, *ifa;
- if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
- syslog(LOG_ERR, "deviceInitAll: old socket: %m");
- exit(1);
- }
- ifc.ifc_len = sizeof ibuf;
- ifc.ifc_buf = (caddr_t)ibuf;
- if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0 ||
- ifc.ifc_len < sizeof(struct ifreq)) {
- syslog(LOG_ERR, "deviceInitAll: old SIOCGIFCONF: %m");
+ if (getifaddrs(&ifap) != 0) {
+ syslog(LOG_ERR, "deviceInitAll: getifaddrs: %m");
exit(1);
}
- ifrp = ibuf;
- n = ifc.ifc_len / sizeof(*ifrp);
- for (; --n >= 0; ++ifrp) {
- if (ioctl(fd, SIOCGIFFLAGS, (char *)ifrp) < 0) {
- continue;
- }
- if (/*(ifrp->ifr_flags & IFF_UP) == 0 ||*/
- ifrp->ifr_flags & IFF_LOOPBACK ||
- ifrp->ifr_flags & IFF_POINTOPOINT)
+
+ for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
+ if (/*(ifa->ifa_flags & IFF_UP) == 0 ||*/
+ ifa->ifa_flags & IFF_LOOPBACK ||
+ ifa->ifa_flags & IFF_POINTOPOINT)
continue;
- deviceInitOne(ifrp->ifr_name);
+ deviceInitOne(ifa->ifa_name);
}
-
- (void) close(fd);
-#endif /* DEV_NEW_CONF */
+ freeifaddrs(ifap);
+#endif
}
diff --git a/usr.sbin/mopd/common/os.h b/usr.sbin/mopd/common/os.h
index feddd40365c..fa0e8e58938 100644
--- a/usr.sbin/mopd/common/os.h
+++ b/usr.sbin/mopd/common/os.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: os.h,v 1.5 2003/06/02 21:38:39 maja Exp $ */
+/* $OpenBSD: os.h,v 1.6 2003/08/19 22:19:08 itojun Exp $ */
/*
* Copyright (c) 1994-95 Mats O Jansson. All rights reserved.
@@ -23,7 +23,7 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
- * $OpenBSD: os.h,v 1.5 2003/06/02 21:38:39 maja Exp $
+ * $OpenBSD: os.h,v 1.6 2003/08/19 22:19:08 itojun Exp $
*
*/
@@ -48,6 +48,7 @@
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
+#include <ifaddrs.h>
#define DEV_NEW_CONF
diff --git a/usr.sbin/mrouted/cfparse.y b/usr.sbin/mrouted/cfparse.y
index 8e318b92ece..8f34d05eae8 100644
--- a/usr.sbin/mrouted/cfparse.y
+++ b/usr.sbin/mrouted/cfparse.y
@@ -27,6 +27,7 @@
#include <stdarg.h>
#include "defs.h"
#include <netdb.h>
+#include <ifaddrs.h>
/*
* Local function declarations
@@ -37,7 +38,7 @@ static void yyerror(char *s);
static char * next_word(void);
static int yylex(void);
static u_int32_t valid_if(char *s);
-static struct ifreq * ifconfaddr(struct ifconf *ifcp, u_int32_t a);
+static const char * ifconfaddr(u_int32_t a);
int yyparse(void);
static FILE *f;
@@ -49,8 +50,6 @@ extern int cache_lifetime;
extern int max_prune_lifetime;
static int lineno;
-static struct ifreq ifbuf[32];
-static struct ifconf ifc;
static struct uvif *v;
@@ -128,26 +127,25 @@ stmt : error
}
ifmods
| TUNNEL interface addrname {
-
- struct ifreq *ifr;
+ const char *ifname;
struct ifreq ffr;
vifi_t vifi;
order++;
- ifr = ifconfaddr(&ifc, $2);
- if (ifr == 0)
+ ifname = ifconfaddr($2);
+ if (ifname == 0)
fatal("Tunnel local address %s is not mine",
inet_fmt($2, s1));
- strncpy(ffr.ifr_name, ifr->ifr_name, IFNAMSIZ);
+ strlcpy(ffr.ifr_name, ifname, sizeof(ffr.ifr_name));
if (ioctl(udp_socket, SIOCGIFFLAGS, (char *)&ffr)<0)
fatal("ioctl SIOCGIFFLAGS on %s",ffr.ifr_name);
if (ffr.ifr_flags & IFF_LOOPBACK)
fatal("Tunnel local address %s is a loopback interface",
inet_fmt($2, s1));
- if (ifconfaddr(&ifc, $3) != 0)
+ if (ifconfaddr($3) != 0)
fatal("Tunnel remote address %s is one of mine",
inet_fmt($3, s1));
@@ -572,11 +570,6 @@ config_vifs_from_file()
return;
}
- ifc.ifc_buf = (char *)ifbuf;
- ifc.ifc_len = sizeof(ifbuf);
- if (ioctl(udp_socket, SIOCGIFCONF, (char *)&ifc) < 0)
- log(LOG_ERR, errno, "ioctl SIOCGIFCONF");
-
yyparse();
fclose(f);
@@ -596,28 +589,24 @@ char *s;
return 0;
}
-static struct ifreq *
-ifconfaddr(ifcp, a)
- struct ifconf *ifcp;
+static const char *
+ifconfaddr(a)
u_int32_t a;
{
- int n;
- struct ifreq *ifrp = (struct ifreq *)ifcp->ifc_buf;
- struct ifreq *ifend = (struct ifreq *)((char *)ifrp + ifcp->ifc_len);
-
- while (ifrp < ifend) {
- if (ifrp->ifr_addr.sa_family == AF_INET &&
- ((struct sockaddr_in *)&ifrp->ifr_addr)->sin_addr.s_addr == a)
- return (ifrp);
-#if (defined(BSD) && (BSD >= 199006))
- n = ifrp->ifr_addr.sa_len + sizeof(ifrp->ifr_name);
- if (n < sizeof(*ifrp))
- ++ifrp;
- else
- ifrp = (struct ifreq *)((char *)ifrp + n);
-#else
- ++ifrp;
-#endif
+ static char ifname[IFNAMSIZ];
+ struct ifaddrs *ifap, *ifa;
+
+ if (getifaddrs(&ifap) != 0)
+ return (NULL);
+
+ for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
+ if (ifa->ifa_addr->sa_family == AF_INET &&
+ ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == a) {
+ strlcpy(ifname, ifa->ifa_name, sizeof(ifname));
+ freeifaddrs(ifap);
+ return (ifname);
+ }
}
+ freeifaddrs(ifap);
return (0);
}
diff --git a/usr.sbin/mtrace/mtrace.c b/usr.sbin/mtrace/mtrace.c
index 4620dc20394..078de372ae1 100644
--- a/usr.sbin/mtrace/mtrace.c
+++ b/usr.sbin/mtrace/mtrace.c
@@ -52,7 +52,7 @@
#ifndef lint
static char rcsid[] =
- "@(#) $Id: mtrace.c,v 1.18 2003/08/19 19:08:38 deraadt Exp $";
+ "@(#) $Id: mtrace.c,v 1.19 2003/08/19 22:19:07 itojun Exp $";
#endif
#include <netdb.h>
@@ -68,6 +68,7 @@ static char rcsid[] =
#ifdef SUNOS5
#include <sys/systeminfo.h>
#endif
+#include <ifaddrs.h>
#define DEFAULT_TIMEOUT 3 /* How long to wait before retrying requests */
#define DEFAULT_RETRIES 3 /* How many times to try */
@@ -284,37 +285,31 @@ get_netmask(s, dst)
int s;
u_int32_t dst;
{
- unsigned int i;
- char ifbuf[5000];
- struct ifconf ifc;
- struct ifreq *ifr;
u_int32_t if_addr, if_mask;
u_int32_t retval = 0xFFFFFFFF;
int found = FALSE;
+ struct ifaddrs *ifap, *ifa;
- ifc.ifc_buf = ifbuf;
- ifc.ifc_len = sizeof(ifbuf);
- if (ioctl(s, SIOCGIFCONF, (char *) &ifc) < 0) {
- perror("ioctl (SIOCGIFCONF)");
+ if (getifaddrs(&ifap) != 0) {
+ perror("getifaddrs");
return (retval);
}
- for (i = 0; i < ifc.ifc_len; ) {
- ifr = (struct ifreq *)((char *)ifc.ifc_req + i);
- i += sizeof(ifr->ifr_name) + ifr->ifr_addr.sa_len;
- if_addr = ((struct sockaddr_in *)&(ifr->ifr_addr))->sin_addr.s_addr;
- if (ioctl(s, SIOCGIFNETMASK, (char *)ifr) >= 0) {
- if_mask = ((struct sockaddr_in *)&(ifr->ifr_addr))->sin_addr.s_addr;
- if ((dst & if_mask) == (if_addr & if_mask)) {
- retval = if_mask;
- if (lcl_addr == 0) lcl_addr = if_addr;
- }
+ for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
+ if_addr = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr;
+ if_mask = ((struct sockaddr_in *)ifa->ifa_netmask)->sin_addr.s_addr;
+ if ((dst & if_mask) == (if_addr & if_mask)) {
+ retval = if_mask;
+ if (lcl_addr == 0)
+ lcl_addr = if_addr;
}
- if (lcl_addr == if_addr) found = TRUE;
+ if (lcl_addr == if_addr)
+ found = TRUE;
}
if (!found && lcl_addr != 0) {
printf("Interface address is not valid\n");
exit(1);
}
+ freeifaddrs(ifap);
return (retval);
}
diff --git a/usr.sbin/pppd/sys-bsd.c b/usr.sbin/pppd/sys-bsd.c
index 8993cf783d4..591db9fe9b4 100644
--- a/usr.sbin/pppd/sys-bsd.c
+++ b/usr.sbin/pppd/sys-bsd.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: sys-bsd.c,v 1.21 2003/05/14 02:02:56 itojun Exp $ */
+/* $OpenBSD: sys-bsd.c,v 1.22 2003/08/19 22:19:07 itojun Exp $ */
/*
* sys-bsd.c - System-dependent procedures for setting up
@@ -78,7 +78,7 @@
#if 0
static char rcsid[] = "Id: sys-bsd.c,v 1.31 1998/04/02 12:04:19 paulus Exp $";
#else
-static char rcsid[] = "$OpenBSD: sys-bsd.c,v 1.21 2003/05/14 02:02:56 itojun Exp $";
+static char rcsid[] = "$OpenBSD: sys-bsd.c,v 1.22 2003/08/19 22:19:07 itojun Exp $";
#endif
#endif
@@ -102,6 +102,7 @@ static char rcsid[] = "$OpenBSD: sys-bsd.c,v 1.21 2003/05/14 02:02:56 itojun Exp
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/stat.h>
+#include <ifaddrs.h>
#ifdef PPP_FILTER
#include <net/bpf.h>
@@ -1427,17 +1428,12 @@ get_ether_addr(ipaddr, hwaddr)
u_int32_t ipaddr;
struct sockaddr_dl *hwaddr;
{
- struct ifreq *ifr, *ifend, *ifp;
u_int32_t ina, mask;
struct sockaddr_dl *dla;
- struct ifreq ifreq;
- struct ifconf ifc;
- struct ifreq ifs[MAX_IFS];
-
- ifc.ifc_len = sizeof(ifs);
- ifc.ifc_req = ifs;
- if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
- syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
+ struct ifaddrs *ifap, *ifa, *ifp;
+
+ if (getifaddrs(&ifap) != 0) {
+ syslog(LOG_ERR, "getifaddrs: %m");
return 0;
}
@@ -1445,29 +1441,21 @@ get_ether_addr(ipaddr, hwaddr)
* Scan through looking for an interface with an Internet
* address on the same subnet as `ipaddr'.
*/
- ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
- for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
- ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len)) {
- if (ifr->ifr_addr.sa_family == AF_INET) {
- ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
- strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
+ for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
+ if (ifa->ifa_addr->sa_family == AF_INET) {
+ ina = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr;
/*
* Check that the interface is up, and not point-to-point
* or loopback.
*/
- if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
- continue;
- if ((ifreq.ifr_flags &
+ if ((ifa->ifa_flags &
(IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
!= (IFF_UP|IFF_BROADCAST))
continue;
/*
* Get its netmask and check that it's on the right subnet.
*/
- ifreq.ifr_addr = ifr->ifr_addr;
- if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
- continue;
- mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
+ mask = ((struct sockaddr_in *)ifa->ifa_netmask)->sin_addr.s_addr;
if ((ipaddr & mask) != (ina & mask))
continue;
@@ -1475,28 +1463,30 @@ get_ether_addr(ipaddr, hwaddr)
}
}
- if (ifr >= ifend)
+ if (ifa == NULL) {
+ freeifaddrs(ifap);
return 0;
- syslog(LOG_INFO, "found interface %s for proxy arp", ifr->ifr_name);
+ }
+ syslog(LOG_INFO, "found interface %s for proxy arp", ifa->ifa_name);
/*
* Now scan through again looking for a link-level address
* for this interface.
*/
- ifp = ifr;
- for (ifr = ifc.ifc_req; ifr < ifend; ) {
- if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0
- && ifr->ifr_addr.sa_family == AF_LINK) {
+ ifp = ifa;
+ for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
+ if (strcmp(ifp->ifa_name, ifa->ifa_name) == 0
+ && ifa->ifa_addr->sa_family == AF_LINK) {
/*
* Found the link-level address - copy it out
*/
- dla = (struct sockaddr_dl *) &ifr->ifr_addr;
+ dla = (struct sockaddr_dl *)ifa->ifa_addr;
BCOPY(dla, hwaddr, dla->sdl_len);
return 1;
}
- ifr = (struct ifreq *) ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len);
}
+ freeifaddrs(ifap);
return 0;
}
@@ -1513,9 +1503,7 @@ GetMask(addr)
u_int32_t addr;
{
u_int32_t mask, nmask, ina;
- struct ifreq *ifr, *ifend, ifreq;
- struct ifconf ifc;
- struct ifreq ifs[MAX_IFS];
+ struct ifaddrs *ifap, *ifa;
addr = ntohl(addr);
if (IN_CLASSA(addr)) /* determine network mask for address class */
@@ -1530,41 +1518,32 @@ GetMask(addr)
/*
* Scan through the system's network interfaces.
*/
- ifc.ifc_len = sizeof(ifs);
- ifc.ifc_req = ifs;
- if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
- syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m");
+ if (getifaddrs(&ifap) != 0) {
+ syslog(LOG_WARNING, "getifaddrs: %m");
return mask;
}
- ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
- for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
- ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len)) {
+ for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
/*
* Check the interface's internet address.
*/
- if (ifr->ifr_addr.sa_family != AF_INET)
+ if (ifa->ifa_addr->sa_family != AF_INET)
continue;
- ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
+ ina = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr;
if ((ntohl(ina) & nmask) != (addr & nmask))
continue;
/*
* Check that the interface is up, and not point-to-point or loopback.
*/
- strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
- if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
- continue;
- if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
+ if ((ifa->ifa_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
!= IFF_UP)
continue;
/*
* Get its netmask and OR it into our mask.
*/
- ifreq.ifr_addr = ifr->ifr_addr;
- if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
- continue;
- mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
+ mask |= ((struct sockaddr_in *)ifa->ifa_netmask)->sin_addr.s_addr;
}
+ freeifaddrs(ifap);
return mask;
}
diff --git a/usr.sbin/pppoe/pppoe.c b/usr.sbin/pppoe/pppoe.c
index 07a53160ac5..79870c6998d 100644
--- a/usr.sbin/pppoe/pppoe.c
+++ b/usr.sbin/pppoe/pppoe.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: pppoe.c,v 1.10 2003/06/28 20:37:29 deraadt Exp $ */
+/* $OpenBSD: pppoe.c,v 1.11 2003/08/19 22:19:07 itojun Exp $ */
/*
* Copyright (c) 2000 Network Security Technologies, Inc. http://www.netsec.net
@@ -46,6 +46,7 @@
#include <sysexits.h>
#include <stdlib.h>
#include <signal.h>
+#include <ifaddrs.h>
#include "pppoe.h"
@@ -295,79 +296,48 @@ getifhwaddr(ifnhint, ifnambuf, ea)
char *ifnhint, *ifnambuf;
struct ether_addr *ea;
{
- int s;
- char *inbuf = NULL;
- struct ifconf ifc;
- struct ifreq *ifrp, ifreq, req;
struct sockaddr_dl *dl;
- int len = 8192, i;
+ struct ifaddrs *ifap, *ifa;
- s = socket(AF_INET, SOCK_DGRAM, 0);
- if (s < 0) {
- perror("socket");
+ if (getifaddrs(&ifap) != 0) {
+ perror("getifaddrs");
return (-1);
}
- while (1) {
- ifc.ifc_len= len;
- ifc.ifc_buf = inbuf = realloc(inbuf, len);
- if (inbuf == NULL)
- err(1, "malloc");
- if (ioctl(s, SIOCGIFCONF, &ifc) < 0)
- err(1, "gifconf");
- if (ifc.ifc_len + sizeof(struct ifreq) < len)
- break;
- len *= 2;
- }
-
- ifrp = ifc.ifc_req;
- ifreq.ifr_name[0] = '\0';
- for (i = 0; i < ifc.ifc_len; ) {
- ifrp = (struct ifreq *)((caddr_t)ifc.ifc_req + i);
- i += sizeof(ifrp->ifr_name) +
- (ifrp->ifr_addr.sa_len > sizeof(struct sockaddr) ?
- ifrp->ifr_addr.sa_len : sizeof(struct sockaddr));
- if (ifrp->ifr_addr.sa_family != AF_LINK)
+ for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
+ if (ifa->ifa_addr->sa_family != AF_LINK)
continue;
- if (ifnhint != NULL && strncmp(ifnhint, ifrp->ifr_name,
- sizeof(ifrp->ifr_name)))
+ if (ifnhint != NULL && strcmp(ifnhint, ifa->ifa_name))
continue;
if (ifnhint == NULL) {
- strlcpy(req.ifr_name, ifrp->ifr_name, IFNAMSIZ);
- if (ioctl(s, SIOCGIFFLAGS, &req) < 0)
- err(EX_IOERR, "get flags");
- if ((req.ifr_flags & IFF_UP) == 0)
+ if ((ifa->ifa_flags & IFF_UP) == 0)
continue;
}
- dl = (struct sockaddr_dl *)&ifrp->ifr_addr;
+ dl = (struct sockaddr_dl *)ifa->ifa_addr;
if (dl->sdl_type != IFT_ETHER) {
if (ifnhint == NULL)
continue;
fprintf(stderr, "not ethernet interface: %s\n",
ifnhint);
- free(inbuf);
- close(s);
+ freeifaddrs(ifap);
return (-1);
}
if (dl->sdl_alen != ETHER_ADDR_LEN) {
fprintf(stderr, "invalid hwaddr len: %u\n",
dl->sdl_alen);
- free(inbuf);
- close(s);
+ freeifaddrs(ifap);
return (-1);
}
bcopy(dl->sdl_data + dl->sdl_nlen, ea, sizeof(*ea));
- strlcpy(ifnambuf, ifrp->ifr_name, IFNAMSIZ);
- free(inbuf);
- close(s);
+ strlcpy(ifnambuf, ifa->ifa_name, IFNAMSIZ);
+ freeifaddrs(ifap);
return (0);
}
- free(inbuf);
+ freeifaddrs(ifap);
if (ifnhint == NULL)
fprintf(stderr, "no running ethernet found\n");
else
fprintf(stderr, "no such interface: %s\n", ifnhint);
- close(s);
return (-1);
}
diff --git a/usr.sbin/rbootd/bpf.c b/usr.sbin/rbootd/bpf.c
index f5e6ded4b41..397a000c597 100644
--- a/usr.sbin/rbootd/bpf.c
+++ b/usr.sbin/rbootd/bpf.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: bpf.c,v 1.12 2003/06/02 23:36:54 millert Exp $ */
+/* $OpenBSD: bpf.c,v 1.13 2003/08/19 22:19:07 itojun Exp $ */
/* $NetBSD: bpf.c,v 1.5.2.1 1995/11/14 08:45:42 thorpej Exp $ */
/*
@@ -45,7 +45,7 @@
#ifndef lint
/*static char sccsid[] = "@(#)bpf.c 8.1 (Berkeley) 6/4/93";*/
-static char rcsid[] = "$OpenBSD: bpf.c,v 1.12 2003/06/02 23:36:54 millert Exp $";
+static char rcsid[] = "$OpenBSD: bpf.c,v 1.13 2003/08/19 22:19:07 itojun Exp $";
#endif /* not lint */
#include <sys/param.h>
@@ -63,6 +63,7 @@ static char rcsid[] = "$OpenBSD: bpf.c,v 1.12 2003/06/02 23:36:54 millert Exp $"
#include <string.h>
#include <syslog.h>
#include <unistd.h>
+#include <ifaddrs.h>
#include "defs.h"
#include "pathnames.h"
@@ -219,80 +220,50 @@ BpfOpen(void)
char *
BpfGetIntfName(char **errmsg)
{
- struct ifreq ibuf[8], *ifrp, *ifend, *mp;
- struct ifconf ifc;
- int fd;
int minunit, n;
char *cp;
- static char device[sizeof(ifrp->ifr_name)];
+ static char device[IFNAMSIZ];
static char errbuf[128] = "No Error!";
+ struct ifaddrs *ifap, *ifa, *mp;
if (errmsg != NULL)
*errmsg = errbuf;
- if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
- (void) strlcpy(errbuf, "bpf: socket: %m", sizeof(errbuf));
+ if (getifaddrs(&ifap) != 0) {
+ (void) strlcpy(errbuf, "bpf: getifaddrs: %m", sizeof(errbuf));
return(NULL);
}
- ifc.ifc_len = sizeof ibuf;
- ifc.ifc_buf = (caddr_t)ibuf;
-
-#ifdef OSIOCGIFCONF
- if (ioctl(fd, OSIOCGIFCONF, (char *)&ifc) < 0 ||
- ifc.ifc_len < sizeof(struct ifreq)) {
- (void) strlcpy(errbuf, "bpf: ioctl(OSIOCGIFCONF): %m",
- sizeof (errbuf));
- return(NULL);
- }
-#else
- if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0 ||
- ifc.ifc_len < sizeof(struct ifreq)) {
- (void) strlcpy(errbuf, "bpf: ioctl(SIOCGIFCONF): %m",
- sizeof(errbuf));
- return(NULL);
- }
-#endif
- ifrp = ibuf;
- ifend = (struct ifreq *)((char *)ibuf + ifc.ifc_len);
-
- mp = 0;
- minunit = 666;
- for (; ifrp < ifend; ++ifrp) {
- if (ioctl(fd, SIOCGIFFLAGS, (char *)ifrp) < 0) {
- (void) strlcpy(errbuf, "bpf: ioctl(SIOCGIFFLAGS): %m",
- sizeof(errbuf));
- return(NULL);
- }
+ for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
/*
* If interface is down or this is the loopback interface,
* ignore it.
*/
- if ((ifrp->ifr_flags & IFF_UP) == 0 ||
+ if ((ifa->ifa_flags & IFF_UP) == 0 ||
#ifdef IFF_LOOPBACK
- (ifrp->ifr_flags & IFF_LOOPBACK))
+ (ifa->ifa_flags & IFF_LOOPBACK))
#else
- (strcmp(ifrp->ifr_name, "lo0") == 0))
+ (strcmp(ifa->ifa_name, "lo0") == 0))
#endif
continue;
- for (cp = ifrp->ifr_name; !isdigit(*cp); ++cp)
+ for (cp = ifa->ifa_name; !isdigit(*cp); ++cp)
;
n = atoi(cp);
if (n < minunit) {
minunit = n;
- mp = ifrp;
+ mp = ifa;
}
}
- (void) close(fd);
if (mp == 0) {
(void) strlcpy(errbuf, "bpf: no interfaces found",
sizeof(errbuf));
return(NULL);
}
- (void) strlcpy(device, mp->ifr_name, sizeof device);
+ (void) strlcpy(device, mp->ifa_name, sizeof device);
+ freeifaddrs(ifap);
return(device);
}
diff --git a/usr.sbin/timed/timed/timed.c b/usr.sbin/timed/timed/timed.c
index faaa3b638ab..82203f9936c 100644
--- a/usr.sbin/timed/timed/timed.c
+++ b/usr.sbin/timed/timed/timed.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: timed.c,v 1.22 2003/07/18 22:58:56 david Exp $ */
+/* $OpenBSD: timed.c,v 1.23 2003/08/19 22:19:08 itojun Exp $ */
/*-
* Copyright (c) 1985, 1993 The Regents of the University of California.
@@ -52,6 +52,7 @@ static char sccsid[] = "@(#)timed.c 5.1 (Berkeley) 5/11/93";
#include <sys/times.h>
#include <netgroup.h>
#include <err.h>
+#include <ifaddrs.h>
int trace = 0;
int sock, sock_raw = -1;
@@ -125,9 +126,6 @@ main(int argc, char **argv)
int nflag, iflag;
struct timeval ntime;
struct servent *srvp;
- char *inbuf = NULL, *cp, *cplim;
- struct ifconf ifc;
- struct ifreq ifreq, ifreqf, *ifr;
struct netinfo *ntp;
struct netinfo *ntip;
struct netinfo *savefromnet;
@@ -136,6 +134,7 @@ main(int argc, char **argv)
u_short port;
int inlen = 8192;
int ch;
+ struct ifaddrs *ifap, *ifa;
ntip = NULL;
@@ -279,85 +278,40 @@ main(int argc, char **argv)
nt->net <<= 8;
}
- while (1) {
- char *ninbuf;
-
- ifc.ifc_len = inlen;
- ninbuf = realloc(inbuf, inlen);
- if (ninbuf == NULL) {
- if (inbuf)
- free(inbuf);
- close(sock);
- return (-1);
- }
- ifc.ifc_buf = inbuf = ninbuf;
- if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
- (void) close(sock);
- free(inbuf);
- perror("timed: get interface configuration");
- exit(1);
- }
- if (ifc.ifc_len + sizeof(ifreq) < inlen)
- break;
- inlen *= 2;
+ if (getifaddrs(&ifap) != 0) {
+ perror("timed: get interface configuration");
+ exit(1);
}
ntp = NULL;
-#define size(p) max((p).sa_len, sizeof(p))
- cplim = inbuf + ifc.ifc_len; /*skip over if's with big ifr_addr's */
- for (cp = inbuf; cp < cplim;
- cp += sizeof (ifr->ifr_name) + size(ifr->ifr_addr)) {
- ifr = (struct ifreq *)cp;
- if (ifr->ifr_addr.sa_family != AF_INET)
+ for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
+ if (ifa->ifa_addr->sa_family != AF_INET)
continue;
if (!ntp)
ntp = (struct netinfo*)malloc(sizeof(struct netinfo));
- bzero(ntp,sizeof(*ntp));
- ntp->my_addr=((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
+ bzero(ntp, sizeof(*ntp));
+ ntp->my_addr=((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
ntp->status = NOMASTER;
- ifreq = *ifr;
- ifreqf = *ifr;
- if (ioctl(sock, SIOCGIFFLAGS, (char *)&ifreqf) < 0) {
- perror("get interface flags");
+ if ((ifa->ifa_flags & IFF_UP) == 0)
continue;
- }
- if ((ifreqf.ifr_flags & IFF_UP) == 0)
- continue;
- if ((ifreqf.ifr_flags & IFF_BROADCAST) == 0 &&
- (ifreqf.ifr_flags & IFF_POINTOPOINT) == 0) {
+ if ((ifa->ifa_flags & IFF_BROADCAST) == 0 &&
+ (ifa->ifa_flags & IFF_POINTOPOINT) == 0) {
continue;
}
- ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr = ntp->my_addr;
- if (ioctl(sock, SIOCGIFNETMASK, (char *)&ifreq) < 0) {
- perror("get netmask");
- continue;
- }
+ ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr = ntp->my_addr;
ntp->mask = ((struct sockaddr_in *)
- &ifreq.ifr_addr)->sin_addr.s_addr;
-
- if (ifreqf.ifr_flags & IFF_BROADCAST) {
- ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr =
- ntp->my_addr;
- if (ioctl(sock, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
- perror("get broadaddr");
- continue;
- }
- ntp->dest_addr = *(struct sockaddr_in *)&ifreq.ifr_broadaddr;
+ ifa->ifa_netmask)->sin_addr.s_addr;
+
+ if (ifa->ifa_flags & IFF_BROADCAST) {
+ ntp->dest_addr = *(struct sockaddr_in *)ifa->ifa_broadaddr;
/* What if the broadcast address is all ones?
* So we cannot just mask ntp->dest_addr. */
ntp->net = ntp->my_addr;
ntp->net.s_addr &= ntp->mask;
} else {
- ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr =
- ntp->my_addr;
- if (ioctl(sock, SIOCGIFDSTADDR,
- (char *)&ifreq) < 0) {
- perror("get destaddr");
- continue;
- }
- ntp->dest_addr = *(struct sockaddr_in *)&ifreq.ifr_dstaddr;
+ ntp->dest_addr = *(struct sockaddr_in *)ifa->ifa_dstaddr;
ntp->net = ntp->dest_addr.sin_addr;
}
@@ -386,7 +340,7 @@ main(int argc, char **argv)
fprintf(stderr, "timed: no network usable\n");
exit(1);
}
- free(inbuf);
+ freeifaddrs(ifap);
/* election timer delay in secs. */
delay2 = casual(MINTOUT, MAXTOUT);