How to use the "pcap_lookupdev()" in libpcap?

why picture why · Sep 19, 2010 · Viewed 9k times · Source

The code cant find any device, I want to know what does pcap_lookupdev() do ? thanks

#include <pcap.h>
int main(int argc, char *argv[])
{
    pcap_t *handle;

    char *dev;// = "eth0";
    char errbuf[PCAP_ERRBUF_SIZE];
    dev = pcap_lookupdev(errbuf);
    if (dev == NULL) {
        fprintf(stderr, "Couldn't find default device: %s\n", errbuf);
        return(2);
    }
    printf("Device: %s\n", dev);
    return(0);

    handle = pcap_open_live(dev, BUFSIZ, 1, 1000, errbuf);
    if (handle == NULL) {
        fprintf(stderr, "Couldn't open device %s: %s\n", dev, errbuf);
        return(2);
    }


}

Answer

nos picture nos · Sep 19, 2010

pcap_lookupdev seems to just return the first device it can find(if any) except the loopback device.

Do you run this as root ? Normal users won't be allowed to open or inspect these devices.

Personally I find pcap_lookupdev rather useless as you don't really have control over what device it gives you.