tags:

views:

270

answers:

3

Hi. I want to capture packets going out of my machine, and I'm using libpcap (version 1.0.0-1) for the same. The problem is, that a basic program like this -

#include <stdio.h>
#include <pcap.h>

int main(int argc, char *argv[]) {
    char *dev, errbuf[PCAP_ERRBUF_SIZE];
    dev = pcap_lookupdev(errbuf);
    if (dev == NULL) {
        fprintf(stderr, "%s\n", errbuf);
        return (2);
    }
    printf("Device : %s\n", dev);
    return (0);
}

does not seem to display the wireless interface. Everytime I compile and run the program, it detects eth0. How can I make it capture the wireless interfaces as well?

+2  A: 

try using pcap_findalldevs(). i guess pcap_lookupdev() matches the first entry in the list is suitable interfaces

Gautham Ganapathy
Yes. And no need to guess, it is in the man page: "pcap_lookupdev() returns a pointer to a network device suitable for use with pcap_open_live() and pcap_lookupnet()"
bortzmeyer
+2  A: 

pcap_lookupdev() returns the default networking device on the system, which is usually the first device listed. pcap_findalldevs() returns an enumeration of all devices in the system, which you can use to select a device and capture from it.

mrcrassic
A: 

As others have stated, pcap_lookupdev() simply returns the first device found. You need to use pcap_findalldevs() to build a list of all available devices, then prompt the user to pick one (or let the user specify a number n on the command line, and then use the _n_th device).

But, if this is just a quick-and-dirty test program, you can find out the interface name and code it directly into your program. You can use ifconfig or tcpdump -D to find out the interface names on your system, then make a call like pcap_create("en1", errbuf).

benzado