这是一个朋友给我的代码,他说可以运行的。可是为什么在我机子要编译都通不过呢?
是不是运行之前还要有什么预处理命令?
[php]
/*
* filter.c will display all tcp packet information on the screen
*
* gcc -O2 -I /usr/src/linux-2.4.20-8/include/ -c filter.c
* insmod filter.o
*
* Author interstar
*
*/
#ifndef __KERNEL__
# define __KERNEL__
#endif
#ifndef MODULE
# define MODULE
#endif
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/skbuff.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/netdevice.h>
#include <linux/in.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <net/tcp.h>
MODULE_LICENSE("GPL");
char *inet_ntoa(__u32 ina)
{
static char buf[4 * sizeof "123"];
unsigned char *ucp = (unsigned char *)&ina;
sprintf(buf, "%d.%d.%d.%d",
ucp[0] & 0xff,
ucp[1] & 0xff,
ucp[2] & 0xff,
ucp[3] & 0xff);
return buf;
}
static unsigned int local_input (unsigned int hooknum, struct sk_buff **pskb,
const struct net_device *in, const struct net_device *out,
int (*okfn) (struct sk_buff *))
{
struct tcphdr *tcph;
struct iphdr *iph;
struct sk_buff *skb = *pskb;
__u32 odaddr;
__u32 osaddr;
__u16 odport;
__u16 osport;
printk("Local Input Called!\n");
printk("Input Device:%s\n",in->name);
printk("Output Device:%s\n",out->name);
if (skb->protocol == htons (ETH_P_IP))
{
iph = skb->nh.iph;
if (iph->protocol == IPPROTO_TCP)
{
tcph = (struct tcphdr *)((__u32 *)iph+iph->ihl);
osaddr = iph->saddr;
odaddr = iph->daddr;
odport=ntohs(tcph->dest);
osport=ntohs(tcph->source);
printk("From:%-16s Port:%u\n",inet_ntoa(odaddr),osport);
printk("To: %-16s Port:%u\n",inet_ntoa(osaddr),odport);
printk("fin:%u,syn:%u,ack:%u\n",tcph->fin,tcph->syn,tcph->ack);
}
}
return NF_ACCEPT;
}
static unsigned int local_output (unsigned int hooknum, struct sk_buff **pskb,
const struct net_device *in, const struct net_device *out,
int (*okfn) (struct sk_buff *))
{
struct tcphdr *tcph;
struct iphdr *iph;
struct sk_buff *skb = *pskb;
__u32 odaddr;
__u32 osaddr;
__u16 odport;
__u16 osport;
printk("Local Output Called!\n");
printk("Input Device:%s\n",in->name);
printk("Output Device:%s\n",out->name);
if (skb->protocol == htons (ETH_P_IP))
{
iph = skb->nh.iph;
if (iph->protocol == IPPROTO_TCP)
{
tcph = (struct tcphdr *)((__u32 *)iph+iph->ihl);
osaddr = iph->saddr;
odaddr = iph->daddr;
odport=ntohs(tcph->dest);
osport=ntohs(tcph->source);
printk("From:%-16s Port:%u\n",inet_ntoa(odaddr),osport);
printk("To: %-16s Port:%u\n",inet_ntoa(osaddr),odport);
printk("fin:%u,syn:%u,ack:%u\n",tcph->fin,tcph->syn,tcph->ack);
}
}
return NF_ACCEPT;
}
static unsigned int forward_filter (unsigned int hooknum, struct sk_buff **pskb,
const struct net_device *in, const struct net_device *out,
int (*okfn) (struct sk_buff *))
{
struct tcphdr *tcph;
struct iphdr *iph;
struct sk_buff *skb = *pskb;
__u32 odaddr;
__u32 osaddr;
__u16 odport;
__u16 osport;
printk("Forword Called!\n");
printk("Input Device:%s\n",in->name);
printk("Output Device:%s\n",out->name);
if (skb->protocol == htons (ETH_P_IP))
{
iph = skb->nh.iph;
if (iph->protocol == IPPROTO_TCP)
{
tcph = (struct tcphdr *)((__u32 *)iph+iph->ihl);
osaddr = iph->saddr;
odaddr = iph->daddr;
odport=ntohs(tcph->dest);
osport=ntohs(tcph->source);
printk("From:%-16s Port:%u\n",inet_ntoa(odaddr),osport);
printk("To: %-16s Port:%u\n",inet_ntoa(osaddr),odport);
printk("fin:%u,syn:%u,ack:%u\n",tcph->fin,tcph->syn,tcph->ack);
}
}
return NF_ACCEPT;
}
static struct nf_hook_ops input_filter =
{
{NULL, NULL},
local_input,
AF_INET,
NF_IP_LOCAL_IN,
NF_IP_PRI_FILTER - 1
};
static struct nf_hook_ops output_filter =
{
{NULL, NULL},
local_output,
AF_INET,
NF_IP_LOCAL_OUT,
NF_IP_PRI_FILTER - 1
};
static struct nf_hook_ops forward =
{
{NULL, NULL},
forward_filter,
AF_INET,
NF_IP_FORWARD,
NF_IP_PRI_FILTER - 1
};
//packet flow diagram:
//
//-NF_IP_PRE_ROUTING---| ROUTING PLOCY |---NF_IP_FORWARD---NF_IP_POST_ROUTING
// | |
// | |
// | |
// NF_IP_LOCAL_IN NF_IP_LOCAL_OUT
// | |
// |__________|PROCESSING|___________|
//
//ip_build_and_send_pkt,ip_queue_xmit ip_build_xmit_slow,ip_build_xmit
int init_module (void)
{
printk ("Load Netfilter Module \n");
if (nf_register_hook (&input_filter) || nf_register_hook (&output_filter) || nf_register_hook (&forward))
return 1;
else
return 0;
}
void cleanup_module (void)
{
nf_unregister_hook (&input_filter);
nf_unregister_hook (&output_filter);
nf_unregister_hook (&forward);
printk ("UnLoad Netfilter Module\n");
return;
}
[/php]