2454657d00923ac0f1b3ac5691987155a3ca554c
[reactos.git] / lib / drivers / ip / transport / tcp / accept.c
1 /*
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)
7 * REVISIONS:
8 * arty 12/21/2004 Created
9 */
10
11 #include "precomp.h"
12
13 /* Listener->Lock MUST be acquired */
14 NTSTATUS TCPServiceListeningSocket( PCONNECTION_ENDPOINT Listener,
15 PCONNECTION_ENDPOINT Connection,
16 PTDI_REQUEST_KERNEL Request ) {
17 NTSTATUS Status;
18 SOCKADDR_IN OutAddr;
19 OSK_UINT OutAddrLen;
20 PTA_IP_ADDRESS RequestAddressReturn;
21 PTDI_CONNECTION_INFORMATION WhoIsConnecting;
22
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;
29
30 Status = TCPTranslateError
31 ( OskitTCPAccept( Listener->SocketContext,
32 &Connection->SocketContext,
33 Connection,
34 &OutAddr,
35 sizeof(OutAddr),
36 &OutAddrLen,
37 Request->RequestFlags & TDI_QUERY_ACCEPT ? 0 : 1 ) );
38
39 TI_DbgPrint(DEBUG_TCP,("Status %x\n", Status));
40
41 if( NT_SUCCESS(Status) && Status != STATUS_PENDING ) {
42 RequestAddressReturn = WhoIsConnecting->RemoteAddress;
43
44 TI_DbgPrint(DEBUG_TCP,("Copying address to %x (Who %x)\n",
45 RequestAddressReturn, WhoIsConnecting));
46
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);
53
54 TI_DbgPrint(DEBUG_TCP,("Done copying\n"));
55 }
56
57 TI_DbgPrint(DEBUG_TCP,("Status %x\n", Status));
58
59 return Status;
60 }
61
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;
67 KIRQL OldIrql;
68 TA_IP_ADDRESS LocalAddress;
69
70 ASSERT(Connection);
71
72 LockObject(Connection, &OldIrql);
73
74 ASSERT_KM_POINTER(Connection->AddressFile);
75
76 TI_DbgPrint(DEBUG_TCP,("TCPListen started\n"));
77
78 TI_DbgPrint(DEBUG_TCP,("Connection->SocketContext %x\n",
79 Connection->SocketContext));
80
81 AddressToBind.sin_family = AF_INET;
82 memcpy( &AddressToBind.sin_addr,
83 &Connection->AddressFile->Address.Address.IPv4Address,
84 sizeof(AddressToBind.sin_addr) );
85 AddressToBind.sin_port = Connection->AddressFile->Port;
86
87 TI_DbgPrint(DEBUG_TCP,("AddressToBind - %x:%x\n", AddressToBind.sin_addr, AddressToBind.sin_port));
88
89 Status = TCPTranslateError( OskitTCPBind( Connection->SocketContext,
90 &AddressToBind,
91 sizeof(AddressToBind) ) );
92 if (NT_SUCCESS(Status))
93 {
94 /* Check if we had an unspecified port */
95 if (!Connection->AddressFile->Port)
96 {
97 /* We did, so we need to copy back the port */
98 Status = TCPGetSockAddress(Connection, (PTRANSPORT_ADDRESS)&LocalAddress, FALSE);
99 if (NT_SUCCESS(Status))
100 {
101 /* Allocate the port in the port bitmap */
102 Connection->AddressFile->Port = TCPAllocatePort(LocalAddress.Address[0].Address[0].sin_port);
103
104 /* This should never fail */
105 ASSERT(Connection->AddressFile->Port != 0xFFFF);
106 }
107 }
108 }
109
110 if (NT_SUCCESS(Status))
111 Status = TCPTranslateError( OskitTCPListen( Connection->SocketContext, Backlog ) );
112
113 UnlockObject(Connection, OldIrql);
114
115 TI_DbgPrint(DEBUG_TCP,("TCPListen finished %x\n", Status));
116
117 return Status;
118 }
119
120 BOOLEAN TCPAbortListenForSocket( PCONNECTION_ENDPOINT Listener,
121 PCONNECTION_ENDPOINT Connection ) {
122 PLIST_ENTRY ListEntry;
123 PTDI_BUCKET Bucket;
124 KIRQL OldIrql;
125 BOOLEAN Found = FALSE;
126
127 LockObject(Listener, &OldIrql);
128
129 ListEntry = Listener->ListenRequest.Flink;
130 while ( ListEntry != &Listener->ListenRequest ) {
131 Bucket = CONTAINING_RECORD(ListEntry, TDI_BUCKET, Entry);
132
133 if( Bucket->AssociatedEndpoint == Connection ) {
134 DereferenceObject(Bucket->AssociatedEndpoint);
135 RemoveEntryList( &Bucket->Entry );
136 ExFreePoolWithTag( Bucket, TDI_BUCKET_TAG );
137 Found = TRUE;
138 break;
139 }
140
141 ListEntry = ListEntry->Flink;
142 }
143
144 UnlockObject(Listener, OldIrql);
145
146 return Found;
147 }
148
149 NTSTATUS TCPAccept ( PTDI_REQUEST Request,
150 PCONNECTION_ENDPOINT Listener,
151 PCONNECTION_ENDPOINT Connection,
152 PTCP_COMPLETION_ROUTINE Complete,
153 PVOID Context )
154 {
155 NTSTATUS Status;
156 PTDI_BUCKET Bucket;
157 KIRQL OldIrql;
158
159 TI_DbgPrint(DEBUG_TCP,("TCPAccept started\n"));
160
161 LockObject(Listener, &OldIrql);
162
163 Status = TCPServiceListeningSocket( Listener, Connection,
164 (PTDI_REQUEST_KERNEL)Request );
165
166 if( Status == STATUS_PENDING ) {
167 Bucket = ExAllocatePoolWithTag( NonPagedPool, sizeof(*Bucket),
168 TDI_BUCKET_TAG );
169
170 if( Bucket ) {
171 ReferenceObject(Connection);
172 Bucket->AssociatedEndpoint = Connection;
173 Bucket->Request.RequestNotifyObject = Complete;
174 Bucket->Request.RequestContext = Context;
175 InsertTailList( &Listener->ListenRequest, &Bucket->Entry );
176 } else
177 Status = STATUS_NO_MEMORY;
178 }
179
180 UnlockObject(Listener, OldIrql);
181
182 TI_DbgPrint(DEBUG_TCP,("TCPAccept finished %x\n", Status));
183 return Status;
184 }