当前位置: 技术问答>linux和unix
调用函数指针的错误
来源: 互联网 发布时间:2016-10-05
本文导语: 我目前在做一个实验,利用netfilter来实现一个简单的firewall,但是遇到问题。 首先,我想实现把每个到达的packet的interface的名字输出来。 代码如下: //#include #include #include #include /* This is the structure we shall ...
我目前在做一个实验,利用netfilter来实现一个简单的firewall,但是遇到问题。
首先,我想实现把每个到达的packet的interface的名字输出来。
代码如下:
//#include
#include
#include
#include
/* This is the structure we shall use to register our function */
static struct nf_hook_ops nfho;
/* This is the hook function itself */
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 *))
{
char drop_if[3];
strcpy(drop_if,"lo");
//注意这一句,记为A吧 int g=strcmp((*in).name, drop_if);
// printk("Dropped packet on %s...n", drop_if);
// return NF_DROP;
//} else {
// return NF_ACCEPT;
//}
return NF_ACCEPT;
}
/* Initialisation routine */
int init_module()
{
/* 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;
}
/* Cleanup routine */
void cleanup_module()
{
nf_unregister_hook(&nfho);
}
然后就通过makefile文件
obj-m += example.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
进行编译,如果注释掉A,那么正常编译,如果不注释掉A,那么就报错
CC [M] /home/serendip/Working/CS5231/Project3/filter/example.o
/home/A/example.c: In function ‘hook_func’:
/home/A/example.c:22: error: dereferencing pointer to incomplete type
/home/A/example.c:22: warning: ISO C90 forbids mixed declarations and code
/home/A/example.c:22: warning: unused variable ‘g’
make[2]: *** [/home/A/example.o] Error 1
make[1]: *** [_module_/home/A] Error 2
make[1]: Leaving directory `/usr/src/linux-headers-2.6.32-25-generic'
make: *** [all] Error 2
22行就是 nfho.hook = hook_func; /* Handler function */
这里怎么出搓了?
首先,我想实现把每个到达的packet的interface的名字输出来。
代码如下:
//#include
#include
#include
#include
/* This is the structure we shall use to register our function */
static struct nf_hook_ops nfho;
/* This is the hook function itself */
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 *))
{
char drop_if[3];
strcpy(drop_if,"lo");
//注意这一句,记为A吧 int g=strcmp((*in).name, drop_if);
// printk("Dropped packet on %s...n", drop_if);
// return NF_DROP;
//} else {
// return NF_ACCEPT;
//}
return NF_ACCEPT;
}
/* Initialisation routine */
int init_module()
{
/* 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;
}
/* Cleanup routine */
void cleanup_module()
{
nf_unregister_hook(&nfho);
}
然后就通过makefile文件
obj-m += example.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
进行编译,如果注释掉A,那么正常编译,如果不注释掉A,那么就报错
CC [M] /home/serendip/Working/CS5231/Project3/filter/example.o
/home/A/example.c: In function ‘hook_func’:
/home/A/example.c:22: error: dereferencing pointer to incomplete type
/home/A/example.c:22: warning: ISO C90 forbids mixed declarations and code
/home/A/example.c:22: warning: unused variable ‘g’
make[2]: *** [/home/A/example.o] Error 1
make[1]: *** [_module_/home/A] Error 2
make[1]: Leaving directory `/usr/src/linux-headers-2.6.32-25-generic'
make: *** [all] Error 2
22行就是 nfho.hook = hook_func; /* Handler function */
这里怎么出搓了?
|
22行到底是哪行?
int g=strcmp((*in).name, drop_if);
还是nfho.hook = hook_func; /* Handler function */
int g=strcmp((*in).name, drop_if);
还是nfho.hook = hook_func; /* Handler function */
|
const struct net_device *in 中的 struct net_device 结构是不是定义在.c里面了 而没定义在.h里面,所以报了个 dereferencing pointer to incomplete type