From b85261abcd57db7dac50c2f10dce1d48bae6f3ae Mon Sep 17 00:00:00 2001 From: itojun Date: Thu, 13 Apr 2000 05:55:19 +0000 Subject: [PATCH] use getifaddrs, to avoid copmlicated align constraint in SIOCGIFCONF. --- lib/libpcap/Makefile | 4 ++-- lib/libpcap/inet.c | 55 ++++++++++++++++++++++++++++++++++++++++++-- 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 f9f4cb87eaa..2f8ef172fa7 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: /home/cvs/src/lib/libpcap/inet.c,v 1.11 1999/07/20 04:49:55 deraadt Exp $ (LBL)"; + "@(#) $Header: /home/cvs/src/lib/libpcap/inet.c,v 1.12 2000/04/13 05:55:19 itojun Exp $ (LBL)"; #endif #include @@ -62,6 +62,9 @@ struct rtentry; #include #include #include +#ifdef HAVE_IFADDRS_H +#include +#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 -- 2.20.1