dect
/
libpcap
Archived
13
0
Fork 0

use getifaddrs whenever available.

SIOCGIFCONF is very complex API, so it would be good to avoid it whenever
possible.
This commit is contained in:
itojun 2000-02-23 11:39:44 +00:00
parent b52c6e13ff
commit 03cc04a438
3 changed files with 53 additions and 6 deletions

4
configure vendored
View File

@ -1,6 +1,6 @@
#! /bin/sh
# From configure.in Revision: 1.6
# From configure.in Revision: 1.72
@ -1192,7 +1192,7 @@ else
fi
echo "$ac_t""$CPP" 1>&6
for ac_hdr in malloc.h sys/ioccom.h sys/sockio.h
for ac_hdr in malloc.h sys/ioccom.h sys/sockio.h ifaddrs.h
do
ac_safe=`echo "$ac_hdr" | sed 'y%./+-%__p_%'`
echo $ac_n "checking for $ac_hdr""... $ac_c" 1>&6

View File

@ -1,4 +1,4 @@
dnl @(#) $Header: /tcpdump/master/libpcap/configure.in,v 1.72 2000-01-08 06:08:49 assar Exp $ (LBL)
dnl @(#) $Header: /tcpdump/master/libpcap/configure.in,v 1.73 2000-02-23 11:39:44 itojun Exp $ (LBL)
dnl
dnl Copyright (c) 1994, 1995, 1996, 1997
dnl The Regents of the University of California. All rights reserved.
@ -6,7 +6,7 @@ dnl
dnl Process this file with autoconf to produce a configure script.
dnl
AC_REVISION($Revision: 1.72 $)
AC_REVISION($Revision: 1.73 $)
AC_INIT(pcap.c)
AC_CANONICAL_SYSTEM
@ -19,7 +19,7 @@ fi
AC_LBL_C_INIT(V_CCOPT, V_INCLS)
AC_CHECK_HEADERS(malloc.h sys/ioccom.h sys/sockio.h)
AC_CHECK_HEADERS(malloc.h sys/ioccom.h sys/sockio.h ifaddrs.h)
AC_LBL_FIXINCLUDES

49
inet.c
View File

@ -33,7 +33,7 @@
#ifndef lint
static const char rcsid[] =
"@(#) $Header: /tcpdump/master/libpcap/inet.c,v 1.26 2000-01-14 23:55:31 mcr Exp $ (LBL)";
"@(#) $Header: /tcpdump/master/libpcap/inet.c,v 1.27 2000-02-23 11:39:45 itojun Exp $ (LBL)";
#endif
#include <sys/param.h>
@ -60,6 +60,9 @@ struct rtentry;
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef HAVE_IFADDRS_H
#include <ifaddrs.h>
#endif
#include "pcap-int.h"
@ -85,6 +88,49 @@ 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)sprintf(errbuf, "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 (strcmp(ifa->ifa_name, "lo0") == 0)
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)strcpy(errbuf, "no suitable device found");
free(ifap);
return (NULL);
}
(void)strncpy(device, mp->ifa_name, sizeof(device) - 1);
device[sizeof(device) - 1] = '\0';
free(ifap);
return (device);
#else
register int fd, minunit, n;
register char *cp;
register struct ifreq *ifrp, *ifend, *ifnext, *mp;
@ -184,6 +230,7 @@ pcap_lookupdev(errbuf)
device[sizeof(device) - 1] = '\0';
free(buf);
return (device);
#endif
}
int