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.
CodePudding user response:
Use pcap_findalldevs
instead of pcap_lookupdev
.
Deprecating software means that it may be usable but is regarded as obsolete and that it should be avoided. Usually deprecated software has been replaced by an alternative.
GCC has features for marking functions has deprecated. When a program uses a function that is marked as deprecated, GCC warns you about it, so that you can modify the code to use more modern functions. Deprecated functions may be removed in future versions of the software they are part of.
This documentation indicates that you can use pcap_findalldevs
to perform the function of the pcap_lookupdev
routine.
CodePudding user response:
Beware: the beginning of the question is about the generic warning. The correct answer to the actual question is later in the post.
Although the standard only defines the #error
directive, most common implementations accepts a non fatal #warning
one. They can be used by libraries writer to raise errors or warnings when something can be detected at compilation time.
This just means that the guys from libpcap have deprecated usage of pcap_lookupdev
and that it is likely to be removed in a later version. They just warn users of their library to stop using it in new code.
But here a different mechanism is involved. Gnu C compiler (but maybe others) have a specific attribute to indicate deprecation. It is intended to be used with the special option -Wdeprecated-declarations
to switch deprecation warnings on, and -Wno-deprecated-declaration
to switch them off.
CodePudding user response:
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.