use getifaddrs, instead of SIOCGIFCONF.

sync with more-recent LBL 0.4, about loopback interface detection
(/^lo[0-9]?$/).

CAVEAT: with GENERIC kernel on laptops laptops, pcap_lookupdev would almost
always pick eon0 as the interface, and fails because eon0 has no bpf
attachment.  we may want to change pcap_lookup{,dev} to check if the
interface has bpf attachment or not.

almost in sync with tcpdump.org source code tree.
This commit is contained in:
itojun 2000-04-13 05:14:19 +00:00
parent b5a6411fbe
commit cf9ebfbd63
2 changed files with 59 additions and 5 deletions

View File

@ -1,4 +1,4 @@
# $NetBSD: Makefile,v 1.19 1999/07/02 16:03:41 simonb Exp $
# $NetBSD: Makefile,v 1.20 2000/04/13 05:14:19 itojun Exp $
LIB= pcap
MAN= pcap.3
@ -9,7 +9,9 @@ CPPFLAGS+=-I. -I${.CURDIR} -DYYBISON
CPPFLAGS+=-DINET6
CPPFLAGS+=-DHAVE_MALLOC_H=1 -DHAVE_SYS_IOCCOM_H=1 -DHAVE_SYS_SOCKIO_H=1
CPPFLAGS+=-DHAVE_ETHER_HOSTTON=1 -DHAVE_STRERROR=1 -DHAVE_SOCKADDR_SA_LEN=1
CFPPLAGS+=-DLBL_ALIGN=1
CPPFLAGS+=-DHAVE_IFADDRS_H=1
# used in no place
#CPPFLAGS+=-DLBL_ALIGN=1
LPREFIX=pcap_
YPREFIX=pcap_
YHEADER=1

View File

@ -1,4 +1,4 @@
/* $NetBSD: inet.c,v 1.7 2000/01/13 17:14:56 itojun Exp $ */
/* $NetBSD: inet.c,v 1.8 2000/04/13 05:14:19 itojun Exp $ */
/*
* Copyright (c) 1994, 1995, 1996, 1997
@ -39,7 +39,7 @@
static const char rcsid[] =
"@(#) Header: inet.c,v 1.21 97/07/17 14:24:58 leres Exp (LBL)";
#else
__RCSID("$NetBSD: inet.c,v 1.7 2000/01/13 17:14:56 itojun Exp $");
__RCSID("$NetBSD: inet.c,v 1.8 2000/04/13 05:14:19 itojun Exp $");
#endif
#endif
@ -67,6 +67,9 @@ struct rtentry;
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef HAVE_IFADDRS_H
#include <ifaddrs.h>
#endif
#include "pcap-int.h"
@ -79,7 +82,8 @@ struct rtentry;
#ifdef IFF_LOOPBACK
#define ISLOOPBACK(p) ((p)->ifr_flags & IFF_LOOPBACK)
#else
#define ISLOOPBACK(p) (strcmp((p)->ifr_name, "lo0") == 0)
#define ISLOOPBACK(p) ((p)->ifr_name[0] == 'l' && (p)->ifr_name[1] == 'o' && \
(isdigit((p)->ifr_name[2]) || (p)->ifr_name[2] == '\0'))
#endif
/*
@ -91,6 +95,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;
register struct ifreq *ifrp, *ifend, *ifnext, *mp;
@ -172,6 +223,7 @@ pcap_lookupdev(errbuf)
(void)strncpy(device, mp->ifr_name, sizeof(device) - 1);
device[sizeof(device) - 1] = '\0';
return (device);
#endif
}
int