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: Deletes a connection endpoint file object
209 * Connection = Pointer to connection endpoint to delete
211 VOID
DeleteConnectionEndpoint(
212 PCONNECTION_ENDPOINT Connection
)
216 TI_DbgPrint(MID_TRACE
, ("Called.\n"));
218 /* Remove connection endpoint from the global list */
219 TcpipAcquireSpinLock(&ConnectionEndpointListLock
, &OldIrql
);
220 RemoveEntryList(&Connection
->ListEntry
);
221 TcpipReleaseSpinLock(&ConnectionEndpointListLock
, OldIrql
);
223 ExFreePool(Connection
);
225 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
229 * FUNCTION: Open an address file object
231 * Request = Pointer to TDI request structure for this request
232 * Address = Pointer to address to be opened
233 * Protocol = Protocol on which to open the address
234 * Options = Pointer to option buffer
236 * Status of operation
238 NTSTATUS
FileOpenAddress(
239 PTDI_REQUEST Request
,
240 PTA_IP_ADDRESS Address
,
244 IPv4_RAW_ADDRESS IPv4Address
;
246 PADDRESS_FILE AddrFile
;
248 TI_DbgPrint(MID_TRACE
, ("Called (Proto %d).\n", Protocol
));
250 AddrFile
= ExAllocatePool(NonPagedPool
, sizeof(ADDRESS_FILE
));
252 TI_DbgPrint(MIN_TRACE
, ("Insufficient resources.\n"));
253 return STATUS_INSUFFICIENT_RESOURCES
;
256 TI_DbgPrint(DEBUG_ADDRFILE
, ("Address file object allocated at (0x%X).\n", AddrFile
));
258 RtlZeroMemory(AddrFile
, sizeof(ADDRESS_FILE
));
260 AddrFile
->Free
= AddrFileFree
;
262 /* Make sure address is a local unicast address or 0 */
264 /* Locate address entry. If specified address is 0, a random address is chosen */
266 /* FIXME: IPv4 only */
267 AddrFile
->Family
= Address
->Address
[0].AddressType
;
268 IPv4Address
= Address
->Address
[0].Address
[0].in_addr
;
269 if (IPv4Address
== 0)
270 Matched
= IPGetDefaultAddress(&AddrFile
->Address
);
272 Matched
= AddrLocateADEv4(IPv4Address
, &AddrFile
->Address
);
275 ExFreePool(AddrFile
);
276 TI_DbgPrint(MIN_TRACE
, ("Non-local address given (0x%X).\n", DN2H(IPv4Address
)));
277 return STATUS_INVALID_PARAMETER
;
280 TI_DbgPrint(MID_TRACE
, ("Opening address %s for communication (P=%d U=%d).\n",
281 A2S(&AddrFile
->Address
), Protocol
, IPPROTO_UDP
));
283 /* Protocol specific handling */
287 TCPAllocatePort(Address
->Address
[0].Address
[0].sin_port
);
288 AddrFile
->Send
= NULL
; /* TCPSendData */
292 TI_DbgPrint(MID_TRACE
,("Allocating udp port\n"));
294 UDPAllocatePort(Address
->Address
[0].Address
[0].sin_port
);
295 TI_DbgPrint(MID_TRACE
,("Setting port %d (wanted %d)\n",
297 Address
->Address
[0].Address
[0].sin_port
));
298 AddrFile
->Send
= UDPSendDatagram
;
302 /* Use raw IP for all other protocols */
304 AddrFile
->Send
= RawIPSendDatagram
;
308 TI_DbgPrint(MID_TRACE
, ("IP protocol number for address file object is %d.\n",
311 TI_DbgPrint(MID_TRACE
, ("Port number for address file object is %d.\n",
312 WN2H(AddrFile
->Port
)));
315 AddrFile
->Protocol
= Protocol
;
317 /* Initialize receive and transmit queues */
318 InitializeListHead(&AddrFile
->ReceiveQueue
);
319 InitializeListHead(&AddrFile
->TransmitQueue
);
321 /* Initialize spin lock that protects the address file object */
322 KeInitializeSpinLock(&AddrFile
->Lock
);
324 /* Set valid flag so the address can be used */
325 AF_SET_VALID(AddrFile
);
327 /* Return address file object */
328 Request
->Handle
.AddressHandle
= AddrFile
;
330 /* Add address file to global list */
331 ExInterlockedInsertTailList(
332 &AddressFileListHead
,
333 &AddrFile
->ListEntry
,
334 &AddressFileListLock
);
336 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
338 return STATUS_SUCCESS
;
343 * FUNCTION: Closes an address file object
345 * Request = Pointer to TDI request structure for this request
347 * Status of operation
349 NTSTATUS
FileCloseAddress(
350 PTDI_REQUEST Request
)
353 PADDRESS_FILE AddrFile
;
354 NTSTATUS Status
= STATUS_SUCCESS
;
356 TI_DbgPrint(MID_TRACE
, ("Called.\n"));
358 AddrFile
= Request
->Handle
.AddressHandle
;
360 TcpipAcquireSpinLock(&AddrFile
->Lock
, &OldIrql
);
362 /* Set address file object exclusive to us */
363 AF_SET_BUSY(AddrFile
);
364 AF_CLR_VALID(AddrFile
);
366 TcpipReleaseSpinLock(&AddrFile
->Lock
, OldIrql
);
368 /* Protocol specific handling */
369 switch (AddrFile
->Protocol
) {
371 TCPFreePort( AddrFile
->Port
);
372 if( AddrFile
->Listener
)
373 TCPClose( AddrFile
->Listener
);
377 UDPFreePort( AddrFile
->Port
);
381 DeleteAddress(AddrFile
);
383 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
390 * FUNCTION: Opens a connection file object
392 * Request = Pointer to TDI request structure for this request
393 * ClientContext = Pointer to client context information
395 * Status of operation
397 NTSTATUS
FileOpenConnection(
398 PTDI_REQUEST Request
,
402 PCONNECTION_ENDPOINT Connection
;
404 TI_DbgPrint(MID_TRACE
, ("Called.\n"));
406 Connection
= TCPAllocateConnectionEndpoint( ClientContext
);
408 if( !Connection
) return STATUS_NO_MEMORY
;
410 Status
= TCPSocket( Connection
, AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
412 /* Return connection endpoint file object */
413 Request
->Handle
.ConnectionContext
= Connection
;
415 /* Add connection endpoint to global list */
416 ExInterlockedInsertTailList(
417 &ConnectionEndpointListHead
,
418 &Connection
->ListEntry
,
419 &ConnectionEndpointListLock
);
421 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
423 return STATUS_SUCCESS
;
428 * FUNCTION: Find a connection by examining the context field. This
429 * is needed in some situations where a FIN reply is needed after a
430 * socket is formally broken.
432 * Request = Pointer to TDI request structure for this request
434 * Status of operation
436 PCONNECTION_ENDPOINT
FileFindConnectionByContext( PVOID Context
) {
439 PCONNECTION_ENDPOINT Connection
= NULL
;
441 TcpipAcquireSpinLock( &ConnectionEndpointListLock
, &OldIrql
);
443 for( Entry
= ConnectionEndpointListHead
.Flink
;
444 Entry
!= &ConnectionEndpointListHead
;
445 Entry
= Entry
->Flink
) {
447 CONTAINING_RECORD( Entry
, CONNECTION_ENDPOINT
, ListEntry
);
448 if( Connection
->SocketContext
== Context
) break;
449 else Connection
= NULL
;
452 TcpipReleaseSpinLock( &ConnectionEndpointListLock
, OldIrql
);
458 * FUNCTION: Closes an connection file object
460 * Request = Pointer to TDI request structure for this request
462 * Status of operation
464 NTSTATUS
FileCloseConnection(
465 PTDI_REQUEST Request
)
467 PCONNECTION_ENDPOINT Connection
;
468 NTSTATUS Status
= STATUS_SUCCESS
;
470 TI_DbgPrint(MID_TRACE
, ("Called.\n"));
472 Connection
= Request
->Handle
.ConnectionContext
;
474 TCPClose(Connection
);
475 DeleteConnectionEndpoint(Connection
);
477 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
484 * FUNCTION: Opens a control channel file object
486 * Request = Pointer to TDI request structure for this request
488 * Status of operation
490 NTSTATUS
FileOpenControlChannel(
491 PTDI_REQUEST Request
)
493 PCONTROL_CHANNEL ControlChannel
;
494 TI_DbgPrint(MID_TRACE
, ("Called.\n"));
496 ControlChannel
= ExAllocatePool(NonPagedPool
, sizeof(*ControlChannel
));
498 if (!ControlChannel
) {
499 TI_DbgPrint(MIN_TRACE
, ("Insufficient resources.\n"));
500 return STATUS_INSUFFICIENT_RESOURCES
;
503 RtlZeroMemory(ControlChannel
, sizeof(CONTROL_CHANNEL
));
505 /* Make sure address is a local unicast address or 0 */
507 /* Locate address entry. If specified address is 0, a random address is chosen */
509 /* Initialize receive and transmit queues */
510 InitializeListHead(&ControlChannel
->ListEntry
);
512 /* Initialize spin lock that protects the address file object */
513 KeInitializeSpinLock(&ControlChannel
->Lock
);
515 /* Return address file object */
516 Request
->Handle
.ControlChannel
= ControlChannel
;
518 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
520 return STATUS_SUCCESS
;
524 * FUNCTION: Closes a control channel file object
526 * Request = Pointer to TDI request structure for this request
528 * Status of operation
530 NTSTATUS
FileCloseControlChannel(
531 PTDI_REQUEST Request
)
533 PCONTROL_CHANNEL ControlChannel
= Request
->Handle
.ControlChannel
;
534 NTSTATUS Status
= STATUS_SUCCESS
;
536 ExFreePool(ControlChannel
);
537 Request
->Handle
.ControlChannel
= NULL
;