0
votes

Having followed the examples http://www.tcpdump.org/pcap.htm, and exploring the documentation, I can't see what I have done wrong with the following code

// main.c
void got_packet(u_char *args, struct pcap_pkthdr *header, const u_char *packet);
/// ...snip...
void got_packet(u_char *args, struct pcap_pkthdr *header, const u_char *packet) {
  printf("Received Packet\n");
}
/// ...snip...
pcap_dispatch(handle, 100, got_packet, NULL);

to warrant the compiler warnings that I am seeing:

gcc -g -O3 -I../sensor/   -c -o sensordatad.o sensordatad.c
main.c: In function ‘start_capture’:
main.c:96:27: warning: initialization from incompatible pointer type [enabled by default]
main.c:146:3: warning: passing argument 3 of ‘pcap_dispatch’ from incompatible pointer     
                       type [enabled by default]

It might be that I'm being too fussy, it seems like every other piece of C code I have ever seen has a page full of compiler warnings when compiling, but I want to at least finish this code without warnings!

I've also tried doing this, to somehow typecast the function pointer, something I hadn't seen in any of the examples; but I felt it worth a shot:

pcap_handler callback = &got_packet;
/// ...snip...
pcap_dispatch(handle, 100, &callback, NULL);

This resulted in the equally perplexing error:

main.c: In function ‘start_capture’:
main.c:96:27: warning: initialization from incompatible pointer type [enabled by default]

It's the first time I've run across function pointers with special typedef'ed types, the man page for pcap_{loop,dispatch} reads:

NAME pcap_loop, pcap_dispatch - process packets from a live capture or savefile

SYNOPSIS #include

      typedef void (*pcap_handler)(u_char *user, const struct pcap_pkthdr *h,
                                  const u_char *bytes);

      int pcap_loop(pcap_t *p, int cnt,
              pcap_handler callback, u_char *user);
      int pcap_dispatch(pcap_t *p, int cnt,
              pcap_handler callback, u_char *user);
1

1 Answers

0
votes

Solved

The function signature should be:

void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet);

I had:

void got_packet(u_char *args, struct pcap_pkthdr *header, const u_char *packet);

I suppose in some ways the error makes sense?! But anyway Solved.