#define __KERNEL__
#define MODULE

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/socket.h>
#include <linux/tcp.h>

static struct nf_hook_ops nfho;

static unsigned char* deny_ip = "\xc0\xa8\x0a\xd2";
static unsigned char* drop_ip = "\x7f\x00\x00\x01";

unsigned int hook_func(unsigned int hooknum,
struct sk_buff **skb, const struct net_device *in, const struct net_device *out,int(*okfn)(struct sk_buff *))
{
	struct sk_buff *sb = *skb;
	printk("RAJENDRA IPAddress is %u.%u.%u.%u\n",NIPQUAD(sb->nh.iph->saddr));
	//if (sb->nh.iph->saddr == *(unsigned int *)deny_ip)
	if (sb->nh.iph->saddr == *(unsigned int *)drop_ip)
	{
	printk("RAJENDRA in if conditionsb->nh.iph->saddr = %d.%d.%d.%d\n",*drop_ip,*(drop_ip+1),*(drop_ip+2),*(drop_ip+3));
	return NF_DROP;
	}
	else
	{
	printk("RAJENDRA in else conditionsb->nh.iph->saddr = %d.%d.%d.%d\n",*drop_ip,*(drop_ip+1),*(drop_ip+2),*(drop_ip+3));
//	printk("RAJENDRA in else condition sb->nh.iph->saddr = %d.%d.%d.%d\n",*deny_ip,*(deny_ip+1),*(deny_ip+2),*(deny_ip+3));
		return NF_ACCEPT;
	}

/*
	if (strcmp(in->name,drop_if) == 0)
	{
		printk("packets dropped = %s\n",drop_if);
		return NF_DROP;
	}
	else
	{
		printk("packets accepted = %s\n",drop_if);
		return NF_ACCEPT;
	}
*/
}

int init_module()
{
	printk("Entering module\n");
	nfho.hook = hook_func;
	nfho.hooknum = NF_IP_PRE_ROUTING;
	nfho.pf = PF_INET;
	nfho.priority = NF_IP_PRI_FIRST;

	nf_register_hook(&nfho);
	return 0;
}

void cleanup_module()
{
	printk("Leaving module\n");
	nf_unregister_hook(&nfho);
}
