📄 udpserver.c
字号:
/////////////////////////////////////////////////////////////////////////////
//// INCLUDE FILES
#include "ndis.h"
#include "INetInc.h"
#include "TDITTCP.h"
// Copyright And Configuration Management ----------------------------------
//
// Test (TTCP) Udp Server Device - UDPSERVER.c
//
// PCAUSA TDI Client Samples For Windows NT
//
// Copyright (c) 1999-2000 Printing Communications Associates, Inc.
// - PCAUSA -
//
// Thomas F. Divine
// 4201 Brunswick Court
// Smyrna, Georgia 30080 USA
// (770) 432-4580
// tdivine@pcausa.com
//
// End ---------------------------------------------------------------------
/////////////////////////////////////////////////////////////////////////////
//// TDI TTCP UDP Server GLOBAL DATA
//
// Some developers have suggested that the gollowing g_ data should
// be placed in the DeviceExtension instead of simply being "ordinary"
// global data.
//
// DeviceExtension memory allocated from the non-paged pool. Device driver
// global memory is also allocated from the non-paged pool - UNLESS you
// use #pragma directives to cause it to be allocated from the paged pool.
//
// In this driver, use of global memory is safe enough...
//
static BOOLEAN g_bSymbolicLinkCreated = FALSE;
static LIST_ENTRY g_UDPServerList;
static KEVENT g_UDPS_KillEvent;
/////////////////////////////////////////////////////////////////////////////
//// LOCAL PROCEDURE PROTOTYPES
/////////////////////////////////////////////////////////////////////////////
//// LOCAL STRUCTURE DEFINITIONS
typedef
struct _UDPS_SERVER
{
LIST_ENTRY m_ListElement;
HANDLE m_hTestThread;
TDITTCP_TEST_PARAMS m_TestParams;
TA_IP_ADDRESS m_LocalAddress; // TDI Address
KS_ADDRESS m_KS_Address;
TA_IP_ADDRESS m_RemoteAddress;
TDI_CONNECTION_INFORMATION m_RemoteConnectionInfo;
}
UDPS_SERVER, *PUDPS_SERVER;
/////////////////////////////////////////////////////////////////////////////
//// UDPS_DeviceLoad
//
// Purpose
// This routine initializes the TDI TTCP UDP Server device.
//
// Parameters
// pDriverObject - Pointer to driver object created by system.
// RegistryPath - Pointer to the Unicode name of the registry path
// for this driver.
//
// Return Value
// The function return value is the final status from the initialization
// operation.
//
// Remarks
//
NTSTATUS
UDPS_DeviceLoad(
IN PDRIVER_OBJECT pDriverObject,
IN PUNICODE_STRING RegistryPath
)
{
UNICODE_STRING UnicodeDeviceName;
PDEVICE_OBJECT pDeviceObject = NULL;
PDEVICE_EXTENSION pDeviceExtension = NULL;
NTSTATUS Status = STATUS_SUCCESS;
NTSTATUS ErrorCode = STATUS_SUCCESS;
KdPrint(("UDPS_DeviceLoad: Entry...\n") );
//
// Initialize Global Data
//
//
// Set up the driver's device entry points.
//
pDriverObject->MajorFunction[IRP_MJ_CREATE] = TDITTCPDeviceOpen;
pDriverObject->MajorFunction[IRP_MJ_CLOSE] = TDITTCPDeviceClose;
pDriverObject->MajorFunction[IRP_MJ_READ] = TDITTCPDeviceRead;
pDriverObject->MajorFunction[IRP_MJ_WRITE] = TDITTCPDeviceWrite;
pDriverObject->MajorFunction[IRP_MJ_CLEANUP] = TDITTCPDeviceCleanup;
pDriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = TDITTCPDeviceIoControl;
pDriverObject->DriverUnload = TDITTCPDriverUnload;
//
// Create The TDI TTCP UDP Server Device
//
NdisInitUnicodeString(
&UnicodeDeviceName,
TDI_UDP_SERVER_DEVICE_NAME_W
);
Status = IoCreateDevice(
pDriverObject,
sizeof(DEVICE_EXTENSION),
&UnicodeDeviceName,
FILE_DEVICE_UDP_SERVER,
0, // Standard device characteristics
FALSE, // This isn't an exclusive device
&pDeviceObject
);
if( !NT_SUCCESS( Status ) )
{
KdPrint(("UDPS_DeviceLoad: IoCreateDevice() failed:\n") );
Status = STATUS_UNSUCCESSFUL;
return(Status);
}
//
// Create The TDI TTCP UDP Server Device Symbolic Link
//
Status = KS_CreateSymbolicLink(
&UnicodeDeviceName,
TRUE
);
if( NT_SUCCESS (Status ) )
{
g_bSymbolicLinkCreated = TRUE;
}
pDeviceObject->Flags |= DO_DIRECT_IO; // Effects Read/Write Only...
//
// Initialize The Device Extension
//
pDeviceExtension = (PDEVICE_EXTENSION)pDeviceObject->DeviceExtension;
RtlZeroMemory( pDeviceExtension, sizeof(DEVICE_EXTENSION) );
pDeviceExtension->pDeviceObject = pDeviceObject;
//
// Setup The Driver Device Entry Points
//
pDeviceExtension->MajorFunction[IRP_MJ_CREATE] = UDPS_DeviceOpen;
pDeviceExtension->MajorFunction[IRP_MJ_CLOSE] = UDPS_DeviceClose;
pDeviceExtension->MajorFunction[IRP_MJ_READ] = NULL;
pDeviceExtension->MajorFunction[IRP_MJ_WRITE] = NULL;
pDeviceExtension->MajorFunction[IRP_MJ_CLEANUP] = UDPS_DeviceCleanup;
pDeviceExtension->MajorFunction[IRP_MJ_DEVICE_CONTROL] = UDPS_DeviceIoControl;
pDeviceExtension->DeviceUnload = UDPS_DeviceUnload;
//
// Fetch Transport Provider Information
//
Status = KS_QueryProviderInfo(
UDP_DEVICE_NAME_W,
&pDeviceExtension->UdpServerContext.TdiProviderInfo
);
#ifdef DBG
if (NT_SUCCESS(Status))
{
DEBUG_DumpProviderInfo(
UDP_DEVICE_NAME_W,
&pDeviceExtension->UdpServerContext.TdiProviderInfo
);
}
#endif // DBG
InitializeListHead( &g_UDPServerList );
//
// Initialize The Sever Kill Event
//
KeInitializeEvent(
&g_UDPS_KillEvent,
NotificationEvent,
FALSE
);
return STATUS_SUCCESS;
}
/////////////////////////////////////////////////////////////////////////////
//// UDPS_DeviceOpen (IRP_MJ_CREATE Dispatch Routine)
//
// Purpose
// This is the dispatch routine for TDI TTCP UDP Server device create/open
// requests.
//
// Parameters
// pDeviceObject - Pointer to the device object.
// pIrp - Pointer to the request packet.
//
// Return Value
// Status is returned.
//
// Remarks
//
NTSTATUS
UDPS_DeviceOpen(
IN PDEVICE_OBJECT pDeviceObject,
IN PIRP pIrp
)
{
PDEVICE_EXTENSION pDeviceExtension;
KdPrint(("UDPS_DeviceOpen: Entry...\n") );
pDeviceExtension = pDeviceObject->DeviceExtension;
//
// No need to do anything.
//
//
// Fill these in before calling IoCompleteRequest.
//
// DON'T get cute and try to use the status field of
// the irp in the return status. That IRP IS GONE as
// soon as you call IoCompleteRequest.
//
KdPrint( ("UDPS_DeviceOpen: Opened!!\n") );
pIrp->IoStatus.Information = 0;
TdiCompleteRequest( pIrp, STATUS_SUCCESS );
return STATUS_SUCCESS;
}
/////////////////////////////////////////////////////////////////////////////
//// UDPS_DeviceClose (IRP_MJ_CLOSE Dispatch Routine)
//
// Purpose
// This is the dispatch routine for TDI TTCP UDP Server device close requests.
//
// Parameters
// pDeviceObject - Pointer to the device object.
// pIrp - Pointer to the request packet.
//
// Return Value
// Status is returned.
//
// Remarks
//
NTSTATUS
UDPS_DeviceClose(
IN PDEVICE_OBJECT pDeviceObject,
IN PIRP pIrp
)
{
PDEVICE_EXTENSION pDeviceExtension;
KdPrint(("UDPS_DeviceClose: Entry...\n") );
pDeviceExtension = pDeviceObject->DeviceExtension;
//
// No need to do anything.
//
//
// Fill these in before calling IoCompleteRequest.
//
// DON'T get cute and try to use the status field of
// the irp in the return status. That IRP IS GONE as
// soon as you call IoCompleteRequest.
//
KdPrint( ("UDPS_DeviceClose: Closed!!\n") );
pIrp->IoStatus.Information = 0;
TdiCompleteRequest( pIrp, STATUS_SUCCESS );
return STATUS_SUCCESS;
}
/////////////////////////////////////////////////////////////////////////////
//// UDPS_ErrorEventHandler
//
// Purpose
//
// Parameters
//
// Return Value
//
// Remarks
// A protocol error has occurred when this indication happens. This indication
// occurs only for errors of the worst type; the address this indication is
// delivered to is no longer usable for protocol-related operations, and
// should not be used for operations henceforth. All connections associated
// it are invalid.
//
NTSTATUS UDPS_ErrorEventHandler(
IN PVOID TdiEventContext, // The endpoint's file object.
IN NTSTATUS Status // Status code indicating error type.
)
{
KdPrint(("UDPS_ErrorEventHandler: Status: 0x%8.8X\n", Status) );
return( STATUS_SUCCESS );
}
/////////////////////////////////////////////////////////////////////////////
//// UDPS_ReceiveDatagramEventHandler
//
// Purpose
//
// Parameters
//
// Return Value
//
// Remarks
//
NTSTATUS UDPS_ReceiveDatagramEventHandler(
IN PVOID TdiEventContext, // Context From SetEventHandler
IN LONG SourceAddressLength, // length of the originator of the datagram
IN PVOID SourceAddress, // string describing the originator of the datagram
IN LONG OptionsLength, // options for the receive
IN PVOID Options, //
IN ULONG ReceiveDatagramFlags, //
IN ULONG BytesIndicated, // number of bytes in this indication
IN ULONG BytesAvailable, // number of bytes in complete Tsdu
OUT ULONG *BytesTaken, // number of bytes used by indication routine
IN PVOID Tsdu, // pointer describing this TSDU, typically a lump of bytes
OUT PIRP *IoRequestPacket // TdiReceive IRP if MORE_PROCESSING_REQUIRED.
)
{
PTRANSPORT_ADDRESS pTransAddr = (PTRANSPORT_ADDRESS )SourceAddress;
BOOLEAN bIsCompleteTsdu = FALSE;
KdPrint(("UDPS_ReceiveDatagramEventHandler: Entry...\n") );
KdPrint((" Bytes Indicated: %d; BytesAvailable: %d; Flags: 0x%8.8x\n",
BytesIndicated, BytesAvailable, ReceiveDatagramFlags));
DEBUG_DumpTransportAddress( pTransAddr );
//
// Determine Whether Tsdu Contains A Full TSDU
// -------------------------------------------
// We could check (ReceiveDatagramFlags & TDI_RECEIVE_ENTIRE_MESSAGE).
// However, checking (BytesIndicated == BytesAvailable) seems more
// reliable.
//
if( BytesIndicated == BytesAvailable )
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -