在 libpcap pcap_loop() 回调上传递参数

发布于 2024-08-11 07:44:13 字数 1536 浏览 6 评论 0原文

因为我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寂寞清仓 2024-08-18 07:44:13

我重写了你的代码,现在编译没有任何错误:

#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);
}

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);
}
落叶缤纷 2024-08-18 07:44:13

您需要在 main() 之外定义结构,并在 got_packet() 中强制转换 args ,如下所示:

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

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);
风铃鹿 2024-08-18 07:44:13

编译上面的代码。

安装 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文