views:

462

answers:

1

Disclaimer: This is for a homework assignment, but the question is not regarding the assignment, just about general syntax weirdness.

I'm trying to use libpcap in the context of a much larger program, but when I try to get the packet header and data for each packet gcc complains that the third parameter to pcap_next_ex is of an incompatible pointer type. Here's some sample code to see what I'm talking about:

#include <stdio.h>
#include <stdlib.h>
#include <pcap.h>

int main()
{
    pcap_t *pcap;
    char pcapErr[PCAP_ERRBUF_SIZE];
    struct pcap_pkthdr *pktHeader;
    u_char *pktData;

    pcap = pcap_open_offline("somefile.pcap", pcapErr);
    if (pcap == NULL)
    {
        fprintf(stderr, "pcap_open_offline failed: %s\n", pcapErr);
        exit(EXIT_FAILURE);
    }

    while (pcap_next_ex(pcap, &pktHeader, &pktData) == 1)
    {
        // do things here
    }

    pcap_close(pcap);

    return EXIT_SUCCESS;
}

The man pages indicated that the prototype for pcap_next_ex() is:

int pcap_next_ex(pcap_t *p, struct pcap_pkthdr **pkt_header, const u_char **pkt_data)

How exactly is what I'm passing an incompatible pointer type? Thanks.

+6  A: 

Change the declaration of pktData to read:

const u_char *pktData;

and gcc should stop complaining.

diciu
Bob: it's also nice to vote up the correct answer.
j_random_hacker