gpt4 book ai didi

c - 使用udphdr时报错 "dereferencing pointer to incomplete type"

转载 作者:太空宇宙 更新时间:2023-11-04 07:08:16 25 4
gpt4 key购买 nike

我用 udphdr.h 写了一个程序。这是我的头文件列表:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/udp.h>
#include <linux/skbuff.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/sched.h>
#include <net/sock.h>
#include <linux/netlink.h>

这是我的代码:

void dealUDPH(struct updhr* udph){
if(udph==NULL){
printk("udph is NULL!!\n");
}else{
printk("updh is %u\n",udph);
printk("udp sport is %u\n",udph->source);
}
}

static unsigned int hookfunc(unsigned int hooknum, struct sk_buff **skb,
const struct net_device *in, const struct net_device *out,
int (*okfn)(struct sk_buff *)){
struct iphdr *iph=ip_hdr(skb);
struct ethhdr * ethh = eth_hdr(skb);
struct tcphdr *tcph=tcp_hdr(skb);
struct udphdr *udph=udp_hdr(skb);
printk("*********************************************************\n");
dealIPH(iph);
dealETHH(ethh);
dealTCPH(tcph);
dealUDPH(udph);
printk("*********************************************************\n");
return NF_ACCEPT;
}

这是我的生成文件:

ifneq ($(KERNELRELEASE),)
mymodule-objs:=main.c
obj-m += main.o

else
PWD := $(shell pwd)
KVER := $(shell uname -r)
KDIR := /lib/modules/$(KVER)/build

all:
$(MAKE) -C $(KDIR) M=$(PWD) modules
clean:
rm -rf *.o *.mod.c *.ko *.symvers *order *.markers *-
endif

当我编译文件时,它说这行:printk("udp sport is %u\n",udph->source);是错误的,错误信息是取消引用指向不完整类型的指针

怎么了?我该如何解决?

最佳答案

你有:

struct updhr* udph

你的意思可能是:

struct udphdr* udph

(在上下文中:void dealUDPH(struct updhr* udph){。)

请注意,只要不需要引用结构中的任何成员,或分配结构的副本,就可以使用 struct anyNameYouLike *ptr 作为参数,无需多费功夫.但是,如果该名称第一次出现在函数原型(prototype)(声明或定义)中,您应该会收到如下警告:

$ gcc -g -O3 -std=c11 -Wall -Wextra -Wmissing-prototypes -Werror -c xx.c
xx.c:2:23: error: ‘struct xyz’ declared inside parameter list [-Werror]
void something(struct xyz *ptr)
^
xx.c:2:23: error: its scope is only this definition or declaration, which is probably not what you want [-Werror]
xx.c:2:6: error: no previous prototype for ‘something’ [-Werror=missing-prototypes]
void something(struct xyz *ptr)
^
cc1: all warnings being treated as errors
rmk: error code 1
$

“没有以前的原型(prototype)”消息是准确的,是使用 -Wmissing-prototypes 选项(结合 -Werror)的结果。

xx.c

#include <stdlib.h>
void something(struct xyz *ptr)
{
if (ptr == 0)
exit(0);
}

关于c - 使用udphdr时报错 "dereferencing pointer to incomplete type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30535717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com