📄 packet.c
字号:
#include <windows.h>
#include "packet.h"
#include "filter.h"
// Global Variables for the Protocol Device Driver
PDEVICE_EXTENSION g_pDeviceExtension = NULL;
//////////////////////////
BOOL bDriverSendFlag=FALSE;
PVOID pSendBufIn=NULL;
PVOID pSendBufStart=NULL;
long lPacketIndex=0; //当前包编号
long lPacketCount=0; //包缓冲数量
BOOL lStartPacket=FALSE;
/////////////////
const NDIS_PHYSICAL_ADDRESS NDIS_ADDR_M1 = {(LONG)(-1), (LONG)-1};
/*
* This function is called bu NDIS when there is some thing to communicate to
* the upper level. Thid function does not requires any implementation.
*
*/
VOID PacketStatus(IN NDIS_HANDLE ProtocolBindingContext,
IN NDIS_STATUS Status,
IN PVOID StatusBuffer,
IN UINT StatusBufferSize)
{
return;
}
/*
* This function is the complete handler for the PacketStatus.
* this function is not required.
*
*/
VOID PacketStatusComplete (IN NDIS_HANDLE ProtocolBindingContext)
{
return;
}
/*
* This function add the adapter names to the list
*
*/
NDIS_STATUS PKTBindNames ()
{
HKEY hKeyComm;
HKEY hKeyAdap;
HKEY hKeyLink;
DWORD dwCnt = 0;
DWORD dwIndex = 0;
WCHAR chName[512];
DWORD dwName = 512;
PADAPTER_NAME pADName;
PIP_ADAPTER_INFO pAdapterInfo = NULL;
ULONG ulSizeAdapterInfo = 0;
// DWORD dwReturnvalueGetAdapterInfo;
NDIS_STATUS STATUS_GETADAPT;
WCHAR adaptName[512];
DWORD buflen=512;
DWORD bufoutlen;
DWORD dwCount;
// open the comm key
if (ERROR_SUCCESS != RegOpenKeyEx (HKEY_LOCAL_MACHINE, L"Comm", 0,
0, &hKeyComm)) {
return NDIS_STATUS_FAILURE;
}
// enumerate all the sub keys in the comm
while (ERROR_SUCCESS == RegEnumKeyEx (hKeyComm, dwIndex++, chName,
&dwName, NULL, NULL, NULL, NULL)) {
// open the adapter key
if (ERROR_SUCCESS == RegOpenKeyEx (hKeyComm, chName, 0,
0, &hKeyAdap)) {
// open the linkage key
if (ERROR_SUCCESS == RegOpenKeyEx (hKeyAdap, L"Linkage", 0,0, &hKeyLink))
{
dwName = 512;
// query the route value
if (ERROR_SUCCESS == RegQueryValueEx (hKeyLink, L"Route", NULL,
NULL, (LPBYTE) chName, &dwName))
{
// store the name in the list
// we can also get the detailed name here
// may be in next version
// allocate the memory that describes the adapter
NdisAllocateMemory ((PVOID*) &pADName, sizeof (ADAPTER_NAME), 0 , NDIS_ADDR_M1);
if (pADName == NULL) {
return NDIS_STATUS_FAILURE;
}
// copy the device name in the adapter name structure
pADName->ndstrDeviceName.Length = wcslen (chName) * 2;
pADName->ndstrDeviceName.MaximumLength = wcslen (chName) * 2 + 2;
pADName->ndstrDeviceName.Buffer = pADName->chDeviceName;
wcsncpy (pADName->chDeviceName, chName, 32);
// insert the adapter name in the global data structure
InsertTailList (&g_pDeviceExtension->listAdapterNames, &pADName->ListElement);
dwCnt++;
}
// close linkage
RegCloseKey (hKeyLink);
}
// close the adapter key
RegCloseKey (hKeyAdap);
}
dwName = 512;
}
// close the comms key
RegCloseKey (hKeyComm);
NdisGetAdapterNames(&STATUS_GETADAPT,(PBYTE)adaptName,buflen,&bufoutlen);
if(STATUS_GETADAPT==NDIS_STATUS_SUCCESS)
{
dwCount=0;
while(1)
{
NdisAllocateMemory ((PVOID*) &pADName, sizeof (ADAPTER_NAME), 0 , NDIS_ADDR_M1);
if (pADName == NULL)
{
return NDIS_STATUS_FAILURE;
}
// copy the device name in the adapter name structure
pADName->ndstrDeviceName.Length = wcslen (adaptName+dwCount) * 2;
pADName->ndstrDeviceName.MaximumLength = wcslen (adaptName+dwCount) * 2 + 2;
pADName->ndstrDeviceName.Buffer = pADName->chDeviceName;
wcsncpy (pADName->chDeviceName, adaptName+dwCount, 32);
// insert the adapter name in the global data structure
InsertTailList (&g_pDeviceExtension->listAdapterNames, &pADName->ListElement);
dwCnt++;
dwCount += wcslen (adaptName + dwCount);
if (adaptName[dwCount] == L'\0' &&
adaptName[dwCount + 1] == L'\0') {
break;
}
dwCount ++;
}
}
///////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
if (dwCnt == 0) {
SetLastError (ERROR_BAD_NET_NAME);
return NDIS_STATUS_FAILURE;
}
return NDIS_STATUS_SUCCESS;
}
/*
* The DllEntry Routine. This function is called by the system whenever a
* process attaches/detaches to the dll. The function does nothing.
*
* Arguments
* hinstDLL - The instance handle of the dll
* dwReason - The reason for which the function is called
* lpReserved - Reserved parameter not used
*
* Return Value
* Always return TRUE
*
*/
BOOL WINAPI DllEntry(HINSTANCE hinstDll, DWORD dwReason, LPVOID lpReserved)
{
switch(dwReason) {
case DLL_PROCESS_ATTACH:
break;
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
/*
* Function called to reset the adapter
*
*/
BOOL PKTReset (POPEN_INSTANCE pOI)
{
NDIS_STATUS Status;
// Call NDIS to reset the adapter
NdisReset (&Status, pOI->AdapterHandle);
if (Status == NDIS_STATUS_PENDING) {
SuspendExecution (pOI);
} else {
PacketResetComplete (pOI, Status);
}
if (Status != NDIS_STATUS_SUCCESS) {
return FALSE;
}
return TRUE;
}
/*
* Reset complete handler
*
*/
VOID PacketResetComplete (IN NDIS_HANDLE ProtocolBindingContext,
IN NDIS_STATUS Status)
{
POPEN_INSTANCE pOI;
pOI = (POPEN_INSTANCE)ProtocolBindingContext;
// set the status
ResumeExecution (pOI, Status);
return;
}
VOID PacketTransferDataComplete (IN NDIS_HANDLE ProtocolBindingContext,
IN PNDIS_PACKET Packet, IN NDIS_STATUS Status,
IN UINT BytesTransferred)
{
}
/*
* This routine initializes the packet driver. The function is called by the
* driver manager automatically when RegisterDevice function is called.
*
* Arguments
* dwContext - Specifies a pointer to a string containing the
* registry path to the active key for the stream
* interface driver.
*
* Return Value
* The function return the device context to be called with other stream
* function like _Open, _Close, etc. If the initialization fails then the
* function returns NULL. If the function returns 0 then the driver is not
* loaded by the device manager.
*
*/
DWORD PKT_Init(DWORD dwContext)
{
NDIS_PROTOCOL_CHARACTERISTICS ProtocolChar;
NDIS_STRING ProtoName = PROTOCOL_NAME;
NDIS_STATUS Status;
// Allocate memory for the global device extension
NdisAllocateMemory ((PVOID*) &g_pDeviceExtension, sizeof (DEVICE_EXTENSION),
0, NDIS_ADDR_M1);
if (g_pDeviceExtension == NULL) {
return 0;
}
// reset the global device extension object
NdisZeroMemory (g_pDeviceExtension, sizeof (DEVICE_EXTENSION));
// reset the protocol characteristics object
NdisZeroMemory (&ProtocolChar, sizeof (NDIS_PROTOCOL_CHARACTERISTICS));
// Initialize the protocol char structure
ProtocolChar.MajorNdisVersion =0x04;// 0x03;
ProtocolChar.MinorNdisVersion = 0x00;//0x00;
ProtocolChar.Reserved = 0;
ProtocolChar.Name = ProtoName;
ProtocolChar.BindAdapterHandler = PacketBindAdapter;
ProtocolChar.CloseAdapterCompleteHandler = PacketCloseAdapterComplete;
ProtocolChar.OpenAdapterCompleteHandler = PacketOpenAdapterComplete;
ProtocolChar.ReceiveHandler = PacketReceive;
ProtocolChar.ReceiveCompleteHandler = PacketReceiveComplete;
ProtocolChar.RequestCompleteHandler = PacketRequestComplete;
ProtocolChar.ResetCompleteHandler = PacketResetComplete;
ProtocolChar.StatusHandler = PacketStatus;
ProtocolChar.StatusCompleteHandler = PacketStatusComplete;
ProtocolChar.TransferDataCompleteHandler = PacketTransferDataComplete;
ProtocolChar.UnbindAdapterHandler = PacketUnbindAdapter;
ProtocolChar.SendCompleteHandler = PacketWriteComplete;
// Registed the protocol handler
NdisRegisterProtocol (&Status, &g_pDeviceExtension->NdisProtocolHandle,
&ProtocolChar, sizeof(NDIS_PROTOCOL_CHARACTERISTICS));
// Check the return value
if (Status != NDIS_STATUS_SUCCESS) {
NdisFreeMemory (g_pDeviceExtension, sizeof (DEVICE_EXTENSION), 0);
return 0;
}
// Initialize the list headers
InitializeListHead (&g_pDeviceExtension->listAdapterNames);
// Initialize the open instance pointer
g_pDeviceExtension->pOpenInstance = NULL;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -