1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
21 // Defining _BSD_SOURCE or _DEFAULT_SOURCE causes header files to expose
22 // definitions that may otherwise be skipped. Skipping can cause implicit
23 // declaration warnings and/or bugs and subtle problems in code execution.
24 // For glibc information on feature test macros,
25 // Refer http://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html
27 // For details on compatibility and glibc support,
28 // Refer http://www.gnu.org/software/libc/manual/html_node/BSD-Random.html
29 #define _DEFAULT_SOURCE
31 #include "iotivity_config.h"
40 #include "caprotocolmessage.h"
42 #include "oic_malloc.h"
43 #include "oic_string.h"
45 #include "cacommonutil.h"
47 #define TAG "OIC_CA_PRTCL_MSG"
49 #define CA_PDU_MIN_SIZE (4)
50 #define CA_ENCODE_BUFFER_SIZE (4)
52 static const char COAP_URI_HEADER[] = "coap://[::]/";
55 static char g_chproxyUri[CA_MAX_URI_LENGTH];
57 CAResult_t CASetProxyUri(const char *uri)
59 VERIFY_NON_NULL(uri, TAG, "uri");
60 OICStrcpy(g_chproxyUri, sizeof (g_chproxyUri), uri);
65 CAResult_t CAGetRequestInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
66 CARequestInfo_t *outReqInfo)
68 VERIFY_NON_NULL(pdu, TAG, "pdu");
69 VERIFY_NON_NULL(outReqInfo, TAG, "outReqInfo");
71 uint32_t code = CA_NOT_FOUND;
72 CAResult_t ret = CAGetInfoFromPDU(pdu, endpoint, &code, &(outReqInfo->info));
73 outReqInfo->method = code;
78 CAResult_t CAGetResponseInfoFromPDU(const coap_pdu_t *pdu, CAResponseInfo_t *outResInfo,
79 const CAEndpoint_t *endpoint)
81 VERIFY_NON_NULL(pdu, TAG, "pdu");
82 VERIFY_NON_NULL(outResInfo, TAG, "outResInfo");
84 uint32_t code = CA_NOT_FOUND;
85 CAResult_t ret = CAGetInfoFromPDU(pdu, endpoint, &code, &(outResInfo->info));
86 outResInfo->result = code;
91 CAResult_t CAGetErrorInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
92 CAErrorInfo_t *errorInfo)
94 VERIFY_NON_NULL(pdu, TAG, "pdu");
97 CAResult_t ret = CAGetInfoFromPDU(pdu, endpoint, &code, &errorInfo->info);
102 coap_pdu_t *CAGeneratePDU(uint32_t code, const CAInfo_t *info, const CAEndpoint_t *endpoint,
103 coap_list_t **optlist, coap_transport_t *transport)
105 VERIFY_NON_NULL_RET(info, TAG, "info", NULL);
106 VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", NULL);
107 VERIFY_NON_NULL_RET(optlist, TAG, "optlist", NULL);
109 coap_pdu_t *pdu = NULL;
111 // RESET have to use only 4byte (empty message)
112 // and ACKNOWLEDGE can use empty message when code is empty.
113 if (CA_MSG_RESET == info->type || (CA_EMPTY == code && CA_MSG_ACKNOWLEDGE == info->type))
115 if (CA_EMPTY != code)
117 OIC_LOG(ERROR, TAG, "reset is not empty message");
121 if (info->payloadSize > 0 || info->payload || info->token || info->tokenLength > 0)
123 OIC_LOG(ERROR, TAG, "Empty message has unnecessary data after messageID");
127 OIC_LOG(DEBUG, TAG, "code is empty");
128 if (!(pdu = CAGeneratePDUImpl((code_t) code, info, endpoint, NULL, transport)))
130 OIC_LOG(ERROR, TAG, "pdu NULL");
136 if (info->resourceUri)
138 uint32_t length = strlen(info->resourceUri);
139 if (CA_MAX_URI_LENGTH < length)
141 OIC_LOG(ERROR, TAG, "URI len err");
145 uint32_t uriLength = length + sizeof(COAP_URI_HEADER);
146 char *coapUri = (char *) OICCalloc(1, uriLength);
149 OIC_LOG(ERROR, TAG, "out of memory");
152 OICStrcat(coapUri, uriLength, COAP_URI_HEADER);
153 OICStrcat(coapUri, uriLength, info->resourceUri);
155 // parsing options in URI
156 CAResult_t res = CAParseURI(coapUri, optlist);
157 if (CA_STATUS_OK != res)
165 // parsing options in HeadOption
166 CAResult_t ret = CAParseHeadOption(code, info, optlist);
167 if (CA_STATUS_OK != ret)
172 pdu = CAGeneratePDUImpl((code_t) code, info, endpoint, *optlist, transport);
175 OIC_LOG(ERROR, TAG, "pdu NULL");
180 // pdu print method : coap_show_pdu(pdu);
184 coap_pdu_t *CAParsePDU(const char *data, uint32_t length, uint32_t *outCode,
185 const CAEndpoint_t *endpoint)
187 VERIFY_NON_NULL_RET(data, TAG, "data", NULL);
188 VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", NULL);
190 coap_transport_t transport = COAP_UDP;
192 if (CAIsSupportedCoAPOverTCP(endpoint->adapter))
194 transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)data)[0] >> 4);
198 coap_pdu_t *outpdu = coap_new_pdu2(transport, length);
201 OIC_LOG(ERROR, TAG, "outpdu is null");
205 OIC_LOG_V(DEBUG, TAG, "pdu parse-transport type : %d", transport);
207 int ret = coap_pdu_parse2((unsigned char *) data, length, outpdu, transport);
208 OIC_LOG_V(DEBUG, TAG, "pdu parse ret: %d", ret);
211 OIC_LOG(ERROR, TAG, "pdu parse failed");
216 if (CAIsSupportedCoAPOverTCP(endpoint->adapter))
218 OIC_LOG(INFO, TAG, "there is no version info in coap header");
223 if (outpdu->transport_hdr->udp.version != COAP_DEFAULT_VERSION)
225 OIC_LOG_V(ERROR, TAG, "coap version is not available : %d",
226 outpdu->transport_hdr->udp.version);
229 if (outpdu->transport_hdr->udp.token_length > CA_MAX_TOKEN_LEN)
231 OIC_LOG_V(ERROR, TAG, "token length has been exceed : %d",
232 outpdu->transport_hdr->udp.token_length);
239 (*outCode) = (uint32_t) CA_RESPONSE_CODE(coap_get_code(outpdu, transport));
245 coap_delete_pdu(outpdu);
249 coap_pdu_t *CAGeneratePDUImpl(code_t code, const CAInfo_t *info,
250 const CAEndpoint_t *endpoint, coap_list_t *options,
251 coap_transport_t *transport)
253 VERIFY_NON_NULL_RET(info, TAG, "info", NULL);
254 VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", NULL);
255 VERIFY_NON_NULL_RET(transport, TAG, "transport", NULL);
257 unsigned int length = COAP_MAX_PDU_SIZE;
259 unsigned int msgLength = 0;
260 if (CAIsSupportedCoAPOverTCP(endpoint->adapter))
264 unsigned short prevOptNumber = 0;
265 for (coap_list_t *opt = options; opt; opt = opt->next)
267 unsigned short curOptNumber = COAP_OPTION_KEY(*(coap_option *) opt->data);
268 if (prevOptNumber > curOptNumber)
270 OIC_LOG(ERROR, TAG, "option list is wrong");
274 size_t optValueLen = COAP_OPTION_LENGTH(*(coap_option *) opt->data);
275 size_t optLength = coap_get_opt_header_length(curOptNumber - prevOptNumber, optValueLen);
278 OIC_LOG(ERROR, TAG, "Reserved for the Payload marker for the option");
281 msgLength += optLength;
282 prevOptNumber = curOptNumber;
283 OIC_LOG_V(DEBUG, TAG, "curOptNumber[%d], prevOptNumber[%d], optValueLen[%zu], "
284 "optLength[%zu], msgLength[%d]",
285 curOptNumber, prevOptNumber, optValueLen, optLength, msgLength);
289 if (info->payloadSize > 0)
291 msgLength = msgLength + info->payloadSize + PAYLOAD_MARKER;
293 *transport = coap_get_tcp_header_type_from_size(msgLength);
294 length = msgLength + coap_get_tcp_header_length_for_transport(*transport)
300 *transport = COAP_UDP;
303 coap_pdu_t *pdu = coap_new_pdu2(*transport, length);
307 OIC_LOG(ERROR, TAG, "malloc failed");
311 OIC_LOG_V(DEBUG, TAG, "transport type: %d, payload size: %zu",
312 *transport, info->payloadSize);
315 if (CAIsSupportedCoAPOverTCP(endpoint->adapter))
317 coap_add_length(pdu, *transport, msgLength);
322 OIC_LOG_V(DEBUG, TAG, "msgID is %d", info->messageId);
323 uint16_t message_id = 0;
324 if (0 == info->messageId)
326 /* initialize message id */
327 prng((uint8_t * ) &message_id, sizeof(message_id));
329 OIC_LOG_V(DEBUG, TAG, "gen msg id=%d", message_id);
333 /* use saved message id */
334 message_id = info->messageId;
336 pdu->transport_hdr->udp.id = message_id;
337 OIC_LOG_V(DEBUG, TAG, "messageId in pdu is %d, %d", message_id, pdu->transport_hdr->udp.id);
339 pdu->transport_hdr->udp.type = info->type;
342 coap_add_code(pdu, *transport, code);
344 if (info->token && CA_EMPTY != code)
346 uint32_t tokenLength = info->tokenLength;
347 OIC_LOG_V(DEBUG, TAG, "token info token length: %d, token :", tokenLength);
348 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)info->token, tokenLength);
350 int32_t ret = coap_add_token2(pdu, tokenLength, (unsigned char *)info->token, *transport);
353 OIC_LOG(ERROR, TAG, "can't add token");
358 if (CA_ADAPTER_GATT_BTLE != endpoint->adapter
360 && !CAIsSupportedCoAPOverTCP(endpoint->adapter)
364 // option list will be added in blockwise-transfer
371 for (coap_list_t *opt = options; opt; opt = opt->next)
373 OIC_LOG_V(DEBUG, TAG, "[%s] opt will be added.",
374 COAP_OPTION_DATA(*(coap_option *) opt->data));
376 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length", pdu->length);
377 coap_add_option2(pdu, COAP_OPTION_KEY(*(coap_option *) opt->data),
378 COAP_OPTION_LENGTH(*(coap_option *) opt->data),
379 COAP_OPTION_DATA(*(coap_option *) opt->data), *transport);
383 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length after option", pdu->length);
385 if (NULL != info->payload && 0 < info->payloadSize)
387 OIC_LOG(DEBUG, TAG, "payload is added");
388 coap_add_data(pdu, info->payloadSize, (const unsigned char *) info->payload);
394 CAResult_t CAParseURI(const char *uriInfo, coap_list_t **optlist)
396 VERIFY_NON_NULL(uriInfo, TAG, "uriInfo");
397 VERIFY_NON_NULL(optlist, TAG, "optlist");
399 OIC_LOG_V(DEBUG, TAG, "url : %s", uriInfo);
401 /* split arg into Uri-* options */
403 coap_split_uri((unsigned char *) uriInfo, strlen(uriInfo), &uri);
405 if (uri.port != COAP_DEFAULT_PORT)
407 unsigned char portbuf[CA_ENCODE_BUFFER_SIZE] = { 0 };
408 int ret = coap_insert(optlist,
409 CACreateNewOptionNode(COAP_OPTION_URI_PORT,
410 coap_encode_var_bytes(portbuf, uri.port),
415 return CA_STATUS_INVALID_PARAM;
419 if (uri.path.s && uri.path.length)
421 CAResult_t ret = CAParseUriPartial(uri.path.s, uri.path.length,
422 COAP_OPTION_URI_PATH, optlist);
423 if (CA_STATUS_OK != ret)
425 OIC_LOG(ERROR, TAG, "CAParseUriPartial failed(uri path)");
430 if (uri.query.s && uri.query.length)
432 CAResult_t ret = CAParseUriPartial(uri.query.s, uri.query.length, COAP_OPTION_URI_QUERY,
434 if (CA_STATUS_OK != ret)
436 OIC_LOG(ERROR, TAG, "CAParseUriPartial failed(uri query)");
444 CAResult_t CAParseUriPartial(const unsigned char *str, size_t length, int target,
445 coap_list_t **optlist)
447 VERIFY_NON_NULL(optlist, TAG, "optlist");
449 if ((target != COAP_OPTION_URI_PATH) && (target != COAP_OPTION_URI_QUERY))
451 // should never occur. Log just in case.
452 OIC_LOG(DEBUG, TAG, "Unexpected URI component.");
453 return CA_NOT_SUPPORTED;
455 else if (str && length)
457 unsigned char uriBuffer[CA_MAX_URI_LENGTH] = { 0 };
458 unsigned char *pBuf = uriBuffer;
459 size_t buflen = sizeof(uriBuffer);
460 int res = (target == COAP_OPTION_URI_PATH) ? coap_split_path(str, length, pBuf, &buflen) :
461 coap_split_query(str, length, pBuf, &buflen);
468 int ret = coap_insert(optlist,
469 CACreateNewOptionNode(target, COAP_OPT_LENGTH(pBuf),
470 (char *)COAP_OPT_VALUE(pBuf)),
474 return CA_STATUS_INVALID_PARAM;
477 size_t optSize = COAP_OPT_SIZE(pBuf);
478 if ((prevIdx + optSize) < buflen)
487 OIC_LOG_V(ERROR, TAG, "Problem parsing URI : %d for %d", res, target);
488 return CA_STATUS_FAILED;
493 OIC_LOG(ERROR, TAG, "str or length is not available");
494 return CA_STATUS_FAILED;
500 CAResult_t CAParseHeadOption(uint32_t code, const CAInfo_t *info, coap_list_t **optlist)
503 VERIFY_NON_NULL_RET(info, TAG, "info", CA_STATUS_INVALID_PARAM);
505 OIC_LOG_V(DEBUG, TAG, "parse Head Opt: %d", info->numOptions);
509 OIC_LOG(ERROR, TAG, "optlist is null");
510 return CA_STATUS_INVALID_PARAM;
513 for (uint32_t i = 0; i < info->numOptions; i++)
515 if(!(info->options + i))
517 OIC_LOG(ERROR, TAG, "options is not available");
518 return CA_STATUS_FAILED;
521 uint32_t id = (info->options + i)->optionID;
522 if (COAP_OPTION_URI_PATH == id || COAP_OPTION_URI_QUERY == id)
524 OIC_LOG_V(DEBUG, TAG, "not Header Opt: %d", id);
528 OIC_LOG_V(DEBUG, TAG, "Head opt ID: %d", id);
529 OIC_LOG_V(DEBUG, TAG, "Head opt data: %s", (info->options + i)->optionData);
530 OIC_LOG_V(DEBUG, TAG, "Head opt length: %d", (info->options + i)->optionLength);
531 int ret = coap_insert(optlist,
532 CACreateNewOptionNode(id, (info->options + i)->optionLength,
533 (info->options + i)->optionData),
537 return CA_STATUS_INVALID_PARAM;
542 // insert one extra header with the payload format if applicable.
543 if (CA_FORMAT_UNDEFINED != info->payloadFormat)
545 coap_list_t* node = NULL;
546 uint8_t buf[CA_ENCODE_BUFFER_SIZE] = {0};
547 switch (info->payloadFormat)
549 case CA_FORMAT_APPLICATION_CBOR:
550 node = CACreateNewOptionNode(
551 COAP_OPTION_CONTENT_FORMAT,
552 coap_encode_var_bytes(buf, (unsigned short)COAP_MEDIATYPE_APPLICATION_CBOR),
556 OIC_LOG_V(ERROR, TAG, "format option:[%d] not supported", info->payloadFormat);
560 OIC_LOG(ERROR, TAG, "format option not created");
561 return CA_STATUS_INVALID_PARAM;
563 int ret = coap_insert(optlist, node, CAOrderOpts);
567 OIC_LOG(ERROR, TAG, "format option not inserted in header");
568 return CA_STATUS_INVALID_PARAM;
571 if (CA_FORMAT_UNDEFINED != info->acceptFormat)
573 coap_list_t* node = NULL;
574 uint8_t buf[CA_ENCODE_BUFFER_SIZE] = {0};
575 switch (info->acceptFormat)
577 case CA_FORMAT_APPLICATION_CBOR:
578 node = CACreateNewOptionNode(
580 coap_encode_var_bytes(buf, (unsigned short)COAP_MEDIATYPE_APPLICATION_CBOR),
584 OIC_LOG_V(ERROR, TAG, "format option:[%d] not supported", info->acceptFormat);
588 OIC_LOG(ERROR, TAG, "format option not created");
589 return CA_STATUS_INVALID_PARAM;
591 int ret = coap_insert(optlist, node, CAOrderOpts);
595 OIC_LOG(ERROR, TAG, "format option not inserted in header");
596 return CA_STATUS_INVALID_PARAM;
603 coap_list_t *CACreateNewOptionNode(uint16_t key, uint32_t length, const char *data)
605 VERIFY_NON_NULL_RET(data, TAG, "data", NULL);
607 coap_option *option = coap_malloc(sizeof(coap_option) + length + 1);
610 OIC_LOG(ERROR, TAG, "Out of memory");
613 memset(option, 0, sizeof(coap_option) + length + 1);
615 COAP_OPTION_KEY(*option) = key;
617 coap_option_def_t* def = coap_opt_def(key);
618 if (NULL != def && coap_is_var_bytes(def))
620 if (length > def->max)
622 // make sure we shrink the value so it fits the coap option definition
623 // by truncating the value, disregard the leading bytes.
624 OIC_LOG_V(DEBUG, TAG, "Option [%d] data size [%d] shrunk to [%d]",
625 def->key, length, def->max);
626 data = &(data[length-def->max]);
629 // Shrink the encoding length to a minimum size for coap
630 // options that support variable length encoding.
631 COAP_OPTION_LENGTH(*option) = coap_encode_var_bytes(
632 COAP_OPTION_DATA(*option),
633 coap_decode_var_bytes((unsigned char *)data, length));
637 COAP_OPTION_LENGTH(*option) = length;
638 memcpy(COAP_OPTION_DATA(*option), data, length);
641 /* we can pass NULL here as delete function since option is released automatically */
642 coap_list_t *node = coap_new_listnode(option, NULL);
646 OIC_LOG(ERROR, TAG, "node is NULL");
654 int CAOrderOpts(void *a, void *b)
658 return a < b ? -1 : 1;
661 if (COAP_OPTION_KEY(*(coap_option *) a) < COAP_OPTION_KEY(*(coap_option * ) b))
666 return COAP_OPTION_KEY(*(coap_option *) a) == COAP_OPTION_KEY(*(coap_option * ) b);
669 uint32_t CAGetOptionCount(coap_opt_iterator_t opt_iter)
672 coap_opt_t *option = NULL;
674 while ((option = coap_option_next(&opt_iter)))
676 if (COAP_OPTION_URI_PATH != opt_iter.type && COAP_OPTION_URI_QUERY != opt_iter.type
677 && COAP_OPTION_BLOCK1 != opt_iter.type && COAP_OPTION_BLOCK2 != opt_iter.type
678 && COAP_OPTION_SIZE1 != opt_iter.type && COAP_OPTION_SIZE2 != opt_iter.type
679 && COAP_OPTION_CONTENT_FORMAT != opt_iter.type
680 && COAP_OPTION_ACCEPT != opt_iter.type
681 && COAP_OPTION_URI_HOST != opt_iter.type && COAP_OPTION_URI_PORT != opt_iter.type
682 && COAP_OPTION_ETAG != opt_iter.type && COAP_OPTION_MAXAGE != opt_iter.type
683 && COAP_OPTION_PROXY_SCHEME != opt_iter.type)
692 CAResult_t CAGetInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
693 uint32_t *outCode, CAInfo_t *outInfo)
695 VERIFY_NON_NULL(pdu, TAG, "pdu");
696 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
697 VERIFY_NON_NULL(outCode, TAG, "outCode");
698 VERIFY_NON_NULL(outInfo, TAG, "outInfo");
700 coap_transport_t transport;
702 if (CAIsSupportedCoAPOverTCP(endpoint->adapter))
704 transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)pdu->transport_hdr)[0] >> 4);
709 transport = COAP_UDP;
712 coap_opt_iterator_t opt_iter;
713 coap_option_iterator_init2((coap_pdu_t *) pdu, &opt_iter, COAP_OPT_ALL, transport);
717 (*outCode) = (uint32_t) CA_RESPONSE_CODE(coap_get_code(pdu, transport));
720 // init HeaderOption list
721 uint32_t count = CAGetOptionCount(opt_iter);
722 memset(outInfo, 0, sizeof(*outInfo));
724 outInfo->numOptions = count;
727 if (CAIsSupportedCoAPOverTCP(endpoint->adapter))
730 outInfo->type = CA_MSG_NONCONFIRM;
731 outInfo->payloadFormat = CA_FORMAT_UNDEFINED;
739 outInfo->type = pdu->transport_hdr->udp.type;
742 outInfo->messageId = pdu->transport_hdr->udp.id;
743 outInfo->payloadFormat = CA_FORMAT_UNDEFINED;
744 outInfo->acceptFormat = CA_FORMAT_UNDEFINED;
749 outInfo->options = (CAHeaderOption_t *) OICCalloc(count, sizeof(CAHeaderOption_t));
750 if (NULL == outInfo->options)
752 OIC_LOG(ERROR, TAG, "Out of memory");
753 return CA_MEMORY_ALLOC_FAILED;
757 coap_opt_t *option = NULL;
758 char optionResult[CA_MAX_URI_LENGTH] = {0};
760 uint32_t optionLength = 0;
761 bool isfirstsetflag = false;
762 bool isQueryBeingProcessed = false;
764 bool isProxyRequest = false;
767 while ((option = coap_option_next(&opt_iter)))
769 char buf[COAP_MAX_PDU_SIZE] = {0};
771 CAGetOptionData(opt_iter.type, (uint8_t *)(COAP_OPT_VALUE(option)),
772 COAP_OPT_LENGTH(option), (uint8_t *)buf, sizeof(buf));
775 OIC_LOG_V(DEBUG, TAG, "COAP URI element : %s", buf);
776 if (COAP_OPTION_URI_PATH == opt_iter.type || COAP_OPTION_URI_QUERY == opt_iter.type)
778 if (false == isfirstsetflag)
780 isfirstsetflag = true;
781 optionResult[optionLength] = '/';
783 // Make sure there is enough room in the optionResult buffer
784 if ((optionLength + bufLength) < sizeof(optionResult))
786 memcpy(&optionResult[optionLength], buf, bufLength);
787 optionLength += bufLength;
796 if (COAP_OPTION_URI_PATH == opt_iter.type)
798 // Make sure there is enough room in the optionResult buffer
799 if (optionLength < sizeof(optionResult))
801 optionResult[optionLength] = '/';
809 else if (COAP_OPTION_URI_QUERY == opt_iter.type)
811 if (false == isQueryBeingProcessed)
813 // Make sure there is enough room in the optionResult buffer
814 if (optionLength < sizeof(optionResult))
816 optionResult[optionLength] = '?';
818 isQueryBeingProcessed = true;
827 // Make sure there is enough room in the optionResult buffer
828 if (optionLength < sizeof(optionResult))
830 optionResult[optionLength] = ';';
839 // Make sure there is enough room in the optionResult buffer
840 if ((optionLength + bufLength) < sizeof(optionResult))
842 memcpy(&optionResult[optionLength], buf, bufLength);
843 optionLength += bufLength;
851 else if (COAP_OPTION_BLOCK1 == opt_iter.type || COAP_OPTION_BLOCK2 == opt_iter.type
852 || COAP_OPTION_SIZE1 == opt_iter.type || COAP_OPTION_SIZE2 == opt_iter.type)
854 OIC_LOG_V(DEBUG, TAG, "option[%d] will be filtering", opt_iter.type);
856 else if (COAP_OPTION_CONTENT_FORMAT == opt_iter.type)
858 if (1 == COAP_OPT_LENGTH(option))
860 outInfo->payloadFormat = CAConvertFormat((uint8_t)buf[0]);
864 outInfo->payloadFormat = CA_FORMAT_UNSUPPORTED;
865 OIC_LOG_V(DEBUG, TAG, "option[%d] has an unsupported format [%d]",
866 opt_iter.type, (uint8_t)buf[0]);
869 else if (COAP_OPTION_ACCEPT == opt_iter.type)
871 if (1 == COAP_OPT_LENGTH(option))
873 outInfo->acceptFormat = CAConvertFormat((uint8_t)buf[0]);
877 outInfo->acceptFormat = CA_FORMAT_UNSUPPORTED;
879 OIC_LOG_V(DEBUG, TAG, "option[%d] has an unsupported format [%d]",
880 opt_iter.type, (uint8_t)buf[0]);
882 else if (COAP_OPTION_URI_PORT == opt_iter.type ||
883 COAP_OPTION_URI_HOST == opt_iter.type ||
884 COAP_OPTION_ETAG == opt_iter.type ||
885 COAP_OPTION_MAXAGE == opt_iter.type ||
886 COAP_OPTION_PROXY_SCHEME== opt_iter.type)
888 OIC_LOG_V(INFO, TAG, "option[%d] has an unsupported format [%d]",
889 opt_iter.type, (uint8_t)buf[0]);
894 if (COAP_OPTION_PROXY_URI == opt_iter.type)
896 isProxyRequest = true;
901 if (bufLength <= sizeof(outInfo->options[0].optionData))
903 outInfo->options[idx].optionID = opt_iter.type;
904 outInfo->options[idx].optionLength = bufLength;
905 outInfo->options[idx].protocolID = CA_COAP_ID;
906 memcpy(outInfo->options[idx].optionData, buf, bufLength);
914 unsigned char* token = NULL;
915 unsigned int token_length = 0;
916 coap_get_token2(pdu->transport_hdr, transport, &token, &token_length);
919 if (token_length > 0)
921 OIC_LOG_V(DEBUG, TAG, "inside token length : %d", token_length);
922 outInfo->token = (char *) OICMalloc(token_length);
923 if (NULL == outInfo->token)
925 OIC_LOG(ERROR, TAG, "Out of memory");
926 OICFree(outInfo->options);
927 return CA_MEMORY_ALLOC_FAILED;
929 memcpy(outInfo->token, token, token_length);
932 outInfo->tokenLength = token_length;
937 if (coap_get_data(pdu, &dataSize, &data))
939 OIC_LOG(DEBUG, TAG, "inside pdu->data");
940 outInfo->payload = (uint8_t *) OICMalloc(dataSize);
941 if (NULL == outInfo->payload)
943 OIC_LOG(ERROR, TAG, "Out of memory");
944 OICFree(outInfo->options);
945 OICFree(outInfo->token);
946 return CA_MEMORY_ALLOC_FAILED;
948 memcpy(outInfo->payload, pdu->data, dataSize);
949 outInfo->payloadSize = dataSize;
952 if (optionResult[0] != '\0')
954 OIC_LOG_V(DEBUG, TAG, "URL length:%zu", strlen(optionResult));
955 outInfo->resourceUri = OICStrdup(optionResult);
956 if (!outInfo->resourceUri)
958 OIC_LOG(ERROR, TAG, "Out of memory");
959 OICFree(outInfo->options);
960 OICFree(outInfo->token);
961 return CA_MEMORY_ALLOC_FAILED;
965 else if(isProxyRequest && g_chproxyUri[0] != '\0')
968 * A request for CoAP-HTTP Proxy will not have any uri element as per CoAP specs
969 * and only COAP_OPTION_PROXY_URI will be present. Use preset proxy uri
972 outInfo->resourceUri = OICStrdup(g_chproxyUri);
973 if (!outInfo->resourceUri)
975 OIC_LOG(ERROR, TAG, "Out of memory");
976 OICFree(outInfo->options);
977 OICFree(outInfo->token);
978 return CA_MEMORY_ALLOC_FAILED;
985 OIC_LOG(ERROR, TAG, "buffer too small");
986 OICFree(outInfo->options);
987 return CA_STATUS_FAILED;
990 CAResult_t CAGetTokenFromPDU(const coap_hdr_transport_t *pdu_hdr,
992 const CAEndpoint_t *endpoint)
994 VERIFY_NON_NULL(pdu_hdr, TAG, "pdu_hdr");
995 VERIFY_NON_NULL(outInfo, TAG, "outInfo");
996 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
998 coap_transport_t transport = COAP_UDP;
1000 if (CAIsSupportedCoAPOverTCP(endpoint->adapter))
1002 transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)pdu_hdr)[0] >> 4);
1006 unsigned char* token = NULL;
1007 unsigned int token_length = 0;
1008 coap_get_token2(pdu_hdr, transport, &token, &token_length);
1011 if (token_length > 0)
1013 OIC_LOG_V(DEBUG, TAG, "token len:%d", token_length);
1014 outInfo->token = (char *) OICMalloc(token_length);
1015 if (NULL == outInfo->token)
1017 OIC_LOG(ERROR, TAG, "Out of memory");
1018 return CA_MEMORY_ALLOC_FAILED;
1020 memcpy(outInfo->token, token, token_length);
1023 outInfo->tokenLength = token_length;
1025 return CA_STATUS_OK;
1028 CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength)
1030 VERIFY_NON_NULL(token, TAG, "token");
1032 if ((tokenLength > CA_MAX_TOKEN_LEN) || (0 == tokenLength))
1034 OIC_LOG(ERROR, TAG, "invalid token length");
1035 return CA_STATUS_INVALID_PARAM;
1038 // memory allocation
1039 char *temp = (char *) OICCalloc(tokenLength, sizeof(char));
1042 OIC_LOG(ERROR, TAG, "Out of memory");
1043 return CA_MEMORY_ALLOC_FAILED;
1046 OCFillRandomMem((uint8_t *)temp, tokenLength);
1051 OIC_LOG_V(DEBUG, TAG, "token len:%d, token:", tokenLength);
1052 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)(*token), tokenLength);
1054 return CA_STATUS_OK;
1057 void CADestroyTokenInternal(CAToken_t token)
1062 uint32_t CAGetOptionData(uint16_t key, const uint8_t *data, uint32_t len,
1063 uint8_t *option, uint32_t buflen)
1067 OIC_LOG(ERROR, TAG, "buflen 0");
1073 OIC_LOG(ERROR, TAG, "option buffer too small");
1077 coap_option_def_t* def = coap_opt_def(key);
1078 if (NULL != def && coap_is_var_bytes(def) && 0 == len)
1080 // A 0 length option is permitted in CoAP but the
1081 // rest or the stack is unaware of variable byte encoding
1082 // should remain that way so a 0 byte of length 1 is inserted.
1088 memcpy(option, data, len);
1095 CAMessageType_t CAGetMessageTypeFromPduBinaryData(const void *pdu, uint32_t size)
1097 VERIFY_NON_NULL_RET(pdu, TAG, "pdu", CA_MSG_NONCONFIRM);
1099 // pdu minimum size is 4 byte.
1100 if (size < CA_PDU_MIN_SIZE)
1102 OIC_LOG(ERROR, TAG, "min size");
1103 return CA_MSG_NONCONFIRM;
1106 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1108 return (CAMessageType_t) hdr->type;
1111 uint16_t CAGetMessageIdFromPduBinaryData(const void *pdu, uint32_t size)
1113 VERIFY_NON_NULL_RET(pdu, TAG, "pdu", 0);
1115 // pdu minimum size is 4 byte.
1116 if (size < CA_PDU_MIN_SIZE)
1118 OIC_LOG(ERROR, TAG, "min size");
1122 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1127 CAResponseResult_t CAGetCodeFromPduBinaryData(const void *pdu, uint32_t size)
1129 VERIFY_NON_NULL_RET(pdu, TAG, "pdu", CA_NOT_FOUND);
1131 // pdu minimum size is 4 byte.
1132 if (size < CA_PDU_MIN_SIZE)
1134 OIC_LOG(ERROR, TAG, "min size");
1135 return CA_NOT_FOUND;
1138 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1140 return (CAResponseResult_t) CA_RESPONSE_CODE(hdr->code);
1143 CAPayloadFormat_t CAConvertFormat(uint8_t format)
1147 case COAP_MEDIATYPE_TEXT_PLAIN:
1148 return CA_FORMAT_TEXT_PLAIN;
1149 case COAP_MEDIATYPE_APPLICATION_LINK_FORMAT:
1150 return CA_FORMAT_APPLICATION_LINK_FORMAT;
1151 case COAP_MEDIATYPE_APPLICATION_XML:
1152 return CA_FORMAT_APPLICATION_XML;
1153 case COAP_MEDIATYPE_APPLICATION_OCTET_STREAM:
1154 return CA_FORMAT_APPLICATION_OCTET_STREAM;
1155 case COAP_MEDIATYPE_APPLICATION_RDF_XML:
1156 return CA_FORMAT_APPLICATION_RDF_XML;
1157 case COAP_MEDIATYPE_APPLICATION_EXI:
1158 return CA_FORMAT_APPLICATION_EXI;
1159 case COAP_MEDIATYPE_APPLICATION_JSON:
1160 return CA_FORMAT_APPLICATION_JSON;
1161 case COAP_MEDIATYPE_APPLICATION_CBOR:
1162 return CA_FORMAT_APPLICATION_CBOR;
1164 return CA_FORMAT_UNSUPPORTED;
1169 bool CAIsSupportedBlockwiseTransfer(CATransportAdapter_t adapter)
1171 if (CA_ADAPTER_IP & adapter || CA_ADAPTER_NFC & adapter
1172 || CA_DEFAULT_ADAPTER == adapter)
1181 bool CAIsSupportedCoAPOverTCP(CATransportAdapter_t adapter)
1183 if (CA_ADAPTER_GATT_BTLE & adapter || CA_ADAPTER_RFCOMM_BTEDR & adapter
1184 || CA_ADAPTER_TCP & adapter || CA_DEFAULT_ADAPTER == adapter)