分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

Linux Netfilter注册钩子点

发布时间:2023-09-06 01:25责任编辑:傅花花关键词:Linux

注册钩子点首先要包含响应的头文件,因为这应该已经属于对kernel的编程了。

1 #include <linux/module.h>2 #include <linux/kernel.h>3 #include <linux/init.h>
其次,要定义钩子函数
 1 static unsigned int example(unsigned int hooknum,struct sk_buff* skb,const struct net_device *in,const struct net_device *out,int (*okfn)(struct sk_buff *)) 2 { 3 ????//可以定义一些处理 4 ????//这里一般要做一个skb非空的判断,可以用if(skb)判断 5 ????/* 6 ???????struct sk_buff* sk = NULL; 7 ???????sk = skb; ?8 ????????一些常用的处理函数: 9 ????????struct iphdr *iph;10 ????????iph = ip_hdr(sk);11 ????????注:tcp、udp同理。12 ????*/13 return NF_ACCEPT;14 } 
第三,设置相关信息
1 ?struct nf_hook_ops example_ops = {2 ???.list = ?{NULL,NULL},3 ???.hook = example,//Hook函数4 ???.pf = PF_INET,//协议5 ???.hooknum = NF_INET_PRE_ROUTING,//钩子点6 ???.priority = NF_IP_PRI_FILTER+2//钩子的优先级,其实代表钩子处理函数的调用次序。7 ?};
调用注册和注销函数 
 1 nf_register_hook(&example_ops); 2 nf_unregister_hook(&example_ops); ?3 可以写入初始化和注销函数里面 4 static int __init example_init() 5 { 6 ??nf_register_hook(&example_ops); 7 ??return 0; 8 } 9 ?static void __exit example_exit()10 {11 ??nf_unregister_hook(&example_ops);12 }
关于SKB
skb是linux内核存储描述数据包的一种结构体
也是skb双向链表的一个节点结构体。
1 module_init(example_init);2 module_exit(example_exit); 3 ?4 MODULE_AUTHOR("*********");5 MODULE_VERSION("0.0.1");6 MOUDLE_ALIAS("test"); 7 MODULE_DESCRIPTION("example") ;

Linux Netfilter注册钩子点

原文地址:http://www.cnblogs.com/KevinGeorge/p/7866876.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved