// =============================================================================
//	PlugBase.h
// =============================================================================
//	Copyright (c) WildPackets, Inc. 2000-2001. All rights reserved.
//	Copyright (c) AG Group, Inc. 1997-2000. All rights reserved.

#ifndef PLUGBASE_H
#define PLUGBASE_H

#include "StdAfx.h"
#include "PeekPlug.h"

using namespace PeekPlugin;

#if TARGET_OS_MAC
	#include <new>
	typedef PeekPlugCallbacks * HMODULE;
	typedef Ptr	FARPROC;
	// CFM routines made to work like windows calls.
	static HMODULE GetModuleHandle( const TCHAR* inClientAppDataP );
	static FARPROC GetProcAddress( HMODULE inModule, const TCHAR* inProcName );
#endif

#if TARGET_OS_WIN32
	#define		kPeekPluginResourceFolder	_T("PluginRes")
#endif
class CPeekPlugin
{
public:
					CPeekPlugin();
	virtual			~CPeekPlugin();

#if _UNICODE
	virtual int		HandleMessageW( SInt16 inMessage, PluginParamBlock* ioParams );
	#define HandleMessage	HandleMessageW
#else
	virtual int		HandleMessageA( SInt16 inMessage, PluginParamBlock* ioParams );
	#define HandleMessage	HandleMessageA
#endif

#if TARGET_OS_WIN32
	virtual BOOL	DllMain( HINSTANCE inInstance, DWORD inReason, LPVOID inReserved );
#endif	// TARGET_OS_WIN32

	// Utility routines.
	static bool		IsMACFrame( const UInt8* inPacketData );
	static UInt16	GetSourceRouteInfoLength( const PluginPacket* inPacket,
						const UInt8* inPacketData );
	static UInt16	GetPacketActualLength( const PluginPacket* inPacket );
	static int		GetTCPUDPInfo( UInt32 inProtoSpecMatched,
			const PluginPacket* inPacket, const UInt8* inPacketData, 
			UInt8 inMediaType, UInt8 inMediaSubType,
			const UInt8** outHdrData, UInt16* outHdrBytes, 
			const UInt8** outData, UInt16* outDataBytes,
			UInt32* outDeliveryProtocol );
	static int		GetIPInfo( UInt32 inProtoSpecMatched,
			const PluginPacket* inPacket, const UInt8* inPacketData, 
			UInt8 inMediaType, UInt8 inMediaSubType,
			const UInt8** outHdrData, UInt16* outHdrBytes, 
			const UInt8** outData, UInt16* outDataBytes,
			UInt32* outDeliveryProtocol );

	// Load string from resource.
	bool			LoadString( UInt32 inID, TCHAR* outString, int nBufSize=256 ) const;

	//
	// Wrappers for callback functions.
	//
	static int		DoGetProtocolName( UInt32 inProtocol, TCHAR* outString );
	static int		DoGetProtocolLongName( UInt32 inProtocol, TCHAR* outString );
	static int		DoGetProtocolHierName( UInt32 inProtocol, TCHAR* outString );
	static int		DoGetProtocolParent( UInt32 inProtocol, UInt32* outProtocolParent );
	static int		DoGetProtocolColor( UInt32 inProtocol, COLORREF* outColor );
	static int		DoIsDescendentOf( UInt32 inSubProtocol, const UInt32* inParentArray, SInt32 inParentCount, UInt32 *outMatchID );

	static int		DoNotify( PluginAppContext inAppContext, PluginCaptureContext inCaptureContext, UInt64 inTimeStamp, UInt8 inSeverity, const TCHAR* inShortString, const TCHAR* inLongString );

	static int		DoLookupName( const UInt8* inEntry, UInt16 inEntryType, TCHAR* outName, UInt8* outType );
	static int		DoLookupName( PluginNameTableEntry* ioEntry );
	static int		DoLookupEntry( const TCHAR* inName, UInt16 inEntryType, UInt8* outEntry );
	static int		DoLookupEntry( PluginNameTableEntry* ioEntry );
	static int		DoAddNameEntry( const TCHAR* inName, const TCHAR* inGroup, const UInt8* inEntry, UInt16 inEntryType, UInt16 inOptions );
	static int		DoAddNameEntry( const PluginNameTableEntry* inEntry, UInt16 inOptions );
	
	
	static int		DoSummaryGetEntry( PluginAppContext inAppContext, PluginCaptureContext inCaptureContext, const TCHAR* inLabel, const TCHAR* inGroup, UInt32* outType, void** outData, UInt8* outSource );
	static int		DoSummaryModifyEntry( PluginAppContext inAppContext, PluginCaptureContext inCaptureContext, const TCHAR* inLabel, const TCHAR* inGroup, UInt32 inType, void* inData );

	static const UInt8*	DoPacketGetLayer( UInt8 inLayerType, UInt8 inMediaType, UInt8 inMediaSubType, const PluginPacket*	inPacket,
						const UInt8* inPacketData, UInt16* ioBytesLeft );
	static const UInt8*	DoPacketGetDataLayer( UInt32 inProtoSpec, UInt8 inMediaType, UInt8 inMediaSubType, const PluginPacket*	inPacket,
						const UInt8* inPacketData, UInt16* ioBytesLeft, UInt32* outSourceProtoSpec );
	static const UInt8*	DoPacketGetHeaderLayer( UInt32 inProtoSpec, UInt8 inMediaType, UInt8 inMediaSubType, const PluginPacket*	inPacket,
						const UInt8* inPacketData, UInt16* ioBytesLeft, UInt32* outSourceProtoSpec );
	static int		DoPacketGetAddress( PluginAppContext inAppContext, PluginCaptureContext inCaptureContext, UInt8 inAddressSelector, UInt8 inMediaType, UInt8 inMediaSubType, const PluginPacket* inPacket, const UInt8* inPacketData, UInt64 inPacketNumber, UInt8* outAddress, UInt16* outAddressType );

	static int		DoPrefsGetValue( PluginAppContext inAppContext, PluginCaptureContext inCaptureContext, const TCHAR* inName, const void* outData, UInt32* ioLength );
	static int		DoPrefsSetValue( PluginAppContext inAppContext, PluginCaptureContext inCaptureContext, const TCHAR* inName, const void* inData, UInt32 inLength );
	
	static int		DoSendPacket( const UInt8* inPacketData, UInt16 inPacketLength );
	static int		DoInsertPacket( PluginAppContext inAppContext, PluginCaptureContext inCaptureContext, const PluginPacket* inPacket, const UInt8* inPacketData, UInt8 inMediaType, UInt8 inMediaSubType, UInt32 inReserved );
	static int		DoSelectPackets( PluginAppContext inAppContext, PluginCaptureContext inCaptureContext, UInt32 inNumPackets, UInt64* inPacketNumbers, UInt32 inFlags );
#if TARGET_OS_WIN32
	static int		DoAddTab( PluginAppContext inAppContext, PluginCaptureContext inCaptureContext, const TCHAR* inTabName, const TCHAR* inWindowClass, void** outTabWnd );
	static int		DoSelectPacketsEx( PluginAppContext inAppContext, PluginCaptureContext inCaptureContext, 
								   const UInt8* inSrcAddr, UInt16 inSrcAddrType, const UInt8* inDestAddr, UInt16 inDestAddrType,
								   UInt16 inSrcPort, UInt16 inDestPort, UInt16 inPortTypes, bool inBothDirections, UInt32 inFlags );
	static int		DoMakeFilter( const UInt8* inSrcAddr, UInt16 inSrcAddrType, const UInt8* inDestAddr, UInt16 inDestAddrType, 
								  UInt16 inSrcPort, UInt16 inDestPort, UInt16 inPortTypes, bool inBothDirections );
	static int		DoClaimPacketString( PluginAppContext inAppContext, PluginCaptureContext inCaptureContext, UInt64 inPacketNumber, bool inExpert );
	static int		DoInvokeNameEditDialog( PluginNameTableEntry* inEntry );
	static int		DoPrefsGetPrefsPath(wchar_t* outString );
	static int		DoResolveAddress( UInt8* inAddress, UInt16 inAddressType );
#elif TARGET_OS_MAC
	static void		DoEventDispatch( const EventRecord* inEvent );
	static int		DoGetProtocolColor( UInt16 inProtocol, RGBColor* outColor );
#endif

protected:
	UInt32							m_APIVersion;
	static GetProtocolNameProc		s_procGetProtocolName;
	static GetProtocolLongNameProc	s_procGetProtocolLongName;
	static GetProtocolHierNameProc	s_procGetProtocolHierName;
	static GetProtocolParentProc	s_procGetProtocolParent;
	static GetProtocolColorProc		s_procGetProtocolColor;
	static IsDescendentOfProc		s_procIsDescendentOf;
	static NotifyProc				s_procNotify;
	static LookupNameProc			s_procLookupName;
	static LookupEntryProc			s_procLookupEntry;
	static AddNameEntryProc			s_procAddNameEntry;
	static SendPacketProc			s_procSendPacket;
	static SummaryModifyEntryProc	s_procSummaryModifyEntry;
	static SummaryGetEntryProc		s_procSummaryGetEntry;
	static PacketGetLayerProc		s_procPacketGetLayer;
	static PacketGetDataLayerProc	s_procPacketGetDataLayer;
	static PacketGetHeaderLayerProc	s_procPacketGetHeaderLayer;
	static PacketGetAddressProc		s_procPacketGetAddress;
	static PrefsGetValueProc		s_procPrefsGetValue;
	static PrefsSetValueProc		s_procPrefsSetValue;
	static InsertPacketProc			s_procInsertPacket;
	static SelectPacketsProc		s_procSelectPackets;
	void*							m_pAppContextData;
#if TARGET_OS_MAC
	// Mac-specific.
	static EventDispatchProc		s_procEventDispatch;	// MacOS event dispatch proc.
	CFBundleRef						mBundle;
#elif TARGET_OS_WIN32
	// Windows-specific.
	static AddTabProc				s_procAddTab;
	static SelectPacketsExProc		s_procSelectPacketsEx;
	static ClaimPacketStringProc	s_procClaimPacketString;
	static InvokeNameEditDialogProc	s_procInvokeNameEditDialog;
	static MakeFilterProc			s_procMakeFilterProc;
	static PrefsGetPrefsPathProc	s_procPrefsGetPrefsPathProc;
	static ResolveAddressProc		s_procResolveAddressProc;
	static GetAppResourcePathProc	s_procGetAppResourcePathProc;
	private:
		HINSTANCE						m_hInstance;			// Calling app's instance handle.
		HINSTANCE						m_hResInstance;	
	protected:
	TCHAR*							m_szResourceFilename;
#if !_UNICODE && !UNICODE
	UINT							m_nCodePage;			// Code page from User's default, for ASCII build
#endif

#endif
	bool							m_bIsPeekUnicode;

	virtual void	GetName( TCHAR* outName ) const = 0;
	virtual void	GetID( PluginID* outID ) const = 0;
	virtual UInt16	GetAttributes() const = 0;
	virtual UInt16	GetSupportedActions() const = 0;
	virtual UInt16	GetDefaultActions() const = 0;
	virtual void	GetSupportedProtoSpecs( UInt32* outNumProtoSpecs,
						UInt32** outProtoSpecs ) const;

	virtual int		Load( PluginLoadParam* ioParams );
	virtual int		Unload();
	virtual int		ProcessPacket( PluginProcessPacketParam* ioParams );
	virtual int		GetPacketString( PluginGetPacketStringParam* ioParams );
	virtual int		GetPacketAnalysis( PluginGetPacketStringParam* ioParams );
	virtual int		Apply( PluginApplyParam* ioParams );
	virtual int		Select( PluginSelectParam* ioParams );
	virtual int		Reset( PluginResetParam* ioParams );
	virtual int		StartCapture( PluginStartCaptureParam* ioParams );
	virtual int		StopCapture( PluginStopCaptureParam* ioParams );
	virtual int		PacketsLoaded( PluginPacketsLoadedParam* ioParams );
	virtual int		About();
	virtual int		Options();
	virtual int		ReadPrefs();
	virtual int		WritePrefs();
	virtual int		Summary( PluginSummaryParam* ioParams );
	virtual int		CreateContext( PluginCreateContextParam* ioParams );
	virtual int		DisposeContext( PluginDisposeContextParam* ioParams );
	virtual int		Filter( PluginFilterParam* ioParams );
	virtual int		FilterOptions();
	virtual int		NameTableUpdate( PluginNameTableUpdateParam* ioParams );
	virtual int		GetAdapterList( PluginGetAdapterListParam* ioParams );
	virtual int		CreateNewAdapter( PluginCreateNewAdapterParam* ioParams );
	virtual int		DeleteAdapter( PluginDeleteAdapterParam* ioParams );
	virtual int		GetAdapterAttribs( PluginAdapterAttribsParam* ioParams );
	virtual int		SetAdapterAttribs( PluginAdapterAttribsParam* ioParams );
	virtual int     DoAdapterProperties( PluginAdapterPropertiesParam* ioParams );
#if TARGET_OS_WIN32
	virtual HINSTANCE	GetResourceHandle();
	private:
		bool		m_bResourceDllLoaded;
#endif
};

// Implements the plugin's main routine.
// Declares a static instance of a CPeekPlugin derived
// class and passes all messages to it.
#if TARGET_OS_MAC
#define IMPLEMENT_PLUGIN(c) \
extern "C" pascal int PlugInMain( SInt16 inMessage, PluginParamBlock* ioParams ); \
##c	sPluginInstance; \
pascal int PlugInMain( SInt16 inMessage, PluginParamBlock* ioParams ) \
{ \
	return sPluginInstance.HandleMessage( inMessage, ioParams ); \
}
#pragma export on
extern "C" pascal int PlugInMain( SInt16 inMessage, PluginParamBlock* ioParams );
#pragma export off
#elif TARGET_OS_WIN32
#define IMPLEMENT_PLUGIN(c) \
extern "C" int WINAPI PlugInMain( SInt16 inMessage, PluginParamBlock* ioParams ); \
##c	sPluginInstance; \
extern "C" BOOL WINAPI DllMain( HINSTANCE inInstance, DWORD inReason, LPVOID inReserved ) \
{ \
	return sPluginInstance.DllMain( inInstance, inReason, inReserved ); \
} \
int WINAPI PlugInMain( SInt16 inMessage, PluginParamBlock* ioParams ) \
{ \
	return sPluginInstance.HandleMessage( inMessage, ioParams ); \
}
#endif

#endif /* PLUGBASE_H */
