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)
9 * CSH 01/08-2000 Created
10 * arty 12/21/2004 Added accept
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
;
24 static VOID
HandleSignalledConnection( PCONNECTION_ENDPOINT Connection
,
26 NTSTATUS Status
= STATUS_SUCCESS
;
27 PTCP_COMPLETION_ROUTINE Complete
;
33 TI_DbgPrint(MID_TRACE
,("Handling signalled state on %x (%x)\n",
34 Connection
, Connection
->SocketContext
));
36 /* Things that can happen when we try the initial connection */
37 if( NewState
& SEL_CONNECT
) {
38 while( !IsListEmpty( &Connection
->ConnectRequest
) ) {
39 Entry
= RemoveHeadList( &Connection
->ConnectRequest
);
40 TI_DbgPrint(DEBUG_TCP
, ("Connect Event\n"));
42 Bucket
= CONTAINING_RECORD( Entry
, TDI_BUCKET
, Entry
);
43 Complete
= Bucket
->Request
.RequestNotifyObject
;
44 TI_DbgPrint(DEBUG_TCP
,
45 ("Completing Request %x\n", Bucket
->Request
));
47 if( (NewState
& (SEL_CONNECT
| SEL_FIN
)) ==
48 (SEL_CONNECT
| SEL_FIN
) )
49 Status
= STATUS_CONNECTION_REFUSED
;
51 Status
= STATUS_SUCCESS
;
53 Complete( Bucket
->Request
.RequestContext
, Status
, 0 );
55 /* Frees the bucket allocated in TCPConnect */
60 if( NewState
& SEL_ACCEPT
) {
61 /* Handle readable on a listening socket --
62 * TODO: Implement filtering
65 TI_DbgPrint(DEBUG_TCP
,("Accepting new connection on %x (Queue: %s)\n",
67 IsListEmpty(&Connection
->ListenRequest
) ?
68 "empty" : "nonempty"));
70 while( !IsListEmpty( &Connection
->ListenRequest
) ) {
71 PIO_STACK_LOCATION IrpSp
;
73 Entry
= RemoveHeadList( &Connection
->ListenRequest
);
74 Bucket
= CONTAINING_RECORD( Entry
, TDI_BUCKET
, Entry
);
75 Complete
= Bucket
->Request
.RequestNotifyObject
;
77 Irp
= Bucket
->Request
.RequestContext
;
78 IrpSp
= IoGetCurrentIrpStackLocation( Irp
);
80 TI_DbgPrint(DEBUG_TCP
,("Getting the socket\n"));
81 Status
= TCPServiceListeningSocket
82 ( Connection
->AddressFile
->Listener
,
83 Bucket
->AssociatedEndpoint
,
84 (PTDI_REQUEST_KERNEL
)&IrpSp
->Parameters
);
86 TI_DbgPrint(DEBUG_TCP
,("Socket: Status: %x\n"));
88 if( Status
== STATUS_PENDING
) {
89 InsertHeadList( &Connection
->ListenRequest
, &Bucket
->Entry
);
92 Complete( Bucket
->Request
.RequestContext
, Status
, 0 );
98 /* Things that happen after we're connected */
99 if( NewState
& SEL_READ
) {
100 TI_DbgPrint(DEBUG_TCP
,("Readable: irp list %s\n",
101 IsListEmpty(&Connection
->ReceiveRequest
) ?
102 "empty" : "nonempty"));
104 while( !IsListEmpty( &Connection
->ReceiveRequest
) ) {
105 OSK_UINT RecvLen
= 0, Received
= 0;
106 OSK_PCHAR RecvBuffer
= 0;
108 Entry
= RemoveHeadList( &Connection
->ReceiveRequest
);
109 Bucket
= CONTAINING_RECORD( Entry
, TDI_BUCKET
, Entry
);
110 Complete
= Bucket
->Request
.RequestNotifyObject
;
112 Irp
= Bucket
->Request
.RequestContext
;
113 Mdl
= Irp
->MdlAddress
;
115 TI_DbgPrint(DEBUG_TCP
,
116 ("Getting the user buffer from %x\n", Mdl
));
118 NdisQueryBuffer( Mdl
, &RecvBuffer
, &RecvLen
);
120 TI_DbgPrint(DEBUG_TCP
,
121 ("Reading %d bytes to %x\n", RecvLen
, RecvBuffer
));
123 TI_DbgPrint(DEBUG_TCP
, ("Connection: %x\n", Connection
));
126 ("Connection->SocketContext: %x\n",
127 Connection
->SocketContext
));
128 TI_DbgPrint(DEBUG_TCP
, ("RecvBuffer: %x\n", RecvBuffer
));
130 Status
= TCPTranslateError
131 ( OskitTCPRecv( Connection
->SocketContext
,
137 TI_DbgPrint(DEBUG_TCP
,("TCP Bytes: %d\n", Received
));
139 if( Status
== STATUS_SUCCESS
) {
140 TI_DbgPrint(DEBUG_TCP
,("Received %d bytes with status %x\n",
143 Complete( Bucket
->Request
.RequestContext
,
144 STATUS_SUCCESS
, Received
);
145 exFreePool( Bucket
);
146 } else if( Status
== STATUS_PENDING
) {
148 ( &Connection
->ReceiveRequest
, &Bucket
->Entry
);
151 TI_DbgPrint(DEBUG_TCP
,
152 ("Completing Receive request: %x %x\n",
153 Bucket
->Request
, Status
));
154 Complete( Bucket
->Request
.RequestContext
, Status
, 0 );
155 exFreePool( Bucket
);
159 if( NewState
& SEL_WRITE
) {
160 TI_DbgPrint(DEBUG_TCP
,("Writeable: irp list %s\n",
161 IsListEmpty(&Connection
->ReceiveRequest
) ?
162 "empty" : "nonempty"));
164 while( !IsListEmpty( &Connection
->SendRequest
) ) {
165 OSK_UINT SendLen
= 0, Sent
= 0;
166 OSK_PCHAR SendBuffer
= 0;
168 Entry
= RemoveHeadList( &Connection
->SendRequest
);
169 Bucket
= CONTAINING_RECORD( Entry
, TDI_BUCKET
, Entry
);
170 Complete
= Bucket
->Request
.RequestNotifyObject
;
172 Irp
= Bucket
->Request
.RequestContext
;
173 Mdl
= Irp
->MdlAddress
;
175 TI_DbgPrint(DEBUG_TCP
,
176 ("Getting the user buffer from %x\n", Mdl
));
178 NdisQueryBuffer( Mdl
, &SendBuffer
, &SendLen
);
180 TI_DbgPrint(DEBUG_TCP
,
181 ("Writing %d bytes to %x\n", SendLen
, SendBuffer
));
183 TI_DbgPrint(DEBUG_TCP
, ("Connection: %x\n", Connection
));
186 ("Connection->SocketContext: %x\n",
187 Connection
->SocketContext
));
189 Status
= TCPTranslateError
190 ( OskitTCPSend( Connection
->SocketContext
,
196 TI_DbgPrint(DEBUG_TCP
,("TCP Bytes: %d\n", Sent
));
198 if( Status
== STATUS_SUCCESS
) {
199 TI_DbgPrint(DEBUG_TCP
,("Sent %d bytes with status %x\n",
202 Complete( Bucket
->Request
.RequestContext
,
203 STATUS_SUCCESS
, Sent
);
204 exFreePool( Bucket
);
205 } else if( Status
== STATUS_PENDING
) {
207 ( &Connection
->SendRequest
, &Bucket
->Entry
);
210 TI_DbgPrint(DEBUG_TCP
,
211 ("Completing Send request: %x %x\n",
212 Bucket
->Request
, Status
));
213 Complete( Bucket
->Request
.RequestContext
, Status
, 0 );
214 exFreePool( Bucket
);
219 if( NewState
& SEL_FIN
) {
220 PLIST_ENTRY ListsToErase
[4];
221 NTSTATUS IrpStatus
[4];
224 TI_DbgPrint(DEBUG_TCP
, ("EOF From socket\n"));
226 ListsToErase
[0] = &Connection
->ReceiveRequest
;
227 IrpStatus
[0] = STATUS_SUCCESS
;
228 ListsToErase
[1] = &Connection
->ListenRequest
;
229 IrpStatus
[1] = STATUS_UNSUCCESSFUL
;
230 ListsToErase
[2] = &Connection
->ConnectRequest
;
231 IrpStatus
[2] = STATUS_UNSUCCESSFUL
;
235 for( i
= 0; ListsToErase
[i
]; i
++ ) {
236 while( !IsListEmpty( ListsToErase
[i
] ) ) {
237 Entry
= RemoveHeadList( ListsToErase
[i
] );
238 Bucket
= CONTAINING_RECORD( Entry
, TDI_BUCKET
, Entry
);
239 Complete
= Bucket
->Request
.RequestNotifyObject
;
240 Complete( Bucket
->Request
.RequestContext
, IrpStatus
[i
], 0 );
241 exFreePool( Bucket
);
246 Connection
->Signalled
= FALSE
;
249 VOID
DrainSignals() {
250 PCONNECTION_ENDPOINT Connection
;
251 PLIST_ENTRY ListEntry
;
253 while( !IsListEmpty( &SignalledConnections
) ) {
254 ListEntry
= RemoveHeadList( &SignalledConnections
);
255 Connection
= CONTAINING_RECORD( ListEntry
, CONNECTION_ENDPOINT
,
257 HandleSignalledConnection( Connection
, Connection
->SignalState
);
261 PCONNECTION_ENDPOINT
TCPAllocateConnectionEndpoint( PVOID ClientContext
) {
262 PCONNECTION_ENDPOINT Connection
=
263 exAllocatePool(NonPagedPool
, sizeof(CONNECTION_ENDPOINT
));
267 TI_DbgPrint(DEBUG_CPOINT
, ("Connection point file object allocated at (0x%X).\n", Connection
));
269 RtlZeroMemory(Connection
, sizeof(CONNECTION_ENDPOINT
));
271 /* Initialize spin lock that protects the connection endpoint file object */
272 TcpipInitializeSpinLock(&Connection
->Lock
);
273 InitializeListHead(&Connection
->ConnectRequest
);
274 InitializeListHead(&Connection
->ListenRequest
);
275 InitializeListHead(&Connection
->ReceiveRequest
);
276 InitializeListHead(&Connection
->SendRequest
);
278 /* Save client context pointer */
279 Connection
->ClientContext
= ClientContext
;
284 VOID
TCPFreeConnectionEndpoint( PCONNECTION_ENDPOINT Connection
) {
285 TCPClose( Connection
);
286 exFreePool( Connection
);
289 NTSTATUS
TCPSocket( PCONNECTION_ENDPOINT Connection
,
290 UINT Family
, UINT Type
, UINT Proto
) {
293 TI_DbgPrint(DEBUG_TCP
,("Called: Connection %x, Family %d, Type %d, "
295 Connection
, Family
, Type
, Proto
));
297 TcpipRecursiveMutexEnter( &TCPLock
, TRUE
);
298 Status
= TCPTranslateError( OskitTCPSocket( Connection
,
299 &Connection
->SocketContext
,
304 ASSERT_KM_POINTER(Connection
->SocketContext
);
306 TI_DbgPrint(DEBUG_TCP
,("Connection->SocketContext %x\n",
307 Connection
->SocketContext
));
309 TcpipRecursiveMutexLeave( &TCPLock
);
314 VOID
TCPReceive(PIP_INTERFACE Interface
, PIP_PACKET IPPacket
)
316 * FUNCTION: Receives and queues TCP data
318 * IPPacket = Pointer to an IP packet that was received
320 * This is the low level interface for receiving TCP data
323 TI_DbgPrint(DEBUG_TCP
,("Sending packet %d (%d) to oskit\n",
325 IPPacket
->HeaderSize
));
327 TcpipRecursiveMutexEnter( &TCPLock
, TRUE
);
329 OskitTCPReceiveDatagram( IPPacket
->Header
,
331 IPPacket
->HeaderSize
);
335 TcpipRecursiveMutexLeave( &TCPLock
);
339 int TCPSocketState( void *ClientData
,
341 void *WhichConnection
,
344 int TCPPacketSend( void *ClientData
,
348 POSK_IFADDR
TCPFindInterface( void *ClientData
,
351 OSK_SOCKADDR
*ReqAddr
);
353 NTSTATUS
TCPMemStartup( void );
354 void *TCPMalloc( void *ClientData
,
355 OSK_UINT bytes
, OSK_PCHAR file
, OSK_UINT line
);
356 void TCPFree( void *ClientData
,
357 void *data
, OSK_PCHAR file
, OSK_UINT line
);
358 void TCPMemShutdown( void );
360 int TCPSleep( void *ClientData
, void *token
, int priority
, char *msg
,
363 void TCPWakeup( void *ClientData
, void *token
);
365 OSKITTCP_EVENT_HANDLERS EventHandlers
= {
366 NULL
, /* Client Data */
367 TCPSocketState
, /* SocketState */
368 TCPPacketSend
, /* PacketSend */
369 TCPFindInterface
, /* FindInterface */
370 TCPMalloc
, /* Malloc */
372 TCPSleep
, /* Sleep */
373 TCPWakeup
/* Wakeup */
376 static KEVENT TimerLoopEvent
;
377 static HANDLE TimerThreadHandle
;
380 * We are running 2 timers here, one with a 200ms interval (fast) and the other
381 * with a 500ms interval (slow). So we need to time out at 200, 400, 500, 600,
382 * 800, 1000 and process the "fast" events at 200, 400, 600, 800, 1000 and the
383 * "slow" events at 500 and 1000.
386 TimerThread(PVOID Context
)
388 LARGE_INTEGER Timeout
;
390 unsigned Current
, NextFast
, NextSlow
, Next
;
397 if (Next
== NextFast
) {
400 if (Next
== NextSlow
) {
403 Next
= min(NextFast
, NextSlow
);
404 Timeout
.QuadPart
= (LONGLONG
) (Next
- Current
) * -1000000; /* 100 ms */
405 Status
= KeWaitForSingleObject(&TimerLoopEvent
, Executive
, KernelMode
,
407 if (Status
!= STATUS_TIMEOUT
) {
408 PsTerminateSystemThread(Status
);
411 TcpipRecursiveMutexEnter( &TCPLock
, TRUE
);
412 TimerOskitTCP( Next
== NextFast
, Next
== NextSlow
);
413 if (Next
== NextSlow
) {
416 TcpipRecursiveMutexLeave( &TCPLock
);
431 KeInitializeEvent(&TimerLoopEvent
, NotificationEvent
, FALSE
);
432 PsCreateSystemThread(&TimerThreadHandle
, THREAD_ALL_ACCESS
, 0, 0, 0,
437 NTSTATUS
TCPStartup(VOID
)
439 * FUNCTION: Initializes the TCP subsystem
441 * Status of operation
446 TcpipRecursiveMutexInit( &TCPLock
);
447 ExInitializeFastMutex( &SleepingThreadsLock
);
448 InitializeListHead( &SleepingThreadsList
);
449 InitializeListHead( &SignalledConnections
);
450 Status
= TCPMemStartup();
451 if ( ! NT_SUCCESS(Status
) ) {
455 Status
= PortsStartup( &TCPPorts
, 1, 0xfffe );
456 if( !NT_SUCCESS(Status
) ) {
461 RegisterOskitTCPEventHandlers( &EventHandlers
);
464 /* Register this protocol with IP layer */
465 IPRegisterProtocol(IPPROTO_TCP
, TCPReceive
);
467 ExInitializeNPagedLookasideList(
468 &TCPSegmentList
, /* Lookaside list */
469 NULL
, /* Allocate routine */
470 NULL
, /* Free routine */
472 sizeof(TCP_SEGMENT
), /* Size of each entry */
473 TAG('T','C','P','S'), /* Tag */
478 TCPInitialized
= TRUE
;
480 return STATUS_SUCCESS
;
484 NTSTATUS
TCPShutdown(VOID
)
486 * FUNCTION: Shuts down the TCP subsystem
488 * Status of operation
491 LARGE_INTEGER WaitForThread
;
494 return STATUS_SUCCESS
;
496 WaitForThread
.QuadPart
= -2500000; /* 250 ms */
497 KeSetEvent(&TimerLoopEvent
, IO_NO_INCREMENT
, FALSE
);
498 ZwWaitForSingleObject(TimerThreadHandle
, FALSE
, &WaitForThread
);
500 /* Deregister this protocol with IP layer */
501 IPRegisterProtocol(IPPROTO_TCP
, NULL
);
503 ExDeleteNPagedLookasideList(&TCPSegmentList
);
505 TCPInitialized
= FALSE
;
509 PortsShutdown( &TCPPorts
);
513 return STATUS_SUCCESS
;
516 NTSTATUS
TCPTranslateError( int OskitError
) {
517 NTSTATUS Status
= STATUS_UNSUCCESSFUL
;
519 switch( OskitError
) {
520 case 0: Status
= STATUS_SUCCESS
; break;
521 case OSK_EADDRNOTAVAIL
:
522 case OSK_EAFNOSUPPORT
: Status
= STATUS_INVALID_CONNECTION
; break;
523 case OSK_ECONNREFUSED
:
524 case OSK_ECONNRESET
: Status
= STATUS_REMOTE_NOT_LISTENING
; break;
525 case OSK_EINPROGRESS
:
526 case OSK_EAGAIN
: Status
= STATUS_PENDING
; break;
527 default: Status
= STATUS_INVALID_CONNECTION
; break;
530 TI_DbgPrint(DEBUG_TCP
,("Error %d -> %x\n", OskitError
, Status
));
535 ( PCONNECTION_ENDPOINT Connection
,
536 PTDI_CONNECTION_INFORMATION ConnInfo
,
537 PTDI_CONNECTION_INFORMATION ReturnInfo
,
538 PTCP_COMPLETION_ROUTINE Complete
,
541 SOCKADDR_IN AddressToConnect
= { 0 }, AddressToBind
= { 0 };
542 IP_ADDRESS RemoteAddress
;
546 TI_DbgPrint(DEBUG_TCP
,("TCPConnect: Called\n"));
548 Bucket
= exAllocatePool( NonPagedPool
, sizeof(*Bucket
) );
549 if( !Bucket
) return STATUS_NO_MEMORY
;
551 TcpipRecursiveMutexEnter( &TCPLock
, TRUE
);
553 /* Freed in TCPSocketState */
554 Bucket
->Request
.RequestNotifyObject
= (PVOID
)Complete
;
555 Bucket
->Request
.RequestContext
= Context
;
557 InsertHeadList( &Connection
->ConnectRequest
, &Bucket
->Entry
);
559 Status
= AddrBuildAddress
560 ((PTRANSPORT_ADDRESS
)ConnInfo
->RemoteAddress
,
564 TI_DbgPrint(DEBUG_TCP
,
565 ("Connecting to address %x:%x\n",
566 RemoteAddress
.Address
.IPv4Address
,
569 if (!NT_SUCCESS(Status
)) {
570 TI_DbgPrint(DEBUG_TCP
, ("Could not AddrBuildAddress in TCPConnect\n"));
571 TcpipRecursiveMutexLeave( &TCPLock
);
575 AddressToConnect
.sin_family
= AF_INET
;
576 AddressToBind
= AddressToConnect
;
578 Status
= TCPTranslateError
579 ( OskitTCPBind( Connection
->SocketContext
,
582 sizeof(AddressToBind
) ) );
584 if (NT_SUCCESS(Status
)) {
585 memcpy( &AddressToConnect
.sin_addr
,
586 &RemoteAddress
.Address
.IPv4Address
,
587 sizeof(AddressToConnect
.sin_addr
) );
588 AddressToConnect
.sin_port
= RemotePort
;
590 Status
= TCPTranslateError
591 ( OskitTCPConnect( Connection
->SocketContext
,
594 sizeof(AddressToConnect
) ) );
597 TcpipRecursiveMutexLeave( &TCPLock
);
602 NTSTATUS TCPDisconnect
603 ( PCONNECTION_ENDPOINT Connection
,
605 PTDI_CONNECTION_INFORMATION ConnInfo
,
606 PTDI_CONNECTION_INFORMATION ReturnInfo
,
607 PTCP_COMPLETION_ROUTINE Complete
,
611 TI_DbgPrint(DEBUG_TCP
,("started\n"));
613 TcpipRecursiveMutexEnter( &TCPLock
, TRUE
);
615 switch( Flags
& (TDI_DISCONNECT_ABORT
| TDI_DISCONNECT_RELEASE
) ) {
617 case TDI_DISCONNECT_ABORT
:
621 case TDI_DISCONNECT_ABORT
| TDI_DISCONNECT_RELEASE
:
625 case TDI_DISCONNECT_RELEASE
:
630 Status
= TCPTranslateError
631 ( OskitTCPShutdown( Connection
->SocketContext
, Flags
) );
633 TcpipRecursiveMutexLeave( &TCPLock
);
635 TI_DbgPrint(DEBUG_TCP
,("finished %x\n", Status
));
641 ( PCONNECTION_ENDPOINT Connection
) {
644 TI_DbgPrint(DEBUG_TCP
,("TCPClose started\n"));
646 TcpipRecursiveMutexEnter( &TCPLock
, TRUE
);
648 Status
= TCPTranslateError( OskitTCPClose( Connection
->SocketContext
) );
650 /* Make our code remove all pending IRPs */
651 Connection
->State
|= SEL_FIN
;
654 TcpipRecursiveMutexLeave( &TCPLock
);
656 TI_DbgPrint(DEBUG_TCP
,("TCPClose finished %x\n", Status
));
661 NTSTATUS TCPReceiveData
662 ( PCONNECTION_ENDPOINT Connection
,
665 PULONG BytesReceived
,
667 PTCP_COMPLETION_ROUTINE Complete
,
669 OSK_PCHAR DataBuffer
;
670 UINT DataLen
, Received
= 0;
674 TI_DbgPrint(DEBUG_TCP
,("Called for %d bytes (on socket %x)\n",
675 ReceiveLength
, Connection
->SocketContext
));
677 ASSERT_KM_POINTER(Connection
->SocketContext
);
679 TcpipRecursiveMutexEnter( &TCPLock
, TRUE
);
681 NdisQueryBuffer( Buffer
, &DataBuffer
, &DataLen
);
683 TI_DbgPrint(DEBUG_TCP
,("TCP>|< Got an MDL %x (%x:%d)\n", Buffer
, DataBuffer
, DataLen
));
685 Status
= TCPTranslateError
687 ( Connection
->SocketContext
,
693 TI_DbgPrint(DEBUG_TCP
,("OskitTCPReceive: %x, %d\n", Status
, Received
));
695 /* Keep this request around ... there was no data yet */
696 if( Status
== STATUS_PENDING
) {
697 /* Freed in TCPSocketState */
698 Bucket
= exAllocatePool( NonPagedPool
, sizeof(*Bucket
) );
700 TI_DbgPrint(DEBUG_TCP
,("Failed to allocate bucket\n"));
701 TcpipRecursiveMutexLeave( &TCPLock
);
702 return STATUS_NO_MEMORY
;
705 Bucket
->Request
.RequestNotifyObject
= Complete
;
706 Bucket
->Request
.RequestContext
= Context
;
709 InsertHeadList( &Connection
->ReceiveRequest
, &Bucket
->Entry
);
710 Status
= STATUS_PENDING
;
711 TI_DbgPrint(DEBUG_TCP
,("Queued read irp\n"));
713 TI_DbgPrint(DEBUG_TCP
,("Got status %x, bytes %d\n", Status
, Received
));
714 *BytesReceived
= Received
;
717 TcpipRecursiveMutexLeave( &TCPLock
);
719 TI_DbgPrint(DEBUG_TCP
,("Status %x\n", Status
));
725 ( PCONNECTION_ENDPOINT Connection
,
730 PTCP_COMPLETION_ROUTINE Complete
,
736 TI_DbgPrint(DEBUG_TCP
,("Called for %d bytes (on socket %x)\n",
737 SendLength
, Connection
->SocketContext
));
739 ASSERT_KM_POINTER(Connection
->SocketContext
);
741 TcpipRecursiveMutexEnter( &TCPLock
, TRUE
);
743 TI_DbgPrint(DEBUG_TCP
,("Connection = %x\n", Connection
));
744 TI_DbgPrint(DEBUG_TCP
,("Connection->SocketContext = %x\n",
745 Connection
->SocketContext
));
747 Status
= TCPTranslateError
748 ( OskitTCPSend( Connection
->SocketContext
,
749 (OSK_PCHAR
)BufferData
, SendLength
,
752 TI_DbgPrint(DEBUG_TCP
,("OskitTCPSend: %x, %d\n", Status
, Sent
));
754 /* Keep this request around ... there was no data yet */
755 if( Status
== STATUS_PENDING
) {
756 /* Freed in TCPSocketState */
757 Bucket
= exAllocatePool( NonPagedPool
, sizeof(*Bucket
) );
759 TI_DbgPrint(DEBUG_TCP
,("Failed to allocate bucket\n"));
760 TcpipRecursiveMutexLeave( &TCPLock
);
761 return STATUS_NO_MEMORY
;
764 Bucket
->Request
.RequestNotifyObject
= Complete
;
765 Bucket
->Request
.RequestContext
= Context
;
768 InsertHeadList( &Connection
->SendRequest
, &Bucket
->Entry
);
769 TI_DbgPrint(DEBUG_TCP
,("Queued write irp\n"));
771 TI_DbgPrint(DEBUG_TCP
,("Got status %x, bytes %d\n", Status
, Sent
));
775 TcpipRecursiveMutexLeave( &TCPLock
);
777 TI_DbgPrint(DEBUG_TCP
,("Status %x\n", Status
));
782 VOID
TCPTimeout(VOID
) {
783 /* Now handled by TimerThread */
786 UINT
TCPAllocatePort( UINT HintPort
) {
788 if( AllocatePort( &TCPPorts
, HintPort
) ) return HintPort
;
791 (MID_TRACE
,("We got a hint port but couldn't allocate it\n"));
794 } else return AllocatePortFromRange( &TCPPorts
, 1024, 5000 );
797 VOID
TCPFreePort( UINT Port
) {
798 DeallocatePort( &TCPPorts
, Port
);
801 NTSTATUS TCPGetPeerAddress
802 ( PCONNECTION_ENDPOINT Connection
,
803 PTRANSPORT_ADDRESS Address
) {
804 OSK_UINT LocalAddress
, RemoteAddress
;
805 OSK_UI16 LocalPort
, RemotePort
;
806 PTA_IP_ADDRESS AddressIP
= (PTA_IP_ADDRESS
)Address
;
808 TcpipRecursiveMutexEnter( &TCPLock
, TRUE
);
811 ( Connection
->SocketContext
,
812 &LocalAddress
, &LocalPort
,
813 &RemoteAddress
, &RemotePort
);
815 AddressIP
->TAAddressCount
= 1;
816 AddressIP
->Address
[0].AddressLength
= TDI_ADDRESS_LENGTH_IP
;
817 AddressIP
->Address
[0].AddressType
= TDI_ADDRESS_TYPE_IP
;
818 AddressIP
->Address
[0].Address
[0].sin_port
= RemotePort
;
819 AddressIP
->Address
[0].Address
[0].in_addr
= RemoteAddress
;
821 TcpipRecursiveMutexLeave( &TCPLock
);
823 return STATUS_SUCCESS
;
826 VOID
TCPRemoveIRP( PCONNECTION_ENDPOINT Endpoint
, PIRP Irp
) {
828 PLIST_ENTRY ListHead
[4];
833 ListHead
[0] = &Endpoint
->SendRequest
;
834 ListHead
[1] = &Endpoint
->ReceiveRequest
;
835 ListHead
[2] = &Endpoint
->ConnectRequest
;
836 ListHead
[3] = &Endpoint
->ListenRequest
;
838 TcpipAcquireSpinLock( &Endpoint
->Lock
, &OldIrql
);
840 for( i
= 0; i
< 4; i
++ ) {
841 for( Entry
= ListHead
[i
]->Flink
;
842 Entry
!= ListHead
[i
];
843 Entry
= Entry
->Flink
) {
844 Bucket
= CONTAINING_RECORD( Entry
, TDI_BUCKET
, Entry
);
846 if( Bucket
->Request
.RequestContext
== Irp
) {
847 RemoveEntryList( &Bucket
->Entry
);
848 exFreePool( Bucket
);
854 TcpipReleaseSpinLock( &Endpoint
->Lock
, OldIrql
);