Hello,
I'm not sure if this is the best place to post this, but I couldn't
find a mailing list specifically for libpcap, and this seemed like the
next best thing. If there's someplace better, let me know, and I'll
post this kind of stuff there next time.
Doing some work on Michael Toren's tcptraceroute (see
http://michael.toren.net/code/tcptraceroute/
), I was surprised to find that there wasn't any code in libpcap to
get a list of all interfaces on the system. To get this list, I ended
up basically copying a good chunk of inet.c, which introduced lots of
complexity and system dependencies into an otherwise straightforward
and portable program. I thought it would be nice if libpcap provided
an interface to it's knowledge of system interfaces, so I threw
something together.
This patch provides a simple function, pcap_findalldevs(), which
returns a NULL-terminated array of pointers to simple interface
structures. These structures try hard to be easy to support on all
platforms, and easy to use for a programmer.
The patch, against libpcap 0.6.2, is available at:
http://www.tir.com/~sgifford/libpcap/libpcap-0.6.2-sg2.patch
Additionally, the patch cleans up and consolidates the error handling
for the code from pcap_lookupdev() which was moved into
pcap_findalldevs(), fixing a few "XXX"-marked items.
pcap_lookupdev() and pcap_lookupnet() are re-implemented in terms of
pcap_findalldevs().
The patch uses a "struct ifreq *" and a "struct sockaddr *t" in the
interface structure, so <net/if.h> and <sys/socket.h> are #include'd
in "pcap.h". Because OpenBSD's <net/if.h> doesn't like to be included
twice, in all files which used to #include <net/if.h>, I've just moved
the include of <pcap.h> or "pcap-inet.h" to the top of their include
list. That minor change makes up a good portion of this patch.
Finally, a sample program "iflist.c" is created. This serves as a
demonstration and test program for this feature; it can be compiled
like:
# Also include -lsocket -lnsl for Solaris
gcc -I. iflist.c libpcap.a
The structure used to store the interface looks like this:
struct pcap_inet_if {
#ifdef IF_NAMESIZE
char name[IF_NAMESIZE+1];
#else
char name[sizeof(unused_ifreq->ifr_name)+1];
#endif
u_int instance;
struct sockaddr *addr, *netmask;
u_int is_up, is_loopback;
};
name is the interface name (as used elsewhere in libpcap), instance is
the instance number (IE, 0 for "eth0", 1 for "eth1"), an address and
netmask are stored in addr and netmask, and is_up and is_loopback are
boolean values indicating whether the interface is up and is a
loopback interface, respectively.
pcap_findalldevs() returns a NULL-terminated array of these
structures. Here's how you would use it:
#include <pcap.h>
pcap_inet_if_t **alldevs;
pcap_inet_if_t *d;
char errbuf[PCAP_ERRBUF_SIZE+1];
alldevs = pcap_findalldevs(errbuf);
if (!alldevs)
{
fprintf(stderr,"Error in pcap_findalldevs: %s\n",errbuf);
exit(1);
}
for(;d=*alldevs;alldevs++)
{
ifprint(d);
}
The patch and test code have been tested on Linux 2.2.17/glibc2.1.92,
OpenBSD 2.9, and Solaris 2.6.
Please let me know if you have any questions or comments, and if you
think this is or isn't something I should continue working on for
libpcap,
-----ScottG.
-
This is the TCPDUMP workers list. It is archived at
http://www.tcpdump.org/lists/workers/index.html
To unsubscribe use mailto:[EMAIL PROTECTED]?body=unsubscribe