Use lookaside lists for two most common allocation sizes (mbuf and data buffer)
[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 NTSTATUS TCPMemStartup( void );
290 void *TCPMalloc( void *ClientData,
291 OSK_UINT bytes, OSK_PCHAR file, OSK_UINT line );
292 void TCPFree( void *ClientData,
293 void *data, OSK_PCHAR file, OSK_UINT line );
294 void TCPMemShutdown( void );
295
296 int TCPSleep( void *ClientData, void *token, int priority, char *msg,
297 int tmio );
298
299 void TCPWakeup( void *ClientData, void *token );
300
301 OSKITTCP_EVENT_HANDLERS EventHandlers = {
302 NULL, /* Client Data */
303 TCPSocketState, /* SocketState */
304 TCPPacketSend, /* PacketSend */
305 TCPFindInterface, /* FindInterface */
306 TCPMalloc, /* Malloc */
307 TCPFree, /* Free */
308 TCPSleep, /* Sleep */
309 TCPWakeup /* Wakeup */
310 };
311
312 static KEVENT TimerLoopEvent;
313 static HANDLE TimerThreadHandle;
314
315 /*
316 * We are running 2 timers here, one with a 200ms interval (fast) and the other
317 * with a 500ms interval (slow). So we need to time out at 200, 400, 500, 600,
318 * 800, 1000 and process the "fast" events at 200, 400, 600, 800, 1000 and the
319 * "slow" events at 500 and 1000.
320 */
321 static VOID DDKAPI
322 TimerThread(PVOID Context)
323 {
324 LARGE_INTEGER Timeout;
325 NTSTATUS Status;
326 unsigned Current, NextFast, NextSlow, Next;
327
328 Current = 0;
329 Next = 0;
330 NextFast = 0;
331 NextSlow = 0;
332 while ( 1 ) {
333 if (Next == NextFast) {
334 NextFast += 2;
335 }
336 if (Next == NextSlow) {
337 NextSlow += 5;
338 }
339 Next = min(NextFast, NextSlow);
340 Timeout.QuadPart = (LONGLONG) (Next - Current) * -1000000; /* 100 ms */
341 Status = KeWaitForSingleObject(&TimerLoopEvent, Executive, KernelMode,
342 FALSE, &Timeout);
343 if (STATUS_SUCCESS == Status) {
344 PsTerminateSystemThread(STATUS_SUCCESS);
345 }
346 ASSERT(STATUS_TIMEOUT == Status);
347
348 TcpipRecursiveMutexEnter( &TCPLock, TRUE );
349 TimerOskitTCP( Next == NextFast, Next == NextSlow );
350 if (Next == NextSlow) {
351 DrainSignals();
352 }
353 TcpipRecursiveMutexLeave( &TCPLock );
354
355 Current = Next;
356 if (10 <= Current) {
357 Current = 0;
358 Next = 0;
359 NextFast = 0;
360 NextSlow = 0;
361 }
362 }
363 }
364
365 static VOID
366 StartTimer(VOID)
367 {
368 KeInitializeEvent(&TimerLoopEvent, NotificationEvent, FALSE);
369 PsCreateSystemThread(&TimerThreadHandle, THREAD_ALL_ACCESS, 0, 0, 0,
370 TimerThread, NULL);
371 }
372
373
374 NTSTATUS TCPStartup(VOID)
375 /*
376 * FUNCTION: Initializes the TCP subsystem
377 * RETURNS:
378 * Status of operation
379 */
380 {
381 NTSTATUS Status;
382
383 TcpipRecursiveMutexInit( &TCPLock );
384 ExInitializeFastMutex( &SleepingThreadsLock );
385 InitializeListHead( &SleepingThreadsList );
386 InitializeListHead( &SignalledConnections );
387 Status = TCPMemStartup();
388 if ( ! NT_SUCCESS(Status) ) {
389 return Status;
390 }
391
392 PortsStartup( &TCPPorts, 1, 0xfffe );
393
394 RegisterOskitTCPEventHandlers( &EventHandlers );
395 InitOskitTCP();
396
397 /* Register this protocol with IP layer */
398 IPRegisterProtocol(IPPROTO_TCP, TCPReceive);
399
400 ExInitializeNPagedLookasideList(
401 &TCPSegmentList, /* Lookaside list */
402 NULL, /* Allocate routine */
403 NULL, /* Free routine */
404 0, /* Flags */
405 sizeof(TCP_SEGMENT), /* Size of each entry */
406 TAG('T','C','P','S'), /* Tag */
407 0); /* Depth */
408
409 StartTimer();
410
411 TCPInitialized = TRUE;
412
413 return STATUS_SUCCESS;
414 }
415
416
417 NTSTATUS TCPShutdown(VOID)
418 /*
419 * FUNCTION: Shuts down the TCP subsystem
420 * RETURNS:
421 * Status of operation
422 */
423 {
424 LARGE_INTEGER WaitForThread;
425
426 if (!TCPInitialized)
427 return STATUS_SUCCESS;
428
429 WaitForThread.QuadPart = -2500000; /* 250 ms */
430 KeSetEvent(&TimerLoopEvent, IO_NO_INCREMENT, FALSE);
431 ZwWaitForSingleObject(TimerThreadHandle, FALSE, &WaitForThread);
432
433 /* Deregister this protocol with IP layer */
434 IPRegisterProtocol(IPPROTO_TCP, NULL);
435
436 ExDeleteNPagedLookasideList(&TCPSegmentList);
437
438 TCPInitialized = FALSE;
439
440 DeinitOskitTCP();
441
442 PortsShutdown( &TCPPorts );
443
444 TCPMemShutdown();
445
446 return STATUS_SUCCESS;
447 }
448
449 NTSTATUS TCPTranslateError( int OskitError ) {
450 NTSTATUS Status = STATUS_UNSUCCESSFUL;
451
452 switch( OskitError ) {
453 case 0: Status = STATUS_SUCCESS; break;
454 case OSK_EADDRNOTAVAIL:
455 case OSK_EAFNOSUPPORT: Status = STATUS_INVALID_CONNECTION; break;
456 case OSK_ECONNREFUSED:
457 case OSK_ECONNRESET: Status = STATUS_REMOTE_NOT_LISTENING; break;
458 case OSK_EINPROGRESS:
459 case OSK_EAGAIN: Status = STATUS_PENDING; break;
460 default: Status = STATUS_INVALID_CONNECTION; break;
461 }
462
463 TI_DbgPrint(DEBUG_TCP,("Error %d -> %x\n", OskitError, Status));
464 return Status;
465 }
466
467 NTSTATUS TCPConnect
468 ( PCONNECTION_ENDPOINT Connection,
469 PTDI_CONNECTION_INFORMATION ConnInfo,
470 PTDI_CONNECTION_INFORMATION ReturnInfo,
471 PTCP_COMPLETION_ROUTINE Complete,
472 PVOID Context ) {
473 NTSTATUS Status;
474 SOCKADDR_IN AddressToConnect = { 0 }, AddressToBind = { 0 };
475 IP_ADDRESS RemoteAddress;
476 USHORT RemotePort;
477 PTDI_BUCKET Bucket;
478
479 DbgPrint("TCPConnect: Called\n");
480
481 Bucket = ExAllocatePool( NonPagedPool, sizeof(*Bucket) );
482 if( !Bucket ) return STATUS_NO_MEMORY;
483
484 TcpipRecursiveMutexEnter( &TCPLock, TRUE );
485
486 /* Freed in TCPSocketState */
487 Bucket->Request.RequestNotifyObject = (PVOID)Complete;
488 Bucket->Request.RequestContext = Context;
489
490 InsertHeadList( &Connection->ConnectRequest, &Bucket->Entry );
491
492 Status = AddrBuildAddress
493 ((PTRANSPORT_ADDRESS)ConnInfo->RemoteAddress,
494 &RemoteAddress,
495 &RemotePort);
496
497 DbgPrint("Connecting to address %x:%x\n",
498 RemoteAddress.Address.IPv4Address,
499 RemotePort);
500
501 if (!NT_SUCCESS(Status)) {
502 TI_DbgPrint(DEBUG_TCP, ("Could not AddrBuildAddress in TCPConnect\n"));
503 return Status;
504 }
505
506 AddressToConnect.sin_family = AF_INET;
507 AddressToBind = AddressToConnect;
508
509 OskitTCPBind( Connection->SocketContext,
510 Connection,
511 &AddressToBind,
512 sizeof(AddressToBind) );
513
514 memcpy( &AddressToConnect.sin_addr,
515 &RemoteAddress.Address.IPv4Address,
516 sizeof(AddressToConnect.sin_addr) );
517 AddressToConnect.sin_port = RemotePort;
518
519 Status = TCPTranslateError
520 ( OskitTCPConnect( Connection->SocketContext,
521 Connection,
522 &AddressToConnect,
523 sizeof(AddressToConnect) ) );
524
525 TcpipRecursiveMutexLeave( &TCPLock );
526
527 if( Status == OSK_EINPROGRESS )
528 return STATUS_PENDING;
529 else
530 return Status;
531 }
532
533 NTSTATUS TCPDisconnect
534 ( PCONNECTION_ENDPOINT Connection,
535 UINT Flags,
536 PTDI_CONNECTION_INFORMATION ConnInfo,
537 PTDI_CONNECTION_INFORMATION ReturnInfo,
538 PTCP_COMPLETION_ROUTINE Complete,
539 PVOID Context ) {
540 NTSTATUS Status;
541
542 TI_DbgPrint(DEBUG_TCP,("started\n"));
543
544 TcpipRecursiveMutexEnter( &TCPLock, TRUE );
545
546 switch( Flags & (TDI_DISCONNECT_ABORT | TDI_DISCONNECT_RELEASE) ) {
547 case 0:
548 case TDI_DISCONNECT_ABORT:
549 Flags = 0;
550 break;
551
552 case TDI_DISCONNECT_ABORT | TDI_DISCONNECT_RELEASE:
553 Flags = 2;
554 break;
555
556 case TDI_DISCONNECT_RELEASE:
557 Flags = 1;
558 break;
559 }
560
561 Status = TCPTranslateError
562 ( OskitTCPShutdown( Connection->SocketContext, Flags ) );
563
564 TcpipRecursiveMutexLeave( &TCPLock );
565
566 TI_DbgPrint(DEBUG_TCP,("finished %x\n", Status));
567
568 return Status;
569 }
570
571 NTSTATUS TCPClose
572 ( PCONNECTION_ENDPOINT Connection ) {
573 NTSTATUS Status;
574
575 TI_DbgPrint(DEBUG_TCP,("TCPClose started\n"));
576
577 TcpipRecursiveMutexEnter( &TCPLock, TRUE );
578
579 Status = TCPTranslateError( OskitTCPClose( Connection->SocketContext ) );
580
581 /* Make our code remove all pending IRPs */
582 Connection->State |= SEL_FIN;
583 DrainSignals();
584
585 TcpipRecursiveMutexLeave( &TCPLock );
586
587 TI_DbgPrint(DEBUG_TCP,("TCPClose finished %x\n", Status));
588
589 return Status;
590 }
591
592 NTSTATUS TCPReceiveData
593 ( PCONNECTION_ENDPOINT Connection,
594 PNDIS_BUFFER Buffer,
595 ULONG ReceiveLength,
596 PULONG BytesReceived,
597 ULONG ReceiveFlags,
598 PTCP_COMPLETION_ROUTINE Complete,
599 PVOID Context ) {
600 OSK_PCHAR DataBuffer;
601 UINT DataLen, Received = 0;
602 NTSTATUS Status;
603 PTDI_BUCKET Bucket;
604
605 TI_DbgPrint(DEBUG_TCP,("Called for %d bytes (on socket %x)\n",
606 ReceiveLength, Connection->SocketContext));
607
608 ASSERT_KM_POINTER(Connection->SocketContext);
609
610 TcpipRecursiveMutexEnter( &TCPLock, TRUE );
611
612 NdisQueryBuffer( Buffer, &DataBuffer, &DataLen );
613
614 TI_DbgPrint(DEBUG_TCP,("TCP>|< Got an MDL %x (%x:%d)\n", Buffer, DataBuffer, DataLen));
615
616 Status = TCPTranslateError
617 ( OskitTCPRecv
618 ( Connection->SocketContext,
619 DataBuffer,
620 DataLen,
621 &Received,
622 ReceiveFlags ) );
623
624 TI_DbgPrint(DEBUG_TCP,("OskitTCPReceive: %x, %d\n", Status, Received));
625
626 /* Keep this request around ... there was no data yet */
627 if( Status == STATUS_PENDING ) {
628 /* Freed in TCPSocketState */
629 Bucket = ExAllocatePool( NonPagedPool, sizeof(*Bucket) );
630 if( !Bucket ) {
631 TI_DbgPrint(DEBUG_TCP,("Failed to allocate bucket\n"));
632 TcpipRecursiveMutexLeave( &TCPLock );
633 return STATUS_NO_MEMORY;
634 }
635
636 Bucket->Request.RequestNotifyObject = Complete;
637 Bucket->Request.RequestContext = Context;
638 *BytesReceived = 0;
639
640 InsertHeadList( &Connection->ReceiveRequest, &Bucket->Entry );
641 Status = STATUS_PENDING;
642 TI_DbgPrint(DEBUG_TCP,("Queued read irp\n"));
643 } else {
644 TI_DbgPrint(DEBUG_TCP,("Got status %x, bytes %d\n", Status, Received));
645 *BytesReceived = Received;
646 }
647
648 TcpipRecursiveMutexLeave( &TCPLock );
649
650 TI_DbgPrint(DEBUG_TCP,("Status %x\n", Status));
651
652 return Status;
653 }
654
655 NTSTATUS TCPSendData
656 ( PCONNECTION_ENDPOINT Connection,
657 PCHAR BufferData,
658 ULONG PacketSize,
659 PULONG DataUsed,
660 ULONG Flags) {
661 NTSTATUS Status;
662
663 ASSERT_KM_POINTER(Connection->SocketContext);
664
665 TcpipRecursiveMutexEnter( &TCPLock, TRUE );
666
667 TI_DbgPrint(DEBUG_TCP,("Connection = %x\n", Connection));
668 TI_DbgPrint(DEBUG_TCP,("Connection->SocketContext = %x\n",
669 Connection->SocketContext));
670
671 Status = OskitTCPSend( Connection->SocketContext,
672 (OSK_PCHAR)BufferData, PacketSize,
673 (PUINT)DataUsed, 0 );
674
675 TcpipRecursiveMutexLeave( &TCPLock );
676
677 return Status;
678 }
679
680 VOID TCPTimeout(VOID) {
681 /* Now handled by TimerThread */
682 }
683
684 UINT TCPAllocatePort( UINT HintPort ) {
685 if( HintPort ) {
686 if( AllocatePort( &TCPPorts, HintPort ) ) return HintPort;
687 else {
688 TI_DbgPrint
689 (MID_TRACE,("We got a hint port but couldn't allocate it\n"));
690 return (UINT)-1;
691 }
692 } else return AllocatePortFromRange( &TCPPorts, 1024, 5000 );
693 }
694
695 VOID TCPFreePort( UINT Port ) {
696 DeallocatePort( &TCPPorts, Port );
697 }
698
699 NTSTATUS TCPGetPeerAddress
700 ( PCONNECTION_ENDPOINT Connection,
701 PTRANSPORT_ADDRESS Address ) {
702 OSK_UINT LocalAddress, RemoteAddress;
703 OSK_UI16 LocalPort, RemotePort;
704 PTA_IP_ADDRESS AddressIP = (PTA_IP_ADDRESS)Address;
705
706 TcpipRecursiveMutexEnter( &TCPLock, TRUE );
707
708 OskitTCPGetAddress
709 ( Connection->SocketContext,
710 &LocalAddress, &LocalPort,
711 &RemoteAddress, &RemotePort );
712
713 AddressIP->TAAddressCount = 1;
714 AddressIP->Address[0].AddressLength = TDI_ADDRESS_LENGTH_IP;
715 AddressIP->Address[0].AddressType = TDI_ADDRESS_TYPE_IP;
716 AddressIP->Address[0].Address[0].sin_port = RemotePort;
717 AddressIP->Address[0].Address[0].in_addr = RemoteAddress;
718
719 TcpipRecursiveMutexLeave( &TCPLock );
720
721 return STATUS_SUCCESS;
722 }
723
724 VOID TCPRemoveIRP( PCONNECTION_ENDPOINT Endpoint, PIRP Irp ) {
725 PLIST_ENTRY Entry;
726 PLIST_ENTRY ListHead[4];
727 KIRQL OldIrql;
728 PTDI_BUCKET Bucket;
729 UINT i = 0;
730
731 ListHead[0] = &Endpoint->ReceiveRequest;
732 ListHead[1] = &Endpoint->ConnectRequest;
733 ListHead[2] = &Endpoint->ListenRequest;
734 ListHead[3] = 0;
735
736 TcpipAcquireSpinLock( &Endpoint->Lock, &OldIrql );
737
738 for( i = 0; ListHead[i]; i++ ) {
739 for( Entry = ListHead[i]->Flink;
740 Entry != ListHead[i];
741 Entry = Entry->Flink ) {
742 Bucket = CONTAINING_RECORD( Entry, TDI_BUCKET, Entry );
743
744 if( Bucket->Request.RequestContext == Irp ) {
745 RemoveEntryList( &Bucket->Entry );
746 break;
747 }
748 }
749 }
750
751 TcpipReleaseSpinLock( &Endpoint->Lock, OldIrql );
752 }
753
754 /* EOF */