微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

不推荐使用pcap_lookupdev如何解决

如何解决不推荐使用pcap_lookupdev如何解决

由于在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",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,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()不起作用,则{{1} }也不起作用。 pcap_lookupdev()出现什么错误?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。