From 6c0321f52c8ed9d1ba9c10f178fdad9db62c465a Mon Sep 17 00:00:00 2001 From: Sakthivel Samidurai Date: Thu, 13 Aug 2015 15:07:37 -0700 Subject: [PATCH] Move security identity out of CAEndpoint_t and OCDevAddr. Move security identity to CAInfo_t. Propagate security to OCClientResponse for application. Change-Id: I535577bedf0ed4f46a97249add56aec6928d2d84 Signed-off-by: John Light Signed-off-by: Sakthivel Samidurai Reviewed-on: https://gerrit.iotivity.org/gerrit/2144 Tested-by: jenkins-iotivity Reviewed-by: Jon A. Cruz --- resource/csdk/connectivity/api/cacommon.h | 37 ++++-- .../csdk/connectivity/inc/caadapterinterface.h | 2 +- resource/csdk/connectivity/inc/caadapternetdtls.h | 4 +- resource/csdk/connectivity/inc/caipinterface.h | 4 +- .../src/adapter_util/caadapternetdtls.c | 63 +++++---- .../csdk/connectivity/src/cainterfacecontroller.c | 6 +- resource/csdk/connectivity/src/camessagehandler.c | 143 +++++++++++---------- .../src/ip_adapter/arduino/caipserver_eth.cpp | 10 +- .../src/ip_adapter/arduino/caipserver_wifi.cpp | 11 +- .../csdk/connectivity/src/ip_adapter/caipadapter.c | 11 +- .../csdk/connectivity/src/ip_adapter/caipserver.c | 20 +-- resource/csdk/security/src/secureresourcemanager.c | 2 +- resource/csdk/stack/include/octypes.h | 11 +- resource/csdk/stack/src/ocstack.c | 17 ++- 14 files changed, 186 insertions(+), 155 deletions(-) diff --git a/resource/csdk/connectivity/api/cacommon.h b/resource/csdk/connectivity/api/cacommon.h index 9f72889..cb58110 100644 --- a/resource/csdk/connectivity/api/cacommon.h +++ b/resource/csdk/connectivity/api/cacommon.h @@ -236,13 +236,21 @@ typedef struct { CATransportAdapter_t adapter; // adapter type CATransportFlags_t flags; // transport modifiers + uint16_t port; // for IP char addr[MAX_ADDR_STR_SIZE_CA]; // address for all uint32_t interface; // usually zero for default interface - uint16_t port; // for IP - CARemoteId_t identity; // endpoint identity } CAEndpoint_t; /** + * Endpoint information for secure messages + */ +typedef struct +{ + CAEndpoint_t endpoint; /**< endpoint */ + CARemoteId_t identity; /**< endpoint identity */ +} CASecureEndpoint_t; + +/** * @enum CAResult_t * Enums for CA return values */ @@ -347,6 +355,7 @@ typedef struct CAPayload_t payload; /**< payload of the request */ size_t payloadSize; /**< size in bytes of the payload */ CAURI_t resourceUri; /**< Resource URI information **/ + CARemoteId_t identity; /**< endpoint identity */ } CAInfo_t; /** @@ -406,8 +415,8 @@ typedef struct */ typedef struct { - int fd; - uint16_t port; + int fd; /**< socket fd */ + uint16_t port; /**< socket port */ } CASocket_t; #define HISTORYSIZE (4) @@ -426,10 +435,15 @@ typedef struct typedef struct { - CATransportFlags_t clientFlags; - CATransportFlags_t serverFlags; - bool client; - bool server; + int ifindex; /**< network interface index */ +} CAIfItem_t; + +typedef struct +{ + CATransportFlags_t clientFlags; /**< flag for client */ + CATransportFlags_t serverFlags; /**< flag for server */ + bool client; /**< client mode */ + bool server; /**< server mode */ struct sockets { @@ -446,12 +460,17 @@ typedef struct int shutdownFds[2]; /**< shutdown pipe */ int selectTimeout; /**< in seconds */ int maxfd; /**< highest fd (for select) */ - int numInterfaces; /**< number of active interfaces */ bool started; /**< the IP adapter has started */ bool terminate; /**< the IP adapter needs to stop */ bool ipv6enabled; /**< IPv6 enabled by OCInit flags */ bool ipv4enabled; /**< IPv4 enabled by OCInit flags */ bool dualstack; /**< IPv6 and IPv4 enabled */ + + struct networkmonitors + { + CAIfItem_t *ifitems; /**< current network interface index list */ + int numifitems; /**< number of current network interfaces */ + } nm; } ip; struct calayer diff --git a/resource/csdk/connectivity/inc/caadapterinterface.h b/resource/csdk/connectivity/inc/caadapterinterface.h index f220870..a750291 100644 --- a/resource/csdk/connectivity/inc/caadapterinterface.h +++ b/resource/csdk/connectivity/inc/caadapterinterface.h @@ -168,7 +168,7 @@ typedef void (*CARegisterConnectivityCallback)(CAConnectivityHandler_t handler, * This will be used during the receive of network requests and response. * @see SendUnicastData(), SendMulticastData() */ -typedef void (*CANetworkPacketReceivedCallback)(const CAEndpoint_t *endPoint, +typedef void (*CANetworkPacketReceivedCallback)(const CASecureEndpoint_t *sep, const void *data, uint32_t dataLen); /** diff --git a/resource/csdk/connectivity/inc/caadapternetdtls.h b/resource/csdk/connectivity/inc/caadapternetdtls.h index dc19b9f..e6940d8 100644 --- a/resource/csdk/connectivity/inc/caadapternetdtls.h +++ b/resource/csdk/connectivity/inc/caadapternetdtls.h @@ -38,7 +38,7 @@ */ extern void OCGetDtlsPskCredentials(CADtlsPskCredsBlob_t **credInfo); -typedef void (*CAPacketReceivedCallback)(const CAEndpoint_t *endpoint, +typedef void (*CAPacketReceivedCallback)(const CASecureEndpoint_t *sep, const void *data, uint32_t dataLength); typedef void (*CAPacketSendCallback)(CAEndpoint_t *endpoint, @@ -249,7 +249,7 @@ CAResult_t CAAdapterNetDtlsEncrypt(const CAEndpoint_t *endpoint, * @retval ::CA_STATUS_FAILED Operation failed. * */ -CAResult_t CAAdapterNetDtlsDecrypt(const CAEndpoint_t *endpoint, +CAResult_t CAAdapterNetDtlsDecrypt(const CASecureEndpoint_t *sep, uint8_t *data, uint32_t dataLen); diff --git a/resource/csdk/connectivity/inc/caipinterface.h b/resource/csdk/connectivity/inc/caipinterface.h index 6a71f28..e5e862a 100644 --- a/resource/csdk/connectivity/inc/caipinterface.h +++ b/resource/csdk/connectivity/inc/caipinterface.h @@ -50,12 +50,12 @@ typedef enum /** * Callback to be notified on reception of any data from remote OIC devices. * - * @param[in] endpoint network endpoint description. + * @param[in] sep network endpoint description. * @param[in] data Data received from remote OIC device. * @param[in] dataLength Length of data in bytes. * @pre Callback must be registered using CAIPSetPacketReceiveCallback(). */ -typedef void (*CAIPPacketReceivedCallback)(const CAEndpoint_t *endpoint, +typedef void (*CAIPPacketReceivedCallback)(const CASecureEndpoint_t *sep, const void *data, uint32_t dataLength); diff --git a/resource/csdk/connectivity/src/adapter_util/caadapternetdtls.c b/resource/csdk/connectivity/src/adapter_util/caadapternetdtls.c index 97a72bf..b4ae019 100644 --- a/resource/csdk/connectivity/src/adapter_util/caadapternetdtls.c +++ b/resource/csdk/connectivity/src/adapter_util/caadapternetdtls.c @@ -50,7 +50,7 @@ static ca_mutex g_dtlsContextMutex = NULL; */ static CAGetDTLSCredentialsHandler g_getCredentialsCallback = NULL; -static CAEndpoint_t *GetPeerInfo(const CAEndpoint_t *peer) +static CASecureEndpoint_t *GetPeerInfo(const CAEndpoint_t *peer) { uint32_t list_index = 0; uint32_t list_length = 0; @@ -61,18 +61,18 @@ static CAEndpoint_t *GetPeerInfo(const CAEndpoint_t *peer) return NULL; } - CAEndpoint_t *peerInfo; + CASecureEndpoint_t *peerInfo = NULL; list_length = u_arraylist_length(g_caDtlsContext->peerInfoList); for (list_index = 0; list_index < list_length; list_index++) { - peerInfo = (CAEndpoint_t *)u_arraylist_get(g_caDtlsContext->peerInfoList, list_index); + peerInfo = (CASecureEndpoint_t *)u_arraylist_get(g_caDtlsContext->peerInfoList, list_index); if (NULL == peerInfo) { continue; } - if((0 == strncmp(peer->addr, peerInfo->addr, MAX_ADDR_STR_SIZE_CA)) && - (peer->port == peerInfo->port)) + if((0 == strncmp(peer->addr, peerInfo->endpoint.addr, MAX_ADDR_STR_SIZE_CA)) && + (peer->port == peerInfo->endpoint.port)) { return peerInfo; } @@ -80,8 +80,8 @@ static CAEndpoint_t *GetPeerInfo(const CAEndpoint_t *peer) return NULL; } -static CAResult_t CAAddIdToPeerInfoList(const char *peerAddr, uint16_t port, - const unsigned char *id, uint16_t id_length, CATransportFlags_t flag) +static CAResult_t CAAddIdToPeerInfoList(const char *peerAddr, uint32_t port, + const unsigned char *id, uint16_t id_length) { if(NULL == peerAddr || NULL == id @@ -93,23 +93,20 @@ static CAResult_t CAAddIdToPeerInfoList(const char *peerAddr, uint16_t port, return CA_STATUS_INVALID_PARAM; } - CAEndpoint_t *peer = (CAEndpoint_t *)OICCalloc(1, sizeof (CAEndpoint_t)); + CASecureEndpoint_t *peer = (CASecureEndpoint_t *)OICCalloc(1, sizeof (CASecureEndpoint_t)); if (NULL == peer) { OIC_LOG(ERROR, NET_DTLS_TAG, "peerInfo malloc failed!"); return CA_MEMORY_ALLOC_FAILED; } - OICStrcpy(peer->addr, sizeof(peer->addr), peerAddr); - peer->port = port; - peer->flags = flag; - peer->flags |= CA_SECURE; - peer->adapter = CA_ADAPTER_IP; + OICStrcpy(peer->endpoint.addr, sizeof(peer->endpoint.addr), peerAddr); + peer->endpoint.port = port; memcpy(peer->identity.id, id, id_length); peer->identity.id_length = id_length; - if(NULL != GetPeerInfo(peer)) + if (NULL != GetPeerInfo(&peer->endpoint)) { OIC_LOG(ERROR, NET_DTLS_TAG, "CAAddIdToPeerInfoList peer already exist"); OICFree(peer); @@ -385,12 +382,13 @@ static int32_t CAReadDecryptedPayload(dtls_context_t *context, stCADtlsAddrInfo_t *addrInfo = (stCADtlsAddrInfo_t *)session; - CAEndpoint_t endpoint = { 0 }; - CAConvertAddrToName(&(addrInfo->addr.st), endpoint.addr, &endpoint.port); - endpoint.flags = addrInfo->addr.st.ss_family == AF_INET ? CA_IPV4 : CA_IPV6; - endpoint.flags |= CA_SECURE; - endpoint.adapter = CA_ADAPTER_IP; - int type = 0; + CASecureEndpoint_t sep = + { .endpoint = + { .adapter = CA_ADAPTER_IP, .flags = + ((addrInfo->addr.st.ss_family == AF_INET) ? CA_IPV4 : CA_IPV6) | CA_SECURE, .port = 0 }, + .identity = + { 0 } }; + CAConvertAddrToName(&(addrInfo->addr.st), sep.endpoint.addr, &sep.endpoint.port); if (NULL == g_caDtlsContext) { @@ -398,17 +396,18 @@ static int32_t CAReadDecryptedPayload(dtls_context_t *context, return 0; } + int type = 0; if ((0 <= type) && (MAX_SUPPORTED_ADAPTERS > type) && (NULL != g_caDtlsContext->adapterCallbacks[type].recvCallback)) { // Get identity of the source of packet - CAEndpoint_t *peerInfo = GetPeerInfo(&endpoint); + CASecureEndpoint_t *peerInfo = GetPeerInfo(&sep.endpoint); if (peerInfo) { - endpoint.identity = peerInfo->identity; + sep.identity = peerInfo->identity; } - g_caDtlsContext->adapterCallbacks[type].recvCallback(&endpoint, buf, bufLen); + g_caDtlsContext->adapterCallbacks[type].recvCallback(&sep, buf, bufLen); } else { @@ -483,7 +482,7 @@ static int32_t CAHandleSecureEvent(dtls_context_t *context, OIC_LOG(INFO, NET_DTLS_TAG, "Peer closing connection"); stCADtlsAddrInfo_t *addrInfo = (stCADtlsAddrInfo_t *)session; - char peerAddr[MAX_ADDR_STR_SIZE_CA] = {0}; + char peerAddr[MAX_ADDR_STR_SIZE_CA] = { 0 }; uint16_t port = 0; CAConvertAddrToName(&(addrInfo->addr.st), peerAddr, &port); CARemovePeerFromPeerInfoList(peerAddr, port); @@ -550,15 +549,11 @@ static int32_t CAGetPskCredentials(dtls_context_t *ctx, // data structure when handshake completes. Therefore, currently this is a // workaround to cache remote end-point identity when tinyDTLS asks for PSK. stCADtlsAddrInfo_t *addrInfo = (stCADtlsAddrInfo_t *)session; - char peerAddress[MAX_ADDR_STR_SIZE_CA] = {0}; + char peerAddr[MAX_ADDR_STR_SIZE_CA] = { 0 }; uint16_t port = 0; - CAConvertAddrToName(&(addrInfo->addr.st), peerAddress, &port); + CAConvertAddrToName(&(addrInfo->addr.st), peerAddr, &port); - CATransportFlags_t flag = - addrInfo->addr.st.ss_family == AF_INET ? CA_IPV4 : CA_IPV6; - - CAResult_t result = CAAddIdToPeerInfoList(peerAddress, - port, desc, descLen, flag); + CAResult_t result = CAAddIdToPeerInfoList(peerAddr, port, desc, descLen); if(CA_STATUS_OK != result ) { OIC_LOG(ERROR, NET_DTLS_TAG, "Fail to add peer id to gDtlsPeerInfoList"); @@ -954,15 +949,15 @@ CAResult_t CAAdapterNetDtlsEncrypt(const CAEndpoint_t *endpoint, return CA_STATUS_OK; } -CAResult_t CAAdapterNetDtlsDecrypt(const CAEndpoint_t *endpoint, +CAResult_t CAAdapterNetDtlsDecrypt(const CASecureEndpoint_t *sep, uint8_t *data, uint32_t dataLen) { OIC_LOG(DEBUG, NET_DTLS_TAG, "IN"); - VERIFY_NON_NULL_RET(endpoint, NET_DTLS_TAG, "endpoint is NULL" , CA_STATUS_INVALID_PARAM); + VERIFY_NON_NULL_RET(sep, NET_DTLS_TAG, "endpoint is NULL" , CA_STATUS_INVALID_PARAM); stCADtlsAddrInfo_t addrInfo = { 0 }; - CAConvertNameToAddr(endpoint->addr, endpoint->port, &(addrInfo.addr.st)); + CAConvertNameToAddr(sep->endpoint.addr, sep->endpoint.port, &(addrInfo.addr.st)); addrInfo.ifIndex = 0; addrInfo.size = CASizeOfAddrInfo(&addrInfo); diff --git a/resource/csdk/connectivity/src/cainterfacecontroller.c b/resource/csdk/connectivity/src/cainterfacecontroller.c index 9df1da3..9b1f8d4 100644 --- a/resource/csdk/connectivity/src/cainterfacecontroller.c +++ b/resource/csdk/connectivity/src/cainterfacecontroller.c @@ -123,14 +123,14 @@ CAResult_t CASetAdapterRAInfo(const CARAInfo_t *caraInfo) } #endif -static void CAReceivedPacketCallback(const CAEndpoint_t *endpoint, const void *data, uint32_t dataLen) +static void CAReceivedPacketCallback(const CASecureEndpoint_t *sep, + const void *data, uint32_t dataLen) { OIC_LOG(DEBUG, TAG, "IN"); - // Call the callback. if (g_networkPacketReceivedCallback != NULL) { - g_networkPacketReceivedCallback(endpoint, data, dataLen); + g_networkPacketReceivedCallback(sep, data, dataLen); } else { diff --git a/resource/csdk/connectivity/src/camessagehandler.c b/resource/csdk/connectivity/src/camessagehandler.c index 3021d8d..577479f 100644 --- a/resource/csdk/connectivity/src/camessagehandler.c +++ b/resource/csdk/connectivity/src/camessagehandler.c @@ -71,8 +71,9 @@ static void CAErrorHandler(const CAEndpoint_t *endpoint, const void *data, uint32_t dataLen, CAResult_t result); -static CAData_t* CAGenerateHandlerData(const CAEndpoint_t *endpoint, const void *data, - CADataType_t dataType); +static CAData_t* CAGenerateHandlerData(const CAEndpoint_t *endpoint, + const CARemoteId_t *identity, + const void *data, CADataType_t dataType); static void CASendErrorInfo(const CAEndpoint_t *endpoint, const CAInfo_t *info, CAResult_t result); @@ -120,15 +121,13 @@ static bool CAIsSelectedNetworkAvailable() return true; } -static CAData_t* CAGenerateHandlerData(const CAEndpoint_t *endpoint, const void *data, CADataType_t dataType) +static CAData_t* CAGenerateHandlerData(const CAEndpoint_t *endpoint, + const CARemoteId_t *identity, + const void *data, CADataType_t dataType) { OIC_LOG(DEBUG, TAG, "CAGenerateHandlerData IN"); - - CAResponseInfo_t* resInfo = NULL; - CARequestInfo_t* reqInfo = NULL; - CAErrorInfo_t *errorInfo = NULL; - - CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof (CAData_t)); + CAInfo_t *info = NULL; + CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t)); if (!cadata) { OIC_LOG(ERROR, TAG, "memory allocation failed"); @@ -139,7 +138,8 @@ static CAData_t* CAGenerateHandlerData(const CAEndpoint_t *endpoint, const void if (!ep) { OIC_LOG(ERROR, TAG, "endpoint clone failed"); - goto errorexit; + OICFree(cadata); + return NULL; } OIC_LOG_V(DEBUG, TAG, "address : %s", ep->addr); @@ -147,96 +147,109 @@ static CAData_t* CAGenerateHandlerData(const CAEndpoint_t *endpoint, const void if(CA_RESPONSE_DATA == dataType) { - resInfo = (CAResponseInfo_t*)OICCalloc(1, sizeof (CAResponseInfo_t)); + CAResponseInfo_t* resInfo = (CAResponseInfo_t*)OICCalloc(1, sizeof(CAResponseInfo_t)); if (!resInfo) { OIC_LOG(ERROR, TAG, "memory allocation failed"); - goto errorexit; + OICFree(cadata); + CAFreeEndpoint(ep); + return NULL; } result = CAGetResponseInfoFromPDU(data, resInfo); if (CA_STATUS_OK != result) { OIC_LOG(ERROR, TAG, "CAGetResponseInfoFromPDU Failed"); - goto errorexit; + CAFreeEndpoint(ep); + CADestroyResponseInfoInternal(resInfo); + OICFree(cadata); + return NULL; } - - if (CADropSecondMessage(&caglobals.ca.responseHistory, endpoint, resInfo->info.messageId)) + cadata->responseInfo = resInfo; + info = &resInfo->info; + if (identity) { - OIC_LOG(ERROR, TAG, "Second Response with same messageID, Drop it"); - goto errorexit; + info->identity = *identity; } - - cadata->responseInfo = resInfo; OIC_LOG(DEBUG, TAG, "Response Info :"); - CALogPayloadInfo(&resInfo->info); + CALogPayloadInfo(info); } else if (CA_REQUEST_DATA == dataType) { - reqInfo = (CARequestInfo_t*)OICCalloc(1, sizeof (CARequestInfo_t)); + CARequestInfo_t* reqInfo = (CARequestInfo_t*)OICCalloc(1, sizeof(CARequestInfo_t)); if (!reqInfo) { OIC_LOG(ERROR, TAG, "memory allocation failed"); - goto errorexit; + OICFree(cadata); + CAFreeEndpoint(ep); + return NULL; } result = CAGetRequestInfoFromPDU(data, reqInfo); if (CA_STATUS_OK != result) { OIC_LOG(ERROR, TAG, "CAGetRequestInfoFromPDU failed"); - goto errorexit; + CAFreeEndpoint(ep); + CADestroyRequestInfoInternal(reqInfo); + OICFree(cadata); + return NULL; } if (CADropSecondMessage(&caglobals.ca.requestHistory, endpoint, reqInfo->info.messageId)) { - OIC_LOG(ERROR, TAG, "Second Request with same messageID, Drop it"); - goto errorexit; + OIC_LOG(ERROR, TAG, "Second Request with same Token, Drop it"); + CAFreeEndpoint(ep); + CADestroyRequestInfoInternal(reqInfo); + OICFree(cadata); + return NULL; } cadata->requestInfo = reqInfo; + info = &reqInfo->info; + if (identity) + { + info->identity = *identity; + } OIC_LOG(DEBUG, TAG, "Request Info :"); - CALogPayloadInfo(&reqInfo->info); - } + CALogPayloadInfo(info); + } else if (CA_ERROR_DATA == dataType) { - errorInfo = (CAErrorInfo_t *)OICCalloc(1, sizeof (CAErrorInfo_t)); + CAErrorInfo_t *errorInfo = (CAErrorInfo_t *)OICCalloc(1, sizeof (CAErrorInfo_t)); if (!errorInfo) { OIC_LOG(ERROR, TAG, "Memory allocation failed!"); - goto errorexit; + OICFree(cadata); + CAFreeEndpoint(ep); + return NULL; } - result = CAGetErrorInfoFromPDU(data, errorInfo); + CAResult_t result = CAGetErrorInfoFromPDU(data, errorInfo); if (CA_STATUS_OK != result) { OIC_LOG(ERROR, TAG, "CAGetErrorInfoFromPDU failed"); - goto errorexit; + CAFreeEndpoint(ep); + OICFree(errorInfo); + OICFree(cadata); + return NULL; } cadata->errorInfo = errorInfo; + info = &errorInfo->info; + if (identity) + { + info->identity = *identity; + } OIC_LOG(DEBUG, TAG, "error Info :"); - CALogPayloadInfo(&errorInfo->info); - } - else - { - OIC_LOG_V(ERROR, TAG, "bad dataType: %d", dataType); - goto errorexit; + CALogPayloadInfo(info); } cadata->remoteEndpoint = ep; cadata->dataType = dataType; - OIC_LOG(DEBUG, TAG, "CAGenerateHandlerData OUT"); - return cadata; -errorexit: - CAFreeEndpoint(ep); - OICFree(cadata); - OICFree(resInfo); - OICFree(reqInfo); - OICFree(errorInfo); - return NULL; + OIC_LOG(DEBUG, TAG, "CAGenerateHandlerData OUT"); } static void CATimeoutCallback(const CAEndpoint_t *endpoint, const void *pdu, uint32_t size) @@ -594,11 +607,11 @@ static bool CADropSecondMessage(CAHistory_t *history, const CAEndpoint_t *ep, ui return ret; } -static void CAReceivedPacketCallback(const CAEndpoint_t *remoteEndpoint, const void *data, - uint32_t dataLen) +static void CAReceivedPacketCallback(const CASecureEndpoint_t *sep, + const void *data, uint32_t dataLen) { OIC_LOG(DEBUG, TAG, "IN"); - VERIFY_NON_NULL_VOID(remoteEndpoint, TAG, "remoteEndpoint"); + VERIFY_NON_NULL_VOID(sep, TAG, "remoteEndpoint"); VERIFY_NON_NULL_VOID(data, TAG, "data"); uint32_t code = CA_NOT_FOUND; @@ -614,7 +627,7 @@ static void CAReceivedPacketCallback(const CAEndpoint_t *remoteEndpoint, const v OIC_LOG_V(DEBUG, TAG, "code = %d", code); if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code) { - cadata = CAGenerateHandlerData(remoteEndpoint, pdu, CA_REQUEST_DATA); + cadata = CAGenerateHandlerData(&(sep->endpoint), &(sep->identity), pdu, CA_REQUEST_DATA); if (!cadata) { OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, CAGenerateHandlerData failed!"); @@ -624,7 +637,7 @@ static void CAReceivedPacketCallback(const CAEndpoint_t *remoteEndpoint, const v } else { - cadata = CAGenerateHandlerData(remoteEndpoint, pdu, CA_RESPONSE_DATA); + cadata = CAGenerateHandlerData(&(sep->endpoint), &(sep->identity), pdu, CA_RESPONSE_DATA); if (!cadata) { OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, CAGenerateHandlerData failed!"); @@ -662,24 +675,24 @@ static void CAReceivedPacketCallback(const CAEndpoint_t *remoteEndpoint, const v CAProcessReceivedData(cadata); #else #ifdef WITH_BWT - if (CA_ADAPTER_GATT_BTLE != remoteEndpoint->adapter) + if (CA_ADAPTER_GATT_BTLE != sep->endpoint.adapter) + { + CAResult_t res = CAReceiveBlockWiseData(pdu, &(sep->endpoint), cadata, dataLen); + if (CA_NOT_SUPPORTED == res) { - CAResult_t res = CAReceiveBlockWiseData(pdu, remoteEndpoint, cadata, dataLen); - if (CA_NOT_SUPPORTED == res) - { - OIC_LOG(ERROR, TAG, "this message does not have block option"); - CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t)); - } - else - { - CADestroyData(cadata, sizeof(CAData_t)); - } + OIC_LOG(ERROR, TAG, "this message does not have block option"); + CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t)); } else -#endif { - CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t)); + CADestroyData(cadata, sizeof(CAData_t)); } + } + else +#endif + { + CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t)); + } #endif coap_delete_pdu(pdu); @@ -1210,7 +1223,7 @@ void CAErrorHandler(const CAEndpoint_t *endpoint, return; } - CAData_t *cadata = CAGenerateHandlerData(endpoint, pdu, CA_ERROR_DATA); + CAData_t *cadata = CAGenerateHandlerData(endpoint, NULL, pdu, CA_ERROR_DATA); if(!cadata) { OIC_LOG(ERROR, TAG, "CAErrorHandler, CAGenerateHandlerData failed!"); diff --git a/resource/csdk/connectivity/src/ip_adapter/arduino/caipserver_eth.cpp b/resource/csdk/connectivity/src/ip_adapter/arduino/caipserver_eth.cpp index 099e6d3..74a51f6 100644 --- a/resource/csdk/connectivity/src/ip_adapter/arduino/caipserver_eth.cpp +++ b/resource/csdk/connectivity/src/ip_adapter/arduino/caipserver_eth.cpp @@ -194,12 +194,10 @@ void CAPacketReceivedCallback(const char *ipAddress, const uint16_t port, OIC_LOG(DEBUG, TAG, "IN"); if (g_packetReceivedCallback) { - CAEndpoint_t ep; - strncpy(ep.addr, ipAddress, MAX_ADDR_STR_SIZE_CA); - ep.port = port; - ep.flags = CA_IPV4; - ep.adapter = CA_ADAPTER_IP; - g_packetReceivedCallback(&ep, data, dataLength); + CASecureEndpoint_t sep = + {.endpoint = {.adapter = CA_ADAPTER_IP, .flags = CA_IPV4, .port = port}}; + OICStrcpy(sep.endpoint.addr, sizeof(sep.endpoint.addr), ipAddress); + g_packetReceivedCallback(&sep, data, dataLength); } OIC_LOG(DEBUG, TAG, "OUT"); } diff --git a/resource/csdk/connectivity/src/ip_adapter/arduino/caipserver_wifi.cpp b/resource/csdk/connectivity/src/ip_adapter/arduino/caipserver_wifi.cpp index 0086b22..d537adc 100644 --- a/resource/csdk/connectivity/src/ip_adapter/arduino/caipserver_wifi.cpp +++ b/resource/csdk/connectivity/src/ip_adapter/arduino/caipserver_wifi.cpp @@ -181,12 +181,11 @@ void CAPacketReceivedCallback(const char *ipAddress, const uint16_t port, OIC_LOG(DEBUG, TAG, "IN"); if (gPacketReceivedCallback) { - CAEndpoint_t ep; - strncpy(ep.addr, ipAddress, MAX_ADDR_STR_SIZE_CA); - ep.port = port; - ep.flags = CA_IPV4; - ep.adapter = CA_ADAPTER_IP; - gPacketReceivedCallback(&ep, data, dataLength); + CASecureEndpoint_t sep = + {.endpoint = {.adapter = CA_ADAPTER_IP, .flags = CA_IPV4, .port = port}}; + + OICStrcpy(sep.endpoint.addr, sizeof(sep.endpoint.addr), ipAddress); + gPacketReceivedCallback(&sep, data, dataLength); OIC_LOG(DEBUG, TAG, "Notified network packet"); } OIC_LOG(DEBUG, TAG, "OUT"); diff --git a/resource/csdk/connectivity/src/ip_adapter/caipadapter.c b/resource/csdk/connectivity/src/ip_adapter/caipadapter.c index 33a0da9..38f0f91 100644 --- a/resource/csdk/connectivity/src/ip_adapter/caipadapter.c +++ b/resource/csdk/connectivity/src/ip_adapter/caipadapter.c @@ -75,7 +75,7 @@ static CANetworkChangeCallback g_networkChangeCallback = NULL; */ static CAErrorHandleCallback g_errorCallback = NULL; -static void CAIPPacketReceivedCB(const CAEndpoint_t *endpoint, +static void CAIPPacketReceivedCB(const CASecureEndpoint_t *endpoint, const void *data, uint32_t dataLength); #ifdef __WITH_DTLS__ static void CAIPPacketSendCB(CAEndpoint_t *endpoint, @@ -164,19 +164,20 @@ static void CAIPPacketSendCB(CAEndpoint_t *endpoint, const void *data, uint32_t } #endif -void CAIPPacketReceivedCB(const CAEndpoint_t *endpoint, const void *data, + +void CAIPPacketReceivedCB(const CASecureEndpoint_t *sep, const void *data, uint32_t dataLength) { OIC_LOG(DEBUG, TAG, "IN"); - VERIFY_NON_NULL_VOID(endpoint, TAG, "ipAddress is NULL"); + VERIFY_NON_NULL_VOID(sep, TAG, "sep is NULL"); VERIFY_NON_NULL_VOID(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); if (g_networkPacketCallback) { - g_networkPacketCallback(endpoint, data, dataLength); + g_networkPacketCallback(sep, data, dataLength); } OIC_LOG(DEBUG, TAG, "OUT"); } diff --git a/resource/csdk/connectivity/src/ip_adapter/caipserver.c b/resource/csdk/connectivity/src/ip_adapter/caipserver.c index 3568b65..0531a03 100644 --- a/resource/csdk/connectivity/src/ip_adapter/caipserver.c +++ b/resource/csdk/connectivity/src/ip_adapter/caipserver.c @@ -230,20 +230,20 @@ static CAResult_t CAReceiveMessage(int fd, CATransportFlags_t flags) return CA_STATUS_FAILED; } - CAEndpoint_t ep = { .adapter = CA_ADAPTER_IP, - .flags = flags }; + CASecureEndpoint_t sep = + {.endpoint = {.adapter = CA_ADAPTER_IP, .flags = flags}}; if (flags & CA_IPV6) { - ep.interface = ((struct sockaddr_in6 *)&srcAddr)->sin6_scope_id; + sep.endpoint.interface = ((struct sockaddr_in6 *)&srcAddr)->sin6_scope_id; ((struct sockaddr_in6 *)&srcAddr)->sin6_scope_id = 0; } - CAConvertAddrToName(&srcAddr, ep.addr, &ep.port); + CAConvertAddrToName(&srcAddr, sep.endpoint.addr, &sep.endpoint.port); if (flags & CA_SECURE) { #ifdef __WITH_DTLS__ - int ret = CAAdapterNetDtlsDecrypt(&ep, (uint8_t *)recvBuffer, recvLen); + int ret = CAAdapterNetDtlsDecrypt(&sep, (uint8_t *)recvBuffer, recvLen); OIC_LOG_V(DEBUG, TAG, "CAAdapterNetDtlsDecrypt returns [%d]", ret); #else OIC_LOG(ERROR, TAG, "Encrypted message but no DTLS"); @@ -253,7 +253,7 @@ static CAResult_t CAReceiveMessage(int fd, CATransportFlags_t flags) { if (g_packetReceivedCallback) { - g_packetReceivedCallback(&ep, recvBuffer, recvLen); + g_packetReceivedCallback(&sep, recvBuffer, recvLen); } } @@ -667,16 +667,16 @@ static void CAHandleNetlink() void CAIPSetPacketReceiveCallback(CAIPPacketReceivedCallback callback) { -OIC_LOG(DEBUG, TAG, "IN"); + OIC_LOG(DEBUG, TAG, "IN"); -g_packetReceivedCallback = callback; + g_packetReceivedCallback = callback; -OIC_LOG(DEBUG, TAG, "OUT"); + OIC_LOG(DEBUG, TAG, "OUT"); } void CAIPSetExceptionCallback(CAIPExceptionCallback callback) { -OIC_LOG(DEBUG, TAG, "IN"); + OIC_LOG(DEBUG, TAG, "IN"); g_exceptionCallback = callback; diff --git a/resource/csdk/security/src/secureresourcemanager.c b/resource/csdk/security/src/secureresourcemanager.c index ddccc55..48e7bfc 100644 --- a/resource/csdk/security/src/secureresourcemanager.c +++ b/resource/csdk/security/src/secureresourcemanager.c @@ -73,7 +73,7 @@ void SRMRequestHandler(const CAEndpoint_t *endPoint, const CARequestInfo_t *requ // Copy the subjectID OicUuid_t subjectId = {.id = {0}}; - memcpy(subjectId.id, endPoint->identity.id, sizeof(subjectId.id)); + memcpy(subjectId.id, requestInfo->info.identity.id, sizeof(subjectId.id)); //Check the URI has the query and skip it before checking the permission char *uri = strstr(requestInfo->info.resourceUri, "?"); diff --git a/resource/csdk/stack/include/octypes.h b/resource/csdk/stack/include/octypes.h index 5f602ff..839fd73 100644 --- a/resource/csdk/stack/include/octypes.h +++ b/resource/csdk/stack/include/octypes.h @@ -377,17 +377,15 @@ typedef struct /** transport modifiers.*/ OCTransportFlags flags; + /** for IP.*/ + uint16_t port; + /** address for all adapters.*/ char addr[MAX_ADDR_STR_SIZE]; /** usually zero for default interface.*/ uint32_t interface; - /** for IP.*/ - uint16_t port; - - /** secure node identity.*/ - OCIdentity identity; } OCDevAddr; /** @@ -1029,6 +1027,9 @@ typedef struct /** backward compatibility.*/ OCConnectivityType connType; + /** the security identity of the remote server.*/ + OCIdentity identity; + /** the is the result of our stack, OCStackResult should contain coap/other error codes.*/ OCStackResult result; diff --git a/resource/csdk/stack/src/ocstack.c b/resource/csdk/stack/src/ocstack.c index 67d8de5..e438bc6 100644 --- a/resource/csdk/stack/src/ocstack.c +++ b/resource/csdk/stack/src/ocstack.c @@ -669,7 +669,7 @@ static OCStackResult ResetPresenceTTL(ClientCB *cbNode, uint32_t maxAgeSeconds) cbNode->presence->TTL = maxAgeSeconds; - for(int index = 0; index < PresenceTimeOutSize; index++) + for (int index = 0; index < PresenceTimeOutSize; index++) { // Guard against overflow if (cbNode->presence->TTL < (UINT32_MAX/(MILLISECONDS_PER_SECOND*PresenceTimeOut[index])) @@ -817,7 +817,7 @@ OCStackResult HandlePresenceResponse(const CAEndpoint_t *endpoint, // check for unicast presence uriLen = FormCanonicalPresenceUri(endpoint, OC_RSRVD_PRESENCE_URI, presenceUri); - if (uriLen < 0 || uriLen >= sizeof (presenceUri)) + if (uriLen < 0 || (size_t)uriLen >= sizeof (presenceUri)) { return OC_STACK_INVALID_URI; } @@ -1046,6 +1046,9 @@ void HandleCAResponses(const CAEndpoint_t* endPoint, const CAResponseInfo_t* res CopyEndpointToDevAddr(endPoint, &response.devAddr); FixUpClientResponse(&response); response.resourceUri = responseInfo->info.resourceUri; + memcpy(response.identity.id, responseInfo->info.identity.id, + sizeof (response.identity.id)); + response.identity.id_length = responseInfo->info.identity.id_length; response.result = CAToOCStackResult(responseInfo->result); cbNode->callBack(cbNode->context, @@ -1063,6 +1066,9 @@ void HandleCAResponses(const CAEndpoint_t* endPoint, const CAResponseInfo_t* res CopyEndpointToDevAddr(endPoint, &response.devAddr); FixUpClientResponse(&response); response.resourceUri = responseInfo->info.resourceUri; + memcpy(response.identity.id, responseInfo->info.identity.id, + sizeof (response.identity.id)); + response.identity.id_length = responseInfo->info.identity.id_length; response.result = CAToOCStackResult(responseInfo->result); if(responseInfo->info.payload && @@ -1654,7 +1660,7 @@ OCStackResult OCInit1(OCMode mode, OCTransportFlags serverFlags, OCTransportFlag VERIFY_SUCCESS(result, OC_STACK_OK); #ifdef WITH_PRESENCE - PresenceTimeOutSize = sizeof(PresenceTimeOut)/sizeof(PresenceTimeOut[0]) - 1; + PresenceTimeOutSize = sizeof (PresenceTimeOut) / sizeof (PresenceTimeOut[0]) - 1; #endif // WITH_PRESENCE //Update Stack state to initialized @@ -2397,7 +2403,7 @@ OCStackResult OCProcessPresence() cbNode->presence->TTLlevel); OC_LOG_V(DEBUG, TAG, "current ticks %d", now); - if(cbNode->presence->TTLlevel >= (PresenceTimeOutSize + 1)) + if (cbNode->presence->TTLlevel > PresenceTimeOutSize) { goto exit; } @@ -2407,7 +2413,6 @@ OCStackResult OCProcessPresence() OC_LOG_V(DEBUG, TAG, "timeout ticks %d", cbNode->presence->timeOut[cbNode->presence->TTLlevel]); } - if (cbNode->presence->TTLlevel >= PresenceTimeOutSize) { OC_LOG(DEBUG, TAG, PCF("No more timeout ticks")); @@ -3774,7 +3779,7 @@ OCStackResult getQueryFromUri(const char * uri, char** query, char ** uriWithout char *pointerToDelimiter = strstr(uri, "?"); - uriWithoutQueryLen = pointerToDelimiter == NULL ? uriLen : pointerToDelimiter - uri; + uriWithoutQueryLen = pointerToDelimiter == NULL ? uriLen : (size_t)(pointerToDelimiter - uri); queryLen = pointerToDelimiter == NULL ? 0 : uriLen - uriWithoutQueryLen - 1; if (uriWithoutQueryLen) -- 2.7.4