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
160 VOID
ControlChannelFree(
163 * FUNCTION: Frees an address file object
165 * Object = Pointer to address file object to free
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
= exAllocatePool(NonPagedPool
, sizeof(ADDRESS_FILE
));
194 TI_DbgPrint(MIN_TRACE
, ("Insufficient resources.\n"));
195 return STATUS_INSUFFICIENT_RESOURCES
;
198 TI_DbgPrint(DEBUG_ADDRFILE
, ("Address file object allocated at (0x%X).\n", AddrFile
));
200 RtlZeroMemory(AddrFile
, sizeof(ADDRESS_FILE
));
202 AddrFile
->Free
= AddrFileFree
;
204 /* Make sure address is a local unicast address or 0 */
205 /* FIXME: IPv4 only */
206 AddrFile
->Family
= Address
->Address
[0].AddressType
;
207 AddrFile
->Address
.Address
.IPv4Address
= Address
->Address
[0].Address
[0].in_addr
;
208 AddrFile
->Address
.Type
= IP_ADDRESS_V4
;
210 if (!AddrIsUnspecified(&AddrFile
->Address
) &&
211 !AddrLocateInterface(&AddrFile
->Address
)) {
212 exFreePool(AddrFile
);
213 TI_DbgPrint(MIN_TRACE
, ("Non-local address given (0x%X).\n", A2S(&AddrFile
->Address
)));
214 return STATUS_INVALID_PARAMETER
;
217 TI_DbgPrint(MID_TRACE
, ("Opening address %s for communication (P=%d U=%d).\n",
218 A2S(&AddrFile
->Address
), Protocol
, IPPROTO_UDP
));
220 /* Protocol specific handling */
224 TCPAllocatePort(Address
->Address
[0].Address
[0].sin_port
);
226 if ((Address
->Address
[0].Address
[0].sin_port
&&
227 AddrFile
->Port
!= Address
->Address
[0].Address
[0].sin_port
) ||
228 AddrFile
->Port
== 0xffff)
230 exFreePool(AddrFile
);
231 return STATUS_INVALID_PARAMETER
;
234 AddrFile
->Send
= NULL
; /* TCPSendData */
238 TI_DbgPrint(MID_TRACE
,("Allocating udp port\n"));
240 UDPAllocatePort(Address
->Address
[0].Address
[0].sin_port
);
242 if ((Address
->Address
[0].Address
[0].sin_port
&&
243 AddrFile
->Port
!= Address
->Address
[0].Address
[0].sin_port
) ||
244 AddrFile
->Port
== 0xffff)
246 exFreePool(AddrFile
);
247 return STATUS_INVALID_PARAMETER
;
250 TI_DbgPrint(MID_TRACE
,("Setting port %d (wanted %d)\n",
252 Address
->Address
[0].Address
[0].sin_port
));
253 AddrFile
->Send
= UDPSendDatagram
;
258 AddrFile
->Send
= ICMPSendDatagram
;
262 /* Use raw IP for all other protocols */
264 AddrFile
->Send
= RawIPSendDatagram
;
268 TI_DbgPrint(MID_TRACE
, ("IP protocol number for address file object is %d.\n",
271 TI_DbgPrint(MID_TRACE
, ("Port number for address file object is %d.\n",
272 WN2H(AddrFile
->Port
)));
275 AddrFile
->Protocol
= Protocol
;
277 /* Initialize receive and transmit queues */
278 InitializeListHead(&AddrFile
->ReceiveQueue
);
279 InitializeListHead(&AddrFile
->TransmitQueue
);
281 /* Initialize spin lock that protects the address file object */
282 KeInitializeSpinLock(&AddrFile
->Lock
);
284 /* Set valid flag so the address can be used */
285 AF_SET_VALID(AddrFile
);
287 /* Return address file object */
288 Request
->Handle
.AddressHandle
= AddrFile
;
290 /* Add address file to global list */
291 ExInterlockedInsertTailList(
292 &AddressFileListHead
,
293 &AddrFile
->ListEntry
,
294 &AddressFileListLock
);
296 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
298 return STATUS_SUCCESS
;
303 * FUNCTION: Closes an address file object
305 * Request = Pointer to TDI request structure for this request
307 * Status of operation
309 NTSTATUS
FileCloseAddress(
310 PTDI_REQUEST Request
)
312 PADDRESS_FILE AddrFile
;
313 NTSTATUS Status
= STATUS_SUCCESS
;
315 PDATAGRAM_RECEIVE_REQUEST ReceiveRequest
;
316 PDATAGRAM_SEND_REQUEST SendRequest
;
317 PLIST_ENTRY CurrentEntry
, NextEntry
;
319 AddrFile
= Request
->Handle
.AddressHandle
;
321 TI_DbgPrint(MID_TRACE
, ("Called.\n"));
323 /* Remove address file from the global list */
324 TcpipAcquireSpinLock(&AddressFileListLock
, &OldIrql
);
325 RemoveEntryList(&AddrFile
->ListEntry
);
326 TcpipReleaseSpinLock(&AddressFileListLock
, OldIrql
);
328 TcpipAcquireSpinLock(&AddrFile
->Lock
, &OldIrql
);
330 /* FIXME: Kill TCP connections on this address file object */
332 /* Return pending requests with error */
334 TI_DbgPrint(DEBUG_ADDRFILE
, ("Aborting receive requests on AddrFile at (0x%X).\n", AddrFile
));
336 /* Go through pending receive request list and cancel them all */
337 CurrentEntry
= AddrFile
->ReceiveQueue
.Flink
;
338 while (CurrentEntry
!= &AddrFile
->ReceiveQueue
) {
339 NextEntry
= CurrentEntry
->Flink
;
340 ReceiveRequest
= CONTAINING_RECORD(CurrentEntry
, DATAGRAM_RECEIVE_REQUEST
, ListEntry
);
341 /* Abort the request and free its resources */
342 TcpipReleaseSpinLock(&AddrFile
->Lock
, OldIrql
);
343 (*ReceiveRequest
->Complete
)(ReceiveRequest
->Context
, STATUS_CANCELLED
, 0);
344 TcpipAcquireSpinLock(&AddrFile
->Lock
, &OldIrql
);
345 CurrentEntry
= NextEntry
;
348 TI_DbgPrint(DEBUG_ADDRFILE
, ("Aborting send requests on address file at (0x%X).\n", AddrFile
));
350 /* Go through pending send request list and cancel them all */
351 CurrentEntry
= AddrFile
->TransmitQueue
.Flink
;
352 while (CurrentEntry
!= &AddrFile
->TransmitQueue
) {
353 NextEntry
= CurrentEntry
->Flink
;
354 SendRequest
= CONTAINING_RECORD(CurrentEntry
,
355 DATAGRAM_SEND_REQUEST
, ListEntry
);
356 /* Abort the request and free its resources */
357 TcpipReleaseSpinLock(&AddrFile
->Lock
, OldIrql
);
358 (*SendRequest
->Complete
)(SendRequest
->Context
, STATUS_CANCELLED
, 0);
359 exFreePool(SendRequest
);
360 TcpipAcquireSpinLock(&AddrFile
->Lock
, &OldIrql
);
361 CurrentEntry
= NextEntry
;
364 TcpipReleaseSpinLock(&AddrFile
->Lock
, OldIrql
);
366 /* Protocol specific handling */
367 switch (AddrFile
->Protocol
) {
369 TCPFreePort( AddrFile
->Port
);
370 if( AddrFile
->Listener
) {
371 TcpipRecursiveMutexEnter(&TCPLock
, TRUE
);
372 TCPClose( AddrFile
->Listener
);
373 TcpipRecursiveMutexLeave(&TCPLock
);
374 exFreePool( AddrFile
->Listener
);
379 UDPFreePort( AddrFile
->Port
);
383 (*AddrFile
->Free
)(AddrFile
);
385 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
392 * FUNCTION: Opens a connection file object
394 * Request = Pointer to TDI request structure for this request
395 * ClientContext = Pointer to client context information
397 * Status of operation
399 NTSTATUS
FileOpenConnection(
400 PTDI_REQUEST Request
,
404 PCONNECTION_ENDPOINT Connection
;
406 TI_DbgPrint(MID_TRACE
, ("Called.\n"));
408 Connection
= TCPAllocateConnectionEndpoint( ClientContext
);
410 if( !Connection
) return STATUS_NO_MEMORY
;
412 TcpipRecursiveMutexEnter(&TCPLock
, TRUE
);
413 Status
= TCPSocket( Connection
, AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
414 TcpipRecursiveMutexLeave(&TCPLock
);
416 if( !NT_SUCCESS(Status
) ) {
417 TCPFreeConnectionEndpoint( Connection
);
421 /* Return connection endpoint file object */
422 Request
->Handle
.ConnectionContext
= Connection
;
424 /* Add connection endpoint to global list */
425 ExInterlockedInsertTailList(
426 &ConnectionEndpointListHead
,
427 &Connection
->ListEntry
,
428 &ConnectionEndpointListLock
);
430 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
432 return STATUS_SUCCESS
;
437 * FUNCTION: Find a connection by examining the context field. This
438 * is needed in some situations where a FIN reply is needed after a
439 * socket is formally broken.
441 * Request = Pointer to TDI request structure for this request
443 * Status of operation
445 PCONNECTION_ENDPOINT
FileFindConnectionByContext( PVOID Context
) {
448 PCONNECTION_ENDPOINT Connection
= NULL
;
450 TcpipAcquireSpinLock( &ConnectionEndpointListLock
, &OldIrql
);
452 for( Entry
= ConnectionEndpointListHead
.Flink
;
453 Entry
!= &ConnectionEndpointListHead
;
454 Entry
= Entry
->Flink
) {
456 CONTAINING_RECORD( Entry
, CONNECTION_ENDPOINT
, ListEntry
);
457 if( Connection
->SocketContext
== Context
) break;
458 else Connection
= NULL
;
461 TcpipReleaseSpinLock( &ConnectionEndpointListLock
, OldIrql
);
467 * FUNCTION: Closes an connection file object
469 * Request = Pointer to TDI request structure for this request
471 * Status of operation
473 NTSTATUS
FileCloseConnection(
474 PTDI_REQUEST Request
)
476 PCONNECTION_ENDPOINT Connection
;
479 TI_DbgPrint(MID_TRACE
, ("Called.\n"));
481 Connection
= Request
->Handle
.ConnectionContext
;
483 TcpipAcquireSpinLock(&ConnectionEndpointListLock
, &OldIrql
);
484 RemoveEntryList(&Connection
->ListEntry
);
485 TcpipReleaseSpinLock(&ConnectionEndpointListLock
, OldIrql
);
487 TcpipRecursiveMutexEnter( &TCPLock
, TRUE
);
488 TCPClose( Connection
);
489 TcpipRecursiveMutexLeave( &TCPLock
);
491 TCPFreeConnectionEndpoint(Connection
);
493 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
495 return STATUS_SUCCESS
;
500 * FUNCTION: Opens a control channel file object
502 * Request = Pointer to TDI request structure for this request
504 * Status of operation
506 NTSTATUS
FileOpenControlChannel(
507 PTDI_REQUEST Request
)
509 PCONTROL_CHANNEL ControlChannel
;
510 TI_DbgPrint(MID_TRACE
, ("Called.\n"));
512 ControlChannel
= exAllocatePool(NonPagedPool
, sizeof(*ControlChannel
));
514 if (!ControlChannel
) {
515 TI_DbgPrint(MIN_TRACE
, ("Insufficient resources.\n"));
516 return STATUS_INSUFFICIENT_RESOURCES
;
519 RtlZeroMemory(ControlChannel
, sizeof(CONTROL_CHANNEL
));
521 /* Make sure address is a local unicast address or 0 */
523 /* Locate address entry. If specified address is 0, a random address is chosen */
525 /* Initialize receive and transmit queues */
526 InitializeListHead(&ControlChannel
->ListEntry
);
528 /* Initialize spin lock that protects the address file object */
529 KeInitializeSpinLock(&ControlChannel
->Lock
);
531 /* Return address file object */
532 Request
->Handle
.ControlChannel
= ControlChannel
;
534 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
536 return STATUS_SUCCESS
;
540 * FUNCTION: Closes a control channel file object
542 * Request = Pointer to TDI request structure for this request
544 * Status of operation
546 NTSTATUS
FileCloseControlChannel(
547 PTDI_REQUEST Request
)
549 PCONTROL_CHANNEL ControlChannel
= Request
->Handle
.ControlChannel
;
550 NTSTATUS Status
= STATUS_SUCCESS
;
552 exFreePool(ControlChannel
);
553 Request
->Handle
.ControlChannel
= NULL
;