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
AddrIsBroadcast(
47 PIP_ADDRESS PossibleMatch
,
48 PIP_ADDRESS TargetAddress
) {
51 ForEachInterface(IF
) {
52 if( AddrIsEqual( &IF
->Unicast
, PossibleMatch
) &&
53 AddrIsEqual( &IF
->Broadcast
, TargetAddress
) )
61 * FUNCTION: Searches through address file entries to find next match
63 * SearchContext = Pointer to search context
65 * Pointer to address file, NULL if none was found
67 PADDRESS_FILE
AddrSearchNext(
68 PAF_SEARCH SearchContext
)
70 PLIST_ENTRY CurrentEntry
;
71 PIP_ADDRESS IPAddress
;
73 PADDRESS_FILE Current
= NULL
;
74 BOOLEAN Found
= FALSE
;
76 if (IsListEmpty(SearchContext
->Next
))
79 CurrentEntry
= SearchContext
->Next
;
81 TcpipAcquireSpinLock(&AddressFileListLock
, &OldIrql
);
83 while (CurrentEntry
!= &AddressFileListHead
) {
84 Current
= CONTAINING_RECORD(CurrentEntry
, ADDRESS_FILE
, ListEntry
);
86 IPAddress
= &Current
->Address
;
88 TI_DbgPrint(DEBUG_ADDRFILE
, ("Comparing: ((%d, %d, %s), (%d, %d, %s)).\n",
92 WN2H(SearchContext
->Port
),
93 SearchContext
->Protocol
,
94 A2S(SearchContext
->Address
)));
96 /* See if this address matches the search criteria */
97 if ((Current
->Port
== SearchContext
->Port
) &&
98 (Current
->Protocol
== SearchContext
->Protocol
) &&
99 (AddrIsEqual(IPAddress
, SearchContext
->Address
) ||
100 AddrIsBroadcast(IPAddress
, SearchContext
->Address
) ||
101 AddrIsUnspecified(IPAddress
))) {
102 /* We've found a match */
106 CurrentEntry
= CurrentEntry
->Flink
;
109 TcpipReleaseSpinLock(&AddressFileListLock
, OldIrql
);
112 SearchContext
->Next
= CurrentEntry
->Flink
;
121 * FUNCTION: Frees an address file object
123 * Object = Pointer to address file object to free
130 VOID
ControlChannelFree(
133 * FUNCTION: Frees an address file object
135 * Object = Pointer to address file object to free
142 VOID
DeleteAddress(PADDRESS_FILE AddrFile
)
144 * FUNCTION: Deletes an address file object
146 * AddrFile = Pointer to address file object to delete
150 PLIST_ENTRY CurrentEntry
;
151 PLIST_ENTRY NextEntry
;
152 PDATAGRAM_SEND_REQUEST SendRequest
;
153 PDATAGRAM_RECEIVE_REQUEST ReceiveRequest
;
155 TI_DbgPrint(MID_TRACE
, ("Called.\n"));
157 /* Remove address file from the global list */
158 TcpipAcquireSpinLock(&AddressFileListLock
, &OldIrql
);
159 RemoveEntryList(&AddrFile
->ListEntry
);
160 TcpipReleaseSpinLock(&AddressFileListLock
, OldIrql
);
162 TcpipAcquireSpinLock(&AddrFile
->Lock
, &OldIrql
);
164 /* FIXME: Kill TCP connections on this address file object */
166 /* Return pending requests with error */
168 TI_DbgPrint(DEBUG_ADDRFILE
, ("Aborting receive requests on AddrFile at (0x%X).\n", AddrFile
));
170 /* Go through pending receive request list and cancel them all */
171 CurrentEntry
= AddrFile
->ReceiveQueue
.Flink
;
172 while (CurrentEntry
!= &AddrFile
->ReceiveQueue
) {
173 NextEntry
= CurrentEntry
->Flink
;
174 ReceiveRequest
= CONTAINING_RECORD(CurrentEntry
, DATAGRAM_RECEIVE_REQUEST
, ListEntry
);
175 /* Abort the request and free its resources */
176 TcpipReleaseSpinLock(&AddrFile
->Lock
, OldIrql
);
177 (*ReceiveRequest
->Complete
)(ReceiveRequest
->Context
, STATUS_ADDRESS_CLOSED
, 0);
178 TcpipAcquireSpinLock(&AddrFile
->Lock
, &OldIrql
);
179 CurrentEntry
= NextEntry
;
182 TI_DbgPrint(DEBUG_ADDRFILE
, ("Aborting send requests on address file at (0x%X).\n", AddrFile
));
184 /* Go through pending send request list and cancel them all */
185 CurrentEntry
= AddrFile
->TransmitQueue
.Flink
;
186 while (CurrentEntry
!= &AddrFile
->TransmitQueue
) {
187 NextEntry
= CurrentEntry
->Flink
;
188 SendRequest
= CONTAINING_RECORD(CurrentEntry
,
189 DATAGRAM_SEND_REQUEST
, ListEntry
);
190 /* Abort the request and free its resources */
191 TcpipReleaseSpinLock(&AddrFile
->Lock
, OldIrql
);
192 (*SendRequest
->Complete
)(SendRequest
->Context
, STATUS_ADDRESS_CLOSED
, 0);
193 exFreePool(SendRequest
);
194 TcpipAcquireSpinLock(&AddrFile
->Lock
, &OldIrql
);
195 CurrentEntry
= NextEntry
;
198 TcpipReleaseSpinLock(&AddrFile
->Lock
, OldIrql
);
200 (*AddrFile
->Free
)(AddrFile
);
202 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
207 * FUNCTION: Open an address file object
209 * Request = Pointer to TDI request structure for this request
210 * Address = Pointer to address to be opened
211 * Protocol = Protocol on which to open the address
212 * Options = Pointer to option buffer
214 * Status of operation
216 NTSTATUS
FileOpenAddress(
217 PTDI_REQUEST Request
,
218 PTA_IP_ADDRESS Address
,
222 IPv4_RAW_ADDRESS IPv4Address
;
223 PADDRESS_FILE AddrFile
;
225 TI_DbgPrint(MID_TRACE
, ("Called (Proto %d).\n", Protocol
));
227 AddrFile
= exAllocatePool(NonPagedPool
, sizeof(ADDRESS_FILE
));
229 TI_DbgPrint(MIN_TRACE
, ("Insufficient resources.\n"));
230 return STATUS_INSUFFICIENT_RESOURCES
;
233 TI_DbgPrint(DEBUG_ADDRFILE
, ("Address file object allocated at (0x%X).\n", AddrFile
));
235 RtlZeroMemory(AddrFile
, sizeof(ADDRESS_FILE
));
237 AddrFile
->Free
= AddrFileFree
;
239 /* Make sure address is a local unicast address or 0 */
240 /* FIXME: IPv4 only */
241 AddrFile
->Family
= Address
->Address
[0].AddressType
;
242 IPv4Address
= Address
->Address
[0].Address
[0].in_addr
;
243 if (IPv4Address
!= 0 &&
244 !AddrLocateADEv4(IPv4Address
, &AddrFile
->Address
)) {
245 exFreePool(AddrFile
);
246 TI_DbgPrint(MIN_TRACE
, ("Non-local address given (0x%X).\n", DN2H(IPv4Address
)));
247 return STATUS_INVALID_PARAMETER
;
251 /* Bound to the default address ... Copy the address type */
252 AddrFile
->Address
.Type
= IP_ADDRESS_V4
;
255 TI_DbgPrint(MID_TRACE
, ("Opening address %s for communication (P=%d U=%d).\n",
256 A2S(&AddrFile
->Address
), Protocol
, IPPROTO_UDP
));
258 /* Protocol specific handling */
262 TCPAllocatePort(Address
->Address
[0].Address
[0].sin_port
);
264 if ((Address
->Address
[0].Address
[0].sin_port
&&
265 AddrFile
->Port
!= Address
->Address
[0].Address
[0].sin_port
) ||
266 AddrFile
->Port
== 0xffff)
268 exFreePool(AddrFile
);
269 return STATUS_INVALID_PARAMETER
;
272 AddrFile
->Send
= NULL
; /* TCPSendData */
276 TI_DbgPrint(MID_TRACE
,("Allocating udp port\n"));
278 UDPAllocatePort(Address
->Address
[0].Address
[0].sin_port
);
280 if ((Address
->Address
[0].Address
[0].sin_port
&&
281 AddrFile
->Port
!= Address
->Address
[0].Address
[0].sin_port
) ||
282 AddrFile
->Port
== 0xffff)
284 exFreePool(AddrFile
);
285 return STATUS_INVALID_PARAMETER
;
288 TI_DbgPrint(MID_TRACE
,("Setting port %d (wanted %d)\n",
290 Address
->Address
[0].Address
[0].sin_port
));
291 AddrFile
->Send
= UDPSendDatagram
;
295 /* Use raw IP for all other protocols */
297 AddrFile
->Send
= RawIPSendDatagram
;
301 TI_DbgPrint(MID_TRACE
, ("IP protocol number for address file object is %d.\n",
304 TI_DbgPrint(MID_TRACE
, ("Port number for address file object is %d.\n",
305 WN2H(AddrFile
->Port
)));
308 AddrFile
->Protocol
= Protocol
;
310 /* Initialize receive and transmit queues */
311 InitializeListHead(&AddrFile
->ReceiveQueue
);
312 InitializeListHead(&AddrFile
->TransmitQueue
);
314 /* Initialize spin lock that protects the address file object */
315 KeInitializeSpinLock(&AddrFile
->Lock
);
317 /* Set valid flag so the address can be used */
318 AF_SET_VALID(AddrFile
);
320 /* Return address file object */
321 Request
->Handle
.AddressHandle
= AddrFile
;
323 /* Add address file to global list */
324 ExInterlockedInsertTailList(
325 &AddressFileListHead
,
326 &AddrFile
->ListEntry
,
327 &AddressFileListLock
);
329 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
331 return STATUS_SUCCESS
;
336 * FUNCTION: Closes an address file object
338 * Request = Pointer to TDI request structure for this request
340 * Status of operation
342 NTSTATUS
FileCloseAddress(
343 PTDI_REQUEST Request
)
346 PADDRESS_FILE AddrFile
;
347 NTSTATUS Status
= STATUS_SUCCESS
;
349 TI_DbgPrint(MID_TRACE
, ("Called.\n"));
351 AddrFile
= Request
->Handle
.AddressHandle
;
353 TcpipAcquireSpinLock(&AddrFile
->Lock
, &OldIrql
);
355 /* Set address file object exclusive to us */
356 AF_SET_BUSY(AddrFile
);
357 AF_CLR_VALID(AddrFile
);
359 TcpipReleaseSpinLock(&AddrFile
->Lock
, OldIrql
);
361 /* Protocol specific handling */
362 switch (AddrFile
->Protocol
) {
364 TCPFreePort( AddrFile
->Port
);
365 if( AddrFile
->Listener
) {
366 TCPClose( AddrFile
->Listener
);
367 exFreePool( AddrFile
->Listener
);
372 UDPFreePort( AddrFile
->Port
);
376 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
383 * FUNCTION: Closes an address file object
385 * Request = Pointer to TDI request structure for this request
387 * Status of operation
389 NTSTATUS
FileFreeAddress(
390 PTDI_REQUEST Request
)
392 PADDRESS_FILE AddrFile
;
393 NTSTATUS Status
= STATUS_SUCCESS
;
395 AddrFile
= Request
->Handle
.AddressHandle
;
397 TI_DbgPrint(MID_TRACE
, ("Called.\n"));
399 DeleteAddress(AddrFile
);
401 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
408 * FUNCTION: Opens a connection file object
410 * Request = Pointer to TDI request structure for this request
411 * ClientContext = Pointer to client context information
413 * Status of operation
415 NTSTATUS
FileOpenConnection(
416 PTDI_REQUEST Request
,
420 PCONNECTION_ENDPOINT Connection
;
422 TI_DbgPrint(MID_TRACE
, ("Called.\n"));
424 Connection
= TCPAllocateConnectionEndpoint( ClientContext
);
426 if( !Connection
) return STATUS_NO_MEMORY
;
428 Status
= TCPSocket( Connection
, AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
430 if( !NT_SUCCESS(Status
) ) {
431 TCPFreeConnectionEndpoint( Connection
);
435 /* Return connection endpoint file object */
436 Request
->Handle
.ConnectionContext
= Connection
;
438 /* Add connection endpoint to global list */
439 ExInterlockedInsertTailList(
440 &ConnectionEndpointListHead
,
441 &Connection
->ListEntry
,
442 &ConnectionEndpointListLock
);
444 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
446 return STATUS_SUCCESS
;
451 * FUNCTION: Find a connection by examining the context field. This
452 * is needed in some situations where a FIN reply is needed after a
453 * socket is formally broken.
455 * Request = Pointer to TDI request structure for this request
457 * Status of operation
459 PCONNECTION_ENDPOINT
FileFindConnectionByContext( PVOID Context
) {
462 PCONNECTION_ENDPOINT Connection
= NULL
;
464 TcpipAcquireSpinLock( &ConnectionEndpointListLock
, &OldIrql
);
466 for( Entry
= ConnectionEndpointListHead
.Flink
;
467 Entry
!= &ConnectionEndpointListHead
;
468 Entry
= Entry
->Flink
) {
470 CONTAINING_RECORD( Entry
, CONNECTION_ENDPOINT
, ListEntry
);
471 if( Connection
->SocketContext
== Context
) break;
472 else Connection
= NULL
;
475 TcpipReleaseSpinLock( &ConnectionEndpointListLock
, OldIrql
);
481 * FUNCTION: Closes an connection file object
483 * Request = Pointer to TDI request structure for this request
485 * Status of operation
487 NTSTATUS
FileCloseConnection(
488 PTDI_REQUEST Request
)
490 PCONNECTION_ENDPOINT Connection
;
492 TI_DbgPrint(MID_TRACE
, ("Called.\n"));
494 Connection
= Request
->Handle
.ConnectionContext
;
496 TcpipRecursiveMutexEnter( &TCPLock
, TRUE
);
497 TCPClose( Connection
);
498 TcpipRecursiveMutexLeave( &TCPLock
);
500 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
502 return STATUS_SUCCESS
;
507 * FUNCTION: Frees an connection file object
509 * Request = Pointer to TDI request structure for this request
511 * Status of operation
513 NTSTATUS
FileFreeConnection(
514 PTDI_REQUEST Request
)
517 PCONNECTION_ENDPOINT Connection
;
519 TI_DbgPrint(MID_TRACE
, ("Called.\n"));
521 Connection
= Request
->Handle
.ConnectionContext
;
523 TcpipAcquireSpinLock(&ConnectionEndpointListLock
, &OldIrql
);
524 RemoveEntryList(&Connection
->ListEntry
);
525 TcpipReleaseSpinLock(&ConnectionEndpointListLock
, OldIrql
);
527 TCPFreeConnectionEndpoint(Connection
);
529 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
531 return STATUS_SUCCESS
;
536 * FUNCTION: Opens a control channel file object
538 * Request = Pointer to TDI request structure for this request
540 * Status of operation
542 NTSTATUS
FileOpenControlChannel(
543 PTDI_REQUEST Request
)
545 PCONTROL_CHANNEL ControlChannel
;
546 TI_DbgPrint(MID_TRACE
, ("Called.\n"));
548 ControlChannel
= exAllocatePool(NonPagedPool
, sizeof(*ControlChannel
));
550 if (!ControlChannel
) {
551 TI_DbgPrint(MIN_TRACE
, ("Insufficient resources.\n"));
552 return STATUS_INSUFFICIENT_RESOURCES
;
555 RtlZeroMemory(ControlChannel
, sizeof(CONTROL_CHANNEL
));
557 /* Make sure address is a local unicast address or 0 */
559 /* Locate address entry. If specified address is 0, a random address is chosen */
561 /* Initialize receive and transmit queues */
562 InitializeListHead(&ControlChannel
->ListEntry
);
564 /* Initialize spin lock that protects the address file object */
565 KeInitializeSpinLock(&ControlChannel
->Lock
);
567 /* Return address file object */
568 Request
->Handle
.ControlChannel
= ControlChannel
;
570 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
572 return STATUS_SUCCESS
;
576 * FUNCTION: Closes a control channel file object
578 * Request = Pointer to TDI request structure for this request
580 * Status of operation
582 NTSTATUS
FileFreeControlChannel(
583 PTDI_REQUEST Request
)
585 PCONTROL_CHANNEL ControlChannel
= Request
->Handle
.ControlChannel
;
586 NTSTATUS Status
= STATUS_SUCCESS
;
588 exFreePool(ControlChannel
);
589 Request
->Handle
.ControlChannel
= NULL
;