【发布时间】:2021-11-25 14:11:23
【问题描述】:
我正在使用 c 和 libpcap 函数编写一个数据包嗅探器。以下是源代码。
#include <pcap.h>
#include <stdlib.h>
#include <stdio.h>
#include "YYY.h"
void pcap_fatal(const char *failed_in, const char *errbuf){
printf("Fata Error in %s: %s\n", failed_in, errbuf);
exit(1);
}
int main(){
struct pcap_pkthdr header;
const u_char *packet;
char errbuf[PCAP_ERRBUF_SIZE];
char *device;
pcap_t *pcap_handle;
int i;
device = pcap_lookupdev(errbuf);
if(device == NULL)
pcap_fatal("pcap_lookupdev", errbuf);
printf("Sniffing of device %s\n", device);
pcap_handle = pcap_open_live(device, 4096, 1, 0, errbuf);
for(i=0;i<3;i++){
packet = pcap_next(pcap_handle, &header);
printf("Got a %d bute packet\n", header.len);
dump(packet, header.len);
}
pcap_close(pcap_handle);
}
但是,当我尝试使用 gcc 编译它时,它会给出以下警告,如下所示。
libcap_sniff.c: In function ‘main’:
libcap_sniff.c:20:2: warning: ‘pcap_lookupdev’ is deprecated: use 'pcap_findalldevs' and use the first device [-Wdeprecated-declarations]
20 | device = pcap_lookupdev(errbuf);
| ^~~~~~
In file included from /usr/include/pcap.h:43,
from libcap_sniff.c:1:
/usr/include/pcap/pcap.h:394:16: note: declared here
394 | PCAP_API char *pcap_lookupdev(char *)
| ^~~~~~~~~~~~~~
谁能帮我解决这个问题。我还不知道什么是弃用警告。
【问题讨论】:
-
您是否阅读了警告信息?你有什么不明白的?
-
该库的作者已标记该函数以警告您不应再使用该函数。所以一般来说你应该重新检查 API 看看是否有更好的选择。
-
一般来说,这意味着提到的函数现在被认为是不安全的,并且建议使用另一个。
-
@Damien 弃用函数通常并不意味着弃用的函数是“不安全”。
标签: c pcap packet-sniffers sniffing