filter.c
资源名称:passthru.rar [点击查看]
上传用户:sabrinaco
上传日期:2016-01-19
资源大小:3177k
文件大小:5k
源码类别:
防火墙与安全工具
开发平台:
Visual C++
- #include "precomp.h"
- #pragma hdrstop
- NDIS_STATUS Filter(IN PVOID HeaderBuffer,IN UINT PacketSize, IN UINT HeaderBufferSize,IN PVOID LookAheadBuffer) //过滤规则设置
- {
- ULONG i,j,k=0,t1=0,t2=0;
- for(j=0;j<usernum;j++)
- {
- t1=0;
- t2=0;
- for(i=0;i<6;i++)
- {
- if(mac[j][i]==((unsigned char*)HeaderBuffer)[i])
- {
- t1++;
- }
- if(mac[j][i]==((unsigned char*)HeaderBuffer)[i+6])
- {
- t2++;
- }
- }
- if(t1==6||t2==6) //源、目的mac地址其中一个满足条件就接收包
- {
- if(((unsigned char*)HeaderBuffer)[12]==8&&((unsigned char*)HeaderBuffer)[13]==0)
- {
- for(k=0;k<4;k++)
- {
- if(t1==6)
- ip[j][k]=((unsigned char*)LookAheadBuffer)[k+16];
- else
- ip[j][k]=((unsigned char*)LookAheadBuffer)[k+12];
- }
- }
- if(num[j]==0) return NDIS_STATUS_RESOURCES;
- else packetnum[j]=packetnum[j]+(PacketSize+HeaderBufferSize)*8;
- return NDIS_STATUS_SUCCESS;
- }
- }
- return NDIS_STATUS_RESOURCES;
- }
- NDIS_STATUS GetBindingList(IN PVOID Buffer,IN ULONG Length, IN OUT PULONG DataLength)//获取适配器列表
- {
- PADAPT pAdapt;
- ULONG nRequiredLength=0;
- ULONG nAdapters=0;
- NdisAcquireSpinLock(&GlobalLock);
- pAdapt = pAdaptList;
- while(pAdapt != NULL)
- {
- nAdapters++;
- pAdapt = pAdapt->Next;
- }
- *(PULONG)Buffer = nAdapters;
- Buffer = (PCHAR)Buffer + sizeof(ULONG);
- nRequiredLength+=sizeof(ULONG);
- pAdapt=pAdaptList;
- while(pAdapt!=NULL)
- {
- NdisMoveMemory(Buffer,pAdapt->DeviceName.Buffer,pAdapt->DeviceName.Length+sizeof(WCHAR));
- Buffer=(PCHAR)Buffer+pAdapt->DeviceName.Length+sizeof(WCHAR);
- nRequiredLength+=pAdapt->DeviceName.Length+sizeof(WCHAR);
- pAdapt=pAdapt->Next;
- }
- if(nRequiredLength > Length)
- {
- NdisReleaseSpinLock(&GlobalLock);
- return STATUS_BUFFER_TOO_SMALL;
- }
- *DataLength=nRequiredLength;
- *(PWCHAR)Buffer = UNICODE_NULL;
- NdisReleaseSpinLock(&GlobalLock);
- return STATUS_SUCCESS;
- }
- PADAPT LookupAdaptByName(PUCHAR pNameBuffer, ULONG nNameLength)
- {
- PADAPT pAdapt;
- NdisAcquireSpinLock(&GlobalLock);
- pAdapt=pAdaptList;
- while(pAdapt!=NULL)
- {
- if(pAdapt->DeviceName.Length==nNameLength && NdisEqualMemory(pAdapt->DeviceName.Buffer,pNameBuffer,nNameLength))
- break;
- pAdapt=pAdapt->Next;
- }
- if(pAdapt!=NULL)
- PtRefAdapter(pAdapt);
- NdisReleaseSpinLock(&GlobalLock);
- return pAdapt;
- }
- // 增加对适配器(PADAPT结构)的引用
- VOID PtRefAdapter(PADAPT pAdapt)
- {
- NdisInterlockedIncrement(&pAdapt->RefCount);
- }
- // 减小对适配器(PADAPT结构)的引用,如果减为0,则释放它占用的内存
- VOID PtDerefAdapter(PADAPT pAdapt)
- {
- if(pAdapt == NULL)
- return;
- if(NdisInterlockedDecrement(&pAdapt->RefCount) == 0) // 已经没有代码再引用它了,释放内存
- {
- MPFreeAllPacketPools (pAdapt);
- NdisFreeMemory(pAdapt,sizeof(ADAPT), 0);
- }
- }
- POPEN_CONTEXT DevAllocateOpenContext(PADAPT pAdapt)
- {
- POPEN_CONTEXT pOpenContext = NULL;
- // 为OPEN_CONTEXT结构申请内存空间
- NdisAllocateMemoryWithTag(&pOpenContext, sizeof(OPEN_CONTEXT), TAG);
- if(pOpenContext == NULL)
- {
- return NULL;
- }
- // 初始化这个内存空间
- NdisZeroMemory(pOpenContext, sizeof(OPEN_CONTEXT));
- NdisAllocateSpinLock(&pOpenContext->Lock);
- NdisInitializeEvent(&pOpenContext->RequestEvent);
- pOpenContext->RefCount = 1;
- pOpenContext->pAdapt = pAdapt;
- return pOpenContext;
- }
- VOID DevRefOpenContext(POPEN_CONTEXT pOpenContext)
- {
- // 首先增加对适配器的引用,然后再增加OPEN_CONTEXT的引用计数
- PtRefAdapter(pOpenContext->pAdapt);
- NdisInterlockedIncrement(&pOpenContext->RefCount);
- }
- // 减少对打开环境的引用,如果减为0,则释放它占用的内存
- VOID DevDerefOpenContext(POPEN_CONTEXT pOpenContext)
- {
- PADAPT pAdapt = NULL;
- if(pOpenContext == NULL)
- return;
- // 首先保存对应的适配器指针,以便后面对它调用PtDerefAdapter函数
- pAdapt = pOpenContext->pAdapt;
- // 减小引用计数,如果没有代码再引用它了,则清除资源
- if(NdisInterlockedDecrement(&pOpenContext->RefCount) == 0)
- {
- NdisFreeSpinLock(&pOpenContext->Lock);
- NdisFreeMemory(pOpenContext,sizeof(pOpenContext), 0);
- }
- // 减少对适配器的引用
- PtDerefAdapter(pAdapt);
- }
- VOID DevRequestComplete(PADAPT pAdapt, PNDIS_REQUEST NdisRequest,NDIS_STATUS Status)
- {
- POPEN_CONTEXT pOpenContext = CONTAINING_RECORD(NdisRequest, OPEN_CONTEXT, Request);
- pOpenContext->RequestStatus = Status;
- NdisSetEvent(&pOpenContext->RequestEvent);
- }