Passing an argument on libpcap pcap_loop() callback

旧城冷巷雨未停 提交于 2019-12-01 19:17:21

You need to define the structure outside of main() and cast args in got_packet() like:

Configuration *conf = (Configuration *) args;
printf ("test: %d\n", conf[0].id);

I rewrite Your code, it is now compile without any error:

#include <pcap.h> 

typedef struct {
  int id;
  char title[255];
} Configuration;

void got_packet( Configuration args[], const struct pcap_pkthdr *header, const u_char *packet){
  (void)header, (void)packet;
  printf("test: %d\n", args[0].id);
}

int main(void){
  Configuration conf[2] = {
    {0, "foo"},
    {1, "bar"}};

  pcap_loop(NULL, 0, (pcap_handler)got_packet, (u_char*)conf);
}

To compile the above code.

install libpcap --> sudo apt-get install libpcap0.8-dev

then --> gcc got_packet.c -lpcap -o got_packet.o

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!