2

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.

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.

Volpone
  • 41
  • 3

1 Answers1

0

Why is it deprecated?

To quote the pcap_lookupdev man page:

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:

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()?

user13951124
  • 176
  • 4