求大神指导,一个简单的netfilter程序编译问题

qq_20738807 2016-12-15 10:44:46
最近在学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能具体的指导一下该怎么编译运行
...全文
1294 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
flives 2018-08-06
  • 打赏
  • 举报
回复
netfilter是一个驱动程序;
驱动的入口不是main,而是module_init();
可以上网找个简单的字符驱动了解下;然后参考仿照写Makefile,链接内核源码符号树。
maomaoya- 2018-07-30
  • 打赏
  • 举报
回复
这个是内核里面跑的东西,不是用户空间。

4,436

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 内核源代码研究区
社区管理员
  • 内核源代码研究区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧