API change , Retransmission Callback on expiry , remove glib source for dynamic linking
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / caprotocolmessage_singlethread.c
1 /******************************************************************
2  *
3  * Copyright 2014 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25
26 #include "caprotocolmessage_singlethread.h"
27 #include "logger.h"
28 #include "oic_malloc.h"
29
30 #define TAG "CPM"
31
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
36
37 #include "util.h"
38
39 uint32_t CAGetRequestInfoFromPdu(const coap_pdu_t *pdu, CARequestInfo_t *outReqInfo,
40                                  char *outUri)
41 {
42     OIC_LOG(DEBUG, TAG, "IN");
43
44     if (NULL == pdu)
45         return 0;
46
47     uint32_t code = CA_NOT_FOUND;
48     CAGetRequestPDUInfo(pdu, &code, &(outReqInfo->info), outUri);
49     outReqInfo->method = code;
50     OIC_LOG(DEBUG, TAG, "OUT");
51     return 1;
52 }
53
54 uint32_t CAGetResponseInfoFromPdu(const coap_pdu_t *pdu, CAResponseInfo_t *outResInfo,
55                                   char *outUri)
56 {
57     OIC_LOG(DEBUG, TAG, "IN");
58     if (NULL == pdu)
59         return 0;
60
61     uint32_t code = CA_NOT_FOUND;
62     CAGetRequestPDUInfo(pdu, &code, &(outResInfo->info), outUri);
63     outResInfo->result = code;
64     OIC_LOG(DEBUG, TAG, "OUT");
65     return 1;
66 }
67
68 coap_pdu_t *CAGeneratePdu(const char *uri, const uint32_t code, const CAInfo_t info)
69 {
70     OIC_LOG(DEBUG, TAG, "IN");
71
72     coap_pdu_t *pdu;
73     char *coapUri = NULL;
74     uint32_t coapHeaderLength = 12;
75     uint32_t length;
76     coap_list_t *optlist = NULL;
77
78     if (NULL == uri)
79         return NULL;
80
81     length = strlen(uri);
82     if (CA_MAX_URI_LENGTH < length)
83     {
84         OIC_LOG(DEBUG, TAG, "URI len err");
85         return NULL;
86     }
87
88     coapUri = (char *) OICMalloc(length + coapHeaderLength + 1);
89     if (coapUri == NULL)
90     {
91         OIC_LOG(ERROR, TAG, "error");
92         return NULL;
93     }
94     memset(coapUri, 0, length + coapHeaderLength + 1);
95
96     if (NULL != coapUri)
97     {
98         memcpy(coapUri, "coap://[::]/", coapHeaderLength);
99         memcpy(coapUri + coapHeaderLength, uri, length);
100
101         // parsing options in URI
102         CAParseURI(coapUri, &optlist);
103
104         // parsing options in HeadOption
105         CAParseHeadOption(code, info, &optlist);
106
107         OICFree(coapUri);
108     }
109
110     if (NULL != info.payload) // payload is include in request / response
111     {
112         if (!(pdu = CACreatePDUforRequestWithPayload((code_t) code, optlist, info.payload, info)))
113             return NULL;
114     }
115     else // payload is not include in request / response
116     {
117         if (!(pdu = CACreatePDUforRequest((code_t) code, optlist, info)))
118             return NULL;
119     }
120
121     // pdu print method : coap_show_pdu(pdu);
122     OIC_LOG(DEBUG, TAG, "OUT");
123     return pdu;
124 }
125
126 coap_pdu_t *CAParsePDU(const char *data, uint32_t length, uint32_t *outCode)
127 {
128     OIC_LOG(DEBUG, TAG, "IN");
129     coap_pdu_t *outpdu = coap_new_pdu();
130
131     coap_pdu_parse((unsigned char *) data, length, outpdu);
132     (*outCode) = (uint32_t) outpdu->hdr->code;
133     OIC_LOG(DEBUG, TAG, "OUT");
134     return outpdu;
135 }
136
137 coap_pdu_t *CACreatePDUforRequestWithPayload(const code_t code, coap_list_t *options,
138         const char *payload, const CAInfo_t info)
139 {
140     OIC_LOG(DEBUG, TAG, "IN");
141
142     coap_pdu_t *pdu;
143     coap_list_t *opt;
144
145     uint32_t CAFlags = 0;
146     coap_block_t CABlock =
147     { .num = 0, .m = 0, .szx = 6 };
148
149     if (!(pdu = coap_new_pdu()))
150         return NULL;
151
152     unsigned short message_id;
153
154     if (info.messageId == 0)
155     {
156         /* initialize message id */
157         prng((unsigned char * )&message_id, sizeof(unsigned short));
158         ++message_id;
159
160         OIC_LOG_V(DEBUG, TAG, "gen msg id=%d", message_id);
161     }
162     else
163     {
164         /* use saved message id */
165         message_id = info.messageId;
166     }
167
168     pdu->hdr->type = info.type;
169     pdu->hdr->id = htons(message_id);
170     pdu->hdr->code = code;
171
172     if (info.token)
173     {
174         pdu->hdr->token_length = CA_MAX_TOKEN_LEN;
175         if (!coap_add_token(pdu, CA_MAX_TOKEN_LEN, (unsigned char *) info.token))
176         {
177             OIC_LOG(DEBUG, TAG, "cant add token");
178         }
179     }
180     for (opt = options; opt; opt = opt->next)
181     {
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));
185     }
186
187     if (NULL != payload)
188     {
189         uint32_t len = strlen(payload);
190         if ((CAFlags & CA_FLAGS_BLOCK) == 0)
191         {
192             OIC_LOG_V(DEBUG, TAG, "add data,payload:%s", payload);
193             coap_add_data(pdu, len, (const unsigned char *) payload);
194         }
195         else
196         {
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);
199         }
200     }
201     OIC_LOG(DEBUG, TAG, "OUT");
202     return pdu;
203 }
204
205 coap_pdu_t *CACreatePDUforRequest(const code_t code, coap_list_t *options,
206                                   const CAInfo_t info)
207 {
208     OIC_LOG(DEBUG, TAG, "IN");
209
210     coap_pdu_t *pdu;
211     coap_list_t *opt;
212
213     if (!(pdu = coap_new_pdu()))
214     {
215         OIC_LOG(ERROR, TAG, "error");
216         return NULL;
217     }
218
219     unsigned short message_id;
220
221     if (info.messageId == 0)
222     {
223         /* initialize message id */
224         prng((unsigned char * )&message_id, sizeof(unsigned short));
225         ++message_id;
226
227         OIC_LOG_V(DEBUG, TAG, "gen msg id(%d)", message_id);
228     }
229     else
230     {
231         /* use saved message id */
232         message_id = info.messageId;
233     }
234
235     pdu->hdr->type = info.type;
236     pdu->hdr->id = htons(message_id);
237     pdu->hdr->code = code;
238
239     OIC_LOG_V(DEBUG, TAG, "token info : %s, %d", info.token, strlen(info.token));
240     pdu->hdr->token_length = CA_MAX_TOKEN_LEN;
241
242     if (!coap_add_token(pdu, CA_MAX_TOKEN_LEN, (unsigned char *) info.token))
243     {
244         OIC_LOG(DEBUG, TAG, "cant add data");
245     }
246
247     for (opt = options; opt; opt = opt->next)
248     {
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));
252     }
253     OIC_LOG(DEBUG, TAG, "OUT");
254     return pdu;
255 }
256
257 void CAParseURI(const char *uriInfo, coap_list_t **optlist)
258 {
259     OIC_LOG(DEBUG, TAG, "IN");
260
261     unsigned char portbuf[2];
262     unsigned char _buf[CA_BUFSIZE];
263     unsigned char *buf = _buf;
264     coap_uri_t uri;
265     size_t buflen;
266     uint32_t res;
267
268     OIC_LOG_V(DEBUG, TAG, "url : %s", uriInfo);
269
270     /* split arg into Uri-* options */
271     coap_split_uri((unsigned char *) uriInfo, strlen(uriInfo), &uri);
272
273     if (uri.port != COAP_DEFAULT_PORT)
274     {
275         coap_insert(optlist,
276                     CACreateNewOptionNode(COAP_OPTION_URI_PORT,
277                                           coap_encode_var_bytes(portbuf, uri.port), portbuf),
278                     CAOrderOpts);
279     }
280
281     if (uri.path.length)
282     {
283         buflen = CA_BUFSIZE;
284         res = coap_split_path(uri.path.s, uri.path.length, buf, &buflen);
285
286         while (res--)
287         {
288             coap_insert(optlist,
289                         CACreateNewOptionNode(COAP_OPTION_URI_PATH, COAP_OPT_LENGTH(buf),
290                                               COAP_OPT_VALUE(buf)), CAOrderOpts);
291             buf += COAP_OPT_SIZE(buf);
292         }
293     }
294
295     if (uri.query.length)
296     {
297         buflen = CA_BUFSIZE;
298         buf = _buf;
299         res = coap_split_query(uri.query.s, uri.query.length, buf, &buflen);
300
301         while (res--)
302         {
303             coap_insert(optlist,
304                         CACreateNewOptionNode(COAP_OPTION_URI_QUERY, COAP_OPT_LENGTH(buf),
305                                               COAP_OPT_VALUE(buf)), CAOrderOpts);
306
307             buf += COAP_OPT_SIZE(buf);
308         }
309     }
310     OIC_LOG(DEBUG, TAG, "OUT");
311 }
312
313 void CAParseHeadOption(const uint32_t code, const CAInfo_t info, coap_list_t **optlist)
314 {
315     OIC_LOG(DEBUG, TAG, "IN");
316     OIC_LOG_V(DEBUG, TAG, "parse Head Opt: %d", info.numOptions);
317
318     uint32_t i;
319     for (i = 0; i < info.numOptions; i++)
320     {
321         uint32_t id = info.options[i].optionID;
322         if (COAP_OPTION_URI_PATH == id || COAP_OPTION_URI_QUERY == id)
323         {
324             OIC_LOG_V(DEBUG, TAG, "not Header Opt: %d", id);
325         }
326         else
327         {
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);
331
332             coap_insert(optlist,
333                         CACreateNewOptionNode(info.options[i].optionID,
334                                               info.options[i].optionLength,
335                                               info.options[i].optionData), CAOrderOpts);
336         }
337     }
338     OIC_LOG(DEBUG, TAG, "OUT");
339 }
340
341 coap_list_t *CACreateNewOptionNode(const uint16_t key, const uint32_t length,
342                                    const uint8_t *data)
343 {
344     OIC_LOG(DEBUG, TAG, "IN");
345     coap_option *option;
346     coap_list_t *node;
347
348     option = coap_malloc(sizeof(coap_option) + length);
349     if (!option)
350     {
351         OIC_LOG(ERROR, TAG, "error");
352         return NULL;
353     }
354     memset(option, 0, sizeof(coap_option) + length);
355
356     COAP_OPTION_KEY(*option) = key;
357     COAP_OPTION_LENGTH(*option) = length;
358     memcpy(COAP_OPTION_DATA(*option), data, length);
359
360     /* we can pass NULL here as delete function since option is released automatically  */
361     node = coap_new_listnode(option, NULL);
362
363     if (!node)
364     {
365         OIC_LOG(DEBUG, TAG, "new_listnode rets NULL");
366         coap_free(option);
367         return NULL;
368     }
369     coap_free(option);
370     OIC_LOG(DEBUG, TAG, "OUT");
371     return node;
372 }
373
374 int CAOrderOpts(void *a, void *b)
375 {
376     OIC_LOG(DEBUG, TAG, "IN");
377     if (!a || !b)
378     {
379         return a < b ? -1 : 1;
380     }
381
382     if (COAP_OPTION_KEY(*(coap_option *)a) < COAP_OPTION_KEY(*(coap_option * )b))
383     {
384         return -1;
385     }
386     OIC_LOG(DEBUG, TAG, "OUT");
387     return COAP_OPTION_KEY(*(coap_option *)a) == COAP_OPTION_KEY(*(coap_option * )b);
388 }
389
390 uint32_t CAGetOptionCount(coap_opt_iterator_t opt_iter)
391 {
392     OIC_LOG(DEBUG, TAG, "IN");
393     uint32_t count = 0;
394     coap_opt_t *option;
395
396     while ((option = coap_option_next(&opt_iter)))
397     {
398         if (COAP_OPTION_URI_PATH == opt_iter.type || COAP_OPTION_URI_QUERY == opt_iter.type)
399         {
400
401         }
402         else
403         {
404             count++;
405         }
406     }
407     OIC_LOG(DEBUG, TAG, "OUT");
408     return count;
409 }
410
411 void CAGetRequestPDUInfo(const coap_pdu_t *pdu, uint32_t *outCode, CAInfo_t *outInfo,
412                          char *outUri)
413 {
414     OIC_LOG(DEBUG, TAG, "IN");
415     char buf[COAP_MAX_PDU_SIZE]; /* need some space for output creation */
416     uint32_t encode = 0;
417     coap_opt_iterator_t opt_iter;
418     coap_opt_t *option;
419     char optionResult[CA_MAX_URI_LENGTH] =
420     { 0, };
421     uint32_t count = 0, idx = 0;
422     uint32_t optionLength = 0;
423     uint32_t isfirstsetflag = 0;
424
425     coap_option_iterator_init((coap_pdu_t *) pdu, &opt_iter, COAP_OPT_ALL);
426
427     memset(optionResult, 0, sizeof(optionResult));
428
429     // set code
430     (*outCode) = (uint32_t) pdu->hdr->code;
431
432     // init HeaderOption list
433     count = CAGetOptionCount(opt_iter);
434
435     memset(outInfo, 0, sizeof(CAInfo_t));
436     outInfo->numOptions = count;
437     // set type
438     outInfo->type = pdu->hdr->type;
439
440     // set message id
441     outInfo->messageId = ntohs(pdu->hdr->id);
442
443     if (count > 0)
444     {
445         outInfo->options = (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t) * count);
446         if (outInfo->options == NULL)
447         {
448             OIC_LOG(DEBUG, TAG, "error");
449             return;
450         }
451         memset(outInfo->options, 0, sizeof(CAHeaderOption_t) * count);
452     }
453
454     while ((option = coap_option_next(&opt_iter)))
455     {
456
457         if (CAGetOptionData((uint8_t *)(COAP_OPT_VALUE(option)),
458                             COAP_OPT_LENGTH(option), (uint8_t *)buf, sizeof(buf),
459                             encode))
460         {
461             if (COAP_OPTION_URI_PATH == opt_iter.type || COAP_OPTION_URI_QUERY == opt_iter.type)
462             {
463                 if (0 == isfirstsetflag)
464                 {
465                     isfirstsetflag = 1;
466                     memcpy(optionResult + optionLength, buf, strlen((const char *) buf));
467                     optionLength += strlen((const char *) buf);
468
469                 }
470                 else
471                 {
472                     if (COAP_OPTION_URI_PATH == opt_iter.type)
473                     {
474                         memcpy(optionResult + optionLength, "/", 1);
475                         optionLength++;
476                     }
477                     else if (COAP_OPTION_URI_QUERY == opt_iter.type)
478                     {
479                         memcpy(optionResult + optionLength, "?", 1);
480                         optionLength++;
481                     }
482                     memcpy(optionResult + optionLength, buf, strlen((const char *) buf));
483                     optionLength += strlen((const char *) buf);
484                 }
485             }
486             else
487             {
488
489                 if (idx < count)
490                 {
491                     uint32_t length = (uint32_t) strlen((const char *) buf);
492
493                     if (length <= CA_MAX_HEADER_OPTION_DATA_LENGTH)
494                     {
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);
499                         idx++;
500                     }
501                 }
502             }
503         }
504     }
505
506     // set token data
507     if (pdu->hdr->token_length > 0)
508     {
509         OIC_LOG(DEBUG, TAG, "pdu->hdr->token_length>0");
510         outInfo->token = (char *) OICMalloc(CA_MAX_TOKEN_LEN);
511         if (outInfo->token == NULL)
512         {
513             OIC_LOG(DEBUG, TAG, "error");
514             OICFree(outInfo->options);
515             return;
516         }
517         memcpy(outInfo->token, pdu->hdr->token, CA_MAX_TOKEN_LEN);
518     }
519
520     // set payload data
521     if (NULL != pdu->data)
522     {
523         OIC_LOG(DEBUG, TAG, "inside pdu->data");
524         outInfo->payload = (char *) OICMalloc(strlen((const char *) pdu->data) + 1);
525         if (outInfo->payload == NULL)
526         {
527             OIC_LOG(DEBUG, TAG, "error");
528             OICFree(outInfo->options);
529             OICFree(outInfo->token);
530             return;
531         }
532         memcpy(outInfo->payload, pdu->data, strlen((const char *) pdu->data) + 1);
533     }
534
535
536     memcpy(outUri, optionResult, strlen(optionResult));
537     OIC_LOG_V(DEBUG, TAG, "made URL:%s\n", optionResult);
538     OIC_LOG(DEBUG, TAG, "OUT");
539
540 }
541
542 CAResult_t CAGenerateTokenInternal(CAToken_t *token)
543 {
544     OIC_LOG(DEBUG, TAG, "IN");
545     if (token == NULL)
546     {
547         return CA_STATUS_FAILED;
548     }
549
550     // memory allocation
551     char *temp = (char *) OICMalloc(sizeof(char) * (CA_MAX_TOKEN_LEN + 1));
552     if (temp == NULL)
553     {
554         OIC_LOG(DEBUG, TAG, "error");
555         return CA_MEMORY_ALLOC_FAILED;
556     }
557     memset(temp, 0, sizeof(char) * (CA_MAX_TOKEN_LEN + 1));
558
559     // set random byte
560     uint32_t index;
561
562     for (index = 0; index < CA_MAX_TOKEN_LEN; index++)
563     {
564         // use valid characters
565         temp[index] = (rand() % 94 + 33) & 0xFF;
566     }
567
568     temp[index] = '\0';
569     // save token
570     *token = temp;
571
572     OIC_LOG_V(DEBUG, TAG, "gen token(%s)", *token);
573     OIC_LOG(DEBUG, TAG, "OUT");
574     return CA_STATUS_OK;
575 }
576
577 void CADestroyTokenInternal(CAToken_t token)
578 {
579     OIC_LOG(DEBUG, TAG, "IN");
580     if (token != NULL)
581     {
582         OIC_LOG_V(DEBUG, TAG, "destroy token(%s)!!", token);
583
584         OICFree(token);
585     }
586     OIC_LOG(DEBUG, TAG, "OUT");
587 }
588
589 void CADeinitialize(CAInfo_t *info)
590 {
591     OIC_LOG(DEBUG, TAG, "IN");
592
593     if (NULL != info)
594     {
595         if (NULL != info->options)
596         {
597             OIC_LOG(DEBUG, TAG, "free opt");
598             OICFree(info->options);
599         }
600
601         if (NULL != info->token)
602         {
603             OIC_LOG(DEBUG, TAG, "free tok");
604             OICFree(info->token);
605         }
606
607         if (NULL != info->payload)
608         {
609             OIC_LOG(DEBUG, TAG, "free payld");
610             OICFree(info->payload);
611         }
612     }
613     OIC_LOG(DEBUG, TAG, "OUT");
614 }
615
616 uint32_t CAGetOptionData(const uint8_t *data, uint32_t len, uint8_t *result,
617                          uint32_t buflen, uint32_t encode_always)
618 {
619     const unsigned char hex[] = "0123456789ABCDEF";
620     uint32_t cnt = 0;
621     assert(data || len == 0);
622
623     if (buflen == 0 || len == 0)
624         return 0;
625
626     while (len)
627     {
628         if (!encode_always)
629         {
630             if (cnt == buflen)
631                 break;
632             *result++ = *data;
633             ++cnt;
634         }
635         else
636         {
637             if (cnt + 4 < buflen)
638             {
639                 *result++ = '\\';
640                 *result++ = 'x';
641                 *result++ = hex[(*data & 0xf0) >> 4];
642                 *result++ = hex[*data & 0x0f];
643                 cnt += 4;
644             }
645             else
646                 break;
647         }
648
649         ++data;
650         --len;
651     }
652
653     *result = '\0';
654     return cnt;
655 }
656
657 CAMessageType_t CAGetMessageTypeFromPduBinaryData(const void *pdu, uint32_t size)
658 {
659     // pdu minimum size is 4 byte.
660     if (size < 4)
661         return CA_MSG_NONCONFIRM;
662
663     coap_hdr_t *hdr = (coap_hdr_t *) pdu;
664
665     return (CAMessageType_t) hdr->type;
666 }
667
668 uint16_t CAGetMessageIdFromPduBinaryData(const void *pdu, uint32_t size)
669 {
670     // pdu minimum size is 4 byte.
671     if (size < 4)
672         return 0;
673
674     coap_hdr_t *hdr = (coap_hdr_t *) pdu;
675
676     return ntohs(hdr->id);
677 }