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

📄 udpserver.c

📁 网络驱动开发
💻 C
📖 第 1 页 / 共 2 页
字号:
      bIsCompleteTsdu = TRUE;
   }

   if( bIsCompleteTsdu )
   {
      //
      // Process Complete TSDU
      // ---------------------
      // Handler should copy all of the indicated data to internal buffer
      // and return control as quickly as possible.
      //

      *BytesTaken = BytesAvailable;

      return( STATUS_SUCCESS );
   }

   //
   // Process Partial TSDU
   // --------------------
   // One could check (ReceiveDatagramFlags & TDI_RECEIVE_COPY_LOOKAHEAD) to
   // determine whether copying the BytesIndicated lookahead data is
   // required or not. However, since the case where lookahead data must
   // be copied must be dealt with anyway, it seems simpler to just go ahead
   // and always copy the lookahead data here in the handler.
   // 

   *BytesTaken = BytesIndicated;

   return( STATUS_SUCCESS );
}

/////////////////////////////////////////////////////////////////////////////
//// UDPS_TestThread
//
// Purpose
//
// Parameters
//
// Return Value
//
// Remarks
//

VOID
UDPS_TestThread(
   IN PVOID pContext
   )
{
   NTSTATUS             Status = STATUS_SUCCESS;
   NDIS_STATUS          nNdisStatus;
   PTTCP_TEST_START_CMD pStartCmd = NULL;
   PUDPS_SERVER         pServer = (PUDPS_SERVER )pContext;
   LARGE_INTEGER        DelayTime;

   KdPrint(("UDPS_TestThread: Starting...\n") );

   //
   // Initialize Default Settings
   //
   Status = STATUS_SUCCESS;      // Always Indicate I/O Success

   //
   // Locate Test Session Parameter Buffer
   //
   pServer = (PUDPS_SERVER )pContext;

   //
   // Setup Local TDI Address
   //
   KS_InitIPAddress(
      &pServer->m_LocalAddress,
      INADDR_ANY,                   // Any Local Address
      pServer->m_TestParams.m_Port  // Specific Port
      );

   //
   // Open Transport Address
   //
   Status = KS_OpenTransportAddress(
                  UDP_DEVICE_NAME_W,
                  (PTRANSPORT_ADDRESS )&pServer->m_LocalAddress,
                  &pServer->m_KS_Address
                  );

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

   //
   // Setup Event Handlers On The Server Address Object
   //
   Status = KS_SetEventHandlers(
                  &pServer->m_KS_Address,
                  pServer,       // Event Context
                  NULL,          // ConnectEventHandler
                  NULL,          // DisconnectEventHandler,
                  UDPS_ErrorEventHandler,
                  NULL,          // ReceiveEventHandler,
                  UDPS_ReceiveDatagramEventHandler,
                  NULL           // ReceiveExpeditedEventHandler
                  );

   if( !NT_SUCCESS( Status ) )
   {
      //
      // Event Handlers Could Not Be Set
      //
      goto ExitTheThread;
   }

   KdPrint(("UDPS_TestThread: Set Event Handlers On The Server Address Object\n") );

   //
   //
   // Add The Server To The Active Server List
   //
	InsertTailList(
		&g_UDPServerList,
		&pServer->m_ListElement
		);

   Status = KeWaitForSingleObject(
               &g_UDPS_KillEvent,  // Object to wait on.
               Executive,  // Reason for waiting
               KernelMode, // Processor mode
               FALSE,      // Alertable
               NULL        // Timeout
               );

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

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

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

   KeDelayExecutionThread( KernelMode, FALSE, &DelayTime );

   //
   // Exit The Thread
   //
ExitTheThread:

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

   if( pServer )
   {
      //
      // Close The Transport Address
      //
      KS_CloseTransportAddress( &pServer->m_KS_Address );

      NdisFreeMemory( pServer, sizeof( UDPS_SERVER ), 0 );
   }

   pServer = NULL;

   (void)PsTerminateSystemThread( STATUS_SUCCESS );
}


/////////////////////////////////////////////////////////////////////////////
//// UDPS_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
UDPS_StartTest(
   IN PDEVICE_OBJECT pDeviceObject,
   IN PIRP pIrp
   )
{
   NDIS_STATUS          nNdisStatus;
   PTTCP_TEST_START_CMD pStartCmd = NULL;
   PUDPS_SERVER         pServer = 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(
                  &pServer,
                  sizeof( UDPS_SERVER ),
                  0,       // Allocate non-paged system-space memory
                  HighestAcceptableMax
                  );

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

   NdisZeroMemory( pServer, sizeof( UDPS_SERVER ) );

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

   //
   // Start The Thread That Will Execute The Test
   //
   PsCreateSystemThread(
      &pServer->m_hTestThread,   // thread handle
      0L,               // desired access
      NULL,             // object attributes
      NULL,             // process handle
      NULL,             // client id
      UDPS_TestThread,  // start routine
      (PVOID )pServer  // start context
      );

   return( STATUS_SUCCESS );
}


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

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

   KdPrint(("UDPS_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_SERVER_START_TEST:
         Status = UDPS_StartTest( pDeviceObject, pIrp );
         break;

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

   TdiCompleteRequest( pIrp, Status );

   return( Status );
}


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

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

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

   pDeviceExtension = pDeviceObject->DeviceExtension;

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

   return( STATUS_SUCCESS );
}


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

VOID
UDPS_DeviceUnload(
   IN PDEVICE_OBJECT pDeviceObject
   )
{
   PDEVICE_EXTENSION pDeviceExtension;
   KEVENT            UDPS_UnloadEvent;

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

   pDeviceExtension = pDeviceObject->DeviceExtension;

   KeSetEvent( &g_UDPS_KillEvent, 0, FALSE);

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

      NdisInitUnicodeString(
         &UnicodeDeviceName,
         TDI_UDP_SERVER_DEVICE_NAME_W
         );

      KS_CreateSymbolicLink(
         &UnicodeDeviceName,
         FALSE
         );
   }
}

⌨️ 快捷键说明

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