gpt4 book ai didi

c - `user` 和 `pcap_loop` 中的 `pcap_dispatch` 参数是什么?

转载 作者:太空宇宙 更新时间:2023-11-04 07:51:47 26 4
gpt4 key购买 nike

我用 Google 搜索了很多,但我不知道 user 参数是什么用于 pcap_loop()。我在网上找到的最好的一个来自斯坦福大学(链接: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)*/
/* <b>If you are wondering what the user argument is all about, so am I!!</b> */
pcap_loop(descr,atoi(argv[1]),my_callback,NULL);

联机帮助页仅提及一次user 参数(在函数中的实际参数之外):

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

我觉得这没什么用。

我可以在 pcap_loop 调用中传递任何字符串,并在回调处理程序中成功打印出来。这是否只是调用者可以将随机字符串传递给处理程序的一种方式?

有谁知道这个参数应该用来做什么?

最佳答案

是的 - 这样您就可以在您的处理程序函数中传递您需要访问的任何自定义数据,因此您不需要全局变量来完成相同的任务。

例如

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

现在在 my_callback 中,您可以访问 something

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

(请注意,user 参数最好指定为 void* ,但出于遗留原因可能是 u_char* 类型)

关于c - `user` 和 `pcap_loop` 中的 `pcap_dispatch` 参数是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53344706/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com