2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS TCP/IP protocol driver
4 * FILE: tcpip/fileobjs.c
5 * PURPOSE: Routines for handling file objects
6 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
8 * CSH 01/08-2000 Created
14 /* List of all address file objects managed by this driver */
15 LIST_ENTRY AddressFileListHead
;
16 KSPIN_LOCK AddressFileListLock
;
18 /* List of all connection endpoint file objects managed by this driver */
19 LIST_ENTRY ConnectionEndpointListHead
;
20 KSPIN_LOCK ConnectionEndpointListLock
;
23 * FUNCTION: Searches through address file entries to find the first match
25 * Address = IP address
27 * Protocol = Protocol number
28 * SearchContext = Pointer to search context
30 * Pointer to address file, NULL if none was found
32 PADDRESS_FILE
AddrSearchFirst(
36 PAF_SEARCH SearchContext
)
38 SearchContext
->Address
= Address
;
39 SearchContext
->Port
= Port
;
40 SearchContext
->Next
= AddressFileListHead
.Flink
;
41 SearchContext
->Protocol
= Protocol
;
43 return AddrSearchNext(SearchContext
);
46 BOOLEAN
AddrIsBroadcastMatch(
47 PIP_ADDRESS UnicastAddress
,
48 PIP_ADDRESS BroadcastAddress
) {
51 ForEachInterface(IF
) {
52 if ((AddrIsUnspecified(UnicastAddress
) ||
53 AddrIsEqual(&IF
->Unicast
, UnicastAddress
)) &&
54 (AddrIsEqual(&IF
->Broadcast
, BroadcastAddress
)))
61 BOOLEAN
AddrReceiveMatch(
62 PIP_ADDRESS LocalAddress
,
63 PIP_ADDRESS RemoteAddress
)
65 if (AddrIsEqual(LocalAddress
, RemoteAddress
))
67 /* Unicast address match */
71 if (AddrIsBroadcastMatch(LocalAddress
, RemoteAddress
))
73 /* Broadcast address match */
77 if (AddrIsUnspecified(LocalAddress
))
79 /* Local address unspecified */
83 if (AddrIsUnspecified(RemoteAddress
))
85 /* Remote address unspecified */
93 * FUNCTION: Searches through address file entries to find next match
95 * SearchContext = Pointer to search context
97 * Pointer to address file, NULL if none was found
99 PADDRESS_FILE
AddrSearchNext(
100 PAF_SEARCH SearchContext
)
102 PLIST_ENTRY CurrentEntry
;
103 PIP_ADDRESS IPAddress
;
105 PADDRESS_FILE Current
= NULL
;
106 BOOLEAN Found
= FALSE
;
108 if (IsListEmpty(SearchContext
->Next
))
111 CurrentEntry
= SearchContext
->Next
;
113 TcpipAcquireSpinLock(&AddressFileListLock
, &OldIrql
);
115 while (CurrentEntry
!= &AddressFileListHead
) {
116 Current
= CONTAINING_RECORD(CurrentEntry
, ADDRESS_FILE
, ListEntry
);
118 IPAddress
= &Current
->Address
;
120 TI_DbgPrint(DEBUG_ADDRFILE
, ("Comparing: ((%d, %d, %s), (%d, %d, %s)).\n",
124 WN2H(SearchContext
->Port
),
125 SearchContext
->Protocol
,
126 A2S(SearchContext
->Address
)));
128 /* See if this address matches the search criteria */
129 if ((Current
->Port
== SearchContext
->Port
) &&
130 (Current
->Protocol
== SearchContext
->Protocol
) &&
131 (AddrReceiveMatch(IPAddress
, SearchContext
->Address
))) {
132 /* We've found a match */
136 CurrentEntry
= CurrentEntry
->Flink
;
139 TcpipReleaseSpinLock(&AddressFileListLock
, OldIrql
);
142 SearchContext
->Next
= CurrentEntry
->Flink
;
151 * FUNCTION: Frees an address file object
153 * Object = Pointer to address file object to free
156 ExFreePoolWithTag(Object
, ADDR_FILE_TAG
);
160 VOID
ControlChannelFree(
163 * FUNCTION: Frees an address file object
165 * Object = Pointer to address file object to free
168 ExFreePoolWithTag(Object
, CONTROL_CHANNEL_TAG
);
173 * FUNCTION: Open an address file object
175 * Request = Pointer to TDI request structure for this request
176 * Address = Pointer to address to be opened
177 * Protocol = Protocol on which to open the address
178 * Options = Pointer to option buffer
180 * Status of operation
182 NTSTATUS
FileOpenAddress(
183 PTDI_REQUEST Request
,
184 PTA_IP_ADDRESS Address
,
188 PADDRESS_FILE AddrFile
;
190 TI_DbgPrint(MID_TRACE
, ("Called (Proto %d).\n", Protocol
));
192 AddrFile
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(ADDRESS_FILE
),
195 TI_DbgPrint(MIN_TRACE
, ("Insufficient resources.\n"));
196 return STATUS_INSUFFICIENT_RESOURCES
;
199 TI_DbgPrint(DEBUG_ADDRFILE
, ("Address file object allocated at (0x%X).\n", AddrFile
));
201 RtlZeroMemory(AddrFile
, sizeof(ADDRESS_FILE
));
203 AddrFile
->Free
= AddrFileFree
;
205 /* Set our default TTL */
208 /* Make sure address is a local unicast address or 0 */
209 /* FIXME: IPv4 only */
210 AddrFile
->Family
= Address
->Address
[0].AddressType
;
211 AddrFile
->Address
.Address
.IPv4Address
= Address
->Address
[0].Address
[0].in_addr
;
212 AddrFile
->Address
.Type
= IP_ADDRESS_V4
;
214 if (!AddrIsUnspecified(&AddrFile
->Address
) &&
215 !AddrLocateInterface(&AddrFile
->Address
)) {
216 ExFreePoolWithTag(AddrFile
, ADDR_FILE_TAG
);
217 TI_DbgPrint(MIN_TRACE
, ("Non-local address given (0x%X).\n", A2S(&AddrFile
->Address
)));
218 return STATUS_INVALID_PARAMETER
;
221 TI_DbgPrint(MID_TRACE
, ("Opening address %s for communication (P=%d U=%d).\n",
222 A2S(&AddrFile
->Address
), Protocol
, IPPROTO_UDP
));
224 /* Protocol specific handling */
228 TCPAllocatePort(Address
->Address
[0].Address
[0].sin_port
);
230 if ((Address
->Address
[0].Address
[0].sin_port
&&
231 AddrFile
->Port
!= Address
->Address
[0].Address
[0].sin_port
) ||
232 AddrFile
->Port
== 0xffff)
234 ExFreePoolWithTag(AddrFile
, ADDR_FILE_TAG
);
235 return STATUS_INVALID_PARAMETER
;
238 AddEntity(CO_TL_ENTITY
, AddrFile
, CO_TL_TCP
);
240 AddrFile
->Send
= NULL
; /* TCPSendData */
244 TI_DbgPrint(MID_TRACE
,("Allocating udp port\n"));
246 UDPAllocatePort(Address
->Address
[0].Address
[0].sin_port
);
248 if ((Address
->Address
[0].Address
[0].sin_port
&&
249 AddrFile
->Port
!= Address
->Address
[0].Address
[0].sin_port
) ||
250 AddrFile
->Port
== 0xffff)
252 ExFreePoolWithTag(AddrFile
, ADDR_FILE_TAG
);
253 return STATUS_INVALID_PARAMETER
;
256 TI_DbgPrint(MID_TRACE
,("Setting port %d (wanted %d)\n",
258 Address
->Address
[0].Address
[0].sin_port
));
260 AddEntity(CL_TL_ENTITY
, AddrFile
, CL_TL_UDP
);
262 AddrFile
->Send
= UDPSendDatagram
;
267 AddrFile
->Send
= ICMPSendDatagram
;
269 /* FIXME: Verify this */
270 AddEntity(ER_ENTITY
, AddrFile
, ER_ICMP
);
274 /* Use raw IP for all other protocols */
276 AddrFile
->Send
= RawIPSendDatagram
;
278 /* FIXME: Verify this */
279 AddEntity(CL_TL_ENTITY
, AddrFile
, 0);
283 TI_DbgPrint(MID_TRACE
, ("IP protocol number for address file object is %d.\n",
286 TI_DbgPrint(MID_TRACE
, ("Port number for address file object is %d.\n",
287 WN2H(AddrFile
->Port
)));
290 AddrFile
->Protocol
= Protocol
;
292 /* Initialize receive and transmit queues */
293 InitializeListHead(&AddrFile
->ReceiveQueue
);
294 InitializeListHead(&AddrFile
->TransmitQueue
);
296 /* Initialize spin lock that protects the address file object */
297 KeInitializeSpinLock(&AddrFile
->Lock
);
299 /* Return address file object */
300 Request
->Handle
.AddressHandle
= AddrFile
;
302 /* Add address file to global list */
303 ExInterlockedInsertTailList(
304 &AddressFileListHead
,
305 &AddrFile
->ListEntry
,
306 &AddressFileListLock
);
308 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
310 return STATUS_SUCCESS
;
315 * FUNCTION: Closes an address file object
317 * Request = Pointer to TDI request structure for this request
319 * Status of operation
321 NTSTATUS
FileCloseAddress(
322 PTDI_REQUEST Request
)
324 PADDRESS_FILE AddrFile
;
325 NTSTATUS Status
= STATUS_SUCCESS
;
327 PDATAGRAM_RECEIVE_REQUEST ReceiveRequest
;
328 PDATAGRAM_SEND_REQUEST SendRequest
;
329 PLIST_ENTRY CurrentEntry
;
331 AddrFile
= Request
->Handle
.AddressHandle
;
333 TI_DbgPrint(MID_TRACE
, ("Called.\n"));
335 /* Remove address file from the global list */
336 TcpipAcquireSpinLock(&AddressFileListLock
, &OldIrql
);
337 RemoveEntryList(&AddrFile
->ListEntry
);
338 TcpipReleaseSpinLock(&AddressFileListLock
, OldIrql
);
340 /* FIXME: Kill TCP connections on this address file object */
342 /* Return pending requests with error */
344 TI_DbgPrint(DEBUG_ADDRFILE
, ("Aborting receive requests on AddrFile at (0x%X).\n", AddrFile
));
346 /* Go through pending receive request list and cancel them all */
347 while ((CurrentEntry
= ExInterlockedRemoveHeadList(&AddrFile
->ReceiveQueue
, &AddrFile
->Lock
))) {
348 ReceiveRequest
= CONTAINING_RECORD(CurrentEntry
, DATAGRAM_RECEIVE_REQUEST
, ListEntry
);
349 (*ReceiveRequest
->Complete
)(ReceiveRequest
->Context
, STATUS_CANCELLED
, 0);
350 /* ExFreePoolWithTag(ReceiveRequest, DATAGRAM_RECV_TAG); FIXME: WTF? */
353 TI_DbgPrint(DEBUG_ADDRFILE
, ("Aborting send requests on address file at (0x%X).\n", AddrFile
));
355 /* Go through pending send request list and cancel them all */
356 while ((CurrentEntry
= ExInterlockedRemoveHeadList(&AddrFile
->ReceiveQueue
, &AddrFile
->Lock
))) {
357 SendRequest
= CONTAINING_RECORD(CurrentEntry
, DATAGRAM_SEND_REQUEST
, ListEntry
);
358 (*SendRequest
->Complete
)(SendRequest
->Context
, STATUS_CANCELLED
, 0);
359 ExFreePoolWithTag(SendRequest
, DATAGRAM_SEND_TAG
);
362 /* Protocol specific handling */
363 switch (AddrFile
->Protocol
) {
365 TCPFreePort( AddrFile
->Port
);
366 if( AddrFile
->Listener
)
367 TCPClose( AddrFile
->Listener
);
371 UDPFreePort( AddrFile
->Port
);
375 RemoveEntityByContext(AddrFile
);
377 (*AddrFile
->Free
)(AddrFile
);
379 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
386 * FUNCTION: Opens a connection file object
388 * Request = Pointer to TDI request structure for this request
389 * ClientContext = Pointer to client context information
391 * Status of operation
393 NTSTATUS
FileOpenConnection(
394 PTDI_REQUEST Request
,
398 PCONNECTION_ENDPOINT Connection
;
400 TI_DbgPrint(MID_TRACE
, ("Called.\n"));
402 Connection
= TCPAllocateConnectionEndpoint( ClientContext
);
404 if( !Connection
) return STATUS_NO_MEMORY
;
406 Status
= TCPSocket( Connection
, AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
408 if( !NT_SUCCESS(Status
) ) {
409 TCPFreeConnectionEndpoint( Connection
);
413 /* Return connection endpoint file object */
414 Request
->Handle
.ConnectionContext
= Connection
;
416 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
418 return STATUS_SUCCESS
;
422 * FUNCTION: Closes an connection file object
424 * Request = Pointer to TDI request structure for this request
426 * Status of operation
428 NTSTATUS
FileCloseConnection(
429 PTDI_REQUEST Request
)
431 PCONNECTION_ENDPOINT Connection
;
433 TI_DbgPrint(MID_TRACE
, ("Called.\n"));
435 Connection
= Request
->Handle
.ConnectionContext
;
437 TCPClose( Connection
);
439 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
441 return STATUS_SUCCESS
;
446 * FUNCTION: Opens a control channel file object
448 * Request = Pointer to TDI request structure for this request
450 * Status of operation
452 NTSTATUS
FileOpenControlChannel(
453 PTDI_REQUEST Request
)
455 PCONTROL_CHANNEL ControlChannel
;
456 TI_DbgPrint(MID_TRACE
, ("Called.\n"));
458 ControlChannel
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(*ControlChannel
),
459 CONTROL_CHANNEL_TAG
);
461 if (!ControlChannel
) {
462 TI_DbgPrint(MIN_TRACE
, ("Insufficient resources.\n"));
463 return STATUS_INSUFFICIENT_RESOURCES
;
466 RtlZeroMemory(ControlChannel
, sizeof(CONTROL_CHANNEL
));
468 /* Make sure address is a local unicast address or 0 */
470 /* Locate address entry. If specified address is 0, a random address is chosen */
472 /* Initialize receive and transmit queues */
473 InitializeListHead(&ControlChannel
->ListEntry
);
475 /* Initialize spin lock that protects the address file object */
476 KeInitializeSpinLock(&ControlChannel
->Lock
);
478 /* Return address file object */
479 Request
->Handle
.ControlChannel
= ControlChannel
;
481 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
483 return STATUS_SUCCESS
;
487 * FUNCTION: Closes a control channel file object
489 * Request = Pointer to TDI request structure for this request
491 * Status of operation
493 NTSTATUS
FileCloseControlChannel(
494 PTDI_REQUEST Request
)
496 PCONTROL_CHANNEL ControlChannel
= Request
->Handle
.ControlChannel
;
497 NTSTATUS Status
= STATUS_SUCCESS
;
499 ExFreePoolWithTag(ControlChannel
, CONTROL_CHANNEL_TAG
);
500 Request
->Handle
.ControlChannel
= NULL
;