Windows Vista Tips

Windows Vista Tips > Newsgroups > Windows Vista Drivers > Open and Close an UDP Connection

Reply
Thread Tools Display Modes

Open and Close an UDP Connection

 
 
Harald
Guest
Posts: n/a

 
      06-02-2009
Hi
I am writing my own little functions to open and close an UDP Port over TDI.
The open function works fine and when i use netstat -a i see that the port is
opened. But when i call the close function the port is still open.

Open:

// open an specific port
NTSTATUS UdpToTdi_OpenPort(UDP_PORT_INFO* port,ushort udpport)
{
NTSTATUS status = STATUS_SUCCESS;
PFILE_FULL_EA_INFORMATION pEa;
PTA_IP_ADDRESS pSin;
PTCP_REQUEST_SET_INFORMATION_EX pSetInfoEx;
PIO_STACK_LOCATION StackLocation;
void *pBuf = &gl_tos_value;
gl_tos_size = sizeof(gl_tos_value);

if(gl_StatusInit == 0)
{
DbgPrint("TdiInterfaceUdp::UdpToTdi_OpenPort: TDI not initialized\n");
return STATUS_ACCESS_DENIED;
}

InitializeObjectAttributes(&port->Attr,
&gl_DeviceName,
OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
NULL,
NULL);

pEa = (PFILE_FULL_EA_INFORMATION)port->pEa_buf;
pEa->NextEntryOffset = 0;
pEa->Flags = 0;
pEa->EaNameLength = TDI_TRANSPORT_ADDRESS_LENGTH;
RtlCopyMemory(pEa->EaName, TdiTransportAddress, pEa->EaNameLength + 1);
pEa->EaValueLength = sizeof(TA_IP_ADDRESS);
pSin = (PTA_IP_ADDRESS)(pEa->EaName + pEa->EaNameLength + 1);
pSin->TAAddressCount = 1;



pSin->Address[0].AddressLength = TDI_ADDRESS_LENGTH_IP;



pSin->Address[0].AddressType = TDI_ADDRESS_TYPE_IP;



pSin->Address[0].Address[0].sin_port = RtlUshortByteSwap(udpport);
pSin->Address[0].Address[0].in_addr = 0; // own ip address (0...he is
searching it himself)
RtlZeroMemory(pSin->Address[0].Address[0].sin_zero,
sizeof(pSin->Address[0].Address[0].sin_zero));

status = ZwCreateFile(&port->hFile,
GENERIC_READ | GENERIC_WRITE,
&port->Attr,
&port->IoStatus,
0,
FILE_ATTRIBUTE_NORMAL,
FILE_SHARE_READ | FILE_SHARE_WRITE,
FILE_OPEN,
0,
pEa,
sizeof(port->pEa_buf)
);
if (!NT_SUCCESS(status))
{
DbgPrint("TdiInterfaceUdp::UdpToTdi_OpenPort: ZwCreateFile failed\n");
return status;
}

status = ObReferenceObjectByHandle(port->hFile,
GENERIC_READ | GENERIC_WRITE,
NULL,
KernelMode,
(PVOID *)&port->pAddrFileObj,
NULL);

if (!NT_SUCCESS(status))
{
DbgPrint("TdiInterfaceUdp::UdpToTdi_OpenPort: ObReferenceObjectByHandle
failed\n");
return status;
}

// set the QoS bits in the IP header
pSetInfoEx = (PTCP_REQUEST_SET_INFORMATION_EX)&(port->pSetInfoEx);
RtlZeroMemory(pSetInfoEx, sizeof(TCP_REQUEST_SET_INFORMATION_EX) +
gl_tos_size);

pSetInfoEx->ID.toi_entity.tei_entity = CL_TL_ENTITY;
pSetInfoEx->ID.toi_entity.tei_instance = 0;
pSetInfoEx->ID.toi_class = INFO_CLASS_PROTOCOL;
pSetInfoEx->ID.toi_type = INFO_TYPE_ADDRESS_OBJECT;
pSetInfoEx->ID.toi_id = AO_OPTION_TOS;

RtlMoveMemory(pSetInfoEx->Buffer, pBuf, gl_tos_size);
pSetInfoEx->BufferSize = gl_tos_size;

port->pIrp = IoBuildDeviceIoControlRequest(IOCTL_TCP_SET_INFORM ATION_EX,
gl_pDevice,
(PVOID)pSetInfoEx,
sizeof(TCP_REQUEST_SET_INFORMATION_EX) + gl_tos_size,
NULL,
0,
FALSE,
NULL,
NULL);

if(port->pIrp)
{
StackLocation = IoGetNextIrpStackLocation(port->pIrp);
StackLocation->DeviceObject = gl_pDevice;
StackLocation->FileObject = port->pAddrFileObj;
status = IoCallDriver(gl_pDevice, port->pIrp);
if(status != STATUS_SUCCESS && status != STATUS_PENDING)
DbgPrint("TdiInterfaceUdp::UdpToTdi_OpenPort: IoCallDriver failed\n");
}

return status;
}

Close:

NTSTATUS UdpToTdi_ClosePort(UDP_PORT_INFO* port)
{
NTSTATUS status = STATUS_SUCCESS;

if(gl_StatusInit == 0)
{
DbgPrint("TdiInterfaceUdp::UdpToTdi_ClosePort: TDI not initialized\n");
return STATUS_ACCESS_DENIED;
}

status = ObDereferenceObject(port->pAddrFileObj);
if(!NT_SUCCESS(status))
DbgPrint("TdiInterfaceUdp::UdpToTdi_ClosePort: ObDereferenceObject
failed\n");

status = ZwClose(&port->hFile);
if(!NT_SUCCESS(status))
DbgPrint("TdiInterfaceUdp::UdpToTdi_ClosePort: ZwClose failed\n");

return status;
}

Did i miss anything in the close function?

Harald
 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Programs won't open until others close Some.Net(Guy) Windows Vista General Discussion 1 11-07-2009 03:27 PM
Open windows slow to close Buddha Windows Vista General Discussion 0 02-07-2009 07:30 PM
WMP wants to close after device connection Limitless Windows Media Player 2 10-25-2008 12:03 AM
lose internet connection when open/close or snd/rcve windows mail Leise Windows Vista Mail 1 07-17-2007 05:18 PM
How to close open window w/o closing ie6.0 Jim Windows Update 2 04-26-2004 03:31 AM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59