Update snapshot(2018-01-04)
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / tcp_adapter / catcpserver.c
index 7d8e80d..7136fe0 100644 (file)
 #include <sys/socket.h>
 #include <sys/select.h>
 #include <sys/ioctl.h>
+#ifdef __TIZENRT__
+#include <tinyara/config.h>
+#include <poll.h>
+#else
 #include <sys/poll.h>
+#endif
 #include <stdio.h>
 #include <unistd.h>
 #include <fcntl.h>
@@ -41,7 +46,7 @@
 #include "caipnwmonitor.h"
 #include <coap/pdu.h>
 #include "caadapterutils.h"
-#include "camutex.h"
+#include "octhread.h"
 #include "oic_malloc.h"
 
 #ifdef __WITH_TLS__
@@ -51,7 +56,8 @@
 /**
  * Logging tag for module name.
  */
-#define TAG "OIC_CA_TCP_SERVER"
+//#define TAG "OIC_CA_TCP_SERVER"
+#define TAG TCP_SERVER_TAG
 
 /**
  * Maximum CoAP over TCP header length
 #define TLS_HEADER_SIZE 5
 
 /**
+ * Max Connection Counts.
+ */
+#define MAX_CONNECTION_COUNTS   500
+
+#define COAP_TCP_MAX_BUFFER_CHUNK_SIZE 65530 //64kb - 6 (coap+tcp max header size)
+
+/**
+ * Thread pool.
+ */
+static ca_thread_pool_t g_threadPool = NULL;
+
+/**
  * Mutex to synchronize device object list.
  */
-static ca_mutex g_mutexObjectList = NULL;
+static oc_mutex g_mutexObjectList = NULL;
 
 /**
  * Conditional mutex to synchronize.
  */
-static ca_cond g_condObjectList = NULL;
+static oc_cond g_condObjectList = NULL;
 
 /**
  * Maintains the callback to be notified when data received from remote device.
@@ -89,10 +107,6 @@ static CATCPErrorHandleCallback g_tcpErrorHandler = NULL;
  */
 static CATCPConnectionHandleCallback g_connectionCallback = NULL;
 
-static CAResult_t CATCPCreateMutex();
-static void CATCPDestroyMutex();
-static CAResult_t CATCPCreateCond();
-static void CATCPDestroyCond();
 static CASocketFd_t CACreateAcceptSocket(int family, CASocket_t *sock);
 static void CAAcceptConnection(CATransportFlags_t flag, CASocket_t *sock);
 static void CAFindReadyMessage();
@@ -100,6 +114,7 @@ static void CASelectReturned(fd_set *readFds);
 static void CAReceiveMessage(int fd);
 static void CAReceiveHandler(void *data);
 static CAResult_t CATCPCreateSocket(int family, CATCPSessionInfo_t *svritem);
+static void CATCPInitializeSocket();
 
 #define CHECKFD(FD) \
     if (FD > caglobals.tcp.maxfd) \
@@ -118,67 +133,20 @@ static CAResult_t CATCPCreateSocket(int family, CATCPSessionInfo_t *svritem);
         FD_SET(caglobals.tcp.TYPE.fd, FDS); \
     }
 
-/**
- * Read length amount of data from socket item->fd
- * Can read less data length then requested
- * Actual read length added to item->len variable
- *
- * @param[in/out] item - used socket, buffer and to update received message length
- * @param[in]  length  - length of data required to read
- * @param[in]  flags   - additional info about socket
- * @return             - CA_STATUS_OK or appropriate error code
- */
-static CAResult_t CARecv(CATCPSessionInfo_t *item, size_t length, int flags)
-{
-    if (NULL == item)
-    {
-        return CA_STATUS_INVALID_PARAM;
-    }
-
-    //skip read operation if requested zero length
-    if (0 == length)
-    {
-        return CA_STATUS_OK;
-    }
-
-    unsigned char *buffer = item->data + item->len;
-
-    int len = recv(item->fd, buffer, length, flags);
-
-    if (len < 0)
-    {
-        OIC_LOG_V(ERROR, TAG, "recv failed %s", strerror(errno));
-        return CA_RECEIVE_FAILED;
-    }
-    else if (0 == len)
-    {
-        OIC_LOG(INFO, TAG, "Received disconnect from peer. Close connection");
-        item->state = DISCONNECTED;
-        return CA_DESTINATION_DISCONNECTED;
-    }
-
-    OIC_LOG_V(DEBUG, TAG, "recv len = %d", len);
-    OIC_LOG_BUFFER(DEBUG, TAG, buffer, len);
-
-    item->len += len;
-
-    return CA_STATUS_OK;
-}
-
-static void CATCPDestroyMutex()
+void CATCPDestroyMutex()
 {
     if (g_mutexObjectList)
     {
-        ca_mutex_free(g_mutexObjectList);
+        oc_mutex_free(g_mutexObjectList);
         g_mutexObjectList = NULL;
     }
 }
 
-static CAResult_t CATCPCreateMutex()
+CAResult_t CATCPCreateMutex()
 {
     if (!g_mutexObjectList)
     {
-        g_mutexObjectList = ca_mutex_new();
+        g_mutexObjectList = oc_mutex_new();
         if (!g_mutexObjectList)
         {
             OIC_LOG(ERROR, TAG, "Failed to created mutex!");
@@ -189,20 +157,20 @@ static CAResult_t CATCPCreateMutex()
     return CA_STATUS_OK;
 }
 
-static void CATCPDestroyCond()
+void CATCPDestroyCond()
 {
     if (g_condObjectList)
     {
-        ca_cond_free(g_condObjectList);
+        oc_cond_free(g_condObjectList);
         g_condObjectList = NULL;
     }
 }
 
-static CAResult_t CATCPCreateCond()
+CAResult_t CATCPCreateCond()
 {
     if (!g_condObjectList)
     {
-        g_condObjectList = ca_cond_new();
+        g_condObjectList = oc_cond_new();
         if (!g_condObjectList)
         {
             OIC_LOG(ERROR, TAG, "Failed to created cond!");
@@ -222,9 +190,9 @@ static void CAReceiveHandler(void *data)
         CAFindReadyMessage();
     }
 
-    ca_mutex_lock(g_mutexObjectList);
-    ca_cond_signal(g_condObjectList);
-    ca_mutex_unlock(g_mutexObjectList);
+    oc_mutex_lock(g_mutexObjectList);
+    oc_cond_signal(g_condObjectList);
+    oc_mutex_unlock(g_mutexObjectList);
 
     OIC_LOG(DEBUG, TAG, "OUT - CAReceiveHandler");
 }
@@ -236,12 +204,15 @@ static void CAFindReadyMessage()
 
     FD_ZERO(&readFds);
     CA_FD_SET(ipv4, &readFds);
+    CA_FD_SET(ipv4s, &readFds);
     CA_FD_SET(ipv6, &readFds);
-
+    CA_FD_SET(ipv6s, &readFds);
+#ifndef __TIZENRT__
     if (OC_INVALID_SOCKET != caglobals.tcp.shutdownFds[0])
     {
         FD_SET(caglobals.tcp.shutdownFds[0], &readFds);
     }
+#endif
     if (OC_INVALID_SOCKET != caglobals.tcp.connectionFds[0])
     {
         FD_SET(caglobals.tcp.connectionFds[0], &readFds);
@@ -262,18 +233,19 @@ static void CAFindReadyMessage()
 
     if (caglobals.tcp.terminate)
     {
-        OIC_LOG_V(DEBUG, TAG, "Packet receiver Stop request received.");
+        OIC_LOG_V(INFO, TAG, "Packet receiver Stop request received.");
         return;
     }
-
-    if (0 < ret)
-    {
-        CASelectReturned(&readFds);
-    }
-    else if (0 > ret)
+    if (0 >= ret)
     {
-        OIC_LOG_V(FATAL, TAG, "select error %s", strerror(errno));
+        if (0 > ret)
+        {
+            OIC_LOG_V(FATAL, TAG, "select error %s", strerror(errno));
+        }
+        return;
     }
+
+    CASelectReturned(&readFds);
 }
 
 static void CASelectReturned(fd_set *readFds)
@@ -285,11 +257,21 @@ static void CASelectReturned(fd_set *readFds)
         CAAcceptConnection(CA_IPV4, &caglobals.tcp.ipv4);
         return;
     }
+    else if (caglobals.tcp.ipv4s.fd != -1 && FD_ISSET(caglobals.tcp.ipv4s.fd, readFds))
+    {
+        CAAcceptConnection(CA_IPV4 | CA_SECURE, &caglobals.tcp.ipv4s);
+        return;
+    }
     else if (caglobals.tcp.ipv6.fd != -1 && FD_ISSET(caglobals.tcp.ipv6.fd, readFds))
     {
         CAAcceptConnection(CA_IPV6, &caglobals.tcp.ipv6);
         return;
     }
+    else if (caglobals.tcp.ipv6s.fd != -1 && FD_ISSET(caglobals.tcp.ipv6s.fd, readFds))
+    {
+        CAAcceptConnection(CA_IPV6 | CA_SECURE, &caglobals.tcp.ipv6s);
+        return;
+    }
     else if (-1 != caglobals.tcp.connectionFds[0] &&
             FD_ISSET(caglobals.tcp.connectionFds[0], readFds))
     {
@@ -302,7 +284,6 @@ static void CASelectReturned(fd_set *readFds)
             return;
         }
         OIC_LOG_V(DEBUG, TAG, "Received new connection event with [%s]", buf);
-        FD_CLR(caglobals.tcp.connectionFds[0], readFds);
         return;
     }
     else
@@ -317,10 +298,6 @@ static void CASelectReturned(fd_set *readFds)
                 if (FD_ISSET(svritem->fd, readFds))
                 {
                     CAReceiveMessage(svritem->fd);
-                    if (-1 != svritem->fd)
-                    {
-                        FD_CLR(svritem->fd, readFds);
-                    }
                 }
             }
         }
@@ -329,8 +306,17 @@ static void CASelectReturned(fd_set *readFds)
 
 static void CAAcceptConnection(CATransportFlags_t flag, CASocket_t *sock)
 {
+    OIC_LOG_V(INFO, TAG, "In %s", __func__);
     VERIFY_NON_NULL_VOID(sock, TAG, "sock is NULL");
 
+    if (MAX_CONNECTION_COUNTS == u_arraylist_length(caglobals.tcp.svrlist))
+    {
+        OIC_LOG_V(INFO, TAG, "Exceeding the max connection counts limit, close listening port");
+        close(sock->fd);
+        sock->fd = OC_INVALID_SOCKET;
+        return;
+    }
+
     struct sockaddr_storage clientaddr;
     socklen_t clientlen = sizeof (struct sockaddr_in);
     if (flag & CA_IPV6)
@@ -354,52 +340,32 @@ static void CAAcceptConnection(CATransportFlags_t flag, CASocket_t *sock)
         svritem->sep.endpoint.flags = flag;
         svritem->sep.endpoint.adapter = CA_ADAPTER_TCP;
         svritem->state = CONNECTED;
+        svritem->isClient = false;
         CAConvertAddrToName((struct sockaddr_storage *)&clientaddr, clientlen,
                             svritem->sep.endpoint.addr, &svritem->sep.endpoint.port);
 
-        ca_mutex_lock(g_mutexObjectList);
+        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);
-            ca_mutex_unlock(g_mutexObjectList);
+            oc_mutex_unlock(g_mutexObjectList);
             return;
         }
-        ca_mutex_unlock(g_mutexObjectList);
+        oc_mutex_unlock(g_mutexObjectList);
 
         CHECKFD(sockfd);
-    }
-}
 
-#ifdef __WITH_TLS__
-static bool CAIsTlsMessage(const unsigned char* data, size_t length)
-{
-    if (NULL == data || 0 == length)
-    {
-        OIC_LOG_V(ERROR, TAG, "%s: null input param", __func__);
-        return false;
-    }
-
-    unsigned char first_byte = data[0];
-
-    //TLS Plaintext has four types: change_cipher_spec = [14], alert = [15],
-    //handshake = [16], application_data = [17] in HEX
-    const uint8_t tls_head_type[] = {0x14, 0x15, 0x16, 0x17};
-    size_t i = 0;
-
-    for (i = 0; i < sizeof(tls_head_type); i++)
-    {
-        if(tls_head_type[i] == first_byte)
+        // pass the connection information to CA Common Layer.
+        if (g_connectionCallback)
         {
-            return true;
+            g_connectionCallback(&(svritem->sep.endpoint), true, svritem->isClient);
         }
     }
-
-    return false;
+    OIC_LOG_V(INFO, TAG, "Out %s", __func__);
 }
-#endif
 
 /**
  * Clean socket state data
@@ -413,8 +379,11 @@ void CACleanData(CATCPSessionInfo_t *svritem)
         OICFree(svritem->data);
         svritem->data = NULL;
         svritem->len = 0;
+#ifdef __WITH_TLS__
         svritem->tlsLen = 0;
+#endif
         svritem->totalLen = 0;
+        svritem->bufLen = 0;
         svritem->protocol = UNKNOWN;
     }
 }
@@ -455,6 +424,7 @@ CAResult_t CAConstructCoAP(CATCPSessionInfo_t *svritem, unsigned char **data,
         // copy 1 byte to parse coap header length
         memcpy(svritem->data, inBuffer, 1);
         svritem->len = 1;
+        svritem->bufLen = COAP_MAX_HEADER_SIZE;
         inBuffer++;
         inLen--;
     }
@@ -499,197 +469,55 @@ CAResult_t CAConstructCoAP(CATCPSessionInfo_t *svritem, unsigned char **data,
 
         //calculate CoAP message length
         svritem->totalLen = CAGetTotalLengthFromHeader(svritem->data);
-
-        // allocate required memory
-        unsigned char *buffer = OICRealloc(svritem->data, svritem->totalLen);
-        if (NULL == buffer)
-        {
-            OIC_LOG(ERROR, TAG, "OICRealloc - out of memory");
-            return CA_MEMORY_ALLOC_FAILED;
-        }
-        svritem->data = buffer;
     }
 
     // PAYLOAD
     if (inLen > 0)
     {
-        // read required bytes to have full CoAP payload
+        // Calculate length of data to be copied.
         copyLen = svritem->totalLen - svritem->len;
         if (inLen < copyLen)
         {
             copyLen = inLen;
         }
 
-        //read required bytes to have full CoAP header
-        memcpy(svritem->data + svritem->len, inBuffer, copyLen);
-        svritem->len += copyLen;
-        inBuffer += copyLen;
-        inLen -= copyLen;
-    }
-
-    *data = inBuffer;
-    *dataLength = inLen;
-
-    OIC_LOG_V(DEBUG, TAG, "after-datalength : %u", *dataLength);
-    OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
-    return CA_STATUS_OK;
-}
-
-/**
- * Read message header from socket item->fd
- *
- * @param[in/out] item - used socket, buffer, current received message length and protocol
- * @return             - CA_STATUS_OK or appropriate error code
- */
-static CAResult_t CAReadHeader(CATCPSessionInfo_t *svritem)
-{
-    CAResult_t res = CA_STATUS_OK;
-
-    if (NULL == svritem)
-    {
-        return CA_STATUS_INVALID_PARAM;
-    }
-
-    if (NULL == svritem->data)
-    {
-        // allocate memory for message header (CoAP header size because it is bigger)
-        svritem->data = (unsigned char *) OICCalloc(1, COAP_MAX_HEADER_SIZE);
-        if (NULL == svritem->data)
-        {
-            OIC_LOG(ERROR, TAG, "OICCalloc - out of memory");
-            return CA_MEMORY_ALLOC_FAILED;
-        }
-    }
-
-    //read data (assume TLS header) from remote device.
-    //use TLS_HEADER_SIZE - svritem->len because even header can be read partially
-    res = CARecv(svritem, TLS_HEADER_SIZE - svritem->len, 0);
-
-    //return if any error occurs
-    if (CA_STATUS_OK != res)
-    {
-        return res;
-    }
-
-    //if not enough data received - read them on next CAReceiveMessage() call
-    if (svritem->len < TLS_HEADER_SIZE)
-    {
-        OIC_LOG(DEBUG, TAG, "Header received partially. Wait for rest header data");
-        return CA_STATUS_OK;
-    }
-
-    //if enough data received - parse header
-#ifdef __WITH_TLS__
-    if (CAIsTlsMessage(svritem->data, svritem->len))
-    {
-        svritem->protocol = TLS;
-
-        //[3][4] bytes in tls header are tls payload length
-        unsigned int message_length = (unsigned int)((svritem->data[3] << 8) | svritem->data[4]);
-        OIC_LOG_V(DEBUG, TAG, "%s: message_length = %d", __func__, message_length);
-
-        svritem->totalLen = message_length + TLS_HEADER_SIZE;
-    }
-    else
-#endif
-    {
-        svritem->protocol = COAP;
-
-        //seems CoAP data received. read full coap header.
-        coap_transport_t transport = coap_get_tcp_header_type_from_initbyte(svritem->data[0] >> 4);
-
-        size_t headerLen = coap_get_tcp_header_length_for_transport(transport);
-
-        if (svritem->len < headerLen)
+        // Is buffer not big enough for remaining data ?
+        if (svritem->len + copyLen > svritem->bufLen)
         {
-            //read required bytes to have full CoAP header
-            //it should be 1 byte (COAP_MAX_HEADER_SIZE - TLS_HEADER_SIZE)
-            res = CARecv(svritem, headerLen - svritem->len, 0);
-
-            //return if any error occurs
-            if (CA_STATUS_OK != res)
+            // Resize buffer to accommodate enough space
+            size_t extLen = svritem->totalLen - svritem->bufLen;
+            if (extLen > COAP_TCP_MAX_BUFFER_CHUNK_SIZE)
             {
-                return res;
+                extLen = COAP_TCP_MAX_BUFFER_CHUNK_SIZE;
             }
 
-            //if not enough data received - read them on next CAReceiveMessage() call
-            if (svritem->len < headerLen)
+            // Allocate required memory
+            unsigned char *buffer = OICRealloc(svritem->data, svritem->bufLen + extLen);
+            if (NULL == buffer)
             {
-                OIC_LOG(DEBUG, TAG, "CoAP header received partially. Wait for rest header data");
-                return CA_STATUS_OK;
+                OIC_LOG(ERROR, TAG, "OICRealloc - out of memory");
+                return CA_MEMORY_ALLOC_FAILED;
             }
+
+            svritem->data = buffer;
+            svritem->bufLen += extLen;
         }
 
-        //calculate CoAP message length
-        svritem->totalLen = CAGetTotalLengthFromHeader(svritem->data);
+        // Read required bytes to have full CoAP payload
+        memcpy(svritem->data + svritem->len, inBuffer, copyLen);
+        svritem->len += copyLen;
+        inBuffer += copyLen;
+        inLen -= copyLen;
     }
 
-    unsigned char *buffer = OICRealloc(svritem->data, svritem->totalLen);
-    if (NULL == buffer)
-    {
-        OIC_LOG(ERROR, TAG, "OICRealloc - out of memory");
-        return CA_MEMORY_ALLOC_FAILED;
-    }
-    svritem->data = buffer;
+    *data = inBuffer;
+    *dataLength = inLen;
 
+    OIC_LOG_V(DEBUG, TAG, "after-datalength : %u", *dataLength);
+    OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
     return CA_STATUS_OK;
 }
 
-/**
- * Read message payload from socket item->fd
-
- *
- * @param[in/out] item - used socket, buffer and to update received message length
- * @return             - CA_STATUS_OK or appropriate error code
- */
-static CAResult_t CAReadPayload(CATCPSessionInfo_t *svritem)
-{
-    if (NULL == svritem)
-    {
-        return CA_STATUS_INVALID_PARAM;
-    }
-
-    return CARecv(svritem, svritem->totalLen - svritem->len, 0);
-}
-
-/**
- * Pass received data to app layer depending on protocol
- *
- * @param[in/out] item - used buffer, received message length and protocol
- */
-static void CAExecuteRequest(CATCPSessionInfo_t *svritem)
-{
-    if (NULL == svritem)
-    {
-        return;
-    }
-
-    switch(svritem->protocol)
-    {
-        case COAP:
-        {
-            if (g_packetReceivedCallback)
-            {
-                g_packetReceivedCallback(&svritem->sep, svritem->data, svritem->len);
-            }
-        }
-        break;
-        case TLS:
-#ifdef __WITH_TLS__
-        {
-            int ret = CAdecryptSsl(&svritem->sep, (uint8_t *)svritem->data, svritem->len);
-
-            OIC_LOG_V(DEBUG, TAG, "%s: CAdecryptSsl returned %d", __func__, ret);
-        }
-        break;
-#endif
-        case UNKNOWN: /* pass through */
-        default:
-            OIC_LOG(ERROR, TAG, "unknown application protocol. Ignore it");
-        break;
-    }
-}
-
 static void CAReceiveMessage(int fd)
 {
     CAResult_t res = CA_STATUS_OK;
@@ -723,12 +551,17 @@ static void CAReceiveMessage(int fd)
             //[3][4] bytes in tls header are tls payload length
             tlsLength = TLS_HEADER_SIZE +
                             (size_t)((svritem->tlsdata[3] << 8) | svritem->tlsdata[4]);
-            OIC_LOG_V(DEBUG, TAG, "toal tls length = %u", tlsLength);
+            OIC_LOG_V(DEBUG, TAG, "total tls length = %u", tlsLength);
             if (tlsLength > sizeof(svritem->tlsdata))
             {
-                OIC_LOG_V(ERROR, TAG, "toal tls length is too big (buffer size : %u)",
+                OIC_LOG_V(ERROR, TAG, "total tls length is too big (buffer size : %u)",
                                     sizeof(svritem->tlsdata));
-                return CA_STATUS_FAILED;
+                if (CA_STATUS_OK != CAcloseSslConnection(&svritem->sep.endpoint))
+                {
+                    OIC_LOG(ERROR, TAG, "Failed to close TLS session");
+                }
+                CASearchAndDeleteTCPSession(&(svritem->sep.endpoint));
+                return;
             }
             nbRead = tlsLength - svritem->tlsLen;
         }
@@ -742,6 +575,7 @@ static void CAReceiveMessage(int fd)
         else if (0 == len)
         {
             OIC_LOG(INFO, TAG, "Received disconnect from peer. Close connection");
+            svritem->state = DISCONNECTED;
             res = CA_DESTINATION_DISCONNECTED;
         }
         else
@@ -754,7 +588,7 @@ static void CAReceiveMessage(int fd)
                 //when successfully read data - pass them to callback.
                 res = CAdecryptSsl(&svritem->sep, (uint8_t *)svritem->tlsdata, svritem->tlsLen);
                 svritem->tlsLen = 0;
-                OIC_LOG_V(DEBUG, TAG, "%s: CAdecryptSsl returned %d", __func__, res);
+                OIC_LOG_V(INFO, TAG, "%s: CAdecryptSsl returned %d", __func__, res);
             }
         }
 #endif
@@ -762,10 +596,10 @@ static void CAReceiveMessage(int fd)
     }
     else
     {
-        unsigned char buffer[65535] = {0,}; // 65535 is the maximum size of ip packet
         svritem->protocol = COAP;
 
-        len = recv(fd, buffer, sizeof(buffer), 0);
+        // svritem->tlsdata can also be used as receiving buffer in case of raw tcp
+        len = recv(fd, svritem->tlsdata, sizeof(svritem->tlsdata), 0);
         if (len < 0)
         {
             OIC_LOG_V(ERROR, TAG, "recv failed %s", strerror(errno));
@@ -782,7 +616,7 @@ static void CAReceiveMessage(int fd)
             //when successfully read data - pass them to callback.
             if (g_packetReceivedCallback)
             {
-                g_packetReceivedCallback(&svritem->sep, buffer, len);
+                res = g_packetReceivedCallback(&svritem->sep, svritem->tlsdata, len);
             }
         }
     }
@@ -790,13 +624,12 @@ static void CAReceiveMessage(int fd)
     //disconnect session and clean-up data if any error occurs
     if (res != CA_STATUS_OK)
     {
-#ifdef __WITH_TLS__
-        if (CA_STATUS_OK != CAcloseSslConnection(&svritem->sep.endpoint))
+        if (CA_STATUS_OK != CATCPDisconnectSession(&svritem->sep.endpoint))
         {
-            OIC_LOG(ERROR, TAG, "Failed to close TLS session");
+            CASearchAndDeleteTCPSession(&svritem->sep.endpoint);
+            OIC_LOG(ERROR, TAG, "Failed to disconnect TCP session");
         }
-#endif
-        CASearchAndDeleteTCPSession(&(svritem->sep.endpoint));
+
         return;
     }
 }
@@ -863,7 +696,7 @@ static CAResult_t CATCPCreateSocket(int family, CATCPSessionInfo_t *svritem)
 {
     VERIFY_NON_NULL(svritem, TAG, "svritem is NULL");
 
-    OIC_LOG_V(DEBUG, TAG, "try to connect with [%s:%u]",
+    OIC_LOG_V(INFO, TAG, "try to connect with [%s:%u]",
               svritem->sep.endpoint.addr, svritem->sep.endpoint.port);
 
     // #1. create tcp socket.
@@ -905,10 +738,12 @@ static CAResult_t CATCPCreateSocket(int family, CATCPSessionInfo_t *svritem)
     if (connect(fd, (struct sockaddr *)&sa, socklen) < 0)
     {
         OIC_LOG_V(ERROR, TAG, "failed to connect socket, %s", strerror(errno));
+        CALogSendStateInfo(svritem->sep.endpoint.adapter, svritem->sep.endpoint.addr,
+                           svritem->sep.endpoint.port, 0, false, strerror(errno));
         return CA_SOCKET_OPERATION_FAILED;
     }
 
-    OIC_LOG(DEBUG, TAG, "connect socket success");
+    OIC_LOG(INFO, TAG, "connect socket success");
     svritem->state = CONNECTED;
     CHECKFD(svritem->fd);
     ssize_t len = CAWakeUpForReadFdsUpdate(svritem->sep.endpoint.addr);
@@ -944,11 +779,14 @@ static CASocketFd_t CACreateAcceptSocket(int family, CASocket_t *sock)
     {
         // the socket is restricted to sending and receiving IPv6 packets only.
         int on = 1;
+//TODO: enable once IPv6 is supported
+#ifndef __TIZENRT__
         if (-1 == setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)))
         {
             OIC_LOG_V(ERROR, TAG, "IPV6_V6ONLY failed: %s", strerror(errno));
             goto exit;
         }
+#endif
         ((struct sockaddr_in6 *)&server)->sin6_port = htons(sock->port);
         socklen = sizeof (struct sockaddr_in6);
     }
@@ -977,7 +815,17 @@ static CASocketFd_t CACreateAcceptSocket(int family, CASocket_t *sock)
         goto exit;
     }
 
-    if (!sock->port)  // return the assigned port
+    if (sock->port) // use the given port
+    {
+        int on = 1;
+        if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, OPTVAL_T(&on), sizeof (on)))
+        {
+            OIC_LOG_V(ERROR, TAG, "SO_REUSEADDR failed: %s", strerror(errno));
+            close(fd);
+            return OC_INVALID_SOCKET;
+        }
+    }
+    else  // return the assigned port
     {
         if (-1 == getsockname(fd, (struct sockaddr *)&server, &socklen))
         {
@@ -1002,6 +850,20 @@ exit:
 static void CAInitializePipe(int *fds)
 {
     int ret = pipe(fds);
+// TODO: Remove temporary workaround once F_GETFD / F_SETFD support is in TizenRT
+/* Temporary workaround: By pass F_GETFD / F_SETFD */
+#ifdef __TIZENRT__
+    if (-1 == ret)
+    {
+        close(fds[1]);
+        close(fds[0]);
+
+        fds[0] = -1;
+        fds[1] = -1;
+
+        OIC_LOG_V(ERROR, TAG, "pipe failed: %s", strerror(errno));
+    }
+#else
     if (-1 != ret)
     {
         ret = fcntl(fds[0], F_GETFD);
@@ -1028,6 +890,7 @@ static void CAInitializePipe(int *fds)
             OIC_LOG_V(ERROR, TAG, "pipe failed: %s", strerror(errno));
         }
     }
+#endif
 }
 
 #define NEWSOCKET(FAMILY, NAME) \
@@ -1039,14 +902,47 @@ static void CAInitializePipe(int *fds)
     } \
     CHECKFD(caglobals.tcp.NAME.fd);
 
+void CATCPInitializeSocket()
+{
+#ifndef __WITH_TLS__
+        NEWSOCKET(AF_INET, ipv4);
+#else
+        NEWSOCKET(AF_INET, ipv4s);
+#endif
+
+//TODO Enable once TizenRT supports IPv6
+#ifndef __TIZENRT__
+#ifndef __WITH_TLS__
+        NEWSOCKET(AF_INET6, ipv6);
+#else
+        NEWSOCKET(AF_INET6, ipv6s);
+#endif
+#endif
+#ifndef __WITH_TLS__
+        OIC_LOG_V(DEBUG, TAG, "IPv4 socket fd=%d, port=%d",
+                  caglobals.tcp.ipv4.fd, caglobals.tcp.ipv4.port);
+        OIC_LOG_V(DEBUG, TAG, "IPv6 socket fd=%d, port=%d",
+                  caglobals.tcp.ipv6.fd, caglobals.tcp.ipv6.port);
+#else
+        OIC_LOG_V(DEBUG, TAG, "IPv4 secure socket fd=%d, port=%d",
+                  caglobals.tcp.ipv4s.fd, caglobals.tcp.ipv4s.port);
+        OIC_LOG_V(DEBUG, TAG, "IPv6 secure socket fd=%d, port=%d",
+                  caglobals.tcp.ipv6s.fd, caglobals.tcp.ipv6s.port);
+#endif
+}
+
 CAResult_t CATCPStartServer(const ca_thread_pool_t threadPool)
 {
+    oc_mutex_lock(g_mutexObjectList);
     if (caglobals.tcp.started)
     {
-        OIC_LOG(DEBUG, TAG, "Adapter is started already");
+        oc_mutex_unlock(g_mutexObjectList);
+        OIC_LOG(INFO, TAG, "Adapter is started already");
         return CA_STATUS_OK;
     }
 
+    g_threadPool = threadPool;
+
     if (!caglobals.tcp.ipv4tcpenabled)
     {
         caglobals.tcp.ipv4tcpenabled = true;    // only needed to run CA tests
@@ -1056,101 +952,107 @@ CAResult_t CATCPStartServer(const ca_thread_pool_t threadPool)
         caglobals.tcp.ipv6tcpenabled = true;    // only needed to run CA tests
     }
 
-    CAResult_t res = CATCPCreateMutex();
-    if (CA_STATUS_OK == res)
-    {
-        res = CATCPCreateCond();
-    }
-    if (CA_STATUS_OK != res)
-    {
-        OIC_LOG(ERROR, TAG, "failed to create mutex/cond");
-        return res;
-    }
-
-    ca_mutex_lock(g_mutexObjectList);
+    caglobals.tcp.terminate = false;
     if (!caglobals.tcp.svrlist)
     {
         caglobals.tcp.svrlist = u_arraylist_create();
     }
-    ca_mutex_unlock(g_mutexObjectList);
 
     if (caglobals.server)
     {
-        NEWSOCKET(AF_INET, ipv4);
-        NEWSOCKET(AF_INET6, ipv6);
-        OIC_LOG_V(DEBUG, TAG, "IPv4 socket fd=%d, port=%d",
-                  caglobals.tcp.ipv4.fd, caglobals.tcp.ipv4.port);
-        OIC_LOG_V(DEBUG, TAG, "IPv6 socket fd=%d, port=%d",
-                  caglobals.tcp.ipv6.fd, caglobals.tcp.ipv6.port);
+        CATCPInitializeSocket();
     }
-
+#ifndef __TIZENRT__
     // create pipe for fast shutdown
     CAInitializePipe(caglobals.tcp.shutdownFds);
     CHECKFD(caglobals.tcp.shutdownFds[0]);
     CHECKFD(caglobals.tcp.shutdownFds[1]);
-
+#endif
     // create pipe for connection event
     CAInitializePipe(caglobals.tcp.connectionFds);
     CHECKFD(caglobals.tcp.connectionFds[0]);
     CHECKFD(caglobals.tcp.connectionFds[1]);
 
-    caglobals.tcp.terminate = false;
-    res = ca_thread_pool_add_task(threadPool, CAReceiveHandler, NULL);
+    CAResult_t res = CA_STATUS_OK;
+#ifndef __TIZENRT__
+    res = ca_thread_pool_add_task(g_threadPool, CAReceiveHandler, NULL, NULL);
+#else
+    res = ca_thread_pool_add_task(g_threadPool, CAReceiveHandler, NULL, NULL,
+                                 "IoT_TCPReceive", CONFIG_IOTIVITY_TCPRECEIVE_PTHREAD_STACKSIZE);
+#endif
     if (CA_STATUS_OK != res)
     {
+        oc_mutex_unlock(g_mutexObjectList);
         OIC_LOG(ERROR, TAG, "thread_pool_add_task failed");
+        CATCPStopServer();
         return res;
     }
-    OIC_LOG(DEBUG, TAG, "CAReceiveHandler thread started successfully.");
 
     caglobals.tcp.started = true;
+    oc_mutex_unlock(g_mutexObjectList);
+
+    OIC_LOG(INFO, TAG, "CAReceiveHandler thread started successfully.");
     return CA_STATUS_OK;
 }
 
 void CATCPStopServer()
 {
+    oc_mutex_lock(g_mutexObjectList);
     if (caglobals.tcp.terminate)
     {
-        OIC_LOG(DEBUG, TAG, "Adapter is not enabled");
+        oc_mutex_unlock(g_mutexObjectList);
+        OIC_LOG(INFO, TAG, "Adapter is not enabled");
         return;
     }
 
-    // mutex lock
-    ca_mutex_lock(g_mutexObjectList);
-
     // set terminate flag.
     caglobals.tcp.terminate = true;
 
-    if (caglobals.tcp.shutdownFds[1] != -1)
-    {
-        close(caglobals.tcp.shutdownFds[1]);
-        caglobals.tcp.shutdownFds[1] = OC_INVALID_SOCKET;
-        // receive thread will stop immediately
-    }
-    if (caglobals.tcp.connectionFds[1] != -1)
+#ifdef __TIZENRT__
+    if (caglobals.tcp.started)
     {
-        close(caglobals.tcp.connectionFds[1]);
-        caglobals.tcp.connectionFds[1] = OC_INVALID_SOCKET;
+        oc_cond_wait(g_condObjectList, g_mutexObjectList);
+        caglobals.tcp.started = false;
     }
+#endif
 
     // close accept socket.
+#ifndef __WITH_TLS__
     CLOSE_SOCKET(ipv4);
     CLOSE_SOCKET(ipv6);
+#else
+    CLOSE_SOCKET(ipv4s);
+    CLOSE_SOCKET(ipv6s);
+#endif
 
+    close(caglobals.tcp.connectionFds[1]);
+    close(caglobals.tcp.connectionFds[0]);
+    caglobals.tcp.connectionFds[1] = OC_INVALID_SOCKET;
+    caglobals.tcp.connectionFds[0] = OC_INVALID_SOCKET;
+#ifndef __TIZENRT__
+    if (caglobals.tcp.shutdownFds[1] != OC_INVALID_SOCKET)
+    {
+        close(caglobals.tcp.shutdownFds[1]);
+        caglobals.tcp.shutdownFds[1] = OC_INVALID_SOCKET;
+        // receive thread will stop immediately
+    }
     if (caglobals.tcp.started)
     {
-        ca_cond_wait(g_condObjectList, g_mutexObjectList);
+        oc_cond_wait(g_condObjectList, g_mutexObjectList);
+        caglobals.tcp.started = false;
     }
-    caglobals.tcp.started = false;
-
-    // mutex unlock
-    ca_mutex_unlock(g_mutexObjectList);
+    if (caglobals.tcp.shutdownFds[0] != OC_INVALID_SOCKET)
+    {
+        close(caglobals.tcp.shutdownFds[0]);
+        caglobals.tcp.shutdownFds[0] = OC_INVALID_SOCKET;
+    }
+#endif
+    oc_mutex_unlock(g_mutexObjectList);
 
     CATCPDisconnectAll();
-    CATCPDestroyMutex();
-    CATCPDestroyCond();
+    sleep(1);
 
-    OIC_LOG(DEBUG, TAG, "Adapter terminated successfully");
+    OIC_LOG(INFO, TAG, "Adapter terminated successfully");
 }
 
 void CATCPSetPacketReceiveCallback(CATCPPacketReceivedCallback callback)
@@ -1174,6 +1076,7 @@ size_t CACheckPayloadLengthFromHeader(const void *data, size_t dlen)
     if (!pdu)
     {
         OIC_LOG(ERROR, TAG, "outpdu is null");
+        OIC_LOG_V(ERROR, TAG, "data length: %zu", dlen);
         return 0;
     }
 
@@ -1219,14 +1122,27 @@ static ssize_t sendData(const CAEndpoint_t *endpoint, const void *data,
 
     // #2. send data to remote device.
     ssize_t remainLen = dlen;
+    size_t sendCounter = 0;
     do
     {
-        ssize_t len = send(sockFd, data, remainLen, 0);
+#ifdef MSG_NOSIGNAL
+        ssize_t len = send(sockFd, data, remainLen, MSG_DONTWAIT | MSG_NOSIGNAL);
+#else
+        ssize_t len = send(sockFd, data, remainLen, MSG_DONTWAIT);
+#endif
         if (-1 == len)
         {
-            if (EWOULDBLOCK != errno)
+            if (EWOULDBLOCK != errno && EAGAIN != errno)
             {
                 OIC_LOG_V(ERROR, TAG, "unicast ipv4tcp sendTo failed: %s", strerror(errno));
+                CALogSendStateInfo(endpoint->adapter, endpoint->addr, endpoint->port,
+                                   len, false, strerror(errno));
+                return len;
+            }
+            sendCounter++;
+            OIC_LOG_V(WARNING, TAG, "send blocked. trying %zu attempt from 25", sendCounter);
+            if(sendCounter >= 25)
+            {
                 return len;
             }
             continue;
@@ -1239,6 +1155,8 @@ static ssize_t sendData(const CAEndpoint_t *endpoint, const void *data,
     (void)fam;
 #endif
     OIC_LOG_V(INFO, TAG, "unicast %stcp sendTo is successful: %zu bytes", fam, dlen);
+    CALogSendStateInfo(endpoint->adapter, endpoint->addr, endpoint->port,
+                       dlen, true, NULL);
     return dlen;
 }
 
@@ -1285,9 +1203,10 @@ CASocketFd_t CAConnectTCPSession(const CAEndpoint_t *endpoint)
     svritem->sep.endpoint.flags = endpoint->flags;
     svritem->sep.endpoint.ifindex = endpoint->ifindex;
     svritem->state = CONNECTING;
+    svritem->isClient = true;
 
     // #2. add TCP connection info to list
-    ca_mutex_lock(g_mutexObjectList);
+    oc_mutex_lock(g_mutexObjectList);
     if (caglobals.tcp.svrlist)
     {
         bool res = u_arraylist_add(caglobals.tcp.svrlist, svritem);
@@ -1296,11 +1215,11 @@ CASocketFd_t CAConnectTCPSession(const CAEndpoint_t *endpoint)
             OIC_LOG(ERROR, TAG, "u_arraylist_add failed.");
             close(svritem->fd);
             OICFree(svritem);
-            ca_mutex_unlock(g_mutexObjectList);
+            oc_mutex_unlock(g_mutexObjectList);
             return OC_INVALID_SOCKET;
         }
     }
-    ca_mutex_unlock(g_mutexObjectList);
+    oc_mutex_unlock(g_mutexObjectList);
 
     // #3. create the socket and connect to TCP server
     int family = (svritem->sep.endpoint.flags & CA_IPV6) ? AF_INET6 : AF_INET;
@@ -1312,7 +1231,7 @@ CASocketFd_t CAConnectTCPSession(const CAEndpoint_t *endpoint)
     // #4. pass the connection information to CA Common Layer.
     if (g_connectionCallback)
     {
-        g_connectionCallback(&(svritem->sep.endpoint), true);
+        g_connectionCallback(&(svritem->sep.endpoint), true, svritem->isClient);
     }
 
     return svritem->fd;
@@ -1339,7 +1258,7 @@ CAResult_t CADisconnectTCPSession(size_t index)
         // pass the connection information to CA Common Layer.
         if (g_connectionCallback && DISCONNECTED == removedData->state)
         {
-            g_connectionCallback(&(removedData->sep.endpoint), false);
+            g_connectionCallback(&(removedData->sep.endpoint), false, removedData->isClient);
         }
     }
     OICFree(removedData->data);
@@ -1349,12 +1268,19 @@ CAResult_t CADisconnectTCPSession(size_t index)
     removedData = NULL;
 
     OIC_LOG(DEBUG, TAG, "data is removed from session list");
+
+    if (caglobals.server && MAX_CONNECTION_COUNTS == u_arraylist_length(caglobals.tcp.svrlist) + 1)
+    {
+        CATCPInitializeSocket();
+    }
     return CA_STATUS_OK;
 }
 
 void CATCPDisconnectAll()
 {
-    ca_mutex_lock(g_mutexObjectList);
+    OIC_LOG(DEBUG, TAG, "IN - CATCPDisconnectAll");
+
+    oc_mutex_lock(g_mutexObjectList);
 
     uint32_t length = u_arraylist_length(caglobals.tcp.svrlist);
     for (ssize_t index = length; index > 0; index--)
@@ -1366,12 +1292,13 @@ void CATCPDisconnectAll()
     u_arraylist_destroy(caglobals.tcp.svrlist);
     caglobals.tcp.svrlist = NULL;
 
-    ca_mutex_unlock(g_mutexObjectList);
+    oc_mutex_unlock(g_mutexObjectList);
 
 #ifdef __WITH_TLS__
-    CAcloseSslConnectionAll();
+    CAcloseSslConnectionAll(CA_ADAPTER_TCP);
 #endif
 
+    OIC_LOG(DEBUG, TAG, "OUT - CATCPDisconnectAll");
 }
 
 CATCPSessionInfo_t *CAGetTCPSessionInfoFromEndpoint(const CAEndpoint_t *endpoint, size_t *index)
@@ -1414,7 +1341,7 @@ CASocketFd_t CAGetSocketFDFromEndpoint(const CAEndpoint_t *endpoint)
     OIC_LOG_V(DEBUG, TAG, "Looking for [%s:%d]", endpoint->addr, endpoint->port);
 
     // get connection info from list.
-    ca_mutex_lock(g_mutexObjectList);
+    oc_mutex_lock(g_mutexObjectList);
     uint32_t length = u_arraylist_length(caglobals.tcp.svrlist);
     for (size_t i = 0; i < length; i++)
     {
@@ -1430,20 +1357,20 @@ CASocketFd_t CAGetSocketFDFromEndpoint(const CAEndpoint_t *endpoint)
                 && (svritem->sep.endpoint.port == endpoint->port)
                 && (svritem->sep.endpoint.flags & endpoint->flags))
         {
-            ca_mutex_unlock(g_mutexObjectList);
+            oc_mutex_unlock(g_mutexObjectList);
             OIC_LOG(DEBUG, TAG, "Found in session list");
             return svritem->fd;
         }
     }
 
-    ca_mutex_unlock(g_mutexObjectList);
+    oc_mutex_unlock(g_mutexObjectList);
     OIC_LOG(DEBUG, TAG, "Session not found");
     return OC_INVALID_SOCKET;
 }
 
 CATCPSessionInfo_t *CAGetSessionInfoFromFD(int fd, size_t *index)
 {
-    ca_mutex_lock(g_mutexObjectList);
+    oc_mutex_lock(g_mutexObjectList);
 
     // check from the last item.
     CATCPSessionInfo_t *svritem = NULL;
@@ -1455,19 +1382,19 @@ CATCPSessionInfo_t *CAGetSessionInfoFromFD(int fd, size_t *index)
         if (svritem && svritem->fd == fd)
         {
             *index = i;
-            ca_mutex_unlock(g_mutexObjectList);
+            oc_mutex_unlock(g_mutexObjectList);
             return svritem;
         }
     }
 
-    ca_mutex_unlock(g_mutexObjectList);
+    oc_mutex_unlock(g_mutexObjectList);
 
     return NULL;
 }
 
 CAResult_t CASearchAndDeleteTCPSession(const CAEndpoint_t *endpoint)
 {
-    ca_mutex_lock(g_mutexObjectList);
+    oc_mutex_lock(g_mutexObjectList);
 
     CAResult_t result = CA_STATUS_OK;
     size_t index = 0;
@@ -1481,10 +1408,41 @@ CAResult_t CASearchAndDeleteTCPSession(const CAEndpoint_t *endpoint)
         }
     }
 
-    ca_mutex_unlock(g_mutexObjectList);
+    oc_mutex_unlock(g_mutexObjectList);
     return result;
 }
 
+void CATCPCloseInProgressConnections()
+{
+    OIC_LOG(INFO, TAG, "IN - CATCPCloseInProgressConnections");
+
+    oc_mutex_lock(g_mutexObjectList);
+
+    uint32_t length = u_arraylist_length(caglobals.tcp.svrlist);
+    for (size_t index = 0; index < length; index++)
+    {
+        CATCPSessionInfo_t *svritem = (CATCPSessionInfo_t *) u_arraylist_get(
+                caglobals.tcp.svrlist, index);
+        if (!svritem)
+        {
+            continue;
+        }
+
+        // Session which are connecting state
+        if (svritem->fd >= 0 && svritem->state == CONNECTING)
+        {
+            shutdown(svritem->fd, SHUT_RDWR);
+            close(svritem->fd);
+            svritem->fd = -1;
+            svritem->state = DISCONNECTED;
+        }
+    }
+
+    oc_mutex_unlock(g_mutexObjectList);
+
+    OIC_LOG(INFO, TAG, "OUT - CATCPCloseInProgressConnections");
+}
+
 size_t CAGetTotalLengthFromHeader(const unsigned char *recvBuffer)
 {
     OIC_LOG(DEBUG, TAG, "IN - CAGetTotalLengthFromHeader");
@@ -1507,4 +1465,3 @@ void CATCPSetErrorHandler(CATCPErrorHandleCallback errorHandleCallback)
 {
     g_tcpErrorHandler = errorHandleCallback;
 }
-