Search code examples
cpcappacket-snifferssniffing

What is a deprecated warning?


I am writing a packet sniffer using c and libpcap functions. following is the source code.

 #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);
 }

However when I tried to compile it with gcc it gives the following warning as follows.

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 *)
      |                ^~~~~~~~~~~~~~

can anyone help me with this. I don't yet know what a deprecated warnning is.


Solution

  • gcc has a non-standard extension called function attributes which can be used for all manner of things, such as inlining or declaring functions in a certain memory section. They are used by writing __attribute__ (arg) after a function declaration (not after a function definition).

    One of these attributes is called deprecated, which tells gcc to give the application programmer a warning about an obsolete function getting called. Optionally, deprecated can be given a message argument. Example:

    #include <stdio.h>
    
    void foo (void) __attribute__((deprecated("consider using bar instead")));
    
    void foo (void)
    {
      puts("foo");
    }
    
    int main (void)
    {
      foo();
    }
    

    This gives:

    warning: 'foo' is deprecated: consider using bar instead [-Wdeprecated-declarations]
    foo();

    This warning is enabled by default. To shut it up, one needs to explicitly use the compiler option -Wno-deprecated-declarations.


    As for why your specific function is deprecated, you have to consult the (lacking) documentation for it. man doesn't mention why it is deprecated.