在 libpcap pcap_loop() 回调上传递参数
因为我想使用 libpcap 和一个小型 C 程序进行一些测试,所以我尝试将结构从 main() 传递到 got_packet()。阅读 libpcap 教程后,我发现了这一点:
pcap_loop() 的原型是 如下:
int pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
最后一个参数在某些情况下很有用 应用程序,但很多时候只是简单 设置为 NULL。假设我们有争论 我们自己的,我们希望发送到我们的 回调函数,除了 pcap_loop() 发送的参数。这 是我们做这件事的地方。显然,你必须 类型转换为 u_char 指针以确保 结果正确地到达那里; 正如我们稍后将看到的,pcap 使用 一些非常有趣的手段 以a的形式传递信息 u_char 指针。
因此,根据this,可以使用pcap_loop()的参数号4来发送got_packet()中的结构。但尝试后,我收到错误。
这是我的(有缺陷的)代码:
int main(int argc, char **argv)
{
/* some line of code, not important */
/* def. of the structure: */
typedef struct _configuration Configuration;
struct _configuration {
int id;
char title[255];
};
/* init. of the structure: */
Configuration conf[2] = {
{0, "foo"},
{1, "bar"}};
/* use pcap_loop with got_packet callback: */
pcap_loop(handle, num_packets, got_packet, &conf);
}
void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet)
{
/* this line don't work: */
printf("test: %d\n", *args[0]->id);
}
经过一些测试后,我得到了这种错误:
gcc -c got_packet.c -o got_packet.o
got_packet.c: In function ‘got_packet’:
got_packet.c:25: error: invalid type argument of ‘->’
你知道如何编辑此代码以便在 got_packet 中传递 conf (带有配置结构数组) () 功能?
非常感谢您的帮助。
问候
Because I would like to make some tests with the libpcap and a small C program, I am trying to pass a structure from main() to got_packet(). After reading the libpcap tutorial, I had found this:
The prototype for pcap_loop() is
below:
int pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
The last argument is useful in some
applications, but many times is simply
set as NULL. Suppose we have arguments
of our own that we wish to send to our
callback function, in addition to the
arguments that pcap_loop() sends. This
is where we do it. Obviously, you must
typecast to a u_char pointer to ensure
the results make it there correctly;
as we will see later, pcap makes use
of some very interesting means of
passing information in the form of a
u_char pointer.
So according to this, it is possible to send the structure in got_packet() using the argument number 4 of pcap_loop(). But after trying, I get an error.
Here is my (bugged) code:
int main(int argc, char **argv)
{
/* some line of code, not important */
/* def. of the structure: */
typedef struct _configuration Configuration;
struct _configuration {
int id;
char title[255];
};
/* init. of the structure: */
Configuration conf[2] = {
{0, "foo"},
{1, "bar"}};
/* use pcap_loop with got_packet callback: */
pcap_loop(handle, num_packets, got_packet, &conf);
}
void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet)
{
/* this line don't work: */
printf("test: %d\n", *args[0]->id);
}
I get this kind of error after some tests:
gcc -c got_packet.c -o got_packet.o
got_packet.c: In function ‘got_packet’:
got_packet.c:25: error: invalid type argument of ‘->’
Do you see how can I edit this code in order to pass conf (with is a array of configuration structure) in got_packet() function?
Many thanks for any help.
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我重写了你的代码,现在编译没有任何错误:
I rewrite Your code, it is now compile without any error:
您需要在 main() 之外定义结构,并在 got_packet() 中强制转换 args ,如下所示:
You need to define the structure outside of main() and cast args in got_packet() like:
编译上面的代码。
安装 libpcap --> sudo apt-get install libpcap0.8-dev
然后 --> gcc got_packet.c -lpcap -o got_packet.o
To compile the above code.
install libpcap --> sudo apt-get install libpcap0.8-dev
then --> gcc got_packet.c -lpcap -o got_packet.o