Bug 311

Summary: Tcp socket close returns -1 but does not set errno.
Product: ns-3 Reporter: Mathieu Lacage <mathieu.lacage>
Component: internetAssignee: Rajib Bhattacharjea <raj.b>
Status: RESOLVED FIXED    
Severity: normal CC: ns-bugs
Priority: P3    
Version: pre-release   
Hardware: All   
OS: All   

Description Mathieu Lacage 2008-09-04 16:08:11 EDT
tcp-socket-impl.cc: line 294:

  if (m_state == CLOSED)
    {
      return -1;
    }

It is illegal to return -1 without setting errno. But I have to confess that I don't really understand the purpose of this check: is it expected to guard against a double close ? If so, I suspect that the return errno here is EBADF.
Comment 1 Mathieu Lacage 2008-09-04 16:09:55 EDT
I should have pointed out that, if the purpose is to guard against a double-close, then, the socket should not be initialized to CLOSED state in the TcpSocketImpl constructor because this would trigger an EBADF upon closing a socket which was just opened.
Comment 2 Rajib Bhattacharjea 2008-09-17 15:34:51 EDT
It seems the purpose of this check was to prevent a double close, BUT it apparently is superfluous in any case, because the state machine has the following entry in it:
aT[CLOSED][APP_CLOSE]   = SA (CLOSED,   NO_ACT);

Since we call ProcessEvent(APP_CLOSE) in this code, in the case of already being in the CLOSED state, nothing would happen in terms of state change or action in response to closing the socket.  As such, deleting the check would cause a double close to silently succeed, and return 0.  Really, this is what should have been done, because the processing of the m_state variable shouldn't explicitly be done in this code since it is handled by the ProcessEvent/Action methods.

diff -r 6105fe16df43 src/internet-stack/tcp-socket-impl.cc
--- a/src/internet-stack/tcp-socket-impl.cc	Tue Sep 16 11:55:52 2008 -0700
+++ b/src/internet-stack/tcp-socket-impl.cc	Wed Sep 17 15:33:07 2008 -0400
@@ -296,10 +296,6 @@
 TcpSocketImpl::Close (void)
 {
   NS_LOG_FUNCTION_NOARGS ();
-  if (m_state == CLOSED) 
-    {
-      return -1;
-    }
   if (m_pendingData && m_pendingData->Size() != 0)
     { // App close with pending data must wait until all data transmitted
       m_closeOnEmpty = true;
Comment 3 Rajib Bhattacharjea 2008-10-27 15:01:55 EDT
changeset 3818	080614a8e247