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
->SocketContext
,
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
= OutAddrLen
;
50 /* BSD uses the first byte of the sockaddr struct as a length.
51 * Since windows doesn't do that we strip it */
52 RequestAddressReturn
->Address
[0].AddressType
=
53 (OutAddr
.sin_family
>> 8) & 0xff;
55 RtlCopyMemory( &RequestAddressReturn
->Address
[0].Address
,
56 ((PCHAR
)&OutAddr
) + sizeof(USHORT
),
57 sizeof(RequestAddressReturn
->Address
[0].Address
[0]) );
59 TI_DbgPrint(DEBUG_TCP
,("Done copying\n"));
62 TI_DbgPrint(DEBUG_TCP
,("Status %x\n", Status
));
67 /* This listen is on a socket we keep as internal. That socket has the same
68 * lifetime as the address file */
69 NTSTATUS
TCPListen( PCONNECTION_ENDPOINT Connection
, UINT Backlog
) {
70 NTSTATUS Status
= STATUS_SUCCESS
;
71 SOCKADDR_IN AddressToBind
;
75 ASSERT_KM_POINTER(Connection
->AddressFile
);
77 LockObject(Connection
, &OldIrql
);
79 TI_DbgPrint(DEBUG_TCP
,("TCPListen started\n"));
81 TI_DbgPrint(DEBUG_TCP
,("Connection->SocketContext %x\n",
82 Connection
->SocketContext
));
84 AddressToBind
.sin_family
= AF_INET
;
85 memcpy( &AddressToBind
.sin_addr
,
86 &Connection
->AddressFile
->Address
.Address
.IPv4Address
,
87 sizeof(AddressToBind
.sin_addr
) );
88 AddressToBind
.sin_port
= Connection
->AddressFile
->Port
;
90 TI_DbgPrint(DEBUG_TCP
,("AddressToBind - %x:%x\n", AddressToBind
.sin_addr
, AddressToBind
.sin_port
));
92 Status
= TCPTranslateError( OskitTCPBind( Connection
->SocketContext
,
94 sizeof(AddressToBind
) ) );
96 if (NT_SUCCESS(Status
))
97 Status
= TCPTranslateError( OskitTCPListen( Connection
->SocketContext
, Backlog
) );
99 UnlockObject(Connection
, OldIrql
);
101 TI_DbgPrint(DEBUG_TCP
,("TCPListen finished %x\n", Status
));
106 BOOLEAN
TCPAbortListenForSocket( PCONNECTION_ENDPOINT Listener
,
107 PCONNECTION_ENDPOINT Connection
) {
108 PLIST_ENTRY ListEntry
;
111 BOOLEAN Found
= FALSE
;
113 LockObject(Listener
, &OldIrql
);
115 ListEntry
= Listener
->ListenRequest
.Flink
;
116 while ( ListEntry
!= &Listener
->ListenRequest
) {
117 Bucket
= CONTAINING_RECORD(ListEntry
, TDI_BUCKET
, Entry
);
119 if( Bucket
->AssociatedEndpoint
== Connection
) {
120 RemoveEntryList( &Bucket
->Entry
);
121 ExFreePoolWithTag( Bucket
, TDI_BUCKET_TAG
);
126 ListEntry
= ListEntry
->Flink
;
129 UnlockObject(Listener
, OldIrql
);
134 NTSTATUS
TCPAccept ( PTDI_REQUEST Request
,
135 PCONNECTION_ENDPOINT Listener
,
136 PCONNECTION_ENDPOINT Connection
,
137 PTCP_COMPLETION_ROUTINE Complete
,
144 TI_DbgPrint(DEBUG_TCP
,("TCPAccept started\n"));
146 LockObject(Listener
, &OldIrql
);
148 Status
= TCPServiceListeningSocket( Listener
, Connection
,
149 (PTDI_REQUEST_KERNEL
)Request
);
151 if( Status
== STATUS_PENDING
) {
152 Bucket
= ExAllocatePoolWithTag( NonPagedPool
, sizeof(*Bucket
),
156 ReferenceObject(Connection
);
157 Bucket
->AssociatedEndpoint
= Connection
;
158 Bucket
->Request
.RequestNotifyObject
= Complete
;
159 Bucket
->Request
.RequestContext
= Context
;
160 InsertTailList( &Listener
->ListenRequest
, &Bucket
->Entry
);
162 Status
= STATUS_NO_MEMORY
;
165 UnlockObject(Listener
, OldIrql
);
167 TI_DbgPrint(DEBUG_TCP
,("TCPAccept finished %x\n", Status
));