最近在学linux防火墙的知识,在netfilter上就遇到了很大问题,什么未定义的引用变量啊,找不到的库函数啊。好不容易用gcc -O -I name.c把.c文件编译成.o文件后,再转换成可执行文件时又出现了函数无法调用的问题,我查了网上有人说要加-c参数,还有人说要加链接库,但是我不知道该用什么链接库,不是很懂该怎么办。gcc报错如下:
[root@localhost root]# gcc -o myfirewall Myfirewall.o
Myfirewall.o(.text+0x5e): In function `main':
: undefined reference to `nf_register_hook'
Myfirewall.o(.text+0x7b): In function `cleanup_moudule':
: undefined reference to `nf_unregister_hook'
collect2: ld returned 1 exit status源代码(网上最常见的netfilter示例程序):#define MODULE
#define __KERNEL__
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/config.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>/*注册hook函数数据结构*/
static struct nf_hook_ops my_hook ;
/*hook处理函数*/
unsigned int hook_func(unsigned int hook,
struct sk_buff **skb,
const struct net_device *in,
const struct net_device *out,
int (*okfn)(struct sk_buff *))
{
printf( "a packet go through\n ");
return NF_ACCEPT;
}/*初始化程序*/
int main()
{
my_hook.hook=hook_func;
//my_hook.owner=THIS_MODULE;
my_hook.pf= PF_INET;
my_hook.hooknum= NF_IP_LOCAL_IN;
my_hook.priority = NF_IP_PRI_FIRST; nf_register_hook(&my_hook);//注册入内核 return 0;
}/*清除钩子*/
void cleanup_moudule()
{
nf_unregister_hook(&my_hook);//将用户定义的钩子从内核中删除
}我用的是red hat9,内核是2.4,希望有dalao能具体的指导一下该怎么编译运行
[root@localhost root]# gcc -o myfirewall Myfirewall.o
Myfirewall.o(.text+0x5e): In function `main':
: undefined reference to `nf_register_hook'
Myfirewall.o(.text+0x7b): In function `cleanup_moudule':
: undefined reference to `nf_unregister_hook'
collect2: ld returned 1 exit status源代码(网上最常见的netfilter示例程序):#define MODULE
#define __KERNEL__
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/config.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>/*注册hook函数数据结构*/
static struct nf_hook_ops my_hook ;
/*hook处理函数*/
unsigned int hook_func(unsigned int hook,
struct sk_buff **skb,
const struct net_device *in,
const struct net_device *out,
int (*okfn)(struct sk_buff *))
{
printf( "a packet go through\n ");
return NF_ACCEPT;
}/*初始化程序*/
int main()
{
my_hook.hook=hook_func;
//my_hook.owner=THIS_MODULE;
my_hook.pf= PF_INET;
my_hook.hooknum= NF_IP_LOCAL_IN;
my_hook.priority = NF_IP_PRI_FIRST; nf_register_hook(&my_hook);//注册入内核 return 0;
}/*清除钩子*/
void cleanup_moudule()
{
nf_unregister_hook(&my_hook);//将用户定义的钩子从内核中删除
}我用的是red hat9,内核是2.4,希望有dalao能具体的指导一下该怎么编译运行
驱动的入口不是main,而是module_init();
可以上网找个简单的字符驱动了解下;然后参考仿照写Makefile,链接内核源码符号树。