[lwIP]
authorClaudiu Mihail <claudiu.bogdan.mihail@gmail.com>
Mon, 20 Jun 2011 21:37:32 +0000 (21:37 +0000)
committerClaudiu Mihail <claudiu.bogdan.mihail@gmail.com>
Mon, 20 Jun 2011 21:37:32 +0000 (21:37 +0000)
- Abort the connection if the socket is not in LISTEN state, instead of closing it
[TCPIP]
- move redundant socket closure where so we don't try to free uninitialized variable
- in case our socket got closed or for some reason doesn't exist return a default success

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

drivers/network/afd/afd/connect.c
lib/drivers/ip/transport/tcp/event.c
lib/drivers/ip/transport/tcp/tcp.c
lib/drivers/lwip/src/rostcp.c

index 4d0ed6d..9272097 100644 (file)
@@ -281,7 +281,7 @@ static NTSTATUS NTAPI StreamSocketConnectComplete
 
     AFD_DbgPrint(MID_TRACE,("Called: FCB %x, FO %x\n",
                            Context, FCB->FileObject));
-    DbgPrint("[StreamSocketConnectComplete] Called: FCB %x, FO %x\n",
+    DbgPrint("[AFD, StreamSocketConnectComplete] Called: FCB 0x%x, FO 0x%x\n",
                            Context, FCB->FileObject);
 
     /* I was wrong about this before as we can have pending writes to a not
@@ -333,7 +333,7 @@ static NTSTATUS NTAPI StreamSocketConnectComplete
            NextIrp = CONTAINING_RECORD(NextIrpEntry, IRP, Tail.Overlay.ListEntry);
            
         AFD_DbgPrint(MID_TRACE,("Completing connect %x\n", NextIrp));
-        DbgPrint("[StreamSocketConnectComplete] Completing connect %x\n", NextIrp);
+        DbgPrint("[AFD, StreamSocketConnectComplete] Completing connect 0x%x\n", NextIrp);
            
         NextIrp->IoStatus.Status = Status;
            NextIrp->IoStatus.Information = NT_SUCCESS(Status) ? ((ULONG_PTR)FCB->Connection.Handle) : 0;
@@ -410,7 +410,7 @@ AfdStreamSocketConnect(PDEVICE_OBJECT DeviceObject, PIRP Irp,
                                       0 );
 
     AFD_DbgPrint(MID_TRACE,("Connect request:\n"));
-    DbgPrint("[AfdStreamSocketConnect] Connect request:\n");
+    DbgPrint("[AFD, AfdStreamSocketConnect] Connect request:\n");
 #if 0
     OskitDumpBuffer
        ( (PCHAR)ConnectReq,
@@ -420,8 +420,7 @@ AfdStreamSocketConnect(PDEVICE_OBJECT DeviceObject, PIRP Irp,
     if( FCB->Flags & AFD_ENDPOINT_CONNECTIONLESS )
     {
         if( FCB->RemoteAddress ) ExFreePool( FCB->RemoteAddress );
-        FCB->RemoteAddress =
-               TaCopyTransportAddress( &ConnectReq->RemoteAddress );
+            FCB->RemoteAddress = TaCopyTransportAddress( &ConnectReq->RemoteAddress );
 
         if( !FCB->RemoteAddress )
                Status = STATUS_NO_MEMORY;
@@ -440,10 +439,10 @@ AfdStreamSocketConnect(PDEVICE_OBJECT DeviceObject, PIRP Irp,
         case SOCKET_STATE_CONNECTING:
                return LeaveIrpUntilLater( FCB, Irp, FUNCTION_CONNECT );
 
-    case SOCKET_STATE_CREATED:
-       if( FCB->LocalAddress ) ExFreePool( FCB->LocalAddress );
-       FCB->LocalAddress =
-           TaBuildNullTransportAddress( ConnectReq->RemoteAddress.Address[0].AddressType );
+        case SOCKET_STATE_CREATED:
+               if( FCB->LocalAddress ) ExFreePool( FCB->LocalAddress );
+                   FCB->LocalAddress = TaBuildNullTransportAddress(
+                    ConnectReq->RemoteAddress.Address[0].AddressType);
 
                if( FCB->LocalAddress )
             {
@@ -460,9 +459,10 @@ AfdStreamSocketConnect(PDEVICE_OBJECT DeviceObject, PIRP Irp,
         /* Drop through to SOCKET_STATE_BOUND */
 
         case SOCKET_STATE_BOUND:
-               if( FCB->RemoteAddress ) ExFreePool( FCB->RemoteAddress );
-                   FCB->RemoteAddress =
-                       TaCopyTransportAddress( &ConnectReq->RemoteAddress );
+               if( FCB->RemoteAddress )
+                ExFreePool( FCB->RemoteAddress );
+               
+            FCB->RemoteAddress = TaCopyTransportAddress( &ConnectReq->RemoteAddress );
 
                if( !FCB->RemoteAddress )
             {
@@ -475,13 +475,10 @@ AfdStreamSocketConnect(PDEVICE_OBJECT DeviceObject, PIRP Irp,
                if( !NT_SUCCESS(Status) )
                    break;
 
-               Status = TdiBuildConnectionInfo
-                   ( &FCB->ConnectInfo,
-                     &ConnectReq->RemoteAddress );
+               Status = TdiBuildConnectionInfo(&FCB->ConnectInfo, &ConnectReq->RemoteAddress);
 
             if( NT_SUCCESS(Status) )
-                Status = TdiBuildConnectionInfo(&TargetAddress,
-                                               &ConnectReq->RemoteAddress);
+                Status = TdiBuildConnectionInfo(&TargetAddress, &ConnectReq->RemoteAddress);
             else break;
 
                if( NT_SUCCESS(Status) )
@@ -502,7 +499,7 @@ AfdStreamSocketConnect(PDEVICE_OBJECT DeviceObject, PIRP Irp,
                     ExFreePool(TargetAddress);
 
                    AFD_DbgPrint(MID_TRACE,("Queueing IRP %x\n", Irp));
-                DbgPrint("[AfdStreamSocketConnect] Queueing IRP %x\n", Irp);
+                DbgPrint("[AFD, AfdStreamSocketConnect] Queueing IRP %x\n", Irp);
 
                    if( Status == STATUS_PENDING )
                 {
@@ -515,7 +512,7 @@ AfdStreamSocketConnect(PDEVICE_OBJECT DeviceObject, PIRP Irp,
         default:
                AFD_DbgPrint(MID_TRACE,("Inappropriate socket state %d for connect\n",
                                        FCB->State));
-            DbgPrint("[AfdStreamSocketConnect] Inappropriate socket state %d for connect\n",
+            DbgPrint("[AFD, AfdStreamSocketConnect] Inappropriate socket state %d for connect\n",
                         FCB->State);
                break;
     }
index ebe045c..96ba26b 100644 (file)
@@ -202,6 +202,10 @@ TCPAcceptEventHandler(void *arg, struct tcp_pcb *newpcb)
 
             DbgPrint("[IP, TCPAcceptEventHandler] Trying to unlock Bucket->AssociatedEndpoint\n");
             UnlockObject(Bucket->AssociatedEndpoint, OldIrql);
+
+            /*  free socket context created in FileOpenConnection, as we're using a new
+                one; we free it asynchornously because otherwise we create a dedlock */
+            ChewCreate(SocketContextCloseWorker, OldSocketContext);
         }
         
         DereferenceObject(Bucket->AssociatedEndpoint);
@@ -212,10 +216,6 @@ TCPAcceptEventHandler(void *arg, struct tcp_pcb *newpcb)
     }
     
     DereferenceObject(Connection);
-
-    /*  free socket context created in FileOpenConnection, as we're using a new
-        one; we free it asynchornously because otherwise we create a dedlock */
-    ChewCreate(SocketContextCloseWorker, OldSocketContext);
 }
 
 VOID
index 574858b..de69e18 100644 (file)
@@ -347,14 +347,23 @@ NTSTATUS TCPDisconnect
 
     LockObject(Connection, &OldIrql);
 
-    if (Flags & TDI_DISCONNECT_RELEASE)
+    if (Connection->SocketContext)
     {
-        Status = LibTCPShutdown(Connection->SocketContext, 0, 1);
-    }
+        if (Flags & TDI_DISCONNECT_RELEASE)
+        {
+            Status = TCPTranslateError(LibTCPShutdown(Connection->SocketContext, 0, 1));
+        }
 
-    if ((Flags & TDI_DISCONNECT_ABORT) || !Flags)
+        if ((Flags & TDI_DISCONNECT_ABORT) || !Flags)
+        {
+            Status = TCPTranslateError(LibTCPShutdown(Connection->SocketContext, 1, 1));
+        }
+    }
+    else
     {
-        Status = LibTCPShutdown(Connection->SocketContext, 1, 1);
+        /* We already got closed by the other side so just return success */
+        DbgPrint("[IP, TCPDisconnect] Socket was alraedy clsoed on the other side\n");
+        Status = STATUS_SUCCESS;
     }
     
     DbgPrint("LibTCPShutdown: %x\n", Status);
index 91a09d5..323a912 100755 (executable)
@@ -612,7 +612,17 @@ LibTCPCloseCallback(void *arg)
 {
     struct close_callback_msg *msg = arg;
     
-    msg->Error = tcp_close(msg->Pcb);
+    if (msg->Pcb->state == LISTEN)
+    {
+        DbgPrint("[lwIP, LibTCPCloseCallback] Closing a listener\n");
+        msg->Error = tcp_close(msg->Pcb);
+    }
+    else
+    {
+        DbgPrint("[lwIP, LibTCPCloseCallback] Aborting a connection\n");
+        tcp_abort(msg->Pcb);
+        msg->Error = ERR_OK;
+    }
     
     KeSetEvent(&msg->Event, IO_NO_INCREMENT, FALSE);
 }