summaryrefslogtreecommitdiff
path: root/lib/libpcap
diff options
context:
space:
mode:
authorJun-ichiro itojun Hagino <itojun@cvs.openbsd.org>2000-04-13 05:55:20 +0000
committerJun-ichiro itojun Hagino <itojun@cvs.openbsd.org>2000-04-13 05:55:20 +0000
commitf54a169ac01105217cbf6e3dfa39f465f0bde571 (patch)
treec6354c1621d7169472859c9bdb894149a3b1ddd0 /lib/libpcap
parentfc2670cdf1872aaf42bd92717b9f579232b8c450 (diff)
use getifaddrs, to avoid copmlicated align constraint in SIOCGIFCONF.
Diffstat (limited to 'lib/libpcap')
-rw-r--r--lib/libpcap/Makefile4
-rw-r--r--lib/libpcap/inet.c55
2 files changed, 55 insertions, 4 deletions
diff --git a/lib/libpcap/Makefile b/lib/libpcap/Makefile
index 068f5346787..8b12773dd95 100644
--- a/lib/libpcap/Makefile
+++ b/lib/libpcap/Makefile
@@ -1,4 +1,4 @@
-# $OpenBSD: Makefile,v 1.11 1999/11/17 05:15:19 millert Exp $
+# $OpenBSD: Makefile,v 1.12 2000/04/13 05:55:19 itojun Exp $
# $NetBSD: Makefile,v 1.3 1996/05/10 21:54:24 cgd Exp $
LIB= pcap
@@ -15,7 +15,7 @@ MLINKS= pcap.3 pcap_open_live.3 pcap.3 pcap_open_offline.3 \
pcap.3 pcap_close.3 pcap.3 pcap_dump_close.3
DEFS= -DHAVE_SYS_IOCCOM_H -DHAVE_SYS_SOCKIO_H -DHAVE_ETHER_HOSTTON \
- -DHAVE_STRERROR -DHAVE_SOCKADDR_SA_LEN -DLBL_ALIGN
+ -DHAVE_STRERROR -DHAVE_SOCKADDR_SA_LEN -DLBL_ALIGN -DHAVE_IFADDRS_H
CFLAGS+=-I. -I${.CURDIR} -Dyylval=pcap_yylval ${DEFS}
diff --git a/lib/libpcap/inet.c b/lib/libpcap/inet.c
index 151fb05e61e..1a9a074dfc3 100644
--- a/lib/libpcap/inet.c
+++ b/lib/libpcap/inet.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: inet.c,v 1.11 1999/07/20 04:49:55 deraadt Exp $ */
+/* $OpenBSD: inet.c,v 1.12 2000/04/13 05:55:19 itojun Exp $ */
/*
* Copyright (c) 1994, 1995, 1996, 1997, 1998
@@ -35,7 +35,7 @@
#ifndef lint
static const char rcsid[] =
- "@(#) $Header: /cvs/OpenBSD/src/lib/libpcap/inet.c,v 1.11 1999/07/20 04:49:55 deraadt Exp $ (LBL)";
+ "@(#) $Header: /cvs/OpenBSD/src/lib/libpcap/inet.c,v 1.12 2000/04/13 05:55:19 itojun Exp $ (LBL)";
#endif
#include <sys/param.h>
@@ -62,6 +62,9 @@ struct rtentry;
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+#ifdef HAVE_IFADDRS_H
+#include <ifaddrs.h>
+#endif
#include "pcap-int.h"
@@ -86,6 +89,53 @@ char *
pcap_lookupdev(errbuf)
register char *errbuf;
{
+#ifdef HAVE_IFADDRS_H
+ struct ifaddrs *ifap, *ifa, *mp;
+ int n, minunit;
+ char *cp;
+ static char device[IF_NAMESIZE + 1];
+
+ if (getifaddrs(&ifap) != 0) {
+ (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
+ "getifaddrs: %s", pcap_strerror(errno));
+ return NULL;
+ }
+
+ mp = NULL;
+ minunit = 666;
+ for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
+ if ((ifa->ifa_flags & IFF_UP) == 0)
+ continue;
+#ifdef IFF_LOOPBACK
+ if ((ifa->ifa_flags & IFF_LOOPBACK) != 0)
+ continue;
+#else
+ if (strncmp(ifa->ifa_name, "lo", 2) == 0 &&
+ (ifa->ifa_name[2] == '\0' || isdigit(ifa->ifa_name[2]))) {
+ continue;
+ }
+#endif
+
+ for (cp = ifa->ifa_name; !isdigit(*cp); ++cp)
+ continue;
+ n = atoi(cp);
+ if (n < minunit) {
+ minunit = n;
+ mp = ifa;
+ }
+ }
+ if (mp == NULL) {
+ (void)strncpy(errbuf, "no suitable device found",
+ PCAP_ERRBUF_SIZE);
+ freeifaddrs(ifap);
+ return (NULL);
+ }
+
+ (void)strncpy(device, mp->ifa_name, sizeof(device) - 1);
+ device[sizeof(device) - 1] = '\0';
+ freeifaddrs(ifap);
+ return (device);
+#else
register int fd, minunit, n;
register char *cp, *ibuf = NULL, *nibuf;
register struct ifreq *ifrp, *ifend, *ifnext, *mp;
@@ -180,6 +230,7 @@ pcap_lookupdev(errbuf)
device[sizeof(device) - 1] = '\0';
free(ibuf);
return (device);
+#endif
}
int