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
);
47 * FUNCTION: Searches through address file entries to find next match
49 * SearchContext = Pointer to search context
51 * Pointer to address file, NULL if none was found
53 PADDRESS_FILE
AddrSearchNext(
54 PAF_SEARCH SearchContext
)
56 PLIST_ENTRY CurrentEntry
;
57 PIP_ADDRESS IPAddress
;
59 PADDRESS_FILE Current
= NULL
;
60 BOOLEAN Found
= FALSE
;
62 if (IsListEmpty(SearchContext
->Next
))
65 CurrentEntry
= SearchContext
->Next
;
67 TcpipAcquireSpinLock(&AddressFileListLock
, &OldIrql
);
69 while (CurrentEntry
!= &AddressFileListHead
) {
70 Current
= CONTAINING_RECORD(CurrentEntry
, ADDRESS_FILE
, ListEntry
);
72 IPAddress
= &Current
->Address
;
74 TI_DbgPrint(DEBUG_ADDRFILE
, ("Comparing: ((%d, %d, %s), (%d, %d, %s)).\n",
78 WN2H(SearchContext
->Port
),
79 SearchContext
->Protocol
,
80 A2S(SearchContext
->Address
)));
82 /* See if this address matches the search criteria */
83 if (((Current
->Port
== SearchContext
->Port
) &&
84 (Current
->Protocol
== SearchContext
->Protocol
) &&
85 (AddrIsEqual(IPAddress
, SearchContext
->Address
))) ||
86 (AddrIsUnspecified(IPAddress
))) {
87 /* We've found a match */
91 CurrentEntry
= CurrentEntry
->Flink
;
94 TcpipReleaseSpinLock(&AddressFileListLock
, OldIrql
);
97 SearchContext
->Next
= CurrentEntry
->Flink
;
106 * FUNCTION: Frees an address file object
108 * Object = Pointer to address file object to free
115 VOID
ControlChannelFree(
118 * FUNCTION: Frees an address file object
120 * Object = Pointer to address file object to free
127 VOID
DeleteAddress(PADDRESS_FILE AddrFile
)
129 * FUNCTION: Deletes an address file object
131 * AddrFile = Pointer to address file object to delete
135 PLIST_ENTRY CurrentEntry
;
136 PLIST_ENTRY NextEntry
;
137 PDATAGRAM_SEND_REQUEST SendRequest
;
138 PDATAGRAM_RECEIVE_REQUEST ReceiveRequest
;
140 TI_DbgPrint(MID_TRACE
, ("Called.\n"));
142 /* Remove address file from the global list */
143 TcpipAcquireSpinLock(&AddressFileListLock
, &OldIrql
);
144 RemoveEntryList(&AddrFile
->ListEntry
);
145 TcpipReleaseSpinLock(&AddressFileListLock
, OldIrql
);
147 TcpipAcquireSpinLock(&AddrFile
->Lock
, &OldIrql
);
149 /* FIXME: Kill TCP connections on this address file object */
151 /* Return pending requests with error */
153 TI_DbgPrint(DEBUG_ADDRFILE
, ("Aborting receive requests on AddrFile at (0x%X).\n", AddrFile
));
155 /* Go through pending receive request list and cancel them all */
156 CurrentEntry
= AddrFile
->ReceiveQueue
.Flink
;
157 while (CurrentEntry
!= &AddrFile
->ReceiveQueue
) {
158 NextEntry
= CurrentEntry
->Flink
;
159 ReceiveRequest
= CONTAINING_RECORD(CurrentEntry
, DATAGRAM_RECEIVE_REQUEST
, ListEntry
);
160 /* Abort the request and free its resources */
161 TcpipReleaseSpinLock(&AddrFile
->Lock
, OldIrql
);
162 (*ReceiveRequest
->Complete
)(ReceiveRequest
->Context
, STATUS_ADDRESS_CLOSED
, 0);
163 TcpipAcquireSpinLock(&AddrFile
->Lock
, &OldIrql
);
164 CurrentEntry
= NextEntry
;
167 TI_DbgPrint(DEBUG_ADDRFILE
, ("Aborting send requests on address file at (0x%X).\n", AddrFile
));
169 /* Go through pending send request list and cancel them all */
170 CurrentEntry
= AddrFile
->TransmitQueue
.Flink
;
171 while (CurrentEntry
!= &AddrFile
->TransmitQueue
) {
172 NextEntry
= CurrentEntry
->Flink
;
173 SendRequest
= CONTAINING_RECORD(CurrentEntry
,
174 DATAGRAM_SEND_REQUEST
, ListEntry
);
175 /* Abort the request and free its resources */
176 TcpipReleaseSpinLock(&AddrFile
->Lock
, OldIrql
);
177 (*SendRequest
->Complete
)(SendRequest
->Context
, STATUS_ADDRESS_CLOSED
, 0);
178 ExFreePool(SendRequest
);
179 TcpipAcquireSpinLock(&AddrFile
->Lock
, &OldIrql
);
180 CurrentEntry
= NextEntry
;
183 TcpipReleaseSpinLock(&AddrFile
->Lock
, OldIrql
);
185 (*AddrFile
->Free
)(AddrFile
);
187 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
192 * FUNCTION: Deletes a connection endpoint file object
194 * Connection = Pointer to connection endpoint to delete
196 VOID
DeleteConnectionEndpoint(
197 PCONNECTION_ENDPOINT Connection
)
201 TI_DbgPrint(MID_TRACE
, ("Called.\n"));
203 /* Remove connection endpoint from the global list */
204 TcpipAcquireSpinLock(&ConnectionEndpointListLock
, &OldIrql
);
205 RemoveEntryList(&Connection
->ListEntry
);
206 TcpipReleaseSpinLock(&ConnectionEndpointListLock
, OldIrql
);
208 ExFreePool(Connection
);
210 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
214 * FUNCTION: Open an address file object
216 * Request = Pointer to TDI request structure for this request
217 * Address = Pointer to address to be opened
218 * Protocol = Protocol on which to open the address
219 * Options = Pointer to option buffer
221 * Status of operation
223 NTSTATUS
FileOpenAddress(
224 PTDI_REQUEST Request
,
225 PTA_IP_ADDRESS Address
,
229 IPv4_RAW_ADDRESS IPv4Address
;
231 PADDRESS_FILE AddrFile
;
233 TI_DbgPrint(MID_TRACE
, ("Called (Proto %d).\n", Protocol
));
235 AddrFile
= ExAllocatePool(NonPagedPool
, sizeof(ADDRESS_FILE
));
237 TI_DbgPrint(MIN_TRACE
, ("Insufficient resources.\n"));
238 return STATUS_INSUFFICIENT_RESOURCES
;
241 TI_DbgPrint(DEBUG_ADDRFILE
, ("Address file object allocated at (0x%X).\n", AddrFile
));
243 RtlZeroMemory(AddrFile
, sizeof(ADDRESS_FILE
));
245 AddrFile
->Free
= AddrFileFree
;
247 /* Make sure address is a local unicast address or 0 */
249 /* Locate address entry. If specified address is 0, a random address is chosen */
251 /* FIXME: IPv4 only */
252 AddrFile
->Family
= Address
->Address
[0].AddressType
;
253 IPv4Address
= Address
->Address
[0].Address
[0].in_addr
;
254 if (IPv4Address
== 0)
255 Matched
= IPGetDefaultAddress(&AddrFile
->Address
);
257 Matched
= AddrLocateADEv4(IPv4Address
, &AddrFile
->Address
);
260 ExFreePool(AddrFile
);
261 TI_DbgPrint(MIN_TRACE
, ("Non-local address given (0x%X).\n", DN2H(IPv4Address
)));
262 return STATUS_INVALID_PARAMETER
;
265 TI_DbgPrint(MID_TRACE
, ("Opening address %s for communication (P=%d U=%d).\n",
266 A2S(&AddrFile
->Address
), Protocol
, IPPROTO_UDP
));
268 /* Protocol specific handling */
271 /* FIXME: If specified port is 0, a port is chosen dynamically */
272 AddrFile
->Port
= TCPAllocatePort(Address
->Address
[0].Address
[0].sin_port
);
273 AddrFile
->Send
= NULL
; /* TCPSendData */
277 TI_DbgPrint(MID_TRACE
,("Allocating udp port\n"));
279 UDPAllocatePort(Address
->Address
[0].Address
[0].sin_port
);
280 TI_DbgPrint(MID_TRACE
,("Setting port %d\n", AddrFile
->Port
));
281 AddrFile
->Send
= UDPSendDatagram
;
285 /* Use raw IP for all other protocols */
287 AddrFile
->Send
= RawIPSendDatagram
;
291 TI_DbgPrint(MID_TRACE
, ("IP protocol number for address file object is %d.\n",
294 TI_DbgPrint(MID_TRACE
, ("Port number for address file object is %d.\n",
295 WN2H(AddrFile
->Port
)));
298 AddrFile
->Protocol
= Protocol
;
300 /* Initialize receive and transmit queues */
301 InitializeListHead(&AddrFile
->ReceiveQueue
);
302 InitializeListHead(&AddrFile
->TransmitQueue
);
304 /* Initialize spin lock that protects the address file object */
305 KeInitializeSpinLock(&AddrFile
->Lock
);
307 /* Set valid flag so the address can be used */
308 AF_SET_VALID(AddrFile
);
310 /* Return address file object */
311 Request
->Handle
.AddressHandle
= AddrFile
;
313 /* Add address file to global list */
314 ExInterlockedInsertTailList(
315 &AddressFileListHead
,
316 &AddrFile
->ListEntry
,
317 &AddressFileListLock
);
319 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
321 return STATUS_SUCCESS
;
326 * FUNCTION: Closes an address file object
328 * Request = Pointer to TDI request structure for this request
330 * Status of operation
332 NTSTATUS
FileCloseAddress(
333 PTDI_REQUEST Request
)
336 PADDRESS_FILE AddrFile
;
337 NTSTATUS Status
= STATUS_SUCCESS
;
339 TI_DbgPrint(MID_TRACE
, ("Called.\n"));
341 AddrFile
= Request
->Handle
.AddressHandle
;
343 TcpipAcquireSpinLock(&AddrFile
->Lock
, &OldIrql
);
345 /* Set address file object exclusive to us */
346 AF_SET_BUSY(AddrFile
);
347 AF_CLR_VALID(AddrFile
);
349 TcpipReleaseSpinLock(&AddrFile
->Lock
, OldIrql
);
351 /* Protocol specific handling */
352 switch (AddrFile
->Protocol
) {
354 TCPFreePort( AddrFile
->Port
);
355 if( AddrFile
->Listener
)
356 TCPClose( AddrFile
->Listener
);
360 UDPFreePort( AddrFile
->Port
);
364 DeleteAddress(AddrFile
);
366 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
373 * FUNCTION: Opens a connection file object
375 * Request = Pointer to TDI request structure for this request
376 * ClientContext = Pointer to client context information
378 * Status of operation
380 NTSTATUS
FileOpenConnection(
381 PTDI_REQUEST Request
,
385 PCONNECTION_ENDPOINT Connection
;
387 TI_DbgPrint(MID_TRACE
, ("Called.\n"));
389 Connection
= TCPAllocateConnectionEndpoint( ClientContext
);
391 if( !Connection
) return STATUS_NO_MEMORY
;
393 Status
= TCPSocket( Connection
, AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
395 /* Return connection endpoint file object */
396 Request
->Handle
.ConnectionContext
= Connection
;
398 /* Add connection endpoint to global list */
399 ExInterlockedInsertTailList(
400 &ConnectionEndpointListHead
,
401 &Connection
->ListEntry
,
402 &ConnectionEndpointListLock
);
404 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
406 return STATUS_SUCCESS
;
411 * FUNCTION: Find a connection by examining the context field. This
412 * is needed in some situations where a FIN reply is needed after a
413 * socket is formally broken.
415 * Request = Pointer to TDI request structure for this request
417 * Status of operation
419 PCONNECTION_ENDPOINT
FileFindConnectionByContext( PVOID Context
) {
422 PCONNECTION_ENDPOINT Connection
= NULL
;
424 TcpipAcquireSpinLock( &ConnectionEndpointListLock
, &OldIrql
);
426 for( Entry
= ConnectionEndpointListHead
.Flink
;
427 Entry
!= &ConnectionEndpointListHead
;
428 Entry
= Entry
->Flink
) {
430 CONTAINING_RECORD( Entry
, CONNECTION_ENDPOINT
, ListEntry
);
431 if( Connection
->SocketContext
== Context
) break;
432 else Connection
= NULL
;
435 TcpipReleaseSpinLock( &ConnectionEndpointListLock
, OldIrql
);
441 * FUNCTION: Closes an connection file object
443 * Request = Pointer to TDI request structure for this request
445 * Status of operation
447 NTSTATUS
FileCloseConnection(
448 PTDI_REQUEST Request
)
450 PCONNECTION_ENDPOINT Connection
;
451 NTSTATUS Status
= STATUS_SUCCESS
;
453 TI_DbgPrint(MID_TRACE
, ("Called.\n"));
455 Connection
= Request
->Handle
.ConnectionContext
;
457 TCPClose(Connection
);
458 DeleteConnectionEndpoint(Connection
);
460 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
467 * FUNCTION: Opens a control channel file object
469 * Request = Pointer to TDI request structure for this request
471 * Status of operation
473 NTSTATUS
FileOpenControlChannel(
474 PTDI_REQUEST Request
)
476 PCONTROL_CHANNEL ControlChannel
;
477 TI_DbgPrint(MID_TRACE
, ("Called.\n"));
479 ControlChannel
= ExAllocatePool(NonPagedPool
, sizeof(*ControlChannel
));
481 if (!ControlChannel
) {
482 TI_DbgPrint(MIN_TRACE
, ("Insufficient resources.\n"));
483 return STATUS_INSUFFICIENT_RESOURCES
;
486 RtlZeroMemory(ControlChannel
, sizeof(CONTROL_CHANNEL
));
488 /* Make sure address is a local unicast address or 0 */
490 /* Locate address entry. If specified address is 0, a random address is chosen */
492 /* Initialize receive and transmit queues */
493 InitializeListHead(&ControlChannel
->ListEntry
);
495 /* Initialize spin lock that protects the address file object */
496 KeInitializeSpinLock(&ControlChannel
->Lock
);
498 /* Return address file object */
499 Request
->Handle
.ControlChannel
= ControlChannel
;
501 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
503 return STATUS_SUCCESS
;
507 * FUNCTION: Closes a control channel file object
509 * Request = Pointer to TDI request structure for this request
511 * Status of operation
513 NTSTATUS
FileCloseControlChannel(
514 PTDI_REQUEST Request
)
516 PCONTROL_CHANNEL ControlChannel
= Request
->Handle
.ControlChannel
;
517 NTSTATUS Status
= STATUS_SUCCESS
;
519 ExFreePool(ControlChannel
);
520 Request
->Handle
.ControlChannel
= NULL
;