如何使用“pcap_lookupdev()”在 libpcap 中?
代码找不到任何设备,我想知道 pcap_lookupdev() 是做什么的?谢谢
#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);
}
}
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
pcap_lookupdev 似乎只返回它可以找到的第一个设备(如果有),除了环回设备。
你以 root 身份运行它吗?普通用户将不被允许打开或检查这些设备。
就我个人而言,我发现 pcap_lookupdev 相当无用,因为您无法真正控制它为您提供的设备。
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.
当你编译pcap编程时。您需要在具有root权限的程序文件末尾提及
-lpcap
。例如。这里,#表示root用户,gcc是GNU编译器集合,-lpcap表示使用libpcap库
When you are compiling the pcap programming. You need to mention
-lpcap
at the end of the programe file with root privilege. For example.Here, # indicate root user, gcc is GNU Compiler Collection, -lpcap indicate use libpcap library
代码必须以 root 身份编译、链接和执行:
对我有用。
The code has to be compiled, linked and executed as root:
worked for me.