gpt4 book ai didi

pcap_lookupdev() is deprecated l. How to resolve it(PCAP_lookupdev()已弃用L,如何解析)

转载 作者:bug小助手 更新时间:2023-10-25 19:39:25 30 4
gpt4 key购买 nike



Since lookupdev has been deprecated in libcap >=1.9, I am facing an error on a code written in v1.8. I have not been able to resolve it.
Suggestion is I use pcap_findalldevs but I am getting an error.

由于在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;

}

}


This is the code.

这就是密码。


更多回答
优秀答案推荐

Why is it deprecated?

为什么它会被弃用?


To quote the pcap_lookupdev man page:

引用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.

What should you do instead?

相反,你应该怎么做呢?


To quote the pcap_lookupdev man page:

引用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.)

On every platform except for Windows, pcap_lookupdev() calls pcap_findalldevs() and returns the first device (unless it's a loopback device), so if pcap_findalldevs() isn't working, pcap_lookupdev() won't work either. What is the error you're getting with pcap_findalldevs()?

在除Windows之外的所有平台上,pCap_lookupdev()都会调用pCap_findalldevs()并返回第一个设备(除非它是环回设备),因此如果pCap_findalldevs()不起作用,pCap_lookupdev()也不会起作用。使用pCap_findalldevs()得到的错误是什么?



Grab the name from the device you want


device = all_devs->name;

Example


int main(int argc, char *argv[]) {
char *device;
char error_buffer[PCAP_ERRBUF_SIZE];
pcap_t *handle;

pcap_if_t *all_devs;

if(pcap_findalldevs(&all_devs, error_buffer) == -1){
fprintf(stderr, "error finding devices");
return 1;
}

device = all_devs->name;
//or loop through all_devs to find the one you want

if (all_devs == NULL) {
printf("Error finding devices: %s\n", error_buffer);
return 1;
}

/* Open device for live capture */
handle = pcap_open_live(
device,
BUFSIZ,
0,
timeout_limit,
error_buffer
);

...

pcap_freealldevs(all_devs);
}

Explanation


Parse through the list and grab the name from the device you want and pass it to pcap_open_live().
The first argument for pcap_openlive() is just a string(char *) indicating the device name.

解析列表并从您想要的设备中获取名称,并将其传递给pCap_open_live()。PCap_Openlive()的第一个参数只是一个表示设备名称的字符串(char*)。


pcap_findalldevs() returns a list(pointer to the first device) of pcap_if_t(structure for each device). The datastructure has members: next, name,description, addresses, flags.

Pcap_findalldevs()返回pcapif_t(每个设备的结构)的列表(指向第一个设备的指针)。该数据结构具有成员:Next、名称、描述、地址、标志。


Each element of the list is of type pcap_if_t, and has the following members:

该列表的每个元素都是pCap_if_t类型,并具有以下成员:



next if not NULL, a pointer to the next element in the list; NULL for the last element of the list




name a pointer to a string giving a name for the device to pass to pcap_open_live()




description if not NULL, a pointer to a string giving a human-readable description of the device




addresses a pointer to the first element of a list of network addresses for the device, or NULL if the device has no addresses



You can read more here

您可以在此处阅读更多内容


Compile your program:


gcc capture.c -lpcap
sudo ./a.out

更多回答

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