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 ******************************************************************/
26 #include "caprotocolmessage_singlethread.h"
28 #include "oic_malloc.h"
32 #define CA_MAX_TOKEN_LEN (8)
33 #define CA_FLAGS_BLOCK 0x01
34 #define CA_BUFSIZE 128
35 #define CA_COAP_MESSAGE_CON 0
39 uint32_t CAGetRequestInfoFromPdu(const coap_pdu_t *pdu, CARequestInfo_t *outReqInfo,
42 OIC_LOG(DEBUG, TAG, "IN");
47 uint32_t code = CA_NOT_FOUND;
48 CAGetRequestPDUInfo(pdu, &code, &(outReqInfo->info), outUri);
49 outReqInfo->method = code;
50 OIC_LOG(DEBUG, TAG, "OUT");
54 uint32_t CAGetResponseInfoFromPdu(const coap_pdu_t *pdu, CAResponseInfo_t *outResInfo,
57 OIC_LOG(DEBUG, TAG, "IN");
61 uint32_t code = CA_NOT_FOUND;
62 CAGetRequestPDUInfo(pdu, &code, &(outResInfo->info), outUri);
63 outResInfo->result = code;
64 OIC_LOG(DEBUG, TAG, "OUT");
68 coap_pdu_t *CAGeneratePdu(const char *uri, const uint32_t code, const CAInfo_t info)
70 OIC_LOG(DEBUG, TAG, "IN");
74 uint32_t coapHeaderLength = 12;
76 coap_list_t *optlist = NULL;
82 if (CA_MAX_URI_LENGTH < length)
84 OIC_LOG(DEBUG, TAG, "URI len err");
88 coapUri = (char *) OICMalloc(length + coapHeaderLength + 1);
91 OIC_LOG(ERROR, TAG, "error");
94 memset(coapUri, 0, length + coapHeaderLength + 1);
98 memcpy(coapUri, "coap://[::]/", coapHeaderLength);
99 memcpy(coapUri + coapHeaderLength, uri, length);
101 // parsing options in URI
102 CAParseURI(coapUri, &optlist);
104 // parsing options in HeadOption
105 CAParseHeadOption(code, info, &optlist);
110 if (NULL != info.payload) // payload is include in request / response
112 if (!(pdu = CACreatePDUforRequestWithPayload((code_t) code, optlist, info.payload, info)))
115 else // payload is not include in request / response
117 if (!(pdu = CACreatePDUforRequest((code_t) code, optlist, info)))
121 // pdu print method : coap_show_pdu(pdu);
122 OIC_LOG(DEBUG, TAG, "OUT");
126 coap_pdu_t *CAParsePDU(const char *data, uint32_t length, uint32_t *outCode)
128 OIC_LOG(DEBUG, TAG, "IN");
129 coap_pdu_t *outpdu = coap_new_pdu();
131 coap_pdu_parse((unsigned char *) data, length, outpdu);
132 (*outCode) = (uint32_t) outpdu->hdr->code;
133 OIC_LOG(DEBUG, TAG, "OUT");
137 coap_pdu_t *CACreatePDUforRequestWithPayload(const code_t code, coap_list_t *options,
138 const char *payload, const CAInfo_t info)
140 OIC_LOG(DEBUG, TAG, "IN");
145 uint32_t CAFlags = 0;
146 coap_block_t CABlock =
147 { .num = 0, .m = 0, .szx = 6 };
149 if (!(pdu = coap_new_pdu()))
152 unsigned short message_id;
154 if (info.messageId == 0)
156 /* initialize message id */
157 prng((unsigned char * )&message_id, sizeof(unsigned short));
160 OIC_LOG_V(DEBUG, TAG, "gen msg id=%d", message_id);
164 /* use saved message id */
165 message_id = info.messageId;
168 pdu->hdr->type = info.type;
169 pdu->hdr->id = htons(message_id);
170 pdu->hdr->code = code;
174 pdu->hdr->token_length = CA_MAX_TOKEN_LEN;
175 if (!coap_add_token(pdu, CA_MAX_TOKEN_LEN, (unsigned char *) info.token))
177 OIC_LOG(DEBUG, TAG, "cant add token");
180 for (opt = options; opt; opt = opt->next)
182 coap_add_option(pdu, COAP_OPTION_KEY(*(coap_option * )opt->data),
183 COAP_OPTION_LENGTH(*(coap_option * )opt->data),
184 COAP_OPTION_DATA(*(coap_option * )opt->data));
189 uint32_t len = strlen(payload);
190 if ((CAFlags & CA_FLAGS_BLOCK) == 0)
192 OIC_LOG_V(DEBUG, TAG, "add data,payload:%s", payload);
193 coap_add_data(pdu, len, (const unsigned char *) payload);
197 OIC_LOG_V(DEBUG, TAG, "add block,payload: %s", payload);
198 coap_add_block(pdu, len, (const unsigned char *) payload, CABlock.num, CABlock.szx);
201 OIC_LOG(DEBUG, TAG, "OUT");
205 coap_pdu_t *CACreatePDUforRequest(const code_t code, coap_list_t *options,
208 OIC_LOG(DEBUG, TAG, "IN");
213 if (!(pdu = coap_new_pdu()))
215 OIC_LOG(ERROR, TAG, "error");
219 unsigned short message_id;
221 if (info.messageId == 0)
223 /* initialize message id */
224 prng((unsigned char * )&message_id, sizeof(unsigned short));
227 OIC_LOG_V(DEBUG, TAG, "gen msg id(%d)", message_id);
231 /* use saved message id */
232 message_id = info.messageId;
235 pdu->hdr->type = info.type;
236 pdu->hdr->id = htons(message_id);
237 pdu->hdr->code = code;
239 OIC_LOG_V(DEBUG, TAG, "token info : %s, %d", info.token, strlen(info.token));
240 pdu->hdr->token_length = CA_MAX_TOKEN_LEN;
242 if (!coap_add_token(pdu, CA_MAX_TOKEN_LEN, (unsigned char *) info.token))
244 OIC_LOG(DEBUG, TAG, "cant add data");
247 for (opt = options; opt; opt = opt->next)
249 coap_add_option(pdu, COAP_OPTION_KEY(*(coap_option * )opt->data),
250 COAP_OPTION_LENGTH(*(coap_option * )opt->data),
251 COAP_OPTION_DATA(*(coap_option * )opt->data));
253 OIC_LOG(DEBUG, TAG, "OUT");
257 void CAParseURI(const char *uriInfo, coap_list_t **optlist)
259 OIC_LOG(DEBUG, TAG, "IN");
261 unsigned char portbuf[2];
262 unsigned char _buf[CA_BUFSIZE];
263 unsigned char *buf = _buf;
268 OIC_LOG_V(DEBUG, TAG, "url : %s", uriInfo);
270 /* split arg into Uri-* options */
271 coap_split_uri((unsigned char *) uriInfo, strlen(uriInfo), &uri);
273 if (uri.port != COAP_DEFAULT_PORT)
276 CACreateNewOptionNode(COAP_OPTION_URI_PORT,
277 coap_encode_var_bytes(portbuf, uri.port), portbuf),
284 res = coap_split_path(uri.path.s, uri.path.length, buf, &buflen);
289 CACreateNewOptionNode(COAP_OPTION_URI_PATH, COAP_OPT_LENGTH(buf),
290 COAP_OPT_VALUE(buf)), CAOrderOpts);
291 buf += COAP_OPT_SIZE(buf);
295 if (uri.query.length)
299 res = coap_split_query(uri.query.s, uri.query.length, buf, &buflen);
304 CACreateNewOptionNode(COAP_OPTION_URI_QUERY, COAP_OPT_LENGTH(buf),
305 COAP_OPT_VALUE(buf)), CAOrderOpts);
307 buf += COAP_OPT_SIZE(buf);
310 OIC_LOG(DEBUG, TAG, "OUT");
313 void CAParseHeadOption(const uint32_t code, const CAInfo_t info, coap_list_t **optlist)
315 OIC_LOG(DEBUG, TAG, "IN");
316 OIC_LOG_V(DEBUG, TAG, "parse Head Opt: %d", info.numOptions);
319 for (i = 0; i < info.numOptions; i++)
321 uint32_t id = info.options[i].optionID;
322 if (COAP_OPTION_URI_PATH == id || COAP_OPTION_URI_QUERY == id)
324 OIC_LOG_V(DEBUG, TAG, "not Header Opt: %d", id);
328 OIC_LOG_V(DEBUG, TAG, "Head Opt ID: %d", info.options[i].optionID);
329 OIC_LOG_V(DEBUG, TAG, "Head Opt data: %s", info.options[i].optionData);
330 OIC_LOG_V(DEBUG, TAG, "Head Opt len: %d", info.options[i].optionLength);
333 CACreateNewOptionNode(info.options[i].optionID,
334 info.options[i].optionLength,
335 info.options[i].optionData), CAOrderOpts);
338 OIC_LOG(DEBUG, TAG, "OUT");
341 coap_list_t *CACreateNewOptionNode(const uint16_t key, const uint32_t length,
344 OIC_LOG(DEBUG, TAG, "IN");
348 option = coap_malloc(sizeof(coap_option) + length);
351 OIC_LOG(ERROR, TAG, "error");
354 memset(option, 0, sizeof(coap_option) + length);
356 COAP_OPTION_KEY(*option) = key;
357 COAP_OPTION_LENGTH(*option) = length;
358 memcpy(COAP_OPTION_DATA(*option), data, length);
360 /* we can pass NULL here as delete function since option is released automatically */
361 node = coap_new_listnode(option, NULL);
365 OIC_LOG(DEBUG, TAG, "new_listnode rets NULL");
370 OIC_LOG(DEBUG, TAG, "OUT");
374 int CAOrderOpts(void *a, void *b)
376 OIC_LOG(DEBUG, TAG, "IN");
379 return a < b ? -1 : 1;
382 if (COAP_OPTION_KEY(*(coap_option *)a) < COAP_OPTION_KEY(*(coap_option * )b))
386 OIC_LOG(DEBUG, TAG, "OUT");
387 return COAP_OPTION_KEY(*(coap_option *)a) == COAP_OPTION_KEY(*(coap_option * )b);
390 uint32_t CAGetOptionCount(coap_opt_iterator_t opt_iter)
392 OIC_LOG(DEBUG, TAG, "IN");
396 while ((option = coap_option_next(&opt_iter)))
398 if (COAP_OPTION_URI_PATH == opt_iter.type || COAP_OPTION_URI_QUERY == opt_iter.type)
407 OIC_LOG(DEBUG, TAG, "OUT");
411 void CAGetRequestPDUInfo(const coap_pdu_t *pdu, uint32_t *outCode, CAInfo_t *outInfo,
414 OIC_LOG(DEBUG, TAG, "IN");
415 char buf[COAP_MAX_PDU_SIZE]; /* need some space for output creation */
417 coap_opt_iterator_t opt_iter;
419 char optionResult[CA_MAX_URI_LENGTH] =
421 uint32_t count = 0, idx = 0;
422 uint32_t optionLength = 0;
423 uint32_t isfirstsetflag = 0;
425 coap_option_iterator_init((coap_pdu_t *) pdu, &opt_iter, COAP_OPT_ALL);
427 memset(optionResult, 0, sizeof(optionResult));
430 (*outCode) = (uint32_t) pdu->hdr->code;
432 // init HeaderOption list
433 count = CAGetOptionCount(opt_iter);
435 memset(outInfo, 0, sizeof(CAInfo_t));
436 outInfo->numOptions = count;
438 outInfo->type = pdu->hdr->type;
441 outInfo->messageId = ntohs(pdu->hdr->id);
445 outInfo->options = (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t) * count);
446 if (outInfo->options == NULL)
448 OIC_LOG(DEBUG, TAG, "error");
451 memset(outInfo->options, 0, sizeof(CAHeaderOption_t) * count);
454 while ((option = coap_option_next(&opt_iter)))
457 if (CAGetOptionData((uint8_t *)(COAP_OPT_VALUE(option)),
458 COAP_OPT_LENGTH(option), (uint8_t *)buf, sizeof(buf),
461 if (COAP_OPTION_URI_PATH == opt_iter.type || COAP_OPTION_URI_QUERY == opt_iter.type)
463 if (0 == isfirstsetflag)
466 memcpy(optionResult + optionLength, buf, strlen((const char *) buf));
467 optionLength += strlen((const char *) buf);
472 if (COAP_OPTION_URI_PATH == opt_iter.type)
474 memcpy(optionResult + optionLength, "/", 1);
477 else if (COAP_OPTION_URI_QUERY == opt_iter.type)
479 memcpy(optionResult + optionLength, "?", 1);
482 memcpy(optionResult + optionLength, buf, strlen((const char *) buf));
483 optionLength += strlen((const char *) buf);
491 uint32_t length = (uint32_t) strlen((const char *) buf);
493 if (length <= CA_MAX_HEADER_OPTION_DATA_LENGTH)
495 outInfo->options[idx].optionID = opt_iter.type;
496 outInfo->options[idx].optionLength = length;
497 outInfo->options[idx].protocolID = CA_COAP_ID;
498 memcpy(outInfo->options[idx].optionData, buf, length);
507 if (pdu->hdr->token_length > 0)
509 OIC_LOG(DEBUG, TAG, "pdu->hdr->token_length>0");
510 outInfo->token = (char *) OICMalloc(CA_MAX_TOKEN_LEN);
511 if (outInfo->token == NULL)
513 OIC_LOG(DEBUG, TAG, "error");
514 OICFree(outInfo->options);
517 memcpy(outInfo->token, pdu->hdr->token, CA_MAX_TOKEN_LEN);
521 if (NULL != pdu->data)
523 OIC_LOG(DEBUG, TAG, "inside pdu->data");
524 outInfo->payload = (char *) OICMalloc(strlen((const char *) pdu->data) + 1);
525 if (outInfo->payload == NULL)
527 OIC_LOG(DEBUG, TAG, "error");
528 OICFree(outInfo->options);
529 OICFree(outInfo->token);
532 memcpy(outInfo->payload, pdu->data, strlen((const char *) pdu->data) + 1);
536 memcpy(outUri, optionResult, strlen(optionResult));
537 OIC_LOG_V(DEBUG, TAG, "made URL:%s\n", optionResult);
538 OIC_LOG(DEBUG, TAG, "OUT");
542 CAResult_t CAGenerateTokenInternal(CAToken_t *token)
544 OIC_LOG(DEBUG, TAG, "IN");
547 return CA_STATUS_FAILED;
551 char *temp = (char *) OICMalloc(sizeof(char) * (CA_MAX_TOKEN_LEN + 1));
554 OIC_LOG(DEBUG, TAG, "error");
555 return CA_MEMORY_ALLOC_FAILED;
557 memset(temp, 0, sizeof(char) * (CA_MAX_TOKEN_LEN + 1));
562 for (index = 0; index < CA_MAX_TOKEN_LEN; index++)
564 // use valid characters
565 temp[index] = (rand() % 94 + 33) & 0xFF;
572 OIC_LOG_V(DEBUG, TAG, "gen token(%s)", *token);
573 OIC_LOG(DEBUG, TAG, "OUT");
577 void CADestroyTokenInternal(CAToken_t token)
579 OIC_LOG(DEBUG, TAG, "IN");
582 OIC_LOG_V(DEBUG, TAG, "destroy token(%s)!!", token);
586 OIC_LOG(DEBUG, TAG, "OUT");
589 void CADeinitialize(CAInfo_t *info)
591 OIC_LOG(DEBUG, TAG, "IN");
595 if (NULL != info->options)
597 OIC_LOG(DEBUG, TAG, "free opt");
598 OICFree(info->options);
601 if (NULL != info->token)
603 OIC_LOG(DEBUG, TAG, "free tok");
604 OICFree(info->token);
607 if (NULL != info->payload)
609 OIC_LOG(DEBUG, TAG, "free payld");
610 OICFree(info->payload);
613 OIC_LOG(DEBUG, TAG, "OUT");
616 uint32_t CAGetOptionData(const uint8_t *data, uint32_t len, uint8_t *result,
617 uint32_t buflen, uint32_t encode_always)
619 const unsigned char hex[] = "0123456789ABCDEF";
621 assert(data || len == 0);
623 if (buflen == 0 || len == 0)
637 if (cnt + 4 < buflen)
641 *result++ = hex[(*data & 0xf0) >> 4];
642 *result++ = hex[*data & 0x0f];
657 CAMessageType_t CAGetMessageTypeFromPduBinaryData(const void *pdu, uint32_t size)
659 // pdu minimum size is 4 byte.
661 return CA_MSG_NONCONFIRM;
663 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
665 return (CAMessageType_t) hdr->type;
668 uint16_t CAGetMessageIdFromPduBinaryData(const void *pdu, uint32_t size)
670 // pdu minimum size is 4 byte.
674 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
676 return ntohs(hdr->id);