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

📄 udpclient.c

📁 网络驱动开发
💻 C
📖 第 1 页 / 共 2 页
字号:
                  0,       // Allocate non-paged system-space memory
                  HighestAcceptableMax
                  );

   if( !NT_SUCCESS( nNdisStatus ) )
   {
      pSession->m_pPatternDataBuffer = NULL;

      goto ExitTheThread;
   }

   TDITTCP_FillPatternBuffer( 
      pSession->m_pPatternDataBuffer,
      pSession->m_PatternDataBufferSize
      );

   pSession->m_pPatternDataMdl = KS_AllocateAndProbeMdl(
                              pSession->m_pPatternDataBuffer,   // Virtual Address
                              pSession->m_PatternDataBufferSize,
                              FALSE,
                              FALSE,
                              NULL
                              );

   if( !pSession->m_pPatternDataMdl )
   {
      goto ExitTheThread;
   }

   //
   // Setup UDP Guard Memory And MDL
   //
   RtlCopyMemory( pSession->m_UdpGuardBuffer, "PCAx", UDP_GUARD_BUFFER_LENGTH );

   pSession->m_pUdpGuardMdl = KS_AllocateAndProbeMdl(
                                 pSession->m_UdpGuardBuffer,   // Virtual Address
                                 UDP_GUARD_BUFFER_LENGTH,
                                 FALSE,
                                 FALSE,
                                 NULL
                                 );

   if( !pSession->m_pUdpGuardMdl )
   {
      goto ExitTheThread;
   }

   //
   // Setup Local TDI Address
   //
   KS_InitIPAddress(
      &pSession->m_LocalAddress,
      INADDR_ANY,    // Any Local Address
      0              // Any Local Port
      );

   //
   // Create The Local Address Object
   //
   Status = KS_OpenTransportAddress(
                  UDP_DEVICE_NAME_W,
                  (PTRANSPORT_ADDRESS )&pSession->m_LocalAddress,
                  &pSession->m_KS_Address
                  );

   if( !NT_SUCCESS( Status ) )
   {
      //
      // Address Object Could Not Be Created
      //
      goto ExitTheThread;
   }

   KdPrint(("UDPC_TestThread: Created Local Address Object\n") );

   //
   // Setup Event Handlers On The Address Object
   //
   Status = KS_SetEventHandlers(
                  &pSession->m_KS_Address,
                  pSession,      // Event Context
                  NULL,          // ConnectEventHandler
                  NULL,          // DisconnectEventHandler,
                  UDPC_ErrorEventHandler,
                  NULL,          // ReceiveEventHandler,
                  UDPC_ReceiveDatagramEventHandler,
                  NULL           // ReceiveExpeditedEventHandler
                  );

   if( !NT_SUCCESS( Status ) )
   {
      //
      // Event Handlers Could Not Be Set
      //
      KS_CloseTransportAddress( &pSession->m_KS_Address );

      goto ExitTheThread;
   }

   //
   // Setup Remote TDI Address
   //
   KS_InitIPAddress(
      &pSession->m_RemoteAddress,
      pSession->m_TestParams.m_RemoteAddress.s_addr,
      pSession->m_TestParams.m_Port
      );

   //
   // Setup Remote Connection Info
   //
   NdisZeroMemory(
      &pSession->m_RemoteConnectionInfo,
      sizeof( TDI_CONNECTION_INFORMATION )
      );

   pSession->m_RemoteConnectionInfo.RemoteAddress = &pSession->m_RemoteAddress;
   pSession->m_RemoteConnectionInfo.RemoteAddressLength = sizeof( TA_IP_ADDRESS );

   if( NT_SUCCESS( nNdisStatus ) )
   {
      int   i;

      switch( pSession->m_TestParams.m_SendMode )
      {
         case TTCP_SEND_NEXT_FROM_COMPLETION:
            KdPrint(( "  Send Mode: Send Next From Completion.\n"));

         case TTCP_SEND_SYNCHRONOUS:
         default:
            KdPrint(( "  Send Mode: Synchronous Send.\n"));

            //
            // Send Guard Buffer Preamble
            //
            Status = UDPC_SendGuardBuffer( pSession );

            if( !NT_SUCCESS(Status) )
            {
               KdPrint(( "UDPC_SendBuffer: Status 0x%8.8X\n", Status ));
               break;
            }

            while( pSession->m_nNumBuffersToSend-- )
            {
               //
               // Synchronous Send On The Connection
               //
               (pSession->m_pPatternHeaderMdl)->Next = pSession->m_pPatternDataMdl;
               (pSession->m_pPatternDataMdl)->Next = NULL;   // IMPORTANT!!!

               Status = KS_SendDatagramOnAddress(
                              &pSession->m_KS_Address,
                              NULL,       // User Completion Event
                              NULL,       // User Completion Routine
                              NULL,       // User Completion Context
                              &pSession->m_pPatternIoStatus,
                              pSession->m_pPatternHeaderMdl,   // First Of Chain
                              &pSession->m_RemoteConnectionInfo
                              );

               if( NT_SUCCESS(Status) )
               {
                  KdPrint(( "UDPC_SendBuffer: Status 0x%8.8X\n", pSession->m_pPatternIoStatus.Status ));
                  KdPrint(( "UDPC_SendBuffer: Sent %d Bytes\n", pSession->m_pPatternIoStatus.Information ));
               }
               else
               {
                  KdPrint(( "UDPC_SendBuffer: Status 0x%8.8X\n", Status ));
                  break;
               }
            }

            for( i = 0; i < 4; ++i )
            {
               //
               // Send Guard Buffer Postamble
               //
               (pSession->m_pUdpGuardMdl)->Next = NULL;   // IMPORTANT!!!

               Status = KS_SendDatagramOnAddress(
                              &pSession->m_KS_Address,
                              NULL,       // User Completion Event
                              NULL,       // User Completion Routine
                              NULL,       // User Completion Context
                              &pSession->m_pPatternIoStatus,
                              pSession->m_pUdpGuardMdl,
                              &pSession->m_RemoteConnectionInfo
                              );

               if( NT_SUCCESS(Status) )
               {
                  KdPrint(( "UDPC_SendBuffer: Sent %d Bytes\n", pSession->m_pPatternIoStatus.Information ));
               }
               else
               {
                  KdPrint(( "UDPC_SendBuffer: Status 0x%8.8X\n", Status ));
                  break;
               }
            }
            break;
      }
   }

   //
   // ATTENTION!!! Test Exit...
   //

   KdPrint(( "Waiting After Disconnect...\n" ));

   DelayTime.QuadPart = 10*1000*1000*5;   // 5 Seconds

   KeDelayExecutionThread( KernelMode, FALSE, &DelayTime );

   KS_CloseTransportAddress( &pSession->m_KS_Address );

   //
   // Exit The Thread
   //
ExitTheThread:

   KdPrint(("UDPC_TestThread: Exiting...\n") );

   if( pSession )
   {
      //
      // Free UDP Guard MDL And Memory
      //
      if( pSession->m_pUdpGuardMdl )
      {
         KS_UnlockAndFreeMdl( pSession->m_pUdpGuardMdl );
      }

      pSession->m_pUdpGuardMdl = NULL;


      //
      // Free Pattern Data MDL And Memory
      //
      if( pSession->m_pPatternDataMdl )
      {
         KS_UnlockAndFreeMdl( pSession->m_pPatternDataMdl );
      }

      pSession->m_pPatternDataMdl = NULL;


      if( pSession->m_pPatternDataBuffer )
      {
         NdisFreeMemory(
            pSession->m_pPatternDataBuffer,
            pSession->m_PatternDataBufferSize,
            0
            );
      }

      pSession->m_pPatternDataBuffer = NULL;


      //
      // Free Pattern Header MDL
      //
      if( pSession->m_pPatternDataMdl )
      {
         KS_UnlockAndFreeMdl( pSession->m_pPatternDataMdl );
      }

      pSession->m_pPatternDataMdl = NULL;

      NdisFreeMemory( pSession, sizeof( UDPC_SESSION ), 0 );
   }

   pSession = NULL;

   (void)PsTerminateSystemThread( STATUS_SUCCESS );
}


/////////////////////////////////////////////////////////////////////////////
//// UDPC_StartTest
//
// Purpose
// Start A TTCP TCP Server Test
//
// Parameters
//    pDeviceObject - Pointer to the device object.
//    pIrp - Pointer to the request packet.
//
// Return Value
// Status is returned.
//
// Remarks
//

NTSTATUS
UDPC_StartTest(
   IN PDEVICE_OBJECT pDeviceObject,
   IN PIRP pIrp
   )
{
   HANDLE               hTestThread;
   NDIS_STATUS          nNdisStatus;
   PTTCP_TEST_START_CMD pStartCmd = NULL;
   PUDPC_SESSION        pSession = NULL;

   //
   // Initialize Default Settings
   //
   pIrp->IoStatus.Information = sizeof( ULONG );  // For m_Status

   //
   // Locate Test Start Command Buffer
   //
   pStartCmd = (PTTCP_TEST_START_CMD )pIrp->AssociatedIrp.SystemBuffer;

   pStartCmd->m_Status = STATUS_UNSUCCESSFUL;

   TDITTCP_DumpTestParams( &pStartCmd->m_TestParams );

   //
   // Allocate Memory For The Test Session
   //
   nNdisStatus = NdisAllocateMemory(
                  &pSession,
                  sizeof( UDPC_SESSION ),
                  0,       // Allocate non-paged system-space memory
                  HighestAcceptableMax
                  );

   if( !NT_SUCCESS( nNdisStatus ) )
   {
      return( nNdisStatus );
   }

   NdisZeroMemory( pSession, sizeof( UDPC_SESSION ) );

   NdisMoveMemory(
      &pSession->m_TestParams,
      &pStartCmd->m_TestParams,
      sizeof( TDITTCP_TEST_PARAMS )
      );

   //
   // Start The Thread That Will Execute The Test
   //
   PsCreateSystemThread(
      &hTestThread,     // thread handle
      0L,               // desired access
      NULL,             // object attributes
      NULL,             // process handle
      NULL,             // client id
      UDPC_TestThread,  // start routine
      (PVOID )pSession  // start context
      );

   return( STATUS_SUCCESS );
}


/////////////////////////////////////////////////////////////////////////////
//// UDPC_DeviceIoControl (IRP_MJ_DEVICE_CONTROL Dispatch Routine)
//
// Purpose
// This is the dispatch routine for TDI TTCP UDP Client device IOCTL requests.
//
// Parameters
//    pDeviceObject - Pointer to the device object.
//    pIrp - Pointer to the request packet.
//
// Return Value
// Status is returned.
//
// Remarks
//

NTSTATUS
UDPC_DeviceIoControl(
    IN PDEVICE_OBJECT pDeviceObject,
    IN PIRP pIrp
    )
{
   PDEVICE_EXTENSION    pDeviceExtension;
   NTSTATUS             Status;
   PIO_STACK_LOCATION   pIrpSp;
   ULONG                nFunctionCode;

   KdPrint(("UDPC_DeviceIoControl: Entry...\n") );

   pDeviceExtension = pDeviceObject->DeviceExtension;

   pIrp->IoStatus.Information = 0;      // Nothing Returned Yet

   pIrpSp = IoGetCurrentIrpStackLocation(pIrp);

   nFunctionCode=pIrpSp->Parameters.DeviceIoControl.IoControlCode;

   switch( nFunctionCode )
   {
      case IOCTL_UDP_CLIENT_START_TEST:
         Status = UDPC_StartTest( pDeviceObject, pIrp );
         break;

      default:
         KdPrint((  "FunctionCode: 0x%8.8X\n", nFunctionCode ));
         Status = STATUS_NOT_IMPLEMENTED;
         break;
   }

   TdiCompleteRequest( pIrp, Status );

   return( Status );
}


/////////////////////////////////////////////////////////////////////////////
//// UDPC_DeviceCleanup (IRP_MJ_CLEANUP Dispatch Routine)
//
// Purpose
// This is the dispatch routine for TDI TTCP UDP Client device cleanup
// requests.
//
// Parameters
//    pDeviceObject - Pointer to the device object.
//    pFlushIrp - Pointer to the flush request packet.
//
// Return Value
// Status is returned.
//
// Remarks
//

NTSTATUS
UDPC_DeviceCleanup(
    IN PDEVICE_OBJECT pDeviceObject,
    IN PIRP pIrp
    )
{
   PDEVICE_EXTENSION    pDeviceExtension;

   KdPrint(("UDPC_DeviceCleanup: Entry...\n"));

   pDeviceExtension = pDeviceObject->DeviceExtension;

   pIrp->IoStatus.Status = STATUS_SUCCESS;
   pIrp->IoStatus.Information = 0;

   return( STATUS_SUCCESS );
}


/////////////////////////////////////////////////////////////////////////////
//// UDPC_DeviceUnload
//
// Purpose
//
// Parameters
//   pDeviceObject - Pointer to device object created by system.
//
// Return Value
//
// Remarks
//

VOID
UDPC_DeviceUnload(
   IN PDEVICE_OBJECT pDeviceObject
   )
{
   PDEVICE_EXTENSION  pDeviceExtension;

   KdPrint(("UDPC_DeviceUnload: Entry...\n") );

   pDeviceExtension = pDeviceObject->DeviceExtension;

   //
   // Destroy The Symbolic Link
   //
   if( g_bSymbolicLinkCreated )
   {
      UNICODE_STRING UnicodeDeviceName;

      NdisInitUnicodeString(
         &UnicodeDeviceName,
         TDI_UDP_CLIENT_DEVICE_NAME_W
         );

      KS_CreateSymbolicLink(
         &UnicodeDeviceName,
         FALSE
         );
   }
}

⌨️ 快捷键说明

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