gpt4 book ai didi

ubuntu - pcap_lookupdev() 已弃用 l。如何解决

转载 作者:行者123 更新时间:2023-12-04 18:33:17 25 4
gpt4 key购买 nike

由于在 libcap >=1.9 中已弃用 lookupdev,因此我在使用 v1.8 编写的代码时遇到错误。我无法解决它。
建议我使用 pcap_findalldevs 但我收到错误消息。

int sniffARPPackets(char* gateway, char* gateway_ipp)
{
strncpy(gtwy, gateway, 17);
strncpy(gateway_ip, gateway_ipp, 15);
int i;
char *dev;
char errbuf[PCAP_ERRBUF_SIZE];
pcap_t* descr;
const u_char *packet;
struct pcap_pkthdr hdr;
struct ether_header *eptr;
struct bpf_program fp;
bpf_u_int32 maskp;
bpf_u_int32 netp;

dev = pcap_lookupdev(errbuf);

if(dev == NULL) {
fprintf(stderr, "%s\n", errbuf);
exit(1);
}

pcap_lookupnet(dev, &netp, &maskp, errbuf);


descr = pcap_open_live(dev, BUFSIZ, 1,-1, errbuf);
if(descr == NULL) {
printf("pcap_open_live(): %s\n", errbuf);
exit(1);
}


if(pcap_compile(descr, &fp, "arp", 0, netp) == -1) {
fprintf(stderr, "Error calling pcap_compile\n");
exit(1);
}


if(pcap_setfilter(descr, &fp) == -1) {
fprintf(stderr, "Error setting filter\n");
exit(1);
}


pcap_loop(descr, -1, my_callback, NULL);
return 0;
}
这是代码。

最佳答案

为什么它被弃用?
引用 pcap_lookupdev 手册页:

BUGS
The pointer returned by pcap_lookupdev() points to a static buffer;
subsequent calls to pcap_lookupdev() in the same thread, or calls to
pcap_lookupdev() in another thread, may overwrite that buffer.

In WinPcap, this function may return a UTF-16 string rather than an
ASCII or UTF-8 string.
你应该怎么做?
引用 pcap_lookupdev 手册页:
DESCRIPTION
This interface is obsoleted by pcap_findalldevs(3PCAP). To find a
default device on which to capture, call pcap_findalldevs() and, if the
list it returns is not empty, use the first device in the list. (If
the list is empty, there are no devices on which capture is possible.)
在除 Windows 之外的所有平台上, pcap_lookupdev()来电 pcap_findalldevs()并返回第一个设备(除非它是环回设备),所以如果 pcap_findalldevs()不工作, pcap_lookupdev()也不会工作。您在使用 pcap_findalldevs() 时遇到什么错误? ?

关于ubuntu - pcap_lookupdev() 已弃用 l。如何解决,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64401044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com