Update snapshot(2018-01-31)
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / tcp_adapter / catcpserver.c
index 627028e..661612e 100644 (file)
@@ -115,6 +115,7 @@ static void CAReceiveMessage(int fd);
 static void CAReceiveHandler(void *data);
 static CAResult_t CATCPCreateSocket(int family, CATCPSessionInfo_t *svritem);
 static void CATCPInitializeSocket();
+static CATCPSessionInfo_t *CAGetSessionInfoFromFDAsOwner(int fd, size_t *index);
 
 #define CHECKFD(FD) \
     if (FD > caglobals.tcp.maxfd) \
@@ -288,8 +289,20 @@ static void CASelectReturned(fd_set *readFds)
     }
     else
     {
+        int *readFDList = NULL;
+        size_t readFDListSize = 0;
+
         oc_mutex_lock(g_mutexObjectList);
         uint32_t length = u_arraylist_length(caglobals.tcp.svrlist);
+
+        readFDList = (int*) OICCalloc(length, sizeof(int));
+        if (NULL == readFDList)
+        {
+            OIC_LOG_V(ERROR, TAG, "Failed to allocate memory!");
+            oc_mutex_unlock(g_mutexObjectList);
+            return;
+        }
+
         for (size_t i = 0; i < length; i++)
         {
             CATCPSessionInfo_t *svritem =
@@ -298,11 +311,19 @@ static void CASelectReturned(fd_set *readFds)
             {
                 if (FD_ISSET(svritem->fd, readFds))
                 {
-                    CAReceiveMessage(svritem->fd);
+                    readFDList[readFDListSize++] = svritem->fd;
                 }
             }
         }
         oc_mutex_unlock(g_mutexObjectList);
+
+        // Read incomming messages from fds
+        for (size_t i = 0; i < readFDListSize; i++)
+        {
+            CAReceiveMessage(readFDList[i]);
+        }
+
+        OICFree(readFDList);
     }
 }
 
@@ -346,12 +367,23 @@ static void CAAcceptConnection(CATransportFlags_t flag, CASocket_t *sock)
         CAConvertAddrToName((struct sockaddr_storage *)&clientaddr, clientlen,
                             svritem->sep.endpoint.addr, &svritem->sep.endpoint.port);
 
+        // Allocate message buffer
+        svritem->tlsdata = (unsigned char*) OICCalloc(TLS_DATA_MAX_SIZE, sizeof(unsigned char));
+        if (!svritem->tlsdata)
+        {
+            OIC_LOG(ERROR, TAG, "Out of memory");
+            close(sockfd);
+            OICFree(svritem);
+            return;
+        }
+
         oc_mutex_lock(g_mutexObjectList);
         bool result = u_arraylist_add(caglobals.tcp.svrlist, svritem);
         if (!result)
         {
             OIC_LOG(ERROR, TAG, "u_arraylist_add failed.");
             close(sockfd);
+            OICFree(svritem->tlsdata);
             OICFree(svritem);
             oc_mutex_unlock(g_mutexObjectList);
             return;
@@ -524,19 +556,21 @@ static void CAReceiveMessage(int fd)
 {
     CAResult_t res = CA_STATUS_OK;
 
+    oc_mutex_lock(g_mutexObjectList);
+
     //get remote device information from file descriptor.
     size_t index = 0;
-    CATCPSessionInfo_t *svritem = CAGetSessionInfoFromFD(fd, &index);
+    CATCPSessionInfo_t *svritem = CAGetSessionInfoFromFDAsOwner(fd, &index);
     if (!svritem)
     {
         OIC_LOG(ERROR, TAG, "there is no connection information in list");
+        oc_mutex_unlock(g_mutexObjectList);
         return;
     }
 
-    // read data
+    CASecureEndpoint_t peerEP = svritem->sep;
     int len = 0;
-
-    if (svritem->sep.endpoint.flags & CA_SECURE)
+    if (svritem->sep.endpoint.flags & CA_SECURE) // Secure connection
     {
         svritem->protocol = TLS;
 
@@ -554,15 +588,12 @@ static void CAReceiveMessage(int fd)
             tlsLength = TLS_HEADER_SIZE +
                             (size_t)((svritem->tlsdata[3] << 8) | svritem->tlsdata[4]);
             OIC_LOG_V(DEBUG, TAG, "total tls length = %u", tlsLength);
-            if (tlsLength > sizeof(svritem->tlsdata))
+            if (tlsLength > TLS_DATA_MAX_SIZE)
             {
                 OIC_LOG_V(ERROR, TAG, "total tls length is too big (buffer size : %u)",
-                                    sizeof(svritem->tlsdata));
-                if (CA_STATUS_OK != CAcloseSslConnection(&svritem->sep.endpoint))
-                {
-                    OIC_LOG(ERROR, TAG, "Failed to close TLS session");
-                }
-                CASearchAndDeleteTCPSession(&(svritem->sep.endpoint));
+                                    TLS_DATA_MAX_SIZE);
+                oc_mutex_unlock(g_mutexObjectList);
+                CATCPDisconnectSession(&peerEP.endpoint);
                 return;
             }
             nbRead = tlsLength - svritem->tlsLen;
@@ -587,21 +618,40 @@ static void CAReceiveMessage(int fd)
                                 nbRead, len, svritem->tlsLen);
             if (tlsLength > 0 && tlsLength == svritem->tlsLen)
             {
-                //when successfully read data - pass them to callback.
-                res = CAdecryptSsl(&svritem->sep, (uint8_t *)svritem->tlsdata, svritem->tlsLen);
-                svritem->tlsLen = 0;
+                // When successfully read data - pass them to callback.
+                // Dont invoke callback locking mutex
+                unsigned char *mesBuf = svritem->tlsdata;
+                size_t mesBufLen = svritem->tlsLen;
+                svritem->tlsdata = NULL;
+                oc_mutex_unlock(g_mutexObjectList);
+
+                res = CAdecryptSsl(&peerEP, (uint8_t *)mesBuf, mesBufLen);
                 OIC_LOG_V(INFO, TAG, "%s: CAdecryptSsl returned %d", __func__, res);
+
+                // Check for the svritem and reset buffer
+                oc_mutex_lock(g_mutexObjectList);
+                svritem = CAGetSessionInfoFromFDAsOwner(fd, &index);
+                if (svritem)
+                {
+                    svritem->tlsdata = mesBuf;
+                    svritem->tlsLen = 0;
+                }
+                else
+                {
+                    // svritem does not exist, thus free the message buffer
+                    OIC_LOG(ERROR, TAG, "svritem not found. Freeing message buffer!");
+                    OICFree(mesBuf);
+                }
             }
         }
 #endif
-
     }
-    else
+    else // Non-Secure connection
     {
         svritem->protocol = COAP;
 
         // svritem->tlsdata can also be used as receiving buffer in case of raw tcp
-        len = recv(fd, svritem->tlsdata, sizeof(svritem->tlsdata), 0);
+        len = recv(fd, svritem->tlsdata, TLS_DATA_MAX_SIZE, 0);
         if (len < 0)
         {
             OIC_LOG_V(ERROR, TAG, "recv failed %s", strerror(errno));
@@ -614,25 +664,40 @@ static void CAReceiveMessage(int fd)
         }
         else
         {
-            OIC_LOG_V(DEBUG, TAG, "recv() : %d bytes", len);
             //when successfully read data - pass them to callback.
+            OIC_LOG_V(DEBUG, TAG, "recv() : %d bytes", len);
             if (g_packetReceivedCallback)
             {
-                res = g_packetReceivedCallback(&svritem->sep, svritem->tlsdata, len);
+                // Dont invoke callback locking mutex
+                unsigned char *mesBuf = svritem->tlsdata;
+                svritem->tlsdata = NULL;
+                oc_mutex_unlock(g_mutexObjectList);
+
+                res = g_packetReceivedCallback(&peerEP, mesBuf, len);
+
+                // Check for the svritem and reset buffer
+                oc_mutex_lock(g_mutexObjectList);
+                svritem = CAGetSessionInfoFromFDAsOwner(fd, &index);
+                if (svritem)
+                {
+                    svritem->tlsdata = mesBuf;
+                    svritem->tlsLen = 0;
+                }
+                else
+                {
+                    // svritem does not exist, thus free the message buffer
+                    OIC_LOG(ERROR, TAG, "svritem not found. Freeing message buffer!");
+                    OICFree(mesBuf);
+                }
             }
         }
     }
 
-    //disconnect session and clean-up data if any error occurs
+    oc_mutex_unlock(g_mutexObjectList);
+
     if (res != CA_STATUS_OK)
     {
-        if (CA_STATUS_OK != CATCPDisconnectSession(&svritem->sep.endpoint))
-        {
-            CASearchAndDeleteTCPSession(&svritem->sep.endpoint);
-            OIC_LOG(ERROR, TAG, "Failed to disconnect TCP session");
-        }
-
-        return;
+        CATCPDisconnectSession(&peerEP.endpoint);
     }
 }
 
@@ -1207,6 +1272,15 @@ CASocketFd_t CAConnectTCPSession(const CAEndpoint_t *endpoint)
     svritem->state = CONNECTING;
     svritem->isClient = true;
 
+    // Allocate message buffer
+    svritem->tlsdata = (unsigned char*) OICCalloc(TLS_DATA_MAX_SIZE, sizeof(unsigned char));
+    if (!svritem->tlsdata)
+    {
+        OIC_LOG(ERROR, TAG, "Out of memory");
+        OICFree(svritem);
+        return OC_INVALID_SOCKET;
+    }
+
     // #2. add TCP connection info to list
     oc_mutex_lock(g_mutexObjectList);
     if (caglobals.tcp.svrlist)
@@ -1216,6 +1290,7 @@ CASocketFd_t CAConnectTCPSession(const CAEndpoint_t *endpoint)
         {
             OIC_LOG(ERROR, TAG, "u_arraylist_add failed.");
             close(svritem->fd);
+            OICFree(svritem->tlsdata);
             OICFree(svritem);
             oc_mutex_unlock(g_mutexObjectList);
             return OC_INVALID_SOCKET;
@@ -1266,6 +1341,9 @@ CAResult_t CADisconnectTCPSession(size_t index)
     OICFree(removedData->data);
     removedData->data = NULL;
 
+    OICFree(removedData->tlsdata);
+    removedData->tlsdata = NULL;
+
     OICFree(removedData);
     removedData = NULL;
 
@@ -1392,6 +1470,24 @@ CATCPSessionInfo_t *CAGetSessionInfoFromFD(int fd, size_t *index)
     return NULL;
 }
 
+static CATCPSessionInfo_t *CAGetSessionInfoFromFDAsOwner(int fd, size_t *index)
+{
+    CATCPSessionInfo_t *svritem = NULL;
+    uint32_t length = u_arraylist_length(caglobals.tcp.svrlist);
+    for (size_t i = 0; i < length; i++)
+    {
+        svritem = (CATCPSessionInfo_t *) u_arraylist_get(caglobals.tcp.svrlist, i);
+
+        if (svritem && svritem->fd == fd)
+        {
+            *index = i;
+            return svritem;
+        }
+    }
+
+    return NULL;
+}
+
 CAResult_t CASearchAndDeleteTCPSession(const CAEndpoint_t *endpoint)
 {
     oc_mutex_lock(g_mutexObjectList);