关于libpcap的pcap_loop传递参数()回调 [英] Passing an argument on libpcap pcap_loop() callback
问题描述
由于我想提出一些测试与 libpcap的和一个小C程序中,我想从主()来got_packet传递一个结构()。读libpcap的教程后,我发现这一点:
)的原型pcap_loop(是
如下:
块引用>INT pcap_loop(pcap_t * P,INT CNT,pcap_handler回调,u_char *用户)
最后一个参数是一些有用的
应用,但很多时候是简单地
设置为NULL。假设我们有争论
我们自己的,我们想发送给我们
回调函数,除了
该pcap_loop参数()发送。这个
是我们做到这一点。很明显,你必须
强制转换为一个u_char指针,以确保
结果使其有正确的;
我们将在后面看到,PCAP利用
的一些非常有趣的方式
传递信息的形式
u_char指针。
块引用>因此根据这一点,有可能使用pcap_loop的参数编号4发送结构中got_packet()()。但是,试图后,我得到一个错误。
下面是我的(窃听)code:
INT主(INT ARGC,字符** argv的)
{
/ *一些线code的,并不重要* / / * DEF。结构:* /
typedef结构_configuration配置;
结构_configuration {
INT ID;
焦炭标题[255];
}; /* 在里面。结构:* /
配置的conf [2] = {
{0,富},
{1,酒吧}}; / *使用pcap_loop与got_packet回调:* /
pcap_loop(手柄,num_packets,got_packet,&安培; CONF);
}无效got_packet(u_char * ARGS,常量结构pcap_pkthdr *头,常量u_char *包)
{
/ *此行不工作:* /
的printf(测试数:%d \\ n,* ARGS [0] - > ID);
}我得到这个错误类型的一些试验后:
GCC -c got_packet.c -o got_packet.o
got_packet.c:在函数'got_packet:
got_packet.c:25:错误:无效的类型参数 - >'你看我怎么可以为了编辑这个code通过的的conf 的(有是配置结构的数组)在got_packet()函数?
任何帮助非常感谢。
问候
解决方案您需要定义的主外)的结构(和铸造 ARGS 在got_packet()这样的:
配置* CONF =(配置*)ARGS;
的printf(测试数:%d \\ n,CONF [0] .ID);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
解决方案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);
这篇关于关于libpcap的pcap_loop传递参数()回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!