[IOT-3075] Aborting TCP connect operation 89/25489/3
authorkoushik.girijala <g.koushik@samsung.com>
Fri, 25 May 2018 10:12:34 +0000 (15:42 +0530)
committerMushfiqul Islam <mmiantu@gmail.com>
Tue, 19 Jun 2018 10:52:00 +0000 (10:52 +0000)
Aborting TCP connect operation during IP disconnect and Adapter stop

Change-Id: If6a51416f90be533ebdd70096781c3d094512752
Signed-off-by: koushik.girijala <g.koushik@samsung.com>
resource/csdk/connectivity/inc/catcpadapter.h
resource/csdk/connectivity/inc/catcpinterface.h
resource/csdk/connectivity/src/ip_adapter/caipadapter.c
resource/csdk/connectivity/src/tcp_adapter/catcpadapter.c
resource/csdk/connectivity/src/tcp_adapter/catcpserver.c

index 0326253..f34eebe 100644 (file)
@@ -206,12 +206,14 @@ CAResult_t CAStopTCP(void);
  */
 void CATerminateTCP(void);
 
+#ifdef TCP_ADAPTER
 /**
  * Set connection status changes callback to process KeepAlive.
  * connection informations are delivered these callbacks.
  * @param[in]   ConnHandler     Connection status changes callback.
  */
 void CATCPSetKeepAliveCallbacks(CAKeepAliveConnectionCallback ConnHandler);
+#endif
 
 /**
  * Get Capability and Settings message(CSM) exchange state.
index d996c47..6d82125 100644 (file)
@@ -81,6 +81,7 @@ typedef void (*CATCPConnectionHandleCallback)(const CAEndpoint_t *endpoint, bool
  */
 void CATCPSetErrorHandler(CATCPErrorHandleCallback errorHandleCallback);
 
+#ifdef TCP_ADAPTER
 /**
  * set keepalive callback to notify connection information in TCP adapter.
  *
@@ -88,6 +89,7 @@ void CATCPSetErrorHandler(CATCPErrorHandleCallback errorHandleCallback);
  * in the TCP adapter.
  */
 void CATCPSetKeepAliveCallback(CAKeepAliveConnectionCallback keepaliveHandler);
+#endif
 
 /**
  * Start TCP server.
@@ -202,6 +204,11 @@ CASocketFd_t CAGetSocketFDFromEndpoint(const CAEndpoint_t *endpoint);
  */
 CAResult_t CASearchAndDeleteTCPSession(const CAEndpoint_t *endpoint);
 
+/**
+ * Abort TCP sessions which are in progress of connecting with servers.
+ */
+void CATCPCloseInProgressConnections();
+
 /**
  * Get total payload length from CoAP over TCP header.
  *
index 6019fd6..9bad8d8 100644 (file)
@@ -31,6 +31,9 @@
 #ifdef __WITH_DTLS__
 #include "ca_adapter_net_ssl.h"
 #endif
+#ifdef WITH_TCP
+#include "catcpinterface.h"
+#endif
 #include "octhread.h"
 #include "uarraylist.h"
 #include "caremotehandler.h"
@@ -177,6 +180,9 @@ void CAIPAdapterHandler(CATransportAdapter_t adapter, CANetworkStatus_t status)
     {
         OIC_LOG(DEBUG, TAG, "Network status for IP is down");
 #ifdef __WITH_DTLS__
+#ifdef TCP_ADAPTER
+        CATCPCloseInProgressConnections();
+#endif
         OIC_LOG(DEBUG, TAG, "close all ssl session");
         CAcloseSslConnectionAll(CA_ADAPTER_IP);
 #endif
index 20427dc..16ce34d 100644 (file)
@@ -523,6 +523,13 @@ CAResult_t CAStopTCP()
 {
     CAIPStopNetworkMonitor(CA_ADAPTER_TCP);
 
+    /* Some times send queue thread fails to terminate as it's worker
+       thread gets blocked at TCP session's socket connect operation.
+       So closing sockets which are in connect operation at the time
+       of termination of adapter would save send queue thread from
+       getting blocked. */
+    CATCPCloseInProgressConnections();
+
     if (g_sendQueueHandle && g_sendQueueHandle->threadMutex)
     {
         CAQueueingThreadStop(g_sendQueueHandle);
index 92e8fda..988eaec 100644 (file)
@@ -1674,3 +1674,27 @@ void CAUpdateCSMState(const CAEndpoint_t *endpoint, CACSMExchangeState_t state)
     oc_mutex_unlock(g_mutexObjectList);
     return;
 }
+
+void CATCPCloseInProgressConnections()
+{
+    OIC_LOG(INFO, TAG, "IN - CATCPCloseInProgressConnections");
+
+#ifndef WSA_WAIT_EVENT_0
+    oc_mutex_lock(g_mutexObjectList);
+
+    CATCPSessionInfo_t *session = NULL;
+    LL_FOREACH(g_sessionList, session)
+    {
+        if (session && session->fd >= 0 && session->state == CONNECTING)
+        {
+            shutdown(session->fd, SHUT_RDWR);
+            close(session->fd);
+            session->fd = -1;
+            session->state = DISCONNECTED;
+        }
+    }
+
+    oc_mutex_unlock(g_mutexObjectList);
+#endif
+    OIC_LOG(INFO, TAG, "OUT - CATCPCloseInProgressConnections");
+}