filter.c
上传用户:sabrinaco
上传日期:2016-01-19
资源大小:3177k
文件大小:5k
开发平台:

Visual C++

  1. #include "precomp.h"
  2. #pragma hdrstop
  3. NDIS_STATUS Filter(IN  PVOID  HeaderBuffer,IN  UINT PacketSize, IN UINT HeaderBufferSize,IN PVOID LookAheadBuffer)  //过滤规则设置
  4. {
  5.     ULONG i,j,k=0,t1=0,t2=0;
  6.     for(j=0;j<usernum;j++)
  7.     {
  8.         t1=0;
  9.         t2=0;
  10.         for(i=0;i<6;i++)
  11.         {
  12.             if(mac[j][i]==((unsigned char*)HeaderBuffer)[i])
  13.             {
  14.                 t1++;
  15.             }
  16.             if(mac[j][i]==((unsigned char*)HeaderBuffer)[i+6])
  17.             {
  18.                 t2++;
  19.             }
  20.         }
  21.         if(t1==6||t2==6)  //源、目的mac地址其中一个满足条件就接收包
  22.         {
  23.             if(((unsigned char*)HeaderBuffer)[12]==8&&((unsigned char*)HeaderBuffer)[13]==0)
  24.             {
  25.                for(k=0;k<4;k++)
  26.                {
  27.                     if(t1==6)
  28.                         ip[j][k]=((unsigned char*)LookAheadBuffer)[k+16];
  29.                     else
  30.                         ip[j][k]=((unsigned char*)LookAheadBuffer)[k+12];
  31.                }
  32.              }
  33.             if(num[j]==0) return NDIS_STATUS_RESOURCES;
  34.             else packetnum[j]=packetnum[j]+(PacketSize+HeaderBufferSize)*8;
  35.                 
  36.             return NDIS_STATUS_SUCCESS;
  37.          }
  38.     }
  39.     return NDIS_STATUS_RESOURCES;
  40. }
  41. NDIS_STATUS GetBindingList(IN PVOID Buffer,IN ULONG Length, IN OUT PULONG DataLength)//获取适配器列表
  42. {
  43.     PADAPT pAdapt;
  44.     ULONG nRequiredLength=0;
  45.     ULONG nAdapters=0;
  46.     NdisAcquireSpinLock(&GlobalLock);
  47.     pAdapt = pAdaptList;
  48.     while(pAdapt != NULL)
  49.     {
  50.         nAdapters++;
  51.         pAdapt = pAdapt->Next;
  52.     }
  53.     *(PULONG)Buffer = nAdapters;
  54.     Buffer = (PCHAR)Buffer + sizeof(ULONG);
  55.     nRequiredLength+=sizeof(ULONG);
  56.     pAdapt=pAdaptList;
  57.     while(pAdapt!=NULL)
  58.     {
  59.         NdisMoveMemory(Buffer,pAdapt->DeviceName.Buffer,pAdapt->DeviceName.Length+sizeof(WCHAR));
  60.         Buffer=(PCHAR)Buffer+pAdapt->DeviceName.Length+sizeof(WCHAR);
  61.         nRequiredLength+=pAdapt->DeviceName.Length+sizeof(WCHAR);
  62.         pAdapt=pAdapt->Next;
  63.     }
  64.     if(nRequiredLength > Length) 
  65.     {
  66.        NdisReleaseSpinLock(&GlobalLock);
  67.        return STATUS_BUFFER_TOO_SMALL;
  68.     }
  69.     *DataLength=nRequiredLength;
  70.     *(PWCHAR)Buffer = UNICODE_NULL;
  71.     NdisReleaseSpinLock(&GlobalLock);
  72.     return STATUS_SUCCESS;
  73. }
  74. PADAPT LookupAdaptByName(PUCHAR pNameBuffer, ULONG nNameLength)
  75. {
  76.     PADAPT pAdapt;
  77.     NdisAcquireSpinLock(&GlobalLock);
  78.     pAdapt=pAdaptList;
  79.     while(pAdapt!=NULL)
  80.     {
  81.         if(pAdapt->DeviceName.Length==nNameLength && NdisEqualMemory(pAdapt->DeviceName.Buffer,pNameBuffer,nNameLength))
  82.             break;
  83.         pAdapt=pAdapt->Next;
  84.     }
  85.     if(pAdapt!=NULL)
  86.         PtRefAdapter(pAdapt);
  87.     NdisReleaseSpinLock(&GlobalLock);
  88.     return pAdapt;
  89. }
  90. // 增加对适配器(PADAPT结构)的引用
  91. VOID PtRefAdapter(PADAPT pAdapt)    
  92. {
  93.    NdisInterlockedIncrement(&pAdapt->RefCount);
  94. }
  95. // 减小对适配器(PADAPT结构)的引用,如果减为0,则释放它占用的内存
  96. VOID PtDerefAdapter(PADAPT pAdapt) 
  97. {
  98.     if(pAdapt == NULL)
  99.         return;
  100.  
  101.     if(NdisInterlockedDecrement(&pAdapt->RefCount) == 0) // 已经没有代码再引用它了,释放内存
  102.     {
  103.         MPFreeAllPacketPools (pAdapt);
  104.         NdisFreeMemory(pAdapt,sizeof(ADAPT), 0);
  105.     }
  106. }
  107. POPEN_CONTEXT DevAllocateOpenContext(PADAPT pAdapt)
  108. {
  109.     POPEN_CONTEXT pOpenContext = NULL;
  110.     // 为OPEN_CONTEXT结构申请内存空间
  111.     NdisAllocateMemoryWithTag(&pOpenContext, sizeof(OPEN_CONTEXT), TAG);
  112.     if(pOpenContext == NULL)
  113.     {
  114.         return NULL;
  115.     }
  116.     // 初始化这个内存空间
  117.     NdisZeroMemory(pOpenContext, sizeof(OPEN_CONTEXT));
  118.     NdisAllocateSpinLock(&pOpenContext->Lock);
  119.     NdisInitializeEvent(&pOpenContext->RequestEvent);
  120.     pOpenContext->RefCount = 1;
  121.     pOpenContext->pAdapt = pAdapt;
  122.     return pOpenContext;
  123. }
  124. VOID DevRefOpenContext(POPEN_CONTEXT pOpenContext) 
  125. {
  126.     // 首先增加对适配器的引用,然后再增加OPEN_CONTEXT的引用计数
  127.    PtRefAdapter(pOpenContext->pAdapt);
  128.    NdisInterlockedIncrement(&pOpenContext->RefCount);
  129. }
  130. // 减少对打开环境的引用,如果减为0,则释放它占用的内存
  131. VOID DevDerefOpenContext(POPEN_CONTEXT pOpenContext) 
  132. {
  133.    PADAPT pAdapt = NULL;
  134.    if(pOpenContext == NULL)
  135.        return;
  136.    // 首先保存对应的适配器指针,以便后面对它调用PtDerefAdapter函数
  137.    pAdapt = pOpenContext->pAdapt;
  138.    // 减小引用计数,如果没有代码再引用它了,则清除资源
  139.    if(NdisInterlockedDecrement(&pOpenContext->RefCount) == 0)
  140.    {
  141.       NdisFreeSpinLock(&pOpenContext->Lock);
  142.       NdisFreeMemory(pOpenContext,sizeof(pOpenContext), 0);
  143.    }
  144.    // 减少对适配器的引用
  145.    PtDerefAdapter(pAdapt);
  146. }
  147. VOID DevRequestComplete(PADAPT pAdapt, PNDIS_REQUEST NdisRequest,NDIS_STATUS Status)
  148. {
  149.     POPEN_CONTEXT pOpenContext = CONTAINING_RECORD(NdisRequest, OPEN_CONTEXT, Request);
  150.     pOpenContext->RequestStatus = Status;
  151.     NdisSetEvent(&pOpenContext->RequestEvent);
  152. }