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

📄 packethandle.c

📁 利用C++工具进行编程,NDIS的PASSTHRU层的驱动程序,是非常实用的程序.
💻 C
字号:
//PacketHandle.c
#include"precomp.h"

/****************Variable Declaration(External)*******************/
int iCurSavedPacketNum=iDefSavedPacketNum;//current number of saved packet.

SavedPacketEntry	*pSavedPacketEntry=NULL;//The Entry link of packet saved.

KSPIN_LOCK SavedPacketEntryLock;
KIRQL SavedPacketEntryIrql;


/*****************Function Declaration(Internal)***************/
//At First Judge whether the buffer can be free!
//If Can be to,You should Get the first buffer of the saved packet.
//		And Trace the buffer chain,free all buffer.
//else Only out ot Judge.
void ZapOneSavedPacketBuffer(
			SavedPacketEntry	*pInputEntry,
			int					iInputNo);



/***********Function Bodies****************/

//The Internal Function Only Delete the buffer which be defined(iInputNo).

//At First Judge whether the buffer can be free!
//If Can be to,You should Get the first buffer of the saved packet.
//		And Trace the buffer chain,free all buffer.
//else Only out ot Judge.
void ZapOneSavedPacketBuffer(
			SavedPacketEntry	*pInputEntry,
			int					iInputNo)
{					
	SavedPacketData *pTempSavedPacketData,*pTempNextSavedPacketData;

	if(pInputEntry[iInputNo].uFilterFlag)									
	{								
		//Loop the saved packet data link and free buffer.
		pTempSavedPacketData=pInputEntry[iInputNo].pSavedData;								
																			
		while( pTempSavedPacketData )													
		{																	
			pTempNextSavedPacketData=pTempSavedPacketData->next;										
																			
			ExFreePool(pTempSavedPacketData->pPacketData);								
																			
			pTempSavedPacketData=pTempNextSavedPacketData;											
		}																	
																			
		pInputEntry[iInputNo].pSavedData=NULL;								
		pInputEntry[iInputNo].uFilterFlag=0;
								
		pInputEntry[iInputNo].uTcpFilterFlag=0;
	}																		
}


/*External*/
//Initialize the buffer of saved packet.
//mainly allocate some buffer and initialize it.
//If Success,return zero,
//else return NoZero.
int InitPacketBuffer(void)
{
	int i;
	
	KeAcquireSpinLock(&SavedPacketEntryLock,&SavedPacketEntryIrql);
	
	//template allocate buffer must success!
	pSavedPacketEntry=(SavedPacketEntry *)ExAllocatePool(
				NonPagedPoolMustSucceed,
				iCurSavedPacketNum*sizeof(SavedPacketEntry)
			);

	//Set the Initialize value.
	for(i=0;i<iCurSavedPacketNum;i++)
	{
		pSavedPacketEntry[i].pSavedData=NULL;
		pSavedPacketEntry[i].uFilterFlag=0;

		pSavedPacketEntry[i].uTcpFilterFlag=0;
	}

	KeReleaseSpinLock(&SavedPacketEntryLock,SavedPacketEntryIrql);
	
	return 0;
}


/*External*/
//Free the buffer of saved packet.
//No return value.
void FreePacketBuffer(void)
{
	int i;
	
	KeAcquireSpinLock(&SavedPacketEntryLock,&SavedPacketEntryIrql);

	for(i=0;i<iCurSavedPacketNum;i++)
	{
		ZapOneSavedPacketBuffer( pSavedPacketEntry,	i);
	}
	
	//Free the entry.
	ExFreePool(pSavedPacketEntry);
	
	pSavedPacketEntry=NULL;

	iCurSavedPacketNum=0;
	
	KeReleaseSpinLock(&SavedPacketEntryLock,SavedPacketEntryIrql);
}


/*external Function*/
//Zap the buffer of saved packet.
//Zero it!!
//You can call it when you Create and Close the Gui device.
void ZapPacketBuffer(void)
{
	int i;
	
	KeAcquireSpinLock(&SavedPacketEntryLock,&SavedPacketEntryIrql);
	
	if(pSavedPacketEntry)
	{
		for(i=0;i<iCurSavedPacketNum;i++)
		{
			ZapOneSavedPacketBuffer( pSavedPacketEntry,	i);
			
			//Set the value to Initialize.
			pSavedPacketEntry[i].uFilterFlag=0;
			pSavedPacketEntry[i].pSavedData=NULL;

			pSavedPacketEntry[i].uTcpFilterFlag=0;

		}
		
	}

	KeReleaseSpinLock(&SavedPacketEntryLock,SavedPacketEntryIrql);
}


/*external Function*/
//ReAllocate the Buffer Size of saved packet.
//If success,return Zero;
//else return NoZero.
int ReAllocatePacketBuffer(int iInputReAllocSize)
{
	int i;
	int iTemp;
	int iTempCount;//Indicate How much item which not NULL.
	SavedPacketEntry *pTempEntry;
	SavedPacketData *pTempSavedPacketData,*pTempNextSavedPacketData;

	if(iInputReAllocSize<=0)
	{
		//Input parameter is error.
		return 1;
	}

	//It is not necessary to Must Success.
	pTempEntry=(SavedPacketEntry *)ExAllocatePool(
				NonPagedPool,
				iInputReAllocSize*sizeof(SavedPacketEntry)
				);

	if(pTempEntry==NULL)
	{
		//ReAllocate buffer Fail!
		return 1;
	}

	KeAcquireSpinLock(&SavedPacketEntryLock,&SavedPacketEntryIrql);
	
	//First copy the Item which not NULL from old to new.
	iTemp=0;
	iTempCount=0;
	while((iTemp<iCurSavedPacketNum) && (iTempCount<iInputReAllocSize))
	{
		if(pSavedPacketEntry[iTemp].uFilterFlag)
		{
			if(pSavedPacketEntry[iTemp].pSavedData)
			{
				//Have item which has data.
				//copy entry from old entry to new.
				pTempEntry[iTempCount].pSavedData=pSavedPacketEntry[iTemp].pSavedData;
		
				pTempEntry[iTempCount].uFilterFlag=pSavedPacketEntry[iTemp].uFilterFlag;

				pTempEntry[iTempCount].uTcpFilterFlag=pSavedPacketEntry[iTemp].uTcpFilterFlag;

				iTempCount++;
			}
		}
		
		iTemp++;
	}

	//The lest Item of New Entry Only set to Initialize.
	for(i=iTempCount;i<iInputReAllocSize;i++)
	{
		pTempEntry[i].pSavedData=NULL;
		pTempEntry[i].uFilterFlag=0;
	}
	
	//Free old entry,the lest item which not NULL will be deleted.
	for(i=iTemp;i<iCurSavedPacketNum;i++)
	{
		ZapOneSavedPacketBuffer( pSavedPacketEntry,	i);
	}

	//Free the old entry
	ExFreePool(pSavedPacketEntry);

	//New entry.
	pSavedPacketEntry=pTempEntry;

	iCurSavedPacketNum=iInputReAllocSize;

	KeReleaseSpinLock(&SavedPacketEntryLock,SavedPacketEntryIrql);

	return 0;
}


/*External Function*/
//copy the data from ndis packet.
//		If the	Variables:iInputCopyFlag(Input) == NoZero,
//			allocate a new buffer and copy All data to it.
//		Else copy bytes equare Or less uInputNumOfBytesToRead(Input) .
//(The Function Copy And Modified from http://www.pcausa.com)
//If Success return Zero,else return NoZero.
int CopyDataFromPacket(
		PNDIS_PACKET	pInputPacket,
		ULONG			uInputNumOfBytesToRead,
		U8				**ppOutputBuffer,
		ULONG			*puOutputNumOfBytesRead,
		int				iInputCopyFlag
	)
{
	UINT			iTempTotalPacketLength;//Total length of packet.
	PNDIS_BUFFER	pTempCurrentBuffer;//the current buffer of packet.
	PUCHAR			pTempVirtualAddress;//the address  of current buffer.
	UINT			iTempCurrentLength;//the length of current buffer.
	UINT			iTempCurrentOffset;//The current offset of Destinate buffer.
	
	int		iTempFlag;

	*puOutputNumOfBytesRead=0;
	iTempCurrentOffset=0;

	//Query Packet.
	NdisQueryPacket(
			pInputPacket,
			(PUINT)NULL,	
			(PUINT)NULL,
			&pTempCurrentBuffer,
			&iTempTotalPacketLength
		);

	if(iTempTotalPacketLength<=0)
	{
		//The packet 's parameter has error.
		return 1;
	}

	if(iInputCopyFlag)
	{
		//Allocate a new buffer to save the packet data.
		*ppOutputBuffer=ExAllocatePool(
			NonPagedPool,
			iTempTotalPacketLength
			);
		
		if( (*ppOutputBuffer) ==NULL)
		{
			//Allocate buffer to save packet Fail.
			return 1;
		}

	}else
	{
		if(iTempTotalPacketLength>uInputNumOfBytesToRead)
			iTempTotalPacketLength=uInputNumOfBytesToRead;
	}
	
	iTempFlag=1;

	while(iTempFlag)
	{
		NdisQueryBufferSafe(
				pTempCurrentBuffer,
				&pTempVirtualAddress,
				&iTempCurrentLength,
				HighPagePriority 
			);
		
		if(iTempCurrentOffset+iTempCurrentLength > iTempTotalPacketLength)
		{
			//Allocate the buffer too small.
			iTempCurrentLength=iTempTotalPacketLength-iTempCurrentOffset;

			//Should out of while next operate.
			iTempFlag=0;
		}
		
		//Copy the data.
		NdisMoveMemory(
				(*ppOutputBuffer)+iTempCurrentOffset,
				pTempVirtualAddress,
				iTempCurrentLength
			);
		
		//move the buffer's pointer.
		iTempCurrentOffset +=iTempCurrentLength;
		
		NdisGetNextBuffer(
				pTempCurrentBuffer,
				&pTempCurrentBuffer,
			);

		if(pTempCurrentBuffer==NULL)
		{
			//Reach the end of buffer.
			iTempFlag=0;
		}
	}

	*puOutputNumOfBytesRead=iTempTotalPacketLength;

	return 0;

}

⌨️ 快捷键说明

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