-# $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
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}
-/* $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
#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 <sys/param.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+#ifdef HAVE_IFADDRS_H
+#include <ifaddrs.h>
+#endif
#include "pcap-int.h"
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;
device[sizeof(device) - 1] = '\0';
free(ibuf);
return (device);
+#endif
}
int