2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS TCP/IP protocol driver
4 * FILE: transport/tcp/accept.c
5 * PURPOSE: Transmission Control Protocol Listen/Accept code
6 * PROGRAMMERS: Art Yerkes (arty@users.sf.net)
8 * arty 12/21/2004 Created
13 /* Listener->Lock MUST be acquired */
14 NTSTATUS
TCPServiceListeningSocket( PCONNECTION_ENDPOINT Listener
,
15 PCONNECTION_ENDPOINT Connection
,
16 PTDI_REQUEST_KERNEL Request
) {
20 PTA_IP_ADDRESS RequestAddressReturn
;
21 PTDI_CONNECTION_INFORMATION WhoIsConnecting
;
23 /* Unpack TDI info -- We need the return connection information
24 * struct to return the address so it can be filtered if needed
25 * by WSAAccept -- The returned address will be passed on to
26 * userland after we complete this irp */
27 WhoIsConnecting
= (PTDI_CONNECTION_INFORMATION
)
28 Request
->ReturnConnectionInformation
;
30 Status
= TCPTranslateError
31 ( OskitTCPAccept( Listener
,
32 &Connection
->SocketContext
,
37 Request
->RequestFlags
& TDI_QUERY_ACCEPT
? 0 : 1 ) );
39 TI_DbgPrint(DEBUG_TCP
,("Status %x\n", Status
));
41 if( NT_SUCCESS(Status
) && Status
!= STATUS_PENDING
) {
42 RequestAddressReturn
= WhoIsConnecting
->RemoteAddress
;
44 TI_DbgPrint(DEBUG_TCP
,("Copying address to %x (Who %x)\n",
45 RequestAddressReturn
, WhoIsConnecting
));
47 RequestAddressReturn
->TAAddressCount
= 1;
48 RequestAddressReturn
->Address
[0].AddressLength
= TDI_ADDRESS_LENGTH_IP
;
49 RequestAddressReturn
->Address
[0].AddressType
= TDI_ADDRESS_TYPE_IP
;
50 RequestAddressReturn
->Address
[0].Address
[0].sin_port
= OutAddr
.sin_port
;
51 RequestAddressReturn
->Address
[0].Address
[0].in_addr
= OutAddr
.sin_addr
.s_addr
;
52 RtlZeroMemory(RequestAddressReturn
->Address
[0].Address
[0].sin_zero
, 8);
54 TI_DbgPrint(DEBUG_TCP
,("Done copying\n"));
57 TI_DbgPrint(DEBUG_TCP
,("Status %x\n", Status
));
62 /* This listen is on a socket we keep as internal. That socket has the same
63 * lifetime as the address file */
64 NTSTATUS
TCPListen( PCONNECTION_ENDPOINT Connection
, UINT Backlog
) {
65 NTSTATUS Status
= STATUS_SUCCESS
;
66 SOCKADDR_IN AddressToBind
;
68 TA_IP_ADDRESS LocalAddress
;
72 LockObject(Connection
, &OldIrql
);
74 ASSERT_KM_POINTER(Connection
->AddressFile
);
76 TI_DbgPrint(DEBUG_TCP
,("TCPListen started\n"));
78 if (Connection
->AddressFile
->Port
)
80 AddressToBind
.sin_family
= AF_INET
;
81 memcpy( &AddressToBind
.sin_addr
,
82 &Connection
->AddressFile
->Address
.Address
.IPv4Address
,
83 sizeof(AddressToBind
.sin_addr
) );
84 AddressToBind
.sin_port
= Connection
->AddressFile
->Port
;
85 TI_DbgPrint(DEBUG_TCP
,("AddressToBind - %x:%x\n", AddressToBind
.sin_addr
, AddressToBind
.sin_port
));
87 /* Perform an explicit bind */
88 Status
= TCPTranslateError(OskitTCPBind(Connection
,
90 sizeof(AddressToBind
)));
94 /* An implicit bind will be performed */
95 Status
= STATUS_SUCCESS
;
98 if (NT_SUCCESS(Status
))
99 Status
= TCPTranslateError( OskitTCPListen( Connection
, Backlog
) );
101 if (NT_SUCCESS(Status
))
103 /* Check if we had an unspecified port */
104 if (!Connection
->AddressFile
->Port
)
106 /* We did, so we need to copy back the port */
107 if (NT_SUCCESS(TCPGetSockAddress(Connection
, (PTRANSPORT_ADDRESS
)&LocalAddress
, FALSE
)))
109 /* Allocate the port in the port bitmap */
110 Connection
->AddressFile
->Port
= TCPAllocatePort(LocalAddress
.Address
[0].Address
[0].sin_port
);
112 /* This should never fail */
113 ASSERT(Connection
->AddressFile
->Port
!= 0xFFFF);
118 UnlockObject(Connection
, OldIrql
);
120 TI_DbgPrint(DEBUG_TCP
,("TCPListen finished %x\n", Status
));
125 BOOLEAN
TCPAbortListenForSocket( PCONNECTION_ENDPOINT Listener
,
126 PCONNECTION_ENDPOINT Connection
) {
127 PLIST_ENTRY ListEntry
;
130 BOOLEAN Found
= FALSE
;
132 LockObject(Listener
, &OldIrql
);
134 ListEntry
= Listener
->ListenRequest
.Flink
;
135 while ( ListEntry
!= &Listener
->ListenRequest
) {
136 Bucket
= CONTAINING_RECORD(ListEntry
, TDI_BUCKET
, Entry
);
138 if( Bucket
->AssociatedEndpoint
== Connection
) {
139 DereferenceObject(Bucket
->AssociatedEndpoint
);
140 RemoveEntryList( &Bucket
->Entry
);
141 ExFreePoolWithTag( Bucket
, TDI_BUCKET_TAG
);
146 ListEntry
= ListEntry
->Flink
;
149 UnlockObject(Listener
, OldIrql
);
154 NTSTATUS
TCPAccept ( PTDI_REQUEST Request
,
155 PCONNECTION_ENDPOINT Listener
,
156 PCONNECTION_ENDPOINT Connection
,
157 PTCP_COMPLETION_ROUTINE Complete
,
164 TI_DbgPrint(DEBUG_TCP
,("TCPAccept started\n"));
166 LockObject(Listener
, &OldIrql
);
168 Status
= TCPServiceListeningSocket( Listener
, Connection
,
169 (PTDI_REQUEST_KERNEL
)Request
);
171 if( Status
== STATUS_PENDING
) {
172 Bucket
= ExAllocatePoolWithTag( NonPagedPool
, sizeof(*Bucket
),
176 ReferenceObject(Connection
);
177 Bucket
->AssociatedEndpoint
= Connection
;
178 Bucket
->Request
.RequestNotifyObject
= Complete
;
179 Bucket
->Request
.RequestContext
= Context
;
180 InsertTailList( &Listener
->ListenRequest
, &Bucket
->Entry
);
182 Status
= STATUS_NO_MEMORY
;
185 UnlockObject(Listener
, OldIrql
);
187 TI_DbgPrint(DEBUG_TCP
,("TCPAccept finished %x\n", Status
));