From 36522229240eaee694a9ce5a174a9aa4c141c3ec Mon Sep 17 00:00:00 2001 From: jnashok Date: Sat, 28 Mar 2015 19:46:39 +0900 Subject: [PATCH] Removed Token ASCII-char limit and length limit Currently, the stack has a fixed-size for Tokens (CAToken_t) which is null terminated and limited to the ASCII character set. The OIC specification requires that tokens be variable length up to 8 bytes, not be null terminated, and be the full binary set. This change removes the null termination behavior (bad, since it is binary data, where the NUL is a valid value), provides for a variable length token, and removes the previous limit that held us to the printable ASCII character set. This issue was captured in IOT-380 in JIRA. Change-Id: I2159dd3202edeff4025c7e1d0de50360b6521e53 Signed-off-by: jnashok Signed-off-by: Sakthivel Samidurai Signed-off-by: Erich Keane Reviewed-on: https://gerrit.iotivity.org/gerrit/598 Tested-by: jenkins-iotivity --- resource/csdk/connectivity/api/cacommon.h | 1 + resource/csdk/connectivity/api/cainterface.h | 28 ++-- resource/csdk/connectivity/inc/camessagehandler.h | 6 +- .../inc/camessagehandler_singlethread.h | 14 +- resource/csdk/connectivity/inc/caprotocolmessage.h | 8 +- .../inc/caprotocolmessage_singlethread.h | 8 +- .../android/sample_service/jni/ResourceModel.c | 51 ++++--- .../csdk/connectivity/samples/arduino/casample.cpp | 71 +++++++--- .../csdk/connectivity/samples/linux/sample_main.c | 156 ++++++++++++++------- .../csdk/connectivity/samples/tizen/casample.c | 126 +++++++++-------- .../csdk/connectivity/src/caconnectivitymanager.c | 13 +- .../src/caconnectivitymanager_singlethread.c | 13 +- resource/csdk/connectivity/src/camessagehandler.c | 65 ++++----- .../src/camessagehandler_singlethread.c | 51 ++++--- resource/csdk/connectivity/src/caprotocolmessage.c | 63 +++++---- .../src/caprotocolmessage_singlethread.c | 62 ++++---- resource/csdk/connectivity/src/caremotehandler.c | 64 +++++---- resource/csdk/stack/include/internal/occlientcb.h | 8 +- resource/csdk/stack/include/internal/ocobserve.h | 7 +- .../csdk/stack/include/internal/ocserverrequest.h | 7 +- .../csdk/stack/include/internal/ocstackinternal.h | 2 +- resource/csdk/stack/src/occlientcb.c | 43 ++++-- resource/csdk/stack/src/ocobserve.c | 40 +++--- resource/csdk/stack/src/ocresource.c | 6 +- resource/csdk/stack/src/ocserverrequest.c | 28 ++-- resource/csdk/stack/src/ocstack.c | 37 +++-- 26 files changed, 571 insertions(+), 407 deletions(-) diff --git a/resource/csdk/connectivity/api/cacommon.h b/resource/csdk/connectivity/api/cacommon.h index dc83805..33e6fb4 100644 --- a/resource/csdk/connectivity/api/cacommon.h +++ b/resource/csdk/connectivity/api/cacommon.h @@ -305,6 +305,7 @@ typedef struct * if message id is zero, it will generated by CA inside. * otherwise, you can use it */ CAToken_t token; /**< Token for CA */ + uint8_t tokenLength; /**< token length*/ CAHeaderOption_t *options; /** Header Options for the request */ uint8_t numOptions; /**< Number of Header options */ CAPayload_t payload; /**< payload of the request */ diff --git a/resource/csdk/connectivity/api/cainterface.h b/resource/csdk/connectivity/api/cainterface.h index 87db8b8..f2d34bd 100644 --- a/resource/csdk/connectivity/api/cainterface.h +++ b/resource/csdk/connectivity/api/cainterface.h @@ -149,16 +149,18 @@ void CADestroyRemoteEndpoint(CARemoteEndpoint_t *object); /** * @brief Generating the token for matching the request and response. - * @param token [OUT] Token for the request + * @param token [OUT] Token for the request + * @param tokenLength [IN] length of the token * @return #CA_STATUS_OK or #CA_STATUS_FAILED or #CA_MEMORY_ALLOC_FAILED + * or #CA_STATUS_NOT_INITIALIZED * @remark Token memory is destroyed by the caller using CADestroyToken(). * @see CADestroyToken */ -CAResult_t CAGenerateToken(CAToken_t *token); +CAResult_t CAGenerateToken(CAToken_t *token, uint8_t tokenLength); /** * @brief Destroy the token generated by CAGenerateToken - * @param token [IN] Token for the request + * @param token [IN] token to be freed * @return NONE */ void CADestroyToken(CAToken_t token); @@ -170,9 +172,10 @@ void CADestroyToken(CAToken_t token); * @param resourceUri [IN] Uri to send multicast search request. Must contain only relative * path of Uri to be search. * @param token [IN] Token for the request - * @return #CA_STATUS_OK or #CA_STATUS_FAILED + * @param tokenLength [IN] length of the token + * @return #CA_STATUS_OK or #CA_STATUS_FAILED or #CA_STATUS_NOT_INITIALIZED */ -CAResult_t CAFindResource(const CAURI_t resourceUri, const CAToken_t token); +CAResult_t CAFindResource(const CAURI_t resourceUri, const CAToken_t token, uint8_t tokenLength); /** * @brief Send control Request on a resource @@ -215,14 +218,17 @@ CAResult_t CASendNotification(const CARemoteEndpoint_t *object, /** * @brief To advertise the resource - * @param resourceUri [IN] URI to be advertised - * @param token [IN] Token for the request - * @param options [IN] Header options information - * @param numOptions [IN] Number of options - * @return #CA_STATUS_OK or #CA_STATUS_FAILED or #CA_MEMORY_ALLOC_FAILED + * @param resourceUri [IN] URI to be advertised + * @param token [IN] Token for the request + * @param tokenLength [IN] length of the token + * @param options [IN] Header options information + * @param numOptions [IN] Number of options + * @return #CA_STATUS_OK or #CA_STATUS_FAILED or + * #CA_MEMORY_ALLOC_FAILED or #CA_STATUS_NOT_INITIALIZED */ CAResult_t CAAdvertiseResource(const CAURI_t resourceUri,const CAToken_t token, - const CAHeaderOption_t *options,const uint8_t numOptions); + uint8_t tokenLength, const CAHeaderOption_t *options, + const uint8_t numOptions); /** * @brief Select network to use diff --git a/resource/csdk/connectivity/inc/camessagehandler.h b/resource/csdk/connectivity/inc/camessagehandler.h index 236aa22..8e9553e 100644 --- a/resource/csdk/connectivity/inc/camessagehandler.h +++ b/resource/csdk/connectivity/inc/camessagehandler.h @@ -66,12 +66,14 @@ CAResult_t CADetachResponseMessage(const CARemoteEndpoint_t *endpoint, * @brief Detaches control from the caller for sending request * @param resourceUri [IN] resource uri that needs to be sent in the request * @param token [IN] token information of the request + * @param tokenLength [IN] length of the token * @param options [IN] header options that need to be append in the request * @param numOptions [IN] number of options be appended - * @return CA_STATUS_OK or ERROR CODES ( CAResult_t error codes in cacommon.h) + * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h) */ CAResult_t CADetachMessageResourceUri(const CAURI_t resourceUri, const CAToken_t token, - const CAHeaderOption_t *options, const uint8_t numOptions); + uint8_t tokenLength, const CAHeaderOption_t *options, + uint8_t numOptions); /** * @brief Setting the request and response callbacks for network packets diff --git a/resource/csdk/connectivity/inc/camessagehandler_singlethread.h b/resource/csdk/connectivity/inc/camessagehandler_singlethread.h index 9a306f6..f2833a1 100644 --- a/resource/csdk/connectivity/inc/camessagehandler_singlethread.h +++ b/resource/csdk/connectivity/inc/camessagehandler_singlethread.h @@ -66,14 +66,16 @@ CAResult_t CADetachResponseMessage(const CARemoteEndpoint_t *object, /** * @brief Detaches control from the caller for sending request - * @param resourceUri [IN] resource uri that needs to be sent in the request - * @param token [IN] token information of the request - * @param options [IN] header options that need to be append in the request - * @param numOptions [IN] number of options be appended - * @return CA_STATUS_OK or ERROR CODES ( CAResult_t error codes in cacommon.h) + * @param resourceUri [IN] resource uri that needs to be sent in the request + * @param token [IN] token information of the request + * @param tokenLength [IN] length of the token + * @param options [IN] header options that need to be append in the request + * @param numOptions [IN] number of options be appended + * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h) */ CAResult_t CADetachMessageResourceUri(const CAURI_t resourceUri, const CAToken_t token, - const CAHeaderOption_t *options, uint8_t numOptions); + uint8_t tokenLength, const CAHeaderOption_t *options, + uint8_t numOptions); /** * @brief Setting the request and response callbacks for network packets diff --git a/resource/csdk/connectivity/inc/caprotocolmessage.h b/resource/csdk/connectivity/inc/caprotocolmessage.h index e49268a..0dd65a4 100644 --- a/resource/csdk/connectivity/inc/caprotocolmessage.h +++ b/resource/csdk/connectivity/inc/caprotocolmessage.h @@ -161,12 +161,12 @@ void CAGetInfoFromPDU(const coap_pdu_t *pdu, uint32_t *outCode, CAInfo_t *outInf coap_pdu_t *CAParsePDU(const char *data, uint32_t length, uint32_t *outCode); /** - * @brief generates the token - * @param token [OUT] generated token + * @brief generates the token + * @param token [OUT] generated token + * @param tokenLength [IN] length of the token * @return CA_STATUS_OK or ERROR CODES ( CAResult_t error codes in cacommon.h) */ - -CAResult_t CAGenerateTokenInternal(CAToken_t *token); +CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength); /** * @brief destroys the token diff --git a/resource/csdk/connectivity/inc/caprotocolmessage_singlethread.h b/resource/csdk/connectivity/inc/caprotocolmessage_singlethread.h index bda05e3..1581559 100644 --- a/resource/csdk/connectivity/inc/caprotocolmessage_singlethread.h +++ b/resource/csdk/connectivity/inc/caprotocolmessage_singlethread.h @@ -163,12 +163,12 @@ void CAGetInfoFromPDU(const coap_pdu_t *pdu, uint32_t *outCode, CAInfo_t *outInf coap_pdu_t *CAParsePDU(const char *data, uint32_t length, uint32_t *outCode); /** - * @brief generates the token - * @param token [OUT] generated token + * @brief generates the token + * @param token [OUT] generated token + * @param tokenLength [IN] length of the token * @return CA_STATUS_OK or ERROR CODES ( CAResult_t error codes in cacommon.h) */ - -CAResult_t CAGenerateTokenInternal(CAToken_t *token); +CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength); /** * @brief destroys the token diff --git a/resource/csdk/connectivity/samples/android/sample_service/jni/ResourceModel.c b/resource/csdk/connectivity/samples/android/sample_service/jni/ResourceModel.c index abb7910..0790b28 100644 --- a/resource/csdk/connectivity/samples/android/sample_service/jni/ResourceModel.c +++ b/resource/csdk/connectivity/samples/android/sample_service/jni/ResourceModel.c @@ -38,6 +38,7 @@ void callback(char *subject, char *receivedData); CAConnectivityType_t g_selectedNwType = CA_WIFI; static CAToken_t g_lastRequestToken = NULL; +static uint8_t g_lastRequestTokenLength; static const char SECURE_COAPS_PREFIX[] = "coaps://"; static const char SECURE_INFO_DATA[] = "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"]," @@ -49,6 +50,8 @@ static const char NORMAL_INFO_DATA[] static jobject g_responseListenerObject = NULL; extern JavaVM *g_jvm; +static CAToken_t g_clientToken; +static uint8_t g_clientTokenLength = NULL; // init JNIEXPORT void JNICALL Java_com_iotivity_service_RMInterface_setNativeResponseListener (JNIEnv *env, jobject obj, jobject listener){ @@ -198,24 +201,31 @@ JNIEXPORT void JNICALL Java_com_iotivity_service_RMInterface_RMFindResource(JNIE // create token CAToken_t token = NULL; - CAResult_t res = CAGenerateToken(&token); - if (res != CA_STATUS_OK) + uint8_t tokenLength = CA_MAX_TOKEN_LEN; + + CAResult_t res = CAGenerateToken(&token, tokenLength); + if ((CA_STATUS_OK != res) || (!token)) { - LOGI("token generate error!!\n"); - token = NULL; + LOGE("token generate error!!"); return; } - LOGI("generated token %s\n", (token != NULL) ? token : ""); + printf("Generated token %s\n", token); + + res = CAFindResource((const CAURI_t) strUri, token, tokenLength); - if(CA_STATUS_OK != CAFindResource((const CAURI_t)strUri, token)) + if (res != CA_STATUS_OK) { - LOGI("Could not find resource"); + LOGE("Could not find resource"); + //destroy token + CADestroyToken(token); } else { LOGI("find resource to %s URI", strUri); + CADestroyToken(g_lastRequestToken); g_lastRequestToken = token; + g_lastRequestTokenLength = tokenLength; } } @@ -249,13 +259,12 @@ JNIEXPORT void JNICALL Java_com_iotivity_service_RMInterface_RMSendRequest(JNIEn // create token CAToken_t token = NULL; - res = CAGenerateToken(&token); - if (res != CA_STATUS_OK) + uint8_t tokenLength = CA_MAX_TOKEN_LEN; + + res = CAGenerateToken(&token, tokenLength); + if ((CA_STATUS_OK != res) || (!token)) { - LOGI("token generate error!!\n"); - token = NULL; - // destroy token - CADestroyToken(token); + LOGE("token generate error!!"); // destroy remote endpoint CADestroyRemoteEndpoint(endpoint); return; @@ -267,6 +276,7 @@ JNIEXPORT void JNICALL Java_com_iotivity_service_RMInterface_RMSendRequest(JNIEn CAInfo_t requestData = {0}; requestData.token = token; + requestData.tokenLength = tokenLength; const char* strPayload = (*env)->GetStringUTFChars(env, payload, NULL); if (isSecured == 1) @@ -351,11 +361,13 @@ JNIEXPORT void JNICALL Java_com_iotivity_service_RMInterface_RMSendResponse(JNIE // create token CAToken_t token = NULL; - res = CAGenerateToken(&token); - if (res != CA_STATUS_OK) + uint8_t tokenLength = CA_MAX_TOKEN_LEN; + + res = CAGenerateToken(&token, tokenLength); + if ((CA_STATUS_OK != res) || (!token)) { - LOGI("token generate error!"); - token = NULL; + LOGE("token generate error!"); + CADestroyRemoteEndpoint(endpoint); } char resourceURI[15] = {0}; @@ -613,8 +625,9 @@ void request_handler(const CARemoteEndpoint_t* object, const CARequestInfo_t* re } } - if (g_lastRequestToken != NULL && requestInfo->info.token != NULL - && (strcmp((char *)g_lastRequestToken, requestInfo->info.token) == 0)) + if ((!g_lastRequestToken) && (!requestInfo->info.token) && + (strncmp(g_lastRequestToken, requestInfo->info.token, + requestInfo->info.tokenLength) == 0)) { LOGI("token is same. received request of it's own. skip.. \n"); return; diff --git a/resource/csdk/connectivity/samples/arduino/casample.cpp b/resource/csdk/connectivity/samples/arduino/casample.cpp index 7ea6b50..1377b99 100644 --- a/resource/csdk/connectivity/samples/arduino/casample.cpp +++ b/resource/csdk/connectivity/samples/arduino/casample.cpp @@ -269,8 +269,10 @@ void FindResource() } // create token CAToken_t token = NULL; - CAResult_t res = CAGenerateToken(&token); - if (res != CA_STATUS_OK || token == NULL) + uint8_t tokenLength = CA_MAX_TOKEN_LEN; + + CAResult_t res = CAGenerateToken(&token, tokenLength); + if (res != CA_STATUS_OK || (!token)) { Serial.println("token error"); return; @@ -279,7 +281,7 @@ void FindResource() Serial.print("token:"); Serial.println(token); - res = CAFindResource(buf, token); + res = CAFindResource(buf, token, tokenLength); if (res != CA_STATUS_OK) { Serial.print("find error: "); @@ -341,17 +343,19 @@ void SendRequest() // create token CAToken_t token = NULL; - res = CAGenerateToken(&token); - if (res != CA_STATUS_OK) + uint8_t tokenLength = CA_MAX_TOKEN_LEN; + + res = CAGenerateToken(&token, tokenLength); + if (res != CA_STATUS_OK || (!token)) { Serial.println("token error"); - token = NULL; return; } - Serial.println((token != NULL) ? token : ""); + Serial.println(token); CAInfo_t requestData = {CA_MSG_RESET}; requestData.token = token; + requestData.tokenLength = tokenLength; requestData.payload = (CAPayload_t)"Json Payload"; requestData.type = msgType; @@ -362,7 +366,7 @@ void SendRequest() // send request CASendRequest(endpoint, &requestInfo); - if (token != NULL) + if (NULL != token) { CADestroyToken(token); } @@ -373,6 +377,7 @@ void SendRequest() CADestroyRemoteEndpoint(endpoint); } + CADestroyToken(token); Serial.println("============"); } @@ -414,18 +419,20 @@ void SendRequestAll() // create token CAToken_t token = NULL; - res = CAGenerateToken(&token); - if (res != CA_STATUS_OK) + uint8_t tokenLength = CA_MAX_TOKEN_LEN; + + res = CAGenerateToken(&token, tokenLength); + if (res != CA_STATUS_OK || (!token)) { Serial.println("token error"); - token = NULL; return; } - Serial.println((token != NULL) ? token : ""); + Serial.println(token); CAInfo_t requestData = {CA_MSG_RESET}; requestData.token = token; + requestData.tokenLength = tokenLength; requestData.payload = "Temp Json Payload"; requestData.type = CA_MSG_NONCONFIRM; @@ -437,7 +444,7 @@ void SendRequestAll() // CASendRequest(endpoint, &requestInfo); CASendRequestToAll(group, &requestInfo); - if (token != NULL) + if (NULL != token) { CADestroyToken(token); } @@ -533,18 +540,19 @@ void AdvertiseResource() Serial.println("============"); // create token CAToken_t token = NULL; - CAResult_t res = CAGenerateToken(&token); - if (res != CA_STATUS_OK) + uint8_t tokenLength = CA_MAX_TOKEN_LEN; + + CAResult_t res = CAGenerateToken(&token, tokenLength); + if (res != CA_STATUS_OK || (!token)) { Serial.println("token error"); - token = NULL; return; } Serial.println("token"); - Serial.println((token != NULL) ? token : ""); + Serial.println(token); - CAAdvertiseResource(buf, token, headerOpt, (uint8_t)optionNum); + CAAdvertiseResource(buf, token, tokenLength, headerOpt, (uint8_t)optionNum); OICFree(headerOpt); CADestroyToken(token); } @@ -578,13 +586,25 @@ void SendNotification() return; } - CAInfo_t respondeData = {CA_MSG_RESET}; - respondeData.token = (CAToken_t)"token"; - respondeData.payload = (CAPayload_t)"Notification Data"; + // create token + CAToken_t token = NULL; + uint8_t tokenLength = CA_MAX_TOKEN_LEN; + + res = CAGenerateToken(&token, tokenLength); + if (res != CA_STATUS_OK || (!token)) + { + Serial.println("token error"); + return; + } + + CAInfo_t respondData = {CA_MSG_NONCONFIRM}; + respondData.token = token; + respondData.tokenLength = tokenLength; + respondData.payload = (CAPayload_t)"Notification Data"; CAResponseInfo_t responseInfo = {CA_BAD_REQ, {CA_MSG_RESET}}; responseInfo.result = CA_SUCCESS; - responseInfo.info = respondeData; + responseInfo.info = respondData; // send request CASendNotification(endpoint, &responseInfo); @@ -593,6 +613,8 @@ void SendNotification() { CADestroyRemoteEndpoint(endpoint); } + + CADestroyToken(token); Serial.println("============"); } @@ -788,6 +810,8 @@ void ResponseHandler(const CARemoteEndpoint_t *object, const CAResponseInfo_t *r void SendResponse(CARemoteEndpoint_t *endpoint, const CAInfo_t* info) { + char buf[MAX_BUF_LEN] = {0}; + Serial.println("============"); CAInfo_t responseData = {CA_MSG_RESET}; if(info && info->type == CA_MSG_CONFIRM) @@ -800,7 +824,8 @@ void SendResponse(CARemoteEndpoint_t *endpoint, const CAInfo_t* info) } responseData.messageId = (info != NULL) ? info->messageId : 0; - responseData.token = (info != NULL) ? (CAToken_t)info->token : (CAToken_t)""; + responseData.token = (info != NULL) ? (CAToken_t)info->token : NULL; + responseData.tokenLength = (info != NULL) ? info->tokenLength : 0; responseData.payload = (CAPayload_t)"response payload"; CAResponseInfo_t responseInfo = {CA_BAD_REQ, {CA_MSG_RESET}}; diff --git a/resource/csdk/connectivity/samples/linux/sample_main.c b/resource/csdk/connectivity/samples/linux/sample_main.c index 7084923..8d48523 100644 --- a/resource/csdk/connectivity/samples/linux/sample_main.c +++ b/resource/csdk/connectivity/samples/linux/sample_main.c @@ -350,23 +350,25 @@ void find_fixed_resource() // create token CAToken_t token = NULL; - CAResult_t res = CAGenerateToken(&token); - if (res != CA_STATUS_OK) + uint8_t tokenLength = CA_MAX_TOKEN_LEN; + + CAResult_t res = CAGenerateToken(&token, tokenLength); + if ((CA_STATUS_OK != res) || (!token)) { - printf("token generate error!!"); + printf("Token generate error!!"); return; } - printf("generated token %s\n", (token != NULL) ? token : ""); + printf("Generated token %s\n", token); - res = CAFindResource(buf, token); - if (res != CA_STATUS_OK) + res = CAFindResource(buf, token, tokenLength); + if (CA_STATUS_OK != res) { - printf("find resource error : %d\n", res); + printf("Find resource error : %d\n", res); } else { - printf("find resource to %s URI\n", buf); + printf("Find resource to %s URI\n", buf); } // delete token @@ -390,29 +392,27 @@ void find_resource() // create token CAToken_t token = NULL; - CAResult_t res = CAGenerateToken(&token); - if (res != CA_STATUS_OK) + uint8_t tokenLength = CA_MAX_TOKEN_LEN; + + CAResult_t res = CAGenerateToken(&token, tokenLength); + if ((CA_STATUS_OK != res) || (!token)) { - printf("token generate error!!\n"); + printf("Token generate error!!\n"); return; } - printf("generated token %s\n", (token != NULL) ? token : ""); + printf("Generated token %s\n", token); - res = CAFindResource(buf, token); - if (res != CA_STATUS_OK) + res = CAFindResource(buf, token, tokenLength); + if (CA_STATUS_OK != res) { - printf("find resource error : %d\n", res); + printf("Find resource error : %d\n", res); + CADestroyToken(token); } else { - printf("find resource to %s URI\n", buf); - - if (g_last_request_token != NULL) - { - CADestroyToken(g_last_request_token); - } - + printf("Find resource to %s URI\n", buf); + CADestroyToken(g_last_request_token); g_last_request_token = token; } @@ -485,14 +485,17 @@ void send_request() // create token CAToken_t token = NULL; - res = CAGenerateToken(&token); - if (CA_STATUS_OK != res) + uint8_t tokenLength = CA_MAX_TOKEN_LEN; + + res = CAGenerateToken(&token, tokenLength); + if ((CA_STATUS_OK != res) || (!token)) { - printf("token generate error, error code : %d\n", res); + printf("Token generate error, error code : %d\n", res); + CADestroyRemoteEndpoint(endpoint); return; } - printf("generated token %s\n", (token != NULL) ? token : ""); + printf("Generated token %s\n", token); // extract relative resourceuri from give uri char resourceURI[15] = {0}; @@ -503,13 +506,17 @@ void send_request() // create request data CAInfo_t requestData = { 0 }; requestData.token = token; + requestData.tokenLength = tokenLength; + if (strcmp(secureRequest, "1") == 0) { int length = sizeof(SECURE_INFO_DATA) + strlen(resourceURI); requestData.payload = (CAPayload_t) calloc(length, sizeof(char)); if (requestData.payload == NULL) { - printf("memory alloc fail\n"); + printf("Memory allocation fail\n"); + CADestroyRemoteEndpoint(endpoint); + CADestroyToken(token); return; } snprintf(requestData.payload, length, SECURE_INFO_DATA, resourceURI, g_local_secure_port); @@ -520,7 +527,9 @@ void send_request() requestData.payload = (CAPayload_t) calloc(length, sizeof(char)); if (requestData.payload == NULL) { - printf("memory alloc fail\n"); + printf("Memory allocation fail\n"); + CADestroyRemoteEndpoint(endpoint); + CADestroyToken(token); return; } snprintf(requestData.payload, length, NORMAL_INFO_DATA, resourceURI); @@ -570,16 +579,22 @@ void send_secure_request() // create token CAToken_t token = NULL; - if (CA_STATUS_OK != CAGenerateToken(&token)) + uint8_t tokenLength = CA_MAX_TOKEN_LEN; + + res = CAGenerateToken(&token, tokenLength); + if ((CA_STATUS_OK != res) || (!token)) { - printf("Failed to generate token !\n"); + printf("Token generate error, error code : %d\n", res); goto exit; } + printf("Generated token %s\n", token); + // create request data CAMessageType_t msgType = CA_MSG_NONCONFIRM; CAInfo_t requestData = { 0 }; requestData.token = token; + requestData.tokenLength = tokenLength; requestData.type = msgType; CARequestInfo_t requestInfo = { 0 }; @@ -642,18 +657,22 @@ void send_request_all() // create token CAToken_t token = NULL; - res = CAGenerateToken(&token); + uint8_t tokenLength = CA_MAX_TOKEN_LEN; - if (res != CA_STATUS_OK) + res = CAGenerateToken(&token, tokenLength); + if ((CA_STATUS_OK != res) || (!token)) { - printf("token generate error!!\n"); + printf("Token generate error!!\n"); + CADestroyRemoteEndpoint(endpoint); + free(group); return; } - printf("generated token %s\n", (token != NULL) ? token : ""); + printf("generated token %s\n", token); CAInfo_t requestData = { 0 }; requestData.token = token; + requestData.tokenLength = tokenLength; requestData.payload = "Temp Json Payload"; requestData.type = CA_MSG_NONCONFIRM; @@ -662,9 +681,17 @@ void send_request_all() requestInfo.info = requestData; // send request - CASendRequestToAll(group, &requestInfo); - - CADestroyToken(token); + res = CASendRequestToAll(group, &requestInfo); + if (CA_STATUS_OK != res) + { + printf("Could not send request to all\n"); + CADestroyToken(token); + } + else + { + CADestroyToken(g_last_request_token); + g_last_request_token = token; + } // destroy remote endpoint CADestroyRemoteEndpoint(endpoint); @@ -736,20 +763,29 @@ void advertise_resource() // create token CAToken_t token = NULL; - CAResult_t res = CAGenerateToken(&token); - if (res != CA_STATUS_OK) + uint8_t tokenLength = CA_MAX_TOKEN_LEN; + + CAResult_t res = CAGenerateToken(&token, tokenLength); + if ((CA_STATUS_OK != res) || (!token)) { - printf("token generate error!!\n"); - token = NULL; + printf("Token generate error!!\n"); + free(headerOpt); return; } - printf("generated token %s\n", (token != NULL) ? token : ""); - - CAAdvertiseResource(buf, token, headerOpt, (uint8_t) optionNum); + printf("Generated token %s\n", token); - // delete token - CADestroyToken(token); + res = CAAdvertiseResource(buf, token, tokenLength, headerOpt, (uint8_t) optionNum); + if (CA_STATUS_OK != res) + { + printf("Could not start advertise resource\n"); + CADestroyToken(token); + } + else + { + CADestroyToken(g_last_request_token); + g_last_request_token = token; + } free(headerOpt); } @@ -788,6 +824,19 @@ void send_notification() return; } + // create token + CAToken_t token = NULL; + uint8_t tokenLength = CA_MAX_TOKEN_LEN; + + res = CAGenerateToken(&token, tokenLength); + if ((CA_STATUS_OK != res) || (!token)) + { + printf("Token generate error!!\n"); + CADestroyRemoteEndpoint(endpoint); + return; + } + + printf("Generated token %s\n", token); CAInfo_t respondData = { 0 }; respondData.token = "client token"; respondData.payload = "Temp Notification Data"; @@ -807,6 +856,8 @@ void send_notification() printf("send notification success\n"); } + // destroy token + CADestroyToken(token); // destroy remote endpoint CADestroyRemoteEndpoint(endpoint); @@ -989,8 +1040,9 @@ void request_handler(const CARemoteEndpoint_t *object, const CARequestInfo_t *re printf("Data: %s\n", requestInfo->info.payload); printf("Message type: %s\n", MESSAGE_TYPE[requestInfo->info.type]); - if (g_last_request_token != NULL && requestInfo->info.token != NULL - && (strcmp((char *)g_last_request_token, requestInfo->info.token) == 0)) + if ((!g_last_request_token) && (!requestInfo->info.token) + && (strncmp(g_last_request_token, requestInfo->info.token, + requestInfo->info.tokenLength) == 0)) { printf("token is same. received request of it's own. skip.. \n"); return; @@ -1100,8 +1152,12 @@ void send_response(CARemoteEndpoint_t *endpoint, CAInfo_t *info) ((info->type == CA_MSG_CONFIRM) ? CA_MSG_ACKNOWLEDGE : CA_MSG_NONCONFIRM) : CA_MSG_NONCONFIRM; responseData.messageId = (info != NULL) ? info->messageId : 0; - responseData.token = (info != NULL) ? info->token : ""; - responseData.payload = "response payload"; + if(3 != messageType) + { + responseData.token = (info != NULL) ? info->token : NULL; + responseData.tokenLength = (info != NULL) ? info->tokenLength : 0; + responseData.payload = "response payload"; + } CAResponseInfo_t responseInfo = { 0 }; responseInfo.result = 203; diff --git a/resource/csdk/connectivity/samples/tizen/casample.c b/resource/csdk/connectivity/samples/tizen/casample.c index 4926b79..de45350 100644 --- a/resource/csdk/connectivity/samples/tizen/casample.c +++ b/resource/csdk/connectivity/samples/tizen/casample.c @@ -376,30 +376,29 @@ void FindFixedResource() // create token CAToken_t token = NULL; - CAResult_t res = CAGenerateToken(&token); - if (res != CA_STATUS_OK) + uint8_t tokenLength = CA_MAX_TOKEN_LEN; + + CAResult_t res = CAGenerateToken(&token, tokenLength); + if ((CA_STATUS_OK != res) || (!token)) { printf("token generate error!!"); - token = NULL; + return; } - printf("generated token %s\n", (token != NULL) ? token : ""); + printf("Generated token %s\n", token); - res = CAFindResource(buf, token); + res = CAFindResource(buf, token, tokenLength); if (res != CA_STATUS_OK) { - printf("find resource error!!\n"); + printf("find resource error : %d\n", res); } else { printf("find resource to %s URI\n", buf); } - //delete token - if (token) - { - CADestroyToken(token); - } + // delete token + CADestroyToken(token); printf("=============================================\n"); } @@ -420,28 +419,28 @@ void FindResource() // create token CAToken_t token = NULL; - CAResult_t res = CAGenerateToken(&token); - if (res != CA_STATUS_OK) + uint8_t tokenLength = CA_MAX_TOKEN_LEN; + + CAResult_t res = CAGenerateToken(&token, tokenLength); + if ((CA_STATUS_OK != res) || (!token)) { printf("token generate error!!\n"); - token = NULL; + return; } - printf("generated token %s\n", (token != NULL) ? token : ""); + printf("Generated token %s\n", token); - res = CAFindResource(buf, token); + res = CAFindResource(buf, token, tokenLength); if (res != CA_STATUS_OK) { - printf("find resource error!!\n"); + printf("find resource error : %d\n", res); + CADestroyToken(token); } else { printf("find resource to %s URI\n", buf); - if (g_lastRequestToken) - { - CADestroyToken(g_lastRequestToken); - } + CADestroyToken(g_lastRequestToken); g_lastRequestToken = token; } @@ -515,13 +514,16 @@ void SendRequest() // create token CAToken_t token = NULL; - if (CA_STATUS_OK != CAGenerateToken(&token)) + uint8_t tokenLength = CA_MAX_TOKEN_LEN; + + res = CAGenerateToken(&token, tokenLength); + if ((CA_STATUS_OK != res) || (!token)) { - printf("token generate error!!\n"); - token = NULL; + printf("token generate error, error code : %d\n", res); + return; } - printf("generated token %s\n", (token != NULL) ? token : ""); + printf("Generated token %s\n", token); // extract relative resourceuri from give uri char resourceURI[15] = {0}; @@ -567,15 +569,9 @@ void SendRequest() // send request CASendRequest(endpoint, &requestInfo); - if (token) - { - CADestroyToken(token); - } + CADestroyToken(token); - if (requestData.payload) - { - free(requestData.payload); - } + free(requestData.payload); // destroy remote endpoint CADestroyRemoteEndpoint(endpoint); @@ -629,18 +625,20 @@ void SendRequestAll() // create token CAToken_t token = NULL; - res = CAGenerateToken(&token); + uint8_t tokenLength = CA_MAX_TOKEN_LEN; - if (res != CA_STATUS_OK) + res = CAGenerateToken(&token, tokenLength); + if ((CA_STATUS_OK != res) || (!token)) { printf("token generate error!!\n"); - token = NULL; + return; } - printf("generated token %s\n", (token != NULL) ? token : ""); + printf("generated token %s\n", token); CAInfo_t requestData = {CA_MSG_RESET}; requestData.token = token; + requestData.tokenLength = tokenLength; requestData.payload = "Temp Json Payload"; requestData.type = CA_MSG_NONCONFIRM; @@ -649,13 +647,9 @@ void SendRequestAll() requestInfo.info = requestData; // send request - // CASendRequest(endpoint, &requestInfo); CASendRequestToAll(group, &requestInfo); - if (token != NULL) - { - CADestroyToken(token); - } + CADestroyToken(token); // destroy remote endpoint CADestroyRemoteEndpoint(endpoint); @@ -712,25 +706,23 @@ void AdvertiseResource() // create token CAToken_t token = NULL; - CAResult_t res = CAGenerateToken(&token); - if (res != CA_STATUS_OK) + uint8_t tokenLength = CA_MAX_TOKEN_LEN; + + CAResult_t res = CAGenerateToken(&token, tokenLength); + if ((CA_STATUS_OK != res) || (!token)) { printf("token generate error!!\n"); - token = NULL; + return; } - printf("generated token %s\n", (token != NULL) ? token : ""); + printf("generated token %s\n", token); - CAAdvertiseResource(buf, token, headerOpt, (uint8_t)optionNum); + CAAdvertiseResource(buf, token, tokenLength, headerOpt, (uint8_t)optionNum); // delete token - if (token) - { - CADestroyToken(token); - } + CADestroyToken(token); free(headerOpt); - } void SendNotification() @@ -766,30 +758,37 @@ void SendNotification() return; } - CAInfo_t respondeData = {CA_MSG_RESET}; - respondeData.token = "client token"; - respondeData.payload = "Temp Notification Data"; + CAInfo_t respondData = {CA_MSG_RESET}; + respondData.tokenLength = CA_MAX_TOKEN_LEN; + + res = CAGenerateToken(&respondData.token, respondData.tokenLength); + if ((CA_STATUS_OK != res) || (!respondData.token)) + { + printf("token generate error!!\n"); + return; + } + + respondData.payload = "Temp Notification Data"; CAResponseInfo_t responseInfo = {0}; responseInfo.result = CA_SUCCESS; - responseInfo.info = respondeData; + responseInfo.info = respondData; // send request res = CASendNotification(endpoint, &responseInfo); if (res != CA_STATUS_OK) { - printf("send notification error\n"); + printf("send notification error, error code: %d\n", res); } else { printf("send notification success\n"); } + // delete token + CADestroyToken(respondData.token); // destroy remote endpoint - if (endpoint != NULL) - { - CADestroyRemoteEndpoint(endpoint); - } + CADestroyRemoteEndpoint(endpoint); printf("\n=============================================\n"); } @@ -1056,6 +1055,7 @@ void ResponseHandler(const CARemoteEndpoint_t *object, const CAResponseInfo_t *r printf("response result : %d\n", responseInfo->result); printf("Data: %s\n", responseInfo->info.payload); printf("Message type: %s\n", g_messageType[responseInfo->info.type]); + printf("Token: %s\n", responseInfo->info.token); if (responseInfo->info.options) { uint32_t len = responseInfo->info.numOptions; @@ -1103,6 +1103,7 @@ void SendResponse(CARemoteEndpoint_t *endpoint, CAInfo_t *info) { responseData.messageId = info->messageId; responseData.token = info->token; + responseData.tokenLength = info->tokenLength; } responseData.payload = "response payload"; @@ -1133,7 +1134,7 @@ void SendResponse(CARemoteEndpoint_t *endpoint, CAInfo_t *info) printf("=============================================\n"); } -void SendRequestTemp(CARemoteEndpoint_t *endpoint, CAToken_t token) +void SendRequestTemp(CARemoteEndpoint_t *endpoint, CAToken_t token, uint8_t tokenLength) { printf("\n=============================================\n"); @@ -1145,6 +1146,7 @@ void SendRequestTemp(CARemoteEndpoint_t *endpoint, CAToken_t token) CAInfo_t requestData ={CA_MSG_RESET}; requestData.token = token; + requestData.tokenLength = tokenLength; requestData.payload = "Temp Json Payload"; CARequestInfo_t requestInfo ={CA_GET, {CA_MSG_RESET}}; diff --git a/resource/csdk/connectivity/src/caconnectivitymanager.c b/resource/csdk/connectivity/src/caconnectivitymanager.c index 421d758..29e5b77 100644 --- a/resource/csdk/connectivity/src/caconnectivitymanager.c +++ b/resource/csdk/connectivity/src/caconnectivitymanager.c @@ -110,11 +110,11 @@ void CADestroyRemoteEndpoint(CARemoteEndpoint_t *rep) CADestroyRemoteEndpointInternal(rep); } -CAResult_t CAGenerateToken(CAToken_t *token) +CAResult_t CAGenerateToken(CAToken_t *token, uint8_t tokenLength) { OIC_LOG_V(DEBUG, TAG, "CAGenerateToken"); - return CAGenerateTokenInternal(token); + return CAGenerateTokenInternal(token, tokenLength); } void CADestroyToken(CAToken_t token) @@ -131,11 +131,11 @@ CAResult_t CAGetNetworkInformation(CALocalConnectivity_t **info, uint32_t *size) return CAGetNetworkInformationInternal(info, size); } -CAResult_t CAFindResource(const CAURI_t resourceUri, const CAToken_t token) +CAResult_t CAFindResource(const CAURI_t resourceUri, const CAToken_t token, uint8_t tokenLength) { OIC_LOG_V(DEBUG, TAG, "CAFindResource"); - return CADetachMessageResourceUri(resourceUri, token, NULL, 0); + return CADetachMessageResourceUri(resourceUri, token, tokenLength, NULL, 0); } @@ -173,11 +173,12 @@ CAResult_t CASendResponse(const CARemoteEndpoint_t *object, } CAResult_t CAAdvertiseResource(const CAURI_t resourceUri,const CAToken_t token, - const CAHeaderOption_t *options,const uint8_t numOptions) + uint8_t tokenLength, const CAHeaderOption_t *options, + const uint8_t numOptions) { OIC_LOG_V(DEBUG, TAG, "CAAdvertiseResource"); - return CADetachMessageResourceUri(resourceUri, token, options, numOptions); + return CADetachMessageResourceUri(resourceUri, token, tokenLength, options, numOptions); } diff --git a/resource/csdk/connectivity/src/caconnectivitymanager_singlethread.c b/resource/csdk/connectivity/src/caconnectivitymanager_singlethread.c index f0fbb21..ad92f51 100644 --- a/resource/csdk/connectivity/src/caconnectivitymanager_singlethread.c +++ b/resource/csdk/connectivity/src/caconnectivitymanager_singlethread.c @@ -90,9 +90,9 @@ void CADestroyRemoteEndpoint(CARemoteEndpoint_t *rep) CADestroyRemoteEndpointInternal(rep); } -CAResult_t CAGenerateToken(CAToken_t *token) +CAResult_t CAGenerateToken(CAToken_t *token, uint8_t tokenLength) { - return CAGenerateTokenInternal(token); + return CAGenerateTokenInternal(token, tokenLength); } void CADestroyToken(CAToken_t token) @@ -107,12 +107,11 @@ CAResult_t CAGetNetworkInformation(CALocalConnectivity_t **info, uint32_t *size) return CAGetNetworkInformationInternal(info, size); } -CAResult_t CAFindResource(const CAURI_t resourceUri, const CAToken_t token) +CAResult_t CAFindResource(const CAURI_t resourceUri, const CAToken_t token, uint8_t tokenLength) { OIC_LOG(DEBUG, TAG, "IN"); - return CADetachMessageResourceUri(resourceUri, token, NULL, 0); - + return CADetachMessageResourceUri(resourceUri, token, tokenLength, NULL, 0); } CAResult_t CASendRequest(const CARemoteEndpoint_t *object,const CARequestInfo_t *requestInfo) @@ -141,10 +140,10 @@ CAResult_t CASendResponse(const CARemoteEndpoint_t *object, } CAResult_t CAAdvertiseResource(const CAURI_t resourceUri,const CAToken_t token, - const CAHeaderOption_t *options, + uint8_t tokenLength, const CAHeaderOption_t *options, const uint8_t numOptions) { - return CADetachMessageResourceUri(resourceUri, token, options, numOptions); + return CADetachMessageResourceUri(resourceUri, token, tokenLength, options, numOptions); } CAResult_t CASelectNetwork(const uint32_t interestedNetwork) diff --git a/resource/csdk/connectivity/src/camessagehandler.c b/resource/csdk/connectivity/src/camessagehandler.c index 7e9de74..cdeae05 100644 --- a/resource/csdk/connectivity/src/camessagehandler.c +++ b/resource/csdk/connectivity/src/camessagehandler.c @@ -266,6 +266,7 @@ static void CASendThreadProcess(void *threadData) info.options = data->options; info.numOptions = data->numOptions; info.token = data->requestInfo->info.token; + info.tokenLength = data->requestInfo->info.tokenLength; info.type = data->requestInfo->info.type; pdu = (coap_pdu_t *) CAGeneratePdu(data->remoteEndpoint->resourceUri, CA_GET, info); @@ -351,7 +352,8 @@ static void CAReceivedPacketCallback(CARemoteEndpoint_t *endpoint, void *data, if (NULL != ReqInfo->info.token) { OIC_LOG(DEBUG, TAG, "Request- token:"); - OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) ReqInfo->info.token, CA_MAX_TOKEN_LEN); + OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) ReqInfo->info.token, + ReqInfo->info.tokenLength); } if (NULL != endpoint) @@ -690,44 +692,48 @@ memory_error_exit: } CAResult_t CADetachMessageResourceUri(const CAURI_t resourceUri, const CAToken_t token, - const CAHeaderOption_t *options,const uint8_t numOptions) + uint8_t tokenLength, const CAHeaderOption_t *options, + uint8_t numOptions) { - if (resourceUri == NULL) - { - return CA_STATUS_FAILED; - } - CARequestInfo_t *ReqInfo = NULL; - CAToken_t tempToken = NULL; + OIC_LOG(DEBUG, TAG, "IN"); + VERIFY_NON_NULL(resourceUri, TAG, "resourceUri is NULL"); + VERIFY_NON_NULL(token, TAG, "Token is NULL"); + CARemoteEndpoint_t *remoteEndpoint = NULL; + CARequestInfo_t *reqInfo = NULL; + char *tempToken = NULL; + // allocate & initialize CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t)); CA_MEMORY_ALLOC_CHECK(data); CAAddress_t addr = {}; remoteEndpoint = CACreateRemoteEndpointInternal(resourceUri, addr, - CA_ETHERNET | CA_WIFI | CA_EDR | CA_LE); + CA_ETHERNET | CA_WIFI | CA_EDR | CA_LE); // create request info - ReqInfo = (CARequestInfo_t *) OICCalloc(1, sizeof(CARequestInfo_t)); - CA_MEMORY_ALLOC_CHECK(ReqInfo); + reqInfo = (CARequestInfo_t *) OICCalloc(1, sizeof(CARequestInfo_t)); + CA_MEMORY_ALLOC_CHECK(reqInfo); - // copy token value - if (token != NULL) + if (tokenLength) { - int32_t len = strlen(token); - tempToken = (char *) OICCalloc((len + 1), sizeof(char)); + // copy token value + tempToken = (char *) OICMalloc(tokenLength); CA_MEMORY_ALLOC_CHECK(tempToken); - strncpy(tempToken, token, len); + memcpy(tempToken, token, tokenLength); } // save request info data - ReqInfo->method = CA_GET; - ReqInfo->info.token = tempToken; - ReqInfo->info.type = CA_MSG_NONCONFIRM; + reqInfo->method = CA_GET; + reqInfo->info.type = CA_MSG_NONCONFIRM; + + reqInfo->info.token = tempToken; + reqInfo->info.tokenLength = tokenLength; + // save data data->type = SEND_TYPE_MULTICAST; data->remoteEndpoint = remoteEndpoint; - data->requestInfo = ReqInfo; + data->requestInfo = reqInfo; data->responseInfo = NULL; data->options = NULL; @@ -756,21 +762,10 @@ memory_error_exit: CADestroyRemoteEndpointInternal(remoteEndpoint); - if (tempToken != NULL) - { - OICFree(tempToken); - } - - if (ReqInfo != NULL) - { - OICFree(ReqInfo); - } - - if (data != NULL) - { - OICFree(data); - } - + OICFree(tempToken); + OICFree(reqInfo); + OICFree(data); + OIC_LOG(DEBUG, TAG, "OUT"); return CA_MEMORY_ALLOC_FAILED; } diff --git a/resource/csdk/connectivity/src/camessagehandler_singlethread.c b/resource/csdk/connectivity/src/camessagehandler_singlethread.c index 1321eca..eb12d91 100644 --- a/resource/csdk/connectivity/src/camessagehandler_singlethread.c +++ b/resource/csdk/connectivity/src/camessagehandler_singlethread.c @@ -126,6 +126,7 @@ static void CAProcessData(CAData_t *data) info.options = data->options; info.numOptions = data->numOptions; info.token = data->requestInfo->info.token; + info.tokenLength = data->requestInfo->info.tokenLength; info.type = data->requestInfo->info.type; pdu = (coap_pdu_t *) CAGeneratePdu(data->remoteEndpoint->resourceUri, CA_GET, info); @@ -434,55 +435,59 @@ memory_error_exit: } CAResult_t CADetachMessageResourceUri(const CAURI_t resourceUri, const CAToken_t token, - const CAHeaderOption_t *options, + uint8_t tokenLength, const CAHeaderOption_t *options, uint8_t numOptions) { OIC_LOG(DEBUG, TAG, "IN"); - if (resourceUri == NULL) - { - return CA_STATUS_FAILED; - } - - CARemoteEndpoint_t *remoteEndpoint = NULL; + VERIFY_NON_NULL(resourceUri, TAG, "resourceUri is NULL"); + VERIFY_NON_NULL(token, TAG, "Token is NULL"); // allocate & initialize CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t)); CA_MEMORY_ALLOC_CHECK(data); CAAddress_t addr = {0}; - remoteEndpoint = CACreateRemoteEndpointInternal(resourceUri, addr, - CA_ETHERNET | CA_WIFI | CA_EDR | CA_LE); + CARemoteEndpoint_t *remoteEndpoint = + CACreateRemoteEndpointInternal(resourceUri, addr, + CA_ETHERNET | CA_WIFI | CA_EDR | CA_LE); + + // create request info + CARequestInfo_t *reqInfo = (CARequestInfo_t *) OICCalloc(1, sizeof(CARequestInfo_t)); + CA_MEMORY_ALLOC_CHECK(reqInfo); + + // save request info data + reqInfo->method = CA_GET; + reqInfo->info.type = CA_MSG_NONCONFIRM; + + reqInfo->info.token = token; + reqInfo->info.tokenLength = tokenLength; // save data data->type = SEND_TYPE_MULTICAST; data->remoteEndpoint = remoteEndpoint; - CARequestInfo_t *ReqInfo = (CARequestInfo_t *) OICCalloc(1, sizeof(CARequestInfo_t)); - CA_MEMORY_ALLOC_CHECK(ReqInfo); - ReqInfo->method = CA_GET; - ReqInfo->info.token = token; - ReqInfo->info.type = CA_MSG_NONCONFIRM; - data->requestInfo = ReqInfo; + data->requestInfo = reqInfo; data->responseInfo = NULL; data->options = NULL; data->numOptions = 0; - CAHeaderOption_t *temp = NULL; - if (options != NULL && numOptions > 0) + CAHeaderOption_t *headerOption = NULL; + if (NULL != options && numOptions > 0) { // copy data - temp = (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t) * numOptions); - CA_MEMORY_ALLOC_CHECK(temp); - memcpy(temp, options, sizeof(CAHeaderOption_t) * numOptions); + headerOption = (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t) * numOptions); + CA_MEMORY_ALLOC_CHECK(headerOption); + memcpy(headerOption, options, sizeof(CAHeaderOption_t) * numOptions); - data->options = temp; + data->options = headerOption; data->numOptions = numOptions; } CAProcessData(data); + CADestroyRemoteEndpoint(remoteEndpoint); - OICFree(temp); + OICFree(headerOption); OICFree(data); - OICFree(ReqInfo); + OICFree(reqInfo); OIC_LOG(DEBUG, TAG, "OUT"); return CA_STATUS_OK; diff --git a/resource/csdk/connectivity/src/caprotocolmessage.c b/resource/csdk/connectivity/src/caprotocolmessage.c index 1a35c33..5f2fed7 100644 --- a/resource/csdk/connectivity/src/caprotocolmessage.c +++ b/resource/csdk/connectivity/src/caprotocolmessage.c @@ -203,10 +203,11 @@ coap_pdu_t *CAGeneratePduImpl(const code_t code, coap_list_t *options, const CAI if (info.token) { - uint32_t tokenLength = strlen(info.token); - OIC_LOG_V(DEBUG, TAG, "token info : %s, %d", info.token, tokenLength); + uint32_t tokenLength = info.tokenLength; + OIC_LOG_V(DEBUG, TAG, "token info token length: %d, token :", tokenLength); + OIC_LOG_BUFFER(DEBUG, TAG, info.token, tokenLength); - int32_t ret = coap_add_token(pdu, tokenLength, (uint8_t *) info.token); + int32_t ret = coap_add_token(pdu, tokenLength, (unsigned char *) info.token); if (0 == ret) { OIC_LOG(DEBUG, TAG, "cannot add token to request"); @@ -563,18 +564,19 @@ void CAGetInfoFromPDU(const coap_pdu_t *pdu, uint32_t *outCode, CAInfo_t *outInf // set token data if (pdu->hdr->token_length > 0) { - OIC_LOG(DEBUG, TAG, "inside pdu->hdr->token_length"); - outInfo->token = (char *) OICMalloc(pdu->hdr->token_length + 1); - if (outInfo->token == NULL) + OIC_LOG_V(DEBUG, TAG, "inside token length : %d", pdu->hdr->token_length); + outInfo->token = (char *) OICMalloc(pdu->hdr->token_length); + if (NULL == outInfo->token) { - OIC_LOG(DEBUG, TAG, "CAGetInfoFromPDU, Memory allocation failed !"); + OIC_LOG(ERROR, TAG, "memory allocation failed"); OICFree(outInfo->options); return; } memcpy(outInfo->token, pdu->hdr->token, pdu->hdr->token_length); - outInfo->token[pdu->hdr->token_length] = '\0'; } + outInfo->tokenLength = pdu->hdr->token_length; + // set payload data if (NULL != pdu->data) { @@ -609,20 +611,20 @@ exit: return; } -CAResult_t CAGenerateTokenInternal(CAToken_t *token) +CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength) { OIC_LOG(DEBUG, TAG, "CAGenerateTokenInternal IN"); - if (token == NULL) + + if(!token) { - return CA_STATUS_FAILED; + OIC_LOG(ERROR, TAG, "invalid token pointer"); + return CA_STATUS_INVALID_PARAM; } - // memory allocation - char *temp = (char *) OICCalloc(1, (CA_MAX_TOKEN_LEN + 1) * sizeof(char)); - if (temp == NULL) + if((tokenLength > CA_MAX_TOKEN_LEN) || (0 == tokenLength)) { - OIC_LOG(DEBUG, TAG, "CAGenerateTokenInternal, Memory allocation failed !"); - return CA_MEMORY_ALLOC_FAILED; + OIC_LOG(ERROR, TAG, "invalid token length"); + return CA_STATUS_INVALID_PARAM; } if (SEED == 0) @@ -632,26 +634,32 @@ CAResult_t CAGenerateTokenInternal(CAToken_t *token) { OIC_LOG(DEBUG, TAG, "Failed to Create Seed!"); SEED = 0; - OICFree(temp); return CA_STATUS_FAILED; } srandom(SEED); } + // memory allocation + char *temp = (char *) OICCalloc(tokenLength, sizeof(char)); + if (NULL == temp) + { + OIC_LOG(ERROR, TAG, "CAGenerateTokenInternal, Memory allocation failed !"); + return CA_MEMORY_ALLOC_FAILED; + } + // set random byte - uint32_t index; - for (index = 0; index < CA_MAX_TOKEN_LEN; index++) + uint8_t index; + for (index = 0; index < tokenLength; index++) { // use valid characters - temp[index] = (random() % 94 + 33) & 0xFF; + temp[index] = random() & 0x00FF; } - temp[index] = '\0'; // save token *token = temp; - OIC_LOG(DEBUG, TAG, "generate the token!!"); - OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) *token, CA_MAX_TOKEN_LEN); + OIC_LOG_V(DEBUG, TAG, "token info token length: %d, token :", tokenLength); + OIC_LOG_BUFFER(DEBUG, TAG, *token, tokenLength); OIC_LOG(DEBUG, TAG, "CAGenerateTokenInternal OUT"); return CA_STATUS_OK; @@ -660,14 +668,7 @@ CAResult_t CAGenerateTokenInternal(CAToken_t *token) void CADestroyTokenInternal(CAToken_t token) { OIC_LOG(DEBUG, TAG, "CADestroyTokenInternal IN"); - if (token != NULL) - { - OIC_LOG(DEBUG, TAG, "destroy the token!!"); - OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) token, CA_MAX_TOKEN_LEN); - OICFree(token); - token = NULL; - } - + OICFree(token); OIC_LOG(DEBUG, TAG, "CADestroyTokenInternal OUT"); } diff --git a/resource/csdk/connectivity/src/caprotocolmessage_singlethread.c b/resource/csdk/connectivity/src/caprotocolmessage_singlethread.c index 5b41819..d7b1ad1 100644 --- a/resource/csdk/connectivity/src/caprotocolmessage_singlethread.c +++ b/resource/csdk/connectivity/src/caprotocolmessage_singlethread.c @@ -177,8 +177,10 @@ coap_pdu_t *CAGeneratePduImpl(code_t code, coap_list_t *options, if (info.token) { - uint32_t tokenLength = strlen(info.token); - OIC_LOG_V(DEBUG, TAG, "token info : %s, %d", info.token, tokenLength); + uint32_t tokenLength = info.tokenLength; + OIC_LOG_V(DEBUG, TAG, "tokenLength : %d, token : ", tokenLength); + OIC_LOG_BUFFER(DEBUG, TAG, info.token, tokenLength); + int32_t ret = coap_add_token(pdu, tokenLength, (unsigned char *) info.token); if (0 == ret) { @@ -539,18 +541,19 @@ void CAGetInfoFromPDU(const coap_pdu_t *pdu, uint32_t *outCode, CAInfo_t *outInf // set token data if (pdu->hdr->token_length > 0) { - OIC_LOG(DEBUG, TAG, "pdu->hdr->token_length>0"); - outInfo->token = (char *) OICMalloc(pdu->hdr->token_length + 1); - if (outInfo->token == NULL) + OIC_LOG(DEBUG, TAG, "pdu->hdr->token_length > 0"); + outInfo->token = (char *) OICMalloc(pdu->hdr->token_length); + if (NULL == outInfo->token) { - OIC_LOG(ERROR, TAG, "malloc failed"); + OIC_LOG(ERROR, TAG, "memory allocation failed"); OICFree(outInfo->options); return; } memcpy(outInfo->token, pdu->hdr->token, pdu->hdr->token_length); - outInfo->token[pdu->hdr->token_length] = '\0'; } + outInfo->tokenLength = pdu->hdr->token_length; + // set payload data if (NULL != pdu->data) { @@ -585,21 +588,20 @@ exit: return; } -CAResult_t CAGenerateTokenInternal(CAToken_t *token) +CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength) { OIC_LOG(DEBUG, TAG, "IN"); - if (token == NULL) + + if(!token) { - OIC_LOG(ERROR, TAG, "tok null"); - return CA_STATUS_FAILED; + OIC_LOG(ERROR, TAG, "invalid token pointer"); + return CA_STATUS_INVALID_PARAM; } - // memory allocation - char *temp = (char *) OICCalloc(CA_MAX_TOKEN_LEN + 1, sizeof(char)); - if (temp == NULL) + if((tokenLength > CA_MAX_TOKEN_LEN) || (0 == tokenLength)) { - OIC_LOG(ERROR, TAG, "malloc failed"); - return CA_MEMORY_ALLOC_FAILED; + OIC_LOG(ERROR, TAG, "invalid token length"); + return CA_STATUS_INVALID_PARAM; } if (SEED == 0) @@ -609,27 +611,30 @@ CAResult_t CAGenerateTokenInternal(CAToken_t *token) { OIC_LOG(DEBUG, TAG, "Failed to Create Seed!"); SEED = 0; - OICFree(temp); return CA_STATUS_FAILED; } srandom(SEED); } - // set random byte - uint32_t index; + // memory allocation + char *temp = (char *) OICCalloc(tokenLength, sizeof(char)); + if (NULL == temp) + { + OIC_LOG(ERROR, TAG, "Calloc failed"); + return CA_MEMORY_ALLOC_FAILED; + } - for (index = 0; index < CA_MAX_TOKEN_LEN; index++) + // set random byte + for (uint8_t index = 0; index < tokenLength; index++) { - // To create random value 33~126(ASCII code) for valid character - temp[index] = (rand() % 94 + 33) & 0xFF; + temp[index] = rand() & 0x00FF; } - temp[index] = '\0'; // save token *token = temp; - OIC_LOG(DEBUG, TAG, "gen token:"); - OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) *token, CA_MAX_TOKEN_LEN); + OIC_LOG_V(DEBUG, TAG, "gen token tokenLength : %d, token : ", tokenLength); + OIC_LOG_BUFFER(DEBUG, TAG, *token, tokenLength); OIC_LOG(DEBUG, TAG, "OUT"); return CA_STATUS_OK; } @@ -637,12 +642,7 @@ CAResult_t CAGenerateTokenInternal(CAToken_t *token) void CADestroyTokenInternal(CAToken_t token) { OIC_LOG(DEBUG, TAG, "IN"); - if (token != NULL) - { - OIC_LOG_V(DEBUG, TAG, "destroy token(%s)!!", token); - OICFree(token); - } - + OICFree(token); OIC_LOG(DEBUG, TAG, "OUT"); } diff --git a/resource/csdk/connectivity/src/caremotehandler.c b/resource/csdk/connectivity/src/caremotehandler.c index c3d8d6b..c5b04d1 100644 --- a/resource/csdk/connectivity/src/caremotehandler.c +++ b/resource/csdk/connectivity/src/caremotehandler.c @@ -276,15 +276,12 @@ CARemoteEndpoint_t *CACreateRemoteEndpointInternal(const CAURI_t resourceUri, CARequestInfo_t *CACloneRequestInfo(const CARequestInfo_t *rep) { - char *temp = NULL; - int len = 0; - if (rep == NULL) return NULL; // allocate the request info structure. CARequestInfo_t *clone = (CARequestInfo_t *) OICMalloc(sizeof(CARequestInfo_t)); - if (clone == NULL) + if (!clone) { OIC_LOG(DEBUG, TAG, "CACloneRequestInfo Out of memory"); return NULL; @@ -292,24 +289,30 @@ CARequestInfo_t *CACloneRequestInfo(const CARequestInfo_t *rep) memcpy(clone, rep, sizeof(CARequestInfo_t)); - if (rep->info.token != NULL) + if (rep->info.token) { + char *temp = NULL; + // allocate token field - len = strlen(rep->info.token); + int len = rep->info.tokenLength; - temp = (char *) OICCalloc(len + 1, sizeof(char)); - if (temp == NULL) + if (len) { - OIC_LOG(DEBUG, TAG, "CACloneRequestInfo Out of memory"); + temp = (char *) OICCalloc(len, sizeof(char)); + if (!temp) + { + OIC_LOG(DEBUG, TAG, "CACloneRequestInfo Out of memory"); - CADestroyRequestInfoInternal(clone); + CADestroyRequestInfoInternal(clone); - return NULL; + return NULL; + } + memcpy(temp, rep->info.token, len); } - strncpy(temp, rep->info.token, len); // save the token clone->info.token = temp; + clone->info.tokenLength = len; } if (rep->info.options != NULL && rep->info.numOptions > 0) @@ -336,8 +339,10 @@ CARequestInfo_t *CACloneRequestInfo(const CARequestInfo_t *rep) if (rep->info.payload != NULL) { + char *temp = NULL; + // allocate payload field - len = strlen(rep->info.payload); + int len = strlen(rep->info.payload); temp = (char *) OICMalloc(sizeof(char) * (len + 1)); if (temp == NULL) @@ -360,9 +365,6 @@ CARequestInfo_t *CACloneRequestInfo(const CARequestInfo_t *rep) CAResponseInfo_t *CACloneResponseInfo(const CAResponseInfo_t *rep) { - char *temp = NULL; - int len = 0; - if (rep == NULL) return NULL; @@ -375,24 +377,29 @@ CAResponseInfo_t *CACloneResponseInfo(const CAResponseInfo_t *rep) } memcpy(clone, rep, sizeof(CAResponseInfo_t)); - if (rep->info.token != NULL) + if (rep->info.token) { + char *temp = NULL; + // allocate token field - len = strlen(rep->info.token); + int len = rep->info.tokenLength; - temp = (char *) OICCalloc(len + 1, sizeof(char)); - if (temp == NULL) + if (len) { - OIC_LOG(DEBUG, TAG, "CACloneResponseInfo Out of memory"); + temp = (char *) OICCalloc(len, sizeof(char)); + if (!temp) + { + OIC_LOG(DEBUG, TAG, "CACloneResponseInfo Out of memory"); - CADestroyResponseInfoInternal(clone); + CADestroyResponseInfoInternal(clone); - return NULL; + return NULL; + } + memcpy(temp, rep->info.token, len); } - strncpy(temp, rep->info.token, len); - // save the token clone->info.token = temp; + clone->info.tokenLength = len; } if (rep->info.options != NULL && rep->info.numOptions) @@ -419,8 +426,9 @@ CAResponseInfo_t *CACloneResponseInfo(const CAResponseInfo_t *rep) if (rep->info.payload != NULL) { + char *temp = NULL; // allocate payload field - len = strlen(rep->info.payload); + int len = strlen(rep->info.payload); temp = (char *) OICCalloc(len + 1, sizeof(char)); if (temp == NULL) @@ -458,7 +466,7 @@ void CADestroyRequestInfoInternal(CARequestInfo_t *rep) return; // free token field - OICFree((char *) rep->info.token); + OICFree(rep->info.token); // free options field OICFree((CAHeaderOption_t *) rep->info.options); @@ -475,7 +483,7 @@ void CADestroyResponseInfoInternal(CAResponseInfo_t *rep) return; // free token field - OICFree((char *) rep->info.token); + OICFree(rep->info.token); // free options field if (rep->info.options != NULL && rep->info.numOptions) diff --git a/resource/csdk/stack/include/internal/occlientcb.h b/resource/csdk/stack/include/internal/occlientcb.h index 5ee8995..5a4fdaf 100644 --- a/resource/csdk/stack/include/internal/occlientcb.h +++ b/resource/csdk/stack/include/internal/occlientcb.h @@ -51,6 +51,8 @@ typedef struct ClientCB { OCClientContextDeleter deleteCallback; // when a response is recvd with this token, above callback will be invoked CAToken_t token; + // a response is recvd with this token length + uint8_t tokenLength; // Invocation handle tied to original call to OCDoResource() OCDoHandle handle; // This is used to determine if all responses should be consumed or not. @@ -96,7 +98,8 @@ extern struct ClientCB *cbList; */ OCStackResult AddClientCB (ClientCB** clientCB, OCCallbackData* cbData, - CAToken_t * token, OCDoHandle *handle, OCMethod method, + CAToken_t * token, uint8_t tokenLength, + OCDoHandle *handle, OCMethod method, char * requestUri, char * resourceTypeName); /** @ingroup ocstack @@ -124,7 +127,8 @@ void DeleteClientCB(ClientCB *cbNode); * * @return address of the node if found, otherwise NULL */ -ClientCB* GetClientCB(const CAToken_t * token, OCDoHandle handle, const char * requestUri); +ClientCB* GetClientCB(const CAToken_t * token, uint8_t tokenLength, + OCDoHandle handle, const char * requestUri); /** * Inserts a new resource type filter into this cb node. diff --git a/resource/csdk/stack/include/internal/ocobserve.h b/resource/csdk/stack/include/internal/ocobserve.h index 28af694..c7604da 100644 --- a/resource/csdk/stack/include/internal/ocobserve.h +++ b/resource/csdk/stack/include/internal/ocobserve.h @@ -38,6 +38,8 @@ typedef struct ResourceObserver char *query; //token for the observe request CAToken_t token; + //token length for the observe request + uint8_t tokenLength; // Resource handle OCResource *resource; //TODO bundle it in Endpoint structure(address, uri, type, secured) @@ -77,14 +79,15 @@ OCStackResult AddObserver (const char *resUri, const char *query, OCObservationId obsId, CAToken_t *token, + uint8_t tokenLength, OCResource *resHandle, OCQualityOfService qos, const CAAddress_t *addressInfo, CAConnectivityType_t connectivityType); -OCStackResult DeleteObserverUsingToken (CAToken_t * token); +OCStackResult DeleteObserverUsingToken (CAToken_t * token, uint8_t tokenLength); -ResourceObserver* GetObserverUsingToken (const CAToken_t * token); +ResourceObserver* GetObserverUsingToken (const CAToken_t * token, uint8_t tokenLength); ResourceObserver* GetObserverUsingId (const OCObservationId observeId); diff --git a/resource/csdk/stack/include/internal/ocserverrequest.h b/resource/csdk/stack/include/internal/ocserverrequest.h index 8c28fe7..7809828 100644 --- a/resource/csdk/stack/include/internal/ocserverrequest.h +++ b/resource/csdk/stack/include/internal/ocserverrequest.h @@ -51,8 +51,10 @@ typedef struct OCServerRequest CAAddress_t addressInfo; /** Connectivity of the endpoint**/ CAConnectivityType_t connectivityType; - // token for the observe request + // token for the request CAToken_t requestToken; + // token length the request + uint8_t tokenLength; // The ID of CoAP pdu //Kept in uint16_t coapID; //CoAP uint8_t delayedResNeeded; @@ -79,7 +81,7 @@ typedef struct OCServerResponse { OCRequestHandle requestHandle; } OCServerResponse; -OCServerRequest * GetServerRequestUsingToken (const CAToken_t token); +OCServerRequest * GetServerRequestUsingToken (const CAToken_t token, uint8_t tokenLength); OCServerRequest * GetServerRequestUsingHandle (const OCServerRequest * handle); @@ -91,6 +93,7 @@ OCStackResult AddServerRequest (OCServerRequest ** request, uint16_t coapID, OCQualityOfService qos, char * query, OCHeaderOption * rcvdVendorSpecificHeaderOptions, char * reqJSONPayload, CAToken_t * requestToken, + uint8_t tokenLength, char * resourceUrl, size_t reqTotalSize, CAAddress_t *addressInfo, CAConnectivityType_t connectivityType); diff --git a/resource/csdk/stack/include/internal/ocstackinternal.h b/resource/csdk/stack/include/internal/ocstackinternal.h index 61a7245..f7204dc 100644 --- a/resource/csdk/stack/include/internal/ocstackinternal.h +++ b/resource/csdk/stack/include/internal/ocstackinternal.h @@ -95,7 +95,7 @@ typedef struct //token for the observe request CAToken_t requestToken; - + uint8_t tokenLength; //token length // The ID of CoAP pdu uint16_t coapID; uint8_t delayedResNeeded; diff --git a/resource/csdk/stack/src/occlientcb.c b/resource/csdk/stack/src/occlientcb.c index 9c8e111..048fd05 100644 --- a/resource/csdk/stack/src/occlientcb.c +++ b/resource/csdk/stack/src/occlientcb.c @@ -36,10 +36,11 @@ static OCMulticastNode * mcPresenceNodes = NULL; OCStackResult AddClientCB (ClientCB** clientCB, OCCallbackData* cbData, - CAToken_t * token, OCDoHandle *handle, OCMethod method, + CAToken_t * token, uint8_t tokenLength, + OCDoHandle *handle, OCMethod method, char * requestUri, char * resourceTypeName) { - if(!clientCB || !cbData || !token || !handle || !requestUri) + if(!clientCB || !cbData || !handle || !requestUri || tokenLength > CA_MAX_TOKEN_LEN) { return OC_STACK_INVALID_PARAM; } @@ -49,7 +50,7 @@ AddClientCB (ClientCB** clientCB, OCCallbackData* cbData, #ifdef WITH_PRESENCE if(method == OC_REST_PRESENCE) { // Retrieve the presence callback structure for this specific requestUri. - cbNode = GetClientCB(NULL, NULL, requestUri); + cbNode = GetClientCB(NULL, 0, NULL, requestUri); } #endif // WITH_PRESENCE @@ -69,6 +70,7 @@ AddClientCB (ClientCB** clientCB, OCCallbackData* cbData, //Note: token memory is allocated in the caller OCDoResource //but freed in DeleteClientCB cbNode->token = *token; + cbNode->tokenLength = tokenLength; cbNode->handle = *handle; cbNode->method = method; cbNode->sequenceNumber = 0; @@ -123,8 +125,8 @@ void DeleteClientCB(ClientCB * cbNode) if(cbNode) { LL_DELETE(cbList, cbNode); OC_LOG(INFO, TAG, PCF("deleting tokens")); + OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)cbNode->token, cbNode->tokenLength); CADestroyToken (cbNode->token); - OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)cbNode->token, CA_MAX_TOKEN_LEN); OCFree(cbNode->handle); OCFree(cbNode->requestUri); if(cbNode->deleteCallback) @@ -155,29 +157,40 @@ void DeleteClientCB(ClientCB * cbNode) } } -ClientCB* GetClientCB(const CAToken_t * token, OCDoHandle handle, const char * requestUri) +ClientCB* GetClientCB(const CAToken_t * token, uint8_t tokenLength, + OCDoHandle handle, const char * requestUri) { + ClientCB* out = NULL; - if(token) { - LL_FOREACH(cbList, out) { + + if(token && *token && + tokenLength <= CA_MAX_TOKEN_LEN && tokenLength > 0) + { + LL_FOREACH(cbList, out) + { OC_LOG(INFO, TAG, PCF("comparing tokens")); - OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)*token, CA_MAX_TOKEN_LEN); - OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)out->token, CA_MAX_TOKEN_LEN); - if(memcmp(out->token, *token, CA_MAX_TOKEN_LEN) == 0) + OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)*token, tokenLength); + OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)out->token, tokenLength); + if(memcmp(out->token, *token, tokenLength) == 0) { return out; } } } - else if(handle) { - LL_FOREACH(cbList, out) { - if(out->handle == handle) { + else if(handle) + { + LL_FOREACH(cbList, out) + { + if(out->handle == handle) + { return out; } } } - else if(requestUri) { - LL_FOREACH(cbList, out) { + else if(requestUri) + { + LL_FOREACH(cbList, out) + { if(out->requestUri && strcmp(out->requestUri, requestUri ) == 0) { return out; diff --git a/resource/csdk/stack/src/ocobserve.c b/resource/csdk/stack/src/ocobserve.c index ebed22d..440b3ef 100644 --- a/resource/csdk/stack/src/ocobserve.c +++ b/resource/csdk/stack/src/ocobserve.c @@ -122,7 +122,8 @@ OCStackResult SendAllObserverNotification (OCMethod method, OCResource *resPtr, result = AddServerRequest(&request, 0, 0, 0, 1, OC_REST_GET, 0, resPtr->sequenceNum, qos, resourceObserver->query, NULL, NULL, - &resourceObserver->token, resourceObserver->resUri, 0, + &resourceObserver->token, resourceObserver->tokenLength, + resourceObserver->resUri, 0, &(resourceObserver->addressInfo), resourceObserver->connectivityType); if(request) @@ -157,7 +158,8 @@ OCStackResult SendAllObserverNotification (OCMethod method, OCResource *resPtr, result = AddServerRequest(&request, 0, 0, 0, 1, OC_REST_GET, 0, resPtr->sequenceNum, qos, resourceObserver->query, NULL, NULL, - &resourceObserver->token, resourceObserver->resUri, 0, + &resourceObserver->token, resourceObserver->tokenLength, + resourceObserver->resUri, 0, &(resourceObserver->addressInfo), resourceObserver->connectivityType); if(result == OC_STACK_OK) @@ -226,7 +228,7 @@ OCStackResult SendListObserverNotification (OCResource * resource, result = AddServerRequest(&request, 0, 0, 0, 1, OC_REST_GET, 0, resource->sequenceNum, qos, observation->query, - NULL, NULL, &observation->token, + NULL, NULL, &observation->token, observation->tokenLength, observation->resUri, 0, &(observation->addressInfo), observation->connectivityType); @@ -308,6 +310,7 @@ OCStackResult AddObserver (const char *resUri, const char *query, OCObservationId obsId, CAToken_t *token, + uint8_t tokenLength, OCResource *resHandle, OCQualityOfService qos, const CAAddress_t *addressInfo, @@ -345,12 +348,15 @@ OCStackResult AddObserver (const char *resUri, VERIFY_NON_NULL (obsNode->query); memcpy (obsNode->query, query, strlen(query)+1); } - - obsNode->token = (CAToken_t)OCMalloc(CA_MAX_TOKEN_LEN+1); - VERIFY_NON_NULL (obsNode->token); - memcpy(obsNode->token, *token, CA_MAX_TOKEN_LEN); - obsNode->token[CA_MAX_TOKEN_LEN]='\0'; - + // If tokenLength is zero, the return value depends on the + // particular library implementation (it may or may not be a null pointer). + if(tokenLength) + { + obsNode->token = (CAToken_t)OCMalloc(tokenLength); + VERIFY_NON_NULL (obsNode->token); + memcpy(obsNode->token, *token, tokenLength); + } + obsNode->tokenLength = tokenLength; obsNode->addressInfo = *addressInfo; obsNode->connectivityType = connectivityType; obsNode->resource = resHandle; @@ -386,7 +392,7 @@ ResourceObserver* GetObserverUsingId (const OCObservationId observeId) return NULL; } -ResourceObserver* GetObserverUsingToken (const CAToken_t * token) +ResourceObserver* GetObserverUsingToken (const CAToken_t * token, uint8_t tokenLength) { ResourceObserver *out = NULL; @@ -395,9 +401,9 @@ ResourceObserver* GetObserverUsingToken (const CAToken_t * token) LL_FOREACH (serverObsList, out) { OC_LOG(INFO, TAG,PCF("comparing tokens")); - OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)token, CA_MAX_TOKEN_LEN); - OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)out->token, CA_MAX_TOKEN_LEN); - if((memcmp(out->token, *token, CA_MAX_TOKEN_LEN) == 0)) + OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)token, tokenLength); + OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)out->token, tokenLength); + if((memcmp(out->token, *token, tokenLength) == 0)) { return out; } @@ -407,7 +413,7 @@ ResourceObserver* GetObserverUsingToken (const CAToken_t * token) return NULL; } -OCStackResult DeleteObserverUsingToken (CAToken_t * token) +OCStackResult DeleteObserverUsingToken (CAToken_t * token, uint8_t tokenLength) { if(!token || !*token) { @@ -416,11 +422,11 @@ OCStackResult DeleteObserverUsingToken (CAToken_t * token) ResourceObserver *obsNode = NULL; - obsNode = GetObserverUsingToken (token); + obsNode = GetObserverUsingToken (token, tokenLength); if (obsNode) { OC_LOG_V(INFO, TAG, PCF("deleting tokens")); - OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)obsNode->token, CA_MAX_TOKEN_LEN); + OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)obsNode->token, tokenLength); LL_DELETE (serverObsList, obsNode); OCFree(obsNode->resUri); OCFree(obsNode->query); @@ -439,7 +445,7 @@ void DeleteObserverList() { if(out) { - DeleteObserverUsingToken (&(out->token)); + DeleteObserverUsingToken (&(out->token), out->tokenLength); } } serverObsList = NULL; diff --git a/resource/csdk/stack/src/ocresource.c b/resource/csdk/stack/src/ocresource.c index 9ab92ae..263c085 100644 --- a/resource/csdk/stack/src/ocresource.c +++ b/resource/csdk/stack/src/ocresource.c @@ -762,7 +762,7 @@ HandleResourceWithEntityHandler (OCServerRequest *request, result = AddObserver ((const char*)(request->resourceUrl), (const char *)(request->query), - ehRequest.obsInfo.obsId, &request->requestToken, + ehRequest.obsInfo.obsId, &request->requestToken, request->tokenLength, resource, request->qos, &request->addressInfo, request->connectivityType); @@ -789,7 +789,7 @@ HandleResourceWithEntityHandler (OCServerRequest *request, { OC_LOG(INFO, TAG, PCF("Deregistering observation requested")); - resObs = GetObserverUsingToken (&request->requestToken); + resObs = GetObserverUsingToken (&request->requestToken, request->tokenLength); if (NULL == resObs) { @@ -801,7 +801,7 @@ HandleResourceWithEntityHandler (OCServerRequest *request, ehRequest.obsInfo.obsId = resObs->observeId; ehFlag = (OCEntityHandlerFlag)(ehFlag | OC_OBSERVE_FLAG); - result = DeleteObserverUsingToken (&request->requestToken); + result = DeleteObserverUsingToken (&request->requestToken, request->tokenLength); if(result == OC_STACK_OK) { diff --git a/resource/csdk/stack/src/ocserverrequest.c b/resource/csdk/stack/src/ocserverrequest.c index 67ab53d..4925258 100644 --- a/resource/csdk/stack/src/ocserverrequest.c +++ b/resource/csdk/stack/src/ocserverrequest.c @@ -38,7 +38,7 @@ static struct OCServerRequest * serverRequestList = NULL; static struct OCServerResponse * serverResponseList = NULL; -OCServerRequest * GetServerRequestUsingToken (const CAToken_t token) +OCServerRequest * GetServerRequestUsingToken (const CAToken_t token, uint8_t tokenLength) { if(!token) { @@ -50,9 +50,9 @@ OCServerRequest * GetServerRequestUsingToken (const CAToken_t token) LL_FOREACH (serverRequestList, out) { OC_LOG(INFO, TAG,PCF("comparing tokens")); - OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)token, CA_MAX_TOKEN_LEN); - OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)out->requestToken, CA_MAX_TOKEN_LEN); - if(memcmp(out->requestToken, token, CA_MAX_TOKEN_LEN) == 0) + OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)token, tokenLength); + OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)out->requestToken, tokenLength); + if(memcmp(out->requestToken, token, tokenLength) == 0) { return out; } @@ -95,6 +95,7 @@ OCStackResult AddServerRequest (OCServerRequest ** request, uint16_t coapID, OCQualityOfService qos, char * query, OCHeaderOption * rcvdVendorSpecificHeaderOptions, char * reqJSONPayload, CAToken_t * requestToken, + uint8_t tokenLength, char * resourceUrl, size_t reqTotalSize, CAAddress_t *addressInfo, CAConnectivityType_t connectivityType) { @@ -141,11 +142,17 @@ OCStackResult AddServerRequest (OCServerRequest ** request, uint16_t coapID, serverRequest->requestComplete = 0; if(requestToken) { - serverRequest->requestToken = (CAToken_t)OCMalloc(CA_MAX_TOKEN_LEN+1); - VERIFY_NON_NULL (serverRequest->requestToken); - memcpy(serverRequest->requestToken, *requestToken, CA_MAX_TOKEN_LEN); - serverRequest->requestToken[CA_MAX_TOKEN_LEN]='\0'; + // If tokenLength is zero, the return value depends on the + // particular library implementation (it may or may not be a null pointer). + if (tokenLength) + { + serverRequest->requestToken = (CAToken_t) OCMalloc(tokenLength); + VERIFY_NON_NULL(serverRequest->requestToken); + memcpy(serverRequest->requestToken, *requestToken, tokenLength); + } + } + serverRequest->tokenLength = tokenLength; if(resourceUrl) { @@ -336,9 +343,10 @@ OCStackResult HandleSingleResponse(OCEntityHandlerResponse * ehResponse) break; } responseInfo.info.type = qualityOfServiceToMessageType(serverRequest->qos); - char token[CA_MAX_TOKEN_LEN + 1] = {}; + char token[CA_MAX_TOKEN_LEN] = {}; responseInfo.info.token = token; - memcpy(responseInfo.info.token, serverRequest->requestToken, CA_MAX_TOKEN_LEN); + memcpy(responseInfo.info.token, serverRequest->requestToken, serverRequest->tokenLength); + responseInfo.info.tokenLength = serverRequest->tokenLength; if(serverRequest->observeResult == OC_STACK_OK) { diff --git a/resource/csdk/stack/src/ocstack.c b/resource/csdk/stack/src/ocstack.c index f862f0a..5eec321 100644 --- a/resource/csdk/stack/src/ocstack.c +++ b/resource/csdk/stack/src/ocstack.c @@ -470,7 +470,7 @@ static OCStackResult HandlePresenceResponse(const CARemoteEndpoint_t* endPoint, snprintf(fullUri, MAX_URI_LENGTH, "coap://%s:%u%s", ipAddress, endPoint->addressInfo.IP.port, OC_PRESENCE_URI); - cbNode = GetClientCB(NULL, NULL, fullUri); + cbNode = GetClientCB(NULL, 0, NULL, fullUri); if(cbNode) { @@ -479,7 +479,7 @@ static OCStackResult HandlePresenceResponse(const CARemoteEndpoint_t* endPoint, else { snprintf(fullUri, MAX_URI_LENGTH, "%s%s", OC_MULTICAST_IP, endPoint->resourceUri); - cbNode = GetClientCB(NULL, NULL, fullUri); + cbNode = GetClientCB(NULL, 0, NULL, fullUri); if(cbNode) { multicastPresenceSubscribe = 1; @@ -689,7 +689,8 @@ void HandleCAResponses(const CARemoteEndpoint_t* endPoint, const CAResponseInfo_ return; } - ClientCB *cbNode = GetClientCB(&(responseInfo->info.token), NULL, NULL); + ClientCB *cbNode = GetClientCB(&(responseInfo->info.token), + responseInfo->info.tokenLength, NULL, NULL); if (cbNode) { @@ -875,17 +876,20 @@ void HandleCARequests(const CARemoteEndpoint_t* endPoint, const CARequestInfo_t* } } - OC_LOG_V(INFO, TAG, "HandleCARequests: CA token length = %d", CA_MAX_TOKEN_LEN); - OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)requestInfo->info.token, CA_MAX_TOKEN_LEN); + OC_LOG_V(INFO, TAG, "HandleCARequests: CA token length = %d", + requestInfo->info.tokenLength); + OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)requestInfo->info.token, + requestInfo->info.tokenLength); - serverRequest.requestToken = (CAToken_t)OCMalloc(CA_MAX_TOKEN_LEN+1); + serverRequest.requestToken = (CAToken_t)OCMalloc(requestInfo->info.tokenLength); + serverRequest.tokenLength = requestInfo->info.tokenLength; // Module Name if (!serverRequest.requestToken) { OC_LOG(FATAL, TAG, "Server Request Token is NULL"); return; } - memcpy(serverRequest.requestToken, requestInfo->info.token, CA_MAX_TOKEN_LEN); + memcpy(serverRequest.requestToken, requestInfo->info.token, requestInfo->info.tokenLength); if (requestInfo->info.type == CA_MSG_CONFIRM) { @@ -960,7 +964,8 @@ OCStackResult HandleStackRequests(OCServerProtocolRequest * protocolRequest) return OC_STACK_INVALID_PARAM; } - OCServerRequest * request = GetServerRequestUsingToken(protocolRequest->requestToken); + OCServerRequest * request = GetServerRequestUsingToken(protocolRequest->requestToken, + protocolRequest->tokenLength); if(!request) { OC_LOG(INFO, TAG, PCF("This is a new Server Request")); @@ -970,6 +975,7 @@ OCStackResult HandleStackRequests(OCServerProtocolRequest * protocolRequest) protocolRequest->observationOption, protocolRequest->qos, protocolRequest->query, protocolRequest->rcvdVendorSpecificHeaderOptions, protocolRequest->reqJSONPayload, &protocolRequest->requestToken, + protocolRequest->tokenLength, protocolRequest->resourceUrl,protocolRequest->reqTotalSize, &protocolRequest->addressInfo, protocolRequest->connectivityType); if (OC_STACK_OK != result) @@ -1316,6 +1322,7 @@ OCStackResult OCDoResource(OCDoHandle *handle, OCMethod method, const char *requ CARemoteEndpoint_t* endpoint = NULL; CAResult_t caResult; CAToken_t token = NULL; + uint8_t tokenLength = CA_MAX_TOKEN_LEN; OCDoHandle resHandle = NULL; CAInfo_t requestData ={}; CARequestInfo_t requestInfo ={}; @@ -1459,7 +1466,7 @@ OCStackResult OCDoResource(OCDoHandle *handle, OCMethod method, const char *requ } // create token - caResult = CAGenerateToken(&token); + caResult = CAGenerateToken(&token, tokenLength); if (caResult != CA_STATUS_OK) { OC_LOG(ERROR, TAG, PCF("CAGenerateToken error")); @@ -1469,6 +1476,7 @@ OCStackResult OCDoResource(OCDoHandle *handle, OCMethod method, const char *requ requestData.type = qualityOfServiceToMessageType(qos); requestData.token = token; + requestData.tokenLength = tokenLength; if ((method == OC_REST_OBSERVE) || (method == OC_REST_OBSERVE_ALL)) { result = CreateObserveHeaderOption (&(requestData.options), options, @@ -1533,7 +1541,7 @@ OCStackResult OCDoResource(OCDoHandle *handle, OCMethod method, const char *requ goto exit; } - if((result = AddClientCB(&clientCB, cbData, &token, &resHandle, method, + if((result = AddClientCB(&clientCB, cbData, &token, tokenLength, &resHandle, method, requestUri, resourceType)) != OC_STACK_OK) { result = OC_STACK_NO_MEMORY; @@ -1617,7 +1625,7 @@ OCStackResult OCCancel(OCDoHandle handle, OCQualityOfService qos, OCHeaderOption OC_LOG(INFO, TAG, PCF("Entering OCCancel")); - ClientCB *clientCB = GetClientCB(NULL, handle, NULL); + ClientCB *clientCB = GetClientCB(NULL, 0, handle, NULL); if(clientCB) { @@ -1637,6 +1645,7 @@ OCStackResult OCCancel(OCDoHandle handle, OCQualityOfService qos, OCHeaderOption memset(&requestData, 0, sizeof(CAInfo_t)); requestData.type = qualityOfServiceToMessageType(qos); requestData.token = clientCB->token; + requestData.tokenLength = clientCB->tokenLength; if (CreateObserveHeaderOption (&(requestData.options), options, numOptions, OC_OBSERVE_DEREGISTER) != OC_STACK_OK) { @@ -1656,6 +1665,7 @@ OCStackResult OCCancel(OCDoHandle handle, OCQualityOfService qos, OCHeaderOption { ret = OC_STACK_OK; } + break; #ifdef WITH_PRESENCE case OC_REST_PRESENCE: FindAndDeleteClientCB(clientCB); @@ -1833,6 +1843,7 @@ OCStackResult OCProcess() */ OCStackResult OCStartPresence(const uint32_t ttl) { + uint8_t tokenLength = CA_MAX_TOKEN_LEN; OCChangeResourceProperty( &(((OCResource *)presenceResource.handle)->resourceProperties), OC_ACTIVE, 1); @@ -1851,7 +1862,7 @@ OCStackResult OCStartPresence(const uint32_t ttl) addressInfo.IP.port = OC_MULTICAST_PORT; CAToken_t caToken = NULL; - CAResult_t caResult = CAGenerateToken(&caToken); + CAResult_t caResult = CAGenerateToken(&caToken, tokenLength); if (caResult != CA_STATUS_OK) { OC_LOG(ERROR, TAG, PCF("CAGenerateToken error")); @@ -1861,7 +1872,7 @@ OCStackResult OCStartPresence(const uint32_t ttl) CAConnectivityType_t connType; OCToCAConnectivityType(OC_ALL, &connType ); - AddObserver(OC_PRESENCE_URI, NULL, 0, &caToken, + AddObserver(OC_PRESENCE_URI, NULL, 0, &caToken, tokenLength, (OCResource *)presenceResource.handle, OC_LOW_QOS, &addressInfo, connType); CADestroyToken(caToken); -- 2.7.4