📄 packet.c
字号:
// Check for the available adapters to bind with
// not reqd for win ce. the os call packet bind adapter for
// every adapters installed in the system
if (PKTBindNames () != NDIS_STATUS_SUCCESS) {
NdisFreeMemory (g_pDeviceExtension, sizeof (DEVICE_EXTENSION), 0);
g_pDeviceExtension = NULL;
return 0;
}
// Status is generally succes`s over here
return PKT_DEVICE_CONTEXT;
}
/*
** Free all the resources allocated in PKT_Init()
*/
BOOL PKT_Deinit(DWORD dwContext)
{
NDIS_STATUS Status;
PLIST_ENTRY pHead;
PLIST_ENTRY pEntry;
PADAPTER_NAME pAName;
POPEN_INSTANCE pOI;
pOI = g_pDeviceExtension->pOpenInstance;
if (pOI != NULL) {
PKTCloseAdapter (pOI);
}
//free the names' list
if (!IsListEmpty (&g_pDeviceExtension->listAdapterNames)) {
pHead = &(g_pDeviceExtension->listAdapterNames);
if (pHead != NULL )
{
pEntry = RemoveTailList (pHead);
while (!IsListEmpty(pEntry)){
pAName = CONTAINING_RECORD (pEntry, ADAPTER_NAME, ListElement);
NdisFreeMemory (pAName, sizeof(ADAPTER_NAME), 0);
pEntry = RemoveTailList (pHead);
}
}
}
//unregister the protocol from NDIS
NdisDeregisterProtocol (&Status, g_pDeviceExtension->NdisProtocolHandle);
//free the global device extension
NdisFreeMemory (g_pDeviceExtension, sizeof (DEVICE_EXTENSION), 0);
g_pDeviceExtension = NULL;
return TRUE;
}
/*
** Handles IOCTLs passed using DeviceIOControl() function
*/
BOOL PKT_IOControl (DWORD dwContext, DWORD dwCode, PBYTE pBufIn,
DWORD dwLenIn, PBYTE pBufOut, DWORD dwLenOut, PDWORD pdwActualOut)
{
BOOL bRet = TRUE;
PWCHAR bpf_prog;
PNDIS_STRING pstrAName;
NDIS_STRING LsStrAName;
// NDIS_STRING pstrAName1;
POPEN_INSTANCE pOpenInst = NULL;
PUCHAR pTempBuf;
ULONG nValue;
PADAPTER_NAME pADName;
// if the context is different then return false
if (dwContext != PKT_FILE_HANDLE) {
SAFE_SET (pdwActualOut, 0);
return FALSE;
}
// if the code is not to open the adapter then there
// should be an open instance of the adapter
if (dwCode != PIOC_OPEN_ADAPTER &&
dwCode != PIOC_GET_MACNAME) {
// get the open instance handle
pOpenInst = g_pDeviceExtension->pOpenInstance;
if (pOpenInst == NULL) {
return FALSE;
}
}
switch (dwCode) {
// open the adapter message
case PIOC_OPEN_ADAPTER:
// UNICODE_STRING MyStrAName;
NdisAllocateMemory ((PVOID*) &pADName, sizeof (ADAPTER_NAME), 0 , NDIS_ADDR_M1);
if (pADName == NULL) {
return FALSE;
}
///王树 添加**********************
pADName->ndstrDeviceName.Length = wcslen ((PWCHAR)pBufIn) * 2;
pADName->ndstrDeviceName.MaximumLength = wcslen ((PWCHAR)pBufIn) * 2 + 2;
pADName->ndstrDeviceName.Buffer = pADName->chDeviceName;
wcsncpy (pADName->chDeviceName, (PWCHAR)pBufIn, 32);
// insert the adapter name in the global data structure
InsertTailList (&g_pDeviceExtension->listAdapterNames, &pADName->ListElement);
// get the adapter NDIS name
pstrAName = PKTGetNDISAdapterName ((PWCHAR)pBufIn, dwLenIn);
if (pstrAName == NULL) {
LsStrAName.Length= wcslen ((PWCHAR)pBufIn) * 2;
LsStrAName.MaximumLength= wcslen ((PWCHAR)pBufIn) * 2 + 2;
LsStrAName.Buffer= (PWCHAR)pBufIn;
pstrAName=&LsStrAName;
}
pSendBufLen=0;
pSendBuf=NULL;
////////////////**********************
// open the adapter
if (! PKTOpenAdapter (pstrAName)) {
bRet = FALSE;
}
break;
// close the adapter message
case PIOC_CLOSE_ADAPTER:
bDriverSendFlag=FALSE;
bRet = PKTCloseAdapter (pOpenInst);
break;
// function to set new bpf filter
case PIOC_SETF:
// free the previous buffer if selected
if (pOpenInst->bpfprogram != NULL) {
NdisFreeMemory (pOpenInst->bpfprogram, pOpenInst->bpfprogramlen, 0);
pOpenInst->bpfprogram = NULL;
pOpenInst->bpfprogramlen = 0;
}
// get the pointer to the new program
bpf_prog = (PWCHAR) pBufIn;
// before accepting the program we must check that it's valid
// otherwise, a bogus program could easily crash the driver
pOpenInst->bpfprogramlen = dwLenIn;
if (bpf_validate ((struct bpf_insn*)bpf_prog,
pOpenInst->bpfprogramlen/sizeof(struct bpf_insn)) == 0) {
pOpenInst->bpfprogramlen = 0;
pOpenInst->bpfprogram = NULL;
return FALSE; // filter not accepted
}
// allocate the memory to contain the new filter program*/
if (NdisAllocateMemory(&pOpenInst->bpfprogram, pOpenInst->bpfprogramlen,
0, NDIS_ADDR_M1) == NDIS_STATUS_FAILURE) {
pOpenInst->bpfprogramlen = 0;
pOpenInst->bpfprogram = NULL;
return FALSE;
}
// copy the program in the new buffer
NdisMoveMemory (pOpenInst->bpfprogram, bpf_prog, pOpenInst->bpfprogramlen);
// reset the buffer that could contain packets that don't match the filter
pOpenInst->Bhead = 0;
pOpenInst->Btail = 0;
pOpenInst->BLastByte= 0;
// return the accepted buffer len
SAFE_SET(pdwActualOut, pOpenInst->bpfprogramlen);
break;
// function to set the internal buffer size
case PIOC_SET_BUFFER_SIZE:
// get the size to allocate
nValue = ((PULONG)pBufIn)[0];
// free the old buffer
if (pOpenInst->Buffer != NULL) {
NdisFreeMemory (pOpenInst->Buffer, pOpenInst->BufSize, 0);
}
pOpenInst->Buffer = NULL;
// allocate the new buffer
if (nValue > 0) {
NdisAllocateMemory ((PVOID*)&pTempBuf, nValue, 0, NDIS_ADDR_M1);
if (pTempBuf == NULL) {
pOpenInst->BufSize = 0;
return FALSE;
}
pOpenInst->Buffer = pTempBuf;
}
pOpenInst->Bhead = 0;
pOpenInst->Btail = 0;
pOpenInst->BLastByte= 0;
pOpenInst->BufSize = (UINT)nValue;
SAFE_SET(pdwActualOut, nValue);
break;
// for sharing the event from the user
case PIOC_EVNAME:
if (pBufOut == NULL || dwLenOut < 32) {
SAFE_SET(pdwActualOut, 0);
bRet = FALSE;
} else {
wcscpy ((PWCHAR)pBufOut, SH_EVENT_NAME);
SAFE_SET (pdwActualOut, wcslen (SH_EVENT_NAME));
}
break;
///王树 2006-03-31 ////
/// 设置丢弃几个包
// case PIOC_SET_JUMP_PACKET:
// SAFE_SET (pdwActualOut, GetFileCount);
// return PKTJumpPacket(pOpenInst,pBufOut);
case PIOC_SETWRITE_PACKETFLAG:
pSendBuf=pBufIn;
pSendBufLen=dwLenOut;
break;
case PIOC_SET_DRIVERSEND: //设置是否由驱动程序发送数据
pSendBufIn=pBufIn;
pSendBufStart=pBufIn;
lPacketCount=dwLenIn;
if(lPacketCount>2){
bDriverSendFlag=TRUE;
lPacketIndex=0;
lStartPacket=TRUE;
pBufOut=(PBYTE)(&pOpenInst->SendFlagEvent);
SAFE_SET(pdwActualOut, 0);
}
break;
///////////////////////////////////////
// set read timeout function
case PIOC_SRTIMEOUT:
pOpenInst->TimeOut = ((PULONG)pBufIn)[0];
SAFE_SET(pdwActualOut, pOpenInst->TimeOut);
break;
// resets the adapter instance
case PIOC_RESET:
bRet = PKTReset (pOpenInst);
break;
// requests of different type
case PIOC_SETOID:
case PIOC_QUERYOID:
return PKTRequest (pOpenInst, dwCode, pBufIn, dwLenIn, pBufOut, dwLenOut, pdwActualOut);
break;
// read packets
case PIOC_READ_PACKET:
return PKTRead (pOpenInst, pBufOut, dwLenOut, pdwActualOut);
break;
// write packets
case PIOC_WRITE_PACKET:
return PKTWrite (pOpenInst, pBufIn, dwLenIn, pdwActualOut);
break;
// read mac name
case PIOC_GET_MACNAME:
*pdwActualOut = PKTGetMacNameList ((PWCHAR)pBufOut, dwLenOut);
break;
// minimum number of bytes to copy
case PIOC_SMINTOCOPY:
pOpenInst->MinToCopy = ((PULONG)pBufIn)[0];
SAFE_SET(pdwActualOut, 0);
break;
// unknown function code. set out length to 0 and return true
default:
bRet = FALSE;
SAFE_SET(pdwActualOut, 0);
break;
}
return bRet;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -