static void CADestroyData(void *data, uint32_t size) { OIC_LOG(DEBUG, TAG, "CADestroyData IN"); if ((size_t)size < sizeof(CAData_t)) { OIC_LOG_V(ERROR, TAG, "Destroy data too small %p %d", data, size); } CAData_t *cadata = (CAData_t *) data; if (NULL == cadata) { OIC_LOG(ERROR, TAG, "cadata is NULL"); return; } if (NULL != cadata->remoteEndpoint) { CAFreeEndpoint(cadata->remoteEndpoint); } if (NULL != cadata->requestInfo) { CADestroyRequestInfoInternal((CARequestInfo_t *) cadata->requestInfo); } if (NULL != cadata->responseInfo) { CADestroyResponseInfoInternal((CAResponseInfo_t *) cadata->responseInfo); } if (NULL != cadata->errorInfo) { CADestroyErrorInfoInternal(cadata->errorInfo); } OICFree(cadata); OIC_LOG(DEBUG, TAG, "CADestroyData OUT"); }
CARequestInfo_t *CACloneRequestInfo(const CARequestInfo_t *rep) { if (NULL == rep) { OIC_LOG(ERROR, TAG, "parameter is null"); return NULL; } // allocate the request info structure. CARequestInfo_t *clone = (CARequestInfo_t *) OICMalloc(sizeof(CARequestInfo_t)); if (!clone) { OIC_LOG(ERROR, TAG, "CACloneRequestInfo Out of memory"); return NULL; } *clone = *rep; if (rep->info.token) { char *temp = NULL; // allocate token field uint8_t len = rep->info.tokenLength; if (len) { temp = (char *) OICCalloc(len, sizeof(char)); if (!temp) { OIC_LOG(ERROR, TAG, "CACloneRequestInfo Out of memory"); CADestroyRequestInfoInternal(clone); return NULL; } memcpy(temp, rep->info.token, len); } // save the token clone->info.token = temp; clone->info.tokenLength = len; } if (NULL != rep->info.options && 0 < rep->info.numOptions) { // save the options clone->info.options = (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t) * rep->info.numOptions); if (NULL == clone->info.options) { OIC_LOG(ERROR, TAG, "CACloneRequestInfo Out of memory"); OICFree(clone->info.token); OICFree(clone); return NULL; } memcpy(clone->info.options, rep->info.options, sizeof(CAHeaderOption_t) * rep->info.numOptions); } else { clone->info.options = NULL; clone->info.numOptions = 0; } if (NULL != rep->info.payload) { // allocate payload field uint8_t *temp = OICMalloc(rep->info.payloadSize); if (NULL == temp) { OIC_LOG(ERROR, TAG, "CACloneRequestInfo Out of memory"); CADestroyRequestInfoInternal(clone); return NULL; } memcpy(temp, rep->info.payload, rep->info.payloadSize); // save the payload clone->info.payload = temp; } if (NULL != rep->info.resourceUri) { // allocate payload field char *temp = OICStrdup(rep->info.resourceUri); if (NULL == temp) { OIC_LOG(ERROR, TAG, "CACloneRequestInfo Out of memory"); CADestroyRequestInfoInternal(clone); return NULL; } // save the resourceUri clone->info.resourceUri = temp; } return clone; }
static void CAReceivedPacketCallback(CAEndpoint_t *endpoint, void *data, uint32_t dataLen) { OIC_LOG(DEBUG, TAG, "IN"); VERIFY_NON_NULL_VOID(data, TAG, "data"); uint32_t code = CA_NOT_FOUND; coap_pdu_t *pdu = (coap_pdu_t *) CAParsePDU((const char *) data, dataLen, &code); OICFree(data); if (NULL == pdu) { OIC_LOG(ERROR, TAG, "Parse PDU failed"); return; } char uri[CA_MAX_URI_LENGTH] = { }; if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code) { CARequestInfo_t *ReqInfo = (CARequestInfo_t *) OICCalloc(1, sizeof(CARequestInfo_t)); if (NULL == ReqInfo) { OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!"); coap_delete_pdu(pdu); return; } CAResult_t res = CAGetRequestInfoFromPDU(pdu, ReqInfo); if (CA_STATUS_OK != res) { OIC_LOG_V(ERROR, TAG, "CAGetRequestInfoFromPDU failed : %d", res); OICFree(ReqInfo); coap_delete_pdu(pdu); return; } if (NULL != ReqInfo->info.options) { for (uint32_t i = 0; i < ReqInfo->info.numOptions; i++) { OIC_LOG_V(DEBUG, TAG, "optionID: %d", ReqInfo->info.options[i].optionID); OIC_LOG_V(DEBUG, TAG, "list: %s", ReqInfo->info.options[i].optionData); } } if (NULL != ReqInfo->info.payload) { OIC_LOG_V(DEBUG, TAG, "Request- payload: %s", ReqInfo->info.payload); } OIC_LOG_V(DEBUG, TAG, "code: %d", ReqInfo->method); OIC_LOG(DEBUG, TAG, "token:"); OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) ReqInfo->info.token, CA_MAX_TOKEN_LEN); if (g_requestHandler) { g_requestHandler(endpoint, ReqInfo); } CADestroyRequestInfoInternal(ReqInfo); } else { CAResponseInfo_t *ResInfo = (CAResponseInfo_t *) OICCalloc(1, sizeof(CAResponseInfo_t)); if (NULL == ResInfo) { OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!"); coap_delete_pdu(pdu); return; } CAResult_t res = CAGetResponseInfoFromPDU(pdu, ResInfo); if (CA_STATUS_OK != res) { OIC_LOG_V(ERROR, TAG, "CAGetResponseInfoFromPDU failed : %d", res); OICFree(ResInfo); coap_delete_pdu(pdu); return; } if (NULL != ResInfo->info.options) { for (uint32_t i = 0; i < ResInfo->info.numOptions; i++) { OIC_LOG_V(DEBUG, TAG, "optionID: %d", ResInfo->info.options[i].optionID); OIC_LOG_V(DEBUG, TAG, "list: %s", ResInfo->info.options[i].optionData); } } if (NULL != ResInfo->info.payload) { OIC_LOG_V(DEBUG, TAG, "payload: %s", ResInfo->info.payload); } OIC_LOG_V(DEBUG, TAG, "code: %d", ResInfo->result); // for retransmission void *retransmissionPdu = NULL; CARetransmissionReceivedData(&g_retransmissionContext, endpoint, pdu->hdr, pdu->length, &retransmissionPdu); // get token from saved data in retransmission list if (retransmissionPdu && CA_EMPTY == code) { CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *)retransmissionPdu, &(ResInfo->info)); if(CA_STATUS_OK != res) { OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list"); OICFree(ResInfo->info.token); } } OICFree(retransmissionPdu); if (NULL != ResInfo) { if (g_responseHandler) { g_responseHandler(endpoint, ResInfo); } CADestroyResponseInfoInternal(ResInfo); } } if (pdu) { coap_delete_pdu(pdu); } OIC_LOG(DEBUG, TAG, "OUT"); }
static CAData_t* CAGenerateHandlerData(const CAEndpoint_t *endpoint, const CARemoteId_t *identity, const void *data, CADataType_t dataType) { OIC_LOG(DEBUG, TAG, "CAGenerateHandlerData IN"); CAInfo_t *info = NULL; CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t)); if (!cadata) { OIC_LOG(ERROR, TAG, "memory allocation failed"); return NULL; } CAEndpoint_t* ep = CACloneEndpoint(endpoint); if (!ep) { OIC_LOG(ERROR, TAG, "endpoint clone failed"); OICFree(cadata); return NULL; } OIC_LOG_V(DEBUG, TAG, "address : %s", ep->addr); CAResult_t result; if(CA_RESPONSE_DATA == dataType) { CAResponseInfo_t* resInfo = (CAResponseInfo_t*)OICCalloc(1, sizeof(CAResponseInfo_t)); if (!resInfo) { OIC_LOG(ERROR, TAG, "memory allocation failed"); OICFree(cadata); CAFreeEndpoint(ep); return NULL; } result = CAGetResponseInfoFromPDU(data, resInfo, endpoint); if (CA_STATUS_OK != result) { OIC_LOG(ERROR, TAG, "CAGetResponseInfoFromPDU Failed"); CAFreeEndpoint(ep); CADestroyResponseInfoInternal(resInfo); OICFree(cadata); return NULL; } cadata->responseInfo = resInfo; info = &resInfo->info; if (identity) { info->identity = *identity; } OIC_LOG(DEBUG, TAG, "Response Info :"); CALogPayloadInfo(info); } else if (CA_REQUEST_DATA == dataType) { CARequestInfo_t* reqInfo = (CARequestInfo_t*)OICCalloc(1, sizeof(CARequestInfo_t)); if (!reqInfo) { OIC_LOG(ERROR, TAG, "memory allocation failed"); OICFree(cadata); CAFreeEndpoint(ep); return NULL; } result = CAGetRequestInfoFromPDU(data, endpoint, reqInfo); if (CA_STATUS_OK != result) { OIC_LOG(ERROR, TAG, "CAGetRequestInfoFromPDU failed"); CAFreeEndpoint(ep); CADestroyRequestInfoInternal(reqInfo); OICFree(cadata); return NULL; } if (CADropSecondMessage(&caglobals.ca.requestHistory, endpoint, reqInfo->info.messageId, reqInfo->info.token, reqInfo->info.tokenLength)) { OIC_LOG(ERROR, TAG, "Second Request with same Token, Drop it"); CAFreeEndpoint(ep); CADestroyRequestInfoInternal(reqInfo); OICFree(cadata); return NULL; } cadata->requestInfo = reqInfo; info = &reqInfo->info; if (identity) { info->identity = *identity; } OIC_LOG(DEBUG, TAG, "Request Info :"); CALogPayloadInfo(info); } else if (CA_ERROR_DATA == dataType) { CAErrorInfo_t *errorInfo = (CAErrorInfo_t *)OICCalloc(1, sizeof (CAErrorInfo_t)); if (!errorInfo) { OIC_LOG(ERROR, TAG, "Memory allocation failed!"); OICFree(cadata); CAFreeEndpoint(ep); return NULL; } CAResult_t result = CAGetErrorInfoFromPDU(data, endpoint, errorInfo); if (CA_STATUS_OK != result) { OIC_LOG(ERROR, TAG, "CAGetErrorInfoFromPDU failed"); CAFreeEndpoint(ep); OICFree(errorInfo); OICFree(cadata); return NULL; } cadata->errorInfo = errorInfo; info = &errorInfo->info; if (identity) { info->identity = *identity; } OIC_LOG(DEBUG, TAG, "error Info :"); CALogPayloadInfo(info); } cadata->remoteEndpoint = ep; cadata->dataType = dataType; return cadata; OIC_LOG(DEBUG, TAG, "CAGenerateHandlerData OUT"); }
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) { OIC_LOG_V(DEBUG, TAG, "memory alloc error!!"); return NULL; } memset(clone, 0, sizeof(CARequestInfo_t)); memcpy(clone, rep, sizeof(CARequestInfo_t)); if (rep->info.token != NULL) { // allocate token field len = strlen(rep->info.token); temp = (char*) OICMalloc(sizeof(char) * (len + 1)); if (temp == NULL) { OIC_LOG_V(DEBUG, TAG, "memory alloc error!!"); CADestroyRequestInfoInternal(clone); return NULL; } memset(temp, 0, sizeof(char) * (len + 1)); strncpy(temp, rep->info.token, len); // save the token clone->info.token = temp; } if (rep->info.options != NULL) { // save the options clone->info.options = (CAHeaderOption_t*) OICMalloc(sizeof(CAHeaderOption_t)); memset(clone->info.options, 0, sizeof(CAHeaderOption_t)); memcpy(clone->info.options, rep->info.options, sizeof(CAHeaderOption_t)); } if (rep->info.payload != NULL) { // allocate payload field len = strlen(rep->info.payload); temp = (char*) OICMalloc(sizeof(char) * (len + 1)); if (temp == NULL) { OIC_LOG_V(DEBUG, TAG, "memory alloc error!!"); CADestroyRequestInfoInternal(clone); return NULL; } memset(temp, 0, sizeof(char) * (len + 1)); strncpy(temp, rep->info.payload, len); // save the payload clone->info.payload = temp; } return clone; }