Cloud Interface Feature base on CoAP over TCP in CA Layer
[platform/upstream/iotivity.git] / resource / csdk / connectivity / lib / libcoap-4.1.1 / pdu.c
1 /* pdu.c -- CoAP message structure
2  *
3  * Copyright (C) 2010,2011 Olaf Bergmann <bergmann@tzi.org>
4  *
5  * This file is part of the CoAP library libcoap. Please see
6  * README for terms of use.
7  */
8
9 #include "config.h"
10
11 #if defined(HAVE_ASSERT_H) && !defined(assert)
12 # include <assert.h>
13 #endif
14
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
18 #ifdef HAVE_ARPA_INET_H
19 #include <arpa/inet.h>
20 #endif
21
22 #include "debug.h"
23 #include "pdu.h"
24 #include "option.h"
25 #include "encode.h"
26
27 #ifdef WITH_ARDUINO
28 #include "util.h"
29 #endif
30
31 #ifdef WITH_CONTIKI
32 #include "memb.h"
33
34 #ifndef WITH_TCP
35 typedef unsigned char _pdu[sizeof(coap_pdu_t) + COAP_MAX_PDU_SIZE];
36
37 MEMB(pdu_storage, _pdu, COAP_PDU_MAXCNT);
38 #endif
39
40 void
41 coap_pdu_resources_init()
42 {
43     memb_init(&pdu_storage);
44 }
45 #else /* WITH_CONTIKI */
46 #include "mem.h"
47 #endif /* WITH_CONTIKI */
48
49 void coap_pdu_clear(coap_pdu_t *pdu, size_t size, coap_transport_type transport, unsigned int length)
50 {
51     assert(pdu);
52
53     memset(pdu, 0, sizeof(coap_pdu_t) + size);
54     pdu->max_size = size;
55     pdu->hdr = (coap_hdr_t *) ((unsigned char *) pdu + sizeof(coap_pdu_t));
56
57     if (coap_udp == transport)
58     {
59         pdu->hdr->coap_hdr_udp_t.version = COAP_DEFAULT_VERSION;
60         /* data is NULL unless explicitly set by coap_add_data() */
61         pdu->length = sizeof(pdu->hdr->coap_hdr_udp_t);
62     }
63 #ifdef WITH_TCP
64     else
65     {
66         /* data is NULL unless explicitly set by coap_add_data() */
67         pdu->length = length;
68     }
69 #endif
70 }
71
72 #ifdef WITH_LWIP
73 coap_pdu_t *
74 coap_pdu_from_pbuf(struct pbuf *pbuf)
75 {
76     LWIP_ASSERT("Can only deal with contiguous PBUFs", pbuf->tot_len == pbuf->len);
77     LWIP_ASSERT("coap_read needs to receive an exclusive copy of the incoming pbuf", pbuf->ref == 1);
78
79     void *data = pbuf->payload;
80     coap_pdu_t *result;
81
82     u8_t header_error = pbuf_header(pbuf, sizeof(coap_pdu_t));
83     LWIP_ASSERT("CoAP PDU header does not fit in existing header space", header_error == 0);
84
85     result = (coap_pdu_t *)pbuf->payload;
86
87     memset(result, 0, sizeof(coap_pdu_t));
88
89     result->max_size = pbuf->tot_len - sizeof(coap_pdu_t);
90     result->length = pbuf->tot_len - sizeof(coap_pdu_t);
91     result->hdr = data;
92     result->pbuf = pbuf;
93
94     return result;
95 }
96 #endif
97
98 coap_pdu_t *
99 coap_pdu_init(unsigned char type, unsigned char code, unsigned short id,
100               size_t size, coap_transport_type transport)
101 {
102     coap_pdu_t *pdu;
103 #ifdef WITH_LWIP
104     struct pbuf *p;
105 #endif
106
107     unsigned int length = 0;
108     switch(transport)
109     {
110         case coap_udp:
111             length = sizeof(pdu->hdr->coap_hdr_udp_t);
112             break;
113 #ifdef WITH_TCP
114         case coap_tcp:
115             length = COAP_TCP_HEADER_NO_FIELD;
116             break;
117         case coap_tcp_8bit:
118             length = COAP_TCP_HEADER_8_BIT;
119             break;
120         case coap_tcp_16bit:
121             length = COAP_TCP_HEADER_16_BIT;
122             break;
123         case coap_tcp_32bit:
124             length = COAP_TCP_HEADER_32_BIT;
125             break;
126 #endif
127         default:
128             debug("it has wrong type\n");
129     }
130
131 #ifndef WITH_TCP
132     assert(size <= COAP_MAX_PDU_SIZE);
133     /* Size must be large enough to fit the header. */
134     if (size < length || size > COAP_MAX_PDU_SIZE)
135         return NULL;
136 #endif
137
138     /* size must be large enough for hdr */
139 #if defined(WITH_POSIX) || defined(WITH_ARDUINO)
140     pdu = (coap_pdu_t *) coap_malloc(sizeof(coap_pdu_t) + size);
141 #endif
142 #ifdef WITH_CONTIKI
143     pdu = (coap_pdu_t *)memb_alloc(&pdu_storage);
144 #endif
145 #ifdef WITH_LWIP
146     p = pbuf_alloc(PBUF_TRANSPORT, size, PBUF_RAM);
147     if (p != NULL)
148     {
149         u8_t header_error = pbuf_header(p, sizeof(coap_pdu_t));
150         /* we could catch that case and allocate larger memory in advance, but then
151          * again, we'd run into greater trouble with incoming packages anyway */
152         LWIP_ASSERT("CoAP PDU header does not fit in transport header", header_error == 0);
153         pdu = p->payload;
154     }
155     else
156     {
157         pdu = NULL;
158     }
159 #endif
160     if (pdu)
161     {
162         coap_pdu_clear(pdu, size, transport, length);
163
164         switch(transport)
165         {
166             case coap_udp:
167                 pdu->hdr->coap_hdr_udp_t.id = id;
168                 pdu->hdr->coap_hdr_udp_t.type = type;
169                 pdu->hdr->coap_hdr_udp_t.code = code;
170                 break;
171 #ifdef WITH_TCP
172             case coap_tcp:
173                 pdu->hdr->coap_hdr_tcp_t.message_length = 0;
174                 pdu->hdr->coap_hdr_tcp_t.code = code;
175                 break;
176             case coap_tcp_8bit:
177                 pdu->hdr->coap_hdr_tcp_8bit_t.message_length = COAP_TCP_LENGTH_FIELD_NUM_8_BIT;
178                 pdu->hdr->coap_hdr_tcp_8bit_t.length_byte = 0;
179                 pdu->hdr->coap_hdr_tcp_8bit_t.code = code;
180                 break;
181             case coap_tcp_16bit:
182                 pdu->hdr->coap_hdr_tcp_16bit_t.header_data[0] = COAP_TCP_LENGTH_FIELD_NUM_16_BIT << 4;
183                 pdu->hdr->coap_hdr_tcp_16bit_t.header_data[3] = code;
184                 break;
185             case coap_tcp_32bit:
186                 pdu->hdr->coap_hdr_tcp_32bit_t.header_data[0] = COAP_TCP_LENGTH_FIELD_NUM_32_BIT << 4;
187                 pdu->hdr->coap_hdr_tcp_32bit_t.header_data[5] = code;
188                 break;
189 #endif
190             default:
191                 debug("it has wrong type\n");
192         }
193
194 #ifdef WITH_LWIP
195         pdu->pbuf = p;
196 #endif
197     }
198     return pdu;
199 }
200
201 coap_pdu_t *
202 coap_new_pdu(coap_transport_type transport, unsigned int size)
203 {
204     coap_pdu_t *pdu;
205
206 #ifndef WITH_CONTIKI
207     pdu = coap_pdu_init(0, 0,
208                         ntohs(COAP_INVALID_TID),
209 #ifndef WITH_TCP
210                         COAP_MAX_PDU_SIZE,
211 #else
212                         size,
213 #endif
214                         transport);
215 #else /* WITH_CONTIKI */
216     pdu = coap_pdu_init(0, 0, uip_ntohs(COAP_INVALID_TID),
217 #ifndef WITH_TCP
218                         COAP_MAX_PDU_SIZE,
219 #else
220                         size,
221 #endif
222                         transport);
223 #endif /* WITH_CONTIKI */
224
225 #ifndef NDEBUG
226     if (!pdu)
227         coap_log(LOG_CRIT, "coap_new_pdu: cannot allocate memory for new PDU\n");
228 #endif
229     return pdu;
230 }
231
232 void coap_delete_pdu(coap_pdu_t *pdu)
233 {
234 #if defined(WITH_POSIX) || defined(WITH_ARDUINO)
235     coap_free( pdu );
236 #endif
237 #ifdef WITH_LWIP
238     if (pdu != NULL) /* accepting double free as the other implementation accept that too */
239         pbuf_free(pdu->pbuf);
240 #endif
241 #ifdef WITH_CONTIKI
242     memb_free(&pdu_storage, pdu);
243 #endif
244 }
245
246 #ifdef WITH_TCP
247 coap_transport_type coap_get_tcp_header_type_from_size(unsigned int size)
248 {
249     if (COAP_TCP_LENGTH_LIMIT_8_BIT < size && COAP_TCP_LENGTH_LIMIT_16_BIT >= size)
250     {
251         return coap_tcp_8bit;
252     }
253     else if (COAP_TCP_LENGTH_LIMIT_16_BIT < size && COAP_TCP_LENGTH_LIMIT_32_BIT >= size)
254     {
255         return coap_tcp_16bit;
256     }
257     else if (COAP_TCP_LENGTH_LIMIT_32_BIT < size)
258     {
259         return coap_tcp_32bit;
260     }
261     else
262     {
263         return coap_tcp;
264     }
265 }
266
267 coap_transport_type coap_get_tcp_header_type_from_initbyte(unsigned int length)
268 {
269     coap_transport_type type;
270     switch(length)
271     {
272         case COAP_TCP_LENGTH_FIELD_NUM_8_BIT:
273             type = coap_tcp_8bit;
274             break;
275         case COAP_TCP_LENGTH_FIELD_NUM_16_BIT:
276             type = coap_tcp_16bit;
277             break;
278         case COAP_TCP_LENGTH_FIELD_NUM_32_BIT:
279             type = coap_tcp_32bit;
280             break;
281         default:
282             type = coap_tcp;
283     }
284     return type;
285 }
286
287 void coap_add_length(const coap_pdu_t *pdu, coap_transport_type transport, unsigned int length)
288 {
289     assert(pdu);
290
291     switch(transport)
292     {
293         case coap_tcp:
294             pdu->hdr->coap_hdr_tcp_t.message_length = length;
295             break;
296         case coap_tcp_8bit:
297             if (length > COAP_TCP_LENGTH_FIELD_8_BIT)
298             {
299                 pdu->hdr->coap_hdr_tcp_8bit_t.length_byte =
300                         length - COAP_TCP_LENGTH_FIELD_8_BIT;
301             }
302             break;
303         case coap_tcp_16bit:
304             if (length > COAP_TCP_LENGTH_FIELD_16_BIT)
305             {
306                 unsigned int total_length = length - COAP_TCP_LENGTH_FIELD_16_BIT;
307                 pdu->hdr->coap_hdr_tcp_16bit_t.header_data[1] = (total_length >> 8) & 0x0000ff;
308                 pdu->hdr->coap_hdr_tcp_16bit_t.header_data[2] = total_length & 0x000000ff;
309             }
310             break;
311         case coap_tcp_32bit:
312             if (length > COAP_TCP_LENGTH_FIELD_32_BIT)
313             {
314                 unsigned int total_length = length - COAP_TCP_LENGTH_FIELD_32_BIT;
315                 pdu->hdr->coap_hdr_tcp_32bit_t.header_data[1] = total_length >> 24;
316                 pdu->hdr->coap_hdr_tcp_32bit_t.header_data[2] = (total_length >> 16) & 0x00ff;
317                 pdu->hdr->coap_hdr_tcp_32bit_t.header_data[3] = (total_length >> 8) & 0x0000ff;
318                 pdu->hdr->coap_hdr_tcp_32bit_t.header_data[4] = total_length & 0x000000ff;
319             }
320             break;
321         default:
322             debug("it has wrong type\n");
323     }
324 }
325
326 unsigned int coap_get_length_from_header(const unsigned char *header, coap_transport_type transport)
327 {
328     assert(header);
329
330     unsigned int length = 0;
331     unsigned int length_field_data = 0;
332     switch(transport)
333     {
334         case coap_tcp_8bit:
335             length = header[1] + COAP_TCP_LENGTH_FIELD_8_BIT;
336             break;
337         case coap_tcp_16bit:
338             length_field_data = (header[1] << 8 | header[2]);
339             length = length_field_data + COAP_TCP_LENGTH_FIELD_16_BIT;
340             break;
341         case coap_tcp_32bit:
342             length_field_data = header[1] << 24 | header[2] << 16 | header[3] << 8 | header[4];
343             length = length_field_data + COAP_TCP_LENGTH_FIELD_32_BIT;
344             break;
345         default:
346             debug("it has wrong type\n");
347     }
348
349     return length;
350 }
351
352 unsigned int coap_get_length(const coap_pdu_t *pdu, coap_transport_type transport)
353 {
354     assert(pdu);
355
356     unsigned int length = 0;
357     unsigned int length_field_data = 0;
358     switch(transport)
359     {
360         case coap_tcp:
361             length = pdu->hdr->coap_hdr_tcp_t.message_length;
362             break;
363         case coap_tcp_8bit:
364             length = pdu->hdr->coap_hdr_tcp_8bit_t.length_byte + COAP_TCP_LENGTH_FIELD_8_BIT;
365             break;
366         case coap_tcp_16bit:
367             length_field_data =
368                     pdu->hdr->coap_hdr_tcp_16bit_t.header_data[1] << 8 |
369                     pdu->hdr->coap_hdr_tcp_16bit_t.header_data[2];
370             length = length_field_data + COAP_TCP_LENGTH_FIELD_16_BIT;
371             break;
372         case coap_tcp_32bit:
373             length_field_data =
374                     pdu->hdr->coap_hdr_tcp_32bit_t.header_data[1] << 24 |
375                     pdu->hdr->coap_hdr_tcp_32bit_t.header_data[2] << 16 |
376                     pdu->hdr->coap_hdr_tcp_32bit_t.header_data[3] << 8 |
377                     pdu->hdr->coap_hdr_tcp_32bit_t.header_data[4];
378             length = length_field_data + COAP_TCP_LENGTH_FIELD_32_BIT;
379             break;
380         default:
381             debug("it has wrong type\n");
382     }
383
384     return length;
385 }
386
387 unsigned int coap_get_tcp_header_length(unsigned char *data)
388 {
389     assert(data);
390
391     unsigned int tokenLength =  data[0] & 0x0f;
392     coap_transport_type transport =
393             coap_get_tcp_header_type_from_initbyte(data[0] >> 4);
394     unsigned int length = 0;
395
396     length = coap_get_tcp_header_length_for_transport(transport) + tokenLength;
397     return length;
398 }
399
400 unsigned int coap_get_tcp_header_length_for_transport(coap_transport_type transport)
401 {
402     unsigned int length = 0;
403     switch(transport)
404     {
405         case coap_tcp:
406             length = COAP_TCP_HEADER_NO_FIELD;
407             break;
408         case coap_tcp_8bit:
409             length = COAP_TCP_HEADER_8_BIT;
410             break;
411         case coap_tcp_16bit:
412             length = COAP_TCP_HEADER_16_BIT;
413             break;
414         case coap_tcp_32bit:
415             length = COAP_TCP_HEADER_32_BIT;
416             break;
417         default:
418             debug("it has wrong type\n");
419     }
420
421     return length;
422 }
423
424 #endif
425
426 void coap_add_code(const coap_pdu_t *pdu, coap_transport_type transport, unsigned int code)
427 {
428     assert(pdu);
429
430     switch(transport)
431     {
432         case coap_udp:
433             pdu->hdr->coap_hdr_udp_t.code = COAP_RESPONSE_CODE(code);
434             break;
435 #ifdef WITH_TCP
436         case coap_tcp:
437             pdu->hdr->coap_hdr_tcp_t.code = COAP_RESPONSE_CODE(code);
438             break;
439         case coap_tcp_8bit:
440             pdu->hdr->coap_hdr_tcp_8bit_t.code = COAP_RESPONSE_CODE(code);
441             break;
442         case coap_tcp_16bit:
443             pdu->hdr->coap_hdr_tcp_16bit_t.header_data[3] = COAP_RESPONSE_CODE(code);
444             break;
445         case coap_tcp_32bit:
446             pdu->hdr->coap_hdr_tcp_32bit_t.header_data[5] = COAP_RESPONSE_CODE(code);
447             break;
448 #endif
449         default:
450             debug("it has wrong type\n");
451     }
452 }
453
454 unsigned int coap_get_code(const coap_pdu_t *pdu, coap_transport_type transport)
455 {
456     assert(pdu);
457
458     unsigned int code = 0;
459     switch(transport)
460     {
461         case coap_udp:
462             code = pdu->hdr->coap_hdr_udp_t.code;
463             break;
464 #ifdef WITH_TCP
465         case coap_tcp:
466             code = pdu->hdr->coap_hdr_tcp_t.code;
467             break;
468         case coap_tcp_8bit:
469             code = pdu->hdr->coap_hdr_tcp_8bit_t.code;
470             break;
471         case coap_tcp_16bit:
472             code = pdu->hdr->coap_hdr_tcp_16bit_t.header_data[3];
473             break;
474         case coap_tcp_32bit:
475             code = pdu->hdr->coap_hdr_tcp_32bit_t.header_data[5];
476             break;
477 #endif
478         default:
479             debug("it has wrong type\n");
480     }
481     return code;
482 }
483
484 int coap_add_token(coap_pdu_t *pdu, size_t len, const unsigned char *data,
485                    coap_transport_type transport)
486 {
487     const size_t HEADERLENGTH = len + 4;
488     /* must allow for pdu == NULL as callers may rely on this */
489     if (!pdu || len > 8 || pdu->max_size < HEADERLENGTH)
490         return 0;
491
492     unsigned char* token = NULL;
493     switch(transport)
494     {
495         case coap_udp:
496             pdu->hdr->coap_hdr_udp_t.token_length = len;
497             token = pdu->hdr->coap_hdr_udp_t.token;
498             pdu->length = HEADERLENGTH;
499             break;
500 #ifdef WITH_TCP
501         case coap_tcp:
502             pdu->hdr->coap_hdr_tcp_t.token_length = len;
503             token = pdu->hdr->coap_hdr_tcp_t.token;
504             pdu->length = len + COAP_TCP_HEADER_NO_FIELD;
505             break;
506         case coap_tcp_8bit:
507             pdu->hdr->coap_hdr_tcp_8bit_t.token_length = len;
508             token = pdu->hdr->coap_hdr_tcp_8bit_t.token;
509             pdu->length = len + COAP_TCP_HEADER_8_BIT;
510             break;
511         case coap_tcp_16bit:
512             pdu->hdr->coap_hdr_tcp_16bit_t.header_data[0] =
513                     pdu->hdr->coap_hdr_tcp_16bit_t.header_data[0] | len;
514             token = pdu->hdr->coap_hdr_tcp_16bit_t.token;
515             pdu->length = len + COAP_TCP_HEADER_16_BIT;
516             break;
517         case coap_tcp_32bit:
518             pdu->hdr->coap_hdr_tcp_32bit_t.header_data[0] =
519                     pdu->hdr->coap_hdr_tcp_32bit_t.header_data[0] | len;
520             token = pdu->hdr->coap_hdr_tcp_32bit_t.token;
521             pdu->length = len + COAP_TCP_HEADER_32_BIT;
522             break;
523 #endif
524         default:
525             debug("it has wrong type\n");
526     }
527
528     if (len)
529     {
530         memcpy(token, data, len);
531     }
532
533     pdu->max_delta = 0;
534     pdu->data = NULL;
535
536     return 1;
537 }
538
539 void coap_get_token(const coap_hdr_t *pdu_hdr, coap_transport_type transport,
540                     unsigned char **token, unsigned int *token_length)
541 {
542     assert(pdu_hdr);
543     assert(token);
544     assert(token_length);
545
546     switch(transport)
547     {
548         case coap_udp:
549             *token_length = pdu_hdr->coap_hdr_udp_t.token_length;
550             *token = (unsigned char *)pdu_hdr->coap_hdr_udp_t.token;
551             break;
552 #ifdef WITH_TCP
553         case coap_tcp:
554             *token_length = pdu_hdr->coap_hdr_tcp_t.token_length;
555             *token = (unsigned char *)pdu_hdr->coap_hdr_tcp_t.token;
556             break;
557         case coap_tcp_8bit:
558             *token_length = pdu_hdr->coap_hdr_tcp_8bit_t.token_length;
559             *token = (unsigned char *)pdu_hdr->coap_hdr_tcp_8bit_t.token;
560             break;
561         case coap_tcp_16bit:
562             *token_length = (pdu_hdr->coap_hdr_tcp_16bit_t.header_data[0]) & 0x0f;
563             *token = (unsigned char *)pdu_hdr->coap_hdr_tcp_16bit_t.token;
564             break;
565         case coap_tcp_32bit:
566             *token_length = (pdu_hdr->coap_hdr_tcp_32bit_t.header_data[0]) & 0x0f;
567             *token = (unsigned char *)pdu_hdr->coap_hdr_tcp_32bit_t.token;
568             break;
569 #endif
570         default:
571             debug("it has wrong type\n");
572     }
573 }
574
575 /** @FIXME de-duplicate code with coap_add_option_later */
576 size_t coap_add_option(coap_pdu_t *pdu, unsigned short type, unsigned int len,
577         const unsigned char *data, coap_transport_type transport)
578 {
579     size_t optsize;
580     coap_opt_t *opt;
581
582     assert(pdu);
583     pdu->data = NULL;
584
585     if (type < pdu->max_delta)
586     {
587         warn("coap_add_option: options are not in correct order\n");
588         return 0;
589     }
590
591     switch(transport)
592     {
593 #ifdef WITH_TCP
594         case coap_tcp:
595             opt = (unsigned char *) &(pdu->hdr->coap_hdr_tcp_t) + pdu->length;
596             break;
597         case coap_tcp_8bit:
598             opt = (unsigned char *) &(pdu->hdr->coap_hdr_tcp_8bit_t) + pdu->length;
599             break;
600         case coap_tcp_16bit:
601             opt = (unsigned char *) &(pdu->hdr->coap_hdr_tcp_16bit_t) + pdu->length;
602             break;
603         case coap_tcp_32bit:
604             opt = (unsigned char *) &(pdu->hdr->coap_hdr_tcp_32bit_t) + pdu->length;
605             break;
606 #endif
607         default:
608             opt = (unsigned char *) &(pdu->hdr->coap_hdr_udp_t) + pdu->length;
609             break;
610     }
611
612     /* encode option and check length */
613     optsize = coap_opt_encode(opt, pdu->max_size - pdu->length, type - pdu->max_delta, data, len);
614
615     if (!optsize)
616     {
617         warn("coap_add_option: cannot add option\n");
618         /* error */
619         return 0;
620     }
621     else
622     {
623         pdu->max_delta = type;
624         pdu->length += optsize;
625     }
626
627     return optsize;
628 }
629
630 /** @FIXME de-duplicate code with coap_add_option */
631 unsigned char*
632 coap_add_option_later(coap_pdu_t *pdu, unsigned short type, unsigned int len)
633 {
634     size_t optsize;
635     coap_opt_t *opt;
636
637     assert(pdu);
638     pdu->data = NULL;
639
640     if (type < pdu->max_delta)
641     {
642         warn("coap_add_option: options are not in correct order\n");
643         return NULL;
644     }
645
646     opt = (unsigned char *) pdu->hdr + pdu->length;
647
648     /* encode option and check length */
649     optsize = coap_opt_encode(opt, pdu->max_size - pdu->length, type - pdu->max_delta, NULL, len);
650
651     if (!optsize)
652     {
653         warn("coap_add_option: cannot add option\n");
654         /* error */
655         return NULL;
656     }
657     else
658     {
659         pdu->max_delta = type;
660         pdu->length += optsize;
661     }
662
663     return ((unsigned char*) opt) + optsize - len;
664 }
665
666 int coap_add_data(coap_pdu_t *pdu, unsigned int len, const unsigned char *data)
667 {
668     assert(pdu);
669     assert(pdu->data == NULL);
670
671     if (len == 0)
672         return 1;
673
674     if (pdu->length + len + 1 > pdu->max_size)
675     {
676         warn("coap_add_data: cannot add: data too large for PDU\n");
677         assert(pdu->data == NULL);
678         return 0;
679     }
680
681     pdu->data = (unsigned char *) pdu->hdr + pdu->length;
682     *pdu->data = COAP_PAYLOAD_START;
683     pdu->data++;
684
685     memcpy(pdu->data, data, len);
686     pdu->length += len + 1;
687     return 1;
688 }
689
690 int coap_get_data(const coap_pdu_t *pdu, size_t *len, unsigned char **data)
691 {
692     assert(pdu);
693     assert(len);
694     assert(data);
695
696     if (pdu->data)
697     {
698         *len = (unsigned char *) pdu->hdr + pdu->length - pdu->data;
699         *data = pdu->data;
700     }
701     else
702     { /* no data, clear everything */
703         *len = 0;
704         *data = NULL;
705     }
706
707     return *data != NULL;
708 }
709
710 #ifndef SHORT_ERROR_RESPONSE
711 typedef struct
712 {
713     unsigned char code;
714     char *phrase;
715 } error_desc_t;
716
717 /* if you change anything here, make sure, that the longest string does not
718  * exceed COAP_ERROR_PHRASE_LENGTH. */
719 error_desc_t coap_error[] =
720 {
721 { COAP_RESPONSE_CODE(65), "2.01 Created" },
722 { COAP_RESPONSE_CODE(66), "2.02 Deleted" },
723 { COAP_RESPONSE_CODE(67), "2.03 Valid" },
724 { COAP_RESPONSE_CODE(68), "2.04 Changed" },
725 { COAP_RESPONSE_CODE(69), "2.05 Content" },
726 { COAP_RESPONSE_CODE(400), "Bad Request" },
727 { COAP_RESPONSE_CODE(401), "Unauthorized" },
728 { COAP_RESPONSE_CODE(402), "Bad Option" },
729 { COAP_RESPONSE_CODE(403), "Forbidden" },
730 { COAP_RESPONSE_CODE(404), "Not Found" },
731 { COAP_RESPONSE_CODE(405), "Method Not Allowed" },
732 { COAP_RESPONSE_CODE(408), "Request Entity Incomplete" },
733 { COAP_RESPONSE_CODE(413), "Request Entity Too Large" },
734 { COAP_RESPONSE_CODE(415), "Unsupported Media Type" },
735 { COAP_RESPONSE_CODE(500), "Internal Server Error" },
736 { COAP_RESPONSE_CODE(501), "Not Implemented" },
737 { COAP_RESPONSE_CODE(502), "Bad Gateway" },
738 { COAP_RESPONSE_CODE(503), "Service Unavailable" },
739 { COAP_RESPONSE_CODE(504), "Gateway Timeout" },
740 { COAP_RESPONSE_CODE(505), "Proxying Not Supported" },
741 { 0, NULL } /* end marker */
742 };
743
744 char *
745 coap_response_phrase(unsigned char code)
746 {
747     int i;
748     for (i = 0; coap_error[i].code; ++i)
749     {
750         if (coap_error[i].code == code)
751             return coap_error[i].phrase;
752     }
753     return NULL;
754 }
755 #endif
756
757 /**
758  * Advances *optp to next option if still in PDU. This function
759  * returns the number of bytes opt has been advanced or @c 0
760  * on error.
761  */
762 static size_t next_option_safe(coap_opt_t **optp, size_t *length, coap_option_t* option)
763 {
764     //coap_option_t option;
765     size_t optsize;
766
767     assert(optp);
768     assert(*optp);
769     assert(length);
770     optsize = coap_opt_parse(*optp, *length, option);
771     if (optsize)
772     {
773         assert(optsize <= *length);
774
775         *optp += optsize;
776         *length -= optsize;
777     }
778
779     return optsize;
780 }
781
782 int coap_pdu_parse(unsigned char *data, size_t length, coap_pdu_t *pdu,
783                    coap_transport_type transport)
784 {
785     assert(data);
786     assert(pdu);
787
788     if (pdu->max_size < length)
789     {
790         debug("insufficient space to store parsed PDU\n");
791         printf("[COAP] insufficient space to store parsed PDU\n");
792         return -1;
793     }
794
795     unsigned int headerSize = 0;
796
797     if (coap_udp == transport)
798     {
799         headerSize = sizeof(pdu->hdr->coap_hdr_udp_t);
800     }
801 #ifdef WITH_TCP
802     else
803     {
804         headerSize = coap_get_tcp_header_length_for_transport(transport);
805     }
806 #endif
807
808     if (length < headerSize)
809     {
810         debug("discarded invalid PDU\n");
811     }
812
813     coap_opt_t *opt = NULL;
814     unsigned int tokenLength = 0;
815 #ifdef WITH_TCP
816     switch(transport)
817     {
818         case coap_udp:
819             break;
820         case coap_tcp:
821             pdu->hdr->coap_hdr_tcp_t.message_length = data[0] >> 4;
822             pdu->hdr->coap_hdr_tcp_t.token_length = data[0] & 0x0f;
823             pdu->hdr->coap_hdr_tcp_t.code = data[1];
824             tokenLength = pdu->hdr->coap_hdr_tcp_t.token_length;
825             opt = (unsigned char *) (&(pdu->hdr->coap_hdr_tcp_t) + 1) + tokenLength;
826             break;
827         case coap_tcp_8bit:
828             pdu->hdr->coap_hdr_tcp_8bit_t.message_length = data[0] >> 4;
829             pdu->hdr->coap_hdr_tcp_8bit_t.token_length = data[0] & 0x0f;
830             pdu->hdr->coap_hdr_tcp_8bit_t.length_byte = data[1];
831             pdu->hdr->coap_hdr_tcp_8bit_t.code = data[2];
832             tokenLength = pdu->hdr->coap_hdr_tcp_8bit_t.token_length;
833             opt = (unsigned char *) (&(pdu->hdr->coap_hdr_tcp_8bit_t))
834                     + tokenLength + COAP_TCP_HEADER_8_BIT;
835             break;
836         case coap_tcp_16bit:
837             for (size_t i = 0 ; i < headerSize ; i++)
838             {
839                 pdu->hdr->coap_hdr_tcp_16bit_t.header_data[i] = data[i];
840             }
841
842             tokenLength = data[0] & 0x0f;
843             opt = (unsigned char *) (&(pdu->hdr->coap_hdr_tcp_16bit_t) + 1) + tokenLength;
844             break;
845         case coap_tcp_32bit:
846             for (size_t i = 0 ; i < headerSize ; i++)
847             {
848                 pdu->hdr->coap_hdr_tcp_32bit_t.header_data[i] = data[i];
849             }
850
851             tokenLength = data[0] & 0x0f;
852             opt = ((unsigned char *) &(pdu->hdr->coap_hdr_tcp_32bit_t)) +
853                     headerSize + tokenLength;
854             break;
855         default:
856             printf("it has wrong type\n");
857     }
858 #endif
859     pdu->length = length;
860
861     if (coap_udp == transport)
862     {
863         pdu->hdr->coap_hdr_udp_t.version = data[0] >> 6;
864         pdu->hdr->coap_hdr_udp_t.type = (data[0] >> 4) & 0x03;
865         pdu->hdr->coap_hdr_udp_t.token_length = data[0] & 0x0f;
866         pdu->hdr->coap_hdr_udp_t.code = data[1];
867         pdu->data = NULL;
868
869         tokenLength = pdu->hdr->coap_hdr_udp_t.token_length;
870
871         /* sanity checks */
872         if (pdu->hdr->coap_hdr_udp_t.code == 0)
873         {
874             if (length != headerSize || tokenLength)
875             {
876                 debug("coap_pdu_parse: empty message is not empty\n");
877                 goto discard;
878             }
879         }
880
881         if (length < headerSize + tokenLength || tokenLength > 8)
882         {
883             debug("coap_pdu_parse: invalid Token\n");
884             goto discard;
885         }
886
887         memcpy(&pdu->hdr->coap_hdr_udp_t.id, data + 2, 2);
888
889         /* Finally calculate beginning of data block and thereby check integrity
890          * of the PDU structure. */
891
892         /* append data (including the Token) to pdu structure */
893         memcpy(&(pdu->hdr->coap_hdr_udp_t) + 1, data + headerSize, length - headerSize);
894
895         /* skip header + token */
896         length -= (tokenLength + headerSize);
897         opt = (unsigned char *) (&(pdu->hdr->coap_hdr_udp_t) + 1) + tokenLength;
898     }
899 #ifdef WITH_TCP
900     else // common for tcp header setting
901     {
902         pdu->data = NULL;
903
904         if (length < headerSize + tokenLength || tokenLength > 8)
905         {
906             debug("coap_pdu_parse: invalid Token\n");
907             goto discard;
908         }
909         /* Finally calculate beginning of data block and thereby check integrity
910          * of the PDU structure. */
911
912         /* append data (including the Token) to pdu structure */
913         memcpy(((unsigned char *) pdu->hdr) + headerSize,
914                data + headerSize, length - headerSize);
915
916         /* skip header + token */
917         length -= (tokenLength + headerSize);
918     }
919 #endif
920
921     while (length && *opt != COAP_PAYLOAD_START)
922     {
923         coap_option_t option;
924         memset(&option, 0, sizeof(coap_option_t));
925         if (!next_option_safe(&opt, (size_t *) &length, &option))
926         {
927             debug("coap_pdu_parse: drop\n");
928             goto discard;
929         }
930     }
931
932     /* end of packet or start marker */
933     if (length)
934     {
935         assert(*opt == COAP_PAYLOAD_START);
936         opt++;
937         length--;
938
939         if (!length)
940         {
941             debug("coap_pdu_parse: message ending in payload start marker\n");
942             goto discard;
943         }
944
945         debug(
946                 "set data to %p (pdu ends at %p)\n", (unsigned char *)opt,
947                 (unsigned char *)pdu->hdr + pdu->length);
948         pdu->data = (unsigned char *) opt;
949         //printf("[COAP] pdu - data : %s\n", pdu->data);
950     }
951
952     return 1;
953
954     discard: return 0;
955 }