X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=resource%2Fcsdk%2Fconnectivity%2Fsrc%2Fcaprotocolmessage.c;h=7d84c1cd8e9cfd1bdfcb7e2ecc23b476ea361ef4;hb=442026128ead8780fa45d0db8a6f17be7c9220e0;hp=2236c244f6f317ca57becb2fbc0012e8fcb7e09c;hpb=84ddde8349acc67db8f013a2f3737669d986bffc;p=platform%2Fupstream%2Fiotivity.git diff --git a/resource/csdk/connectivity/src/caprotocolmessage.c b/resource/csdk/connectivity/src/caprotocolmessage.c index 2236c24..7d84c1c 100644 --- a/resource/csdk/connectivity/src/caprotocolmessage.c +++ b/resource/csdk/connectivity/src/caprotocolmessage.c @@ -24,7 +24,6 @@ // For glibc information on feature test macros, // Refer http://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html // -// This file requires #define use due to random() and srandom() // For details on compatibility and glibc support, // Refer http://www.gnu.org/software/libc/manual/html_node/BSD-Random.html #define _DEFAULT_SOURCE @@ -41,87 +40,60 @@ #include "logger.h" #include "oic_malloc.h" #include "oic_string.h" +#include "ocrandom.h" +#include "cacommonutil.h" -// ARM GCC compiler doesnt define srandom function. -#if defined(ARDUINO) && !defined(ARDUINO_ARCH_SAM) -#define HAVE_SRANDOM 1 -#endif - -#define TAG "CA_PRTCL_MSG" - -/** - * @def VERIFY_NON_NULL_RET - * @brief Macro to verify the validity of input argument - */ -#define VERIFY_NON_NULL_RET(arg, log_tag, log_message,ret) \ - if (NULL == arg ){ \ - OIC_LOG_V(ERROR, log_tag, "Invalid input:%s", log_message); \ - return ret; \ - } +#define TAG "OIC_CA_PRTCL_MSG" #define CA_BUFSIZE (128) #define CA_PDU_MIN_SIZE (4) -#define CA_PORT_BUFFER_SIZE (4) +#define CA_ENCODE_BUFFER_SIZE (4) static const char COAP_URI_HEADER[] = "coap://[::]/"; -static unsigned int SEED = 0; - -CAResult_t CAGetRequestInfoFromPDU(const coap_pdu_t *pdu, CARequestInfo_t *outReqInfo) +CAResult_t CAGetRequestInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint, + CARequestInfo_t *outReqInfo) { - OIC_LOG(DEBUG, TAG, "IN"); - - if (NULL == pdu || NULL == outReqInfo) - { - OIC_LOG(ERROR, TAG, "parameter is null"); - return CA_STATUS_INVALID_PARAM; - } + VERIFY_NON_NULL(pdu, TAG, "pdu"); + VERIFY_NON_NULL(outReqInfo, TAG, "outReqInfo"); uint32_t code = CA_NOT_FOUND; - CAResult_t ret = CAGetInfoFromPDU(pdu, &code, &(outReqInfo->info)); + CAResult_t ret = CAGetInfoFromPDU(pdu, endpoint, &code, &(outReqInfo->info)); outReqInfo->method = code; - OIC_LOG(DEBUG, TAG, "OUT"); return ret; } -CAResult_t CAGetResponseInfoFromPDU(const coap_pdu_t *pdu, CAResponseInfo_t *outResInfo) +CAResult_t CAGetResponseInfoFromPDU(const coap_pdu_t *pdu, CAResponseInfo_t *outResInfo, + const CAEndpoint_t *endpoint) { - OIC_LOG(DEBUG, TAG, "IN"); - - if (NULL == pdu || NULL == outResInfo) - { - OIC_LOG(ERROR, TAG, "parameter is null"); - return CA_STATUS_INVALID_PARAM; - } + VERIFY_NON_NULL(pdu, TAG, "pdu"); + VERIFY_NON_NULL(outResInfo, TAG, "outResInfo"); uint32_t code = CA_NOT_FOUND; - CAResult_t ret = CAGetInfoFromPDU(pdu, &code, &(outResInfo->info)); + CAResult_t ret = CAGetInfoFromPDU(pdu, endpoint, &code, &(outResInfo->info)); outResInfo->result = code; - OIC_LOG(DEBUG, TAG, "OUT"); return ret; } -CAResult_t CAGetErrorInfoFromPDU(const coap_pdu_t *pdu, CAErrorInfo_t *errorInfo) +CAResult_t CAGetErrorInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint, + CAErrorInfo_t *errorInfo) { - OIC_LOG(DEBUG, TAG, "IN"); - - if (!pdu) - { - OIC_LOG(ERROR, TAG, "parameter is null"); - return CA_STATUS_INVALID_PARAM; - } + VERIFY_NON_NULL(pdu, TAG, "pdu"); uint32_t code = 0; - CAResult_t ret = CAGetInfoFromPDU(pdu, &code, &errorInfo->info); - OIC_LOG(DEBUG, TAG, "OUT"); + CAResult_t ret = CAGetInfoFromPDU(pdu, endpoint, &code, &errorInfo->info); + return ret; } -coap_pdu_t *CAGeneratePDU(uint32_t code, const CAInfo_t *info) +coap_pdu_t *CAGeneratePDU(uint32_t code, const CAInfo_t *info, const CAEndpoint_t *endpoint, + coap_list_t **optlist, coap_transport_type *transport) { - OIC_LOG(DEBUG, TAG, "IN"); + VERIFY_NON_NULL_RET(info, TAG, "info", NULL); + VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", NULL); + VERIFY_NON_NULL_RET(optlist, TAG, "optlist", NULL); coap_pdu_t *pdu = NULL; @@ -129,8 +101,20 @@ coap_pdu_t *CAGeneratePDU(uint32_t code, const CAInfo_t *info) // and ACKNOWLEDGE can use empty message when code is empty. if (CA_MSG_RESET == info->type || (CA_EMPTY == code && CA_MSG_ACKNOWLEDGE == info->type)) { + if (CA_EMPTY != code) + { + OIC_LOG(ERROR, TAG, "reset is not empty message"); + return NULL; + } + + if (info->payloadSize > 0 || info->payload || info->token || info->tokenLength > 0) + { + OIC_LOG(ERROR, TAG, "Empty message has unnecessary data after messageID"); + return NULL; + } + OIC_LOG(DEBUG, TAG, "code is empty"); - if (!(pdu = CAGeneratePDUImpl((code_t) code, NULL, info, NULL, 0))) + if (!(pdu = CAGeneratePDUImpl((code_t) code, info, endpoint, NULL, transport))) { OIC_LOG(ERROR, TAG, "pdu NULL"); return NULL; @@ -138,18 +122,9 @@ coap_pdu_t *CAGeneratePDU(uint32_t code, const CAInfo_t *info) } else { - coap_list_t *optlist = NULL; - - if (CA_MSG_ACKNOWLEDGE != info->type) + if (info->resourceUri) { - const char *uri = info->resourceUri; - if (NULL == uri) - { - OIC_LOG(ERROR, TAG, "uri NULL"); - return NULL; - } - - uint32_t length = strlen(uri); + uint32_t length = strlen(info->resourceUri); if (CA_MAX_URI_LENGTH < length) { OIC_LOG(ERROR, TAG, "URI len err"); @@ -164,16 +139,12 @@ coap_pdu_t *CAGeneratePDU(uint32_t code, const CAInfo_t *info) return NULL; } OICStrcat(coapUri, uriLength, COAP_URI_HEADER); - OICStrcat(coapUri, uriLength, uri); + OICStrcat(coapUri, uriLength, info->resourceUri); // parsing options in URI - CAResult_t res = CAParseURI(coapUri, &optlist); + CAResult_t res = CAParseURI(coapUri, optlist); if (CA_STATUS_OK != res) { - if (optlist) - { - coap_delete_list(optlist); - } OICFree(coapUri); return NULL; } @@ -181,69 +152,148 @@ coap_pdu_t *CAGeneratePDU(uint32_t code, const CAInfo_t *info) OICFree(coapUri); } // parsing options in HeadOption - CAResult_t ret = CAParseHeadOption(code, info, &optlist); + CAResult_t ret = CAParseHeadOption(code, info, optlist); if (CA_STATUS_OK != ret) { - coap_delete_list(optlist); return NULL; } - pdu = CAGeneratePDUImpl((code_t) code, optlist, info, info->payload, info->payloadSize); + + pdu = CAGeneratePDUImpl((code_t) code, info, endpoint, *optlist, transport); if (NULL == pdu) { OIC_LOG(ERROR, TAG, "pdu NULL"); - coap_delete_list(optlist); return NULL; } - - // free option list - coap_delete_list(optlist); } // pdu print method : coap_show_pdu(pdu); - OIC_LOG(DEBUG, TAG, "OUT"); return pdu; } -coap_pdu_t *CAParsePDU(const char *data, uint32_t length, uint32_t *outCode) +coap_pdu_t *CAParsePDU(const char *data, uint32_t length, uint32_t *outCode, + const CAEndpoint_t *endpoint) { - OIC_LOG(DEBUG, TAG, "IN"); + VERIFY_NON_NULL_RET(data, TAG, "data", NULL); + VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", NULL); - if (NULL == data) + coap_transport_type transport; +#ifdef WITH_TCP + if (CAIsSupportedCoAPOverTCP(endpoint->adapter)) { - OIC_LOG(ERROR, TAG, "data is null"); - return NULL; + transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)data)[0] >> 4); + } + else +#endif + { + transport = coap_udp; } - coap_pdu_t *outpdu = coap_new_pdu(); + coap_pdu_t *outpdu = coap_new_pdu(transport, length); if (NULL == outpdu) { OIC_LOG(ERROR, TAG, "outpdu is null"); return NULL; } - if (0 >= coap_pdu_parse((unsigned char *) data, length, outpdu)) + OIC_LOG_V(DEBUG, TAG, "pdu parse-transport type : %d", transport); + + int ret = coap_pdu_parse((unsigned char *) data, length, outpdu, transport); + OIC_LOG_V(DEBUG, TAG, "pdu parse ret: %d", ret); + if (0 >= ret) { OIC_LOG(ERROR, TAG, "pdu parse failed"); - coap_delete_pdu(outpdu); - return NULL; + goto exit; + } + +#ifdef WITH_TCP + if (CAIsSupportedCoAPOverTCP(endpoint->adapter)) + { + OIC_LOG(INFO, TAG, "there is no version info in coap header"); + } + else +#endif + { + if (outpdu->hdr->coap_hdr_udp_t.version != COAP_DEFAULT_VERSION) + { + OIC_LOG_V(ERROR, TAG, "coap version is not available : %d", + outpdu->hdr->coap_hdr_udp_t.version); + goto exit; + } + if (outpdu->hdr->coap_hdr_udp_t.token_length > CA_MAX_TOKEN_LEN) + { + OIC_LOG_V(ERROR, TAG, "token length has been exceed : %d", + outpdu->hdr->coap_hdr_udp_t.token_length); + goto exit; + } } if (outCode) { - (*outCode) = (uint32_t) CA_RESPONSE_CODE(outpdu->hdr->code); + (*outCode) = (uint32_t) CA_RESPONSE_CODE(coap_get_code(outpdu, transport)); } - OIC_LOG(DEBUG, TAG, "OUT"); return outpdu; + +exit: + coap_delete_pdu(outpdu); + return NULL; } -coap_pdu_t *CAGeneratePDUImpl(code_t code, coap_list_t *options, const CAInfo_t *info, - const uint8_t *payload, size_t payloadSize) +coap_pdu_t *CAGeneratePDUImpl(code_t code, const CAInfo_t *info, + const CAEndpoint_t *endpoint, coap_list_t *options, + coap_transport_type *transport) { - OIC_LOG(DEBUG, TAG, "IN"); - VERIFY_NON_NULL_RET(info, TAG, "info is NULL", NULL); + VERIFY_NON_NULL_RET(info, TAG, "info", NULL); + VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", NULL); + VERIFY_NON_NULL_RET(transport, TAG, "transport", NULL); + + unsigned int length = COAP_MAX_PDU_SIZE; +#ifdef WITH_TCP + unsigned int msgLength = 0; + if (CAIsSupportedCoAPOverTCP(endpoint->adapter)) + { + if (options) + { + unsigned short prevOptNumber = 0; + for (coap_list_t *opt = options; opt; opt = opt->next) + { + unsigned short curOptNumber = COAP_OPTION_KEY(*(coap_option *) opt->data); + if (prevOptNumber > curOptNumber) + { + OIC_LOG(ERROR, TAG, "option list is wrong"); + return NULL; + } - coap_pdu_t *pdu = coap_new_pdu(); + size_t optValueLen = COAP_OPTION_LENGTH(*(coap_option *) opt->data); + size_t optLength = coap_get_opt_header_length(curOptNumber - prevOptNumber, optValueLen); + if (0 == optLength) + { + OIC_LOG(ERROR, TAG, "Reserved for the Payload marker for the option"); + return NULL; + } + msgLength += optLength; + prevOptNumber = curOptNumber; + OIC_LOG_V(DEBUG, TAG, "curOptNumber[%d], prevOptNumber[%d], optValueLen[%zu], " + "optLength[%zu], msgLength[%d]", + curOptNumber, prevOptNumber, optValueLen, optLength, msgLength); + } + } + + if (info->payloadSize > 0) + { + msgLength = msgLength + info->payloadSize + PAYLOAD_MARKER; + } + *transport = coap_get_tcp_header_type_from_size(msgLength); + length = msgLength + coap_get_tcp_header_length_for_transport(*transport) + + info->tokenLength; + } + else +#endif + { + *transport = coap_udp; + } + + coap_pdu_t *pdu = coap_new_pdu(*transport, length); if (NULL == pdu) { @@ -251,25 +301,38 @@ coap_pdu_t *CAGeneratePDUImpl(code_t code, coap_list_t *options, const CAInfo_t return NULL; } - OIC_LOG_V(DEBUG, TAG, "msgID is %d", info->messageId); - uint16_t message_id; - if (0 == info->messageId) - { - /* initialize message id */ - prng((uint8_t * ) &message_id, sizeof(message_id)); + OIC_LOG_V(DEBUG, TAG, "transport type: %d, payload size: %zu", + *transport, info->payloadSize); - OIC_LOG_V(DEBUG, TAG, "gen msg id=%d", message_id); +#ifdef WITH_TCP + if (CAIsSupportedCoAPOverTCP(endpoint->adapter)) + { + coap_add_length(pdu, *transport, msgLength); } else +#endif { - /* use saved message id */ - message_id = info->messageId; + OIC_LOG_V(DEBUG, TAG, "msgID is %d", info->messageId); + uint16_t message_id; + if (0 == info->messageId) + { + /* initialize message id */ + prng((uint8_t * ) &message_id, sizeof(message_id)); + + OIC_LOG_V(DEBUG, TAG, "gen msg id=%d", message_id); + } + else + { + /* use saved message id */ + message_id = info->messageId; + } + pdu->hdr->coap_hdr_udp_t.id = message_id; + OIC_LOG_V(DEBUG, TAG, "messageId in pdu is %d, %d", message_id, pdu->hdr->coap_hdr_udp_t.id); + + pdu->hdr->coap_hdr_udp_t.type = info->type; } - pdu->hdr->id = message_id; - OIC_LOG_V(DEBUG, TAG, "messageId in pdu is %d, %d", message_id, pdu->hdr->id); - pdu->hdr->type = info->type; - pdu->hdr->code = COAP_RESPONSE_CODE(code); + coap_add_code(pdu, *transport, code); if (info->token && CA_EMPTY != code) { @@ -277,64 +340,64 @@ coap_pdu_t *CAGeneratePDUImpl(code_t code, coap_list_t *options, const CAInfo_t OIC_LOG_V(DEBUG, TAG, "token info token length: %d, token :", tokenLength); OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)info->token, tokenLength); - int32_t ret = coap_add_token(pdu, tokenLength, (unsigned char *)info->token); + int32_t ret = coap_add_token(pdu, tokenLength, (unsigned char *)info->token, *transport); if (0 == ret) { OIC_LOG(ERROR, TAG, "can't add token"); } } +#ifdef WITH_BWT + if (CA_ADAPTER_GATT_BTLE != endpoint->adapter +#ifdef WITH_TCP + && !CAIsSupportedCoAPOverTCP(endpoint->adapter) +#endif + ) + { + // option list will be added in blockwise-transfer + return pdu; + } +#endif + if (options) { for (coap_list_t *opt = options; opt; opt = opt->next) { OIC_LOG_V(DEBUG, TAG, "[%s] opt will be added.", COAP_OPTION_DATA(*(coap_option *) opt->data)); + + OIC_LOG_V(DEBUG, TAG, "[%d] pdu length", pdu->length); coap_add_option(pdu, COAP_OPTION_KEY(*(coap_option *) opt->data), COAP_OPTION_LENGTH(*(coap_option *) opt->data), - COAP_OPTION_DATA(*(coap_option *) opt->data)); + COAP_OPTION_DATA(*(coap_option *) opt->data), *transport); } } -#ifndef WITH_BWT - if (NULL != payload) + OIC_LOG_V(DEBUG, TAG, "[%d] pdu length after option", pdu->length); + + if (NULL != info->payload && 0 < info->payloadSize) { - coap_add_data(pdu, payloadSize, (const unsigned char *) payload); + OIC_LOG(DEBUG, TAG, "payload is added"); + coap_add_data(pdu, info->payloadSize, (const unsigned char *) info->payload); } -#else - (void)payload; - (void)payloadSize; -#endif - OIC_LOG(DEBUG, TAG, "OUT"); return pdu; } CAResult_t CAParseURI(const char *uriInfo, coap_list_t **optlist) { - OIC_LOG(DEBUG, TAG, "IN"); - - if (NULL == uriInfo) - { - OIC_LOG(ERROR, TAG, "uriInfo is null"); - return CA_STATUS_INVALID_PARAM; - } + VERIFY_NON_NULL(uriInfo, TAG, "uriInfo"); + VERIFY_NON_NULL(optlist, TAG, "optlist"); OIC_LOG_V(DEBUG, TAG, "url : %s", uriInfo); - if (NULL == optlist) - { - OIC_LOG(ERROR, TAG, "optlist is null"); - return CA_STATUS_INVALID_PARAM; - } - /* split arg into Uri-* options */ coap_uri_t uri; coap_split_uri((unsigned char *) uriInfo, strlen(uriInfo), &uri); if (uri.port != COAP_DEFAULT_PORT) { - unsigned char portbuf[CA_PORT_BUFFER_SIZE] = { 0 }; + unsigned char portbuf[CA_ENCODE_BUFFER_SIZE] = { 0 }; int ret = coap_insert(optlist, CACreateNewOptionNode(COAP_OPTION_URI_PORT, coap_encode_var_bytes(portbuf, uri.port), @@ -368,18 +431,13 @@ CAResult_t CAParseURI(const char *uriInfo, coap_list_t **optlist) } } - OIC_LOG(DEBUG, TAG, "OUT"); return CA_STATUS_OK; } CAResult_t CAParseUriPartial(const unsigned char *str, size_t length, int target, coap_list_t **optlist) { - if (!optlist) - { - OIC_LOG(ERROR, TAG, "optlist is null"); - return CA_STATUS_INVALID_PARAM; - } + VERIFY_NON_NULL(optlist, TAG, "optlist"); if ((target != COAP_OPTION_URI_PATH) && (target != COAP_OPTION_URI_QUERY)) { @@ -435,8 +493,7 @@ CAResult_t CAParseUriPartial(const unsigned char *str, size_t length, int target CAResult_t CAParseHeadOption(uint32_t code, const CAInfo_t *info, coap_list_t **optlist) { (void)code; - OIC_LOG(DEBUG, TAG, "IN"); - VERIFY_NON_NULL_RET(info, TAG, "info is NULL", CA_STATUS_INVALID_PARAM); + VERIFY_NON_NULL_RET(info, TAG, "info", CA_STATUS_INVALID_PARAM); OIC_LOG_V(DEBUG, TAG, "parse Head Opt: %d", info->numOptions); @@ -461,36 +518,84 @@ CAResult_t CAParseHeadOption(uint32_t code, const CAInfo_t *info, coap_list_t ** } else { - if ((info->options + i)->optionData && (info->options + i)->optionLength > 0) + OIC_LOG_V(DEBUG, TAG, "Head opt ID: %d", id); + OIC_LOG_V(DEBUG, TAG, "Head opt data: %s", (info->options + i)->optionData); + OIC_LOG_V(DEBUG, TAG, "Head opt length: %d", (info->options + i)->optionLength); + int ret = coap_insert(optlist, + CACreateNewOptionNode(id, (info->options + i)->optionLength, + (info->options + i)->optionData), + CAOrderOpts); + if (ret <= 0) { - OIC_LOG_V(DEBUG, TAG, "Head opt ID: %d", id); - OIC_LOG_V(DEBUG, TAG, "Head opt data: %s", (info->options + i)->optionData); - OIC_LOG_V(DEBUG, TAG, "Head opt length: %d", (info->options + i)->optionLength); - int ret = coap_insert(optlist, - CACreateNewOptionNode(id, (info->options + i)->optionLength, - (info->options + i)->optionData), - CAOrderOpts); - if (ret <= 0) - { - return CA_STATUS_INVALID_PARAM; - } + return CA_STATUS_INVALID_PARAM; } } } - OIC_LOG(DEBUG, TAG, "OUT"); + // insert one extra header with the payload format if applicable. + if (CA_FORMAT_UNDEFINED != info->payloadFormat) + { + coap_list_t* node = NULL; + uint8_t buf[CA_ENCODE_BUFFER_SIZE] = {0}; + switch (info->payloadFormat) + { + case CA_FORMAT_APPLICATION_CBOR: + node = CACreateNewOptionNode( + COAP_OPTION_CONTENT_FORMAT, + coap_encode_var_bytes(buf, (unsigned short)COAP_MEDIATYPE_APPLICATION_CBOR), + (char *)buf); + break; + default: + OIC_LOG_V(ERROR, TAG, "format option:[%d] not supported", info->payloadFormat); + } + if (!node) + { + OIC_LOG(ERROR, TAG, "format option not created"); + return CA_STATUS_INVALID_PARAM; + } + int ret = coap_insert(optlist, node, CAOrderOpts); + if (ret <= 0) + { + coap_delete(node); + OIC_LOG(ERROR, TAG, "format option not inserted in header"); + return CA_STATUS_INVALID_PARAM; + } + } + if (CA_FORMAT_UNDEFINED != info->acceptFormat) + { + coap_list_t* node = NULL; + uint8_t buf[CA_ENCODE_BUFFER_SIZE] = {0}; + switch (info->acceptFormat) + { + case CA_FORMAT_APPLICATION_CBOR: + node = CACreateNewOptionNode( + COAP_OPTION_ACCEPT, + coap_encode_var_bytes(buf, (unsigned short)COAP_MEDIATYPE_APPLICATION_CBOR), + (char *)buf); + break; + default: + OIC_LOG_V(ERROR, TAG, "format option:[%d] not supported", info->acceptFormat); + } + if (!node) + { + OIC_LOG(ERROR, TAG, "format option not created"); + return CA_STATUS_INVALID_PARAM; + } + int ret = coap_insert(optlist, node, CAOrderOpts); + if (ret <= 0) + { + coap_delete(node); + OIC_LOG(ERROR, TAG, "format option not inserted in header"); + return CA_STATUS_INVALID_PARAM; + } + } + return CA_STATUS_OK; } coap_list_t *CACreateNewOptionNode(uint16_t key, uint32_t length, const char *data) { - OIC_LOG(DEBUG, TAG, "IN"); - - if (!data) - { - OIC_LOG(ERROR, TAG, "invalid pointer parameter"); - return NULL; - } + VERIFY_NON_NULL_RET(data, TAG, "data", NULL); coap_option *option = coap_malloc(sizeof(coap_option) + length + 1); if (!option) @@ -501,8 +606,30 @@ coap_list_t *CACreateNewOptionNode(uint16_t key, uint32_t length, const char *da memset(option, 0, sizeof(coap_option) + length + 1); COAP_OPTION_KEY(*option) = key; - COAP_OPTION_LENGTH(*option) = length; - memcpy(COAP_OPTION_DATA(*option), data, length); + + coap_option_def_t* def = coap_opt_def(key); + if (NULL != def && coap_is_var_bytes(def)) + { + if (length > def->max) + { + // make sure we shrink the value so it fits the coap option definition + // by truncating the value, disregard the leading bytes. + OIC_LOG_V(DEBUG, TAG, "Option [%d] data size [%d] shrunk to [%d]", + def->key, length, def->max); + data = &(data[length-def->max]); + length = def->max; + } + // Shrink the encoding length to a minimum size for coap + // options that support variable length encoding. + COAP_OPTION_LENGTH(*option) = coap_encode_var_bytes( + COAP_OPTION_DATA(*option), + coap_decode_var_bytes((unsigned char *)data, length)); + } + else + { + COAP_OPTION_LENGTH(*option) = length; + memcpy(COAP_OPTION_DATA(*option), data, length); + } /* we can pass NULL here as delete function since option is released automatically */ coap_list_t *node = coap_new_listnode(option, NULL); @@ -514,13 +641,11 @@ coap_list_t *CACreateNewOptionNode(uint16_t key, uint32_t length, const char *da return NULL; } - OIC_LOG(DEBUG, TAG, "OUT"); return node; } int CAOrderOpts(void *a, void *b) { - OIC_LOG(DEBUG, TAG, "IN"); if (!a || !b) { return a < b ? -1 : 1; @@ -531,13 +656,11 @@ int CAOrderOpts(void *a, void *b) return -1; } - OIC_LOG(DEBUG, TAG, "OUT"); return COAP_OPTION_KEY(*(coap_option *) a) == COAP_OPTION_KEY(*(coap_option * ) b); } uint32_t CAGetOptionCount(coap_opt_iterator_t opt_iter) { - OIC_LOG(DEBUG, TAG, "IN"); uint32_t count = 0; coap_opt_t *option; @@ -545,32 +668,46 @@ uint32_t CAGetOptionCount(coap_opt_iterator_t opt_iter) { if (COAP_OPTION_URI_PATH != opt_iter.type && COAP_OPTION_URI_QUERY != opt_iter.type && COAP_OPTION_BLOCK1 != opt_iter.type && COAP_OPTION_BLOCK2 != opt_iter.type - && COAP_OPTION_SIZE1 != opt_iter.type && COAP_OPTION_SIZE2 != opt_iter.type) + && COAP_OPTION_SIZE1 != opt_iter.type && COAP_OPTION_SIZE2 != opt_iter.type + && COAP_OPTION_CONTENT_FORMAT != opt_iter.type + && COAP_OPTION_ACCEPT != opt_iter.type + && COAP_OPTION_URI_HOST != opt_iter.type && COAP_OPTION_URI_PORT != opt_iter.type + && COAP_OPTION_ETAG != opt_iter.type && COAP_OPTION_MAXAGE != opt_iter.type + && COAP_OPTION_PROXY_URI != opt_iter.type && COAP_OPTION_PROXY_SCHEME != opt_iter.type) { count++; } } - OIC_LOG(DEBUG, TAG, "OUT"); return count; } -CAResult_t CAGetInfoFromPDU(const coap_pdu_t *pdu, uint32_t *outCode, CAInfo_t *outInfo) +CAResult_t CAGetInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint, + uint32_t *outCode, CAInfo_t *outInfo) { - OIC_LOG(DEBUG, TAG, "IN"); - - if (!pdu || !outCode || !outInfo) + VERIFY_NON_NULL(pdu, TAG, "pdu"); + VERIFY_NON_NULL(endpoint, TAG, "endpoint"); + VERIFY_NON_NULL(outCode, TAG, "outCode"); + VERIFY_NON_NULL(outInfo, TAG, "outInfo"); + + coap_transport_type transport; +#ifdef WITH_TCP + if (CAIsSupportedCoAPOverTCP(endpoint->adapter)) { - OIC_LOG(ERROR, TAG, "NULL pointer param"); - return CA_STATUS_INVALID_PARAM; + transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)pdu->hdr)[0] >> 4); + } + else +#endif + { + transport = coap_udp; } coap_opt_iterator_t opt_iter; - coap_option_iterator_init((coap_pdu_t *) pdu, &opt_iter, COAP_OPT_ALL); + coap_option_iterator_init((coap_pdu_t *) pdu, &opt_iter, COAP_OPT_ALL, transport); if (outCode) { - (*outCode) = (uint32_t) CA_RESPONSE_CODE(pdu->hdr->code); + (*outCode) = (uint32_t) CA_RESPONSE_CODE(coap_get_code(pdu, transport)); } // init HeaderOption list @@ -579,11 +716,26 @@ CAResult_t CAGetInfoFromPDU(const coap_pdu_t *pdu, uint32_t *outCode, CAInfo_t * outInfo->numOptions = count; - // set type - outInfo->type = pdu->hdr->type; +#ifdef WITH_TCP + if (CAIsSupportedCoAPOverTCP(endpoint->adapter)) + { + // set type + outInfo->type = CA_MSG_NONCONFIRM; + outInfo->payloadFormat = CA_FORMAT_UNDEFINED; + } + else +#else + (void) endpoint; +#endif + { + // set type + outInfo->type = pdu->hdr->coap_hdr_udp_t.type; - // set message id - outInfo->messageId = pdu->hdr->id; + // set message id + outInfo->messageId = pdu->hdr->coap_hdr_udp_t.id; + outInfo->payloadFormat = CA_FORMAT_UNDEFINED; + outInfo->acceptFormat = CA_FORMAT_UNDEFINED; + } if (count > 0) { @@ -605,11 +757,12 @@ CAResult_t CAGetInfoFromPDU(const coap_pdu_t *pdu, uint32_t *outCode, CAInfo_t * while ((option = coap_option_next(&opt_iter))) { char buf[COAP_MAX_PDU_SIZE] = {0}; - if (CAGetOptionData((uint8_t *)(COAP_OPT_VALUE(option)), - COAP_OPT_LENGTH(option), (uint8_t *)buf, sizeof(buf))) + uint32_t bufLength = + CAGetOptionData(opt_iter.type, (uint8_t *)(COAP_OPT_VALUE(option)), + COAP_OPT_LENGTH(option), (uint8_t *)buf, sizeof(buf)); + if (bufLength) { OIC_LOG_V(DEBUG, TAG, "COAP URI element : %s", buf); - uint32_t bufLength = strlen(buf); if (COAP_OPTION_URI_PATH == opt_iter.type || COAP_OPTION_URI_QUERY == opt_iter.type) { if (false == isfirstsetflag) @@ -690,18 +843,52 @@ CAResult_t CAGetInfoFromPDU(const coap_pdu_t *pdu, uint32_t *outCode, CAInfo_t * { OIC_LOG_V(DEBUG, TAG, "option[%d] will be filtering", opt_iter.type); } + else if (COAP_OPTION_CONTENT_FORMAT == opt_iter.type) + { + if (1 == COAP_OPT_LENGTH(option)) + { + outInfo->payloadFormat = CAConvertFormat((uint8_t)buf[0]); + } + else + { + outInfo->payloadFormat = CA_FORMAT_UNSUPPORTED; + OIC_LOG_V(DEBUG, TAG, "option[%d] has an unsupported format [%d]", + opt_iter.type, (uint8_t)buf[0]); + } + } + else if (COAP_OPTION_ACCEPT == opt_iter.type) + { + if (1 == COAP_OPT_LENGTH(option)) + { + outInfo->acceptFormat = CAConvertFormat((uint8_t)buf[0]); + } + else + { + outInfo->acceptFormat = CA_FORMAT_UNSUPPORTED; + } + OIC_LOG_V(DEBUG, TAG, "option[%d] has an unsupported format [%d]", + opt_iter.type, (uint8_t)buf[0]); + } + else if (COAP_OPTION_URI_PORT == opt_iter.type || + COAP_OPTION_URI_HOST == opt_iter.type || + COAP_OPTION_ETAG == opt_iter.type || + COAP_OPTION_MAXAGE == opt_iter.type || + COAP_OPTION_PROXY_URI == opt_iter.type || + COAP_OPTION_PROXY_SCHEME== opt_iter.type) + { + OIC_LOG_V(INFO, TAG, "option[%d] has an unsupported format [%d]", + opt_iter.type, (uint8_t)buf[0]); + } else { if (idx < count) { - uint32_t length = bufLength; - - if (length <= CA_MAX_HEADER_OPTION_DATA_LENGTH) + if (bufLength <= sizeof(outInfo->options[0].optionData)) { outInfo->options[idx].optionID = opt_iter.type; - outInfo->options[idx].optionLength = length; + outInfo->options[idx].optionLength = bufLength; outInfo->options[idx].protocolID = CA_COAP_ID; - memcpy(outInfo->options[idx].optionData, buf, length); + memcpy(outInfo->options[idx].optionData, buf, bufLength); idx++; } } @@ -709,21 +896,25 @@ CAResult_t CAGetInfoFromPDU(const coap_pdu_t *pdu, uint32_t *outCode, CAInfo_t * } } + unsigned char* token = NULL; + unsigned int token_length = 0; + coap_get_token(pdu->hdr, transport, &token, &token_length); + // set token data - if (pdu->hdr->token_length > 0) + if (token_length > 0) { - OIC_LOG_V(DEBUG, TAG, "inside token length : %d", pdu->hdr->token_length); - outInfo->token = (char *) OICMalloc(pdu->hdr->token_length); + OIC_LOG_V(DEBUG, TAG, "inside token length : %d", token_length); + outInfo->token = (char *) OICMalloc(token_length); if (NULL == outInfo->token) { OIC_LOG(ERROR, TAG, "Out of memory"); OICFree(outInfo->options); return CA_MEMORY_ALLOC_FAILED; } - memcpy(outInfo->token, pdu->hdr->token, pdu->hdr->token_length); + memcpy(outInfo->token, token, token_length); } - outInfo->tokenLength = pdu->hdr->token_length; + outInfo->tokenLength = token_length; // set payload data size_t dataSize; @@ -745,7 +936,7 @@ CAResult_t CAGetInfoFromPDU(const coap_pdu_t *pdu, uint32_t *outCode, CAInfo_t * if (optionResult[0] != '\0') { - OIC_LOG_V(DEBUG, TAG, "URL length:%d", strlen(optionResult)); + OIC_LOG_V(DEBUG, TAG, "URL length:%zu", strlen(optionResult)); outInfo->resourceUri = OICStrdup(optionResult); if (!outInfo->resourceUri) { @@ -756,7 +947,6 @@ CAResult_t CAGetInfoFromPDU(const coap_pdu_t *pdu, uint32_t *outCode, CAInfo_t * } } - OIC_LOG(DEBUG, TAG, "OUT"); return CA_STATUS_OK; exit: @@ -765,50 +955,50 @@ exit: return CA_STATUS_FAILED; } -CAResult_t CAGetTokenFromPDU(const coap_hdr_t *pdu_hdr, CAInfo_t *outInfo) +CAResult_t CAGetTokenFromPDU(const coap_hdr_t *pdu_hdr, CAInfo_t *outInfo, + const CAEndpoint_t *endpoint) { - OIC_LOG(DEBUG, TAG, "IN"); - if (NULL == pdu_hdr) + VERIFY_NON_NULL(pdu_hdr, TAG, "pdu_hdr"); + VERIFY_NON_NULL(outInfo, TAG, "outInfo"); + VERIFY_NON_NULL(endpoint, TAG, "endpoint"); + + coap_transport_type transport; +#ifdef WITH_TCP + if (CAIsSupportedCoAPOverTCP(endpoint->adapter)) { - OIC_LOG(ERROR, TAG, "pdu_hdr is null"); - return CA_STATUS_INVALID_PARAM; + transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)pdu_hdr)[0] >> 4); } - - if (NULL == outInfo) + else +#endif { - OIC_LOG(ERROR, TAG, "outInfo is null"); - return CA_STATUS_INVALID_PARAM; + transport = coap_udp; } + unsigned char* token = NULL; + unsigned int token_length = 0; + coap_get_token(pdu_hdr, transport, &token, &token_length); + // set token data - if (pdu_hdr->token_length > 0) + if (token_length > 0) { - OIC_LOG_V(DEBUG, TAG, "token len:%d", pdu_hdr->token_length); - outInfo->token = (char *) OICMalloc(pdu_hdr->token_length); + OIC_LOG_V(DEBUG, TAG, "token len:%d", token_length); + outInfo->token = (char *) OICMalloc(token_length); if (NULL == outInfo->token) { - OIC_LOG(ERROR, TAG, "out of memory"); + OIC_LOG(ERROR, TAG, "Out of memory"); return CA_MEMORY_ALLOC_FAILED; } - memcpy(outInfo->token, pdu_hdr->token, pdu_hdr->token_length); + memcpy(outInfo->token, token, token_length); } - outInfo->tokenLength = pdu_hdr->token_length; - - OIC_LOG(DEBUG, TAG, "OUT"); + outInfo->tokenLength = token_length; return CA_STATUS_OK; } CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength) { - OIC_LOG(DEBUG, TAG, "IN"); - - if (!token) - { - OIC_LOG(ERROR, TAG, "invalid token pointer"); - return CA_STATUS_INVALID_PARAM; - } + VERIFY_NON_NULL(token, TAG, "token"); if ((tokenLength > CA_MAX_TOKEN_LEN) || (0 == tokenLength)) { @@ -816,26 +1006,6 @@ CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength) return CA_STATUS_INVALID_PARAM; } - if (SEED == 0) - { -#ifdef ARDUINO - SEED = now(); -#else - SEED = time(NULL); -#endif - if (SEED == (unsigned int)((time_t)-1)) - { - OIC_LOG(ERROR, TAG, "seed is not made"); - SEED = 0; - return CA_STATUS_FAILED; - } -#if HAVE_SRANDOM - srandom(SEED); -#else - srand(SEED); -#endif - } - // memory allocation char *temp = (char *) OICCalloc(tokenLength, sizeof(char)); if (NULL == temp) @@ -844,16 +1014,7 @@ CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength) return CA_MEMORY_ALLOC_FAILED; } - // set random byte - for (uint8_t index = 0; index < tokenLength; index++) - { - // use valid characters -#ifdef ARDUINO - temp[index] = rand() & 0x00FF; -#else - temp[index] = random() & 0x00FF; -#endif - } + OCFillRandomMem((uint8_t *)temp, tokenLength); // save token *token = temp; @@ -861,47 +1022,20 @@ CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength) OIC_LOG_V(DEBUG, TAG, "token len:%d, token:", tokenLength); OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)(*token), tokenLength); - OIC_LOG(DEBUG, TAG, "OUT"); return CA_STATUS_OK; } void CADestroyTokenInternal(CAToken_t token) { - OIC_LOG(DEBUG, TAG, "IN"); OICFree(token); - OIC_LOG(DEBUG, TAG, "OUT"); } -void CADestroyInfo(CAInfo_t *info) +uint32_t CAGetOptionData(uint16_t key, const uint8_t *data, uint32_t len, + uint8_t *option, uint32_t buflen) { - OIC_LOG(DEBUG, TAG, "IN"); - - if (NULL != info) - { - OIC_LOG(DEBUG, TAG, "free options"); - OICFree(info->options); - - OIC_LOG(DEBUG, TAG, "free token"); - OICFree(info->token); - - OIC_LOG(DEBUG, TAG, "free payload"); - OICFree(info->payload); - } - - OIC_LOG(DEBUG, TAG, "OUT"); -} - -uint32_t CAGetOptionData(const uint8_t *data, uint32_t len, uint8_t *option, uint32_t buflen) -{ - if (0 == buflen || 0 == len) - { - OIC_LOG(ERROR, TAG, "len 0"); - return 0; - } - - if (NULL == data || NULL == option) + if (0 == buflen) { - OIC_LOG(ERROR, TAG, "data/option NULL"); + OIC_LOG(ERROR, TAG, "buflen 0"); return 0; } @@ -911,19 +1045,25 @@ uint32_t CAGetOptionData(const uint8_t *data, uint32_t len, uint8_t *option, uin return 0; } - memcpy(option, data, len); - option[len] = '\0'; + coap_option_def_t* def = coap_opt_def(key); + if (NULL != def && coap_is_var_bytes(def) && 0 == len) + { + // A 0 length option is permitted in CoAP but the + // rest or the stack is unaware of variable byte encoding + // should remain that way so a 0 byte of length 1 is inserted. + len = 1; + option[0]=0; + } else { + memcpy(option, data, len); + option[len] = '\0'; + } return len; } CAMessageType_t CAGetMessageTypeFromPduBinaryData(const void *pdu, uint32_t size) { - if (NULL == pdu) - { - OIC_LOG(ERROR, TAG, "pdu is null"); - return CA_MSG_NONCONFIRM; - } + VERIFY_NON_NULL_RET(pdu, TAG, "pdu", CA_MSG_NONCONFIRM); // pdu minimum size is 4 byte. if (size < CA_PDU_MIN_SIZE) @@ -934,16 +1074,12 @@ CAMessageType_t CAGetMessageTypeFromPduBinaryData(const void *pdu, uint32_t size coap_hdr_t *hdr = (coap_hdr_t *) pdu; - return (CAMessageType_t) hdr->type; + return (CAMessageType_t) hdr->coap_hdr_udp_t.type; } uint16_t CAGetMessageIdFromPduBinaryData(const void *pdu, uint32_t size) { - if (NULL == pdu) - { - OIC_LOG(ERROR, TAG, "pdu is null"); - return 0; - } + VERIFY_NON_NULL_RET(pdu, TAG, "pdu", 0); // pdu minimum size is 4 byte. if (size < CA_PDU_MIN_SIZE) @@ -954,16 +1090,12 @@ uint16_t CAGetMessageIdFromPduBinaryData(const void *pdu, uint32_t size) coap_hdr_t *hdr = (coap_hdr_t *) pdu; - return hdr->id; + return hdr->coap_hdr_udp_t.id; } CAResponseResult_t CAGetCodeFromPduBinaryData(const void *pdu, uint32_t size) { - if (NULL == pdu) - { - OIC_LOG(ERROR, TAG, "pdu is null"); - return CA_NOT_FOUND; - } + VERIFY_NON_NULL_RET(pdu, TAG, "pdu", CA_NOT_FOUND); // pdu minimum size is 4 byte. if (size < CA_PDU_MIN_SIZE) @@ -974,5 +1106,54 @@ CAResponseResult_t CAGetCodeFromPduBinaryData(const void *pdu, uint32_t size) coap_hdr_t *hdr = (coap_hdr_t *) pdu; - return (CAResponseResult_t) CA_RESPONSE_CODE(hdr->code); + return (CAResponseResult_t) CA_RESPONSE_CODE(hdr->coap_hdr_udp_t.code); } + +CAPayloadFormat_t CAConvertFormat(uint8_t format) +{ + switch (format) + { + case COAP_MEDIATYPE_TEXT_PLAIN: + return CA_FORMAT_TEXT_PLAIN; + case COAP_MEDIATYPE_APPLICATION_LINK_FORMAT: + return CA_FORMAT_APPLICATION_LINK_FORMAT; + case COAP_MEDIATYPE_APPLICATION_XML: + return CA_FORMAT_APPLICATION_XML; + case COAP_MEDIATYPE_APPLICATION_OCTET_STREAM: + return CA_FORMAT_APPLICATION_OCTET_STREAM; + case COAP_MEDIATYPE_APPLICATION_RDF_XML: + return CA_FORMAT_APPLICATION_RDF_XML; + case COAP_MEDIATYPE_APPLICATION_EXI: + return CA_FORMAT_APPLICATION_EXI; + case COAP_MEDIATYPE_APPLICATION_JSON: + return CA_FORMAT_APPLICATION_JSON; + case COAP_MEDIATYPE_APPLICATION_CBOR: + return CA_FORMAT_APPLICATION_CBOR; + default: + return CA_FORMAT_UNSUPPORTED; + } +} + +#ifdef WITH_BWT +bool CAIsSupportedBlockwiseTransfer(CATransportAdapter_t adapter) +{ + if (CA_ADAPTER_IP & adapter || CA_ADAPTER_NFC & adapter + || CA_DEFAULT_ADAPTER == adapter) + { + return true; + } + return false; +} +#endif + +#ifdef WITH_TCP +bool CAIsSupportedCoAPOverTCP(CATransportAdapter_t adapter) +{ + if (CA_ADAPTER_GATT_BTLE & adapter || CA_ADAPTER_RFCOMM_BTEDR & adapter + || CA_ADAPTER_TCP & adapter || CA_DEFAULT_ADAPTER == adapter) + { + return true; + } + return false; +} +#endif