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;
(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();
}
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)
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)
{
*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;
}
//[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)",
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)
{
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");
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 %zd)", 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 = %zd", 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;
}
#endif
}
+#ifndef DISABLE_TCP_SERVER
#define NEWSOCKET(FAMILY, NAME) \
caglobals.tcp.NAME.fd = CACreateAcceptSocket(FAMILY, &caglobals.tcp.NAME); \
if (caglobals.tcp.NAME.fd == -1) \
caglobals.tcp.ipv6s.fd, caglobals.tcp.ipv6s.port);
#endif
}
+#endif // DISABLE_TCP_SERVER
CAResult_t CATCPStartServer(const ca_thread_pool_t threadPool)
{
caglobals.tcp.svrlist = u_arraylist_create();
}
+#ifndef DISABLE_TCP_SERVER
if (caglobals.server)
{
CATCPInitializeSocket();
}
+#endif
+
#ifndef __TIZENRT__
// create pipe for fast shutdown
CAInitializePipe(caglobals.tcp.shutdownFds);
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)
{
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);
OIC_LOG(DEBUG, TAG, "data is removed from session list");
+#ifndef DISABLE_TCP_SERVER
if (caglobals.server && MAX_CONNECTION_COUNTS == u_arraylist_length(caglobals.tcp.svrlist) + 1)
{
CATCPInitializeSocket();
}
+#endif
+
return CA_STATUS_OK;
}