⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pcatdih.c

📁 网络驱动开发
💻 C
📖 第 1 页 / 共 3 页
字号:
   {
      KdPrint(( "TDIH_DefaultCompletion: Invalid Device Object Pointer\n" ));
      return(STATUS_SUCCESS);
   }

   // Note that you could do all sorts of processing at this point
   // depending upon the results of the operation. Be careful though
   // about what you chose to do, about the fact that this completion
   // routine is being invoked in an arbitrary thread context and probably
   // at high IRQL.

   UTIL_DecrementLargeInteger(
      pTDIH_DeviceExtension->OutstandingIoRequests,
      (unsigned long)1,
      &(pTDIH_DeviceExtension->IoRequestsSpinLock)
      );

   // If the outstanding count is 0, signal the appropriate event which will
   // allow any pending detach to proceed.
   UTIL_IsLargeIntegerZero(
      CanDetachProceed,
      pTDIH_DeviceExtension->OutstandingIoRequests,
      &(pTDIH_DeviceExtension->IoRequestsSpinLock)
      );

   if (CanDetachProceed)
   {
      // signal the event object. Note that this is simply an
      // advisory check we do here (to wake up a sleeping thread).
      // It is the responsibility of the thread performing the detach to
      // ensure that no operations are truly in progress.
      KeSetEvent(&(pTDIH_DeviceExtension->IoInProgressEvent), IO_NO_INCREMENT, FALSE);
   }

   // Although the success return value is hard-coded here, you can
   // return an appropriate value (either success or more-processing-reqd)
   // based upon what it is that you wish to do in your completion routine.
   return(STATUS_SUCCESS);
}


/////////////////////////////////////////////////////////////////////////////
//// TDIH_Create
//
// Purpose
//
// Parameters
//
// Return Value
// 
// Remarks
//

NTSTATUS
TDIH_Create(
   PTDIH_DeviceExtension   pTDIH_DeviceExtension,
   PIRP                    Irp,
   PIO_STACK_LOCATION      IrpSp
   )
{
   NTSTATUS                            RC;
   FILE_FULL_EA_INFORMATION            *ea;
   FILE_FULL_EA_INFORMATION UNALIGNED  *targetEA;

   ea = (PFILE_FULL_EA_INFORMATION) Irp->AssociatedIrp.SystemBuffer;

   //
   // Handle Control Channel Open
   //
   if (!ea)
   {
      KdPrint(( "TDIH_TdiOpenControlChannel: Entry...\n" ));
   }

   //
   // Handle Address Object Open
   //
   targetEA = FindEA(ea, TdiTransportAddress, TDI_TRANSPORT_ADDRESS_LENGTH);

   if (targetEA != NULL)
   {
      return( TDIH_TdiOpenAddress( pTDIH_DeviceExtension, Irp, IrpSp ) );
   }

   //
   // Handle Connection Object Open
   //
   targetEA = FindEA(ea, TdiConnectionContext, TDI_CONNECTION_CONTEXT_LENGTH);

   if (targetEA != NULL)
   {
      return( TDIH_TdiOpenConnection(
               pTDIH_DeviceExtension,
               Irp,
               IrpSp,
               *((CONNECTION_CONTEXT UNALIGNED *)
                  &(targetEA->EaName[targetEA->EaNameLength + 1]))
               )
            );
   }

//   KdPrint(( "TDIH_Create: Creating UNKNOWN TDI Object\n" ));

   try
   {
      PDEVICE_OBJECT       pLowerDeviceObject = NULL;

      pLowerDeviceObject = pTDIH_DeviceExtension->LowerDeviceObject;

      IoCopyCurrentIrpStackLocationToNext( Irp );

      IoSetCompletionRoutine(
         Irp,
         TDIH_DefaultCompletion,
         pTDIH_DeviceExtension,
         TRUE,
         TRUE,
         TRUE
         );

      UTIL_IncrementLargeInteger(
         pTDIH_DeviceExtension->OutstandingIoRequests,
         (unsigned long)1,
         &(pTDIH_DeviceExtension->IoRequestsSpinLock)
         );

      // Clear the fast-IO notification event protected by the resource
      // we have acquired.
      KeClearEvent(&(pTDIH_DeviceExtension->IoInProgressEvent));

      RC = IoCallDriver(pLowerDeviceObject, Irp);

      try_return(RC);

      try_exit:   NOTHING;
   }
   finally
   {
   }

   return(RC);
}


/////////////////////////////////////////////////////////////////////////////
//// TDIH_DispatchDeviceControl
//
// Purpose
//
// Parameters
//
// Return Value
// 
// Remarks
//

NTSTATUS
TDIH_DispatchDeviceControl(
   PDEVICE_OBJECT pDeviceObject,
   PIRP                    Irp,
   PIO_STACK_LOCATION      IrpSp
   )
{
   NTSTATUS               RC;
   PTDIH_DeviceExtension   pTDIH_DeviceExtension;

   // Get a pointer to the device extension that must exist for
   // all of the device objects created by the filter driver.
   pTDIH_DeviceExtension = (PTDIH_DeviceExtension )(pDeviceObject->DeviceExtension);

   switch(IrpSp->Parameters.DeviceIoControl.IoControlCode)
   {
      case IOCTL_TCP_QUERY_INFORMATION_EX:
         KdPrint(( "TCPQueryInformationEx: Entry...\n" ));
//      return(TCPQueryInformationEx(Irp, IrpSp));
         break;

      case IOCTL_TCP_SET_INFORMATION_EX:
         KdPrint(( "TCPSetInformationEx: Entry...\n" ));
//      return(TCPSetInformationEx(Irp, IrpSp));
         break;

      default:
         KdPrint(( "TDIH_DispatchDeviceControl: Code: %d\n",
            IrpSp->Parameters.DeviceIoControl.IoControlCode ));
         break;
   }

   try
   {
      PDEVICE_OBJECT       pLowerDeviceObject = NULL;

      pLowerDeviceObject = pTDIH_DeviceExtension->LowerDeviceObject;

      // Be careful about not screwing up badly. This is actually not recommended by the I/O Manager.
      if (Irp->CurrentLocation == 1)
      {
         ULONG ReturnedInformation = 0;

         // Bad!! Fudge the error code. Break if we can ...

         KdPrint(("TDIH_DispatchDeviceControl encountered bogus current location\n"));

         KdPrint(("   TDIH_DispatchD: Node Type: %d, Context2: %d; Major FCN: 0x%2.2X, Minor FCN: 0x%2.2X\n",
                     pTDIH_DeviceExtension->NodeIdentifier.NodeType,
                     (int)IrpSp->FileObject->FsContext2,
                     IrpSp->MajorFunction,
                     IrpSp->MinorFunction
                     ));

   //      UTIL_BreakPoint();
         RC = STATUS_INVALID_DEVICE_REQUEST;
         Irp->IoStatus.Status = RC;
         Irp->IoStatus.Information = ReturnedInformation;
         IoCompleteRequest(Irp, IO_NO_INCREMENT);

         return( RC );
      }

      IoCopyCurrentIrpStackLocationToNext( Irp );

      IoSetCompletionRoutine(
         Irp,
         TDIH_DefaultCompletion,
         pTDIH_DeviceExtension,
         TRUE,
         TRUE,
         TRUE
         );

      UTIL_IncrementLargeInteger(
         pTDIH_DeviceExtension->OutstandingIoRequests,
         (unsigned long)1,
         &(pTDIH_DeviceExtension->IoRequestsSpinLock)
         );

      // Clear the fast-IO notification event protected by the resource
      // we have acquired.
      KeClearEvent(&(pTDIH_DeviceExtension->IoInProgressEvent));

      RC = IoCallDriver(pLowerDeviceObject, Irp);

      try_return(RC);

      try_exit:   NOTHING;
   }
   finally
   {
   }

   return(RC);
}


/////////////////////////////////////////////////////////////////////////////
//// TDIH_Cleanup
//
// Purpose
//
// Parameters
//
// Return Value
// 
// Remarks
//

NTSTATUS
TDIH_Cleanup(
   PTDIH_DeviceExtension   pTDIH_DeviceExtension,
   PIRP                    Irp,
   PIO_STACK_LOCATION      IrpSp
   )
{
   NTSTATUS                            RC;

   switch ((int) IrpSp->FileObject->FsContext2)
   {
      case TDI_TRANSPORT_ADDRESS_FILE:
         return( TDIH_TdiCloseAddress( pTDIH_DeviceExtension, Irp, IrpSp ) );

      case TDI_CONNECTION_FILE:
         return( TDIH_TdiCloseConnection( pTDIH_DeviceExtension, Irp, IrpSp ) );

      case TDI_CONTROL_CHANNEL_FILE:
      default:
         //
         // This Should Never Happen
         //
         break;
   }

   try
   {
      PDEVICE_OBJECT       pLowerDeviceObject = NULL;

      pLowerDeviceObject = pTDIH_DeviceExtension->LowerDeviceObject;

      IoCopyCurrentIrpStackLocationToNext( Irp );

      IoSetCompletionRoutine(
         Irp,
         TDIH_DefaultCompletion,
         pTDIH_DeviceExtension,
         TRUE,
         TRUE,
         TRUE
         );

      UTIL_IncrementLargeInteger(
         pTDIH_DeviceExtension->OutstandingIoRequests,
         (unsigned long)1,
         &(pTDIH_DeviceExtension->IoRequestsSpinLock)
         );

      // Clear the fast-IO notification event protected by the resource
      // we have acquired.
      KeClearEvent(&(pTDIH_DeviceExtension->IoInProgressEvent));

      RC = IoCallDriver(pLowerDeviceObject, Irp);

      try_return(RC);

      try_exit:   NOTHING;
   }
   finally
   {
   }

   return(RC);
}


/////////////////////////////////////////////////////////////////////////////
//// TDIH_Close
//
// Purpose
//
// Parameters
//
// Return Value
// 
// Remarks
//

NTSTATUS
TDIH_Close(
   PTDIH_DeviceExtension   pTDIH_DeviceExtension,
   PIRP                    Irp,
   PIO_STACK_LOCATION      IrpSp
   )
{
   NTSTATUS                            RC;

   switch ((int) IrpSp->FileObject->FsContext2)
   {
      case TDI_TRANSPORT_ADDRESS_FILE:
         KdPrint(( "TDIH_Close: Address File\n" ));
         {
            AddrObj  *pAddrObj;

            pAddrObj = TDIH_GetAddrObjFromFileObject( IrpSp->FileObject );

            if( pAddrObj )
            {
               KdPrint(("   AddrObj Still Open!!!\n"));
            }
         }
         break;

      case TDI_CONNECTION_FILE:
         KdPrint(( "TDIH_Close: Connection File\n" ));
         break;

      case TDI_CONTROL_CHANNEL_FILE:
         KdPrint(( "TDIH_Close: Control Channel File\n" ));
         break;

      default:
         //
         // This Should Never Happen
         //
         break;
   }

   try
   {
      PDEVICE_OBJECT       pLowerDeviceObject = NULL;

      pLowerDeviceObject = pTDIH_DeviceExtension->LowerDeviceObject;

      IoCopyCurrentIrpStackLocationToNext( Irp );

      IoSetCompletionRoutine(
         Irp,
         TDIH_DefaultCompletion,
         pTDIH_DeviceExtension,
         TRUE,
         TRUE,
         TRUE
         );

      UTIL_IncrementLargeInteger(
         pTDIH_DeviceExtension->OutstandingIoRequests,
         (unsigned long)1,
         &(pTDIH_DeviceExtension->IoRequestsSpinLock)
         );

      // Clear the fast-IO notification event protected by the resource
      // we have acquired.
      KeClearEvent(&(pTDIH_DeviceExtension->IoInProgressEvent));

      RC = IoCallDriver(pLowerDeviceObject, Irp);

      try_return(RC);

      try_exit:   NOTHING;
   }
   finally
   {
   }

   return(RC);
}


/////////////////////////////////////////////////////////////////////////////
//// TDIH_DefaultDispatch
//
// Purpose
//
// Parameters
//
// Return Value
// 
// Remarks
// This is the generic dispatch routine for IP/TCP/UDP/RawIP and W32API
// devices.
//
// The PCATDIH driver supports five(5) different devices, each with their
// own interpretaion of how the driver MajorFunction table. To accomodate
// this, all functions in the one-and-only driver MajorFunction table are
// initialized to point to a single function: TDIH_DefaultDispatch.
//
// TDIH_DefaultDispatch uses various means to determine which device is
// associated with each call. The primary means is to examine the
// NodeIdentifier.NodeType field which we have defined in ALL device
// extensions that are created by PCATDIH. An alternate means would have
// been to have saved pointers to each device object that we created
// as global variables and then to compare the DeviceObject pointer passed
// to this routine with each of the device object pointers that we created.
//
// Two of the devices are handled specially:
//   \\Device\PCATDIH - The "Win32 API Device"
//   \\Device\Ip      - The IP Filter Device
//
// The other three are "ordinary" TDI filter devices and are dispatched to
// functions like TDIH_Create, TDIH_DispatchInternalDeviceControl, etc.
//

NTSTATUS
TDIH_DefaultDispatch(
   PDEVICE_OBJECT DeviceObject,
   PIRP Irp
   )
{
   NTSTATUS                RC = STATUS_SUCCESS;
   PIO_STACK_LOCATION      IrpSp = NULL;
   PTDIH_DeviceExtension   pTDIH_DeviceExtension;
   PDEVICE_OBJECT          pLowerDeviceObject = NULL;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -