- Get IP to compile.
[reactos.git] / reactos / drivers / lib / ip / transport / tcp / tcp.c
index c46b8ce..ad572cd 100644 (file)
@@ -30,31 +30,42 @@ static VOID HandleSignalledConnection( PCONNECTION_ENDPOINT Connection,
     PIRP Irp;
     PMDL Mdl;
 
+    TI_DbgPrint(MID_TRACE,("Handling signalled state on %x (%x)\n",
+                           Connection, Connection->SocketContext));
+
     /* Things that can happen when we try the initial connection */
-    if( ((NewState & SEL_CONNECT) || (NewState & SEL_FIN)) &&
-       !(Connection->State & (SEL_CONNECT | SEL_FIN)) ) {
+    if( NewState & SEL_CONNECT ) {
        while( !IsListEmpty( &Connection->ConnectRequest ) ) {
-           Connection->State |= NewState & (SEL_CONNECT | SEL_FIN);
-           Entry = RemoveHeadList( &Connection->ConnectRequest );
-           Bucket = CONTAINING_RECORD( Entry, TDI_BUCKET, Entry );
-           Complete = Bucket->Request.RequestNotifyObject;
-           TI_DbgPrint(DEBUG_TCP,
-                       ("Completing Connect Request %x\n", Bucket->Request));
-           if( NewState & SEL_FIN ) Status = STATUS_CONNECTION_REFUSED;
-           Complete( Bucket->Request.RequestContext, Status, 0 );
-           /* Frees the bucket allocated in TCPConnect */
-           PoolFreeBuffer( Bucket );
-       }
+            Connection->State |= NewState;
+            Entry = RemoveHeadList( &Connection->ConnectRequest );
+            TI_DbgPrint(DEBUG_TCP, ("Connect Event\n"));
+
+            Bucket = CONTAINING_RECORD( Entry, TDI_BUCKET, Entry );
+            Complete = Bucket->Request.RequestNotifyObject;
+            TI_DbgPrint(DEBUG_TCP,
+                        ("Completing Request %x\n", Bucket->Request));
+
+            if( (NewState & (SEL_CONNECT | SEL_FIN)) ==
+                (SEL_CONNECT | SEL_FIN) )
+                Status = STATUS_CONNECTION_REFUSED;
+            else
+                Status = STATUS_SUCCESS;
+
+            Complete( Bucket->Request.RequestContext, Status, 0 );
+
+            /* Frees the bucket allocated in TCPConnect */
+            PoolFreeBuffer( Bucket );
+        }
     }
 
-    if( (NewState & SEL_ACCEPT) ) {
-       /* Handle readable on a listening socket -- 
-        * TODO: Implement filtering 
+    if( NewState & SEL_ACCEPT ) {
+       /* Handle readable on a listening socket --
+        * TODO: Implement filtering
         */
 
        TI_DbgPrint(DEBUG_TCP,("Accepting new connection on %x (Queue: %s)\n",
                               Connection,
-                              IsListEmpty(&Connection->ListenRequest) ? 
+                              IsListEmpty(&Connection->ListenRequest) ?
                               "empty" : "nonempty"));
 
        while( !IsListEmpty( &Connection->ListenRequest ) ) {
@@ -63,14 +74,14 @@ static VOID HandleSignalledConnection( PCONNECTION_ENDPOINT Connection,
            Entry = RemoveHeadList( &Connection->ListenRequest );
            Bucket = CONTAINING_RECORD( Entry, TDI_BUCKET, Entry );
            Complete = Bucket->Request.RequestNotifyObject;
-           
+
            Irp = Bucket->Request.RequestContext;
            IrpSp = IoGetCurrentIrpStackLocation( Irp );
-           
+
            TI_DbgPrint(DEBUG_TCP,("Getting the socket\n"));
            Status = TCPServiceListeningSocket
-               ( Connection->AddressFile->Listener, 
-                 Bucket->AssociatedEndpoint, 
+               ( Connection->AddressFile->Listener,
+                 Bucket->AssociatedEndpoint,
                  (PTDI_REQUEST_KERNEL)&IrpSp->Parameters );
 
            TI_DbgPrint(DEBUG_TCP,("Socket: Status: %x\n"));
@@ -78,17 +89,17 @@ static VOID HandleSignalledConnection( PCONNECTION_ENDPOINT Connection,
            if( Status == STATUS_PENDING ) {
                InsertHeadList( &Connection->ListenRequest, &Bucket->Entry );
                break;
-           } else 
+           } else
                Complete( Bucket->Request.RequestContext, Status, 0 );
        }
     }
 
     /* Things that happen after we're connected */
-    if( (NewState & SEL_READ) ) {
+    if( NewState & SEL_READ ) {
        TI_DbgPrint(DEBUG_TCP,("Readable: irp list %s\n",
                               IsListEmpty(&Connection->ReceiveRequest) ?
                               "empty" : "nonempty"));
-       
+
        while( !IsListEmpty( &Connection->ReceiveRequest ) ) {
            OSK_UINT RecvLen = 0, Received = 0;
            OSK_PCHAR RecvBuffer = 0;
@@ -110,11 +121,11 @@ static VOID HandleSignalledConnection( PCONNECTION_ENDPOINT Connection,
 
            TI_DbgPrint(DEBUG_TCP, ("Connection: %x\n", Connection));
            TI_DbgPrint
-               (DEBUG_TCP, 
-                ("Connection->SocketContext: %x\n", 
+               (DEBUG_TCP,
+                ("Connection->SocketContext: %x\n",
                  Connection->SocketContext));
            TI_DbgPrint(DEBUG_TCP, ("RecvBuffer: %x\n", RecvBuffer));
-           
+
            Status = TCPTranslateError
                ( OskitTCPRecv( Connection->SocketContext,
                                RecvBuffer,
@@ -123,11 +134,11 @@ static VOID HandleSignalledConnection( PCONNECTION_ENDPOINT Connection,
                                0 ) );
 
            TI_DbgPrint(DEBUG_TCP,("TCP Bytes: %d\n", Received));
-           
+
            if( Status == STATUS_SUCCESS ) {
                TI_DbgPrint(DEBUG_TCP,("Received %d bytes with status %x\n",
                                       Received, Status));
-               
+
                Complete( Bucket->Request.RequestContext,
                          STATUS_SUCCESS, Received );
            } else if( Status == STATUS_PENDING ) {
@@ -142,18 +153,89 @@ static VOID HandleSignalledConnection( PCONNECTION_ENDPOINT Connection,
            }
        }
     }
-    if( NewState & SEL_FIN ) {
-       TI_DbgPrint(DEBUG_TCP, ("EOF From socket\n"));
-       
-       while( !IsListEmpty( &Connection->ReceiveRequest ) ) {
-           Entry = RemoveHeadList( &Connection->ReceiveRequest );
+    if( NewState & SEL_WRITE ) {
+       TI_DbgPrint(DEBUG_TCP,("Writeable: irp list %s\n",
+                              IsListEmpty(&Connection->ReceiveRequest) ?
+                              "empty" : "nonempty"));
+
+       while( !IsListEmpty( &Connection->SendRequest ) ) {
+           OSK_UINT SendLen = 0, Sent = 0;
+           OSK_PCHAR SendBuffer = 0;
+
+           Entry = RemoveHeadList( &Connection->SendRequest );
            Bucket = CONTAINING_RECORD( Entry, TDI_BUCKET, Entry );
            Complete = Bucket->Request.RequestNotifyObject;
 
-           Complete( Bucket->Request.RequestContext, STATUS_SUCCESS, 0 );
+           Irp = Bucket->Request.RequestContext;
+           Mdl = Irp->MdlAddress;
+
+           TI_DbgPrint(DEBUG_TCP,
+                       ("Getting the user buffer from %x\n", Mdl));
+
+           NdisQueryBuffer( Mdl, &SendBuffer, &SendLen );
+
+           TI_DbgPrint(DEBUG_TCP,
+                       ("Writing %d bytes to %x\n", SendLen, SendBuffer));
+
+           TI_DbgPrint(DEBUG_TCP, ("Connection: %x\n", Connection));
+           TI_DbgPrint
+               (DEBUG_TCP,
+                ("Connection->SocketContext: %x\n",
+                 Connection->SocketContext));
+
+           Status = TCPTranslateError
+               ( OskitTCPSend( Connection->SocketContext,
+                               SendBuffer,
+                               SendLen,
+                               &Sent,
+                               0 ) );
+
+           TI_DbgPrint(DEBUG_TCP,("TCP Bytes: %d\n", Sent));
+
+           if( Status == STATUS_SUCCESS ) {
+               TI_DbgPrint(DEBUG_TCP,("Sent %d bytes with status %x\n",
+                                      Sent, Status));
+
+               Complete( Bucket->Request.RequestContext,
+                         STATUS_SUCCESS, Sent );
+           } else if( Status == STATUS_PENDING ) {
+               InsertHeadList
+                   ( &Connection->SendRequest, &Bucket->Entry );
+               break;
+           } else {
+               TI_DbgPrint(DEBUG_TCP,
+                           ("Completing Send request: %x %x\n",
+                            Bucket->Request, Status));
+               Complete( Bucket->Request.RequestContext, Status, 0 );
+           }
        }
     }
 
+    if( NewState & SEL_FIN ) {
+        PLIST_ENTRY ListsToErase[4];
+        NTSTATUS    IrpStatus[4];
+        UINT i;
+
+       TI_DbgPrint(DEBUG_TCP, ("EOF From socket\n"));
+
+        ListsToErase[0] = &Connection->ReceiveRequest;
+        IrpStatus   [0] = STATUS_SUCCESS;
+        ListsToErase[1] = &Connection->ListenRequest;
+        IrpStatus   [1] = STATUS_UNSUCCESSFUL;
+        ListsToErase[2] = &Connection->ConnectRequest;
+        IrpStatus   [2] = STATUS_UNSUCCESSFUL;
+        ListsToErase[3] = 0;
+
+        for( i = 0; ListsToErase[i]; i++ ) {
+            while( !IsListEmpty( ListsToErase[i] ) ) {
+                Entry = RemoveHeadList( ListsToErase[i] );
+                Bucket = CONTAINING_RECORD( Entry, TDI_BUCKET, Entry );
+                Complete = Bucket->Request.RequestNotifyObject;
+                Complete( Bucket->Request.RequestContext, STATUS_SUCCESS, 0 );
+            }
+        }
+    }
+
     Connection->Signalled = FALSE;
 }
 
@@ -170,24 +252,25 @@ VOID DrainSignals() {
 }
 
 PCONNECTION_ENDPOINT TCPAllocateConnectionEndpoint( PVOID ClientContext ) {
-    PCONNECTION_ENDPOINT Connection = 
+    PCONNECTION_ENDPOINT Connection =
        ExAllocatePool(NonPagedPool, sizeof(CONNECTION_ENDPOINT));
     if (!Connection)
        return Connection;
-    
+
     TI_DbgPrint(DEBUG_CPOINT, ("Connection point file object allocated at (0x%X).\n", Connection));
-    
+
     RtlZeroMemory(Connection, sizeof(CONNECTION_ENDPOINT));
-    
+
     /* Initialize spin lock that protects the connection endpoint file object */
     TcpipInitializeSpinLock(&Connection->Lock);
     InitializeListHead(&Connection->ConnectRequest);
     InitializeListHead(&Connection->ListenRequest);
     InitializeListHead(&Connection->ReceiveRequest);
-    
+    InitializeListHead(&Connection->SendRequest);
+
     /* Save client context pointer */
     Connection->ClientContext = ClientContext;
-    
+
     return Connection;
 }
 
@@ -197,7 +280,7 @@ VOID TCPFreeConnectionEndpoint( PCONNECTION_ENDPOINT Connection ) {
     ExFreePool( Connection );
 }
 
-NTSTATUS TCPSocket( PCONNECTION_ENDPOINT Connection, 
+NTSTATUS TCPSocket( PCONNECTION_ENDPOINT Connection,
                    UINT Family, UINT Type, UINT Proto ) {
     NTSTATUS Status;
 
@@ -231,14 +314,14 @@ VOID TCPReceive(PIP_INTERFACE Interface, PIP_PACKET IPPacket)
  *     This is the low level interface for receiving TCP data
  */
 {
-    TI_DbgPrint(DEBUG_TCP,("Sending packet %d (%d) to oskit\n", 
+    TI_DbgPrint(DEBUG_TCP,("Sending packet %d (%d) to oskit\n",
                           IPPacket->TotalSize,
                           IPPacket->HeaderSize));
 
     TcpipRecursiveMutexEnter( &TCPLock, TRUE );
 
-    OskitTCPReceiveDatagram( IPPacket->Header, 
-                            IPPacket->TotalSize, 
+    OskitTCPReceiveDatagram( IPPacket->Header,
+                            IPPacket->TotalSize,
                             IPPacket->HeaderSize );
 
     DrainSignals();
@@ -261,10 +344,12 @@ POSK_IFADDR TCPFindInterface( void *ClientData,
                              OSK_UINT FindType,
                              OSK_SOCKADDR *ReqAddr );
 
+NTSTATUS TCPMemStartup( void );
 void *TCPMalloc( void *ClientData,
                 OSK_UINT bytes, OSK_PCHAR file, OSK_UINT line );
 void TCPFree( void *ClientData,
              void *data, OSK_PCHAR file, OSK_UINT line );
+void TCPMemShutdown( void );
 
 int TCPSleep( void *ClientData, void *token, int priority, char *msg,
              int tmio );
@@ -282,6 +367,68 @@ OSKITTCP_EVENT_HANDLERS EventHandlers = {
     TCPWakeup         /* Wakeup */
 };
 
+static KEVENT TimerLoopEvent;
+static HANDLE TimerThreadHandle;
+
+/*
+ * We are running 2 timers here, one with a 200ms interval (fast) and the other
+ * with a 500ms interval (slow). So we need to time out at 200, 400, 500, 600,
+ * 800, 1000 and process the "fast" events at 200, 400, 600, 800, 1000 and the
+ * "slow" events at 500 and 1000.
+ */
+static VOID NTAPI
+TimerThread(PVOID Context)
+{
+    LARGE_INTEGER Timeout;
+    NTSTATUS Status;
+    unsigned Current, NextFast, NextSlow, Next;
+
+    Current = 0;
+    Next = 0;
+    NextFast = 0;
+    NextSlow = 0;
+    while ( 1 ) {
+        if (Next == NextFast) {
+            NextFast += 2;
+        }
+        if (Next == NextSlow) {
+            NextSlow += 5;
+        }
+        Next = min(NextFast, NextSlow);
+        Timeout.QuadPart = (LONGLONG) (Next - Current) * -1000000; /* 100 ms */
+        Status = KeWaitForSingleObject(&TimerLoopEvent, Executive, KernelMode,
+                                       FALSE, &Timeout);
+        if (STATUS_SUCCESS == Status) {
+            PsTerminateSystemThread(STATUS_SUCCESS);
+        }
+        ASSERT(STATUS_TIMEOUT == Status);
+
+        TcpipRecursiveMutexEnter( &TCPLock, TRUE );
+        TimerOskitTCP( Next == NextFast, Next == NextSlow );
+        if (Next == NextSlow) {
+            DrainSignals();
+        }
+        TcpipRecursiveMutexLeave( &TCPLock );
+
+        Current = Next;
+        if (10 <= Current) {
+            Current = 0;
+            Next = 0;
+            NextFast = 0;
+            NextSlow = 0;
+        }
+    }
+}
+
+static VOID
+StartTimer(VOID)
+{
+    KeInitializeEvent(&TimerLoopEvent, NotificationEvent, FALSE);
+    PsCreateSystemThread(&TimerThreadHandle, THREAD_ALL_ACCESS, 0, 0, 0,
+                         TimerThread, NULL);
+}
+
+
 NTSTATUS TCPStartup(VOID)
 /*
  * FUNCTION: Initializes the TCP subsystem
@@ -289,19 +436,25 @@ NTSTATUS TCPStartup(VOID)
  *     Status of operation
  */
 {
+    NTSTATUS Status;
+
     TcpipRecursiveMutexInit( &TCPLock );
     ExInitializeFastMutex( &SleepingThreadsLock );
-    InitializeListHead( &SleepingThreadsList );    
+    InitializeListHead( &SleepingThreadsList );
     InitializeListHead( &SignalledConnections );
+    Status = TCPMemStartup();
+    if ( ! NT_SUCCESS(Status) ) {
+       return Status;
+    }
 
     PortsStartup( &TCPPorts, 1, 0xfffe );
 
     RegisterOskitTCPEventHandlers( &EventHandlers );
     InitOskitTCP();
-    
+
     /* Register this protocol with IP layer */
     IPRegisterProtocol(IPPROTO_TCP, TCPReceive);
-    
+
     ExInitializeNPagedLookasideList(
        &TCPSegmentList,                /* Lookaside list */
        NULL,                           /* Allocate routine */
@@ -310,9 +463,11 @@ NTSTATUS TCPStartup(VOID)
        sizeof(TCP_SEGMENT),            /* Size of each entry */
        TAG('T','C','P','S'),           /* Tag */
        0);                             /* Depth */
-    
+
+    StartTimer();
+
     TCPInitialized = TRUE;
-    
+
     return STATUS_SUCCESS;
 }
 
@@ -324,20 +479,28 @@ NTSTATUS TCPShutdown(VOID)
  *     Status of operation
  */
 {
+    LARGE_INTEGER WaitForThread;
+
     if (!TCPInitialized)
        return STATUS_SUCCESS;
-    
+
+    WaitForThread.QuadPart = -2500000; /* 250 ms */
+    KeSetEvent(&TimerLoopEvent, IO_NO_INCREMENT, FALSE);
+    ZwWaitForSingleObject(TimerThreadHandle, FALSE, &WaitForThread);
+
     /* Deregister this protocol with IP layer */
     IPRegisterProtocol(IPPROTO_TCP, NULL);
-    
+
     ExDeleteNPagedLookasideList(&TCPSegmentList);
-    
+
     TCPInitialized = FALSE;
 
     DeinitOskitTCP();
 
     PortsShutdown( &TCPPorts );
 
+    TCPMemShutdown();
+
     return STATUS_SUCCESS;
 }
 
@@ -359,39 +522,6 @@ NTSTATUS TCPTranslateError( int OskitError ) {
     return Status;
 }
 
-#if 0
-NTSTATUS TCPBind
-( PCONNECTION_ENDPOINT Connection,
-  PTDI_CONNECTION_INFORMATION ConnInfo ) {
-    NTSTATUS Status;
-    SOCKADDR_IN AddressToConnect;
-    PIP_ADDRESS LocalAddress;
-    USHORT LocalPort;
-
-    TI_DbgPrint(DEBUG_TCP,("Called\n"));
-
-    Status = AddrBuildAddress
-       ((PTA_ADDRESS)ConnInfo->LocalAddress,
-        &LocalAddress,
-        &LocalPort);
-
-    AddressToBind.sin_family = AF_INET;
-    memcpy( &AddressToBind.sin_addr, 
-           &LocalAddress->Address.IPv4Address,
-           sizeof(AddressToBind.sin_addr) );
-    AddressToBind.sin_port = LocalPort;
-
-    Status = OskitTCPBind( Connection->SocketContext,
-                          Connection,
-                          &AddressToBind, 
-                          sizeof(AddressToBind));
-
-    TI_DbgPrint(DEBUG_TCP,("Leaving %x\n", Status));
-
-    return Status;
-}
-#endif
-
 NTSTATUS TCPConnect
 ( PCONNECTION_ENDPOINT Connection,
   PTDI_CONNECTION_INFORMATION ConnInfo,
@@ -404,7 +534,7 @@ NTSTATUS TCPConnect
     USHORT RemotePort;
     PTDI_BUCKET Bucket;
 
-    DbgPrint("TCPConnect: Called\n");
+    TI_DbgPrint(DEBUG_TCP,("TCPConnect: Called\n"));
 
     Bucket = ExAllocatePool( NonPagedPool, sizeof(*Bucket) );
     if( !Bucket ) return STATUS_NO_MEMORY;
@@ -422,15 +552,16 @@ NTSTATUS TCPConnect
         &RemoteAddress,
         &RemotePort);
 
-    DbgPrint("Connecting to address %x:%x\n",
-            RemoteAddress.Address.IPv4Address,
-            RemotePort);
+    TI_DbgPrint(DEBUG_TCP,
+                ("Connecting to address %x:%x\n",
+                 RemoteAddress.Address.IPv4Address,
+                 RemotePort));
 
     if (!NT_SUCCESS(Status)) {
        TI_DbgPrint(DEBUG_TCP, ("Could not AddrBuildAddress in TCPConnect\n"));
        return Status;
     }
-    
+
     AddressToConnect.sin_family = AF_INET;
     AddressToBind = AddressToConnect;
 
@@ -439,7 +570,7 @@ NTSTATUS TCPConnect
                  &AddressToBind,
                  sizeof(AddressToBind) );
 
-    memcpy( &AddressToConnect.sin_addr, 
+    memcpy( &AddressToConnect.sin_addr,
            &RemoteAddress.Address.IPv4Address,
            sizeof(AddressToConnect.sin_addr) );
     AddressToConnect.sin_port = RemotePort;
@@ -447,12 +578,12 @@ NTSTATUS TCPConnect
     Status = TCPTranslateError
        ( OskitTCPConnect( Connection->SocketContext,
                           Connection,
-                          &AddressToConnect, 
+                          &AddressToConnect,
                           sizeof(AddressToConnect) ) );
 
     TcpipRecursiveMutexLeave( &TCPLock );
-    
-    if( Status == OSK_EINPROGRESS ) 
+
+    if( Status == OSK_EINPROGRESS )
        return STATUS_PENDING;
     else
        return Status;
@@ -466,7 +597,7 @@ NTSTATUS TCPDisconnect
   PTCP_COMPLETION_ROUTINE Complete,
   PVOID Context ) {
     NTSTATUS Status;
-    
+
     TI_DbgPrint(DEBUG_TCP,("started\n"));
 
     TcpipRecursiveMutexEnter( &TCPLock, TRUE );
@@ -490,7 +621,7 @@ NTSTATUS TCPDisconnect
        ( OskitTCPShutdown( Connection->SocketContext, Flags ) );
 
     TcpipRecursiveMutexLeave( &TCPLock );
-    
+
     TI_DbgPrint(DEBUG_TCP,("finished %x\n", Status));
 
     return Status;
@@ -499,18 +630,19 @@ NTSTATUS TCPDisconnect
 NTSTATUS TCPClose
 ( PCONNECTION_ENDPOINT Connection ) {
     NTSTATUS Status;
-    
+
     TI_DbgPrint(DEBUG_TCP,("TCPClose started\n"));
 
     TcpipRecursiveMutexEnter( &TCPLock, TRUE );
 
     Status = TCPTranslateError( OskitTCPClose( Connection->SocketContext ) );
 
-    if( Connection->Signalled ) 
-       RemoveEntryList( &Connection->SignalList );
+    /* Make our code remove all pending IRPs */
+    Connection->State |= SEL_FIN;
+    DrainSignals();
 
     TcpipRecursiveMutexLeave( &TCPLock );
-    
+
     TI_DbgPrint(DEBUG_TCP,("TCPClose finished %x\n", Status));
 
     return Status;
@@ -529,7 +661,8 @@ NTSTATUS TCPReceiveData
     NTSTATUS Status;
     PTDI_BUCKET Bucket;
 
-    TI_DbgPrint(DEBUG_TCP,("Called for %d bytes\n", ReceiveLength));
+    TI_DbgPrint(DEBUG_TCP,("Called for %d bytes (on socket %x)\n",
+                           ReceiveLength, Connection->SocketContext));
 
     ASSERT_KM_POINTER(Connection->SocketContext);
 
@@ -546,7 +679,7 @@ NTSTATUS TCPReceiveData
            DataLen,
            &Received,
            ReceiveFlags ) );
-    
+
     TI_DbgPrint(DEBUG_TCP,("OskitTCPReceive: %x, %d\n", Status, Received));
 
     /* Keep this request around ... there was no data yet */
@@ -558,7 +691,7 @@ NTSTATUS TCPReceiveData
            TcpipRecursiveMutexLeave( &TCPLock );
            return STATUS_NO_MEMORY;
        }
-       
+
        Bucket->Request.RequestNotifyObject = Complete;
        Bucket->Request.RequestContext = Context;
        *BytesReceived = 0;
@@ -581,10 +714,17 @@ NTSTATUS TCPReceiveData
 NTSTATUS TCPSendData
 ( PCONNECTION_ENDPOINT Connection,
   PCHAR BufferData,
-  ULONG PacketSize,
-  PULONG DataUsed,
-  ULONG Flags) {
+  ULONG SendLength,
+  PULONG BytesSent,
+  ULONG Flags,
+  PTCP_COMPLETION_ROUTINE Complete,
+  PVOID Context ) {
+    UINT Sent = 0;
     NTSTATUS Status;
+    PTDI_BUCKET Bucket;
+
+    TI_DbgPrint(DEBUG_TCP,("Called for %d bytes (on socket %x)\n",
+                           SendLength, Connection->SocketContext));
 
     ASSERT_KM_POINTER(Connection->SocketContext);
 
@@ -594,29 +734,53 @@ NTSTATUS TCPSendData
     TI_DbgPrint(DEBUG_TCP,("Connection->SocketContext = %x\n",
                           Connection->SocketContext));
 
-    Status = OskitTCPSend( Connection->SocketContext, 
-                          (OSK_PCHAR)BufferData, PacketSize,
-                          (PUINT)DataUsed, 0 );
+    Status = TCPTranslateError
+       ( OskitTCPSend( Connection->SocketContext,
+                       (OSK_PCHAR)BufferData, SendLength,
+                       &Sent, 0 ) );
+
+    TI_DbgPrint(DEBUG_TCP,("OskitTCPSend: %x, %d\n", Status, Sent));
+
+    /* Keep this request around ... there was no data yet */
+    if( Status == STATUS_PENDING ) {
+       /* Freed in TCPSocketState */
+       Bucket = ExAllocatePool( NonPagedPool, sizeof(*Bucket) );
+       if( !Bucket ) {
+           TI_DbgPrint(DEBUG_TCP,("Failed to allocate bucket\n"));
+           TcpipRecursiveMutexLeave( &TCPLock );
+           return STATUS_NO_MEMORY;
+       }
+
+       Bucket->Request.RequestNotifyObject = Complete;
+       Bucket->Request.RequestContext = Context;
+       *BytesSent = 0;
+
+       InsertHeadList( &Connection->SendRequest, &Bucket->Entry );
+       TI_DbgPrint(DEBUG_TCP,("Queued write irp\n"));
+    } else {
+       TI_DbgPrint(DEBUG_TCP,("Got status %x, bytes %d\n", Status, Sent));
+       *BytesSent = Sent;
+    }
 
     TcpipRecursiveMutexLeave( &TCPLock );
 
+    TI_DbgPrint(DEBUG_TCP,("Status %x\n", Status));
+
     return Status;
 }
 
-VOID TCPTimeout(VOID) { 
-    static int Times = 0;
-    TcpipRecursiveMutexEnter( &TCPLock, TRUE );
-    if( (Times++ % 5) == 0 ) {
-       TimerOskitTCP();
-    }
-    DrainSignals();
-    TcpipRecursiveMutexLeave( &TCPLock );
+VOID TCPTimeout(VOID) {
+    /* Now handled by TimerThread */
 }
 
 UINT TCPAllocatePort( UINT HintPort ) {
     if( HintPort ) {
-       if( AllocatePort( &TCPPorts, HintPort ) ) return HintPort; 
-       else return (UINT)-1;
+       if( AllocatePort( &TCPPorts, HintPort ) ) return HintPort;
+       else {
+            TI_DbgPrint
+                (MID_TRACE,("We got a hint port but couldn't allocate it\n"));
+            return (UINT)-1;
+        }
     } else return AllocatePortFromRange( &TCPPorts, 1024, 5000 );
 }
 
@@ -624,4 +788,59 @@ VOID TCPFreePort( UINT Port ) {
     DeallocatePort( &TCPPorts, Port );
 }
 
+NTSTATUS TCPGetPeerAddress
+( PCONNECTION_ENDPOINT Connection,
+  PTRANSPORT_ADDRESS Address ) {
+    OSK_UINT LocalAddress, RemoteAddress;
+    OSK_UI16 LocalPort, RemotePort;
+    PTA_IP_ADDRESS AddressIP = (PTA_IP_ADDRESS)Address;
+
+    TcpipRecursiveMutexEnter( &TCPLock, TRUE );
+
+    OskitTCPGetAddress
+        ( Connection->SocketContext,
+          &LocalAddress, &LocalPort,
+          &RemoteAddress, &RemotePort );
+
+    AddressIP->TAAddressCount = 1;
+    AddressIP->Address[0].AddressLength = TDI_ADDRESS_LENGTH_IP;
+    AddressIP->Address[0].AddressType = TDI_ADDRESS_TYPE_IP;
+    AddressIP->Address[0].Address[0].sin_port = RemotePort;
+    AddressIP->Address[0].Address[0].in_addr = RemoteAddress;
+
+    TcpipRecursiveMutexLeave( &TCPLock );
+
+    return STATUS_SUCCESS;
+}
+
+VOID TCPRemoveIRP( PCONNECTION_ENDPOINT Endpoint, PIRP Irp ) {
+    PLIST_ENTRY Entry;
+    PLIST_ENTRY ListHead[4];
+    KIRQL OldIrql;
+    PTDI_BUCKET Bucket;
+    UINT i = 0;
+
+    ListHead[0] = &Endpoint->SendRequest;
+    ListHead[1] = &Endpoint->ReceiveRequest;
+    ListHead[2] = &Endpoint->ConnectRequest;
+    ListHead[3] = &Endpoint->ListenRequest;
+
+    TcpipAcquireSpinLock( &Endpoint->Lock, &OldIrql );
+
+    for( i = 0; i < sizeof( ListHead ) / sizeof( ListHead[0] ); i++ ) {
+       for( Entry = ListHead[i]->Flink;
+            Entry != ListHead[i];
+            Entry = Entry->Flink ) {
+           Bucket = CONTAINING_RECORD( Entry, TDI_BUCKET, Entry );
+           
+           if( Bucket->Request.RequestContext == Irp ) {
+               RemoveEntryList( &Bucket->Entry );
+               break;
+           }
+       }
+    }
+
+    TcpipReleaseSpinLock( &Endpoint->Lock, OldIrql );
+}
+
 /* EOF */