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 NTSTATUS
TCPServiceListeningSocket( PCONNECTION_ENDPOINT Listener
,
14 PCONNECTION_ENDPOINT Connection
,
15 PTDI_REQUEST_KERNEL Request
) {
19 PTA_IP_ADDRESS RequestAddressReturn
;
20 PTDI_CONNECTION_INFORMATION WhoIsConnecting
;
22 /* Unpack TDI info -- We need the return connection information
23 * struct to return the address so it can be filtered if needed
24 * by WSAAccept -- The returned address will be passed on to
25 * userland after we complete this irp */
26 WhoIsConnecting
= (PTDI_CONNECTION_INFORMATION
)
27 Request
->ReturnConnectionInformation
;
29 Status
= TCPTranslateError
30 ( OskitTCPAccept( Listener
->SocketContext
,
31 &Connection
->SocketContext
,
35 Request
->RequestFlags
& TDI_QUERY_ACCEPT
? 0 : 1 ) );
37 TI_DbgPrint(DEBUG_TCP
,("Status %x\n", Status
));
39 if( NT_SUCCESS(Status
) && Status
!= STATUS_PENDING
) {
40 RequestAddressReturn
= WhoIsConnecting
->RemoteAddress
;
42 TI_DbgPrint(DEBUG_TCP
,("Copying address to %x (Who %x)\n",
43 RequestAddressReturn
, WhoIsConnecting
));
45 RequestAddressReturn
->TAAddressCount
= 1;
46 RequestAddressReturn
->Address
[0].AddressLength
= OutAddrLen
;
48 /* BSD uses the first byte of the sockaddr struct as a length.
49 * Since windows doesn't do that we strip it */
50 RequestAddressReturn
->Address
[0].AddressType
=
51 (OutAddr
.sin_family
>> 8) & 0xff;
53 RtlCopyMemory( &RequestAddressReturn
->Address
[0].Address
,
54 ((PCHAR
)&OutAddr
) + sizeof(USHORT
),
55 sizeof(RequestAddressReturn
->Address
[0].Address
[0]) );
57 TI_DbgPrint(DEBUG_TCP
,("Done copying\n"));
60 TI_DbgPrint(DEBUG_TCP
,("Status %x\n", Status
));
65 /* This listen is on a socket we keep as internal. That socket has the same
66 * lifetime as the address file */
67 NTSTATUS
TCPListen( PCONNECTION_ENDPOINT Connection
, UINT Backlog
) {
68 NTSTATUS Status
= STATUS_SUCCESS
;
69 SOCKADDR_IN AddressToBind
;
71 TI_DbgPrint(DEBUG_TCP
,("TCPListen started\n"));
73 TI_DbgPrint(DEBUG_TCP
,("Connection->SocketContext %x\n",
74 Connection
->SocketContext
));
77 ASSERT_KM_POINTER(Connection
->SocketContext
);
78 ASSERT_KM_POINTER(Connection
->AddressFile
);
80 TcpipRecursiveMutexEnter( &TCPLock
, TRUE
);
82 AddressToBind
.sin_family
= AF_INET
;
83 memcpy( &AddressToBind
.sin_addr
,
84 &Connection
->AddressFile
->Address
.Address
.IPv4Address
,
85 sizeof(AddressToBind
.sin_addr
) );
86 AddressToBind
.sin_port
= Connection
->AddressFile
->Port
;
88 TI_DbgPrint(DEBUG_TCP
,("AddressToBind - %x:%x\n", AddressToBind
.sin_addr
, AddressToBind
.sin_port
));
90 OskitTCPBind( Connection
->SocketContext
,
93 sizeof(AddressToBind
) );
95 Status
= TCPTranslateError( OskitTCPListen( Connection
->SocketContext
,
98 TcpipRecursiveMutexLeave( &TCPLock
);
100 TI_DbgPrint(DEBUG_TCP
,("TCPListen finished %x\n", Status
));
105 VOID
TCPAbortListenForSocket( PCONNECTION_ENDPOINT Listener
,
106 PCONNECTION_ENDPOINT Connection
) {
107 PLIST_ENTRY ListEntry
;
110 TcpipRecursiveMutexEnter( &TCPLock
, TRUE
);
112 ListEntry
= Listener
->ListenRequest
.Flink
;
113 while ( ListEntry
!= &Listener
->ListenRequest
) {
114 Bucket
= CONTAINING_RECORD(ListEntry
, TDI_BUCKET
, Entry
);
115 ListEntry
= ListEntry
->Flink
;
117 if( Bucket
->AssociatedEndpoint
== Connection
) {
118 RemoveEntryList( ListEntry
->Blink
);
119 ExFreePool( Bucket
);
123 TcpipRecursiveMutexLeave( &TCPLock
);
127 ( PTDI_REQUEST Request
,
128 PCONNECTION_ENDPOINT Listener
,
129 PCONNECTION_ENDPOINT Connection
,
130 PTCP_COMPLETION_ROUTINE Complete
,
135 TI_DbgPrint(DEBUG_TCP
,("TCPAccept started\n"));
137 TcpipRecursiveMutexEnter( &TCPLock
, TRUE
);
139 Status
= TCPServiceListeningSocket( Listener
, Connection
,
140 (PTDI_REQUEST_KERNEL
)Request
);
142 if( Status
== STATUS_PENDING
) {
143 Bucket
= ExAllocatePool( NonPagedPool
, sizeof(*Bucket
) );
144 Bucket
->AssociatedEndpoint
= Connection
;
145 Bucket
->Request
.RequestNotifyObject
= Complete
;
146 Bucket
->Request
.RequestContext
= Context
;
147 InsertHeadList( &Listener
->ListenRequest
, &Bucket
->Entry
);
150 TcpipRecursiveMutexLeave( &TCPLock
);
152 TI_DbgPrint(DEBUG_TCP
,("TCPAccept finished %x\n", Status
));