X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=resource%2Fcsdk%2Fconnectivity%2Fsrc%2Ftcp_adapter%2Fcatcpadapter.c;h=cd33343bff1c2f49ee5d780298022d7c141a73ff;hb=refs%2Ftags%2Fsubmit%2Ftizen%2F20180201.042114;hp=f75c5c5a6fb9f38356fbb2902d5898123b93c554;hpb=e2137b3741b8e04cc5d482c387b9ff5ea09b0c32;p=platform%2Fupstream%2Fiotivity.git diff --git a/resource/csdk/connectivity/src/tcp_adapter/catcpadapter.c b/resource/csdk/connectivity/src/tcp_adapter/catcpadapter.c index f75c5c5..cd33343 100644 --- a/resource/csdk/connectivity/src/tcp_adapter/catcpadapter.c +++ b/resource/csdk/connectivity/src/tcp_adapter/catcpadapter.c @@ -23,24 +23,30 @@ #include #include +#ifndef __STDC_FORMAT_MACROS #define __STDC_FORMAT_MACROS +#endif #include +#include "cainterface.h" +#include "caipnwmonitor.h" #include "catcpadapter.h" #include "catcpinterface.h" #include "caqueueingthread.h" #include "caadapterutils.h" -#include "camutex.h" +#include "octhread.h" #include "uarraylist.h" #include "caremotehandler.h" #include "logger.h" #include "oic_malloc.h" -#include "oic_string.h" +#ifdef __WITH_TLS__ +#include "ca_adapter_net_ssl.h" +#endif /** * Logging tag for module name. */ -#define TAG "TCP_ADAP" +#define TAG "OIC_CA_TCP_ADAP" /** * Holds internal thread TCP data information. @@ -68,17 +74,30 @@ static CAQueueingThread_t *g_sendQueueHandle = NULL; static CANetworkPacketReceivedCallback g_networkPacketCallback = NULL; /** - * Network Changed Callback to CA. + * Adapter Changed Callback to CA. */ -static CANetworkChangeCallback g_networkChangeCallback = NULL; +static CAAdapterChangeCallback g_networkChangeCallback = NULL; + +/** + * Connection Changed Callback to CA. + */ +static CAConnectionChangeCallback g_connectionChangeCallback = NULL; /** * error Callback to CA adapter. */ static CAErrorHandleCallback g_errorCallback = NULL; -static void CATCPPacketReceivedCB(const CAEndpoint_t *endpoint, - const void *data, uint32_t dataLength); +/** + * KeepAlive Connected or Disconnected Callback to CA adapter. + */ +static CAKeepAliveConnectionCallback g_connKeepAliveCallback = NULL; + +static CAResult_t CATCPPacketReceivedCB(const CASecureEndpoint_t *sep, + const void *data, uint32_t dataLength); + +static void CATCPErrorHandler(const CAEndpoint_t *endpoint, const void *data, + size_t dataLength, CAResult_t result); static CAResult_t CATCPInitializeQueueHandles(); @@ -136,22 +155,94 @@ void CATCPConnectionStateCB(const char *ipAddress, CANetworkStatus_t status) (void)status; } -void CATCPPacketReceivedCB(const CAEndpoint_t *endpoint, const void *data, - uint32_t dataLength) +CAResult_t CATCPPacketReceivedCB(const CASecureEndpoint_t *sep, const void *data, + uint32_t dataLength) { - VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint is NULL"); - VERIFY_NON_NULL_VOID(data, TAG, "data is NULL"); + VERIFY_NON_NULL(sep, TAG, "sep is NULL"); + VERIFY_NON_NULL(data, TAG, "data is NULL"); - OIC_LOG_V(DEBUG, TAG, "Address: %s, port:%d", endpoint->addr, endpoint->port); + OIC_LOG_V(DEBUG, TAG, "Address: %s, port:%d", sep->endpoint.addr, sep->endpoint.port); + CAResult_t res = CA_STATUS_OK; +#ifdef SINGLE_THREAD if (g_networkPacketCallback) { - g_networkPacketCallback(endpoint, data, dataLength); + res = g_networkPacketCallback(sep, data, dataLength); + if (CA_STATUS_OK != res) + { + OIC_LOG(ERROR, TAG, "Error parsing CoAP data"); + } + } +#else + unsigned char *buffer = (unsigned char*)data; + size_t bufferLen = dataLength; + size_t index = 0; + + //get remote device information from file descriptor. + CATCPSessionInfo_t *svritem = CAGetTCPSessionInfoFromEndpoint(&sep->endpoint, &index); + if (!svritem) + { + OIC_LOG(ERROR, TAG, "there is no connection information in list"); + return CA_STATUS_INVALID_PARAM; + } + if (UNKNOWN == svritem->protocol) + { + OIC_LOG(ERROR, TAG, "invalid protocol type"); + return CA_STATUS_INVALID_PARAM; } + + //totalLen filled only when header fully read and parsed + while (0 != bufferLen) + { + res = CAConstructCoAP(svritem, &buffer, &bufferLen); + if (CA_STATUS_OK != res) + { + OIC_LOG_V(ERROR, TAG, "CAConstructCoAP return error : %d", res); + return res; + } + + //when successfully read all required data - pass them to upper layer. + if (svritem->len == svritem->totalLen) + { + if (g_networkPacketCallback) + { + res = g_networkPacketCallback(sep, svritem->data, svritem->totalLen); + if (CA_STATUS_OK != res) + { + OIC_LOG(ERROR, TAG, "Error parsing CoAP data"); + return res; + } + } + CACleanData(svritem); + } + else + { + OIC_LOG_V(DEBUG, TAG, "%u bytes required for complete CoAP", + svritem->totalLen - svritem->len); + } + } +#endif + return res; } -void CATCPErrorHandler(const CAEndpoint_t *endpoint, const void *data, - uint32_t dataLength, CAResult_t result) +#ifdef __WITH_TLS__ +static ssize_t CATCPPacketSendCB(CAEndpoint_t *endpoint, const void *data, size_t dataLength) +{ + OIC_LOG_V(DEBUG, TAG, "In %s", __func__); + VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint is NULL", -1); + VERIFY_NON_NULL_RET(data, TAG, "data is NULL", -1); + + OIC_LOG_V(DEBUG, TAG, "Address: %s, port:%d", endpoint->addr, endpoint->port); + OIC_LOG_BUFFER(DEBUG, TAG, data, dataLength); + + ssize_t ret = CATCPSendData(endpoint, data, dataLength); + OIC_LOG_V(DEBUG, TAG, "Out %s : %d bytes sent", __func__, ret); + return ret; +} +#endif + +static void CATCPErrorHandler(const CAEndpoint_t *endpoint, const void *data, + size_t dataLength, CAResult_t result) { VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint is NULL"); VERIFY_NON_NULL_VOID(data, TAG, "data is NULL"); @@ -162,8 +253,75 @@ void CATCPErrorHandler(const CAEndpoint_t *endpoint, const void *data, } } +static void CATCPConnectionHandler(const CAEndpoint_t *endpoint, bool isConnected, bool isClient) +{ + // Pass the changed connection status to RI Layer for keepalive. + if (g_connKeepAliveCallback) + { + g_connKeepAliveCallback(endpoint, isConnected, isClient); + } + + // Pass the changed connection status to CAUtil. + if (g_connectionChangeCallback) + { + g_connectionChangeCallback(endpoint, isConnected); + } +} + +void CATCPSetKeepAliveCallbacks(CAKeepAliveConnectionCallback ConnHandler) +{ + g_connKeepAliveCallback = ConnHandler; +} + +void CATCPAdapterHandler(CATransportAdapter_t adapter, CANetworkStatus_t status) +{ + if (g_networkChangeCallback) + { + g_networkChangeCallback(adapter, status); + } + + if (CA_INTERFACE_DOWN == status) + { + OIC_LOG(INFO, TAG, "Network status is down, close all session"); + + CAResult_t res = CAQueueingThreadClearData(g_sendQueueHandle); + if (res != CA_STATUS_OK) + { + OIC_LOG_V(ERROR, TAG, "CAQueueingThreadClearData failed[%d]", res); + } + + CATCPStopServer(); + } + else if (CA_INTERFACE_UP == status) + { + OIC_LOG(INFO, TAG, "Network status is up, create new socket for listening"); + + CAResult_t ret = CA_STATUS_FAILED; +#ifndef SINGLE_THREAD + ret = CATCPStartServer((const ca_thread_pool_t)caglobals.tcp.threadpool); +#else + ret = CATCPStartServer(); +#endif + if (CA_STATUS_OK != ret) + { + OIC_LOG_V(ERROR, TAG, "CATCPStartServer failed[%d]", ret); + } + } +} + static void CAInitializeTCPGlobals() { + caglobals.tcp.ipv4.fd = -1; + caglobals.tcp.ipv4s.fd = -1; + caglobals.tcp.ipv6.fd = -1; + caglobals.tcp.ipv6s.fd = -1; + + // Set the port number received from application. + caglobals.tcp.ipv4.port = caglobals.ports.tcp.u4; + caglobals.tcp.ipv4s.port = caglobals.ports.tcp.u4s; + caglobals.tcp.ipv6.port = caglobals.ports.tcp.u6; + caglobals.tcp.ipv6s.port = caglobals.ports.tcp.u6s; + caglobals.tcp.selectTimeout = CA_TCP_SELECT_TIMEOUT; caglobals.tcp.listenBacklog = CA_TCP_LISTEN_BACKLOG; caglobals.tcp.svrlist = NULL; @@ -179,30 +337,56 @@ static void CAInitializeTCPGlobals() } caglobals.tcp.ipv4tcpenabled = flags & CA_IPV4; + caglobals.tcp.ipv6tcpenabled = flags & CA_IPV6; } CAResult_t CAInitializeTCP(CARegisterConnectivityCallback registerCallback, CANetworkPacketReceivedCallback networkPacketCallback, - CANetworkChangeCallback netCallback, + CAAdapterChangeCallback netCallback, + CAConnectionChangeCallback connCallback, CAErrorHandleCallback errorCallback, ca_thread_pool_t handle) { OIC_LOG(DEBUG, TAG, "IN"); VERIFY_NON_NULL(registerCallback, TAG, "registerCallback"); VERIFY_NON_NULL(networkPacketCallback, TAG, "networkPacketCallback"); VERIFY_NON_NULL(netCallback, TAG, "netCallback"); +#ifndef SINGLE_THREAD VERIFY_NON_NULL(handle, TAG, "thread pool handle"); +#endif g_networkChangeCallback = netCallback; + g_connectionChangeCallback = connCallback; g_networkPacketCallback = networkPacketCallback; g_errorCallback = errorCallback; CAInitializeTCPGlobals(); + + 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"); + CATCPDestroyMutex(); + CATCPDestroyCond(); + return res; + } + +#ifndef SINGLE_THREAD caglobals.tcp.threadpool = handle; +#endif + CATCPSetConnectionChangedCallback(CATCPConnectionHandler); CATCPSetPacketReceiveCallback(CATCPPacketReceivedCB); CATCPSetErrorHandler(CATCPErrorHandler); - CAConnectivityHandler_t TCPHandler = { +#ifdef __WITH_TLS__ + CAsetSslAdapterCallbacks(CATCPPacketReceivedCB, CATCPPacketSendCB, CA_ADAPTER_TCP); +#endif + + CAConnectivityHandler_t tcpHandler = { .startAdapter = CAStartTCP, .startListenServer = CAStartTCPListeningServer, .stopListenServer = CAStopTCPListeningServer, @@ -212,8 +396,10 @@ CAResult_t CAInitializeTCP(CARegisterConnectivityCallback registerCallback, .GetnetInfo = CAGetTCPInterfaceInformation, .readData = CAReadTCPData, .stopAdapter = CAStopTCP, - .terminate = CATerminateTCP }; - registerCallback(TCPHandler, CA_ADAPTER_TCP); + .terminate = CATerminateTCP, + .cType = CA_ADAPTER_TCP}; + + registerCallback(tcpHandler); OIC_LOG(INFO, TAG, "OUT IntializeTCP is Success"); return CA_STATUS_OK; @@ -221,6 +407,9 @@ CAResult_t CAInitializeTCP(CARegisterConnectivityCallback registerCallback, CAResult_t CAStartTCP() { + OIC_LOG(DEBUG, TAG, "IN"); + +#ifndef SINGLE_THREAD if (CA_STATUS_OK != CATCPInitializeQueueHandles()) { OIC_LOG(ERROR, TAG, "Failed to Initialize Queue Handle"); @@ -229,24 +418,96 @@ CAResult_t CAStartTCP() } // Start send queue thread +#ifndef __TIZENRT__ if (CA_STATUS_OK != CAQueueingThreadStart(g_sendQueueHandle)) +#else + if (CA_STATUS_OK != CAQueueingThreadStart(g_sendQueueHandle, "IoT_TCPSendQueue")) +#endif { OIC_LOG(ERROR, TAG, "Failed to Start Send Data Thread"); return CA_STATUS_FAILED; } - - CAResult_t ret = CATCPStartServer((const ca_thread_pool_t)caglobals.tcp.threadpool); +#else + CAResult_t ret = CATCPStartServer(); if (CA_STATUS_OK != ret) { - OIC_LOG_V(ERROR, TAG, "Failed to start server![%d]", ret); + OIC_LOG_V(DEBUG, TAG, "CATCPStartServer failed[%d]", ret); return ret; } +#endif + + // Start network monitoring to receive adapter status changes. + CAIPStartNetworkMonitor(CATCPAdapterHandler, CA_ADAPTER_TCP); return CA_STATUS_OK; } +static bool CAClearQueueEndpointDataContext(void *data, uint32_t size, void *ctx) +{ + (void)size; + + if (NULL == data || NULL == ctx) + { + return false; + } + + CATCPData *tcpData = (CATCPData *)data; + CAEndpoint_t *endpoint = (CAEndpoint_t *)ctx; + + if (NULL != tcpData && NULL != tcpData->remoteEndpoint) + { + if (strcmp(tcpData->remoteEndpoint->addr, endpoint->addr) == 0 + && tcpData->remoteEndpoint->port == endpoint->port) + { + return true; + } + } + return false; +} + +CAResult_t CATCPDisconnectSession(const CAEndpoint_t *endpoint) +{ + CAResult_t res = CAQueueingThreadClearContextData(g_sendQueueHandle, + CAClearQueueEndpointDataContext, + endpoint); + if (CA_STATUS_OK != res) + { + OIC_LOG(ERROR, TAG, "failed to clear context data"); + } + +#ifdef __WITH_TLS__ + res = CAcloseSslConnection(endpoint); + if (CA_STATUS_OK != res) + { + OIC_LOG(ERROR, TAG, "failed to close TLS session"); + } +#endif + + res = CASearchAndDeleteTCPSession(endpoint); + if (CA_STATUS_OK != res) + { + OIC_LOG(ERROR, TAG, "failed to close TCP session"); + } + + return res; +} + CAResult_t CAStartTCPListeningServer() { +#ifndef SINGLE_THREAD + if (!caglobals.server) + { + caglobals.server = true; // only needed to run CA tests + } + + CAResult_t ret = CATCPStartServer((const ca_thread_pool_t)caglobals.tcp.threadpool); + if (CA_STATUS_OK != ret) + { + OIC_LOG_V(ERROR, TAG, "Failed to start listening server![%d]", ret); + return ret; + } +#endif + return CA_STATUS_OK; } @@ -257,6 +518,18 @@ CAResult_t CAStopTCPListeningServer() CAResult_t CAStartTCPDiscoveryServer() { + if (!caglobals.client) + { + caglobals.client = true; // only needed to run CA tests + } + + CAResult_t ret = CATCPStartServer((const ca_thread_pool_t)caglobals.tcp.threadpool); + if (CA_STATUS_OK != ret) + { + OIC_LOG_V(ERROR, TAG, "Failed to start discovery server![%d]", ret); + return ret; + } + return CA_STATUS_OK; } @@ -288,32 +561,59 @@ static size_t CAQueueTCPData(bool isMulticast, const CAEndpoint_t *endpoint, } int32_t CASendTCPUnicastData(const CAEndpoint_t *endpoint, - const void *data, uint32_t dataLength) + const void *data, uint32_t dataLength, + CADataType_t dataType) { + OIC_LOG(DEBUG, TAG, "IN"); + (void)dataType; +#ifndef SINGLE_THREAD return CAQueueTCPData(false, endpoint, data, dataLength); +#else + return CATCPSendData(endpoint, data, dataLength); +#endif } int32_t CASendTCPMulticastData(const CAEndpoint_t *endpoint, - const void *data, uint32_t dataLength) + const void *data, uint32_t dataLength, + CADataType_t dataType) { + (void)dataType; return CAQueueTCPData(true, endpoint, data, dataLength); } CAResult_t CAReadTCPData() { + OIC_LOG(DEBUG, TAG, "IN"); +#ifdef SINGLE_THREAD + CATCPPullData(); +#endif return CA_STATUS_OK; } 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(); + +#ifndef SINGLE_THREAD + // Stop send queue thread. if (g_sendQueueHandle && g_sendQueueHandle->threadMutex) { CAQueueingThreadStop(g_sendQueueHandle); } - CATCPDeinitializeQueueHandles(); +#endif + + // Close TCP servers and established connections. CATCPStopServer(); - //Re-initializing the Globals to start them again + + // Re-initializing the Globals to start them again. CAInitializeTCPGlobals(); return CA_STATUS_OK; @@ -323,7 +623,8 @@ void CATerminateTCP() { CATCPSetPacketReceiveCallback(NULL); - CATCPDeinitializeQueueHandles(); + CATCPDestroyMutex(); + CATCPDestroyCond(); } void CATCPSendDataThread(void *threadData) @@ -335,6 +636,14 @@ void CATCPSendDataThread(void *threadData) return; } + if (caglobals.tcp.terminate) + { + OIC_LOG(DEBUG, TAG, "Adapter is not enabled"); + CATCPErrorHandler(tcpData->remoteEndpoint, tcpData->data, tcpData->dataLen, + CA_SEND_FAILED); + return; + } + if (tcpData->isMulticast) { //Processing for sending multicast @@ -343,8 +652,34 @@ void CATCPSendDataThread(void *threadData) } else { +#ifdef __WITH_TLS__ + if (tcpData->remoteEndpoint && tcpData->remoteEndpoint->flags & CA_SECURE) + { + CAResult_t result = CA_STATUS_OK; + OIC_LOG(DEBUG, TAG, "CAencryptSsl called!"); + result = CAencryptSsl(tcpData->remoteEndpoint, tcpData->data, tcpData->dataLen); + + if (CA_STATUS_OK != result) + { + OIC_LOG(ERROR, TAG, "CAAdapterNetDtlsEncrypt failed!"); + CASearchAndDeleteTCPSession(tcpData->remoteEndpoint); + CATCPErrorHandler(tcpData->remoteEndpoint, tcpData->data, tcpData->dataLen, + CA_SEND_FAILED); + } + OIC_LOG_V(DEBUG, TAG, + "CAAdapterNetDtlsEncrypt returned with result[%d]", result); + return; + } +#endif //Processing for sending unicast - CATCPSendData(tcpData->remoteEndpoint, tcpData->data, tcpData->dataLen, false); + ssize_t dlen = CATCPSendData(tcpData->remoteEndpoint, tcpData->data, tcpData->dataLen); + if (-1 == dlen) + { + OIC_LOG(ERROR, TAG, "CATCPSendData failed"); + CASearchAndDeleteTCPSession(tcpData->remoteEndpoint); + CATCPErrorHandler(tcpData->remoteEndpoint, tcpData->data, tcpData->dataLen, + CA_SEND_FAILED); + } } } @@ -391,9 +726,63 @@ void CADataDestroyer(void *data, uint32_t size) { if (size < sizeof(CATCPData)) { +#ifndef __TIZENRT__ OIC_LOG_V(ERROR, TAG, "Destroy data too small %p %" PRIu32, data, size); +#endif } CATCPData *TCPData = (CATCPData *) data; CAFreeTCPData(TCPData); } + +#ifdef SINGLE_THREAD +size_t CAGetTotalLengthFromPacketHeader(const unsigned char *recvBuffer, size_t size) +{ + OIC_LOG(DEBUG, TAG, "IN - CAGetTotalLengthFromHeader"); + + if (NULL == recvBuffer || !size) + { + OIC_LOG(ERROR, TAG, "recvBuffer is NULL"); + return 0; + } + + coap_transport_t transport = coap_get_tcp_header_type_from_initbyte( + ((unsigned char *)recvBuffer)[0] >> 4); + size_t optPaylaodLen = coap_get_length_from_header((unsigned char *)recvBuffer, + transport); + size_t headerLen = coap_get_tcp_header_length((unsigned char *)recvBuffer); + + OIC_LOG_V(DEBUG, TAG, "option/paylaod length [%d]", optPaylaodLen); + OIC_LOG_V(DEBUG, TAG, "header length [%d]", headerLen); + OIC_LOG_V(DEBUG, TAG, "total data length [%d]", headerLen + optPaylaodLen); + + OIC_LOG(DEBUG, TAG, "OUT - CAGetTotalLengthFromHeader"); + return headerLen + optPaylaodLen; +} + +void CAGetTCPHeaderDetails(unsigned char* recvBuffer, coap_transport_t *transport, + size_t *headerlen) +{ + if (NULL == recvBuffer) + { + OIC_LOG(ERROR, TAG, "recvBuffer is NULL"); + return; + } + + if (NULL == transport) + { + OIC_LOG(ERROR, TAG, "transport is NULL"); + return; + } + + if (NULL == headerlen) + { + OIC_LOG(ERROR, TAG, "headerlen is NULL"); + return; + } + + *transport = coap_get_tcp_header_type_from_initbyte( + ((unsigned char *)recvBuffer)[0] >> 4); + *headerlen = coap_get_tcp_header_length_for_transport(*transport); +} +#endif