Send delayed ACKs after 200ms instead of 2500ms
[reactos.git] / reactos / drivers / lib / ip / transport / tcp / tcp.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS TCP/IP protocol driver
4 * FILE: transport/tcp/tcp.c
5 * PURPOSE: Transmission Control Protocol
6 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
7 * Art Yerkes (arty@users.sf.net)
8 * REVISIONS:
9 * CSH 01/08-2000 Created
10 * arty 12/21/2004 Added accept
11 */
12
13 #include "precomp.h"
14
15 LONG TCP_IPIdentification = 0;
16 static BOOLEAN TCPInitialized = FALSE;
17 static NPAGED_LOOKASIDE_LIST TCPSegmentList;
18 LIST_ENTRY SignalledConnections;
19 LIST_ENTRY SleepingThreadsList;
20 FAST_MUTEX SleepingThreadsLock;
21 RECURSIVE_MUTEX TCPLock;
22 PORT_SET TCPPorts;
23
24 static VOID HandleSignalledConnection( PCONNECTION_ENDPOINT Connection,
25 ULONG NewState ) {
26 NTSTATUS Status = STATUS_SUCCESS;
27 PTCP_COMPLETION_ROUTINE Complete;
28 PTDI_BUCKET Bucket;
29 PLIST_ENTRY Entry;
30 PIRP Irp;
31 PMDL Mdl;
32
33 TI_DbgPrint(MID_TRACE,("Handling signalled state on %x (%x)\n",
34 Connection, Connection->SocketContext));
35
36 /* Things that can happen when we try the initial connection */
37 if( NewState & SEL_CONNECT ) {
38 while( !IsListEmpty( &Connection->ConnectRequest ) ) {
39 Connection->State |= NewState;
40 Entry = RemoveHeadList( &Connection->ConnectRequest );
41 TI_DbgPrint(DEBUG_TCP, ("Connect Event\n"));
42
43 Bucket = CONTAINING_RECORD( Entry, TDI_BUCKET, Entry );
44 Complete = Bucket->Request.RequestNotifyObject;
45 TI_DbgPrint(DEBUG_TCP,
46 ("Completing Request %x\n", Bucket->Request));
47
48 if( (NewState & (SEL_CONNECT | SEL_FIN)) ==
49 (SEL_CONNECT | SEL_FIN) )
50 Status = STATUS_CONNECTION_REFUSED;
51 else
52 Status = STATUS_SUCCESS;
53
54 Complete( Bucket->Request.RequestContext, Status, 0 );
55
56 /* Frees the bucket allocated in TCPConnect */
57 PoolFreeBuffer( Bucket );
58 }
59 }
60
61 if( NewState & SEL_ACCEPT ) {
62 /* Handle readable on a listening socket --
63 * TODO: Implement filtering
64 */
65
66 TI_DbgPrint(DEBUG_TCP,("Accepting new connection on %x (Queue: %s)\n",
67 Connection,
68 IsListEmpty(&Connection->ListenRequest) ?
69 "empty" : "nonempty"));
70
71 while( !IsListEmpty( &Connection->ListenRequest ) ) {
72 PIO_STACK_LOCATION IrpSp;
73
74 Entry = RemoveHeadList( &Connection->ListenRequest );
75 Bucket = CONTAINING_RECORD( Entry, TDI_BUCKET, Entry );
76 Complete = Bucket->Request.RequestNotifyObject;
77
78 Irp = Bucket->Request.RequestContext;
79 IrpSp = IoGetCurrentIrpStackLocation( Irp );
80
81 TI_DbgPrint(DEBUG_TCP,("Getting the socket\n"));
82 Status = TCPServiceListeningSocket
83 ( Connection->AddressFile->Listener,
84 Bucket->AssociatedEndpoint,
85 (PTDI_REQUEST_KERNEL)&IrpSp->Parameters );
86
87 TI_DbgPrint(DEBUG_TCP,("Socket: Status: %x\n"));
88
89 if( Status == STATUS_PENDING ) {
90 InsertHeadList( &Connection->ListenRequest, &Bucket->Entry );
91 break;
92 } else
93 Complete( Bucket->Request.RequestContext, Status, 0 );
94 }
95 }
96
97 /* Things that happen after we're connected */
98 if( NewState & SEL_READ ) {
99 TI_DbgPrint(DEBUG_TCP,("Readable: irp list %s\n",
100 IsListEmpty(&Connection->ReceiveRequest) ?
101 "empty" : "nonempty"));
102
103 while( !IsListEmpty( &Connection->ReceiveRequest ) ) {
104 OSK_UINT RecvLen = 0, Received = 0;
105 OSK_PCHAR RecvBuffer = 0;
106
107 Entry = RemoveHeadList( &Connection->ReceiveRequest );
108 Bucket = CONTAINING_RECORD( Entry, TDI_BUCKET, Entry );
109 Complete = Bucket->Request.RequestNotifyObject;
110
111 Irp = Bucket->Request.RequestContext;
112 Mdl = Irp->MdlAddress;
113
114 TI_DbgPrint(DEBUG_TCP,
115 ("Getting the user buffer from %x\n", Mdl));
116
117 NdisQueryBuffer( Mdl, &RecvBuffer, &RecvLen );
118
119 TI_DbgPrint(DEBUG_TCP,
120 ("Reading %d bytes to %x\n", RecvLen, RecvBuffer));
121
122 TI_DbgPrint(DEBUG_TCP, ("Connection: %x\n", Connection));
123 TI_DbgPrint
124 (DEBUG_TCP,
125 ("Connection->SocketContext: %x\n",
126 Connection->SocketContext));
127 TI_DbgPrint(DEBUG_TCP, ("RecvBuffer: %x\n", RecvBuffer));
128
129 Status = TCPTranslateError
130 ( OskitTCPRecv( Connection->SocketContext,
131 RecvBuffer,
132 RecvLen,
133 &Received,
134 0 ) );
135
136 TI_DbgPrint(DEBUG_TCP,("TCP Bytes: %d\n", Received));
137
138 if( Status == STATUS_SUCCESS ) {
139 TI_DbgPrint(DEBUG_TCP,("Received %d bytes with status %x\n",
140 Received, Status));
141
142 Complete( Bucket->Request.RequestContext,
143 STATUS_SUCCESS, Received );
144 } else if( Status == STATUS_PENDING ) {
145 InsertHeadList
146 ( &Connection->ReceiveRequest, &Bucket->Entry );
147 break;
148 } else {
149 TI_DbgPrint(DEBUG_TCP,
150 ("Completing Receive request: %x %x\n",
151 Bucket->Request, Status));
152 Complete( Bucket->Request.RequestContext, Status, 0 );
153 }
154 }
155 }
156
157 if( NewState & SEL_FIN ) {
158 PLIST_ENTRY ListsToErase[4];
159 NTSTATUS IrpStatus[4];
160 UINT i;
161
162 TI_DbgPrint(DEBUG_TCP, ("EOF From socket\n"));
163
164 ListsToErase[0] = &Connection->ReceiveRequest;
165 IrpStatus [0] = STATUS_SUCCESS;
166 ListsToErase[1] = &Connection->ListenRequest;
167 IrpStatus [1] = STATUS_UNSUCCESSFUL;
168 ListsToErase[2] = &Connection->ConnectRequest;
169 IrpStatus [2] = STATUS_UNSUCCESSFUL;
170 ListsToErase[3] = 0;
171
172 for( i = 0; ListsToErase[i]; i++ ) {
173 while( !IsListEmpty( ListsToErase[i] ) ) {
174 Entry = RemoveHeadList( ListsToErase[i] );
175 Bucket = CONTAINING_RECORD( Entry, TDI_BUCKET, Entry );
176 Complete = Bucket->Request.RequestNotifyObject;
177 Complete( Bucket->Request.RequestContext, STATUS_SUCCESS, 0 );
178 }
179 }
180 }
181
182 Connection->Signalled = FALSE;
183 }
184
185 VOID DrainSignals() {
186 PCONNECTION_ENDPOINT Connection;
187 PLIST_ENTRY ListEntry;
188
189 while( !IsListEmpty( &SignalledConnections ) ) {
190 ListEntry = RemoveHeadList( &SignalledConnections );
191 Connection = CONTAINING_RECORD( ListEntry, CONNECTION_ENDPOINT,
192 SignalList );
193 HandleSignalledConnection( Connection, Connection->SignalState );
194 }
195 }
196
197 PCONNECTION_ENDPOINT TCPAllocateConnectionEndpoint( PVOID ClientContext ) {
198 PCONNECTION_ENDPOINT Connection =
199 ExAllocatePool(NonPagedPool, sizeof(CONNECTION_ENDPOINT));
200 if (!Connection)
201 return Connection;
202
203 TI_DbgPrint(DEBUG_CPOINT, ("Connection point file object allocated at (0x%X).\n", Connection));
204
205 RtlZeroMemory(Connection, sizeof(CONNECTION_ENDPOINT));
206
207 /* Initialize spin lock that protects the connection endpoint file object */
208 TcpipInitializeSpinLock(&Connection->Lock);
209 InitializeListHead(&Connection->ConnectRequest);
210 InitializeListHead(&Connection->ListenRequest);
211 InitializeListHead(&Connection->ReceiveRequest);
212
213 /* Save client context pointer */
214 Connection->ClientContext = ClientContext;
215
216 return Connection;
217 }
218
219 VOID TCPFreeConnectionEndpoint( PCONNECTION_ENDPOINT Connection ) {
220 TI_DbgPrint(MAX_TRACE,("FIXME: Cancel all pending requests\n"));
221 /* XXX Cancel all pending requests */
222 ExFreePool( Connection );
223 }
224
225 NTSTATUS TCPSocket( PCONNECTION_ENDPOINT Connection,
226 UINT Family, UINT Type, UINT Proto ) {
227 NTSTATUS Status;
228
229 TI_DbgPrint(DEBUG_TCP,("Called: Connection %x, Family %d, Type %d, "
230 "Proto %d\n",
231 Connection, Family, Type, Proto));
232
233 TcpipRecursiveMutexEnter( &TCPLock, TRUE );
234 Status = TCPTranslateError( OskitTCPSocket( Connection,
235 &Connection->SocketContext,
236 Family,
237 Type,
238 Proto ) );
239
240 ASSERT_KM_POINTER(Connection->SocketContext);
241
242 TI_DbgPrint(DEBUG_TCP,("Connection->SocketContext %x\n",
243 Connection->SocketContext));
244
245 TcpipRecursiveMutexLeave( &TCPLock );
246
247 return Status;
248 }
249
250 VOID TCPReceive(PIP_INTERFACE Interface, PIP_PACKET IPPacket)
251 /*
252 * FUNCTION: Receives and queues TCP data
253 * ARGUMENTS:
254 * IPPacket = Pointer to an IP packet that was received
255 * NOTES:
256 * This is the low level interface for receiving TCP data
257 */
258 {
259 TI_DbgPrint(DEBUG_TCP,("Sending packet %d (%d) to oskit\n",
260 IPPacket->TotalSize,
261 IPPacket->HeaderSize));
262
263 TcpipRecursiveMutexEnter( &TCPLock, TRUE );
264
265 OskitTCPReceiveDatagram( IPPacket->Header,
266 IPPacket->TotalSize,
267 IPPacket->HeaderSize );
268
269 DrainSignals();
270
271 TcpipRecursiveMutexLeave( &TCPLock );
272 }
273
274 /* event.c */
275 int TCPSocketState( void *ClientData,
276 void *WhichSocket,
277 void *WhichConnection,
278 OSK_UINT NewState );
279
280 int TCPPacketSend( void *ClientData,
281 OSK_PCHAR Data,
282 OSK_UINT Len );
283
284 POSK_IFADDR TCPFindInterface( void *ClientData,
285 OSK_UINT AddrType,
286 OSK_UINT FindType,
287 OSK_SOCKADDR *ReqAddr );
288
289 void *TCPMalloc( void *ClientData,
290 OSK_UINT bytes, OSK_PCHAR file, OSK_UINT line );
291 void TCPFree( void *ClientData,
292 void *data, OSK_PCHAR file, OSK_UINT line );
293
294 int TCPSleep( void *ClientData, void *token, int priority, char *msg,
295 int tmio );
296
297 void TCPWakeup( void *ClientData, void *token );
298
299 OSKITTCP_EVENT_HANDLERS EventHandlers = {
300 NULL, /* Client Data */
301 TCPSocketState, /* SocketState */
302 TCPPacketSend, /* PacketSend */
303 TCPFindInterface, /* FindInterface */
304 TCPMalloc, /* Malloc */
305 TCPFree, /* Free */
306 TCPSleep, /* Sleep */
307 TCPWakeup /* Wakeup */
308 };
309
310 static KEVENT TimerLoopEvent;
311 static HANDLE TimerThreadHandle;
312
313 /*
314 * We are running 2 timers here, one with a 200ms interval (fast) and the other
315 * with a 500ms interval (slow). So we need to time out at 200, 400, 500, 600,
316 * 800, 1000 and process the "fast" events at 200, 400, 600, 800, 1000 and the
317 * "slow" events at 500 and 1000.
318 */
319 static VOID DDKAPI
320 TimerThread(PVOID Context)
321 {
322 LARGE_INTEGER Timeout;
323 NTSTATUS Status;
324 unsigned Current, NextFast, NextSlow, Next;
325
326 Current = 0;
327 Next = 0;
328 NextFast = 0;
329 NextSlow = 0;
330 while ( 1 ) {
331 if (Next == NextFast) {
332 NextFast += 2;
333 }
334 if (Next == NextSlow) {
335 NextSlow += 5;
336 }
337 Next = min(NextFast, NextSlow);
338 Timeout.QuadPart = (LONGLONG) (Next - Current) * -1000000; /* 100 ms */
339 Status = KeWaitForSingleObject(&TimerLoopEvent, Executive, KernelMode,
340 FALSE, &Timeout);
341 if (STATUS_SUCCESS == Status) {
342 PsTerminateSystemThread(STATUS_SUCCESS);
343 }
344 ASSERT(STATUS_TIMEOUT == Status);
345
346 TcpipRecursiveMutexEnter( &TCPLock, TRUE );
347 TimerOskitTCP( Next == NextFast, Next == NextSlow );
348 if (Next == NextSlow) {
349 DrainSignals();
350 }
351 TcpipRecursiveMutexLeave( &TCPLock );
352
353 Current = Next;
354 if (10 <= Current) {
355 Current = 0;
356 Next = 0;
357 NextFast = 0;
358 NextSlow = 0;
359 }
360 }
361 }
362
363 static VOID
364 StartTimer(VOID)
365 {
366 KeInitializeEvent(&TimerLoopEvent, NotificationEvent, FALSE);
367 PsCreateSystemThread(&TimerThreadHandle, THREAD_ALL_ACCESS, 0, 0, 0,
368 TimerThread, NULL);
369 }
370
371
372 NTSTATUS TCPStartup(VOID)
373 /*
374 * FUNCTION: Initializes the TCP subsystem
375 * RETURNS:
376 * Status of operation
377 */
378 {
379 TcpipRecursiveMutexInit( &TCPLock );
380 ExInitializeFastMutex( &SleepingThreadsLock );
381 InitializeListHead( &SleepingThreadsList );
382 InitializeListHead( &SignalledConnections );
383
384 PortsStartup( &TCPPorts, 1, 0xfffe );
385
386 RegisterOskitTCPEventHandlers( &EventHandlers );
387 InitOskitTCP();
388
389 /* Register this protocol with IP layer */
390 IPRegisterProtocol(IPPROTO_TCP, TCPReceive);
391
392 ExInitializeNPagedLookasideList(
393 &TCPSegmentList, /* Lookaside list */
394 NULL, /* Allocate routine */
395 NULL, /* Free routine */
396 0, /* Flags */
397 sizeof(TCP_SEGMENT), /* Size of each entry */
398 TAG('T','C','P','S'), /* Tag */
399 0); /* Depth */
400
401 StartTimer();
402
403 TCPInitialized = TRUE;
404
405 return STATUS_SUCCESS;
406 }
407
408
409 NTSTATUS TCPShutdown(VOID)
410 /*
411 * FUNCTION: Shuts down the TCP subsystem
412 * RETURNS:
413 * Status of operation
414 */
415 {
416 LARGE_INTEGER WaitForThread;
417
418 if (!TCPInitialized)
419 return STATUS_SUCCESS;
420
421 WaitForThread.QuadPart = -2500000; /* 250 ms */
422 KeSetEvent(&TimerLoopEvent, IO_NO_INCREMENT, TRUE);
423 KeWaitForSingleObject(&TimerThreadHandle, Executive, KernelMode,
424 FALSE, &WaitForThread);
425
426 /* Deregister this protocol with IP layer */
427 IPRegisterProtocol(IPPROTO_TCP, NULL);
428
429 ExDeleteNPagedLookasideList(&TCPSegmentList);
430
431 TCPInitialized = FALSE;
432
433 DeinitOskitTCP();
434
435 PortsShutdown( &TCPPorts );
436
437 return STATUS_SUCCESS;
438 }
439
440 NTSTATUS TCPTranslateError( int OskitError ) {
441 NTSTATUS Status = STATUS_UNSUCCESSFUL;
442
443 switch( OskitError ) {
444 case 0: Status = STATUS_SUCCESS; break;
445 case OSK_EADDRNOTAVAIL:
446 case OSK_EAFNOSUPPORT: Status = STATUS_INVALID_CONNECTION; break;
447 case OSK_ECONNREFUSED:
448 case OSK_ECONNRESET: Status = STATUS_REMOTE_NOT_LISTENING; break;
449 case OSK_EINPROGRESS:
450 case OSK_EAGAIN: Status = STATUS_PENDING; break;
451 default: Status = STATUS_INVALID_CONNECTION; break;
452 }
453
454 TI_DbgPrint(DEBUG_TCP,("Error %d -> %x\n", OskitError, Status));
455 return Status;
456 }
457
458 NTSTATUS TCPConnect
459 ( PCONNECTION_ENDPOINT Connection,
460 PTDI_CONNECTION_INFORMATION ConnInfo,
461 PTDI_CONNECTION_INFORMATION ReturnInfo,
462 PTCP_COMPLETION_ROUTINE Complete,
463 PVOID Context ) {
464 NTSTATUS Status;
465 SOCKADDR_IN AddressToConnect = { 0 }, AddressToBind = { 0 };
466 IP_ADDRESS RemoteAddress;
467 USHORT RemotePort;
468 PTDI_BUCKET Bucket;
469
470 DbgPrint("TCPConnect: Called\n");
471
472 Bucket = ExAllocatePool( NonPagedPool, sizeof(*Bucket) );
473 if( !Bucket ) return STATUS_NO_MEMORY;
474
475 TcpipRecursiveMutexEnter( &TCPLock, TRUE );
476
477 /* Freed in TCPSocketState */
478 Bucket->Request.RequestNotifyObject = (PVOID)Complete;
479 Bucket->Request.RequestContext = Context;
480
481 InsertHeadList( &Connection->ConnectRequest, &Bucket->Entry );
482
483 Status = AddrBuildAddress
484 ((PTRANSPORT_ADDRESS)ConnInfo->RemoteAddress,
485 &RemoteAddress,
486 &RemotePort);
487
488 DbgPrint("Connecting to address %x:%x\n",
489 RemoteAddress.Address.IPv4Address,
490 RemotePort);
491
492 if (!NT_SUCCESS(Status)) {
493 TI_DbgPrint(DEBUG_TCP, ("Could not AddrBuildAddress in TCPConnect\n"));
494 return Status;
495 }
496
497 AddressToConnect.sin_family = AF_INET;
498 AddressToBind = AddressToConnect;
499
500 OskitTCPBind( Connection->SocketContext,
501 Connection,
502 &AddressToBind,
503 sizeof(AddressToBind) );
504
505 memcpy( &AddressToConnect.sin_addr,
506 &RemoteAddress.Address.IPv4Address,
507 sizeof(AddressToConnect.sin_addr) );
508 AddressToConnect.sin_port = RemotePort;
509
510 Status = TCPTranslateError
511 ( OskitTCPConnect( Connection->SocketContext,
512 Connection,
513 &AddressToConnect,
514 sizeof(AddressToConnect) ) );
515
516 TcpipRecursiveMutexLeave( &TCPLock );
517
518 if( Status == OSK_EINPROGRESS )
519 return STATUS_PENDING;
520 else
521 return Status;
522 }
523
524 NTSTATUS TCPDisconnect
525 ( PCONNECTION_ENDPOINT Connection,
526 UINT Flags,
527 PTDI_CONNECTION_INFORMATION ConnInfo,
528 PTDI_CONNECTION_INFORMATION ReturnInfo,
529 PTCP_COMPLETION_ROUTINE Complete,
530 PVOID Context ) {
531 NTSTATUS Status;
532
533 TI_DbgPrint(DEBUG_TCP,("started\n"));
534
535 TcpipRecursiveMutexEnter( &TCPLock, TRUE );
536
537 switch( Flags & (TDI_DISCONNECT_ABORT | TDI_DISCONNECT_RELEASE) ) {
538 case 0:
539 case TDI_DISCONNECT_ABORT:
540 Flags = 0;
541 break;
542
543 case TDI_DISCONNECT_ABORT | TDI_DISCONNECT_RELEASE:
544 Flags = 2;
545 break;
546
547 case TDI_DISCONNECT_RELEASE:
548 Flags = 1;
549 break;
550 }
551
552 Status = TCPTranslateError
553 ( OskitTCPShutdown( Connection->SocketContext, Flags ) );
554
555 TcpipRecursiveMutexLeave( &TCPLock );
556
557 TI_DbgPrint(DEBUG_TCP,("finished %x\n", Status));
558
559 return Status;
560 }
561
562 NTSTATUS TCPClose
563 ( PCONNECTION_ENDPOINT Connection ) {
564 NTSTATUS Status;
565
566 TI_DbgPrint(DEBUG_TCP,("TCPClose started\n"));
567
568 TcpipRecursiveMutexEnter( &TCPLock, TRUE );
569
570 Status = TCPTranslateError( OskitTCPClose( Connection->SocketContext ) );
571
572 /* Make our code remove all pending IRPs */
573 Connection->State |= SEL_FIN;
574 DrainSignals();
575
576 TcpipRecursiveMutexLeave( &TCPLock );
577
578 TI_DbgPrint(DEBUG_TCP,("TCPClose finished %x\n", Status));
579
580 return Status;
581 }
582
583 NTSTATUS TCPReceiveData
584 ( PCONNECTION_ENDPOINT Connection,
585 PNDIS_BUFFER Buffer,
586 ULONG ReceiveLength,
587 PULONG BytesReceived,
588 ULONG ReceiveFlags,
589 PTCP_COMPLETION_ROUTINE Complete,
590 PVOID Context ) {
591 OSK_PCHAR DataBuffer;
592 UINT DataLen, Received = 0;
593 NTSTATUS Status;
594 PTDI_BUCKET Bucket;
595
596 TI_DbgPrint(DEBUG_TCP,("Called for %d bytes (on socket %x)\n",
597 ReceiveLength, Connection->SocketContext));
598
599 ASSERT_KM_POINTER(Connection->SocketContext);
600
601 TcpipRecursiveMutexEnter( &TCPLock, TRUE );
602
603 NdisQueryBuffer( Buffer, &DataBuffer, &DataLen );
604
605 TI_DbgPrint(DEBUG_TCP,("TCP>|< Got an MDL %x (%x:%d)\n", Buffer, DataBuffer, DataLen));
606
607 Status = TCPTranslateError
608 ( OskitTCPRecv
609 ( Connection->SocketContext,
610 DataBuffer,
611 DataLen,
612 &Received,
613 ReceiveFlags ) );
614
615 TI_DbgPrint(DEBUG_TCP,("OskitTCPReceive: %x, %d\n", Status, Received));
616
617 /* Keep this request around ... there was no data yet */
618 if( Status == STATUS_PENDING ) {
619 /* Freed in TCPSocketState */
620 Bucket = ExAllocatePool( NonPagedPool, sizeof(*Bucket) );
621 if( !Bucket ) {
622 TI_DbgPrint(DEBUG_TCP,("Failed to allocate bucket\n"));
623 TcpipRecursiveMutexLeave( &TCPLock );
624 return STATUS_NO_MEMORY;
625 }
626
627 Bucket->Request.RequestNotifyObject = Complete;
628 Bucket->Request.RequestContext = Context;
629 *BytesReceived = 0;
630
631 InsertHeadList( &Connection->ReceiveRequest, &Bucket->Entry );
632 Status = STATUS_PENDING;
633 TI_DbgPrint(DEBUG_TCP,("Queued read irp\n"));
634 } else {
635 TI_DbgPrint(DEBUG_TCP,("Got status %x, bytes %d\n", Status, Received));
636 *BytesReceived = Received;
637 }
638
639 TcpipRecursiveMutexLeave( &TCPLock );
640
641 TI_DbgPrint(DEBUG_TCP,("Status %x\n", Status));
642
643 return Status;
644 }
645
646 NTSTATUS TCPSendData
647 ( PCONNECTION_ENDPOINT Connection,
648 PCHAR BufferData,
649 ULONG PacketSize,
650 PULONG DataUsed,
651 ULONG Flags) {
652 NTSTATUS Status;
653
654 ASSERT_KM_POINTER(Connection->SocketContext);
655
656 TcpipRecursiveMutexEnter( &TCPLock, TRUE );
657
658 TI_DbgPrint(DEBUG_TCP,("Connection = %x\n", Connection));
659 TI_DbgPrint(DEBUG_TCP,("Connection->SocketContext = %x\n",
660 Connection->SocketContext));
661
662 Status = OskitTCPSend( Connection->SocketContext,
663 (OSK_PCHAR)BufferData, PacketSize,
664 (PUINT)DataUsed, 0 );
665
666 TcpipRecursiveMutexLeave( &TCPLock );
667
668 return Status;
669 }
670
671 VOID TCPTimeout(VOID) {
672 /* Now handled by TimerThread */
673 }
674
675 UINT TCPAllocatePort( UINT HintPort ) {
676 if( HintPort ) {
677 if( AllocatePort( &TCPPorts, HintPort ) ) return HintPort;
678 else {
679 TI_DbgPrint
680 (MID_TRACE,("We got a hint port but couldn't allocate it\n"));
681 return (UINT)-1;
682 }
683 } else return AllocatePortFromRange( &TCPPorts, 1024, 5000 );
684 }
685
686 VOID TCPFreePort( UINT Port ) {
687 DeallocatePort( &TCPPorts, Port );
688 }
689
690 NTSTATUS TCPGetPeerAddress
691 ( PCONNECTION_ENDPOINT Connection,
692 PTRANSPORT_ADDRESS Address ) {
693 OSK_UINT LocalAddress, RemoteAddress;
694 OSK_UI16 LocalPort, RemotePort;
695 PTA_IP_ADDRESS AddressIP = (PTA_IP_ADDRESS)Address;
696
697 TcpipRecursiveMutexEnter( &TCPLock, TRUE );
698
699 OskitTCPGetAddress
700 ( Connection->SocketContext,
701 &LocalAddress, &LocalPort,
702 &RemoteAddress, &RemotePort );
703
704 AddressIP->TAAddressCount = 1;
705 AddressIP->Address[0].AddressLength = TDI_ADDRESS_LENGTH_IP;
706 AddressIP->Address[0].AddressType = TDI_ADDRESS_TYPE_IP;
707 AddressIP->Address[0].Address[0].sin_port = RemotePort;
708 AddressIP->Address[0].Address[0].in_addr = RemoteAddress;
709
710 TcpipRecursiveMutexLeave( &TCPLock );
711
712 return STATUS_SUCCESS;
713 }
714
715 VOID TCPRemoveIRP( PCONNECTION_ENDPOINT Endpoint, PIRP Irp ) {
716 PLIST_ENTRY Entry;
717 PLIST_ENTRY ListHead[4];
718 KIRQL OldIrql;
719 PTDI_BUCKET Bucket;
720 UINT i = 0;
721
722 ListHead[0] = &Endpoint->ReceiveRequest;
723 ListHead[1] = &Endpoint->ConnectRequest;
724 ListHead[2] = &Endpoint->ListenRequest;
725 ListHead[3] = 0;
726
727 TcpipAcquireSpinLock( &Endpoint->Lock, &OldIrql );
728
729 for( i = 0; ListHead[i]; i++ ) {
730 for( Entry = ListHead[i]->Flink;
731 Entry != ListHead[i];
732 Entry = Entry->Flink ) {
733 Bucket = CONTAINING_RECORD( Entry, TDI_BUCKET, Entry );
734
735 if( Bucket->Request.RequestContext == Irp ) {
736 RemoveEntryList( &Bucket->Entry );
737 break;
738 }
739 }
740 }
741
742 TcpipReleaseSpinLock( &Endpoint->Lock, OldIrql );
743 }
744
745 /* EOF */