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
;
19 CLIENT_DATA ClientInfo
;
21 VOID
HandleSignalledConnection(PCONNECTION_ENDPOINT Connection
)
29 PTCP_COMPLETION_ROUTINE Complete
;
31 if (ClientInfo
.Unlocked
)
32 LockObjectAtDpcLevel(Connection
);
34 TI_DbgPrint(MID_TRACE
,("Handling signalled state on %x (%x)\n",
35 Connection
, Connection
->SocketContext
));
37 /* Things that can happen when we try the initial connection */
38 if( Connection
->SignalState
& (SEL_CONNECT
| SEL_FIN
) ) {
39 while (!IsListEmpty(&Connection
->ConnectRequest
)) {
40 Entry
= RemoveHeadList( &Connection
->ConnectRequest
);
42 Bucket
= CONTAINING_RECORD( Entry
, TDI_BUCKET
, Entry
);
44 Bucket
->Status
= (Connection
->SignalState
& SEL_CONNECT
) ? STATUS_SUCCESS
: STATUS_CANCELLED
;
45 Bucket
->Information
= 0;
47 InsertTailList(&Connection
->CompletionQueue
, &Bucket
->Entry
);
51 if( Connection
->SignalState
& (SEL_ACCEPT
| SEL_FIN
) ) {
52 /* Handle readable on a listening socket --
53 * TODO: Implement filtering
55 TI_DbgPrint(DEBUG_TCP
,("Accepting new connection on %x (Queue: %s)\n",
57 IsListEmpty(&Connection
->ListenRequest
) ?
58 "empty" : "nonempty"));
60 while (!IsListEmpty(&Connection
->ListenRequest
)) {
61 PIO_STACK_LOCATION IrpSp
;
63 Entry
= RemoveHeadList( &Connection
->ListenRequest
);
65 Bucket
= CONTAINING_RECORD( Entry
, TDI_BUCKET
, Entry
);
67 Irp
= Bucket
->Request
.RequestContext
;
68 IrpSp
= IoGetCurrentIrpStackLocation( Irp
);
70 TI_DbgPrint(DEBUG_TCP
,("Getting the socket\n"));
72 Status
= TCPServiceListeningSocket
73 ( Connection
->AddressFile
->Listener
,
74 Bucket
->AssociatedEndpoint
,
75 (PTDI_REQUEST_KERNEL
)&IrpSp
->Parameters
);
77 TI_DbgPrint(DEBUG_TCP
,("Socket: Status: %x\n"));
79 if( Status
== STATUS_PENDING
&& !(Connection
->SignalState
& SEL_FIN
) ) {
80 InsertHeadList( &Connection
->ListenRequest
, &Bucket
->Entry
);
83 Bucket
->Status
= (Status
== STATUS_PENDING
) ? STATUS_CANCELLED
: Status
;
84 Bucket
->Information
= 0;
85 DereferenceObject(Bucket
->AssociatedEndpoint
);
87 InsertTailList(&Connection
->CompletionQueue
, &Bucket
->Entry
);
92 /* Things that happen after we're connected */
93 if( Connection
->SignalState
& (SEL_READ
| SEL_FIN
) ) {
94 TI_DbgPrint(DEBUG_TCP
,("Readable: irp list %s\n",
95 IsListEmpty(&Connection
->ReceiveRequest
) ?
96 "empty" : "nonempty"));
98 while (!IsListEmpty(&Connection
->ReceiveRequest
)) {
99 OSK_UINT RecvLen
= 0, Received
= 0;
100 PVOID RecvBuffer
= 0;
102 Entry
= RemoveHeadList( &Connection
->ReceiveRequest
);
104 Bucket
= CONTAINING_RECORD( Entry
, TDI_BUCKET
, Entry
);
106 Irp
= Bucket
->Request
.RequestContext
;
107 Mdl
= Irp
->MdlAddress
;
109 TI_DbgPrint(DEBUG_TCP
,
110 ("Getting the user buffer from %x\n", Mdl
));
112 NdisQueryBuffer( Mdl
, &RecvBuffer
, &RecvLen
);
114 TI_DbgPrint(DEBUG_TCP
,
115 ("Reading %d bytes to %x\n", RecvLen
, RecvBuffer
));
117 TI_DbgPrint(DEBUG_TCP
, ("Connection: %x\n", Connection
));
120 ("Connection->SocketContext: %x\n",
121 Connection
->SocketContext
));
122 TI_DbgPrint(DEBUG_TCP
, ("RecvBuffer: %x\n", RecvBuffer
));
124 Status
= TCPTranslateError
125 ( OskitTCPRecv( Connection
->SocketContext
,
131 TI_DbgPrint(DEBUG_TCP
,("TCP Bytes: %d\n", Received
));
133 if( Status
== STATUS_PENDING
&& !(Connection
->SignalState
& SEL_FIN
) ) {
134 InsertHeadList( &Connection
->ReceiveRequest
, &Bucket
->Entry
);
137 TI_DbgPrint(DEBUG_TCP
,
138 ("Completing Receive request: %x %x\n",
139 Bucket
->Request
, Status
));
141 Bucket
->Status
= (Status
== STATUS_PENDING
) ? STATUS_CANCELLED
: Status
;
142 Bucket
->Information
= (Bucket
->Status
== STATUS_SUCCESS
) ? Received
: 0;
144 InsertTailList(&Connection
->CompletionQueue
, &Bucket
->Entry
);
148 if( Connection
->SignalState
& (SEL_WRITE
| SEL_FIN
) ) {
149 TI_DbgPrint(DEBUG_TCP
,("Writeable: irp list %s\n",
150 IsListEmpty(&Connection
->SendRequest
) ?
151 "empty" : "nonempty"));
153 while (!IsListEmpty(&Connection
->SendRequest
)) {
154 OSK_UINT SendLen
= 0, Sent
= 0;
155 PVOID SendBuffer
= 0;
157 Entry
= RemoveHeadList( &Connection
->SendRequest
);
159 Bucket
= CONTAINING_RECORD( Entry
, TDI_BUCKET
, Entry
);
161 Irp
= Bucket
->Request
.RequestContext
;
162 Mdl
= Irp
->MdlAddress
;
164 TI_DbgPrint(DEBUG_TCP
,
165 ("Getting the user buffer from %x\n", Mdl
));
167 NdisQueryBuffer( Mdl
, &SendBuffer
, &SendLen
);
169 TI_DbgPrint(DEBUG_TCP
,
170 ("Writing %d bytes to %x\n", SendLen
, SendBuffer
));
172 TI_DbgPrint(DEBUG_TCP
, ("Connection: %x\n", Connection
));
175 ("Connection->SocketContext: %x\n",
176 Connection
->SocketContext
));
178 Status
= TCPTranslateError
179 ( OskitTCPSend( Connection
->SocketContext
,
185 TI_DbgPrint(DEBUG_TCP
,("TCP Bytes: %d\n", Sent
));
187 if( Status
== STATUS_PENDING
&& !(Connection
->SignalState
& SEL_FIN
) ) {
188 InsertHeadList( &Connection
->SendRequest
, &Bucket
->Entry
);
191 TI_DbgPrint(DEBUG_TCP
,
192 ("Completing Send request: %x %x\n",
193 Bucket
->Request
, Status
));
195 Bucket
->Status
= (Status
== STATUS_PENDING
) ? STATUS_CANCELLED
: Status
;
196 Bucket
->Information
= (Bucket
->Status
== STATUS_SUCCESS
) ? Sent
: 0;
198 InsertTailList(&Connection
->CompletionQueue
, &Bucket
->Entry
);
203 ReferenceObject(Connection
);
204 if (ClientInfo
.Unlocked
)
206 UnlockObjectFromDpcLevel(Connection
);
207 KeReleaseSpinLock(&ClientInfo
.Lock
, ClientInfo
.OldIrql
);
211 UnlockObject(Connection
, Connection
->OldIrql
);
214 while ((Entry
= ExInterlockedRemoveHeadList(&Connection
->CompletionQueue
,
217 Bucket
= CONTAINING_RECORD(Entry
, TDI_BUCKET
, Entry
);
218 Complete
= Bucket
->Request
.RequestNotifyObject
;
220 Complete(Bucket
->Request
.RequestContext
, Bucket
->Status
, Bucket
->Information
);
222 ExFreePoolWithTag(Bucket
, TDI_BUCKET_TAG
);
225 if (!ClientInfo
.Unlocked
)
227 LockObject(Connection
, &OldIrql
);
231 KeAcquireSpinLock(&ClientInfo
.Lock
, &ClientInfo
.OldIrql
);
233 DereferenceObject(Connection
);
235 /* If the socket is dead, remove the reference we added for oskit */
236 if (Connection
->SignalState
& SEL_FIN
)
237 DereferenceObject(Connection
);
240 VOID
ConnectionFree(PVOID Object
) {
241 PCONNECTION_ENDPOINT Connection
= Object
;
244 TI_DbgPrint(DEBUG_TCP
, ("Freeing TCP Endpoint\n"));
246 TcpipAcquireSpinLock(&ConnectionEndpointListLock
, &OldIrql
);
247 RemoveEntryList(&Connection
->ListEntry
);
248 TcpipReleaseSpinLock(&ConnectionEndpointListLock
, OldIrql
);
250 ExFreePoolWithTag( Connection
, CONN_ENDPT_TAG
);
253 PCONNECTION_ENDPOINT
TCPAllocateConnectionEndpoint( PVOID ClientContext
) {
254 PCONNECTION_ENDPOINT Connection
=
255 ExAllocatePoolWithTag(NonPagedPool
, sizeof(CONNECTION_ENDPOINT
),
260 TI_DbgPrint(DEBUG_CPOINT
, ("Connection point file object allocated at (0x%X).\n", Connection
));
262 RtlZeroMemory(Connection
, sizeof(CONNECTION_ENDPOINT
));
264 /* Initialize spin lock that protects the connection endpoint file object */
265 KeInitializeSpinLock(&Connection
->Lock
);
266 InitializeListHead(&Connection
->ConnectRequest
);
267 InitializeListHead(&Connection
->ListenRequest
);
268 InitializeListHead(&Connection
->ReceiveRequest
);
269 InitializeListHead(&Connection
->SendRequest
);
270 InitializeListHead(&Connection
->CompletionQueue
);
272 /* Save client context pointer */
273 Connection
->ClientContext
= ClientContext
;
275 /* Add an extra reference for oskit */
276 Connection
->RefCount
= 2;
277 Connection
->Free
= ConnectionFree
;
279 /* Add connection endpoint to global list */
280 ExInterlockedInsertTailList(&ConnectionEndpointListHead
,
281 &Connection
->ListEntry
,
282 &ConnectionEndpointListLock
);
287 NTSTATUS
TCPSocket( PCONNECTION_ENDPOINT Connection
,
288 UINT Family
, UINT Type
, UINT Proto
) {
292 LockObject(Connection
, &OldIrql
);
294 TI_DbgPrint(DEBUG_TCP
,("Called: Connection %x, Family %d, Type %d, "
296 Connection
, Family
, Type
, Proto
));
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 UnlockObject(Connection
, OldIrql
);
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
325 TI_DbgPrint(DEBUG_TCP
,("Sending packet %d (%d) to oskit\n",
327 IPPacket
->HeaderSize
));
329 KeAcquireSpinLock(&ClientInfo
.Lock
, &OldIrql
);
330 ClientInfo
.Unlocked
= TRUE
;
331 ClientInfo
.OldIrql
= OldIrql
;
333 OskitTCPReceiveDatagram( IPPacket
->Header
,
335 IPPacket
->HeaderSize
);
337 ClientInfo
.Unlocked
= FALSE
;
338 KeReleaseSpinLock(&ClientInfo
.Lock
, OldIrql
);
342 int TCPSocketState( void *ClientData
,
344 void *WhichConnection
,
347 int TCPPacketSend( void *ClientData
,
351 POSK_IFADDR
TCPFindInterface( void *ClientData
,
354 OSK_SOCKADDR
*ReqAddr
);
356 NTSTATUS
TCPMemStartup( void );
357 void *TCPMalloc( void *ClientData
,
358 OSK_UINT bytes
, OSK_PCHAR file
, OSK_UINT line
);
359 void TCPFree( void *ClientData
,
360 void *data
, OSK_PCHAR file
, OSK_UINT line
);
361 void TCPMemShutdown( void );
363 OSKITTCP_EVENT_HANDLERS EventHandlers
= {
364 NULL
, /* Client Data */
365 TCPSocketState
, /* SocketState */
366 TCPPacketSend
, /* PacketSend */
367 TCPFindInterface
, /* FindInterface */
368 TCPMalloc
, /* Malloc */
374 static KEVENT TimerLoopEvent
;
375 static HANDLE TimerThreadHandle
;
378 * We are running 2 timers here, one with a 200ms interval (fast) and the other
379 * with a 500ms interval (slow). So we need to time out at 200, 400, 500, 600,
380 * 800, 1000 and process the "fast" events at 200, 400, 600, 800, 1000 and the
381 * "slow" events at 500 and 1000.
384 TimerThread(PVOID Context
)
386 LARGE_INTEGER Timeout
;
388 unsigned Current
, NextFast
, NextSlow
, Next
;
395 if (Next
== NextFast
) {
398 if (Next
== NextSlow
) {
401 Next
= min(NextFast
, NextSlow
);
402 Timeout
.QuadPart
= (LONGLONG
) (Next
- Current
) * -1000000; /* 100 ms */
403 Status
= KeWaitForSingleObject(&TimerLoopEvent
, Executive
, KernelMode
,
405 if (Status
!= STATUS_TIMEOUT
) {
406 PsTerminateSystemThread(Status
);
409 TimerOskitTCP( Next
== NextFast
, Next
== NextSlow
);
424 KeInitializeEvent(&TimerLoopEvent
, NotificationEvent
, FALSE
);
425 PsCreateSystemThread(&TimerThreadHandle
, THREAD_ALL_ACCESS
, 0, 0, 0,
429 NTSTATUS
TCPStartup(VOID
)
431 * FUNCTION: Initializes the TCP subsystem
433 * Status of operation
438 Status
= TCPMemStartup();
439 if ( ! NT_SUCCESS(Status
) ) {
443 Status
= PortsStartup( &TCPPorts
, 1, 0xfffe );
444 if( !NT_SUCCESS(Status
) ) {
449 KeInitializeSpinLock(&ClientInfo
.Lock
);
450 ClientInfo
.Unlocked
= FALSE
;
452 RegisterOskitTCPEventHandlers( &EventHandlers
);
455 /* Register this protocol with IP layer */
456 IPRegisterProtocol(IPPROTO_TCP
, TCPReceive
);
458 ExInitializeNPagedLookasideList(
459 &TCPSegmentList
, /* Lookaside list */
460 NULL
, /* Allocate routine */
461 NULL
, /* Free routine */
463 sizeof(TCP_SEGMENT
), /* Size of each entry */
469 TCPInitialized
= TRUE
;
471 return STATUS_SUCCESS
;
475 NTSTATUS
TCPShutdown(VOID
)
477 * FUNCTION: Shuts down the TCP subsystem
479 * Status of operation
482 LARGE_INTEGER WaitForThread
;
485 return STATUS_SUCCESS
;
487 WaitForThread
.QuadPart
= -2500000; /* 250 ms */
488 KeSetEvent(&TimerLoopEvent
, IO_NO_INCREMENT
, FALSE
);
489 ZwWaitForSingleObject(TimerThreadHandle
, FALSE
, &WaitForThread
);
491 /* Deregister this protocol with IP layer */
492 IPRegisterProtocol(IPPROTO_TCP
, NULL
);
494 ExDeleteNPagedLookasideList(&TCPSegmentList
);
496 TCPInitialized
= FALSE
;
500 PortsShutdown( &TCPPorts
);
504 return STATUS_SUCCESS
;
507 NTSTATUS
TCPTranslateError( int OskitError
) {
510 switch( OskitError
) {
511 case 0: Status
= STATUS_SUCCESS
; break;
512 case OSK_EADDRNOTAVAIL
: Status
= STATUS_INVALID_ADDRESS
; break;
513 case OSK_EADDRINUSE
: Status
= STATUS_ADDRESS_ALREADY_EXISTS
; break;
514 case OSK_EAFNOSUPPORT
: Status
= STATUS_INVALID_CONNECTION
; break;
515 case OSK_ECONNREFUSED
: Status
= STATUS_REMOTE_NOT_LISTENING
; break;
516 case OSK_ECONNRESET
: Status
= STATUS_REMOTE_DISCONNECT
; break;
517 case OSK_ECONNABORTED
: Status
= STATUS_LOCAL_DISCONNECT
; break;
518 case OSK_EWOULDBLOCK
:
519 case OSK_EINPROGRESS
: Status
= STATUS_PENDING
; break;
520 case OSK_EINVAL
: Status
= STATUS_INVALID_PARAMETER
; break;
522 case OSK_ENOBUFS
: Status
= STATUS_INSUFFICIENT_RESOURCES
; break;
523 case OSK_ESHUTDOWN
: Status
= STATUS_FILE_CLOSED
; break;
524 case OSK_EMSGSIZE
: Status
= STATUS_BUFFER_TOO_SMALL
; break;
525 case OSK_ETIMEDOUT
: Status
= STATUS_TIMEOUT
; break;
526 case OSK_ENETUNREACH
: Status
= STATUS_NETWORK_UNREACHABLE
; break;
527 case OSK_EFAULT
: Status
= STATUS_ACCESS_VIOLATION
; break;
529 DbgPrint("OskitTCP returned unhandled error code: %d\n", OskitError
);
530 Status
= STATUS_INVALID_CONNECTION
;
534 TI_DbgPrint(DEBUG_TCP
,("Error %d -> %x\n", OskitError
, Status
));
539 ( PCONNECTION_ENDPOINT Connection
,
540 PTDI_CONNECTION_INFORMATION ConnInfo
,
541 PTDI_CONNECTION_INFORMATION ReturnInfo
,
542 PTCP_COMPLETION_ROUTINE Complete
,
545 SOCKADDR_IN AddressToConnect
= { 0 }, AddressToBind
= { 0 };
546 IP_ADDRESS RemoteAddress
;
549 PNEIGHBOR_CACHE_ENTRY NCE
;
552 TI_DbgPrint(DEBUG_TCP
,("TCPConnect: Called\n"));
554 Status
= AddrBuildAddress
555 ((PTRANSPORT_ADDRESS
)ConnInfo
->RemoteAddress
,
559 if (!NT_SUCCESS(Status
)) {
560 TI_DbgPrint(DEBUG_TCP
, ("Could not AddrBuildAddress in TCPConnect\n"));
564 /* Freed in TCPSocketState */
565 TI_DbgPrint(DEBUG_TCP
,
566 ("Connecting to address %x:%x\n",
567 RemoteAddress
.Address
.IPv4Address
,
570 AddressToConnect
.sin_family
= AF_INET
;
571 AddressToBind
= AddressToConnect
;
573 LockObject(Connection
, &OldIrql
);
575 if (!Connection
->AddressFile
)
577 UnlockObject(Connection
, OldIrql
);
578 return STATUS_INVALID_PARAMETER
;
581 if (AddrIsUnspecified(&Connection
->AddressFile
->Address
))
583 if (!(NCE
= RouteGetRouteToDestination(&RemoteAddress
)))
585 UnlockObject(Connection
, OldIrql
);
586 return STATUS_NETWORK_UNREACHABLE
;
589 AddressToBind
.sin_addr
.s_addr
= NCE
->Interface
->Unicast
.Address
.IPv4Address
;
593 AddressToBind
.sin_addr
.s_addr
= Connection
->AddressFile
->Address
.Address
.IPv4Address
;
596 Status
= TCPTranslateError
597 ( OskitTCPBind( Connection
->SocketContext
,
599 sizeof(AddressToBind
) ) );
601 if (NT_SUCCESS(Status
)) {
602 memcpy( &AddressToConnect
.sin_addr
,
603 &RemoteAddress
.Address
.IPv4Address
,
604 sizeof(AddressToConnect
.sin_addr
) );
605 AddressToConnect
.sin_port
= RemotePort
;
607 Status
= TCPTranslateError
608 ( OskitTCPConnect( Connection
->SocketContext
,
610 sizeof(AddressToConnect
) ) );
612 if (Status
== STATUS_PENDING
)
614 Bucket
= ExAllocatePoolWithTag( NonPagedPool
, sizeof(*Bucket
), TDI_BUCKET_TAG
);
617 UnlockObject(Connection
, OldIrql
);
618 return STATUS_NO_MEMORY
;
621 Bucket
->Request
.RequestNotifyObject
= (PVOID
)Complete
;
622 Bucket
->Request
.RequestContext
= Context
;
624 InsertTailList( &Connection
->ConnectRequest
, &Bucket
->Entry
);
628 UnlockObject(Connection
, OldIrql
);
633 NTSTATUS TCPDisconnect
634 ( PCONNECTION_ENDPOINT Connection
,
636 PTDI_CONNECTION_INFORMATION ConnInfo
,
637 PTDI_CONNECTION_INFORMATION ReturnInfo
,
638 PTCP_COMPLETION_ROUTINE Complete
,
640 NTSTATUS Status
= STATUS_INVALID_PARAMETER
;
643 TI_DbgPrint(DEBUG_TCP
,("started\n"));
645 LockObject(Connection
, &OldIrql
);
647 if (Flags
& TDI_DISCONNECT_RELEASE
)
648 Status
= TCPTranslateError(OskitTCPDisconnect(Connection
->SocketContext
));
650 if ((Flags
& TDI_DISCONNECT_ABORT
) || !Flags
)
651 Status
= TCPTranslateError(OskitTCPShutdown(Connection
->SocketContext
, FWRITE
| FREAD
));
653 UnlockObject(Connection
, OldIrql
);
655 TI_DbgPrint(DEBUG_TCP
,("finished %x\n", Status
));
661 ( PCONNECTION_ENDPOINT Connection
)
667 /* We don't rely on SocketContext == NULL for socket
668 * closure anymore but we still need it to determine
669 * if we caused the closure
671 LockObject(Connection
, &OldIrql
);
672 Socket
= Connection
->SocketContext
;
673 Connection
->SocketContext
= NULL
;
675 /* Don't try to close again if the other side closed us already */
676 if (!(Connection
->SignalState
& SEL_FIN
))
678 /* We need to close here otherwise oskit will never indicate
679 * SEL_FIN and we will never fully close the connection */
680 Status
= TCPTranslateError( OskitTCPClose( Socket
) );
682 if (!NT_SUCCESS(Status
))
684 Connection
->SocketContext
= Socket
;
685 UnlockObject(Connection
, OldIrql
);
691 /* We are already closed by the other end so return success */
692 Status
= STATUS_SUCCESS
;
695 if (Connection
->AddressFile
)
696 DereferenceObject(Connection
->AddressFile
);
698 UnlockObject(Connection
, OldIrql
);
700 DereferenceObject(Connection
);
705 NTSTATUS TCPReceiveData
706 ( PCONNECTION_ENDPOINT Connection
,
709 PULONG BytesReceived
,
711 PTCP_COMPLETION_ROUTINE Complete
,
714 UINT DataLen
, Received
= 0;
719 TI_DbgPrint(DEBUG_TCP
,("Called for %d bytes (on socket %x)\n",
720 ReceiveLength
, Connection
->SocketContext
));
722 NdisQueryBuffer( Buffer
, &DataBuffer
, &DataLen
);
724 TI_DbgPrint(DEBUG_TCP
,("TCP>|< Got an MDL %x (%x:%d)\n", Buffer
, DataBuffer
, DataLen
));
726 LockObject(Connection
, &OldIrql
);
728 Status
= TCPTranslateError
730 ( Connection
->SocketContext
,
736 TI_DbgPrint(DEBUG_TCP
,("OskitTCPReceive: %x, %d\n", Status
, Received
));
738 /* Keep this request around ... there was no data yet */
739 if( Status
== STATUS_PENDING
) {
740 /* Freed in TCPSocketState */
741 Bucket
= ExAllocatePoolWithTag( NonPagedPool
, sizeof(*Bucket
), TDI_BUCKET_TAG
);
743 TI_DbgPrint(DEBUG_TCP
,("Failed to allocate bucket\n"));
744 UnlockObject(Connection
, OldIrql
);
745 return STATUS_NO_MEMORY
;
748 Bucket
->Request
.RequestNotifyObject
= Complete
;
749 Bucket
->Request
.RequestContext
= Context
;
752 InsertTailList( &Connection
->ReceiveRequest
, &Bucket
->Entry
);
753 TI_DbgPrint(DEBUG_TCP
,("Queued read irp\n"));
755 TI_DbgPrint(DEBUG_TCP
,("Got status %x, bytes %d\n", Status
, Received
));
756 *BytesReceived
= Received
;
759 UnlockObject(Connection
, OldIrql
);
761 TI_DbgPrint(DEBUG_TCP
,("Status %x\n", Status
));
767 ( PCONNECTION_ENDPOINT Connection
,
772 PTCP_COMPLETION_ROUTINE Complete
,
779 LockObject(Connection
, &OldIrql
);
781 TI_DbgPrint(DEBUG_TCP
,("Called for %d bytes (on socket %x)\n",
782 SendLength
, Connection
->SocketContext
));
784 TI_DbgPrint(DEBUG_TCP
,("Connection = %x\n", Connection
));
785 TI_DbgPrint(DEBUG_TCP
,("Connection->SocketContext = %x\n",
786 Connection
->SocketContext
));
788 Status
= TCPTranslateError
789 ( OskitTCPSend( Connection
->SocketContext
,
790 (OSK_PCHAR
)BufferData
, SendLength
,
793 TI_DbgPrint(DEBUG_TCP
,("OskitTCPSend: %x, %d\n", Status
, Sent
));
795 /* Keep this request around ... there was no data yet */
796 if( Status
== STATUS_PENDING
) {
797 /* Freed in TCPSocketState */
798 Bucket
= ExAllocatePoolWithTag( NonPagedPool
, sizeof(*Bucket
), TDI_BUCKET_TAG
);
800 UnlockObject(Connection
, OldIrql
);
801 TI_DbgPrint(DEBUG_TCP
,("Failed to allocate bucket\n"));
802 return STATUS_NO_MEMORY
;
805 Bucket
->Request
.RequestNotifyObject
= Complete
;
806 Bucket
->Request
.RequestContext
= Context
;
809 InsertTailList( &Connection
->SendRequest
, &Bucket
->Entry
);
810 TI_DbgPrint(DEBUG_TCP
,("Queued write irp\n"));
812 TI_DbgPrint(DEBUG_TCP
,("Got status %x, bytes %d\n", Status
, Sent
));
816 UnlockObject(Connection
, OldIrql
);
818 TI_DbgPrint(DEBUG_TCP
,("Status %x\n", Status
));
823 UINT
TCPAllocatePort( UINT HintPort
) {
825 if( AllocatePort( &TCPPorts
, HintPort
) ) return HintPort
;
828 (MID_TRACE
,("We got a hint port but couldn't allocate it\n"));
831 } else return AllocatePortFromRange( &TCPPorts
, 1024, 5000 );
834 VOID
TCPFreePort( UINT Port
) {
835 DeallocatePort( &TCPPorts
, Port
);
838 NTSTATUS TCPGetSockAddress
839 ( PCONNECTION_ENDPOINT Connection
,
840 PTRANSPORT_ADDRESS Address
,
841 BOOLEAN GetRemote
) {
842 OSK_UINT LocalAddress
, RemoteAddress
;
843 OSK_UI16 LocalPort
, RemotePort
;
844 PTA_IP_ADDRESS AddressIP
= (PTA_IP_ADDRESS
)Address
;
848 LockObject(Connection
, &OldIrql
);
850 Status
= TCPTranslateError(OskitTCPGetAddress(Connection
->SocketContext
,
851 &LocalAddress
, &LocalPort
,
852 &RemoteAddress
, &RemotePort
));
854 UnlockObject(Connection
, OldIrql
);
856 if (!NT_SUCCESS(Status
))
859 AddressIP
->TAAddressCount
= 1;
860 AddressIP
->Address
[0].AddressLength
= TDI_ADDRESS_LENGTH_IP
;
861 AddressIP
->Address
[0].AddressType
= TDI_ADDRESS_TYPE_IP
;
862 AddressIP
->Address
[0].Address
[0].sin_port
= GetRemote
? RemotePort
: LocalPort
;
863 AddressIP
->Address
[0].Address
[0].in_addr
= GetRemote
? RemoteAddress
: LocalAddress
;
868 BOOLEAN
TCPRemoveIRP( PCONNECTION_ENDPOINT Endpoint
, PIRP Irp
) {
870 PLIST_ENTRY ListHead
[4];
874 BOOLEAN Found
= FALSE
;
876 ListHead
[0] = &Endpoint
->SendRequest
;
877 ListHead
[1] = &Endpoint
->ReceiveRequest
;
878 ListHead
[2] = &Endpoint
->ConnectRequest
;
879 ListHead
[3] = &Endpoint
->ListenRequest
;
881 LockObject(Endpoint
, &OldIrql
);
883 for( i
= 0; i
< 4; i
++ )
885 for( Entry
= ListHead
[i
]->Flink
;
886 Entry
!= ListHead
[i
];
887 Entry
= Entry
->Flink
)
889 Bucket
= CONTAINING_RECORD( Entry
, TDI_BUCKET
, Entry
);
890 if( Bucket
->Request
.RequestContext
== Irp
)
892 RemoveEntryList( &Bucket
->Entry
);
893 ExFreePoolWithTag( Bucket
, TDI_BUCKET_TAG
);
900 UnlockObject(Endpoint
, OldIrql
);