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 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
;
236 TcpipAcquireSpinLock(&AddressFileListLock
, &OldIrql
);
238 if (SearchContext
->Next
== &AddressFileListHead
)
240 TcpipReleaseSpinLock(&AddressFileListLock
, OldIrql
);
244 /* Remove the extra reference we added to keep this address file in memory */
245 DereferenceObject(CONTAINING_RECORD(SearchContext
->Next
, ADDRESS_FILE
, ListEntry
));
247 CurrentEntry
= SearchContext
->Next
;
249 while (CurrentEntry
!= &AddressFileListHead
) {
250 Current
= CONTAINING_RECORD(CurrentEntry
, ADDRESS_FILE
, ListEntry
);
252 IPAddress
= &Current
->Address
;
254 TI_DbgPrint(DEBUG_ADDRFILE
, ("Comparing: ((%d, %d, %s), (%d, %d, %s)).\n",
258 WN2H(SearchContext
->Port
),
259 SearchContext
->Protocol
,
260 A2S(SearchContext
->Address
)));
262 /* See if this address matches the search criteria */
263 if ((Current
->Port
== SearchContext
->Port
) &&
264 (Current
->Protocol
== SearchContext
->Protocol
) &&
265 (AddrReceiveMatch(IPAddress
, SearchContext
->Address
))) {
266 /* We've found a match */
270 CurrentEntry
= CurrentEntry
->Flink
;
275 SearchContext
->Next
= CurrentEntry
->Flink
;
277 if (SearchContext
->Next
!= &AddressFileListHead
)
279 /* Reference the next address file to prevent the link from disappearing behind our back */
280 ReferenceObject(CONTAINING_RECORD(SearchContext
->Next
, ADDRESS_FILE
, ListEntry
));
286 TcpipReleaseSpinLock(&AddressFileListLock
, OldIrql
);
294 * FUNCTION: Frees an address file object
296 * Object = Pointer to address file object to free
299 PADDRESS_FILE AddrFile
= Object
;
301 PDATAGRAM_RECEIVE_REQUEST ReceiveRequest
;
302 PDATAGRAM_SEND_REQUEST SendRequest
;
303 PLIST_ENTRY CurrentEntry
;
305 TI_DbgPrint(MID_TRACE
, ("Called.\n"));
307 /* We should not be associated with a connection here */
308 ASSERT(!AddrFile
->Connection
);
310 /* Remove address file from the global list */
311 TcpipAcquireSpinLock(&AddressFileListLock
, &OldIrql
);
312 RemoveEntryList(&AddrFile
->ListEntry
);
313 TcpipReleaseSpinLock(&AddressFileListLock
, OldIrql
);
315 /* FIXME: Kill TCP connections on this address file object */
317 /* Return pending requests with error */
319 TI_DbgPrint(DEBUG_ADDRFILE
, ("Aborting receive requests on AddrFile at (0x%X).\n", AddrFile
));
321 /* Go through pending receive request list and cancel them all */
322 while ((CurrentEntry
= ExInterlockedRemoveHeadList(&AddrFile
->ReceiveQueue
, &AddrFile
->Lock
))) {
323 ReceiveRequest
= CONTAINING_RECORD(CurrentEntry
, DATAGRAM_RECEIVE_REQUEST
, ListEntry
);
324 (*ReceiveRequest
->Complete
)(ReceiveRequest
->Context
, STATUS_CANCELLED
, 0);
325 /* ExFreePoolWithTag(ReceiveRequest, DATAGRAM_RECV_TAG); FIXME: WTF? */
328 TI_DbgPrint(DEBUG_ADDRFILE
, ("Aborting send requests on address file at (0x%X).\n", AddrFile
));
330 /* Go through pending send request list and cancel them all */
331 while ((CurrentEntry
= ExInterlockedRemoveHeadList(&AddrFile
->ReceiveQueue
, &AddrFile
->Lock
))) {
332 SendRequest
= CONTAINING_RECORD(CurrentEntry
, DATAGRAM_SEND_REQUEST
, ListEntry
);
333 (*SendRequest
->Complete
)(SendRequest
->Context
, STATUS_CANCELLED
, 0);
334 ExFreePoolWithTag(SendRequest
, DATAGRAM_SEND_TAG
);
337 /* Protocol specific handling */
338 switch (AddrFile
->Protocol
) {
342 TCPFreePort(AddrFile
->Port
);
347 UDPFreePort( AddrFile
->Port
);
351 RemoveEntityByContext(AddrFile
);
353 ExFreePoolWithTag(Object
, ADDR_FILE_TAG
);
357 VOID
ControlChannelFree(
360 * FUNCTION: Frees an address file object
362 * Object = Pointer to address file object to free
365 ExFreePoolWithTag(Object
, CONTROL_CHANNEL_TAG
);
370 * FUNCTION: Open an address file object
372 * Request = Pointer to TDI request structure for this request
373 * Address = Pointer to address to be opened
374 * Protocol = Protocol on which to open the address
375 * Shared = Specifies if the address is opened for shared access
376 * Options = Pointer to option buffer
378 * Status of operation
380 NTSTATUS
FileOpenAddress(
381 PTDI_REQUEST Request
,
382 PTA_IP_ADDRESS Address
,
387 PADDRESS_FILE AddrFile
;
389 TI_DbgPrint(MID_TRACE
, ("Called (Proto %d).\n", Protocol
));
391 /* If it's shared and has a port specified, look for a match */
392 if ((Shared
!= FALSE
) && (Address
->Address
[0].Address
[0].sin_port
!= 0))
394 AddrFile
= AddrFindShared(NULL
, Address
->Address
[0].Address
[0].sin_port
, Protocol
);
395 if (AddrFile
!= NULL
)
397 Request
->Handle
.AddressHandle
= AddrFile
;
398 return STATUS_SUCCESS
;
402 AddrFile
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(ADDRESS_FILE
),
405 TI_DbgPrint(MIN_TRACE
, ("Insufficient resources.\n"));
406 return STATUS_INSUFFICIENT_RESOURCES
;
409 RtlZeroMemory(AddrFile
, sizeof(ADDRESS_FILE
));
411 AddrFile
->RefCount
= 1;
412 AddrFile
->Free
= AddrFileFree
;
413 AddrFile
->Sharers
= 1;
415 /* Set our default options */
419 AddrFile
->HeaderIncl
= 1;
421 /* Make sure address is a local unicast address or 0 */
422 /* FIXME: IPv4 only */
423 AddrFile
->Family
= Address
->Address
[0].AddressType
;
424 AddrFile
->Address
.Address
.IPv4Address
= Address
->Address
[0].Address
[0].in_addr
;
425 AddrFile
->Address
.Type
= IP_ADDRESS_V4
;
427 if (!AddrIsUnspecified(&AddrFile
->Address
) &&
428 !AddrLocateInterface(&AddrFile
->Address
)) {
429 TI_DbgPrint(MIN_TRACE
, ("Non-local address given (0x%X).\n", A2S(&AddrFile
->Address
)));
430 ExFreePoolWithTag(AddrFile
, ADDR_FILE_TAG
);
431 return STATUS_INVALID_ADDRESS
;
434 TI_DbgPrint(MID_TRACE
, ("Opening address %s for communication (P=%d U=%d).\n",
435 A2S(&AddrFile
->Address
), Protocol
, IPPROTO_UDP
));
437 /* Protocol specific handling */
440 if (Address
->Address
[0].Address
[0].sin_port
)
442 /* The client specified an explicit port so we force a bind to this */
443 AddrFile
->Port
= TCPAllocatePort(Address
->Address
[0].Address
[0].sin_port
);
445 /* Check for bind success */
446 if (AddrFile
->Port
== 0xffff)
448 ExFreePoolWithTag(AddrFile
, ADDR_FILE_TAG
);
449 return STATUS_ADDRESS_ALREADY_EXISTS
;
453 ASSERT(Address
->Address
[0].Address
[0].sin_port
== AddrFile
->Port
);
455 else if (!AddrIsUnspecified(&AddrFile
->Address
))
457 /* The client is trying to bind to a local address so allocate a port now too */
458 AddrFile
->Port
= TCPAllocatePort(0);
460 /* Check for bind success */
461 if (AddrFile
->Port
== 0xffff)
463 ExFreePoolWithTag(AddrFile
, ADDR_FILE_TAG
);
464 return STATUS_ADDRESS_ALREADY_EXISTS
;
469 /* The client wants an unspecified port with an unspecified address so we wait to see what the TCP library gives us */
473 AddEntity(CO_TL_ENTITY
, AddrFile
, CO_TL_TCP
);
475 AddrFile
->Send
= NULL
; /* TCPSendData */
479 TI_DbgPrint(MID_TRACE
,("Allocating udp port\n"));
481 UDPAllocatePort(Address
->Address
[0].Address
[0].sin_port
);
483 if ((Address
->Address
[0].Address
[0].sin_port
&&
484 AddrFile
->Port
!= Address
->Address
[0].Address
[0].sin_port
) ||
485 AddrFile
->Port
== 0xffff)
487 ExFreePoolWithTag(AddrFile
, ADDR_FILE_TAG
);
488 return STATUS_ADDRESS_ALREADY_EXISTS
;
491 TI_DbgPrint(MID_TRACE
,("Setting port %d (wanted %d)\n",
493 Address
->Address
[0].Address
[0].sin_port
));
495 AddEntity(CL_TL_ENTITY
, AddrFile
, CL_TL_UDP
);
497 AddrFile
->Send
= UDPSendDatagram
;
502 AddrFile
->Send
= ICMPSendDatagram
;
504 /* FIXME: Verify this */
505 AddEntity(ER_ENTITY
, AddrFile
, ER_ICMP
);
509 /* Use raw IP for all other protocols */
511 AddrFile
->Send
= RawIPSendDatagram
;
513 /* FIXME: Verify this */
514 AddEntity(CL_TL_ENTITY
, AddrFile
, 0);
518 TI_DbgPrint(MID_TRACE
, ("IP protocol number for address file object is %d.\n",
521 TI_DbgPrint(MID_TRACE
, ("Port number for address file object is %d.\n",
522 WN2H(AddrFile
->Port
)));
525 AddrFile
->Protocol
= Protocol
;
527 /* Initialize receive and transmit queues */
528 InitializeListHead(&AddrFile
->ReceiveQueue
);
529 InitializeListHead(&AddrFile
->TransmitQueue
);
531 /* Initialize spin lock that protects the address file object */
532 KeInitializeSpinLock(&AddrFile
->Lock
);
534 /* Return address file object */
535 Request
->Handle
.AddressHandle
= AddrFile
;
537 /* Add address file to global list */
538 ExInterlockedInsertTailList(
539 &AddressFileListHead
,
540 &AddrFile
->ListEntry
,
541 &AddressFileListLock
);
543 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
545 return STATUS_SUCCESS
;
550 * FUNCTION: Closes an address file object
552 * Request = Pointer to TDI request structure for this request
554 * Status of operation
556 NTSTATUS
FileCloseAddress(
557 PTDI_REQUEST Request
)
559 PADDRESS_FILE AddrFile
= Request
->Handle
.AddressHandle
;
562 if (!Request
->Handle
.AddressHandle
) return STATUS_INVALID_PARAMETER
;
564 LockObject(AddrFile
, &OldIrql
);
566 if (InterlockedDecrement(&AddrFile
->Sharers
) != 0)
568 /* Still other guys have open handles to this, so keep it around */
569 UnlockObject(AddrFile
, OldIrql
);
570 return STATUS_SUCCESS
;
573 /* We have to close this listener because we started it */
574 if( AddrFile
->Listener
)
576 TCPClose( AddrFile
->Listener
);
579 UnlockObject(AddrFile
, OldIrql
);
581 DereferenceObject(AddrFile
);
583 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
585 return STATUS_SUCCESS
;
590 * FUNCTION: Opens a connection file object
592 * Request = Pointer to TDI request structure for this request
593 * ClientContext = Pointer to client context information
595 * Status of operation
597 NTSTATUS
FileOpenConnection(
598 PTDI_REQUEST Request
,
602 PCONNECTION_ENDPOINT Connection
;
604 TI_DbgPrint(MID_TRACE
, ("Called.\n"));
606 Connection
= TCPAllocateConnectionEndpoint( ClientContext
);
608 if( !Connection
) return STATUS_NO_MEMORY
;
610 Status
= TCPSocket( Connection
, AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
612 if( !NT_SUCCESS(Status
) ) {
613 DereferenceObject( Connection
);
617 /* Return connection endpoint file object */
618 Request
->Handle
.ConnectionContext
= Connection
;
620 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
622 return STATUS_SUCCESS
;
626 * FUNCTION: Closes an connection file object
628 * Request = Pointer to TDI request structure for this request
630 * Status of operation
632 NTSTATUS
FileCloseConnection(
633 PTDI_REQUEST Request
)
635 PCONNECTION_ENDPOINT Connection
;
637 TI_DbgPrint(MID_TRACE
, ("Called.\n"));
639 Connection
= Request
->Handle
.ConnectionContext
;
641 if (!Connection
) return STATUS_INVALID_PARAMETER
;
643 TCPClose( Connection
);
645 Request
->Handle
.ConnectionContext
= NULL
;
647 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
649 return STATUS_SUCCESS
;
653 * FUNCTION: Opens a control channel file object
655 * Request = Pointer to TDI request structure for this request
657 * Status of operation
659 NTSTATUS
FileOpenControlChannel(
660 PTDI_REQUEST Request
)
662 PCONTROL_CHANNEL ControlChannel
;
663 TI_DbgPrint(MID_TRACE
, ("Called.\n"));
665 ControlChannel
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(*ControlChannel
),
666 CONTROL_CHANNEL_TAG
);
668 if (!ControlChannel
) {
669 TI_DbgPrint(MIN_TRACE
, ("Insufficient resources.\n"));
670 return STATUS_INSUFFICIENT_RESOURCES
;
673 RtlZeroMemory(ControlChannel
, sizeof(CONTROL_CHANNEL
));
675 /* Make sure address is a local unicast address or 0 */
677 /* Locate address entry. If specified address is 0, a random address is chosen */
679 /* Initialize receive and transmit queues */
680 InitializeListHead(&ControlChannel
->ListEntry
);
682 /* Initialize spin lock that protects the address file object */
683 KeInitializeSpinLock(&ControlChannel
->Lock
);
685 ControlChannel
->RefCount
= 1;
686 ControlChannel
->Free
= ControlChannelFree
;
688 /* Return address file object */
689 Request
->Handle
.ControlChannel
= ControlChannel
;
691 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
693 return STATUS_SUCCESS
;
697 * FUNCTION: Closes a control channel file object
699 * Request = Pointer to TDI request structure for this request
701 * Status of operation
703 NTSTATUS
FileCloseControlChannel(
704 PTDI_REQUEST Request
)
706 if (!Request
->Handle
.ControlChannel
) return STATUS_INVALID_PARAMETER
;
708 DereferenceObject((PCONTROL_CHANNEL
)Request
->Handle
.ControlChannel
);
710 Request
->Handle
.ControlChannel
= NULL
;
712 return STATUS_SUCCESS
;