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
AddrIsBroadcastMatch(
47 PIP_ADDRESS UnicastAddress
,
48 PIP_ADDRESS BroadcastAddress
) {
51 ForEachInterface(IF
) {
52 if ((AddrIsUnspecified(UnicastAddress
) ||
53 AddrIsEqual(&IF
->Unicast
, UnicastAddress
)) &&
54 (AddrIsEqual(&IF
->Broadcast
, BroadcastAddress
)))
61 BOOLEAN
AddrReceiveMatch(
62 PIP_ADDRESS LocalAddress
,
63 PIP_ADDRESS RemoteAddress
)
65 if (AddrIsEqual(LocalAddress
, RemoteAddress
))
67 /* Unicast address match */
71 if (AddrIsBroadcastMatch(LocalAddress
, RemoteAddress
))
73 /* Broadcast address match */
77 if (AddrIsUnspecified(LocalAddress
))
79 /* Local address unspecified */
83 if (AddrIsUnspecified(RemoteAddress
))
85 /* Remote address unspecified */
93 * FUNCTION: Searches through address file entries to find next match
95 * SearchContext = Pointer to search context
97 * Pointer to address file, NULL if none was found
99 PADDRESS_FILE
AddrSearchNext(
100 PAF_SEARCH SearchContext
)
102 PLIST_ENTRY CurrentEntry
;
103 PIP_ADDRESS IPAddress
;
105 PADDRESS_FILE Current
= NULL
;
106 BOOLEAN Found
= FALSE
;
108 if (IsListEmpty(SearchContext
->Next
))
111 CurrentEntry
= SearchContext
->Next
;
113 TcpipAcquireSpinLock(&AddressFileListLock
, &OldIrql
);
115 while (CurrentEntry
!= &AddressFileListHead
) {
116 Current
= CONTAINING_RECORD(CurrentEntry
, ADDRESS_FILE
, ListEntry
);
118 IPAddress
= &Current
->Address
;
120 TI_DbgPrint(DEBUG_ADDRFILE
, ("Comparing: ((%d, %d, %s), (%d, %d, %s)).\n",
124 WN2H(SearchContext
->Port
),
125 SearchContext
->Protocol
,
126 A2S(SearchContext
->Address
)));
128 /* See if this address matches the search criteria */
129 if ((Current
->Port
== SearchContext
->Port
) &&
130 (Current
->Protocol
== SearchContext
->Protocol
) &&
131 (AddrReceiveMatch(IPAddress
, SearchContext
->Address
))) {
132 /* We've found a match */
136 CurrentEntry
= CurrentEntry
->Flink
;
139 TcpipReleaseSpinLock(&AddressFileListLock
, OldIrql
);
142 SearchContext
->Next
= CurrentEntry
->Flink
;
151 * FUNCTION: Frees an address file object
153 * Object = Pointer to address file object to free
160 VOID
ControlChannelFree(
163 * FUNCTION: Frees an address file object
165 * Object = Pointer to address file object to free
173 * FUNCTION: Open an address file object
175 * Request = Pointer to TDI request structure for this request
176 * Address = Pointer to address to be opened
177 * Protocol = Protocol on which to open the address
178 * Options = Pointer to option buffer
180 * Status of operation
182 NTSTATUS
FileOpenAddress(
183 PTDI_REQUEST Request
,
184 PTA_IP_ADDRESS Address
,
188 PADDRESS_FILE AddrFile
;
190 TI_DbgPrint(MID_TRACE
, ("Called (Proto %d).\n", Protocol
));
192 AddrFile
= exAllocatePool(NonPagedPool
, sizeof(ADDRESS_FILE
));
194 TI_DbgPrint(MIN_TRACE
, ("Insufficient resources.\n"));
195 return STATUS_INSUFFICIENT_RESOURCES
;
198 TI_DbgPrint(DEBUG_ADDRFILE
, ("Address file object allocated at (0x%X).\n", AddrFile
));
200 RtlZeroMemory(AddrFile
, sizeof(ADDRESS_FILE
));
202 AddrFile
->Free
= AddrFileFree
;
204 /* Set our default TTL */
207 /* Make sure address is a local unicast address or 0 */
208 /* FIXME: IPv4 only */
209 AddrFile
->Family
= Address
->Address
[0].AddressType
;
210 AddrFile
->Address
.Address
.IPv4Address
= Address
->Address
[0].Address
[0].in_addr
;
211 AddrFile
->Address
.Type
= IP_ADDRESS_V4
;
213 if (!AddrIsUnspecified(&AddrFile
->Address
) &&
214 !AddrLocateInterface(&AddrFile
->Address
)) {
215 exFreePool(AddrFile
);
216 TI_DbgPrint(MIN_TRACE
, ("Non-local address given (0x%X).\n", A2S(&AddrFile
->Address
)));
217 return STATUS_INVALID_PARAMETER
;
220 TI_DbgPrint(MID_TRACE
, ("Opening address %s for communication (P=%d U=%d).\n",
221 A2S(&AddrFile
->Address
), Protocol
, IPPROTO_UDP
));
223 /* Protocol specific handling */
227 TCPAllocatePort(Address
->Address
[0].Address
[0].sin_port
);
229 if ((Address
->Address
[0].Address
[0].sin_port
&&
230 AddrFile
->Port
!= Address
->Address
[0].Address
[0].sin_port
) ||
231 AddrFile
->Port
== 0xffff)
233 exFreePool(AddrFile
);
234 return STATUS_INVALID_PARAMETER
;
237 AddEntity(CO_TL_ENTITY
, AddrFile
, CO_TL_TCP
);
239 AddrFile
->Send
= NULL
; /* TCPSendData */
243 TI_DbgPrint(MID_TRACE
,("Allocating udp port\n"));
245 UDPAllocatePort(Address
->Address
[0].Address
[0].sin_port
);
247 if ((Address
->Address
[0].Address
[0].sin_port
&&
248 AddrFile
->Port
!= Address
->Address
[0].Address
[0].sin_port
) ||
249 AddrFile
->Port
== 0xffff)
251 exFreePool(AddrFile
);
252 return STATUS_INVALID_PARAMETER
;
255 TI_DbgPrint(MID_TRACE
,("Setting port %d (wanted %d)\n",
257 Address
->Address
[0].Address
[0].sin_port
));
259 AddEntity(CL_TL_ENTITY
, AddrFile
, CL_TL_UDP
);
261 AddrFile
->Send
= UDPSendDatagram
;
266 AddrFile
->Send
= ICMPSendDatagram
;
268 /* FIXME: Verify this */
269 AddEntity(ER_ENTITY
, AddrFile
, ER_ICMP
);
273 /* Use raw IP for all other protocols */
275 AddrFile
->Send
= RawIPSendDatagram
;
277 /* FIXME: Verify this */
278 AddEntity(CL_TL_ENTITY
, AddrFile
, 0);
282 TI_DbgPrint(MID_TRACE
, ("IP protocol number for address file object is %d.\n",
285 TI_DbgPrint(MID_TRACE
, ("Port number for address file object is %d.\n",
286 WN2H(AddrFile
->Port
)));
289 AddrFile
->Protocol
= Protocol
;
291 /* Initialize receive and transmit queues */
292 InitializeListHead(&AddrFile
->ReceiveQueue
);
293 InitializeListHead(&AddrFile
->TransmitQueue
);
295 /* Initialize spin lock that protects the address file object */
296 KeInitializeSpinLock(&AddrFile
->Lock
);
298 /* Return address file object */
299 Request
->Handle
.AddressHandle
= AddrFile
;
301 /* Add address file to global list */
302 ExInterlockedInsertTailList(
303 &AddressFileListHead
,
304 &AddrFile
->ListEntry
,
305 &AddressFileListLock
);
307 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
309 return STATUS_SUCCESS
;
314 * FUNCTION: Closes an address file object
316 * Request = Pointer to TDI request structure for this request
318 * Status of operation
320 NTSTATUS
FileCloseAddress(
321 PTDI_REQUEST Request
)
323 PADDRESS_FILE AddrFile
;
324 NTSTATUS Status
= STATUS_SUCCESS
;
326 PDATAGRAM_RECEIVE_REQUEST ReceiveRequest
;
327 PDATAGRAM_SEND_REQUEST SendRequest
;
328 PLIST_ENTRY CurrentEntry
;
330 AddrFile
= Request
->Handle
.AddressHandle
;
332 TI_DbgPrint(MID_TRACE
, ("Called.\n"));
334 /* Remove address file from the global list */
335 TcpipAcquireSpinLock(&AddressFileListLock
, &OldIrql
);
336 RemoveEntryList(&AddrFile
->ListEntry
);
337 TcpipReleaseSpinLock(&AddressFileListLock
, OldIrql
);
339 /* FIXME: Kill TCP connections on this address file object */
341 /* Return pending requests with error */
343 TI_DbgPrint(DEBUG_ADDRFILE
, ("Aborting receive requests on AddrFile at (0x%X).\n", AddrFile
));
345 /* Go through pending receive request list and cancel them all */
346 while ((CurrentEntry
= ExInterlockedRemoveHeadList(&AddrFile
->ReceiveQueue
, &AddrFile
->Lock
))) {
347 ReceiveRequest
= CONTAINING_RECORD(CurrentEntry
, DATAGRAM_RECEIVE_REQUEST
, ListEntry
);
348 (*ReceiveRequest
->Complete
)(ReceiveRequest
->Context
, STATUS_CANCELLED
, 0);
349 /* exFreePool(ReceiveRequest); FIXME: WTF? */
352 TI_DbgPrint(DEBUG_ADDRFILE
, ("Aborting send requests on address file at (0x%X).\n", AddrFile
));
354 /* Go through pending send request list and cancel them all */
355 while ((CurrentEntry
= ExInterlockedRemoveHeadList(&AddrFile
->ReceiveQueue
, &AddrFile
->Lock
))) {
356 SendRequest
= CONTAINING_RECORD(CurrentEntry
, DATAGRAM_SEND_REQUEST
, ListEntry
);
357 (*SendRequest
->Complete
)(SendRequest
->Context
, STATUS_CANCELLED
, 0);
358 exFreePool(SendRequest
);
361 /* Protocol specific handling */
362 switch (AddrFile
->Protocol
) {
364 TCPFreePort( AddrFile
->Port
);
365 if( AddrFile
->Listener
)
366 TCPClose( AddrFile
->Listener
);
370 UDPFreePort( AddrFile
->Port
);
374 RemoveEntityByContext(AddrFile
);
376 (*AddrFile
->Free
)(AddrFile
);
378 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
385 * FUNCTION: Opens a connection file object
387 * Request = Pointer to TDI request structure for this request
388 * ClientContext = Pointer to client context information
390 * Status of operation
392 NTSTATUS
FileOpenConnection(
393 PTDI_REQUEST Request
,
397 PCONNECTION_ENDPOINT Connection
;
399 TI_DbgPrint(MID_TRACE
, ("Called.\n"));
401 Connection
= TCPAllocateConnectionEndpoint( ClientContext
);
403 if( !Connection
) return STATUS_NO_MEMORY
;
405 Status
= TCPSocket( Connection
, AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
407 if( !NT_SUCCESS(Status
) ) {
408 TCPFreeConnectionEndpoint( Connection
);
412 /* Return connection endpoint file object */
413 Request
->Handle
.ConnectionContext
= Connection
;
415 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
417 return STATUS_SUCCESS
;
421 * FUNCTION: Closes an connection file object
423 * Request = Pointer to TDI request structure for this request
425 * Status of operation
427 NTSTATUS
FileCloseConnection(
428 PTDI_REQUEST Request
)
430 PCONNECTION_ENDPOINT Connection
;
432 TI_DbgPrint(MID_TRACE
, ("Called.\n"));
434 Connection
= Request
->Handle
.ConnectionContext
;
436 TCPClose( Connection
);
438 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
440 return STATUS_SUCCESS
;
445 * FUNCTION: Opens a control channel file object
447 * Request = Pointer to TDI request structure for this request
449 * Status of operation
451 NTSTATUS
FileOpenControlChannel(
452 PTDI_REQUEST Request
)
454 PCONTROL_CHANNEL ControlChannel
;
455 TI_DbgPrint(MID_TRACE
, ("Called.\n"));
457 ControlChannel
= exAllocatePool(NonPagedPool
, sizeof(*ControlChannel
));
459 if (!ControlChannel
) {
460 TI_DbgPrint(MIN_TRACE
, ("Insufficient resources.\n"));
461 return STATUS_INSUFFICIENT_RESOURCES
;
464 RtlZeroMemory(ControlChannel
, sizeof(CONTROL_CHANNEL
));
466 /* Make sure address is a local unicast address or 0 */
468 /* Locate address entry. If specified address is 0, a random address is chosen */
470 /* Initialize receive and transmit queues */
471 InitializeListHead(&ControlChannel
->ListEntry
);
473 /* Initialize spin lock that protects the address file object */
474 KeInitializeSpinLock(&ControlChannel
->Lock
);
476 /* Return address file object */
477 Request
->Handle
.ControlChannel
= ControlChannel
;
479 TI_DbgPrint(MAX_TRACE
, ("Leaving.\n"));
481 return STATUS_SUCCESS
;
485 * FUNCTION: Closes a control channel file object
487 * Request = Pointer to TDI request structure for this request
489 * Status of operation
491 NTSTATUS
FileCloseControlChannel(
492 PTDI_REQUEST Request
)
494 PCONTROL_CHANNEL ControlChannel
= Request
->Handle
.ControlChannel
;
495 NTSTATUS Status
= STATUS_SUCCESS
;
497 exFreePool(ControlChannel
);
498 Request
->Handle
.ControlChannel
= NULL
;