[TCPIP]
authorClaudiu Mihail <claudiu.bogdan.mihail@gmail.com>
Mon, 1 Aug 2011 20:10:55 +0000 (20:10 +0000)
committerClaudiu Mihail <claudiu.bogdan.mihail@gmail.com>
Mon, 1 Aug 2011 20:10:55 +0000 (20:10 +0000)
- merge r53017
- do some code cleanup
[lwIP]
- do some code cleanup

svn path=/branches/GSoC_2011/TcpIpDriver/; revision=53020

drivers/network/tcpip/include/tcp.h
drivers/network/tcpip/tcpip/dispatch.c
lib/drivers/ip/transport/tcp/event.c
lib/drivers/ip/transport/tcp/tcp.c
lib/drivers/lwip/src/core/tcp_in.c
lib/drivers/lwip/src/include/rosip.h
lib/drivers/lwip/src/rosip.c
lib/drivers/lwip/src/rosmem.c
lib/drivers/lwip/src/rostcp.c

index 8724d16..0b9ad77 100644 (file)
@@ -166,11 +166,11 @@ NTSTATUS TCPSendData(
 
 NTSTATUS TCPClose( PCONNECTION_ENDPOINT Connection );
 
-NTSTATUS TCPTranslateError( INT8 err );
+NTSTATUS TCPTranslateError( const INT8 err );
 
-UINT TCPAllocatePort( UINT HintPort );
+UINT TCPAllocatePort( const UINT HintPort );
 
-VOID TCPFreePort( UINT Port );
+VOID TCPFreePort( const UINT Port );
 
 NTSTATUS TCPGetSockAddress
 ( PCONNECTION_ENDPOINT Connection,
@@ -192,4 +192,4 @@ VOID
 TCPUpdateInterfaceIPInformation(PIP_INTERFACE IF);
 
 VOID
-FlushAllQueues(PCONNECTION_ENDPOINT Connection, NTSTATUS Status);
+FlushAllQueues(PCONNECTION_ENDPOINT Connection, const NTSTATUS Status);
index 66922d6..d42c4fe 100644 (file)
@@ -122,6 +122,7 @@ VOID NTAPI DispCancelRequest(
     PTRANSPORT_CONTEXT TranContext;
     PFILE_OBJECT FileObject;
     UCHAR MinorFunction;
+    PCONNECTION_ENDPOINT Connection;
     BOOLEAN DequeuedIrp = TRUE;
 
     IoReleaseCancelSpinLock(Irp->CancelIrql);
@@ -173,7 +174,16 @@ VOID NTAPI DispCancelRequest(
         break;
             
     case TDI_DISCONNECT:
+        Connection = (PCONNECTION_ENDPOINT)TranContext->Handle.ConnectionContext;
+
         DequeuedIrp = TCPRemoveIRP(TranContext->Handle.ConnectionContext, Irp);
+        if (DequeuedIrp)
+        {
+            if (KeCancelTimer(&Connection->DisconnectTimer))
+            {
+                DereferenceObject(Connection);
+            }
+        }
         break;
 
     default:
index 1729b13..68deb31 100644 (file)
@@ -46,8 +46,9 @@ BucketCompletionWorker(PVOID Context)
     ExFreePoolWithTag(Bucket, TDI_BUCKET_TAG);
 }
 
+static
 VOID
-CompleteBucket(PCONNECTION_ENDPOINT Connection, PTDI_BUCKET Bucket, BOOLEAN Synchronous)
+CompleteBucket(PCONNECTION_ENDPOINT Connection, PTDI_BUCKET Bucket, const BOOLEAN Synchronous)
 {
     ReferenceObject(Connection);
     Bucket->AssociatedEndpoint = Connection;
@@ -62,7 +63,7 @@ CompleteBucket(PCONNECTION_ENDPOINT Connection, PTDI_BUCKET Bucket, BOOLEAN Sync
 }
 
 VOID
-FlushAllQueues(PCONNECTION_ENDPOINT Connection, NTSTATUS Status)
+FlushAllQueues(PCONNECTION_ENDPOINT Connection, const NTSTATUS Status)
 {
     PTDI_BUCKET Bucket;
     PLIST_ENTRY Entry;
@@ -140,7 +141,7 @@ FlushAllQueues(PCONNECTION_ENDPOINT Connection, NTSTATUS Status)
 }
 
 VOID
-TCPFinEventHandler(void *arg, err_t err)
+TCPFinEventHandler(void *arg, const err_t err)
 {
     PCONNECTION_ENDPOINT Connection = (PCONNECTION_ENDPOINT)arg;
     const NTSTATUS status = TCPTranslateError(err);
@@ -148,7 +149,7 @@ TCPFinEventHandler(void *arg, err_t err)
     DbgPrint("[IP, TCPFinEventHandler] Called for Connection( 0x%x )-> SocketContext = pcb (0x%x)\n", Connection, Connection->SocketContext);
 
     /* Only clear the pointer if the shutdown was caused by an error */
-    if ((err != ERR_OK))
+    if (err != ERR_OK)
     {
         /* We're already closed by the error so we don't want to call lwip_close */
         DbgPrint("[IP, TCPFinEventHandler] MAKING Connection( 0x%x )-> SocketContext = pcb (0x%x) NULL\n", Connection, Connection->SocketContext);
@@ -162,7 +163,7 @@ TCPFinEventHandler(void *arg, err_t err)
 }
     
 VOID
-TCPAcceptEventHandler(void *arg, struct tcp_pcb *newpcb)
+TCPAcceptEventHandler(void *arg, PTCP_PCB newpcb)
 {
     PCONNECTION_ENDPOINT Connection = (PCONNECTION_ENDPOINT)arg;
     PTDI_BUCKET Bucket;
@@ -200,13 +201,13 @@ TCPAcceptEventHandler(void *arg, struct tcp_pcb *newpcb)
         {
             DbgPrint("[IP, TCPAcceptEventHandler] newpcb->state = %s, listen_pcb->state = %s, newpcb = 0x%x\n",
                 tcp_state_str[newpcb->state],
-                tcp_state_str[((struct tcp_pcb*)Connection->SocketContext)->state],
+                tcp_state_str[((PTCP_PCB)Connection->SocketContext)->state],
                 newpcb);
 
             LockObject(Bucket->AssociatedEndpoint, &OldIrql);
 
             /* sanity assert...this should never be in anything else but a CLOSED state */
-            ASSERT(((struct tcp_pcb*)Bucket->AssociatedEndpoint->SocketContext)->state == CLOSED);
+            ASSERT( ((PTCP_PCB)Bucket->AssociatedEndpoint->SocketContext)->state == CLOSED );
             
             /*  free socket context created in FileOpenConnection, as we're using a new one */
             LibTCPClose(Bucket->AssociatedEndpoint, TRUE);
@@ -233,7 +234,7 @@ TCPAcceptEventHandler(void *arg, struct tcp_pcb *newpcb)
 VOID
 TCPSendEventHandler(void *arg, u16_t space)
 {
-    PCONNECTION_ENDPOINT Connection = arg;
+    PCONNECTION_ENDPOINT Connection = (PCONNECTION_ENDPOINT)arg;
     PTDI_BUCKET Bucket;
     PLIST_ENTRY Entry;
     PIRP Irp;
index 5b25b5f..e456836 100644 (file)
@@ -105,8 +105,7 @@ NTSTATUS TCPSocket( PCONNECTION_ENDPOINT Connection,
     return Status;
 }
 
-NTSTATUS TCPClose
-( PCONNECTION_ENDPOINT Connection )
+NTSTATUS TCPClose( PCONNECTION_ENDPOINT Connection )
 {
     KIRQL OldIrql;
     PVOID Socket;
@@ -116,7 +115,6 @@ NTSTATUS TCPClose
     DbgPrint("[IP, TCPClose] Called for Connection( 0x%x )->SocketConext( 0x%x )\n", Connection, Connection->SocketContext);
 
     Socket = Connection->SocketContext;
-    //Connection->SocketContext = NULL;
 
     /* We should not be associated to an address file at this point */
     ASSERT(!Connection->AddressFile);
@@ -131,7 +129,7 @@ NTSTATUS TCPClose
         LibTCPClose(Connection, FALSE);
     }
 
-    DbgPrint("[IP, TCPClose] Leaving. Connection->RefCount = %d\n", Connection->RefCount);
+    DbgPrint("[IP, TCPClose] Leaving\n");
 
     UnlockObject(Connection, OldIrql);
 
@@ -170,7 +168,7 @@ NTSTATUS TCPStartup(VOID)
     NTSTATUS Status;
 
     Status = PortsStartup( &TCPPorts, 1, 0xfffe );
-    if( !NT_SUCCESS(Status) )
+    if (!NT_SUCCESS(Status))
     {
         return Status;
     }
@@ -209,7 +207,7 @@ NTSTATUS TCPShutdown(VOID)
     return STATUS_SUCCESS;
 }
 
-NTSTATUS TCPTranslateError( err_t err )
+NTSTATUS TCPTranslateError(const err_t err)
 {
     NTSTATUS Status;
 
@@ -328,7 +326,7 @@ NTSTATUS TCPConnect
             connaddr.addr = RemoteAddress.Address.IPv4Address;
 
             Bucket = ExAllocatePoolWithTag( NonPagedPool, sizeof(*Bucket), TDI_BUCKET_TAG );
-            if( !Bucket )
+            if (!Bucket)
             {
                 UnlockObject(Connection, OldIrql);
                 return STATUS_NO_MEMORY;
@@ -427,7 +425,7 @@ NTSTATUS TCPReceiveData
         LockObject(Connection, &OldIrql);
     
         /* Freed in TCPSocketState */
-        Bucket = ExAllocatePoolWithTag( NonPagedPool, sizeof(*Bucket), TDI_BUCKET_TAG );
+        Bucket = ExAllocatePoolWithTag(NonPagedPool, sizeof(*Bucket), TDI_BUCKET_TAG);
         if (!Bucket)
         {
             TI_DbgPrint(DEBUG_TCP,("[IP, TCPReceiveData] Failed to allocate bucket\n"));
@@ -492,11 +490,11 @@ NTSTATUS TCPSendData
     TI_DbgPrint(DEBUG_TCP,("[IP, TCPSendData] Send: %x, %d\n", Status, SendLength));
 
     /* Keep this request around ... there was no data yet */
-    if( Status == STATUS_PENDING )
+    if (Status == STATUS_PENDING)
     {
         /* Freed in TCPSocketState */
         Bucket = ExAllocatePoolWithTag( NonPagedPool, sizeof(*Bucket), TDI_BUCKET_TAG );
-        if( !Bucket )
+        if (!Bucket)
         {
             UnlockObject(Connection, OldIrql);
             TI_DbgPrint(DEBUG_TCP,("[IP, TCPSendData] Failed to allocate bucket\n"));
@@ -526,16 +524,15 @@ NTSTATUS TCPSendData
     return Status;
 }
 
-UINT TCPAllocatePort( UINT HintPort )
+UINT TCPAllocatePort(const UINT HintPort)
 {
-    if( HintPort )
+    if (HintPort)
     {
-        if( AllocatePort( &TCPPorts, HintPort ) )
+        if (AllocatePort(&TCPPorts, HintPort))
             return HintPort;
         else
         {
-            TI_DbgPrint
-                (MID_TRACE,("We got a hint port but couldn't allocate it\n"));
+            TI_DbgPrint(MID_TRACE,("We got a hint port but couldn't allocate it\n"));
             return (UINT)-1;
         }
     }
@@ -543,9 +540,9 @@ UINT TCPAllocatePort( UINT HintPort )
         return AllocatePortFromRange( &TCPPorts, 1024, 5000 );
 }
 
-VOID TCPFreePort( UINT Port )
+VOID TCPFreePort(const UINT Port)
 {
-    DeallocatePort( &TCPPorts, Port );
+    DeallocatePort(&TCPPorts, Port);
 }
 
 NTSTATUS TCPGetSockAddress
index 9095264..303b46f 100644 (file)
@@ -79,6 +79,20 @@ static void tcp_parseopt(struct tcp_pcb *pcb);
 static err_t tcp_listen_input(struct tcp_pcb_listen *pcb);
 static err_t tcp_timewait_input(struct tcp_pcb *pcb);
 
+static const char * const tcp_state_str[] = {
+  "CLOSED",      
+  "LISTEN",      
+  "SYN_SENT",    
+  "SYN_RCVD",    
+  "ESTABLISHED", 
+  "FIN_WAIT_1",  
+  "FIN_WAIT_2",  
+  "CLOSE_WAIT",  
+  "CLOSING",     
+  "LAST_ACK",    
+  "TIME_WAIT"   
+};
+
 /**
  * The initial input processing of TCP. It verifies the TCP header, demultiplexes
  * the segment between the PCBs and passes it on to tcp_process(), which implements
@@ -325,12 +339,14 @@ tcp_input(struct pbuf *p, struct netif *inp)
            end. We then call the error callback to inform the
            application that the connection is dead before we
            deallocate the PCB. */
+          DbgPrint("tcp_input: removing pcb = 0x%x in state %s with TF_RESET\n", pcb, tcp_state_str[pcb->state]);
         TCP_EVENT_ERR(pcb->errf, pcb->callback_arg, ERR_RST);
         tcp_pcb_remove(&tcp_active_pcbs, pcb);
         memp_free(MEMP_TCP_PCB, pcb);
       } else if (recv_flags & TF_CLOSED) {
         /* The connection has been closed and we will deallocate the
            PCB. */
+          DbgPrint("tcp_input: removing pcb = 0x%x with TF_CLOSED\n", pcb);
         tcp_pcb_remove(&tcp_active_pcbs, pcb);
         memp_free(MEMP_TCP_PCB, pcb);
       } else {
@@ -422,6 +438,7 @@ aborted:
     pbuf_free(p);
   }
 
+  DbgPrint("tcp_input: done\n");
   LWIP_ASSERT("tcp_input: tcp_pcbs_sane()", tcp_pcbs_sane());
   PERF_STOP("tcp_input");
 }
index c6335ad..d05f4cf 100755 (executable)
@@ -22,7 +22,7 @@ NTSTATUS    LibTCPGetDataFromConnectionQueue(PCONNECTION_ENDPOINT Connection, PU
 
 /* External TCP event handlers */
 extern void TCPConnectEventHandler(void *arg, const err_t err);
-extern void TCPAcceptEventHandler(void *arg, struct tcp_pcb *newpcb);
+extern void TCPAcceptEventHandler(void *arg, PTCP_PCB newpcb);
 extern void TCPSendEventHandler(void *arg, const u16_t space);
 extern void TCPFinEventHandler(void *arg, const err_t err);
 extern u32_t TCPRecvEventHandler(void *arg, struct pbuf *p);
index 9d62201..07100d2 100755 (executable)
@@ -5,6 +5,8 @@
 
 #include <debug.h>
 
+typedef struct netif* PNETIF;
+
 void
 LibIPInsertPacket(void *ifarg,
                   const void *const data,
@@ -26,9 +28,7 @@ LibIPInsertPacket(void *ifarg,
             RtlCopyMemory(p1->payload, ((PUCHAR)data) + i, p1->len);
         }
 
-        DbgPrint("LibIPInsertPacket: called 0x%x\n", *((struct netif *)ifarg)->input);
-
-        ((struct netif *)ifarg)->input(p, ifarg);
+        ((PNETIF)ifarg)->input(p, (PNETIF)ifarg);
     }
 }
 
index 53673c7..3ba8647 100755 (executable)
@@ -1,10 +1,11 @@
-
 #include "lwip/opt.h"
 
 #include "lwip/def.h"
 #include "lwip/mem.h"
 
-#define LWIP_TAG 'PIwl'
+#ifndef LWIP_TAG
+    #define LWIP_TAG 'PIwl'
+#endif
 
 void *
 malloc(mem_size_t size)
@@ -27,6 +28,7 @@ calloc(mem_size_t count, mem_size_t size)
 void
 free(void *mem)
 {
+    //DbgPrint("ROSMEM: free 0x%x\n", mem);
     ExFreePoolWithTag(mem, LWIP_TAG);
 }
 
index 987f38e..3e504c1 100755 (executable)
@@ -45,8 +45,7 @@ LibTCPEmptyQueue(PCONNECTION_ENDPOINT Connection)
 
         Entry = RemoveHeadList(&Connection->PacketQueue);
         qp = CONTAINING_RECORD(Entry, QUEUE_ENTRY, ListEntry);
-        
-        // reenable this later
+
         pbuf_free(qp->p);
 
         ExFreePoolWithTag(qp, LWIP_TAG);
@@ -772,7 +771,7 @@ CloseCallbacks(struct tcp_pcb *pcb)
 {
     tcp_arg(pcb, NULL);
     /*  
-        if this pcb is not in LISTEN state than it has
+        if this pcb is not in LISTEN state then it has
         valid recv, send and err callbacks to cancel
     */
     if (pcb->state != LISTEN)
@@ -796,8 +795,6 @@ LibTCPCloseCallback(void *arg)
     if (!msg->Connection->SocketContext)
     {
         DbgPrint("[lwIP, LibTCPCloseCallback] NULL pcb...bail, bail!!!\n");
-        
-        //ASSERT(FALSE);
 
         msg->Error = ERR_OK;
         return;