Send delayed ACKs after 200ms instead of 2500ms
[reactos.git] / reactos / drivers / lib / 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 NTSTATUS TCPServiceListeningSocket( PCONNECTION_ENDPOINT Listener,
14 PCONNECTION_ENDPOINT Connection,
15 PTDI_REQUEST_KERNEL Request ) {
16 NTSTATUS Status;
17 SOCKADDR_IN OutAddr;
18 OSK_UINT OutAddrLen;
19 PTA_IP_ADDRESS RequestAddressReturn;
20 PTDI_CONNECTION_INFORMATION WhoIsConnecting;
21
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;
28
29 Status = TCPTranslateError
30 ( OskitTCPAccept( Listener->SocketContext,
31 &Connection->SocketContext,
32 &OutAddr,
33 sizeof(OutAddr),
34 &OutAddrLen,
35 Request->RequestFlags & TDI_QUERY_ACCEPT ? 0 : 1 ) );
36
37 TI_DbgPrint(DEBUG_TCP,("Status %x\n", Status));
38
39 if( NT_SUCCESS(Status) && Status != STATUS_PENDING ) {
40 RequestAddressReturn = WhoIsConnecting->RemoteAddress;
41
42 TI_DbgPrint(DEBUG_TCP,("Copying address to %x (Who %x)\n",
43 RequestAddressReturn, WhoIsConnecting));
44
45 RequestAddressReturn->TAAddressCount = 1;
46 RequestAddressReturn->Address[0].AddressLength = OutAddrLen;
47
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;
52
53 RtlCopyMemory( &RequestAddressReturn->Address[0].Address,
54 ((PCHAR)&OutAddr) + sizeof(USHORT),
55 sizeof(RequestAddressReturn->Address[0].Address[0]) );
56
57 TI_DbgPrint(DEBUG_TCP,("Done copying\n"));
58 }
59
60 TI_DbgPrint(DEBUG_TCP,("Status %x\n", Status));
61
62 return Status;
63 }
64
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;
70
71 TI_DbgPrint(DEBUG_TCP,("TCPListen started\n"));
72
73 TI_DbgPrint(DEBUG_TCP,("Connection->SocketContext %x\n",
74 Connection->SocketContext));
75
76 ASSERT(Connection);
77 ASSERT_KM_POINTER(Connection->SocketContext);
78 ASSERT_KM_POINTER(Connection->AddressFile);
79
80 TcpipRecursiveMutexEnter( &TCPLock, TRUE );
81
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;
87
88 TI_DbgPrint(DEBUG_TCP,("AddressToBind - %x:%x\n", AddressToBind.sin_addr, AddressToBind.sin_port));
89
90 OskitTCPBind( Connection->SocketContext,
91 Connection,
92 &AddressToBind,
93 sizeof(AddressToBind) );
94
95 Status = TCPTranslateError( OskitTCPListen( Connection->SocketContext,
96 Backlog ) );
97
98 TcpipRecursiveMutexLeave( &TCPLock );
99
100 TI_DbgPrint(DEBUG_TCP,("TCPListen finished %x\n", Status));
101
102 return Status;
103 }
104
105 VOID TCPAbortListenForSocket( PCONNECTION_ENDPOINT Listener,
106 PCONNECTION_ENDPOINT Connection ) {
107 PLIST_ENTRY ListEntry;
108 PTDI_BUCKET Bucket;
109
110 TcpipRecursiveMutexEnter( &TCPLock, TRUE );
111
112 ListEntry = Listener->ListenRequest.Flink;
113 while ( ListEntry != &Listener->ListenRequest ) {
114 Bucket = CONTAINING_RECORD(ListEntry, TDI_BUCKET, Entry);
115 ListEntry = ListEntry->Flink;
116
117 if( Bucket->AssociatedEndpoint == Connection ) {
118 #ifdef MEMTRACK
119 UntrackFL( __FILE__, __LINE__, Bucket->Request.RequestContext );
120 #endif
121 RemoveEntryList( ListEntry->Blink );
122 ExFreePool( Bucket );
123 }
124 }
125
126 TcpipRecursiveMutexLeave( &TCPLock );
127 }
128
129 NTSTATUS TCPAccept
130 ( PTDI_REQUEST Request,
131 PCONNECTION_ENDPOINT Listener,
132 PCONNECTION_ENDPOINT Connection,
133 PTCP_COMPLETION_ROUTINE Complete,
134 PVOID Context ) {
135 NTSTATUS Status;
136 PTDI_BUCKET Bucket;
137
138 TI_DbgPrint(DEBUG_TCP,("TCPAccept started\n"));
139
140 TcpipRecursiveMutexEnter( &TCPLock, TRUE );
141
142 Status = TCPServiceListeningSocket( Listener, Connection,
143 (PTDI_REQUEST_KERNEL)Request );
144
145 if( Status == STATUS_PENDING ) {
146 Bucket = ExAllocatePool( NonPagedPool, sizeof(*Bucket) );
147 Bucket->AssociatedEndpoint = Connection;
148 Bucket->Request.RequestNotifyObject = Complete;
149 Bucket->Request.RequestContext = Context;
150 InsertHeadList( &Listener->ListenRequest, &Bucket->Entry );
151 }
152
153 TcpipRecursiveMutexLeave( &TCPLock );
154
155 TI_DbgPrint(DEBUG_TCP,("TCPAccept finished %x\n", Status));
156 return Status;
157 }