0

I added the following function to the sniffex code (http://www.tcpdump.org/sniffex.c):

typedef struct pcap_stat mystat;

mystat *mystatp;

/* Put the interface in statstics mode */
if(pcap_stats(handle, mystatp) < 0)
{
    fprintf(stderr,"\nError setting the mode.\n");
    pcap_close(handle);
    /* Free the device list */
    return;
}

Sniffex code is working fine for me - but as soon as I add this code, I am getting a segmentation fault error :(

Can anyone please help me out ?

Thanks a ton.

dev
  • 11,071
  • 22
  • 74
  • 122
  • Just adding a bit of information... I am trying to emulate this code http://www.winpcap.org/docs/docs_40_2/html/group__wpcap__tut9.html on lipcap. There must be some libpcap way to do it out, right ? – dev Jun 04 '10 at 14:45

1 Answers1

3

I believe you forgot to allocate memory for mystat.

Try this:

typedef struct pcap_stat mystat;

...

mystat actualStat; /* allocate memory for mystat on stack - you can also do it on the heap by malloc-ing */
mystat *mystatp = &actualStat; /* use allocated memory */

/* Put the interface in statistics mode */
if(pcap_stats(handle, mystatp) < 0)
{
    fprintf(stderr,"\nError setting the mode.\n");
    pcap_close(handle);
    /* Free the device list */
    return;
}

In Pcap.Net I use pcap_stats_ex() but it's probably only available on WinPcap and not on libpcap.

brickner
  • 6,595
  • 3
  • 41
  • 54
  • 1
    Or, even better, `pcap_stats(handle, &actualStat)` - if a function takes an "XXX \*" as an argument, you don't have to pass it an "XXX \*" variable, you can pass it a pointer to an "XXX" variable, with the `&` operator used in the call. –  Oct 19 '12 at 20:48