www.pudn.com > IpFilter.rar > IpHookGlobal.h


/////////////////////////////////////////////////////////////////////////////// 
// 
//	(C) Copyright 1999 - 2000 Mark Roddy 
//	All Rights Reserved 
// 
//	Hollis Technology Solutions 
//	94 Dow Road 
//	Hollis, NH 03049 
//	info@hollistech.com 
// 
//	Synopsis:  
//  
// 
//	Version Information: 
// 
//	$Header: /iphook/sys/driver/IpHookGlobal.h 4     1/27/00 10:35p Markr $  
// 
/////////////////////////////////////////////////////////////////////////////// 
#pragma once 
// 
// this was a simple structure but alas it has developed class 
// it ought to be a class in fact and as it is the single global 
// data object it ought to enforce its singleton nature. 
// 
class IP_HOOK_GLOBAL_DATA { 
 
private: 
 
	// 
	// we track the thread that started a hook 
	// 
	PVOID owningContext; 
	PDEVICE_OBJECT IpfDeviceObject; 
	PFILE_OBJECT IpfFileObject; 
	ULONG IpHookSequence; 
	// 
	// we don't know what the actual situation 
	// is here - are we called serially at our 
	// hook function? Are we called at DISPATCH_LEVEL? 
	// 
	// until we figure this out use a spinlock. 
	//  
	spinLock Lock; 
	 
	LIST_ENTRY queue; 
	LIST_ENTRY cancelQueue; 
	PIRP currentIrp; 
	PIPHOOK_BUFFER currentBuffer; 
 
	// 
	// a worker thread wakes up every now and then and flushes the 
	// current buffer 
	// 
	HANDLE  workerThread; 
	PVOID workerThreadObject; 
	KEVENT	 stopEvent; 
 
 
public: 
 
	NTSTATUS createThread(); 
 
	NTSTATUS initDevice(); 
 
	void queueRequest(PIRP Irp); 
 
	PIRP popQueue(BOOLEAN locked = FALSE);	 
 
	PIPHOOK_DATA getBuffer(); 
 
	void flushBuffer(BOOLEAN locked = FALSE); 
 
	void flushBufferIfData(); 
 
	void releaseAllBuffers(BOOLEAN locked = FALSE); 
 
	IP_HOOK_GLOBAL_DATA(); 
 
	~IP_HOOK_GLOBAL_DATA(); 
 
	BOOLEAN IsOwner(PVOID context) 
	{ 
		if (context == owningContext) { 
 
			return TRUE; 
		} 
 
		return FALSE; 
	} 
 
	BOOLEAN owned() 
	{ 
		return (owningContext != NULL); 
	} 
 
	PVOID owner() 
	{ 
		return owningContext; 
	} 
 
	BOOLEAN setOwner(PVOID context) 
	{ 
		// 
		// we sort of kinda hafta serialize this 
		// 
		PVOID inUse =  
			InterlockedCompareExchangePointer(&owningContext,  
											  context, 
											  NULL); 
 
		if (inUse != NULL) { 
 
			return FALSE; 
		} 
 
		return TRUE; 
	} 
 
	void freeOwner(PVOID context) 
	{ 
		PVOID lockContext; 
 
		lock(lockContext); 
 
		if (IsOwner(context)) { 
 
			releaseAllBuffers(TRUE); 
			 
			InterlockedExchangePointer(&owningContext, NULL); 
		} 
 
		unlock(lockContext); 
	} 
 
	ULONG getSequence() 
	{ 
		return InterlockedIncrement((PLONG)&IpHookSequence); 
	} 
 
	PDEVICE_OBJECT getIpDevice() 
	{ 
		return IpfDeviceObject; 
	} 
 
	void lock(PVOID& context) 
	{ 
		Lock.lock(context); 
	} 
 
	void unlock(PVOID context) 
	{ 
		Lock.unlock(context); 
	} 
 
}; 
/////////////////////////////////////////////////////////////////////////////// 
//  
// Change History Log 
// 
// $Log: /iphook/sys/driver/IpHookGlobal.h $ 
//  
// 4     1/27/00 10:35p Markr 
// Prepare to release! 
// 
///////////////////////////////////////////////////////////////////////////////