passthru.c
上传用户:jencksen
上传日期:2016-04-08
资源大小:66k
文件大小:11k
源码类别:

ICQ弱点检测代码

开发平台:

Visual C++

  1. /*++
  2. Copyright (c) 1992-2000  Microsoft Corporation
  3.  
  4. Module Name:
  5.  
  6.     passthru.c
  7. Abstract:
  8.     Ndis Intermediate Miniport driver sample. This is a passthru driver.
  9. Author:
  10. Environment:
  11. Revision History:
  12. --*/
  13. #include "precomp.h"
  14. #pragma hdrstop
  15. #pragma NDIS_INIT_FUNCTION(DriverEntry)
  16. NDIS_HANDLE         ProtHandle = NULL;
  17. NDIS_HANDLE         DriverHandle = NULL;
  18. NDIS_MEDIUM         MediumArray[4] =
  19.                     {
  20.                         NdisMedium802_3,    // Ethernet
  21.                         NdisMedium802_5,    // Token-ring
  22.                         NdisMediumFddi,     // Fddi
  23.                         NdisMediumWan       // NDISWAN
  24.                     };
  25. NDIS_SPIN_LOCK     GlobalLock;
  26. PADAPT             pAdaptList = NULL;
  27. LONG               MiniportCount = 0;
  28. NDIS_HANDLE        NdisWrapperHandle;
  29. //
  30. // To support ioctls from user-mode:
  31. //
  32. #define LINKNAME_STRING     L"\DosDevices\Passthru"
  33. #define NTDEVICE_STRING     L"\Device\Passthru"
  34. NDIS_HANDLE     NdisDeviceHandle = NULL;
  35. PDEVICE_OBJECT  ControlDeviceObject = NULL;
  36. enum _DEVICE_STATE
  37. {
  38.     PS_DEVICE_STATE_READY = 0,    // ready for create/delete
  39.     PS_DEVICE_STATE_CREATING,    // create operation in progress
  40.     PS_DEVICE_STATE_DELETING    // delete operation in progress
  41. } ControlDeviceState = PS_DEVICE_STATE_READY;
  42. NTSTATUS
  43. DriverEntry(
  44.     IN PDRIVER_OBJECT        DriverObject,
  45.     IN PUNICODE_STRING       RegistryPath
  46.     )
  47. /*++
  48. Routine Description:
  49.     First entry point to be called, when this driver is loaded.
  50.     Register with NDIS as an intermediate driver.
  51. Arguments:
  52.     DriverObject - pointer to the system's driver object structure
  53.         for this driver
  54.     
  55.     RegistryPath - system's registry path for this driver
  56.     
  57. Return Value:
  58.     STATUS_SUCCESS if all initialization is successful, STATUS_XXX
  59.     error code if not.
  60. --*/
  61. {
  62.     NDIS_STATUS                        Status;
  63.     NDIS_PROTOCOL_CHARACTERISTICS      PChars;
  64.     NDIS_MINIPORT_CHARACTERISTICS      MChars;
  65.     NDIS_STRING                        Name;
  66.     Status = NDIS_STATUS_SUCCESS;
  67.     NdisAllocateSpinLock(&GlobalLock);
  68.     NdisMInitializeWrapper(&NdisWrapperHandle, DriverObject, RegistryPath, NULL);
  69.     do
  70.     {
  71.         //
  72.         // Register the miniport with NDIS. Note that it is the miniport
  73.         // which was started as a driver and not the protocol. Also the miniport
  74.         // must be registered prior to the protocol since the protocol's BindAdapter
  75.         // handler can be initiated anytime and when it is, it must be ready to
  76.         // start driver instances.
  77.         //
  78.         NdisZeroMemory(&MChars, sizeof(NDIS_MINIPORT_CHARACTERISTICS));
  79.         MChars.MajorNdisVersion = PASSTHRU_MAJOR_NDIS_VERSION;
  80.         MChars.MinorNdisVersion = PASSTHRU_MINOR_NDIS_VERSION;
  81.         MChars.InitializeHandler = MPInitialize;
  82.         MChars.QueryInformationHandler = MPQueryInformation;
  83.         MChars.SetInformationHandler = MPSetInformation;
  84.         MChars.ResetHandler = NULL;
  85.         MChars.TransferDataHandler = MPTransferData;
  86.         MChars.HaltHandler = MPHalt;
  87. #ifdef NDIS51_MINIPORT
  88.         MChars.CancelSendPacketsHandler = MPCancelSendPackets;
  89.         MChars.PnPEventNotifyHandler = MPDevicePnPEvent;
  90.         MChars.AdapterShutdownHandler = MPAdapterShutdown;
  91. #endif // NDIS51_MINIPORT
  92.         //
  93.         // We will disable the check for hang timeout so we do not
  94.         // need a check for hang handler!
  95.         //
  96.         MChars.CheckForHangHandler = NULL;
  97.         MChars.ReturnPacketHandler = MPReturnPacket;
  98.         //
  99.         // Either the Send or the SendPackets handler should be specified.
  100.         // If SendPackets handler is specified, SendHandler is ignored
  101.         //
  102.         MChars.SendHandler = NULL;    // MPSend;
  103.         MChars.SendPacketsHandler = MPSendPackets;
  104.         Status = NdisIMRegisterLayeredMiniport(NdisWrapperHandle,
  105.                                                   &MChars,
  106.                                                   sizeof(MChars),
  107.                                                   &DriverHandle);
  108.         if (Status != NDIS_STATUS_SUCCESS)
  109.         {
  110.             break;
  111.         }
  112. #ifndef WIN9X
  113.         NdisMRegisterUnloadHandler(NdisWrapperHandle, PtUnload);
  114. #endif
  115.         //
  116.         // Now register the protocol.
  117.         //
  118.         NdisZeroMemory(&PChars, sizeof(NDIS_PROTOCOL_CHARACTERISTICS));
  119.         PChars.MajorNdisVersion = PASSTHRU_PROT_MAJOR_NDIS_VERSION;
  120.         PChars.MinorNdisVersion = PASSTHRU_PROT_MINOR_NDIS_VERSION;
  121.         //
  122.         // Make sure the protocol-name matches the service-name
  123.         // (from the INF) under which this protocol is installed.
  124.         // This is needed to ensure that NDIS can correctly determine
  125.         // the binding and call us to bind to miniports below.
  126.         //
  127.         NdisInitUnicodeString(&Name, L"Passthru");    // Protocol name
  128.         PChars.Name = Name;
  129.         PChars.OpenAdapterCompleteHandler = PtOpenAdapterComplete;
  130.         PChars.CloseAdapterCompleteHandler = PtCloseAdapterComplete;
  131.         PChars.SendCompleteHandler = PtSendComplete;
  132.         PChars.TransferDataCompleteHandler = PtTransferDataComplete;
  133.     
  134.         PChars.ResetCompleteHandler = PtResetComplete;
  135.         PChars.RequestCompleteHandler = PtRequestComplete;
  136.         PChars.ReceiveHandler = PtReceive;
  137.         PChars.ReceiveCompleteHandler = PtReceiveComplete;
  138.         PChars.StatusHandler = PtStatus;
  139.         PChars.StatusCompleteHandler = PtStatusComplete;
  140.         PChars.BindAdapterHandler = PtBindAdapter;
  141.         PChars.UnbindAdapterHandler = PtUnbindAdapter;
  142.         PChars.UnloadHandler = PtUnloadProtocol;
  143.         PChars.ReceivePacketHandler = PtReceivePacket;
  144.         PChars.PnPEventHandler= PtPNPHandler;
  145.         NdisRegisterProtocol(&Status,
  146.                              &ProtHandle,
  147.                              &PChars,
  148.                              sizeof(NDIS_PROTOCOL_CHARACTERISTICS));
  149.         if (Status != NDIS_STATUS_SUCCESS)
  150.         {
  151.             NdisIMDeregisterLayeredMiniport(DriverHandle);
  152.             break;
  153.         }
  154.         NdisIMAssociateMiniport(DriverHandle, ProtHandle);
  155.     }
  156.     while (FALSE);
  157.     if (Status != NDIS_STATUS_SUCCESS)
  158.     {
  159.         NdisTerminateWrapper(NdisWrapperHandle, NULL);
  160.     }
  161.     return(Status);
  162. }
  163. NDIS_STATUS
  164. PtRegisterDevice(
  165.     VOID
  166.     )
  167. /*++
  168. Routine Description:
  169.     Register an ioctl interface - a device object to be used for this
  170.     purpose is created by NDIS when we call NdisMRegisterDevice.
  171.     This routine is called whenever a new miniport instance is
  172.     initialized. However, we only create one global device object,
  173.     when the first miniport instance is initialized. This routine
  174.     handles potential race conditions with PtDeregisterDevice via
  175.     the ControlDeviceState and MiniportCount variables.
  176.     NOTE: do not call this from DriverEntry; it will prevent the driver
  177.     from being unloaded (e.g. on uninstall).
  178. Arguments:
  179.     None
  180. Return Value:
  181.     NDIS_STATUS_SUCCESS if we successfully register a device object.
  182. --*/
  183. {
  184.     NDIS_STATUS            Status = NDIS_STATUS_SUCCESS;
  185.     UNICODE_STRING         DeviceName;
  186.     UNICODE_STRING         DeviceLinkUnicodeString;
  187.     PDRIVER_DISPATCH       DispatchTable[IRP_MJ_MAXIMUM_FUNCTION+1];
  188.     DBGPRINT(("==>PtRegisterDevice  Register our ioctl interface n"));
  189.     NdisAcquireSpinLock(&GlobalLock);
  190.     ++MiniportCount;
  191.     
  192.     if (1 == MiniportCount)
  193.     {
  194.         ASSERT(ControlDeviceState != PS_DEVICE_STATE_CREATING);
  195.         //
  196.         // Another thread could be running PtDeregisterDevice on
  197.         // behalf of another miniport instance. If so, wait for
  198.         // it to exit.
  199.         //
  200.         while (ControlDeviceState != PS_DEVICE_STATE_READY)
  201.         {
  202.             NdisReleaseSpinLock(&GlobalLock);
  203.             NdisMSleep(1);
  204.             NdisAcquireSpinLock(&GlobalLock);
  205.         }
  206.         ControlDeviceState = PS_DEVICE_STATE_CREATING;
  207.         NdisReleaseSpinLock(&GlobalLock);
  208.     
  209.         NdisZeroMemory(DispatchTable, (IRP_MJ_MAXIMUM_FUNCTION+1) * sizeof(PDRIVER_DISPATCH));
  210. // BEGIN_PTUSERIO 我们自己的派遣例程
  211.         DispatchTable[IRP_MJ_CREATE] = DevOpen;
  212. DispatchTable[IRP_MJ_CLEANUP] = DevCleanup;
  213.         DispatchTable[IRP_MJ_CLOSE] = DevClose;
  214.         DispatchTable[IRP_MJ_DEVICE_CONTROL] = DevIoControl;
  215. // END_PTUSERIO
  216.         
  217.         NdisInitUnicodeString(&DeviceName, NTDEVICE_STRING);
  218.         NdisInitUnicodeString(&DeviceLinkUnicodeString, LINKNAME_STRING);
  219.         //
  220.         // Create a device object and register our dispatch handlers
  221.         //
  222.         
  223.         Status = NdisMRegisterDevice(
  224.                     NdisWrapperHandle, 
  225.                     &DeviceName,
  226.                     &DeviceLinkUnicodeString,
  227.                     &DispatchTable[0],
  228.                     &ControlDeviceObject,
  229.                     &NdisDeviceHandle
  230.                     );
  231.         NdisAcquireSpinLock(&GlobalLock);
  232.         ControlDeviceState = PS_DEVICE_STATE_READY;
  233.     }
  234.     NdisReleaseSpinLock(&GlobalLock);
  235.     DBGPRINT(("<==PtRegisterDevice: %xn", Status));
  236.     return (Status);
  237. }
  238. // BEGIN_PTUSERIO
  239.       // 在这里删除了PtDispatch的实现
  240. // END_PTUSERIO
  241. NDIS_STATUS
  242. PtDeregisterDevice(
  243.     VOID
  244.     )
  245. /*++
  246. Routine Description:
  247.     Deregister the ioctl interface. This is called whenever a miniport
  248.     instance is halted. When the last miniport instance is halted, we
  249.     request NDIS to delete the device object
  250. Arguments:
  251.     NdisDeviceHandle - Handle returned by NdisMRegisterDevice
  252. Return Value:
  253.     NDIS_STATUS_SUCCESS if everything worked ok
  254. --*/
  255. {
  256.     NDIS_STATUS Status = NDIS_STATUS_SUCCESS;
  257.     DBGPRINT(("==>PassthruDeregisterDevicen"));
  258.     NdisAcquireSpinLock(&GlobalLock);
  259.     ASSERT(MiniportCount > 0);
  260.     --MiniportCount;
  261.     
  262.     if (0 == MiniportCount)
  263.     {
  264.         //
  265.         // All miniport instances have been halted. Deregister
  266.         // the control device.
  267.         //
  268.         ASSERT(ControlDeviceState == PS_DEVICE_STATE_READY);
  269.         //
  270.         // Block PtRegisterDevice() while we release the control
  271.         // device lock and deregister the device.
  272.         // 
  273.         ControlDeviceState = PS_DEVICE_STATE_DELETING;
  274.         NdisReleaseSpinLock(&GlobalLock);
  275.         if (NdisDeviceHandle != NULL)
  276.         {
  277.             Status = NdisMDeregisterDevice(NdisDeviceHandle);
  278.             NdisDeviceHandle = NULL;
  279.         }
  280.         NdisAcquireSpinLock(&GlobalLock);
  281.         ControlDeviceState = PS_DEVICE_STATE_READY;
  282.     }
  283.     NdisReleaseSpinLock(&GlobalLock);
  284.     DBGPRINT(("<== PassthruDeregisterDevice: %xn", Status));
  285.     return Status;
  286.     
  287. }
  288. VOID
  289. PtUnload(
  290.     IN PDRIVER_OBJECT        DriverObject
  291.     )
  292. //
  293. // PassThru driver unload function
  294. //
  295. {
  296.     UNREFERENCED_PARAMETER(DriverObject);
  297.     
  298.     DBGPRINT(("PtUnload: enteredn"));
  299.     PtUnloadProtocol();
  300.     NdisIMDeregisterLayeredMiniport(DriverHandle);
  301.     NdisFreeSpinLock(&GlobalLock);
  302.     DBGPRINT(("PtUnload: done!n"));
  303. }