c - Pcap 不完全类型

标签 c linux sockets pcap

我正在尝试使用 pcap 为一个小项目查找 MAC 地址。截至目前,我正在使用的结构如下所示:

    struct ethernet_header
    {
         u_char dhost[6];
         u_char shost[6];
         u_short type;
    };

代码中的调用看起来像这样:

    void get_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet)
    {
         const struct ethernet_header *ethernet;
         const struct ip_header *ip;
         ethernet = (struct ethernet_header *)(packet);
         ip = (struct ip_header *)(packet + 16);

         printf("Destination MAC: %s\n", ethernet->dhost);
    }

我收到的错误是

error: dereferencing pointer to incomplete type

现在据我所知,数据包 var 正在正确初始化,因为它在代码的其他部分中使用没有问题。在 ip 结构的情况下,这也可以正常工作,没有错误。我知道那个特定地址正在加载什么我只是不知道发生了什么。任何人都有任何想法。

最佳答案

error: dereferencing pointer to incomplete type

您错过了在具有函数 void get_packet() 的 c 文件中包含定义 struct ethernet_header 的头文件。

错误是因为编译器无法看到结构的定义,很可能你只是向前声明它。但是,由于您取消引用指向结构的指针,编译器必须知道结构的布局,因此必须看到结构的定义。

所以只需包含它您需要在这个特定的 c 文件中包含包含结构定义的头文件。

关于c - Pcap 不完全类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9733536/

相关文章:

linux - 如何刷新原始 AF_PACKET 套接字以获取正确的过滤数据包

java - 我需要同步 Sockets 的 ArrayList 吗?

objective-c - 为什么在 C 中将 1,000,000,000 写为 1000*1000*1000?

c - 尝试从 C 中的 txt 文件中读取数字时出错

linux - 检查 rtattr 中的长度是否均匀

linux - 将 pv 输出重定向到文件

c - 运行时错误 - SIGSEGV

c - 如何将数字转换为Q格式

linux - "book"的输出中的 'lscpu -p' 是什么意思?

mysql - 工作台显示 : Failed to Connect to MySQL at 127. 0.0.1 :3306 through SSH tunnel at 34. xxx.xxx.xxx:22,用户为 root