开发者

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

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);
    }开发者_运维百科


}


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.


When you are compiling the pcap programming. You need to mention -lpcap at the end of the programe file with root privilege. For example.

#gcc YourFileName.c -lpcap
#./a.out
or
#gcc -Wall -o YourFileName YourFileName.c -lpcap
#./YourFileName

Here, # indicate root user, gcc is GNU Compiler Collection, -lpcap indicate use libpcap library


The code has to be compiled, linked and executed as root:

sudo gcc -o dev-find dev-find.c lpcap

worked for me.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜