⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 initval.c

📁 Linux下面截获以态网数据包!是在内核态下面运行的驱动程序
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <linux/module.h> 
#include <linux/config.h> 
#include <linux/init.h> 
#include <linux/netdevice.h> 		/* for dev_base */
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/sched.h>
#include <linux/file.h>
#include <linux/dnotify.h>
#include <linux/if_ether.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/udp.h>
#include <linux/time.h>
#include <linux/vmalloc.h>
#include <asm/atomic.h>
#include <asm/semaphore.h>
#include <asm/unistd.h>

#include "createDir.h"
#include "DomainIPHash.h"
#include "list.h"
#include "Rule.h"
#include "RuleIp.h"
#include "protocol.h"
#include "function.h"


extern GVAR gVar;

char Malloc_Var(void);
char AllocateHashHeadPool(void);
void FreeDoubleListPool( PDOUBLE_LIST_HEAD pListHead );
char InitHash(void);
char InitSmtpHash(void);
char InitHttpHash(void);
char InitIPHash(void);
char InitDomainHash(void);
char InitMsnHash(void);
char InitFtpHash(void);		
char InitTelnetHash(void);	
char InitIcqHash(void);	
char InitYahooHash(void);		
char InitRuleHash(void);
char InitRuleIPHash(void);
char InitIrcHash(void);		
char InitPpHash(void);
char InitGtalkHash(void);	
char InitPostHash(void);

char InitGVar( )
{
	//为全局变量分配内存
	if( !Malloc_Var() )
	{
		printk( "Allocate pool failed!\r\n" );
		return 0;
	}
	
	//初始化哈希表
	if( !InitHash() )
	{
		printk( "Init Hash failed!\r\n" );
		return 0;
	}
	
	//初始化webmail config
	if( !InitWebMailConfig() )
	{
		printk( "Init web mail config failed!\r\n" );
		return 0;
	}
	
	//读取配置
	DriverConfig();
	
	if( !INIT_HASHHEAD_INFO() )
		return 0;

	if( !INIT_PACKETHEAD_INFO() )
		return 0;
		
	return 1;	
}

char Malloc_Var( )
{
	int					i, j;
	PDOUBLE_LIST_PACKET pPacket;
	PSMTP_HASH_HEAD		pSmtpPacket;
	PPACKET_DATA		temp;
	
	pPacket = NULL;
	memset( &gVar, 0, sizeof(gVar) );
		
	/* 进行内存分配,分配好所有的数据*/
	atomic64_set( &gVar.TcpCycArrayHead.iWrite, 0 );
	atomic64_set( &gVar.TcpCycArrayHead.iRead, 0 );
	for( i = 0; i < _MAX_CYC_ARRAY_DEPTH; i ++ )
	{
		temp = NULL;
		temp = (PPACKET_DATA)kmalloc( sizeof(PACKET_DATA), GFP_KERNEL );
		if( !temp )
		{
			printk( "Allocate tcp buffer failed\r\n" );
			
			for( j = 0; j < i; j ++ )
				kfree( gVar.TcpCycArrayHead.PacketArray[ j ] );

			goto FAILED;
		}
		
		gVar.TcpCycArrayHead.PacketArray[ i ] = temp;
	}

	atomic64_set( &gVar.UdpCycArrayHead.iWrite, 0 );
	atomic64_set( &gVar.UdpCycArrayHead.iRead, 0 );
	for( i = 0; i < _MAX_CYC_ARRAY_DEPTH; i ++ )
	{
		temp = NULL;
		temp = (PPACKET_DATA)kmalloc( sizeof(PACKET_DATA), GFP_KERNEL );
		if( !temp )
		{
			printk( "Allocate udp buffer failed\r\n" );

			for( j = 0; j < i; j ++ )
				kfree( gVar.UdpCycArrayHead.PacketArray[ j ] );

			goto FAILED;
		}
		
		gVar.UdpCycArrayHead.PacketArray[ i ] = temp;
	}

	gVar.pShortPacketPool = kmalloc( sizeof(DOUBLE_LIST_HEAD), GFP_USER );
	if( gVar.pShortPacketPool == NULL )
	{
		printk( "Allocate DOUBLE_LIST_HEAD of pLeisurePool failed \r\n" );
		return false;
	}
	_InitializeList( gVar.pShortPacketPool );
	
	for( i = 0; i < _SHORT_LEISURE_POOL_DEPTH; i ++)
	{
		pPacket = NULL;
		pPacket = (PDOUBLE_LIST_PACKET)kmalloc( sizeof(SHORT_DOUBLE_LIST_PACKET), GFP_USER );
		if( !pPacket )
		{
			printk( "Allocate short packet list failed\r\n" );
			goto FAILED;
		}

		pPacket->bLong = false;
		pPacket->bDynamic = false;
		_InsertHeadList( gVar.pShortPacketPool, &(pPacket->doubleListItem) );
	}

	gVar.pLongPacketPool = kmalloc( sizeof(DOUBLE_LIST_HEAD), GFP_USER );
	if( gVar.pLongPacketPool == NULL )
	{
		printk( "Allocate DOUBLE_LIST_HEAD of pLeisurePool failed \r\n" );
		return false;
	}
	_InitializeList( gVar.pLongPacketPool );
	
	for( i = 0; i < _LONG_LEISURE_POOL_DEPTH; i ++ )
	{
		pPacket = NULL;
		pPacket = (PDOUBLE_LIST_PACKET)kmalloc( sizeof(DOUBLE_LIST_PACKET), GFP_USER );
		if( !pPacket )
		{
			printk( "allocate long packet list failed\r\n" );
			goto FAILED;
		}

		pPacket->bLong = true;
		pPacket->bDynamic = false;
		_InsertHeadList( gVar.pLongPacketPool, &(pPacket->doubleListItem) );
	}

	gVar.pSmtpQueque = NULL;
	gVar.pSmtpQueque = kmalloc( sizeof(DOUBLE_LIST_HEAD), GFP_KERNEL );
	if( gVar.pSmtpQueque == NULL )
	{
		printk( "Allocate DOUBLE_LIST_HEAD of gVar.pSmtpQueque failed \r\n" );
		goto FAILED;
	}
	_InitializeList( gVar.pSmtpQueque );

	gVar.pHttpQueque = NULL;	
	gVar.pHttpQueque = kmalloc( sizeof(DOUBLE_LIST_HEAD), GFP_KERNEL );
	if( gVar.pHttpQueque == NULL )
	{
		printk( "Allocate DOUBLE_LIST_HEAD of gVar.pHttpQueque failed \r\n" );
		goto FAILED;
	}
	_InitializeList( gVar.pHttpQueque );
	
	gVar.pMsnQueque = NULL;
	gVar.pMsnQueque = kmalloc( sizeof(DOUBLE_LIST_HEAD), GFP_KERNEL );
	if( gVar.pMsnQueque == NULL )
	{	
		printk( "Allocate DOUBLE_LIST_HEAD of gVar.pMsnQueque failed \r\n" );
		goto FAILED;
	}
	_InitializeList( gVar.pMsnQueque );
	
	gVar.pHttpUrlQueque = NULL;
	gVar.pHttpUrlQueque = kmalloc( sizeof(DOUBLE_LIST_HEAD), GFP_KERNEL );
	if( gVar.pHttpUrlQueque == NULL )
	{
		printk( "Allocate DOUBLE_LIST_HEAD of gVar.pHttpUrlQueque failed \r\n" );
		goto FAILED;
	}
	_InitializeList( gVar.pHttpUrlQueque );	

	gVar.pFtpQueque = NULL;
	gVar.pFtpQueque = kmalloc( sizeof(DOUBLE_LIST_HEAD), GFP_KERNEL );
	if( gVar.pFtpQueque == NULL )
	{
		printk( "Allocate DOUBLE_LIST_HEAD of gVar.pFtpQueque failed \r\n" );
		goto FAILED;
	}
	_InitializeList( gVar.pFtpQueque );	

	gVar.pTelnetQueque = NULL;
	gVar.pTelnetQueque = kmalloc( sizeof(DOUBLE_LIST_HEAD), GFP_KERNEL );
	if( gVar.pTelnetQueque == NULL )
	{
		printk( "Allocate DOUBLE_LIST_HEAD of gVar.pTelnetQueque failed \r\n" );
		goto FAILED;
	}
	_InitializeList( gVar.pTelnetQueque );
	
	gVar.pIcqQueque	= NULL;
	gVar.pIcqQueque = kmalloc( sizeof(DOUBLE_LIST_HEAD), GFP_KERNEL );
	if( gVar.pIcqQueque == NULL )
	{
		printk("Allocate DOUBLE_LIST_HEAD of gVar.pIcqQueque failed \r\n");
		goto FAILED;
	}
	_InitializeList( gVar.pIcqQueque );		
	
	gVar.pYahooQueque = NULL;
	gVar.pYahooQueque = kmalloc( sizeof(DOUBLE_LIST_HEAD), GFP_KERNEL );
	if( gVar.pYahooQueque == NULL )
	{
		printk("Allocate DOUBLE_LIST_HEAD of gVar.pYahooQueque failed \r\n");
		goto FAILED;
	}
	_InitializeList( gVar.pYahooQueque );
	
	gVar.pIrcQueque = NULL;
	gVar.pIrcQueque = kmalloc( sizeof(DOUBLE_LIST_HEAD), GFP_KERNEL );
	if( gVar.pIrcQueque == NULL )
	{
		printk("Allocate DOUBLE_LIST_HEAD of gVar.pIrcQueque failed \r\n");
		goto FAILED;
	}
	_InitializeList( gVar.pIrcQueque );
	
	gVar.pPpQueque = NULL;
	gVar.pPpQueque = kmalloc( sizeof(DOUBLE_LIST_HEAD), GFP_KERNEL );
	if( gVar.pPpQueque == NULL )
	{
		printk("Allocate DOUBLE_LIST_HEAD of gVar.pPpQueque failed \r\n");
		goto FAILED;
	}
	_InitializeList( gVar.pPpQueque );

	gVar.pGtalkQueque = NULL;	
	gVar.pGtalkQueque = kmalloc( sizeof(DOUBLE_LIST_HEAD), GFP_KERNEL );
	if( gVar.pGtalkQueque == NULL )
	{
		printk("Allocate DOUBLE_LIST_HEAD of gVar.pGtalkQueque failed \r\n");
		goto FAILED;
	}
	_InitializeList( gVar.pGtalkQueque );

	//要保存的文件队列
	gVar.pFileQueque = NULL;
	gVar.pFileQueque = kmalloc( sizeof(DOUBLE_LIST_HEAD), GFP_KERNEL );
	if( gVar.pFileQueque == NULL )
	{
		printk( "Allocate DOUBLE_LIST_HEAD of gVar.pPop3Queque failed \r\n" );
		goto FAILED;
	}
	_InitializeList( gVar.pFileQueque );	

	gVar.pWebLoginFileQueque = NULL;
	gVar.pWebLoginFileQueque = kmalloc( sizeof(DOUBLE_LIST_HEAD), GFP_KERNEL );
	if( gVar.pWebLoginFileQueque == NULL )
	{
		printk( "Allocate DOUBLE_LIST_HEAD of gVar.pWebLoginFileQueque failed \r\n" );
		goto FAILED;
	}
	_InitializeList( gVar.pWebLoginFileQueque );
	
	gVar.pWebMailFileQueque = NULL;
	gVar.pWebMailFileQueque = kmalloc( sizeof(DOUBLE_LIST_HEAD), GFP_KERNEL );
	if( gVar.pWebMailFileQueque == NULL )
	{
		printk( "Allocate DOUBLE_LIST_HEAD of gVar.pWebMailFileQueque failed \r\n" );
		goto FAILED;
	}
	_InitializeList( gVar.pWebMailFileQueque );

	gVar.pBBSFileQueque = NULL;
	gVar.pBBSFileQueque = kmalloc( sizeof(DOUBLE_LIST_HEAD), GFP_KERNEL );
	if( gVar.pBBSFileQueque == NULL )
	{
		printk( "Allocate DOUBLE_LIST_HEAD of gVar.pBBSFileQueque failed \r\n" );
		goto FAILED;
	}
	_InitializeList( gVar.pBBSFileQueque );	
	
	gVar.pMsnFileQueque = NULL;
	gVar.pMsnFileQueque = kmalloc( sizeof(DOUBLE_LIST_HEAD), GFP_KERNEL );
	if( gVar.pMsnFileQueque == NULL )
	{
		printk( "Allocate DOUBLE_LIST_HEAD of gVar.pMsnFileQueque failed \r\n" );
		goto FAILED;
	}
	_InitializeList( gVar.pMsnFileQueque );	

	gVar.pFtpFileQueque = NULL;
	gVar.pFtpFileQueque = kmalloc( sizeof(DOUBLE_LIST_HEAD), GFP_KERNEL );
	if( gVar.pFtpFileQueque == NULL )
	{
		printk( "Allocate DOUBLE_LIST_HEAD of gVar.pFtpFileQueque failed \r\n" );
		goto FAILED;
	}
	_InitializeList( gVar.pFtpFileQueque );	

	gVar.pTelnetFileQueque = NULL;
	gVar.pTelnetFileQueque = kmalloc( sizeof(DOUBLE_LIST_HEAD), GFP_KERNEL );
	if( gVar.pTelnetFileQueque == NULL )
	{
		printk("Allocate DOUBLE_LIST_HEAD of gVar.pTelnetFileQueque failed \r\n");
		goto FAILED;
	}
	_InitializeList( gVar.pTelnetFileQueque );	

	gVar.pIcqFileQueque = NULL;
	gVar.pIcqFileQueque = kmalloc( sizeof(DOUBLE_LIST_HEAD), GFP_KERNEL );
	if( gVar.pIcqFileQueque == NULL )
	{
		printk( "Allocate DOUBLE_LIST_HEAD of gVar.pIcqFileQueque failed \r\n" );
		goto FAILED;
	}
	_InitializeList( gVar.pIcqFileQueque );

	gVar.pYahooFileQueque = NULL;	
	gVar.pYahooFileQueque = kmalloc( sizeof(DOUBLE_LIST_HEAD), GFP_KERNEL );
	if( gVar.pYahooFileQueque == NULL )
	{
		printk( "Allocate DOUBLE_LIST_HEAD of gVar.pYahooFileQueque failed \r\n" );
		goto FAILED;
	}
	_InitializeList( gVar.pYahooFileQueque );
	
	gVar.pHttpUrlFileQueque = NULL;
	gVar.pHttpUrlFileQueque = kmalloc( sizeof(DOUBLE_LIST_HEAD), GFP_KERNEL );
	if( gVar.pHttpUrlFileQueque == NULL )
	{
		printk("Allocate DOUBLE_LIST_HEAD of gVar.pHttpUrlFileQueque failed \r\n");
		goto FAILED;
	}
	_InitializeList( gVar.pHttpUrlFileQueque );		

	gVar.pIrcFileQueque = NULL;
	gVar.pIrcFileQueque = kmalloc( sizeof(DOUBLE_LIST_HEAD), GFP_KERNEL );
	if( gVar.pIrcFileQueque == NULL )
	{
		printk("Allocate DOUBLE_LIST_HEAD of gVar.pIrcFileQueque failed \r\n");
		goto FAILED;
	}
	_InitializeList( gVar.pIrcFileQueque );		

	gVar.pPpFileQueque = NULL;
	gVar.pPpFileQueque = kmalloc( sizeof(DOUBLE_LIST_HEAD), GFP_KERNEL );
	if( gVar.pPpFileQueque == NULL )
	{
		printk("Allocate DOUBLE_LIST_HEAD of gVar.pPpFileQueque failed \r\n");
		goto FAILED;
	}
	_InitializeList( gVar.pPpFileQueque );		

	gVar.pGtalkFileQueque = NULL;
	gVar.pGtalkFileQueque = kmalloc( sizeof(DOUBLE_LIST_HEAD), GFP_KERNEL );
	if( gVar.pGtalkFileQueque == NULL )
	{
		printk("Allocate DOUBLE_LIST_HEAD of gVar.pGtalkFileQueque failed \r\n");
		goto FAILED;
	}
	_InitializeList( gVar.pGtalkFileQueque );
	
	pSmtpPacket = NULL;
	gVar.pHashHeadQueque = NULL;
	gVar.pHashHeadQueque = kmalloc( sizeof(DOUBLE_LIST_HEAD), GFP_KERNEL );
	if( gVar.pHashHeadQueque == NULL )
	{
		printk("Allocate DOUBLE_LIST_HEAD of pHashHeadPool failed \r\n");
		goto FAILED;
	}
	_InitializeList( gVar.pHashHeadQueque );


	for( i = 0; i < _HASH_HEAD_NUMBER; i ++ )
	{
		pSmtpPacket = kmalloc( sizeof(SMTP_HASH_HEAD), GFP_KERNEL );
		
		if( !pSmtpPacket )
			goto FAILED;
		
		memset( pSmtpPacket, 0, sizeof(SMTP_HASH_HEAD) );	
		_InsertHeadList( gVar.pHashHeadQueque, &(pSmtpPacket->item) );
	}
	
	return 1;
	
FAILED:
	for( i = 0; i < _MAX_CYC_ARRAY_DEPTH; i ++ )
	{
		if( gVar.TcpCycArrayHead.PacketArray[ i ] )
		{
			kfree( gVar.TcpCycArrayHead.PacketArray[ i ] );
			gVar.TcpCycArrayHead.PacketArray[ i ] = NULL;
		}
	}

	for( i = 0; i < _MAX_CYC_ARRAY_DEPTH; i ++ )
	{
		if( gVar.UdpCycArrayHead.PacketArray[ i ] )
		{
			kfree( gVar.UdpCycArrayHead.PacketArray[ i ] );
			gVar.UdpCycArrayHead.PacketArray[ i ] = NULL;
		}
	}

	if( gVar.pSmtpQueque )
		kfree( gVar.pSmtpQueque );
	
	if( gVar.pHttpQueque )
		kfree( gVar.pHttpQueque );
	
	if( gVar.pMsnQueque )
		kfree( gVar.pMsnQueque);
	
	if( gVar.pPop3Queque )
		kfree( gVar.pPop3Queque );
	
	if( gVar.pHttpUrlQueque )
		kfree( gVar.pHttpUrlQueque );
	
	if( gVar.pFtpQueque )
		kfree( gVar.pFtpQueque );
		
	if( gVar.pTelnetQueque )
		kfree( gVar.pTelnetQueque );
	
	if( gVar.pYahooQueque )
		kfree( gVar.pYahooQueque );
	
	if( gVar.pIcqQueque )
		kfree( gVar.pIcqQueque );
	
	if( gVar.pIrcQueque )
		kfree( gVar.pIrcQueque );
	
	if( gVar.pAolQueque )
		kfree( gVar.pAolQueque );
	
	if( gVar.pPpQueque )
		kfree( gVar.pPpQueque );
	
	if( gVar.pGtalkQueque )
		kfree( gVar.pGtalkQueque );
	
	if( gVar.pHashHeadQueque )
		kfree( gVar.pHashHeadQueque );

	if( gVar.pWebLoginFileQueque )	
		kfree( gVar.pWebLoginFileQueque );
	
	if( gVar.pWebMailFileQueque )
		kfree( gVar.pWebMailFileQueque );
	
	if( gVar.pBBSFileQueque )
		kfree( gVar.pBBSFileQueque );
		
	if( gVar.pMsnFileQueque )
		kfree( gVar.pMsnFileQueque );
	
	if( gVar.pFtpFileQueque )
		kfree( gVar.pFtpFileQueque );
	
	if( gVar.pTelnetFileQueque )
		kfree( gVar.pTelnetFileQueque );
	
	if( gVar.pIcqFileQueque )
		kfree( gVar.pIcqFileQueque );
	
	if( gVar.pYahooFileQueque )
		kfree( gVar.pYahooFileQueque );
		
	if( gVar.pFileQueque )
		kfree( gVar.pFileQueque );
	
	if( gVar.pHttpUrlFileQueque )
		kfree( gVar.pHttpUrlFileQueque );
		
	if( gVar.pPpFileQueque )
		kfree( gVar.pPpFileQueque );
	
	if( gVar.pGtalkFileQueque )
		kfree( gVar.pGtalkFileQueque );
	
	if( gVar.pIrcFileQueque )
		kfree( gVar.pIrcFileQueque );
	
	for( i = 0; i < _MAX_SMTP_HASH_BUCKET; i ++ )
		kfree( gVar.pSmtpHash[ i ] );
		
	for( i = 0; i < _MAX_HTTP_HASH_BUCKET; i ++ )
		kfree( gVar.pHttpHash[ i ] );
	
	for( i = 0; i < _MAX_MSN_HASH_BUCKET; i ++ )
		kfree( gVar.pMsnHash[ i ] );
		
	for( i = 0; i < _MAX_HTTP_POST_BUCKET; i ++ )
		kfree( gVar.pPostHash[ i ] );
		
	for( i = 0; i < _MAX_FTP_HASH_BUCKET; i ++ )
		kfree( gVar.pFtpHash[ i ] );	

	for( i = 0; i < _MAX_TELNET_HASH_BUCKET; i ++ )
		kfree( gVar.pTelnetHash[ i ] );
		
	for( i = 0; i < _MAX_ICQ_HASH_BUCKET; i ++ )
		kfree( gVar.pIcqHash[ i ] );

	for( i = 0; i < _MAX_YAHOO_HASH_BUCKET; i ++ )
		kfree( gVar.pYahooHash[ i ] );
	
	for( i = 0; i < _MAX_IRC_HASH_BUCKET; i ++ )
		kfree( gVar.pIrcHash[ i ] );
	
	for( i = 0; i < _MAX_AOL_HASH_BUCKET; i ++ )
		kfree( gVar.pAolHash[ i ] );
	
	for( i = 0; i < _MAX_PP_HASH_BUCKET; i ++ )
		kfree( gVar.pPpHash[ i ] );
	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -