3

I have Google'd this a bunch, but I have no idea what the user parameter is for pcap_loop(). The best one I found online is from Stanford (link: http://yuba.stanford.edu/~casado/pcap/section3.html):

    /* allright here we call pcap_loop(..) and pass in our callback function */
    /* int pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user)*/
    /* If you are wondering what the user argument is all about, so am I!!   */
    pcap_loop(descr,atoi(argv[1]),my_callback,NULL);

The manpage only mentions the user parameter once (outside the actual parameters in the functions):

...three arguments: a u_char pointer which is passed in the user argument to pcap_loop() or pcap_dispatch()...

I find that very unhelpful.

I can pass any string in the invocation of pcap_loop and successfully print it out in the callback handler. Is this just meant to be a way the caller can pass a random string to the handler?

Does anyone know what this parameter is supposed to be used for?

Greg Schmit
  • 4,275
  • 2
  • 21
  • 36

1 Answers1

4

Yes - it's so you can pass in any custom data you need to access to from within your handler function, so you don't need a global variable to accomplish the same.

e.g.

struct my_struct something; 
...
pcap_loop(descr,atoi(argv[1]),my_callback, (u_char*)&something);

Now in my_callback you get access to the something

void my_callback(u_char *user, const struct pcap_pkthdr *h, const u_char *bytes) 
{
    struct my_struct *something = (struct my_struct *)user;
    ..
}

(Note, the user argument would be better specified as a void* , but is probably a u_char* type for legacy reasons)

nos
  • 223,662
  • 58
  • 417
  • 506