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
13 /* Uncomment for logging of connections and address files every 10 seconds */
16 /* List of all address file objects managed by this driver */
17 LIST_ENTRY AddressFileListHead
;
18 KSPIN_LOCK AddressFileListLock
;
20 /* List of all connection endpoint file objects managed by this driver */
21 LIST_ENTRY ConnectionEndpointListHead
;
22 KSPIN_LOCK ConnectionEndpointListLock
;
25 * FUNCTION: Searches through address file entries to find the first match
27 * Address = IP address
29 * Protocol = Protocol number
30 * SearchContext = Pointer to search context
32 * Pointer to address file, NULL if none was found
34 PADDRESS_FILE
AddrSearchFirst(
38 PAF_SEARCH SearchContext
)
42 SearchContext
->Address
= Address
;
43 SearchContext
->Port
= Port
;
44 SearchContext
->Protocol
= Protocol
;
46 TcpipAcquireSpinLock(&AddressFileListLock
, &OldIrql
);
48 SearchContext
->Next
= AddressFileListHead
.Flink
;
50 if (!IsListEmpty(&AddressFileListHead
))
51 ReferenceObject(CONTAINING_RECORD(SearchContext
->Next
, ADDRESS_FILE
, ListEntry
));
53 TcpipReleaseSpinLock(&AddressFileListLock
, OldIrql
);
55 return AddrSearchNext(SearchContext
);
58 BOOLEAN
AddrIsBroadcastMatch(
59 PIP_ADDRESS UnicastAddress
,
60 PIP_ADDRESS BroadcastAddress
) {
63 ForEachInterface(IF
) {
64 if ((AddrIsUnspecified(UnicastAddress
) ||
65 AddrIsEqual(&IF
->Unicast
, UnicastAddress
)) &&
66 (AddrIsEqual(&IF
->Broadcast
, BroadcastAddress
)))
73 BOOLEAN
AddrReceiveMatch(
74 PIP_ADDRESS LocalAddress
,
75 PIP_ADDRESS RemoteAddress
)
77 if (AddrIsEqual(LocalAddress
, RemoteAddress
))
79 /* Unicast address match */
83 if (AddrIsBroadcastMatch(LocalAddress
, RemoteAddress
))
85 /* Broadcast address match */
89 if (AddrIsUnspecified(LocalAddress
))
91 /* Local address unspecified */
95 if (AddrIsUnspecified(RemoteAddress
))
97 /* Remote address unspecified */
105 LogActiveObjects(VOID
)
108 PLIST_ENTRY CurrentEntry
;
110 PADDRESS_FILE AddrFile
;
111 PCONNECTION_ENDPOINT Conn
;
113 DbgPrint("----------- TCP/IP Active Object Dump -------------\n");
115 TcpipAcquireSpinLock(&AddressFileListLock
, &OldIrql
);
117 CurrentEntry
= AddressFileListHead
.Flink
;
118 while (CurrentEntry
!= &AddressFileListHead
)
120 AddrFile
= CONTAINING_RECORD(CurrentEntry
, ADDRESS_FILE
, ListEntry
);
122 DbgPrint("Address File (%s, %d, %d) @ 0x%p | Ref count: %d | Sharers: %d\n",
123 A2S(&AddrFile
->Address
), WN2H(AddrFile
->Port
), AddrFile
->Protocol
,
124 AddrFile
, AddrFile
->RefCount
, AddrFile
->Sharers
);
125 DbgPrint("\tListener: ");
126 if (AddrFile
->Listener
== NULL
)
127 DbgPrint("<None>\n");
129 DbgPrint("0x%p\n", AddrFile
->Listener
);
130 DbgPrint("\tAssociated endpoints: ");
131 if (AddrFile
->Connection
== NULL
)
132 DbgPrint("<None>\n");
135 Conn
= AddrFile
->Connection
;
138 DbgPrint("0x%p ", Conn
);
144 CurrentEntry
= CurrentEntry
->Flink
;
147 TcpipReleaseSpinLock(&AddressFileListLock
, OldIrql
);
149 TcpipAcquireSpinLock(&ConnectionEndpointListLock
, &OldIrql
);
151 CurrentEntry
= ConnectionEndpointListHead
.Flink
;
152 while (CurrentEntry
!= &ConnectionEndpointListHead
)
154 Conn
= CONTAINING_RECORD(CurrentEntry
, CONNECTION_ENDPOINT
, ListEntry
);
156 DbgPrint("Connection @ 0x%p | Ref count: %d\n", Conn
, Conn
->RefCount
);
158 if (Conn
->SocketContext
== NULL
)
159 DbgPrint("<None>\n");
162 DbgPrint("0x%p\n", Conn
->SocketContext
);
163 LibTCPDumpPcb(Conn
->SocketContext
);
165 DbgPrint("\tPacket queue status: %s\n", IsListEmpty(&Conn
->PacketQueue
) ? "Empty" : "Not Empty");
166 DbgPrint("\tRequest lists: Connect: %s | Recv: %s | Send: %s | Shutdown: %s | Listen: %s\n",
167 IsListEmpty(&Conn
->ConnectRequest
) ? "Empty" : "Not Empty",
168 IsListEmpty(&Conn
->ReceiveRequest
) ? "Empty" : "Not Empty",
169 IsListEmpty(&Conn
->SendRequest
) ? "Empty" : "Not Empty",
170 IsListEmpty(&Conn
->ShutdownRequest
) ? "Empty" : "Not Empty",
171 IsListEmpty(&Conn
->ListenRequest
) ? "Empty" : "Not Empty");
172 DbgPrint("\tSend shutdown: %s\n", Conn
->SendShutdown
? "Yes" : "No");
173 DbgPrint("\tReceive shutdown: %s\n", Conn
->ReceiveShutdown
? "Yes" : "No");
174 if (Conn
->ReceiveShutdown
) DbgPrint("\tReceive shutdown status: 0x%x\n", Conn
->ReceiveShutdownStatus
);
175 DbgPrint("\tClosing: %s\n", Conn
->Closing
? "Yes" : "No");
177 CurrentEntry
= CurrentEntry
->Flink
;
180 TcpipReleaseSpinLock(&ConnectionEndpointListLock
, OldIrql
);
182 DbgPrint("---------------------------------------------------\n");
186 PADDRESS_FILE
AddrFindShared(
187 PIP_ADDRESS BindAddress
,
191 PLIST_ENTRY CurrentEntry
;
193 PADDRESS_FILE Current
= NULL
;
195 TcpipAcquireSpinLock(&AddressFileListLock
, &OldIrql
);
197 CurrentEntry
= AddressFileListHead
.Flink
;
198 while (CurrentEntry
!= &AddressFileListHead
) {
199 Current
= CONTAINING_RECORD(CurrentEntry
, ADDRESS_FILE
, ListEntry
);
201 /* See if this address matches the search criteria */
202 if ((Current
->Port
== Port
) &&
203 (Current
->Protocol
== Protocol
))
205 /* Increase the sharer count */
206 ASSERT(Current
->Sharers
!= 0);
207 InterlockedIncrement(&Current
->Sharers
);
211 CurrentEntry
= CurrentEntry
->Flink
;
215 TcpipReleaseSpinLock(&AddressFileListLock
, OldIrql
);
221 * FUNCTION: Searches through address file entries to find next match
223 * SearchContext = Pointer to search context
225 * Pointer to referenced address file, NULL if none was found
227 PADDRESS_FILE
AddrSearchNext(
228 PAF_SEARCH SearchContext
)
230 PLIST_ENTRY CurrentEntry
;
231 PIP_ADDRESS IPAddress
;
233 PADDRESS_FILE Current
= NULL
;
234 BOOLEAN Found
= FALSE
;
235 PADDRESS_FILE StartingAddrFile
;
237 TcpipAcquireSpinLock(&AddressFileListLock
, &OldIrql
);
239 if (SearchContext
->Next
== &AddressFileListHead
)
241 TcpipReleaseSpinLock(&AddressFileListLock
, OldIrql
);
245 /* Save this pointer so we can dereference it later */
246 StartingAddrFile
= CONTAINING_RECORD(SearchContext
->Next
, ADDRESS_FILE
, ListEntry
);
248 CurrentEntry
= SearchContext
->Next
;
250 while (CurrentEntry
!= &AddressFileListHead
) {
251 Current
= CONTAINING_RECORD(CurrentEntry
, ADDRESS_FILE
, ListEntry
);
253 IPAddress
= &Current
->Address
;
255 TI_DbgPrint(DEBUG_ADDRFILE
, ("Comparing: ((%d, %d, %s), (%d, %d, %s)).\n",
259 WN2H(SearchContext
->Port
),
260 SearchContext
->Protocol
,
261 A2S(SearchContext
->Address
)));
263 /* See if this address matches the search criteria */
264 if ((Current
->Port
== SearchContext
->Port
) &&
265 (Current
->Protocol
== SearchContext
->Protocol
) &&
266 (AddrReceiveMatch(IPAddress
, SearchContext
->Address
))) {
267 /* We've found a match */
271 CurrentEntry
= CurrentEntry
->Flink
;
276 SearchContext
->Next
= CurrentEntry
->Flink
;
278 if (SearchContext
->Next
!= &AddressFileListHead
)
280 /* Reference the next address file to prevent the link from disappearing behind our back */
281 ReferenceObject(CONTAINING_RECORD(SearchContext
->Next
, ADDRESS_FILE
, ListEntry
));
284 /* Reference the returned address file before dereferencing the starting
285 * address file because it may be that Current == StartingAddrFile */
286 ReferenceObject(Current
);
291 DereferenceObject(StartingAddrFile
);
293 TcpipReleaseSpinLock(&AddressFileListLock
, OldIrql
);
301 * FUNCTION: Frees an address file object
303 * Object = Pointer to address file object to free
306 PADDRESS_FILE AddrFile
= Object
;
308 PDATAGRAM_RECEIVE_REQUEST ReceiveRequest
;
309 PDATAGRAM_SEND_REQUEST SendRequest
;
310 PLIST_ENTRY CurrentEntry
;
312 TI_DbgPrint(MID_TRACE
, ("Called.\n"));
314 /* We should not be associated with a connection here */
315 ASSERT(!AddrFile
->Connection
);
317 /* Remove address file from the global list */
318 TcpipAcquireSpinLock(&AddressFileListLock
, &OldIrql
);
319 RemoveEntryList(&AddrFile
->ListEntry
);
320 TcpipReleaseSpinLock(&AddressFileListLock
, OldIrql
);
322 /* FIXME: Kill TCP connections on this address file object */
324 /* Return pending requests with error */
326 TI_DbgPrint(DEBUG_ADDRFILE
, ("Aborting receive requests on AddrFile at (0x%X).\n", AddrFile
));
328 /* Go through pending receive request list and cancel them all */
329 while ((CurrentEntry
= ExInterlockedRemoveHeadList(&AddrFile
->ReceiveQueue
, &AddrFile
->Lock
))) {
330 ReceiveRequest
= CONTAINING_RECORD(CurrentEntry
, DATAGRAM_RECEIVE_REQUEST
, ListEntry
);
331 (*ReceiveRequest
->Complete
)(ReceiveRequest
->Context
, STATUS_CANCELLED
, 0);
332 /* ExFreePoolWithTag(ReceiveRequest, DATAGRAM_RECV_TAG); FIXME: WTF? */
335 TI_DbgPrint(DEBUG_ADDRFILE
, ("Aborting send requests on address file at (0x%X).\n", AddrFile
));
337 /* Go through pending send request list and cancel them all */
338 while ((CurrentEntry
= ExInterlockedRemoveHeadList(&AddrFile
->ReceiveQueue
, &AddrFile
->Lock
))) {
339 SendRequest
= CONTAINING_RECORD(CurrentEntry
, DATAGRAM_SEND_REQUEST
, ListEntry
);
340 (*SendRequest
->Complete
)(SendRequest
->Context
, STATUS_CANCELLED
, 0);
341 ExFreePoolWithTag(SendRequest
, DATAGRAM_SEND_TAG
);
344 /* Protocol specific handling */
345 switch (AddrFile
->Protocol
) {
349 TCPFreePort(AddrFile
->Port
);
354 UDPFreePort( AddrFile
->Port
);
358 RemoveEntityByContext(AddrFile
);
360 ExFreePoolWithTag(Object
, ADDR_FILE_TAG
);
364 VOID
ControlChannelFree(
367 * FUNCTION: Frees an address file object
369 * Object = Pointer to address file object to free
372 ExFreePoolWithTag(Object
, CONTROL_CHANNEL_TAG
);
377 * FUNCTION: Open an address file object
379 * Request = Pointer to TDI request structure for this request
380 * Address = Pointer to address to be opened
381 * Protocol = Protocol on which to open the address
382 * Shared = Specifies if the address is opened for shared access
383 * Options = Pointer to option buffer
385 * Status of operation
387 NTSTATUS
FileOpenAddress(
388 PTDI_REQUEST Request
,
389 PTA_IP_ADDRESS Address
,
394 PADDRESS_FILE AddrFile
;
396 TI_DbgPrint(MID_TRACE
, ("Called (Proto %d).\n", Protocol
));
398 /* If it's shared and has a port specified, look for a match */
399 if ((Shared
!= FALSE
) && (Address
->Address
[0].Address
[0].sin_port
!= 0))
401 AddrFile
= AddrFindShared(NULL
, Address
->Address
[0].Address
[0].sin_port
, Protocol
);
402 if (AddrFile
!= NULL
)
404 Request
->Handle
.AddressHandle
= AddrFile
;
405 return STATUS_SUCCESS
;
409 AddrFile
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(ADDRESS_FILE
),
412 TI_DbgPrint(MIN_TRACE
, ("Insufficient resources.\n"));
413 return STATUS_INSUFFICIENT_RESOURCES
;
416 RtlZeroMemory(AddrFile
, sizeof(ADDRESS_FILE
));
418 AddrFile
->RefCount
= 1;
419 AddrFile
->Free
= AddrFileFree
;
420 AddrFile
->Sharers
= 1;
422 /* Set our default options */
426 AddrFile
->HeaderIncl
= 1;
428 /* Make sure address is a local unicast address or 0 */
429 /* FIXME: IPv4 only */
430 AddrFile
->Family
= Address
->Address
[0].AddressType
;
431 AddrFile
->Address
.Address
.IPv4Address
= Address
->Address
[0].Address
[0].in_addr
;
432 AddrFile
->Address
.Type
= IP_ADDRESS_V4
;
434 if (!AddrIsUnspecified(&AddrFile
->Address
) &&
435 !AddrLocateInterface(&AddrFile
->Address
)) {
436 TI_DbgPrint(MIN_TRACE
, ("Non-local address given (0x%X).\n", A2S(&AddrFile
->Address
)));
437 ExFreePoolWithTag(AddrFile
, ADDR_FILE_TAG
);
438 return STATUS_INVALID_ADDRESS
;
441 TI_DbgPrint(MID_TRACE
, ("Opening address %s for communication (P=%d U=%d).\n",
442 A2S(&AddrFile
->Address
), Protocol
, IPPROTO_UDP
));
444 /* Protocol specific handling */
447 if (Address
->Address
[0].Address
[0].sin_port
)
449 /* The client specified an explicit port so we force a bind to this */
450 AddrFile
->Port
= TCPAllocatePort(Address
->Address
[0].Address
[0].sin_port
);
452 /* Check for bind success */
453 if (AddrFile
->Port
== 0xffff)
455 ExFreePoolWithTag(AddrFile
, ADDR_FILE_TAG
);
456 return STATUS_ADDRESS_ALREADY_EXISTS
;
460 ASSERT(Address
->Address
[0].Address
[0].sin_port
== AddrFile
->Port
);
462 else if (!AddrIsUnspecified(&AddrFile
->Address
))
464 /* The client is trying to bind to a local address so allocate a port now too */
465 AddrFile
->Port
= TCPAllocatePort(0);
467 /* Check for bind success */
468 if (AddrFile
->Port
== 0xffff)
470 ExFreePoolWithTag(AddrFile
, ADDR_FILE_TAG
);
471 return STATUS_ADDRESS_ALREADY_EXISTS
;
476 /* The client wants an unspecified port with an unspecified address so we wait to see what the TCP library gives us */
480 AddEntity(CO_TL_ENTITY
, AddrFile
, CO_TL_TCP
);
482 AddrFile
->Send
= NULL
; /* TCPSendData */
486 TI_DbgPrint(MID_TRACE
,("Allocating udp port\n"));
488 UDPAllocatePort(Address
->Address
[0].Address
[0].sin_port
);
490 if ((Address
->Address
[0].Address
[0].sin_port
&&
491 AddrFile
->Port
!= Address
->Address
[0].Address
[0].sin_port
) ||
492 AddrFile
->Port
== 0xffff)
494 ExFreePoolWithTag(AddrFile
, ADDR_FILE_TAG
);
495 return STATUS_ADDRESS_ALREADY_EXISTS
;
498 TI_DbgPrint(MID_TRACE
,("Setting port %d (wanted %d)\n",
500 Address
->Address
[0].Address
[0].sin_port
));
502 AddEntity(CL_TL_ENTITY
, AddrFile
, CL_TL_UDP
);
504 AddrFile
->Send
= UDPSendDatagram
;
509 AddrFile
->Send
= ICMPSendDatagram
;
511 /* FIXME: Verify this */
512 AddEntity(ER_ENTITY
, AddrFile
, ER_ICMP
);
516 /* Use raw IP for all other protocols */
518 AddrFile
->Send
= RawIPSendDatagram
;
520 /* FIXME: Verify this */
521 AddEntity(CL_TL_ENTITY
, AddrFile
, 0);
525 TI_DbgPrint(MID_TRACE
, ("IP protocol number for address file object is %d.\n",
528 TI_DbgPrint(MID_TRACE
, ("Port number for address file object is %d.\n",
529 WN2H(AddrFile
->Port
)));
532 AddrFile
->Protocol
= Protocol
;
534 /* Initialize receive and transmit queues */
535 InitializeListHead(&AddrFile
->ReceiveQueue
);
536 InitializeListHead(&AddrFile
->TransmitQueue
);
538 /* Initialize spin lock that protects the address file object */
539 KeInitializeSpinLock(&AddrFile
->Lock
);
541 /* Return address file object */
542 Request
->Handle
.AddressHandle
= AddrFile
;
544 /* Add address file to global list */
545 ExInterlockedInsertTailList(
546 &AddressFileListHead
,
547 &AddrFile
->ListEntry
,
548 &AddressFileListLock
);
550 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
552 return STATUS_SUCCESS
;
557 * FUNCTION: Closes an address file object
559 * Request = Pointer to TDI request structure for this request
561 * Status of operation
563 NTSTATUS
FileCloseAddress(
564 PTDI_REQUEST Request
)
566 PADDRESS_FILE AddrFile
= Request
->Handle
.AddressHandle
;
569 if (!Request
->Handle
.AddressHandle
) return STATUS_INVALID_PARAMETER
;
571 LockObject(AddrFile
, &OldIrql
);
573 if (InterlockedDecrement(&AddrFile
->Sharers
) != 0)
575 /* Still other guys have open handles to this, so keep it around */
576 UnlockObject(AddrFile
, OldIrql
);
577 return STATUS_SUCCESS
;
580 /* We have to close this listener because we started it */
581 if( AddrFile
->Listener
)
583 TCPClose( AddrFile
->Listener
);
586 UnlockObject(AddrFile
, OldIrql
);
588 DereferenceObject(AddrFile
);
590 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
592 return STATUS_SUCCESS
;
597 * FUNCTION: Opens a connection file object
599 * Request = Pointer to TDI request structure for this request
600 * ClientContext = Pointer to client context information
602 * Status of operation
604 NTSTATUS
FileOpenConnection(
605 PTDI_REQUEST Request
,
609 PCONNECTION_ENDPOINT Connection
;
611 TI_DbgPrint(MID_TRACE
, ("Called.\n"));
613 Connection
= TCPAllocateConnectionEndpoint( ClientContext
);
615 if( !Connection
) return STATUS_NO_MEMORY
;
617 Status
= TCPSocket( Connection
, AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
619 if( !NT_SUCCESS(Status
) ) {
620 DereferenceObject( Connection
);
624 /* Return connection endpoint file object */
625 Request
->Handle
.ConnectionContext
= Connection
;
627 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
629 return STATUS_SUCCESS
;
633 * FUNCTION: Closes an connection file object
635 * Request = Pointer to TDI request structure for this request
637 * Status of operation
639 NTSTATUS
FileCloseConnection(
640 PTDI_REQUEST Request
)
642 PCONNECTION_ENDPOINT Connection
;
644 TI_DbgPrint(MID_TRACE
, ("Called.\n"));
646 Connection
= Request
->Handle
.ConnectionContext
;
648 if (!Connection
) return STATUS_INVALID_PARAMETER
;
650 TCPClose( Connection
);
652 Request
->Handle
.ConnectionContext
= NULL
;
654 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
656 return STATUS_SUCCESS
;
660 * FUNCTION: Opens a control channel file object
662 * Request = Pointer to TDI request structure for this request
664 * Status of operation
666 NTSTATUS
FileOpenControlChannel(
667 PTDI_REQUEST Request
)
669 PCONTROL_CHANNEL ControlChannel
;
670 TI_DbgPrint(MID_TRACE
, ("Called.\n"));
672 ControlChannel
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(*ControlChannel
),
673 CONTROL_CHANNEL_TAG
);
675 if (!ControlChannel
) {
676 TI_DbgPrint(MIN_TRACE
, ("Insufficient resources.\n"));
677 return STATUS_INSUFFICIENT_RESOURCES
;
680 RtlZeroMemory(ControlChannel
, sizeof(CONTROL_CHANNEL
));
682 /* Make sure address is a local unicast address or 0 */
684 /* Locate address entry. If specified address is 0, a random address is chosen */
686 /* Initialize receive and transmit queues */
687 InitializeListHead(&ControlChannel
->ListEntry
);
689 /* Initialize spin lock that protects the address file object */
690 KeInitializeSpinLock(&ControlChannel
->Lock
);
692 ControlChannel
->RefCount
= 1;
693 ControlChannel
->Free
= ControlChannelFree
;
695 /* Return address file object */
696 Request
->Handle
.ControlChannel
= ControlChannel
;
698 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
700 return STATUS_SUCCESS
;
704 * FUNCTION: Closes a control channel file object
706 * Request = Pointer to TDI request structure for this request
708 * Status of operation
710 NTSTATUS
FileCloseControlChannel(
711 PTDI_REQUEST Request
)
713 if (!Request
->Handle
.ControlChannel
) return STATUS_INVALID_PARAMETER
;
715 DereferenceObject((PCONTROL_CHANNEL
)Request
->Handle
.ControlChannel
);
717 Request
->Handle
.ControlChannel
= NULL
;
719 return STATUS_SUCCESS
;