基于ndis的防火墙源码供大家赏析firewall c

本文档详细介绍了基于ndis的防火墙NetWall的驱动程序入口点`DriverEntry`,以及数据接收和返回处理函数`PtReceive`、`PtReceiveComplete`和`MPReturnPacket`。代码展示了如何注册Miniport和Protocol驱动,以及如何处理接收和释放网络包。

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.youkuaiyun.com/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

 #include "precomp.h"
#pragma hdrstop

#pragma NDIS_INIT_FUNCTION(DriverEntry)

NDIS_PHYSICAL_ADDRESS         HighestAcceptableMax = NDIS_PHYSICAL_ADDRESS_CONST(-1, -1);

NDIS_MEDIUM                           MediumArray[3] =
                                         {
                                        NdisMedium802_3,      // Ethernet
                                            NdisMedium802_5,      // Token-ring
                                        NdisMediumFddi            // Fddi
                                         };

//
// g_GlobalLock - Protects resources
//
NDIS_SPIN_LOCK         g_GlobalLock;

//
// g_ProtocolHandle - handle returned by NDIS when registering the protocol portion of the IM
//
NDIS_HANDLE                           g_ProtocolHandle = NULL;

//
// g_MiniportHandle - handle returned by NDIS when MP portion registers as LM
//
NDIS_HANDLE                           g_MiniportHandle   = NULL;

//
// g_AdapterList - List of adapters to which the NetWall is bound
//
PADAPT               g_AdapterList     = NULL;

//
// g_DriverObject - pointer to NT driver and device objects
//
PDRIVER_OBJECT         g_DriverObject     = NULL;


/**
* Routine Description:
*
*   This is the primary initialization routine for the NetWall IM driver.
*   It is simply responsible for the intializing the wrapper and registering
*   the Miniport and Protocol driver.
*
* Arguments:
*
*       IN DriverObject - Pointer to driver object created by the system.
*   IN RegistryPath - Registry path string for driver service key
*
* Return Value:
*
*   The status of the operation.
*/
NTSTATUS
DriverEntry(
     IN      PDRIVER_OBJECT            DriverObject,
     IN      PUNICODE_STRING            RegistryPath
     )
{
     NDIS_STATUS                                    Status;
     NDIS_PROTOCOL_CHARACTERISTICS      PChars;
     NDIS_MINIPORT_CHARACTERISTICS      MChars;
     NDIS_STRING               protoName;      
  UNICODE_STRING             ntDeviceName;
  UNICODE_STRING             win32DeviceName;
     NDIS_HANDLE                                    WrapperHandle;
  UINT                   FuncIndex   = 0;

  DBGPRINT("===> NetWall - DriverEntry/n");
 
  DbgPrint("=== RegistryPath is : %ws ===/n", RegistryPath->Buffer);

  g_DriverObject = DriverObject;

  //
  // Initialize Global Lock
  //
  NdisAllocateSpinLock(&g_GlobalLock);  

  do
  {
        //
    // 1. Register the miniport with NDIS. Note that it is the miniport
        //   which was started as a driver and not the protocol. Also the miniport
        //   must be registered prior to the protocol since the protocol's BindAdapter
        //   handler can be initiated anytime and when it is, it must be ready to
        //   start driver instances.
        //
        NdisMInitializeWrapper(&WrapperHandle, DriverObject, RegistryPath, NULL);

    //
    // 2. Perform IM Driver's Miniport Initialization
    //
        NdisZeroMemory(&MChars, sizeof(NDIS_MINIPORT_CHARACTERISTICS));

        MChars.MajorNdisVersion       = 4;
        MChars.MinorNdisVersion       = 0;

        MChars.InitializeHandler     = MPInitialize;
        MChars.QueryInformationHandler = MPQueryInformation;
        MChars.SetInformationHandler   = MPSetInformation;
        MChars.ResetHandler         = MPReset;
        MChars.TransferDataHandler     = MPTransferData;
        MChars.HaltHandler         = MPHalt;

        //
        // We will disable the check for hang timeout so we do not
        // need a check for hang handler!
        //
        MChars.CheckForHangHandler     = NULL;
        MChars.SendHandler         = MPSend;
        MChars.ReturnPacketHandler     = MPReturnPacket;

        //
        // Either the Send or the SendPackets handler should be specified.
        // If SendPackets handler is specified, SendHandler is ignored
        //
        MChars.SendPacketsHandler     = NULL;//MPSendPackets;

        Status = NdisIMRegisterLayeredMiniport(WrapperHandle,
                                                                &MChars,
                                                                sizeof(MChars),
                                                                &g_MiniportHandle);
        if (! NT_SUCCESS(Status))
    {
        DbgPrint("MPRegisterAsMiniport Failed! Status: 0x%x/n", Status);

        NdisWriteErrorLogEntry(
                    DriverObject,
                    (ULONG)IM_ERROR_IM_REGISTER_FAILED,
                    0,
                    1,
                    (ULONG )Status);
        break;
    }

    //
    // 3. Perform initialization supported by WDM
    //
    // Create a control device object for this driver.
    // Application can send an IOCTL to this device to get
    // bound adapter information.
    //
    //

    DBGPRINT("===> NetWall - NdisWDMInitialize/n");

        NdisAcquireSpinLock(&g_GlobalLock); // sync

        ++g_MiniportCount;
       
        if (1 == g_MiniportCount)
    {
              ASSERT(g_DeviceState != PS_DEVICE_STATE_CREATING); //
   
              while (g_DeviceState != PS_DEVICE_STATE_READY)
              {
                    NdisReleaseSpinLock(&g_GlobalLock);
                    NdisMSleep(1); // Waiting
                    NdisAcquireSpinLock(&g_GlobalLock);
              }
              g_DeviceState = PS_DEVICE_STATE_CREATING;

              // Now Can Create Dev

Xfilter 源代码完整的文件列表 ------------------------------------------------------ .\Common .\Lib .\Property .\Release .\TcpIpDog .\Xfilter.dsw .\readme.txt .\filelist.txt .\Common\XLogFile.h .\Common\XLogFile.cpp .\Common\XInstall.cpp .\Common\XFile.h .\Common\XInstall.h .\Common\XFile.cpp .\Common\Debug.h .\Common\XFileRes.h .\Lib\htmlhelp.h .\Lib\htmlhelp.lib .\Property\Property.opt .\Property\Property.aps .\Property\Property.dsp .\Property\Property.rc .\Property\Property.clw .\Property\ReadMe.txt .\Property\resource.h .\Property\NetIPAria.h .\Property\GuiRes.h .\Property\AclSet.cpp .\Property\SetTime.h .\Property\Acl.cpp .\Property\SetNet.h .\Property\MainSheet.h .\Property\AclSet.h .\Property\Register.h .\Property\SystemSet.cpp .\Property\Splash.h .\Property\Property.dsw .\Property\Acl.h .\Property\Property.ncb .\Property\Splash.cpp .\Property\Property.h .\Property\LogQuery.h .\Property\SetTime.cpp .\Property\MainSheet.cpp .\Property\SetNet.cpp .\Property\NetIPAria.cpp .\Property\About.cpp .\Property\StdAfx.h .\Property\StdAfx.cpp .\Property\SystemSet.h .\Property\Register.cpp .\Property\About.h .\Property\LogQuery.cpp .\Property\Property.cpp .\Property\NetTimeSheet.h .\Property\PacketMonitor.h .\Property\Property.plg .\Property\NetTimeSheet.cpp .\Property\PacketMonitor.cpp .\Property\Internet .\Property\MainFrame .\Property\SystemTray .\Property\res .\Property\HyperLink .\Property\Internet\Internet.cpp .\Property\Internet\Internet.h .\Property\MainFrame\MainFrame.cpp .\Property\MainFrame\mainframe.h .\Property\SystemTray\SystemTray.cpp .\Property\SystemTray\SystemTray.h .\Property\res\Property.rc2 .\Property\res\NULL.ico .\Property\res\Property.ico .\Property\res\about.bmp .\Property\res\Alert.ico .\Property\res\DenyEx1.ico .\Property\res\PassEx1.ico .\Property\res\QueryEx1.ico .\Property\res\splash.bmp .\Property\res\MEMO.ICO .\Property\res\ALERTSET.ICO .\Property\res\APPSET.ICO .\Property\res\BASESET.ICO .\Property\res\COMMONSET.ICO .\Property\res\Monitor.ico .\Property\res\NETSET.ICO .\Property\res\SUPERSET.ICO .\Property\res\TIMESET.ICO .\Property\res\Xfilter.ico .\Property\res\IPSET.ICO .\Property\res\Email.ico .\Property\res\QueryResult.ico .\Property\res\QuerySet.ICO .\Property\res\UserInfo.ico .\Property\res\ACLSET.ICO .\Property\res\Message.ico .\Property\HyperLink\HyperLink.cpp .\Property\HyperLink\HyperLink.h .\Release\xacl.cfg .\Release\Xfilter.chm .\Release\Xfilter.exe .\Release\Xfilter.dll .\Release\xlog.dat .\TcpIpDog\StdAfx.cpp .\TcpIpDog\TcpIpDog.dsp .\TcpIpDog\LspServ.def .\TcpIpDog\ReadMe.txt .\TcpIpDog\CheckAcl.cpp .\TcpIpDog\TcpIpdog.cpp .\TcpIpDog\Codes.h .\TcpIpDog\TcpIpDog.h .\TcpIpDog\ProtocolInfo.h .\TcpIpDog\CheckAcl.h .\TcpIpDog\StdAfx.h .\TcpIpDog\ProtocolInfo.cpp .\TcpIpDog\TcpIpDog.plg
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值