【问题标题】:pcap_lookupdev() is deprecated l. How to resolve itpcap_lookupdev() 已弃用 l。如何解决
【发布时间】:2020-10-17 09:38:04
【问题描述】:

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

}

这是代码。

【问题讨论】:

    标签: ubuntu tcpdump libpcap


    【解决方案1】:

    为什么不推荐使用它?

    引用 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() 遇到的错误是什么?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-20
      • 2011-04-10
      • 2018-01-30
      • 2020-06-23
      • 2020-03-04
      • 2016-09-12
      相关资源
      最近更新 更多