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://[::]/";
54 static char g_chproxyUri[CA_MAX_URI_LENGTH];
56 CAResult_t CASetProxyUri(const char *uri)
58 VERIFY_NON_NULL(uri, TAG, "uri");
59 OICStrcpy(g_chproxyUri, sizeof (g_chproxyUri), uri);
63 CAResult_t CAGetRequestInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
64 CARequestInfo_t *outReqInfo)
66 VERIFY_NON_NULL(pdu, TAG, "pdu");
67 VERIFY_NON_NULL(outReqInfo, TAG, "outReqInfo");
69 uint32_t code = CA_NOT_FOUND;
70 CAResult_t ret = CAGetInfoFromPDU(pdu, endpoint, &code, &(outReqInfo->info));
71 outReqInfo->method = code;
76 CAResult_t CAGetResponseInfoFromPDU(const coap_pdu_t *pdu, CAResponseInfo_t *outResInfo,
77 const CAEndpoint_t *endpoint)
79 VERIFY_NON_NULL(pdu, TAG, "pdu");
80 VERIFY_NON_NULL(outResInfo, TAG, "outResInfo");
82 uint32_t code = CA_NOT_FOUND;
83 CAResult_t ret = CAGetInfoFromPDU(pdu, endpoint, &code, &(outResInfo->info));
84 outResInfo->result = code;
89 CAResult_t CAGetErrorInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
90 CAErrorInfo_t *errorInfo)
92 VERIFY_NON_NULL(pdu, TAG, "pdu");
95 CAResult_t ret = CAGetInfoFromPDU(pdu, endpoint, &code, &errorInfo->info);
100 coap_pdu_t *CAGeneratePDU(uint32_t code, const CAInfo_t *info, const CAEndpoint_t *endpoint,
101 coap_list_t **optlist, coap_transport_t *transport)
103 VERIFY_NON_NULL_RET(info, TAG, "info", NULL);
104 VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", NULL);
105 VERIFY_NON_NULL_RET(optlist, TAG, "optlist", NULL);
107 OIC_LOG_V(DEBUG, TAG, "generate pdu for [%d]adapter, [%d]flags",
108 endpoint->adapter, endpoint->flags);
110 coap_pdu_t *pdu = NULL;
112 // RESET have to use only 4byte (empty message)
113 // and ACKNOWLEDGE can use empty message when code is empty.
114 if (CA_MSG_RESET == info->type || (CA_EMPTY == code && CA_MSG_ACKNOWLEDGE == info->type))
116 if (CA_EMPTY != code)
118 OIC_LOG(ERROR, TAG, "reset is not empty message");
122 if (info->payloadSize > 0 || info->payload || info->token || info->tokenLength > 0)
124 OIC_LOG(ERROR, TAG, "Empty message has unnecessary data after messageID");
128 OIC_LOG(DEBUG, TAG, "code is empty");
129 if (!(pdu = CAGeneratePDUImpl((code_t) code, info, endpoint, NULL, transport)))
131 OIC_LOG(ERROR, TAG, "pdu NULL");
137 if (info->resourceUri)
139 OIC_LOG_V(DEBUG, TAG, "uri : %s", info->resourceUri);
141 uint32_t length = strlen(info->resourceUri);
142 if (CA_MAX_URI_LENGTH < length)
144 OIC_LOG(ERROR, TAG, "URI len err");
148 uint32_t uriLength = length + sizeof(COAP_URI_HEADER);
149 char *coapUri = (char *) OICCalloc(1, uriLength);
152 OIC_LOG(ERROR, TAG, "out of memory");
155 OICStrcat(coapUri, uriLength, COAP_URI_HEADER);
156 OICStrcat(coapUri, uriLength, info->resourceUri);
158 // parsing options in URI
159 CAResult_t res = CAParseURI(coapUri, optlist);
160 if (CA_STATUS_OK != res)
168 // parsing options in HeadOption
169 CAResult_t ret = CAParseHeadOption(code, info, optlist);
170 if (CA_STATUS_OK != ret)
175 pdu = CAGeneratePDUImpl((code_t) code, info, endpoint, *optlist, transport);
178 OIC_LOG(ERROR, TAG, "pdu NULL");
183 // pdu print method : coap_show_pdu(pdu);
187 coap_pdu_t *CAParsePDU(const char *data, size_t length, uint32_t *outCode,
188 const CAEndpoint_t *endpoint)
190 VERIFY_NON_NULL_RET(data, TAG, "data", NULL);
191 VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", NULL);
193 coap_transport_t transport = COAP_UDP;
195 if (CAIsSupportedCoAPOverTCP(endpoint->adapter))
197 transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)data)[0] >> 4);
202 coap_pdu_init2(0, 0, ntohs(COAP_INVALID_TID), length, transport);
205 OIC_LOG(ERROR, TAG, "outpdu is null");
209 OIC_LOG_V(DEBUG, TAG, "pdu parse-transport type : %d", transport);
211 int ret = coap_pdu_parse2((unsigned char *) data, length, outpdu, transport);
212 OIC_LOG_V(DEBUG, TAG, "pdu parse ret: %d", ret);
215 OIC_LOG(ERROR, TAG, "pdu parse failed");
220 if (CAIsSupportedCoAPOverTCP(endpoint->adapter))
222 OIC_LOG(INFO, TAG, "there is no version info in coap header");
227 if (outpdu->transport_hdr->udp.version != COAP_DEFAULT_VERSION)
229 OIC_LOG_V(ERROR, TAG, "coap version is not available : %d",
230 outpdu->transport_hdr->udp.version);
233 if (outpdu->transport_hdr->udp.token_length > CA_MAX_TOKEN_LEN)
235 OIC_LOG_V(ERROR, TAG, "token length has been exceed : %d",
236 outpdu->transport_hdr->udp.token_length);
243 (*outCode) = (uint32_t) CA_RESPONSE_CODE(coap_get_code(outpdu, transport));
249 coap_delete_pdu(outpdu);
253 coap_pdu_t *CAGeneratePDUImpl(code_t code, const CAInfo_t *info,
254 const CAEndpoint_t *endpoint, coap_list_t *options,
255 coap_transport_t *transport)
257 VERIFY_NON_NULL_RET(info, TAG, "info", NULL);
258 VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", NULL);
259 VERIFY_NON_NULL_RET(transport, TAG, "transport", NULL);
261 unsigned int length = COAP_MAX_PDU_SIZE;
263 unsigned int msgLength = 0;
264 if (CAIsSupportedCoAPOverTCP(endpoint->adapter))
268 unsigned short prevOptNumber = 0;
269 for (coap_list_t *opt = options; opt; opt = opt->next)
271 unsigned short curOptNumber = COAP_OPTION_KEY(*(coap_option *) opt->data);
272 if (prevOptNumber > curOptNumber)
274 OIC_LOG(ERROR, TAG, "option list is wrong");
278 size_t optValueLen = COAP_OPTION_LENGTH(*(coap_option *) opt->data);
279 size_t optLength = coap_get_opt_header_length(curOptNumber - prevOptNumber, optValueLen);
282 OIC_LOG(ERROR, TAG, "Reserved for the Payload marker for the option");
285 msgLength += optLength;
286 prevOptNumber = curOptNumber;
287 OIC_LOG_V(DEBUG, TAG, "curOptNumber[%d], prevOptNumber[%d], optValueLen[%zu], "
288 "optLength[%zu], msgLength[%d]",
289 curOptNumber, prevOptNumber, optValueLen, optLength, msgLength);
293 if (info->payloadSize > 0)
295 msgLength = msgLength + info->payloadSize + PAYLOAD_MARKER;
297 *transport = coap_get_tcp_header_type_from_size(msgLength);
298 length = msgLength + coap_get_tcp_header_length_for_transport(*transport)
304 *transport = COAP_UDP;
307 coap_pdu_t *pdu = coap_new_pdu2(*transport, length);
311 OIC_LOG(ERROR, TAG, "malloc failed");
315 OIC_LOG_V(DEBUG, TAG, "transport type: %d, payload size: %zu",
316 *transport, info->payloadSize);
319 if (CAIsSupportedCoAPOverTCP(endpoint->adapter))
321 coap_add_length(pdu, *transport, msgLength);
326 OIC_LOG_V(DEBUG, TAG, "msgID is %d", info->messageId);
327 uint16_t message_id = 0;
328 if (0 == info->messageId)
330 /* initialize message id */
331 prng((uint8_t * ) &message_id, sizeof(message_id));
333 OIC_LOG_V(DEBUG, TAG, "gen msg id=%d", message_id);
337 /* use saved message id */
338 message_id = info->messageId;
340 pdu->transport_hdr->udp.id = message_id;
341 OIC_LOG_V(DEBUG, TAG, "messageId in pdu is %d, %d", message_id, pdu->transport_hdr->udp.id);
343 pdu->transport_hdr->udp.type = info->type;
346 coap_add_code(pdu, *transport, code);
348 if (info->token && CA_EMPTY != code)
350 uint32_t tokenLength = info->tokenLength;
351 OIC_LOG_V(DEBUG, TAG, "token info token length: %d, token :", tokenLength);
352 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)info->token, tokenLength);
354 int32_t ret = coap_add_token2(pdu, tokenLength, (unsigned char *)info->token, *transport);
357 OIC_LOG(ERROR, TAG, "can't add token");
362 if (CA_ADAPTER_GATT_BTLE != endpoint->adapter
364 && !CAIsSupportedCoAPOverTCP(endpoint->adapter)
368 // option list will be added in blockwise-transfer
375 for (coap_list_t *opt = options; opt; opt = opt->next)
377 OIC_LOG_V(DEBUG, TAG, "[%s] opt will be added.",
378 COAP_OPTION_DATA(*(coap_option *) opt->data));
380 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length", pdu->length);
381 coap_add_option2(pdu, COAP_OPTION_KEY(*(coap_option *) opt->data),
382 COAP_OPTION_LENGTH(*(coap_option *) opt->data),
383 COAP_OPTION_DATA(*(coap_option *) opt->data), *transport);
387 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length after option", pdu->length);
389 if (NULL != info->payload && 0 < info->payloadSize)
391 OIC_LOG(DEBUG, TAG, "payload is added");
392 coap_add_data(pdu, info->payloadSize, (const unsigned char *) info->payload);
398 CAResult_t CAParseURI(const char *uriInfo, coap_list_t **optlist)
400 VERIFY_NON_NULL(uriInfo, TAG, "uriInfo");
401 VERIFY_NON_NULL(optlist, TAG, "optlist");
403 /* split arg into Uri-* options */
405 coap_split_uri((unsigned char *) uriInfo, strlen(uriInfo), &uri);
407 if (uri.port != COAP_DEFAULT_PORT)
409 unsigned char portbuf[CA_ENCODE_BUFFER_SIZE] = { 0 };
410 int ret = coap_insert(optlist,
411 CACreateNewOptionNode(COAP_OPTION_URI_PORT,
412 coap_encode_var_bytes(portbuf, uri.port),
417 return CA_STATUS_INVALID_PARAM;
421 if (uri.path.s && uri.path.length)
423 CAResult_t ret = CAParseUriPartial(uri.path.s, uri.path.length,
424 COAP_OPTION_URI_PATH, optlist);
425 if (CA_STATUS_OK != ret)
427 OIC_LOG(ERROR, TAG, "CAParseUriPartial failed(uri path)");
432 if (uri.query.s && uri.query.length)
434 CAResult_t ret = CAParseUriPartial(uri.query.s, uri.query.length, COAP_OPTION_URI_QUERY,
436 if (CA_STATUS_OK != ret)
438 OIC_LOG(ERROR, TAG, "CAParseUriPartial failed(uri query)");
446 CAResult_t CAParseUriPartial(const unsigned char *str, size_t length, int target,
447 coap_list_t **optlist)
449 VERIFY_NON_NULL(optlist, TAG, "optlist");
451 if ((target != COAP_OPTION_URI_PATH) && (target != COAP_OPTION_URI_QUERY))
453 // should never occur. Log just in case.
454 OIC_LOG(DEBUG, TAG, "Unexpected URI component.");
455 return CA_NOT_SUPPORTED;
457 else if (str && length)
459 unsigned char uriBuffer[CA_MAX_URI_LENGTH] = { 0 };
460 unsigned char *pBuf = uriBuffer;
461 size_t buflen = sizeof(uriBuffer);
462 int res = (target == COAP_OPTION_URI_PATH) ? coap_split_path(str, length, pBuf, &buflen) :
463 coap_split_query(str, length, pBuf, &buflen);
470 int ret = coap_insert(optlist,
471 CACreateNewOptionNode(target, COAP_OPT_LENGTH(pBuf),
472 (char *)COAP_OPT_VALUE(pBuf)),
476 return CA_STATUS_INVALID_PARAM;
479 size_t optSize = COAP_OPT_SIZE(pBuf);
480 if ((prevIdx + optSize) < buflen)
489 OIC_LOG_V(ERROR, TAG, "Problem parsing URI : %d for %d", res, target);
490 return CA_STATUS_FAILED;
495 OIC_LOG(ERROR, TAG, "str or length is not available");
496 return CA_STATUS_FAILED;
502 CAResult_t CAParseHeadOption(uint32_t code, const CAInfo_t *info, coap_list_t **optlist)
505 VERIFY_NON_NULL_RET(info, TAG, "info", CA_STATUS_INVALID_PARAM);
507 OIC_LOG_V(DEBUG, TAG, "parse Head Opt: %d", info->numOptions);
511 OIC_LOG(ERROR, TAG, "optlist is null");
512 return CA_STATUS_INVALID_PARAM;
515 for (uint32_t i = 0; i < info->numOptions; i++)
517 if(!(info->options + i))
519 OIC_LOG(ERROR, TAG, "options is not available");
520 return CA_STATUS_FAILED;
523 uint32_t id = (info->options + i)->optionID;
524 if (COAP_OPTION_URI_PATH == id || COAP_OPTION_URI_QUERY == id)
526 OIC_LOG_V(DEBUG, TAG, "not Header Opt: %d", id);
530 OIC_LOG_V(DEBUG, TAG, "Head opt ID: %d", id);
531 OIC_LOG_V(DEBUG, TAG, "Head opt data: %s", (info->options + i)->optionData);
532 OIC_LOG_V(DEBUG, TAG, "Head opt length: %d", (info->options + i)->optionLength);
533 int ret = coap_insert(optlist,
534 CACreateNewOptionNode(id, (info->options + i)->optionLength,
535 (info->options + i)->optionData),
539 return CA_STATUS_INVALID_PARAM;
544 // insert one extra header with the payload format if applicable.
545 if (CA_FORMAT_UNDEFINED != info->payloadFormat)
547 coap_list_t* node = NULL;
548 uint8_t buf[CA_ENCODE_BUFFER_SIZE] = {0};
549 switch (info->payloadFormat)
551 case CA_FORMAT_APPLICATION_CBOR:
552 node = CACreateNewOptionNode(
553 COAP_OPTION_CONTENT_FORMAT,
554 coap_encode_var_bytes(buf, (unsigned short)COAP_MEDIATYPE_APPLICATION_CBOR),
558 OIC_LOG_V(ERROR, TAG, "format option:[%d] not supported", info->payloadFormat);
562 OIC_LOG(ERROR, TAG, "format option not created");
563 return CA_STATUS_INVALID_PARAM;
565 int ret = coap_insert(optlist, node, CAOrderOpts);
569 OIC_LOG(ERROR, TAG, "format option not inserted in header");
570 return CA_STATUS_INVALID_PARAM;
573 if (CA_FORMAT_UNDEFINED != info->acceptFormat)
575 coap_list_t* node = NULL;
576 uint8_t buf[CA_ENCODE_BUFFER_SIZE] = {0};
577 switch (info->acceptFormat)
579 case CA_FORMAT_APPLICATION_CBOR:
580 node = CACreateNewOptionNode(
582 coap_encode_var_bytes(buf, (unsigned short)COAP_MEDIATYPE_APPLICATION_CBOR),
586 OIC_LOG_V(ERROR, TAG, "format option:[%d] not supported", info->acceptFormat);
590 OIC_LOG(ERROR, TAG, "format option not created");
591 return CA_STATUS_INVALID_PARAM;
593 int ret = coap_insert(optlist, node, CAOrderOpts);
597 OIC_LOG(ERROR, TAG, "format option not inserted in header");
598 return CA_STATUS_INVALID_PARAM;
605 coap_list_t *CACreateNewOptionNode(uint16_t key, uint32_t length, const char *data)
607 VERIFY_NON_NULL_RET(data, TAG, "data", NULL);
609 coap_option *option = coap_malloc(sizeof(coap_option) + length + 1);
612 OIC_LOG(ERROR, TAG, "Out of memory");
615 memset(option, 0, sizeof(coap_option) + length + 1);
617 COAP_OPTION_KEY(*option) = key;
619 coap_option_def_t* def = coap_opt_def(key);
620 if (NULL != def && coap_is_var_bytes(def))
622 if (length > def->max)
624 // make sure we shrink the value so it fits the coap option definition
625 // by truncating the value, disregard the leading bytes.
626 OIC_LOG_V(DEBUG, TAG, "Option [%d] data size [%d] shrunk to [%d]",
627 def->key, length, def->max);
628 data = &(data[length-def->max]);
631 // Shrink the encoding length to a minimum size for coap
632 // options that support variable length encoding.
633 COAP_OPTION_LENGTH(*option) = coap_encode_var_bytes(
634 COAP_OPTION_DATA(*option),
635 coap_decode_var_bytes((unsigned char *)data, length));
639 COAP_OPTION_LENGTH(*option) = length;
640 memcpy(COAP_OPTION_DATA(*option), data, length);
643 /* we can pass NULL here as delete function since option is released automatically */
644 coap_list_t *node = coap_new_listnode(option, NULL);
648 OIC_LOG(ERROR, TAG, "node is NULL");
656 int CAOrderOpts(void *a, void *b)
660 return a < b ? -1 : 1;
663 if (COAP_OPTION_KEY(*(coap_option *) a) < COAP_OPTION_KEY(*(coap_option * ) b))
668 return COAP_OPTION_KEY(*(coap_option *) a) == COAP_OPTION_KEY(*(coap_option * ) b);
671 uint32_t CAGetOptionCount(coap_opt_iterator_t opt_iter)
674 coap_opt_t *option = NULL;
676 while ((option = coap_option_next(&opt_iter)))
678 if (COAP_OPTION_URI_PATH != opt_iter.type && COAP_OPTION_URI_QUERY != opt_iter.type
679 && COAP_OPTION_BLOCK1 != opt_iter.type && COAP_OPTION_BLOCK2 != opt_iter.type
680 && COAP_OPTION_SIZE1 != opt_iter.type && COAP_OPTION_SIZE2 != opt_iter.type
681 && COAP_OPTION_CONTENT_FORMAT != opt_iter.type
682 && COAP_OPTION_ACCEPT != opt_iter.type
683 && COAP_OPTION_URI_HOST != opt_iter.type && COAP_OPTION_URI_PORT != opt_iter.type
684 && COAP_OPTION_ETAG != opt_iter.type && COAP_OPTION_MAXAGE != opt_iter.type
685 && COAP_OPTION_PROXY_SCHEME != opt_iter.type)
694 CAResult_t CAGetInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
695 uint32_t *outCode, CAInfo_t *outInfo)
697 VERIFY_NON_NULL(pdu, TAG, "pdu");
698 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
699 VERIFY_NON_NULL(outCode, TAG, "outCode");
700 VERIFY_NON_NULL(outInfo, TAG, "outInfo");
702 coap_transport_t transport;
704 if (CAIsSupportedCoAPOverTCP(endpoint->adapter))
706 transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)pdu->transport_hdr)[0] >> 4);
711 transport = COAP_UDP;
714 coap_opt_iterator_t opt_iter;
715 coap_option_iterator_init2((coap_pdu_t *) pdu, &opt_iter, COAP_OPT_ALL, transport);
719 (*outCode) = (uint32_t) CA_RESPONSE_CODE(coap_get_code(pdu, transport));
722 // init HeaderOption list
723 uint32_t count = CAGetOptionCount(opt_iter);
724 memset(outInfo, 0, sizeof(*outInfo));
726 outInfo->numOptions = count;
729 if (CAIsSupportedCoAPOverTCP(endpoint->adapter))
732 outInfo->type = CA_MSG_NONCONFIRM;
733 outInfo->payloadFormat = CA_FORMAT_UNDEFINED;
741 outInfo->type = pdu->transport_hdr->udp.type;
744 outInfo->messageId = pdu->transport_hdr->udp.id;
745 outInfo->payloadFormat = CA_FORMAT_UNDEFINED;
746 outInfo->acceptFormat = CA_FORMAT_UNDEFINED;
751 outInfo->options = (CAHeaderOption_t *) OICCalloc(count, sizeof(CAHeaderOption_t));
752 if (NULL == outInfo->options)
754 OIC_LOG(ERROR, TAG, "Out of memory");
755 return CA_MEMORY_ALLOC_FAILED;
759 coap_opt_t *option = NULL;
760 char optionResult[CA_MAX_URI_LENGTH] = {0};
762 uint32_t optionLength = 0;
763 bool isfirstsetflag = false;
764 bool isQueryBeingProcessed = false;
765 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]);
893 if (COAP_OPTION_PROXY_URI == opt_iter.type)
895 isProxyRequest = true;
899 if (bufLength <= sizeof(outInfo->options[0].optionData))
901 outInfo->options[idx].optionID = opt_iter.type;
902 outInfo->options[idx].optionLength = bufLength;
903 outInfo->options[idx].protocolID = CA_COAP_ID;
904 memcpy(outInfo->options[idx].optionData, buf, bufLength);
912 unsigned char* token = NULL;
913 unsigned int token_length = 0;
914 coap_get_token2(pdu->transport_hdr, transport, &token, &token_length);
917 if (token_length > 0)
919 OIC_LOG_V(DEBUG, TAG, "inside token length : %d", token_length);
920 outInfo->token = (char *) OICMalloc(token_length);
921 if (NULL == outInfo->token)
923 OIC_LOG(ERROR, TAG, "Out of memory");
924 OICFree(outInfo->options);
925 return CA_MEMORY_ALLOC_FAILED;
927 memcpy(outInfo->token, token, token_length);
930 outInfo->tokenLength = token_length;
935 if (coap_get_data(pdu, &dataSize, &data))
937 OIC_LOG(DEBUG, TAG, "inside pdu->data");
938 outInfo->payload = (uint8_t *) OICMalloc(dataSize);
939 if (NULL == outInfo->payload)
941 OIC_LOG(ERROR, TAG, "Out of memory");
942 OICFree(outInfo->options);
943 OICFree(outInfo->token);
944 return CA_MEMORY_ALLOC_FAILED;
946 memcpy(outInfo->payload, pdu->data, dataSize);
947 outInfo->payloadSize = dataSize;
950 if (optionResult[0] != '\0')
952 OIC_LOG_V(DEBUG, TAG, "URL length:%zu", strlen(optionResult));
953 outInfo->resourceUri = OICStrdup(optionResult);
954 if (!outInfo->resourceUri)
956 OIC_LOG(ERROR, TAG, "Out of memory");
957 OICFree(outInfo->options);
958 OICFree(outInfo->token);
959 return CA_MEMORY_ALLOC_FAILED;
962 else if(isProxyRequest && g_chproxyUri[0] != '\0')
965 * A request for Proxy will not have any uri element as per CoAP specs
966 * and only COAP_OPTION_PROXY_URI will be present. Use preset proxy uri
969 outInfo->resourceUri = OICStrdup(g_chproxyUri);
970 if (!outInfo->resourceUri)
972 OIC_LOG(ERROR, TAG, "Out of memory");
973 OICFree(outInfo->options);
974 OICFree(outInfo->token);
975 return CA_MEMORY_ALLOC_FAILED;
981 OIC_LOG(ERROR, TAG, "buffer too small");
982 OICFree(outInfo->options);
983 return CA_STATUS_FAILED;
986 CAResult_t CAGetTokenFromPDU(const coap_hdr_transport_t *pdu_hdr,
988 const CAEndpoint_t *endpoint)
990 VERIFY_NON_NULL(pdu_hdr, TAG, "pdu_hdr");
991 VERIFY_NON_NULL(outInfo, TAG, "outInfo");
992 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
994 coap_transport_t transport = COAP_UDP;
996 if (CAIsSupportedCoAPOverTCP(endpoint->adapter))
998 transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)pdu_hdr)[0] >> 4);
1002 unsigned char* token = NULL;
1003 unsigned int token_length = 0;
1004 coap_get_token2(pdu_hdr, transport, &token, &token_length);
1007 if (token_length > 0)
1009 OIC_LOG_V(DEBUG, TAG, "token len:%d", token_length);
1010 outInfo->token = (char *) OICMalloc(token_length);
1011 if (NULL == outInfo->token)
1013 OIC_LOG(ERROR, TAG, "Out of memory");
1014 return CA_MEMORY_ALLOC_FAILED;
1016 memcpy(outInfo->token, token, token_length);
1019 outInfo->tokenLength = token_length;
1021 return CA_STATUS_OK;
1024 CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength)
1026 VERIFY_NON_NULL(token, TAG, "token");
1028 if ((tokenLength > CA_MAX_TOKEN_LEN) || (0 == tokenLength))
1030 OIC_LOG(ERROR, TAG, "invalid token length");
1031 return CA_STATUS_INVALID_PARAM;
1034 // memory allocation
1035 char *temp = (char *) OICCalloc(tokenLength, sizeof(char));
1038 OIC_LOG(ERROR, TAG, "Out of memory");
1039 return CA_MEMORY_ALLOC_FAILED;
1042 OCFillRandomMem((uint8_t *)temp, tokenLength);
1047 OIC_LOG_V(DEBUG, TAG, "token len:%d, token:", tokenLength);
1048 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)(*token), tokenLength);
1050 return CA_STATUS_OK;
1053 void CADestroyTokenInternal(CAToken_t token)
1058 uint32_t CAGetOptionData(uint16_t key, const uint8_t *data, uint32_t len,
1059 uint8_t *option, uint32_t buflen)
1063 OIC_LOG(ERROR, TAG, "buflen 0");
1069 OIC_LOG(ERROR, TAG, "option buffer too small");
1073 coap_option_def_t* def = coap_opt_def(key);
1074 if (NULL != def && coap_is_var_bytes(def) && 0 == len)
1076 // A 0 length option is permitted in CoAP but the
1077 // rest or the stack is unaware of variable byte encoding
1078 // should remain that way so a 0 byte of length 1 is inserted.
1084 memcpy(option, data, len);
1091 CAMessageType_t CAGetMessageTypeFromPduBinaryData(const void *pdu, uint32_t size)
1093 VERIFY_NON_NULL_RET(pdu, TAG, "pdu", CA_MSG_NONCONFIRM);
1095 // pdu minimum size is 4 byte.
1096 if (size < CA_PDU_MIN_SIZE)
1098 OIC_LOG(ERROR, TAG, "min size");
1099 return CA_MSG_NONCONFIRM;
1102 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1104 return (CAMessageType_t) hdr->type;
1107 uint16_t CAGetMessageIdFromPduBinaryData(const void *pdu, uint32_t size)
1109 VERIFY_NON_NULL_RET(pdu, TAG, "pdu", 0);
1111 // pdu minimum size is 4 byte.
1112 if (size < CA_PDU_MIN_SIZE)
1114 OIC_LOG(ERROR, TAG, "min size");
1118 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1123 CAResponseResult_t CAGetCodeFromPduBinaryData(const void *pdu, uint32_t size)
1125 VERIFY_NON_NULL_RET(pdu, TAG, "pdu", CA_NOT_FOUND);
1127 // pdu minimum size is 4 byte.
1128 if (size < CA_PDU_MIN_SIZE)
1130 OIC_LOG(ERROR, TAG, "min size");
1131 return CA_NOT_FOUND;
1134 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1136 return (CAResponseResult_t) CA_RESPONSE_CODE(hdr->code);
1139 CAPayloadFormat_t CAConvertFormat(uint8_t format)
1143 case COAP_MEDIATYPE_TEXT_PLAIN:
1144 return CA_FORMAT_TEXT_PLAIN;
1145 case COAP_MEDIATYPE_APPLICATION_LINK_FORMAT:
1146 return CA_FORMAT_APPLICATION_LINK_FORMAT;
1147 case COAP_MEDIATYPE_APPLICATION_XML:
1148 return CA_FORMAT_APPLICATION_XML;
1149 case COAP_MEDIATYPE_APPLICATION_OCTET_STREAM:
1150 return CA_FORMAT_APPLICATION_OCTET_STREAM;
1151 case COAP_MEDIATYPE_APPLICATION_RDF_XML:
1152 return CA_FORMAT_APPLICATION_RDF_XML;
1153 case COAP_MEDIATYPE_APPLICATION_EXI:
1154 return CA_FORMAT_APPLICATION_EXI;
1155 case COAP_MEDIATYPE_APPLICATION_JSON:
1156 return CA_FORMAT_APPLICATION_JSON;
1157 case COAP_MEDIATYPE_APPLICATION_CBOR:
1158 return CA_FORMAT_APPLICATION_CBOR;
1160 return CA_FORMAT_UNSUPPORTED;
1165 bool CAIsSupportedBlockwiseTransfer(CATransportAdapter_t adapter)
1167 if (CA_ADAPTER_IP & adapter || CA_ADAPTER_NFC & adapter
1168 || CA_DEFAULT_ADAPTER == adapter)
1177 bool CAIsSupportedCoAPOverTCP(CATransportAdapter_t adapter)
1179 if (CA_ADAPTER_GATT_BTLE & adapter || CA_ADAPTER_RFCOMM_BTEDR & adapter
1180 || CA_ADAPTER_TCP & adapter || CA_DEFAULT_ADAPTER == adapter)