Fix the build error for tizen 5.5's dlog format
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / tcp_adapter / catcpserver.c
index a1572c0..748d9a8 100644 (file)
@@ -129,6 +129,26 @@ static CAResult_t CATCPCreateSocket(int family, CATCPSessionInfo_t *svritem);
 static void CATCPInitializeSocket();
 static CATCPSessionInfo_t *CAGetSessionInfoFromFDAsOwner(int fd, size_t *index);
 
+#if defined(__TIZEN__)
+static char g_cloudproxyUri[CA_MAX_URI_LENGTH];
+
+CAResult_t CASetCloudAddressForProxy(const char *uri)
+{
+    if (uri == NULL)
+        memset(g_cloudproxyUri, '\0', sizeof (g_cloudproxyUri));
+    else
+        OICStrcpy(g_cloudproxyUri, sizeof (g_cloudproxyUri), uri);
+    return CA_STATUS_OK;
+}
+
+const char *CAGetCloudAddressForProxy()
+{
+    if (g_cloudproxyUri[0] == '\0')
+        return NULL;
+    return g_cloudproxyUri;
+}
+#endif
+
 #define CHECKFD(FD) \
     if (FD > caglobals.tcp.maxfd) \
         caglobals.tcp.maxfd = FD;
@@ -245,8 +265,15 @@ static void CAReceiveHandler(void *data)
     (void)data;
     OIC_LOG(DEBUG, TAG, "IN - CAReceiveHandler");
 
-    while (!caglobals.tcp.terminate)
+    while (true)
     {
+        oc_mutex_lock(g_mutexObjectList);
+        if (caglobals.tcp.terminate)
+        {
+            oc_mutex_unlock(g_mutexObjectList);
+            break;
+        }
+        oc_mutex_unlock(g_mutexObjectList);
         CAFindReadyMessage();
     }
 
@@ -291,11 +318,14 @@ static void CAFindReadyMessage()
 
     int ret = select(caglobals.tcp.maxfd + 1, &readFds, NULL, NULL, &timeout);
 
+    oc_mutex_lock(g_mutexObjectList);
     if (caglobals.tcp.terminate)
     {
+        oc_mutex_unlock(g_mutexObjectList);
         OIC_LOG_V(INFO, TAG, "Packet receiver Stop request received.");
         return;
     }
+    oc_mutex_unlock(g_mutexObjectList);
     if (0 >= ret)
     {
         if (0 > ret)
@@ -502,7 +532,7 @@ CAResult_t CAConstructCoAP(CATCPSessionInfo_t *svritem, unsigned char **data,
 
     unsigned char *inBuffer = *data;
     size_t inLen = *dataLength;
-    OIC_LOG_V(DEBUG, TAG, "before-datalength : %u", *dataLength);
+    OIC_LOG_V(DEBUG, TAG, "before-datalength : %zd", *dataLength);
 
     if (NULL == svritem->data && inLen > 0)
     {
@@ -606,7 +636,7 @@ CAResult_t CAConstructCoAP(CATCPSessionInfo_t *svritem, unsigned char **data,
     *data = inBuffer;
     *dataLength = inLen;
 
-    OIC_LOG_V(DEBUG, TAG, "after-datalength : %u", *dataLength);
+    OIC_LOG_V(DEBUG, TAG, "after-datalength : %zd", *dataLength);
     OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
     return CA_STATUS_OK;
 }
@@ -646,7 +676,7 @@ 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, "total tls length = %u", tlsLength);
+            OIC_LOG_V(DEBUG, TAG, "total tls length = %zd", tlsLength);
             if (tlsLength > TLS_DATA_MAX_SIZE)
             {
                 OIC_LOG_V(ERROR, TAG, "total tls length is too big (buffer size : %u)",
@@ -673,7 +703,7 @@ static void CAReceiveMessage(int fd)
         else
         {
             svritem->tlsLen += len;
-            OIC_LOG_V(DEBUG, TAG, "nb_read : %u bytes , recv() : %d bytes, svritem->tlsLen : %u bytes",
+            OIC_LOG_V(DEBUG, TAG, "nb_read : %zd bytes , recv() : %d bytes, svritem->tlsLen : %zd bytes",
                                 nbRead, len, svritem->tlsLen);
             if (tlsLength > 0 && tlsLength == svritem->tlsLen)
             {
@@ -818,6 +848,21 @@ static CAResult_t CATCPConvertNameToAddr(int family, const char *host, uint16_t
     return CA_STATUS_OK;
 }
 
+#if defined(__TIZEN__)
+static int CAGetHTTPStatusCode(char * response) {
+    char *resp, *code_plus, *ptrSave;
+    int ret = -1;
+
+    resp = strdup(response);
+    strtok_r(resp, " ", &ptrSave);  /* skip HTTP version */
+    code_plus = strtok_r(NULL, " ", &ptrSave);
+
+    ret = code_plus ? atoi(code_plus) : -1;
+    free(resp);
+    return ret;
+}
+#endif
+
 static CAResult_t CATCPCreateSocket(int family, CATCPSessionInfo_t *svritem)
 {
     VERIFY_NON_NULL(svritem, TAG, "svritem is NULL");
@@ -878,6 +923,54 @@ static CAResult_t CATCPCreateSocket(int family, CATCPSessionInfo_t *svritem)
         OIC_LOG(ERROR, TAG, "wakeup receive thread failed");
         return CA_SOCKET_OPERATION_FAILED;
     }
+
+#if defined(__TIZEN__)
+    // #5. Send HTTP CONNECT to proxy if proxy
+
+    const char *cloud_address = CAGetCloudAddressForProxy();
+    OIC_LOG_V(INFO, TAG, "Proxy : '%s'", cloud_address ? cloud_address : "(nil)");
+
+    if(cloud_address && *cloud_address)
+    {
+        char message[4096];
+        int len = sprintf(message,
+                "CONNECT %s HTTP/1.1\r\n"
+                "Host: %s\r\n\r\n", cloud_address, cloud_address
+        );
+
+        ssize_t l = send(fd, message, len, 0);
+        if(l != len)
+       {
+            OIC_LOG_V(ERROR, TAG, "failed to send HTTP CONNECT data (expected %d bytes, ret %d)", len, l);
+            close(fd);
+            svritem->fd = -1;
+            return CA_SOCKET_OPERATION_FAILED;
+        }
+
+        // maybe this should be called in other thread, it causes bottleneck.
+        OIC_LOG_V(INFO, TAG, "Message sent is : '%s'\n", message);
+
+        *message = '\0';
+        OIC_LOG_V(INFO, TAG, "Receiving response to CONNECT from proxy...");
+
+        l = recv(fd, message, 4096, 0);
+
+        OIC_LOG_V(INFO, TAG, "Received data : '%s'", message);
+        OIC_LOG_V(INFO, TAG, "Received len = %d", l);
+
+        int status_code = CAGetHTTPStatusCode(message);
+
+        OIC_LOG_V(INFO, TAG, "HTTP status_code : %d", status_code);
+        if(status_code < 200 || status_code > 299)
+       {
+            OIC_LOG_V(ERROR, TAG, "Error, Wrong status code: %d", status_code);
+            close(fd);
+            svritem->fd = -1;
+            return CA_SOCKET_OPERATION_FAILED;
+        }
+    }
+#endif
+
     return CA_STATUS_OK;
 }
 
@@ -1160,10 +1253,16 @@ void CATCPStopServer()
     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;
+    if (caglobals.tcp.connectionFds[1] != OC_INVALID_SOCKET)
+    {
+       close(caglobals.tcp.connectionFds[1]);
+       caglobals.tcp.connectionFds[1] = OC_INVALID_SOCKET;
+    }
+    if (caglobals.tcp.connectionFds[0] != OC_INVALID_SOCKET)
+    {
+       close(caglobals.tcp.connectionFds[0]);
+       caglobals.tcp.connectionFds[0] = OC_INVALID_SOCKET;
+    }
 #ifndef __TIZENRT__
     if (caglobals.tcp.shutdownFds[1] != OC_INVALID_SOCKET)
     {
@@ -1288,10 +1387,13 @@ static ssize_t sendData(const CAEndpoint_t *endpoint, const void *data,
             oc_cond_wait_for(g_condSend, g_mutexSend, waitTime);
             oc_mutex_unlock(g_mutexSend);
 
+            oc_mutex_lock(g_mutexObjectList);
             if (caglobals.tcp.terminate)
             {
+                oc_mutex_unlock(g_mutexObjectList);
                 return len;
             }
+            oc_mutex_unlock(g_mutexObjectList);
 
             sendRetryTime = (sendRetryTime << 1);