// check return value when address is NULL as multicast TEST(SendResponseTest, DISABLED_TC_20_Negative_01) { addr = NULL; CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, 0, &tempRep); memset(&responseData, 0, sizeof(CAInfo_t)); responseData.type = CA_MSG_NONCONFIRM; responseData.messageId = 1; responseData.payload = (CAPayload_t)malloc(sizeof("response payload")); EXPECT_TRUE(responseData.payload != NULL); if(!responseData.payload) { CADestroyEndpoint(tempRep); return; } memcpy(responseData.payload, "response payload", sizeof("response payload")); responseData.payloadSize = sizeof("response payload"); CAGenerateToken(&tempToken, tokenLength); requestData.token = tempToken; requestData.tokenLength = tokenLength; memset(&responseInfo, 0, sizeof(CAResponseInfo_t)); responseInfo.result = CA_CONTENT; responseInfo.info = responseData; EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo)); CADestroyToken(tempToken); CADestroyEndpoint(tempRep); free (responseData.payload); tempRep = NULL; }
TEST_F(CATests, SendResponseTestWithInvalidCode) { EXPECT_EQ(CA_STATUS_OK, CASelectNetwork(CA_ADAPTER_IP)); addr = (char *) ADDRESS; CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep); memset(&responseData, 0, sizeof(CAInfo_t)); responseData.type = CA_MSG_RESET; responseData.messageId = 1; responseData.payload = (CAPayload_t)malloc(sizeof("response payload")); EXPECT_TRUE(responseData.payload != NULL); if (responseData.payload) { CAGenerateToken(&tempToken, tokenLength); requestData.token = tempToken; requestData.tokenLength = tokenLength; memset(&responseInfo, 0, sizeof(CAResponseInfo_t)); responseInfo.result = CA_CONTENT; responseInfo.info = responseData; EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo)); CADestroyToken(tempToken); CADestroyEndpoint(tempRep); free(responseData.payload); tempRep = NULL; } }
// check return value NULL is passed instead of a valid CAResponseInfo_t address TEST_F(CATests, SendResponseTest) { addr = (char *) ADDRESS; CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep); EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendResponse(tempRep, NULL)); CADestroyEndpoint(tempRep); tempRep = NULL; }
/** * Ensure no accept header option is included when sending responses and add routing info to * outgoing response. * * @param object CA remote endpoint. * @param requestInfo CA request info. * * @return ::OC_STACK_OK on success, some other value upon failure. */ static OCStackResult OCSendResponse(const CAEndpoint_t *object, CAResponseInfo_t *responseInfo) { #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP) // Add route info in RM option. OCStackResult rmResult = RMAddInfo(object->routeData, responseInfo, false, NULL); if(OC_STACK_OK != rmResult) { OIC_LOG(ERROR, TAG, "Add option failed"); return rmResult; } #endif // Do not include the accept header option responseInfo->info.acceptFormat = CA_FORMAT_UNDEFINED; CAResult_t result = CASendResponse(object, responseInfo); if(CA_STATUS_OK != result) { OIC_LOG_V(ERROR, TAG, "CASendResponse failed with CA error %u", result); return CAResultToOCResult(result); } return OC_STACK_OK; }
void SendResponse(CAEndpoint_t *endpoint, const CAInfo_t* info) { char buf[MAX_BUF_LEN] = {0}; Serial.println("============"); Serial.println("Select Message Type"); Serial.println("CON: 0"); Serial.println("NON: 1"); Serial.println("ACK: 2"); Serial.println("RESET: 3"); size_t len = 0; int messageType = 0; while(1) { GetData(buf, sizeof(buf), &len); if(len >= 1) { messageType = buf[0] - '0'; if (messageType >= 0 && messageType <= 3) { break; } } Serial.println("Invalid type"); } int respCode = 0; if(messageType != 3) { Serial.println("============"); Serial.println("Enter Resp Code:"); Serial.println("For Ex: Empty : 0"); Serial.println("Created: 201"); Serial.println("Deleted: 202"); Serial.println("Valid : 203"); Serial.println("Changed: 204"); Serial.println("Content: 205"); Serial.println("BadReq : 400"); Serial.println("BadOpt : 402"); Serial.println("NotFnd : 404"); Serial.println("Internal Srv Err:500"); Serial.println("Timeout: 504"); while(1) { GetData(buf, sizeof(buf), &len); if(len >= 1) { respCode = atoi(buf); if (respCode >= 0 && respCode <= 504) { break; } } Serial.println("Invalid response"); } } CAInfo_t responseData = {CA_MSG_RESET}; responseData.type = static_cast<CAMessageType_t>(messageType); responseData.messageId = (info != NULL) ? info->messageId : 0; responseData.resourceUri = (info != NULL) ? info->resourceUri : 0; if(messageType != 3) { responseData.token = (info != NULL) ? info->token : NULL; responseData.tokenLength = (info != NULL) ? info->tokenLength : 0; responseData.payload = reinterpret_cast<CAPayload_t>(const_cast<char*>("response payload")); responseData.payloadSize = strlen((const char *) responseData.payload); } CAResponseInfo_t responseInfo = {CA_BAD_REQ, {CA_MSG_RESET}}; responseInfo.result = static_cast<CAResponseResult_t>(respCode); responseInfo.info = responseData; // send request (transportType from remoteEndpoint of request Info) CAResult_t res = CASendResponse(endpoint, &responseInfo); if(res != CA_STATUS_OK) { Serial.println("Snd Resp error"); } else { Serial.println("Snd Resp success"); } Serial.println("============"); }
JNIEXPORT void JNICALL Java_org_iotivity_ca_service_RMInterface_RMSendResponse(JNIEnv *env, jobject obj, jint selectedNetwork, jint isSecured, jint msgType, jint responseValue) { LOGI("RMSendResponse"); if (!env || !obj) { LOGI("Invalid input parameter"); return; } LOGI("selectedNetwork - %d", selectedNetwork); CAResult_t res = get_network_type(selectedNetwork); if (CA_STATUS_OK != res) { LOGE("Not supported network type"); return; } if (NULL == g_clientEndpoint) { LOGE("No Request received"); return; } CAMessageType_t messageType = msgType; CAInfo_t responseData = { 0 }; responseData.type = messageType; responseData.messageId = g_clientMsgId; responseData.resourceUri = (CAURI_t)g_resourceUri; CAResponseInfo_t responseInfo = { 0 }; if (msgType != CA_MSG_RESET) { responseData.token = g_clientToken; responseData.tokenLength = g_clientTokenLength; responseInfo.result = responseValue; if (1 == isSecured) { uint32_t length = strlen(SECURE_INFO_DATA) + strlen(g_resourceUri) + 1; responseData.payload = (CAPayload_t) malloc(length); sprintf((char *) responseData.payload, SECURE_INFO_DATA, g_resourceUri, g_localSecurePort); responseData.payloadSize = length; } else { uint32_t length = strlen(NORMAL_INFO_DATA) + strlen(g_resourceUri) + 1; responseData.payload = (CAPayload_t) malloc(length); sprintf((char *) responseData.payload, NORMAL_INFO_DATA, g_resourceUri); responseData.payloadSize = length; } } //msgType is RESET else { responseInfo.result = CA_EMPTY; } responseInfo.info = responseData; // send response res = CASendResponse(g_clientEndpoint, &responseInfo); if (CA_STATUS_OK != res) { LOGE("Could not send response"); } // destroy token CADestroyToken(g_clientToken); g_clientToken = NULL; g_clientTokenLength = 0; // destroy remote endpoint CADestroyEndpoint(g_clientEndpoint); g_clientEndpoint = NULL; }
void send_response(const CAEndpoint_t *endpoint, const CAInfo_t *info) { printf("entering send_response\n"); printf("\n=============================================\n"); printf("\tselect message type\n"); printf("CON : 0\n"); printf("NON : 1\n"); printf("ACK : 2\n"); printf("RESET : 3\n"); printf("select : "); char buf[MAX_BUF_LEN] = { 0 }; if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN)) { return; } int messageType = buf[0] - '0'; if (0 > messageType || 3 < messageType) { printf("Invalid message type\n"); return; } int responseCode = 0 ; char responseCodeBuf[MAX_BUF_LEN] = { 0 }; if (CA_MSG_RESET != messageType) { printf("\n=============================================\n"); printf("\tselect response code\n"); printf("EMPTY : 0\n"); printf("SUCCESS : 200\n"); printf("CREATED : 201\n"); printf("DELETED : 202\n"); printf("VALID : 203\n"); printf("CHANGED : 204\n"); printf("CONTENT : 205\n"); printf("BAD_REQ : 400\n"); printf("BAD_OPT : 402\n"); printf("NOT_FOUND : 404\n"); printf("INTERNAL_SERVER_ERROR : 500\n"); printf("RETRANSMIT_TIMEOUT : 504\n"); printf("select : "); if (CA_STATUS_OK != get_input_data(responseCodeBuf, MAX_BUF_LEN)) { return; } responseCode = atoi(responseCodeBuf); } // create response data uint16_t messageId = (info != NULL) ? info->messageId : 0; CAURI_t resourceUri = (info != NULL) ? info->resourceUri : 0; CAInfo_t responseData = { .type = messageType, .messageId = messageId, .token = NULL, .tokenLength = 0, .options = NULL, .numOptions = 0, .payload = NULL, .payloadSize = 0, .resourceUri = resourceUri }; if(CA_MSG_RESET != messageType) { responseData.token = (info != NULL) ? info->token : NULL; responseData.tokenLength = (info != NULL) ? info->tokenLength : 0; if (endpoint->flags & CA_SECURE) { if(!responseData.resourceUri) { printf("resourceUri not available in SECURE\n"); return; } printf("Sending response on secure communication\n"); uint32_t length = sizeof(SECURE_INFO_DATA) + strlen(responseData.resourceUri); responseData.payload = (CAPayload_t) calloc(length, sizeof(char)); if (NULL == responseData.payload) { printf("Memory allocation fail\n"); return; } snprintf((char *) responseData.payload, length, SECURE_INFO_DATA, (const char *) responseData.resourceUri, g_local_secure_port); responseData.payloadSize = length; } else { printf("Sending response on non-secure communication\n"); bool useBigPayload = select_payload_type(); if (useBigPayload) { size_t payloadLength = 0; CAPayload_t binaryPayload = get_binary_payload(&payloadLength); if (NULL == binaryPayload) { free(binaryPayload); return; } responseData.payload = (CAPayload_t) malloc(payloadLength); if (NULL == responseData.payload) { printf("Memory allocation failed!"); free(binaryPayload); return; } memcpy(responseData.payload, binaryPayload, payloadLength); responseData.payloadSize = payloadLength; // memory free free(binaryPayload); } else { if(!responseData.resourceUri) { printf("resourceUri not available in NON-SECURE\n"); return; } uint32_t length = sizeof(NORMAL_INFO_DATA) + strlen(responseData.resourceUri); responseData.payload = (CAPayload_t) calloc(length, sizeof(char)); if (NULL == responseData.payload) { printf("Memory allocation fail\n"); return; } snprintf((char *) responseData.payload, length, NORMAL_INFO_DATA, (const char *) responseData.resourceUri); responseData.payloadSize = length; } } } CAResponseInfo_t responseInfo = { .result = responseCode, .info = responseData }; // send response (transportType from remoteEndpoint of request Info) CAResult_t res = CASendResponse(endpoint, &responseInfo); if (CA_STATUS_OK != res) { printf("Send response error\n"); } else { printf("Send response success\n"); } if (responseData.payload) { free(responseData.payload); } printf("=============================================\n"); } int get_secure_information(CAPayload_t payLoad) { printf("Entering get_secure_information\n"); if (!payLoad) { printf("Payload is NULL\n"); return -1; } char *subString = NULL; if (NULL == (subString = strstr((const char *) payLoad, "\"sec\":1"))) { printf("This is not secure resource\n"); return -1; } if (NULL == (subString = strstr((const char *) payLoad, "\"port\":"))) { printf("This secure resource does not have port information\n"); return -1; } char *startPos = strstr(subString, ":"); if (!startPos) { printf("Parsing failed !\n"); return -1; } char *endPos = strstr(startPos, "}"); if (!endPos) { printf("Parsing failed !\n"); return -1; } char portStr[6] = {0}; OICStrcpyPartial(portStr, sizeof(portStr), startPos + 1, (endPos - 1) - startPos); printf("secured port is: %s\n", portStr); return atoi(portStr); }
void send_response(const CAEndpoint_t *endpoint, const CAInfo_t *info) { printf("entering send_response\n"); printf("\n=============================================\n"); printf("\tselect message type\n"); printf("CON : 0\n"); printf("NON : 1\n"); printf("ACK : 2\n"); printf("RESET : 3\n"); printf("select : "); char buf[MAX_BUF_LEN] = { 0 }; if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN)) { return; } int messageType = buf[0] - '0'; int responseCode = 0 ; char responseCodeBuf[MAX_BUF_LEN] = { 0 }; if (CA_MSG_RESET != messageType) { printf("\n=============================================\n"); printf("\tselect response code\n"); printf("EMPTY : 0\n"); printf("CREATED : 201\n"); printf("DELETED : 202\n"); printf("VALID : 203\n"); printf("CHANGED : 204\n"); printf("CONTENT : 205\n"); printf("BAD_REQ : 400\n"); printf("BAD_OPT : 402\n"); printf("NOT_FOUND : 404\n"); printf("INTERNAL_SERVER_ERROR : 500\n"); printf("RETRANSMIT_TIMEOUT : 504\n"); printf("select : "); if (CA_STATUS_OK != get_input_data(responseCodeBuf, MAX_BUF_LEN)) { return; } responseCode = atoi(responseCodeBuf); } CAInfo_t responseData = { 0 }; responseData.type = messageType; responseData.messageId = (info != NULL) ? info->messageId : 0; responseData.resourceUri = (info != NULL) ? info->resourceUri : 0; if(CA_MSG_RESET != messageType) { responseData.token = (info != NULL) ? info->token : NULL; responseData.tokenLength = (info != NULL) ? info->tokenLength : 0; if (endpoint->flags & CA_SECURE) { printf("Sending response on secure communication\n"); uint32_t length = sizeof(SECURE_INFO_DATA) + strlen(responseData.resourceUri) + sizeof(g_local_secure_port); responseData.payload = (CAPayload_t) calloc(length, sizeof(char)); if (NULL == responseData.payload) { printf("Memory allocation fail\n"); return; } snprintf((char *) responseData.payload, length, SECURE_INFO_DATA, (const char *) responseData.resourceUri, g_local_secure_port); } else { printf("Sending response on non-secure communication\n"); bool useBigPayload = select_payload(); if (useBigPayload) { responseData.payload = (CAPayload_t) calloc(BIG_PAYLOAD_SIZE, sizeof(char)); if (NULL == responseData.payload) { printf("Memory allocation fail\n"); return; } populate_binary_payload(responseData.payload, BIG_PAYLOAD_SIZE); } else { size_t length = sizeof(NORMAL_INFO_DATA) + strlen(responseData.resourceUri); responseData.payload = (CAPayload_t) calloc(length, sizeof(char)); if (NULL == responseData.payload) { printf("Memory allocation fail\n"); return; } snprintf((char *) responseData.payload, length, NORMAL_INFO_DATA, (const char *) responseData.resourceUri); } } } responseData.payloadSize = strlen((char *)responseData.payload)+1; CAResponseInfo_t responseInfo = { 0 }; responseInfo.result = responseCode; responseInfo.info = responseData; // send response (transportType from remoteEndpoint of request Info) CAResult_t res = CASendResponse(endpoint, &responseInfo); if (CA_STATUS_OK != res) { printf("Send response error\n"); } else { printf("Send response success\n"); } printf("=============================================\n"); }