ReactOS 0.4.16-dev-197-g92996da
udp.c File Reference
#include "precomp.h"
Include dependency graph for udp.c:

Go to the source code of this file.

Functions

NTSTATUS AddUDPHeaderIPv4 (PADDRESS_FILE AddrFile, PIP_ADDRESS RemoteAddress, USHORT RemotePort, PIP_ADDRESS LocalAddress, USHORT LocalPort, PIP_PACKET IPPacket, PVOID Data, UINT DataLength)
 
NTSTATUS BuildUDPPacket (PADDRESS_FILE AddrFile, PIP_PACKET Packet, PIP_ADDRESS RemoteAddress, USHORT RemotePort, PIP_ADDRESS LocalAddress, USHORT LocalPort, PCHAR DataBuffer, UINT DataLen)
 
NTSTATUS UDPSendDatagram (PADDRESS_FILE AddrFile, PTDI_CONNECTION_INFORMATION ConnInfo, PCHAR BufferData, ULONG DataSize, PULONG DataUsed)
 
VOID UDPReceive (PIP_INTERFACE Interface, PIP_PACKET IPPacket)
 
NTSTATUS UDPStartup (VOID)
 
NTSTATUS UDPShutdown (VOID)
 
UINT UDPAllocatePort (UINT HintPort)
 
VOID UDPFreePort (UINT Port)
 

Variables

BOOLEAN UDPInitialized = FALSE
 
PORT_SET UDPPorts
 

Function Documentation

◆ AddUDPHeaderIPv4()

NTSTATUS AddUDPHeaderIPv4 ( PADDRESS_FILE  AddrFile,
PIP_ADDRESS  RemoteAddress,
USHORT  RemotePort,
PIP_ADDRESS  LocalAddress,
USHORT  LocalPort,
PIP_PACKET  IPPacket,
PVOID  Data,
UINT  DataLength 
)

Definition at line 16 of file udp.c.

35{
38
39 TI_DbgPrint(MID_TRACE, ("Packet: %x NdisPacket %x\n",
40 IPPacket, IPPacket->NdisPacket));
41
43 ( AddrFile, RemoteAddress, RemotePort,
44 LocalAddress, LocalPort,
45 IPPacket, DataLength, IPPROTO_UDP,
46 sizeof(UDP_HEADER), (PVOID *)&UDPHeader );
47
48 if (!NT_SUCCESS(Status))
49 return Status;
50
51 /* Port values are already big-endian values */
52 UDPHeader->SourcePort = LocalPort;
53 UDPHeader->DestPort = RemotePort;
54 UDPHeader->Checksum = 0;
55 /* Length of UDP header and data */
56 UDPHeader->Length = WH2N(DataLength + sizeof(UDP_HEADER));
57
58 TI_DbgPrint(MID_TRACE, ("Copying data (hdr %x data %x (%d))\n",
59 IPPacket->Header, IPPacket->Data,
60 (PCHAR)IPPacket->Data - (PCHAR)IPPacket->Header));
61
62 RtlCopyMemory(IPPacket->Data, Data, DataLength);
63
66 DataLength + sizeof(UDP_HEADER));
67 UDPHeader->Checksum = WH2N(UDPHeader->Checksum);
68
69 TI_DbgPrint(MID_TRACE, ("Packet: %d ip %d udp %d payload\n",
70 (PCHAR)UDPHeader - (PCHAR)IPPacket->Header,
71 (PCHAR)IPPacket->Data - (PCHAR)UDPHeader,
72 DataLength));
73
74 return STATUS_SUCCESS;
75}
#define WH2N(w)
Definition: addrconv.c:40
LONG NTSTATUS
Definition: precomp.h:26
#define MID_TRACE
Definition: debug.h:15
_In_ ULONG _In_opt_ WDFREQUEST _In_opt_ PVOID _In_ size_t _In_ PVOID _In_ size_t _Out_ size_t * DataLength
Definition: cdrom.h:1444
ULONG UDPv4ChecksumCalculate(PIPv4_HEADER IPHeader, PUCHAR PacketBuffer, ULONG DataLength)
Definition: checksum.c:60
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
#define TI_DbgPrint(_t_, _x_)
Definition: debug.h:45
#define IPPROTO_UDP
Definition: ip.h:197
struct _tagUDPHeader UDPHeader
Status
Definition: gdiplustypes.h:25
NTSTATUS AddGenericHeaderIPv4(PADDRESS_FILE AddrFile, PIP_ADDRESS RemoteAddress, USHORT RemotePort, PIP_ADDRESS LocalAddress, USHORT LocalPort, PIP_PACKET IPPacket, UINT DataLength, UINT Protocol, UINT ExtraLength, PVOID *NextHeader)
Definition: rawip.c:13
#define STATUS_SUCCESS
Definition: shellext.h:65
Definition: udp.h:15
PVOID Data
Definition: ip.h:85
PNDIS_PACKET NdisPacket
Definition: ip.h:88
PVOID Header
Definition: ip.h:83
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
unsigned char * PUCHAR
Definition: typedefs.h:53
char * PCHAR
Definition: typedefs.h:51
_Must_inspect_result_ _In_ ULONG _In_ PSOCKADDR LocalAddress
Definition: wsk.h:171
_Must_inspect_result_ _In_ ULONG _In_ PSOCKADDR _In_ PSOCKADDR RemoteAddress
Definition: wsk.h:172

Referenced by BuildUDPPacket().

◆ BuildUDPPacket()

NTSTATUS BuildUDPPacket ( PADDRESS_FILE  AddrFile,
PIP_PACKET  Packet,
PIP_ADDRESS  RemoteAddress,
USHORT  RemotePort,
PIP_ADDRESS  LocalAddress,
USHORT  LocalPort,
PCHAR  DataBuffer,
UINT  DataLen 
)

Definition at line 78 of file udp.c.

97{
99
100 TI_DbgPrint(MAX_TRACE, ("Called.\n"));
101
102 /* FIXME: Assumes IPv4 */
104
105 Packet->TotalSize = sizeof(IPv4_HEADER) + sizeof(UDP_HEADER) + DataLen;
106
107 /* Prepare packet */
109 NULL,
110 Packet->TotalSize );
111
112 if( !NT_SUCCESS(Status) )
113 {
114 Packet->Free(Packet);
115 return Status;
116 }
117
118 TI_DbgPrint(MID_TRACE, ("Allocated packet: %x\n", Packet->NdisPacket));
119 TI_DbgPrint(MID_TRACE, ("Local Addr : %s\n", A2S(LocalAddress)));
120 TI_DbgPrint(MID_TRACE, ("Remote Addr: %s\n", A2S(RemoteAddress)));
121
122 switch (RemoteAddress->Type) {
123 case IP_ADDRESS_V4:
124 Status = AddUDPHeaderIPv4(AddrFile, RemoteAddress, RemotePort,
125 LocalAddress, LocalPort, Packet, DataBuffer, DataLen);
126 break;
127 case IP_ADDRESS_V6:
128 /* FIXME: Support IPv6 */
129 TI_DbgPrint(MIN_TRACE, ("IPv6 UDP datagrams are not supported.\n"));
130 default:
132 break;
133 }
134 if (!NT_SUCCESS(Status)) {
135 TI_DbgPrint(MIN_TRACE, ("Cannot add UDP header. Status = (0x%X)\n",
136 Status));
137 Packet->Free(Packet);
138 return Status;
139 }
140
141 TI_DbgPrint(MID_TRACE, ("Displaying packet\n"));
142
144
145 TI_DbgPrint(MID_TRACE, ("Leaving\n"));
146
147 return STATUS_SUCCESS;
148}
#define MIN_TRACE
Definition: debug.h:14
#define MAX_TRACE
Definition: debug.h:16
#define NULL
Definition: types.h:112
#define IP_ADDRESS_V6
Definition: ip.h:33
#define IP_ADDRESS_V4
Definition: ip.h:32
PIP_PACKET IPInitializePacket(PIP_PACKET IPPacket, ULONG Type)
Definition: ip.c:92
PCHAR A2S(PIP_ADDRESS Address)
Definition: address.c:17
NTSTATUS AddUDPHeaderIPv4(PADDRESS_FILE AddrFile, PIP_ADDRESS RemoteAddress, USHORT RemotePort, PIP_ADDRESS LocalAddress, USHORT LocalPort, PIP_PACKET IPPacket, PVOID Data, UINT DataLength)
Definition: udp.c:16
#define AllocatePacketWithBuffer(x, y, z)
Definition: memtrack.h:7
_In_ NDIS_HANDLE _In_ PNDIS_PACKET Packet
Definition: ndis.h:1549
#define DISPLAY_IP_PACKET(x)
Definition: routines.h:69
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132

Referenced by UDPSendDatagram().

◆ UDPAllocatePort()

UINT UDPAllocatePort ( UINT  HintPort)

Definition at line 382 of file udp.c.

382 {
383 if( HintPort ) {
384 if( AllocatePort( &UDPPorts, HintPort ) ) return HintPort;
385 else return (UINT)-1;
386 } else return AllocatePortFromRange
389}
BOOLEAN AllocatePort(PPORT_SET PortSet, ULONG Port)
Definition: ports.c:47
ULONG AllocatePortFromRange(PPORT_SET PortSet, ULONG Lowest, ULONG Highest)
Definition: ports.c:86
#define UDP_STARTING_PORT
Definition: udp.h:10
#define UDP_DYNAMIC_PORTS
Definition: udp.h:11
PORT_SET UDPPorts
Definition: udp.c:14
unsigned int UINT
Definition: ndis.h:50

Referenced by FileOpenAddress().

◆ UDPFreePort()

VOID UDPFreePort ( UINT  Port)

Definition at line 391 of file udp.c.

391 {
393}
VOID DeallocatePort(PPORT_SET PortSet, ULONG Port)
Definition: ports.c:35
CPPORT Port[4]
Definition: headless.c:35

Referenced by AddrFileFree().

◆ UDPReceive()

VOID UDPReceive ( PIP_INTERFACE  Interface,
PIP_PACKET  IPPacket 
)

Definition at line 240 of file udp.c.

250{
251 AF_SEARCH SearchContext;
253 PADDRESS_FILE AddrFile;
255 PIP_ADDRESS DstAddress, SrcAddress;
256 UINT DataSize, i;
257
258 TI_DbgPrint(MAX_TRACE, ("Called.\n"));
259
260 switch (IPPacket->Type) {
261 /* IPv4 packet */
262 case IP_ADDRESS_V4:
263 IPv4Header = IPPacket->Header;
264 DstAddress = &IPPacket->DstAddr;
265 SrcAddress = &IPPacket->SrcAddr;
266 break;
267
268 /* IPv6 packet */
269 case IP_ADDRESS_V6:
270 TI_DbgPrint(MIN_TRACE, ("Discarded IPv6 UDP datagram (%i bytes).\n", IPPacket->TotalSize));
271
272 /* FIXME: IPv6 is not supported */
273 return;
274
275 default:
276 return;
277 }
278
279 UDPHeader = (PUDP_HEADER)IPPacket->Data;
280
281 /* Calculate and validate UDP checksum */
284 WH2N(UDPHeader->Length));
285 if (i != DH2N(0x0000FFFF) && UDPHeader->Checksum != 0)
286 {
287 TI_DbgPrint(MIN_TRACE, ("Bad checksum on packet received.\n"));
288 return;
289 }
290
291 /* Sanity checks */
292 i = WH2N(UDPHeader->Length);
293 if ((i < sizeof(UDP_HEADER)) || (i > IPPacket->TotalSize - IPPacket->Position)) {
294 /* Incorrect or damaged packet received, discard it */
295 TI_DbgPrint(MIN_TRACE, ("Incorrect or damaged UDP packet received.\n"));
296 return;
297 }
298
299 DataSize = i - sizeof(UDP_HEADER);
300
301 /* Go to UDP data area */
302 IPPacket->Data = (PVOID)((ULONG_PTR)IPPacket->Data + sizeof(UDP_HEADER));
303
304 /* Locate a receive request on destination address file object
305 and deliver the packet if one is found. If there is no receive
306 request on the address file object, call the associated receive
307 handler. If no receive handler is registered, drop the packet */
308
309 AddrFile = AddrSearchFirst(DstAddress,
310 UDPHeader->DestPort,
312 &SearchContext);
313 if (AddrFile) {
314 do {
315 DGDeliverData(AddrFile,
316 SrcAddress,
317 DstAddress,
318 UDPHeader->SourcePort,
319 UDPHeader->DestPort,
320 IPPacket,
321 DataSize);
322 DereferenceObject(AddrFile);
323 } while ((AddrFile = AddrSearchNext(&SearchContext)) != NULL);
324 } else {
325 /* There are no open address files that will take this datagram */
326 /* FIXME: IPv4 only */
327 TI_DbgPrint(MID_TRACE, ("Cannot deliver IPv4 UDP datagram to address (0x%X).\n",
328 DN2H(DstAddress->Address.IPv4Address)));
329
330 /* FIXME: Send ICMP reply */
331 }
332 TI_DbgPrint(MAX_TRACE, ("Leaving.\n"));
333}
#define DH2N(dw)
Definition: addrconv.c:28
#define DN2H(dw)
Definition: addrconv.c:21
PADDRESS_FILE AddrSearchFirst(PIP_ADDRESS Address, USHORT Port, USHORT Protocol, PAF_SEARCH SearchContext)
Definition: fileobjs.c:38
PADDRESS_FILE AddrSearchNext(PAF_SEARCH SearchContext)
Definition: fileobjs.c:231
VOID DGDeliverData(PADDRESS_FILE AddrFile, PIP_ADDRESS SrcAddress, PIP_ADDRESS DstAddress, USHORT SrcPort, USHORT DstPort, PIP_PACKET IPPacket, UINT DataSize)
Definition: datagram.c:52
struct _tagIPv4Header IPv4Header
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
struct UDP_HEADER * PUDP_HEADER
_In_ NDIS_STATUS _In_ ULONG _In_ USHORT _In_opt_ PVOID _In_ ULONG DataSize
Definition: ndis.h:4755
Definition: ip.h:23
union IP_ADDRESS::@1014 Address
IPv4_RAW_ADDRESS IPv4Address
Definition: ip.h:26
UCHAR Type
Definition: ip.h:79
IP_ADDRESS SrcAddr
Definition: ip.h:89
UINT Position
Definition: ip.h:87
IP_ADDRESS DstAddr
Definition: ip.h:90
UINT TotalSize
Definition: ip.h:86
#define DereferenceObject(Object)
Definition: titypes.h:24
void * PVOID
Definition: typedefs.h:50
uint32_t ULONG_PTR
Definition: typedefs.h:65

Referenced by UDPStartup().

◆ UDPSendDatagram()

NTSTATUS UDPSendDatagram ( PADDRESS_FILE  AddrFile,
PTDI_CONNECTION_INFORMATION  ConnInfo,
PCHAR  BufferData,
ULONG  DataSize,
PULONG  DataUsed 
)

Definition at line 150 of file udp.c.

166{
168 PTA_IP_ADDRESS RemoteAddressTa = (PTA_IP_ADDRESS)ConnInfo->RemoteAddress;
171 USHORT RemotePort;
174
175 LockObject(AddrFile);
176
177 TI_DbgPrint(MID_TRACE,("Sending Datagram(%x %x %x %d)\n",
178 AddrFile, ConnInfo, BufferData, DataSize));
179 TI_DbgPrint(MID_TRACE,("RemoteAddressTa: %x\n", RemoteAddressTa));
180
181 switch( RemoteAddressTa->Address[0].AddressType ) {
184 RemoteAddress.Address.IPv4Address =
185 RemoteAddressTa->Address[0].Address[0].in_addr;
186 RemotePort = RemoteAddressTa->Address[0].Address[0].sin_port;
187 break;
188
189 default:
190 UnlockObject(AddrFile);
191 return STATUS_UNSUCCESSFUL;
192 }
193
194 LocalAddress = AddrFile->Address;
196 {
197 /* If the local address is unspecified (0),
198 * then use the unicast address of the
199 * interface we're sending over
200 */
202 UnlockObject(AddrFile);
204 }
205
206 LocalAddress = NCE->Interface->Unicast;
207 }
208 else
209 {
210 if(!(NCE = NBLocateNeighbor( &LocalAddress, NULL ))) {
211 UnlockObject(AddrFile);
213 }
214 }
215
216 Status = BuildUDPPacket( AddrFile,
217 &Packet,
219 RemotePort,
221 AddrFile->Port,
222 BufferData,
223 DataSize );
224
225 UnlockObject(AddrFile);
226
227 if( !NT_SUCCESS(Status) )
228 return Status;
229
231 if (!NT_SUCCESS(Status))
232 return Status;
233
234 *DataUsed = DataSize;
235
236 return STATUS_SUCCESS;
237}
BOOLEAN AddrIsUnspecified(PIP_ADDRESS Address)
Definition: address.c:113
PNEIGHBOR_CACHE_ENTRY RouteGetRouteToDestination(PIP_ADDRESS Destination)
Definition: router.c:300
NTSTATUS BuildUDPPacket(PADDRESS_FILE AddrFile, PIP_PACKET Packet, PIP_ADDRESS RemoteAddress, USHORT RemotePort, PIP_ADDRESS LocalAddress, USHORT LocalPort, PCHAR DataBuffer, UINT DataLen)
Definition: udp.c:78
PNEIGHBOR_CACHE_ENTRY NBLocateNeighbor(PIP_ADDRESS Address, PIP_INTERFACE Interface)
Definition: neighbor.c:417
#define STATUS_NETWORK_UNREACHABLE
Definition: ntstatus.h:704
unsigned short USHORT
Definition: pedump.c:61
Definition: neighbor.h:28
USHORT Port
Definition: titypes.h:120
IP_ADDRESS Address
Definition: titypes.h:117
Definition: ip.h:77
struct _TA_ADDRESS_IP::_AddrIp Address[1]
struct _TA_ADDRESS_IP * PTA_IP_ADDRESS
#define TDI_ADDRESS_TYPE_IP
Definition: tdi.h:345
#define UnlockObject(Object)
Definition: titypes.h:44
#define LockObject(Object)
Definition: titypes.h:34
NTSTATUS IPSendDatagram(PIP_PACKET IPPacket, PNEIGHBOR_CACHE_ENTRY NCE)
Definition: transmit.c:223
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135

Referenced by FileOpenAddress().

◆ UDPShutdown()

NTSTATUS UDPShutdown ( VOID  )

Definition at line 361 of file udp.c.

368{
369 if (!UDPInitialized)
370 return STATUS_SUCCESS;
371
373
374 /* Deregister this protocol with IP layer */
376
378
379 return STATUS_SUCCESS;
380}
#define FALSE
Definition: types.h:117
VOID IPRegisterProtocol(UINT ProtocolNumber, IP_PROTOCOL_HANDLER Handler)
Definition: ip.c:386
VOID PortsShutdown(PPORT_SET PortSet)
Definition: ports.c:31
BOOLEAN UDPInitialized
Definition: udp.c:13

Referenced by TiUnload().

◆ UDPStartup()

NTSTATUS UDPStartup ( VOID  )

Definition at line 336 of file udp.c.

343{
345
347
349
350 if( !NT_SUCCESS(Status) ) return Status;
351
352 /* Register this protocol with IP layer */
354
356
357 return STATUS_SUCCESS;
358}
#define TRUE
Definition: types.h:120
NTSTATUS PortsStartup(PPORT_SET PortSet, UINT StartingPort, UINT PortsToManage)
Definition: ports.c:13
UDP_STATISTICS UDPStats
Definition: main.c:30
VOID UDPReceive(PIP_INTERFACE Interface, PIP_PACKET IPPacket)
Definition: udp.c:240
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262

Referenced by DriverEntry().

Variable Documentation

◆ UDPInitialized

BOOLEAN UDPInitialized = FALSE

Definition at line 13 of file udp.c.

Referenced by UDPShutdown(), and UDPStartup().

◆ UDPPorts

PORT_SET UDPPorts

Definition at line 14 of file udp.c.

Referenced by UDPAllocatePort(), UDPFreePort(), UDPShutdown(), and UDPStartup().