iphdr 类型转换给我错误

iphdr type casting giving me error

我正在尝试打印 TCP 地址,但出现 "Dereferencing pointer to incomplete type" 错误。我认为 iphdr 类型转换不起作用。我该如何解决这个问题?

unsigned int hook_func(unsigned int hooknum,
                   struct sk_buff *skb,
                   const struct net_device *in,
                   const struct net_device *out,
                   int (*okfn)(struct sk_buff *))
{

   struct iphdr *ip_header;       // ip header struct

   if (!skb)
      return NF_ACCEPT;

   ip_header = (struct iphdr *)skb_network_header(skb); /* I think this is not working */

   printk("addr : %lu.\n",ip_header->saddr);
   return NF_ACCEPT;          
}

int init_module()
{
    printk(KERN_INFO "initialize kernel module\n");
    /* Fill in our hook structure */
    nfho.hook = hook_func;         /* Handler function */
    nfho.hooknum  = NF_INET_PRE_ROUTING; /* First hook for IPv4 */
    nfho.pf       = PF_INET;
    nfho.priority = NF_IP_PRI_FIRST;   /* Make our function first */

    nf_register_hook(&nfho);

    return 0;
}

要取消引用 struct iphdr 类型的指针,您需要

#include <linux/ip.h>

(其实这个类型是在include/uapi/linux/ip.h中定义的,但通常uapi/下的headers是通过其他的包含进来的。)