libcoap 4.3.1
pdu.c
Go to the documentation of this file.
1/* pdu.c -- CoAP PDU handling
2 *
3 * Copyright (C) 2010--2022 Olaf Bergmann <bergmann@tzi.org>
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 *
7 * This file is part of the CoAP library libcoap. Please see
8 * README for terms of use.
9 */
10
16#include "coap3/coap_internal.h"
17
18#if defined(HAVE_LIMITS_H)
19#include <limits.h>
20#endif
21
22#include <stdlib.h>
23#include <stdio.h>
24#include <string.h>
25#ifdef HAVE_ARPA_INET_H
26#include <arpa/inet.h>
27#endif
28#ifdef HAVE_WINSOCK2_H
29#include <winsock2.h>
30#endif
31#include <ctype.h>
32
33#ifdef HAVE_INTTYPES_H
34#include <inttypes.h>
35#else /* ! HAVE_INTTYPES_H */
36#define PRIu32 "u"
37#endif /* ! HAVE_INTTYPES_H */
38
39#ifndef min
40#define min(a,b) ((a) < (b) ? (a) : (b))
41#endif
42
43#ifndef max
44#define max(a,b) ((a) > (b) ? (a) : (b))
45#endif
46
47void
48coap_pdu_clear(coap_pdu_t *pdu, size_t size) {
49 assert(pdu);
50 assert(pdu->token);
52 if (pdu->alloc_size > size)
53 pdu->alloc_size = size;
54 pdu->type = 0;
55 pdu->code = 0;
56 pdu->hdr_size = 0;
57 pdu->token_length = 0;
58 pdu->crit_opt = 0;
59 pdu->mid = 0;
60 pdu->max_opt = 0;
61 pdu->max_size = size;
62 pdu->used_size = 0;
63 pdu->data = NULL;
64 pdu->body_data = NULL;
65 pdu->body_length = 0;
66 pdu->body_offset = 0;
67 pdu->body_total = 0;
68 pdu->lg_xmit = NULL;
69}
70
71#ifdef WITH_LWIP
73coap_pdu_from_pbuf( struct pbuf *pbuf )
74{
75 coap_pdu_t *pdu;
76
77 if (pbuf == NULL) return NULL;
78
79 LWIP_ASSERT("Can only deal with contiguous PBUFs (increase PBUF_POOL_BUFSIZE)", pbuf->tot_len == pbuf->len);
80 LWIP_ASSERT("coap_io_do_io needs to receive an exclusive copy of the incoming pbuf", pbuf->ref == 1);
81
82 pdu = coap_malloc_type(COAP_PDU, sizeof(coap_pdu_t) );
83 if (!pdu) {
84 pbuf_free(pbuf);
85 return NULL;
86 }
87
89 pdu->pbuf = pbuf;
90 pdu->token = (uint8_t *)pbuf->payload + pdu->max_hdr_size;
91 pdu->alloc_size = pbuf->tot_len - pdu->max_hdr_size;
92 coap_pdu_clear(pdu, pdu->alloc_size);
93
94 return pdu;
95}
96#endif /* LWIP */
97
100 size_t size) {
101 coap_pdu_t *pdu;
102
103 assert(type <= 0x3);
104 assert(code <= 0xff);
105 assert(mid >= 0 && mid <= 0xffff);
106
107#ifdef WITH_LWIP
108#if MEMP_STATS
109 /* Reserve 1 PDU for a response packet */
110 if (memp_pools[MEMP_COAP_PDU]->stats->used + 1 >=
111 memp_pools[MEMP_COAP_PDU]->stats->avail) {
112 memp_pools[MEMP_COAP_PDU]->stats->err++;
113 return NULL;
114 }
115#endif /* MEMP_STATS */
116#endif /* LWIP */
117 pdu = coap_malloc_type(COAP_PDU, sizeof(coap_pdu_t));
118 if (!pdu) return NULL;
119
120#if defined(WITH_CONTIKI) || defined(WITH_LWIP)
121 assert(size <= COAP_MAX_MESSAGE_SIZE_TCP16 + 4);
122 if (size > COAP_MAX_MESSAGE_SIZE_TCP16 + 4)
123 return NULL;
125#else
127#endif
128
129#ifdef WITH_LWIP
130 pdu->pbuf = pbuf_alloc(PBUF_TRANSPORT, size + pdu->max_hdr_size, PBUF_RAM);
131 if (pdu->pbuf == NULL) {
133 return NULL;
134 }
135 pdu->token = (uint8_t *)pdu->pbuf->payload + pdu->max_hdr_size;
136#else /* WITH_LWIP */
137 uint8_t *buf;
138 pdu->alloc_size = min(size, 256);
140 if (buf == NULL) {
142 return NULL;
143 }
144 pdu->token = buf + pdu->max_hdr_size;
145#endif /* WITH_LWIP */
146 coap_pdu_clear(pdu, size);
147 pdu->mid = mid;
148 pdu->type = type;
149 pdu->code = code;
150 return pdu;
151}
152
155 coap_session_t *session) {
156 coap_pdu_t *pdu = coap_pdu_init(type, code, coap_new_message_id(session),
158 if (!pdu)
159 coap_log_crit("coap_new_pdu: cannot allocate memory for new PDU\n");
160 return pdu;
161}
162
163void
165 if (pdu != NULL) {
166#ifdef WITH_LWIP
167 pbuf_free(pdu->pbuf);
168#else
169 if (pdu->token != NULL)
171#endif
173 }
174}
175
178 coap_session_t *session,
179 size_t token_length,
180 const uint8_t *token,
181 coap_opt_filter_t *drop_options) {
182 coap_pdu_t *pdu = coap_pdu_init(old_pdu->type,
183 old_pdu->code,
184 coap_new_message_id(session),
186
187 if (pdu == NULL)
188 return NULL;
189
190 coap_add_token(pdu, token_length, token);
191 pdu->lg_xmit = old_pdu->lg_xmit;
192
193 if (drop_options == NULL) {
194 /* Drop COAP_PAYLOAD_START as well if data */
195 size_t length = old_pdu->used_size - old_pdu->token_length -
196 (old_pdu->data ?
197 old_pdu->used_size - (old_pdu->data - old_pdu->token) +1 : 0);
198 if (!coap_pdu_resize(pdu, length + pdu->token_length))
199 goto fail;
200 /* Copy the options but not any data across */
201 memcpy(pdu->token + pdu->token_length,
202 old_pdu->token + old_pdu->token_length, length);
203 pdu->used_size += length;
204 pdu->max_opt = old_pdu->max_opt;
205 }
206 else {
207 /* Copy across all the options the slow way */
208 coap_opt_iterator_t opt_iter;
209 coap_opt_t *option;
210
211 coap_option_iterator_init(old_pdu, &opt_iter, COAP_OPT_ALL);
212 while ((option = coap_option_next(&opt_iter))) {
213 if (drop_options && coap_option_filter_get(drop_options, opt_iter.number))
214 continue;
215 if (!coap_add_option_internal(pdu, opt_iter.number,
216 coap_opt_length(option),
217 coap_opt_value(option)))
218 goto fail;
219 }
220 }
221 return pdu;
222
223fail:
224 coap_delete_pdu(pdu);
225 return NULL;
226}
227
228
229/*
230 * The new size does not include the coap header (max_hdr_size)
231 */
232int
233coap_pdu_resize(coap_pdu_t *pdu, size_t new_size) {
234 if (new_size > pdu->alloc_size) {
235#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
236 uint8_t *new_hdr;
237 size_t offset;
238#endif
239 if (pdu->max_size && new_size > pdu->max_size) {
240 coap_log_warn("coap_pdu_resize: pdu too big\n");
241 return 0;
242 }
243#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
244 if (pdu->data != NULL) {
245 assert(pdu->data > pdu->token);
246 offset = pdu->data - pdu->token;
247 } else {
248 offset = 0;
249 }
250 new_hdr = (uint8_t*)realloc(pdu->token - pdu->max_hdr_size,
251 new_size + pdu->max_hdr_size);
252 if (new_hdr == NULL) {
253 coap_log_warn("coap_pdu_resize: realloc failed\n");
254 return 0;
255 }
256 pdu->token = new_hdr + pdu->max_hdr_size;
257 if (offset > 0)
258 pdu->data = pdu->token + offset;
259 else
260 pdu->data = NULL;
261#endif
262 }
263 pdu->alloc_size = new_size;
264 return 1;
265}
266
267int
269 if (size > pdu->alloc_size) {
270 size_t new_size = max(256, pdu->alloc_size * 2);
271 while (size > new_size)
272 new_size *= 2;
273 if (pdu->max_size && new_size > pdu->max_size) {
274 new_size = pdu->max_size;
275 if (new_size < size)
276 return 0;
277 }
278 if (!coap_pdu_resize(pdu, new_size))
279 return 0;
280 }
281 return 1;
282}
283
284int
285coap_add_token(coap_pdu_t *pdu, size_t len, const uint8_t *data) {
286 /* must allow for pdu == NULL as callers may rely on this */
287 if (!pdu || len > 8)
288 return 0;
289
290 if (pdu->used_size) {
292 "coap_add_token: The token must defined first. Token ignored\n");
293 return 0;
294 }
295 if (!coap_pdu_check_resize(pdu, len))
296 return 0;
297 pdu->token_length = (uint8_t)len;
298 if (len)
299 memcpy(pdu->token, data, len);
300 pdu->max_opt = 0;
301 pdu->used_size = len;
302 pdu->data = NULL;
303
304 return 1;
305}
306
307/* It is assumed that coap_encode_var_safe8() has been called to reduce data */
308int
309coap_update_token(coap_pdu_t *pdu, size_t len, const uint8_t *data) {
310 /* must allow for pdu == NULL as callers may rely on this */
311 if (!pdu || len > 8)
312 return 0;
313
314 if (pdu->used_size == 0) {
315 return coap_add_token(pdu, len, data);
316 }
317 if (len == pdu->token_length) {
318 /* Easy case - just data has changed */
319 }
320 else if (len > pdu->token_length) {
321 if (!coap_pdu_check_resize(pdu, pdu->used_size + len - pdu->token_length)) {
322 coap_log_warn("Failed to update token\n");
323 return 0;
324 }
325 memmove(&pdu->token[len - pdu->token_length], pdu->token, pdu->used_size);
326 pdu->used_size += len - pdu->token_length;
327 }
328 else {
329 pdu->used_size -= pdu->token_length - len;
330 memmove(pdu->token, &pdu->token[pdu->token_length - len], pdu->used_size);
331 }
332 if (pdu->data) {
333 pdu->data += len - pdu->token_length;
334 }
335 pdu->token_length = (uint8_t)len;
336 if (len)
337 memcpy(pdu->token, data, len);
338
339 return 1;
340}
341
342int
344 coap_opt_iterator_t opt_iter;
345 coap_opt_t *option;
346 coap_opt_t *next_option = NULL;
347 size_t opt_delta;
348 coap_option_t decode_this;
349 coap_option_t decode_next;
350
351 /* Need to locate where in current options to remove this one */
353 while ((option = coap_option_next(&opt_iter))) {
354 if (opt_iter.number == number) {
355 /* Found option to delete */
356 break;
357 }
358 }
359 if (!option)
360 return 0;
361
362 if (!coap_opt_parse(option, pdu->used_size - (option - pdu->token),
363 &decode_this))
364 return 0;
365
366 next_option = coap_option_next(&opt_iter);
367 if (next_option) {
368 if (!coap_opt_parse(next_option,
369 pdu->used_size - (next_option - pdu->token),
370 &decode_next))
371 return 0;
372 opt_delta = decode_this.delta + decode_next.delta;
373 if (opt_delta <= 12) {
374 /* can simply update the delta of next option */
375 next_option[0] = (next_option[0] & 0x0f) + (coap_opt_t)(opt_delta << 4);
376 }
377 else if (opt_delta <= 269 && decode_next.delta <= 12) {
378 /* next option delta size increase */
379 next_option -= 1;
380 next_option[0] = (next_option[1] & 0x0f) + (13 << 4);
381 next_option[1] = (coap_opt_t)(opt_delta - 13);
382 }
383 else if (opt_delta <= 269) {
384 /* can simply update the delta of next option */
385 next_option[1] = (coap_opt_t)(opt_delta - 13);
386 }
387 else if (decode_next.delta <= 12) {
388 /* next option delta size increase */
389 if (next_option - option < 2) {
390 /* Need to shuffle everything up by 1 before decrement */
391 if (!coap_pdu_check_resize(pdu, pdu->used_size + 1))
392 return 0;
393 /* Possible a re-size took place with a realloc() */
394 /* Need to rediscover this and next options */
396 while ((option = coap_option_next(&opt_iter))) {
397 if (opt_iter.number == number) {
398 /* Found option to delete */
399 break;
400 }
401 }
402 next_option = coap_option_next(&opt_iter);
403 assert(option != NULL);
404 assert(next_option != NULL);
405 memmove(&next_option[1], next_option,
406 pdu->used_size - (next_option - pdu->token));
407 pdu->used_size++;
408 if (pdu->data)
409 pdu->data++;
410 next_option++;
411 }
412 next_option -= 2;
413 next_option[0] = (next_option[2] & 0x0f) + (14 << 4);
414 next_option[1] = (coap_opt_t)((opt_delta - 269) >> 8);
415 next_option[2] = (opt_delta - 269) & 0xff;
416 }
417 else if (decode_next.delta <= 269) {
418 /* next option delta size increase */
419 next_option -= 1;
420 next_option[0] = (next_option[1] & 0x0f) + (14 << 4);
421 next_option[1] = (coap_opt_t)((opt_delta - 269) >> 8);
422 next_option[2] = (opt_delta - 269) & 0xff;
423 }
424 else {
425 next_option[1] = (coap_opt_t)((opt_delta - 269) >> 8);
426 next_option[2] = (opt_delta - 269) & 0xff;
427 }
428 }
429 else {
430 next_option = option + coap_opt_encode_size(decode_this.delta,
431 coap_opt_length(option));
432 pdu->max_opt -= decode_this.delta;
433 }
434 if (pdu->used_size - (next_option - pdu->token))
435 memmove(option, next_option, pdu->used_size - (next_option - pdu->token));
436 pdu->used_size -= next_option - option;
437 if (pdu->data)
438 pdu->data -= next_option - option;
439 return 1;
440}
441
442int
444 /* Validate that the option is repeatable */
445 switch (number) {
446 /* Ignore list of genuine repeatable */
448 case COAP_OPTION_ETAG:
453 case COAP_OPTION_RTAG:
454 break;
455 /* Protest at the known non-repeatable options and ignore them */
471 case COAP_OPTION_ECHO:
474 "Option number %d is not defined as repeatable - dropped\n",
475 number);
476 return 0;
477 default:
478 coap_log_info("Option number %d is not defined as repeatable\n",
479 number);
480 /* Accepting it after warning as there may be user defineable options */
481 break;
482 }
483 return 1;
484}
485
486size_t
488 const uint8_t *data) {
489 coap_opt_iterator_t opt_iter;
490 coap_opt_t *option;
491 uint16_t prev_number = 0;
492 size_t shift;
493 size_t opt_delta;
494 coap_option_t decode;
495 size_t shrink = 0;
496
497 if (number >= pdu->max_opt)
498 return coap_add_option_internal(pdu, number, len, data);
499
500 /* Need to locate where in current options to insert this one */
502 while ((option = coap_option_next(&opt_iter))) {
503 if (opt_iter.number > number) {
504 /* Found where to insert */
505 break;
506 }
507 prev_number = opt_iter.number;
508 }
509 assert(option != NULL);
510 /* size of option inc header to insert */
511 shift = coap_opt_encode_size(number - prev_number, len);
512
513 /* size of next option (header may shrink in size as delta changes */
514 if (!coap_opt_parse(option, pdu->used_size - (option - pdu->token), &decode))
515 return 0;
516 opt_delta = opt_iter.number - number;
517 if (opt_delta == 0) {
518 if (!coap_option_check_repeatable(number))
519 return 0;
520 }
521
522 if (!coap_pdu_check_resize(pdu,
523 pdu->used_size + shift - shrink))
524 return 0;
525
526 /* Possible a re-size took place with a realloc() */
527 /* Need to locate where in current options to insert this one */
529 while ((option = coap_option_next(&opt_iter))) {
530 if (opt_iter.number > number) {
531 /* Found where to insert */
532 break;
533 }
534 }
535 assert(option != NULL);
536
537 if (decode.delta < 13) {
538 /* can simply patch in the new delta of next option */
539 option[0] = (option[0] & 0x0f) + (coap_opt_t)(opt_delta << 4);
540 } else if (decode.delta < 269 && opt_delta < 13) {
541 /* option header is going to shrink by one byte */
542 option[1] = (option[0] & 0x0f) + (coap_opt_t)(opt_delta << 4);
543 shrink = 1;
544 } else if (decode.delta < 269 && opt_delta < 269) {
545 /* can simply patch in the new delta of next option */
546 option[1] = (coap_opt_t)(opt_delta - 13);
547 } else if (opt_delta < 13) {
548 /* option header is going to shrink by two bytes */
549 option[2] = (option[0] & 0x0f) + (coap_opt_t)(opt_delta << 4);
550 shrink = 2;
551 } else if (opt_delta < 269) {
552 /* option header is going to shrink by one bytes */
553 option[1] = (option[0] & 0x0f) + 0xd0;
554 option[2] = (coap_opt_t)(opt_delta - 13);
555 shrink = 1;
556 } else {
557 /* can simply patch in the new delta of next option */
558 option[1] = (coap_opt_t)((opt_delta - 269) >> 8);
559 option[2] = (opt_delta - 269) & 0xff;
560 }
561
562 memmove(&option[shift], &option[shrink],
563 pdu->used_size - (option - pdu->token) - shrink);
564 if (!coap_opt_encode(option, pdu->alloc_size - pdu->used_size,
565 number - prev_number, data, len))
566 return 0;
567
568 pdu->used_size += shift - shrink;
569 if (pdu->data)
570 pdu->data += shift - shrink;
571 return shift;
572}
573
574size_t
576 const uint8_t *data) {
577 coap_opt_iterator_t opt_iter;
578 coap_opt_t *option;
579 coap_option_t decode;
580 size_t new_length = 0;
581 size_t old_length = 0;
582
583 option = coap_check_option(pdu, number, &opt_iter);
584 if (!option)
585 return coap_insert_option(pdu, number, len, data);
586
587 old_length = coap_opt_parse(option, (size_t)-1, &decode);
588 if (old_length == 0)
589 return 0;
590 new_length = coap_opt_encode_size(decode.delta, len);
591
592 if (new_length > old_length) {
593 if (!coap_pdu_check_resize(pdu,
594 pdu->used_size + new_length - old_length))
595 return 0;
596 /* Possible a re-size took place with a realloc() */
597 option = coap_check_option(pdu, number, &opt_iter);
598 }
599
600 if (new_length != old_length)
601 memmove(&option[new_length], &option[old_length],
602 pdu->used_size - (option - pdu->token) - old_length);
603
604 if (!coap_opt_encode(option, new_length,
605 decode.delta, data, len))
606 return 0;
607
608 pdu->used_size += new_length - old_length;
609 if (pdu->data)
610 pdu->data += new_length - old_length;
611 return 1;
612}
613
614size_t
616 const uint8_t *data) {
617 if (pdu->data) {
618 coap_log_warn("coap_add_optlist_pdu: PDU already contains data\n");
619 return 0;
620 }
621 return coap_add_option_internal(pdu, number, len, data);
622}
623
624size_t
626 const uint8_t *data) {
627 size_t optsize;
628 coap_opt_t *opt;
629
630 assert(pdu);
631
632 if (number == pdu->max_opt) {
633 if (!coap_option_check_repeatable(number))
634 return 0;
635 }
636
637 if (COAP_PDU_IS_REQUEST(pdu) &&
638 (number == COAP_OPTION_PROXY_URI ||
639 number == COAP_OPTION_PROXY_SCHEME)) {
640 /*
641 * Need to check whether there is a hop-limit option. If not, it needs
642 * to be inserted by default (RFC 8768).
643 */
644 coap_opt_iterator_t opt_iter;
645
646 if (coap_check_option(pdu, COAP_OPTION_HOP_LIMIT, &opt_iter) == NULL) {
647 size_t hop_limit = COAP_OPTION_HOP_LIMIT;
648
649 coap_insert_option(pdu, COAP_OPTION_HOP_LIMIT, 1, (uint8_t *)&hop_limit);
650 }
651 }
652
653 if (number < pdu->max_opt) {
655 "coap_add_option: options are not in correct order\n");
656 return coap_insert_option(pdu, number, len, data);
657 }
658
659 optsize = coap_opt_encode_size(number - pdu->max_opt, len);
660 if (!coap_pdu_check_resize(pdu,
661 pdu->used_size + optsize))
662 return 0;
663
664 if (pdu->data) {
665 /* include option delimiter */
666 memmove (&pdu->data[optsize-1], &pdu->data[-1],
667 pdu->used_size - (pdu->data - pdu->token) + 1);
668 opt = pdu->data -1;
669 pdu->data += optsize;
670 }
671 else {
672 opt = pdu->token + pdu->used_size;
673 }
674
675 /* encode option and check length */
676 optsize = coap_opt_encode(opt, pdu->alloc_size - pdu->used_size,
677 number - pdu->max_opt, data, len);
678
679 if (!optsize) {
680 coap_log_warn("coap_add_option: cannot add option\n");
681 /* error */
682 return 0;
683 } else {
684 pdu->max_opt = number;
685 pdu->used_size += optsize;
686 }
687
688 return optsize;
689}
690
691int
692coap_add_data(coap_pdu_t *pdu, size_t len, const uint8_t *data) {
693 if (len == 0) {
694 return 1;
695 } else {
696 uint8_t *payload = coap_add_data_after(pdu, len);
697 if (payload != NULL)
698 memcpy(payload, data, len);
699 return payload != NULL;
700 }
701}
702
703uint8_t *
705 assert(pdu);
706 if (pdu->data) {
707 coap_log_warn("coap_add_data: PDU already contains data\n");
708 return 0;
709 }
710
711 if (len == 0)
712 return NULL;
713
714 if (!coap_pdu_resize(pdu, pdu->used_size + len + 1))
715 return 0;
716 pdu->token[pdu->used_size++] = COAP_PAYLOAD_START;
717 pdu->data = pdu->token + pdu->used_size;
718 pdu->used_size += len;
719 return pdu->data;
720}
721
722int
723coap_get_data(const coap_pdu_t *pdu, size_t *len, const uint8_t **data) {
724 size_t offset;
725 size_t total;
726
727 return coap_get_data_large(pdu, len, data, &offset, &total);
728}
729
730int
731coap_get_data_large(const coap_pdu_t *pdu, size_t *len, const uint8_t **data,
732 size_t *offset, size_t *total) {
733 assert(pdu);
734 assert(len);
735 assert(data);
736
737 *offset = pdu->body_offset;
738 *total = pdu->body_total;
739 if (pdu->body_data) {
740 *data = pdu->body_data;
741 *len = pdu->body_length;
742 return 1;
743 }
744 *data = pdu->data;
745 if(pdu->data == NULL) {
746 *len = 0;
747 *total = 0;
748 return 0;
749 }
750
751 *len = pdu->used_size - (pdu->data - pdu->token);
752 if (*total == 0)
753 *total = *len;
754
755 return 1;
756}
757
758#ifndef SHORT_ERROR_RESPONSE
759typedef struct {
760 unsigned char code;
761 const char *phrase;
763
764/* if you change anything here, make sure, that the longest string does not
765 * exceed COAP_ERROR_PHRASE_LENGTH. */
767 { COAP_RESPONSE_CODE(201), "Created" },
768 { COAP_RESPONSE_CODE(202), "Deleted" },
769 { COAP_RESPONSE_CODE(203), "Valid" },
770 { COAP_RESPONSE_CODE(204), "Changed" },
771 { COAP_RESPONSE_CODE(205), "Content" },
772 { COAP_RESPONSE_CODE(231), "Continue" },
773 { COAP_RESPONSE_CODE(400), "Bad Request" },
774 { COAP_RESPONSE_CODE(401), "Unauthorized" },
775 { COAP_RESPONSE_CODE(402), "Bad Option" },
776 { COAP_RESPONSE_CODE(403), "Forbidden" },
777 { COAP_RESPONSE_CODE(404), "Not Found" },
778 { COAP_RESPONSE_CODE(405), "Method Not Allowed" },
779 { COAP_RESPONSE_CODE(406), "Not Acceptable" },
780 { COAP_RESPONSE_CODE(408), "Request Entity Incomplete" },
781 { COAP_RESPONSE_CODE(409), "Conflict" },
782 { COAP_RESPONSE_CODE(412), "Precondition Failed" },
783 { COAP_RESPONSE_CODE(413), "Request Entity Too Large" },
784 { COAP_RESPONSE_CODE(415), "Unsupported Content-Format" },
785 { COAP_RESPONSE_CODE(422), "Unprocessable" },
786 { COAP_RESPONSE_CODE(429), "Too Many Requests" },
787 { COAP_RESPONSE_CODE(500), "Internal Server Error" },
788 { COAP_RESPONSE_CODE(501), "Not Implemented" },
789 { COAP_RESPONSE_CODE(502), "Bad Gateway" },
790 { COAP_RESPONSE_CODE(503), "Service Unavailable" },
791 { COAP_RESPONSE_CODE(504), "Gateway Timeout" },
792 { COAP_RESPONSE_CODE(505), "Proxying Not Supported" },
793 { COAP_RESPONSE_CODE(508), "Hop Limit Reached" },
794 { 0, NULL } /* end marker */
795};
796
797const char *
798coap_response_phrase(unsigned char code) {
799 int i;
800 for (i = 0; coap_error[i].code; ++i) {
801 if (coap_error[i].code == code)
802 return coap_error[i].phrase;
803 }
804 return NULL;
805}
806#endif
807
813static size_t
814next_option_safe(coap_opt_t **optp, size_t *length, uint16_t *max_opt) {
815 coap_option_t option;
816 size_t optsize;
817
818 assert(optp); assert(*optp);
819 assert(length);
820
821 optsize = coap_opt_parse(*optp, *length, &option);
822 if (optsize) {
823 assert(optsize <= *length);
824
825 /* signal an error if this option would exceed the
826 * allowed number space */
827 if (*max_opt + option.delta > COAP_MAX_OPT) {
828 return 0;
829 }
830 *max_opt += option.delta;
831 *optp += optsize;
832 *length -= optsize;
833 }
834
835 return optsize;
836}
837
838size_t
840 const uint8_t *data) {
841 assert(data);
842 size_t header_size = 0;
843
844 if (proto == COAP_PROTO_TCP || proto==COAP_PROTO_TLS) {
845 uint8_t len = *data >> 4;
846 if (len < 13)
847 header_size = 2;
848 else if (len==13)
849 header_size = 3;
850 else if (len==14)
851 header_size = 4;
852 else
853 header_size = 6;
854 } else if (proto == COAP_PROTO_UDP || proto==COAP_PROTO_DTLS) {
855 header_size = 4;
856 }
857
858 return header_size;
859}
860
861size_t
863 const uint8_t *data,
864 size_t length) {
865 assert(data);
866 assert(proto == COAP_PROTO_TCP || proto == COAP_PROTO_TLS);
867 assert(coap_pdu_parse_header_size(proto, data) <= length );
868
869 size_t size = 0;
870
871 if ((proto == COAP_PROTO_TCP || proto==COAP_PROTO_TLS) && length >= 1) {
872 uint8_t len = *data >> 4;
873 if (len < 13) {
874 size = len;
875 } else if (length >= 2) {
876 if (len==13) {
877 size = (size_t)data[1] + COAP_MESSAGE_SIZE_OFFSET_TCP8;
878 } else if (length >= 3) {
879 if (len==14) {
880 size = ((size_t)data[1] << 8) + data[2] + COAP_MESSAGE_SIZE_OFFSET_TCP16;
881 } else if (length >= 5) {
882 size = ((size_t)data[1] << 24) + ((size_t)data[2] << 16)
883 + ((size_t)data[3] << 8) + data[4] + COAP_MESSAGE_SIZE_OFFSET_TCP32;
884 }
885 }
886 }
887 size += data[0] & 0x0f;
888 }
889
890 return size;
891}
892
893int
895 uint8_t *hdr = pdu->token - pdu->hdr_size;
896 if (proto == COAP_PROTO_UDP || proto == COAP_PROTO_DTLS) {
897 assert(pdu->hdr_size == 4);
898 if ((hdr[0] >> 6) != COAP_DEFAULT_VERSION) {
899 coap_log_debug("coap_pdu_parse: UDP version not supported\n");
900 return 0;
901 }
902 pdu->type = (hdr[0] >> 4) & 0x03;
903 pdu->token_length = hdr[0] & 0x0f;
904 pdu->code = hdr[1];
905 pdu->mid = (uint16_t)hdr[2] << 8 | hdr[3];
906 } else if (proto == COAP_PROTO_TCP || proto == COAP_PROTO_TLS) {
907 assert(pdu->hdr_size >= 2 && pdu->hdr_size <= 6);
908 pdu->type = COAP_MESSAGE_CON;
909 pdu->token_length = hdr[0] & 0x0f;
910 pdu->code = hdr[pdu->hdr_size-1];
911 pdu->mid = 0;
912 } else {
913 coap_log_debug("coap_pdu_parse: unsupported protocol\n");
914 return 0;
915 }
916 if (pdu->token_length > pdu->alloc_size) {
917 /* Invalid PDU provided - not wise to assert here though */
918 coap_log_debug("coap_pdu_parse: PDU header token size broken\n");
919 pdu->token_length = (uint8_t)pdu->alloc_size;
920 return 0;
921 }
922 return 1;
923}
924
925static int
927 switch ((coap_pdu_signaling_proto_t)pdu->code) {
929 switch (pdu->max_opt) {
931 if (len > 4) goto bad;
932 break;
934 if (len > 0) goto bad;
935 break;
936 default:
937 ;
938 }
939 break;
942 switch (pdu->max_opt) {
944 if (len > 0) goto bad;
945 break;
946 default:
947 ;
948 }
949 break;
951 switch (pdu->max_opt) {
953 if (len < 1 || len > 255) goto bad;
954 break;
956 if (len > 3) goto bad;
957 break;
958 default:
959 ;
960 }
961 break;
963 switch (pdu->max_opt) {
965 if (len > 2) goto bad;
966 break;
967 default:
968 ;
969 }
970 break;
971 default:
972 ;
973 }
974 return 1;
975bad:
976 return 0;
977}
978
979static int
981 int res = 1;
982
983 switch (pdu->max_opt) {
984 case COAP_OPTION_IF_MATCH: if (len > 8) res = 0; break;
985 case COAP_OPTION_URI_HOST: if (len < 1 || len > 255) res = 0; break;
986 case COAP_OPTION_ETAG: if (len < 1 || len > 8) res = 0; break;
987 case COAP_OPTION_IF_NONE_MATCH: if (len != 0) res = 0; break;
988 case COAP_OPTION_OBSERVE: if (len > 3) res = 0; break;
989 case COAP_OPTION_URI_PORT: if (len > 2) res = 0; break;
990 case COAP_OPTION_LOCATION_PATH: if (len > 255) res = 0; break;
991 case COAP_OPTION_OSCORE: if (len > 255) res = 0; break;
992 case COAP_OPTION_URI_PATH: if (len > 255) res = 0; break;
993 case COAP_OPTION_CONTENT_FORMAT:if (len > 2) res = 0; break;
994 case COAP_OPTION_MAXAGE: if (len > 4) res = 0; break;
995 case COAP_OPTION_URI_QUERY: if (len < 1 || len > 255) res = 0; break;
996 case COAP_OPTION_HOP_LIMIT: if (len != 1) res = 0; break;
997 case COAP_OPTION_ACCEPT: if (len > 2) res = 0; break;
998 case COAP_OPTION_LOCATION_QUERY:if (len > 255) res = 0; break;
999 case COAP_OPTION_BLOCK2: if (len > 3) res = 0; break;
1000 case COAP_OPTION_BLOCK1: if (len > 3) res = 0; break;
1001 case COAP_OPTION_SIZE2: if (len > 4) res = 0; break;
1002 case COAP_OPTION_PROXY_URI: if (len < 1 || len > 1034) res = 0; break;
1003 case COAP_OPTION_PROXY_SCHEME: if (len < 1 || len > 255) res = 0; break;
1004 case COAP_OPTION_SIZE1: if (len > 4) res = 0; break;
1005 case COAP_OPTION_ECHO: if (len > 40) res = 0; break;
1006 case COAP_OPTION_NORESPONSE: if (len > 1) res = 0; break;
1007 case COAP_OPTION_RTAG: if (len > 8) res = 0; break;
1008 default:
1009 ;
1010 }
1011 return res;
1012}
1013
1014static int
1015write_prefix(char **obp, size_t *len, const char *prf, size_t prflen) {
1016 /* Make sure space for null terminating byte */
1017 if (*len < prflen +1) {
1018 return 0;
1019 }
1020
1021 memcpy(*obp, prf, prflen);
1022 *obp += prflen;
1023 *len -= prflen;
1024 return 1;
1025}
1026
1027static int
1028write_char(char **obp, size_t *len, char c, int printable) {
1029 /* Make sure space for null terminating byte */
1030 if (*len < 2 +1) {
1031 return 0;
1032 }
1033
1034 if (!printable) {
1035 const uint8_t hex[] = "0123456789abcdef";
1036 (*obp)[0] = hex[(c & 0xf0) >> 4];
1037 (*obp)[1] = hex[c & 0x0f];
1038 } else {
1039 (*obp)[0] = isprint(c) ? c : '.';
1040 (*obp)[1] = ' ';
1041 }
1042 *obp += 2;
1043 *len -= 2;
1044 return 1;
1045}
1046
1047int
1049
1050 int good = 1;
1051 /* sanity checks */
1052 if (pdu->code == 0) {
1053 if (pdu->used_size != 0 || pdu->token_length) {
1054 coap_log_debug("coap_pdu_parse: empty message is not empty\n");
1055 return 0;
1056 }
1057 }
1058
1059 if (pdu->token_length > pdu->used_size || pdu->token_length > 8) {
1060 coap_log_debug("coap_pdu_parse: invalid Token\n");
1061 return 0;
1062 }
1063
1064 pdu->max_opt = 0;
1065 if (pdu->code == 0) {
1066 /* empty packet */
1067 pdu->used_size = 0;
1068 pdu->data = NULL;
1069 } else {
1070 /* skip header + token */
1071 coap_opt_t *opt = pdu->token + pdu->token_length;
1072 size_t length = pdu->used_size - pdu->token_length;
1073
1074 while (length > 0 && *opt != COAP_PAYLOAD_START) {
1075 coap_opt_t *opt_last = opt;
1076 size_t optsize = next_option_safe(&opt, &length, &pdu->max_opt);
1077 const uint32_t len =
1078 optsize ? coap_opt_length((const uint8_t *)opt - optsize) : 0;
1079 if (optsize == 0) {
1081 "coap_pdu_parse: %d.%02d: offset %u malformed option\n",
1082 pdu->code >> 5, pdu->code & 0x1F,
1083 (int)(opt_last - pdu->token - pdu->token_length));
1084 good = 0;
1085 break;
1086 }
1087 if (COAP_PDU_IS_SIGNALING(pdu) ?
1088 !coap_pdu_parse_opt_csm(pdu, len) :
1089 !coap_pdu_parse_opt_base(pdu, len)) {
1091 "coap_pdu_parse: %d.%02d: offset %u option %u has bad length %" PRIu32 "\n",
1092 pdu->code >> 5, pdu->code & 0x1F,
1093 (int)(opt_last - pdu->token - pdu->token_length), pdu->max_opt,
1094 len);
1095 good = 0;
1096 }
1097 }
1098
1099 if (!good) {
1100 /*
1101 * Dump the options in the PDU for analysis, space separated except
1102 * error options which are prefixed by *
1103 * Two rows - hex and ascii (if printable)
1104 */
1105 static char outbuf[COAP_DEBUG_BUF_SIZE];
1106 char *obp;
1107 size_t tlen;
1108 size_t outbuflen;
1109 int i;
1110 int ok;
1111
1112 for (i = 0; i < 2; i++) {
1113 opt = pdu->token + pdu->token_length;
1114 length = pdu->used_size - pdu->token_length;
1115 pdu->max_opt = 0;
1116
1117 outbuflen = sizeof(outbuf);
1118 obp = outbuf;
1119 ok = write_prefix(&obp, &outbuflen, "O: ", 3);
1120 while (length > 0 && *opt != COAP_PAYLOAD_START) {
1121 coap_opt_t *opt_last = opt;
1122 size_t optsize = next_option_safe(&opt, &length, &pdu->max_opt);
1123 const uint32_t len =
1124 optsize ? coap_opt_length((const uint8_t *)opt - optsize) : 0;
1125 if (!optsize || (COAP_PDU_IS_SIGNALING(pdu) ?
1126 !coap_pdu_parse_opt_csm(pdu, len) :
1127 !coap_pdu_parse_opt_base(pdu, len))) {
1128 ok = ok && write_prefix(&obp, &outbuflen, "*", 1);
1129 if (!optsize) {
1130 /* Skip to end of options to output all data */
1131 opt = pdu->token + pdu->used_size;
1132 length = 0;
1133 }
1134 }
1135 else {
1136 ok = ok && write_prefix(&obp, &outbuflen, " ", 1);
1137 }
1138 tlen = opt - opt_last;
1139 while (tlen--) {
1140 ok = ok && write_char(&obp, &outbuflen, *opt_last, i);
1141 opt_last++;
1142 }
1143 }
1144 if (length && *opt == COAP_PAYLOAD_START) {
1145 ok = ok && write_char(&obp, &outbuflen, *opt, i);
1146 }
1147 /* write_*() always leaves a spare byte to null terminate */
1148 *obp = '\000';
1149 coap_log_debug("%s\n", outbuf);
1150 }
1151 }
1152
1153 if (length > 0) {
1154 assert(*opt == COAP_PAYLOAD_START);
1155 opt++; length--;
1156
1157 if (length == 0) {
1159 "coap_pdu_parse: message ending in payload start marker\n");
1160 return 0;
1161 }
1162 }
1163 if (length > 0)
1164 pdu->data = (uint8_t*)opt;
1165 else
1166 pdu->data = NULL;
1167 }
1168
1169 return good;
1170}
1171
1172int
1174 const uint8_t *data,
1175 size_t length,
1176 coap_pdu_t *pdu)
1177{
1178 size_t hdr_size;
1179
1180 if (length == 0)
1181 return 0;
1182 hdr_size = coap_pdu_parse_header_size(proto, data);
1183 if (!hdr_size || hdr_size > length)
1184 return 0;
1185 if (hdr_size > pdu->max_hdr_size)
1186 return 0;
1187 if (!coap_pdu_resize(pdu, length - hdr_size))
1188 return 0;
1189 if (pdu->token - hdr_size != data)
1190 memcpy(pdu->token - hdr_size, data, length);
1191 pdu->hdr_size = (uint8_t)hdr_size;
1192 pdu->used_size = length - hdr_size;
1193 return coap_pdu_parse_header(pdu, proto) && coap_pdu_parse_opt(pdu);
1194}
1195
1196size_t
1198 if (proto == COAP_PROTO_UDP || proto == COAP_PROTO_DTLS) {
1199 assert(pdu->max_hdr_size >= 4);
1200 if (pdu->max_hdr_size < 4) {
1202 "coap_pdu_encode_header: not enough space for UDP-style header\n");
1203 return 0;
1204 }
1205 pdu->token[-4] = COAP_DEFAULT_VERSION << 6
1206 | pdu->type << 4
1207 | pdu->token_length;
1208 pdu->token[-3] = pdu->code;
1209 pdu->token[-2] = (uint8_t)(pdu->mid >> 8);
1210 pdu->token[-1] = (uint8_t)(pdu->mid);
1211 pdu->hdr_size = 4;
1212 } else if (proto == COAP_PROTO_TCP || proto == COAP_PROTO_TLS) {
1213 size_t len;
1214 assert(pdu->used_size >= pdu->token_length);
1215 if (pdu->used_size < pdu->token_length) {
1216 coap_log_warn("coap_pdu_encode_header: corrupted PDU\n");
1217 return 0;
1218 }
1219 len = pdu->used_size - pdu->token_length;
1220 if (len <= COAP_MAX_MESSAGE_SIZE_TCP0) {
1221 assert(pdu->max_hdr_size >= 2);
1222 if (pdu->max_hdr_size < 2) {
1224 "coap_pdu_encode_header: not enough space for TCP0 header\n");
1225 return 0;
1226 }
1227 pdu->token[-2] = (uint8_t)len << 4
1228 | pdu->token_length;
1229 pdu->token[-1] = pdu->code;
1230 pdu->hdr_size = 2;
1231 } else if (len <= COAP_MAX_MESSAGE_SIZE_TCP8) {
1232 assert(pdu->max_hdr_size >= 3);
1233 if (pdu->max_hdr_size < 3) {
1235 "coap_pdu_encode_header: not enough space for TCP8 header\n");
1236 return 0;
1237 }
1238 pdu->token[-3] = 13 << 4 | pdu->token_length;
1239 pdu->token[-2] = (uint8_t)(len - COAP_MESSAGE_SIZE_OFFSET_TCP8);
1240 pdu->token[-1] = pdu->code;
1241 pdu->hdr_size = 3;
1242 } else if (len <= COAP_MAX_MESSAGE_SIZE_TCP16) {
1243 assert(pdu->max_hdr_size >= 4);
1244 if (pdu->max_hdr_size < 4) {
1246 "coap_pdu_encode_header: not enough space for TCP16 header\n");
1247 return 0;
1248 }
1249 pdu->token[-4] = 14 << 4 | pdu->token_length;
1250 pdu->token[-3] = (uint8_t)((len - COAP_MESSAGE_SIZE_OFFSET_TCP16) >> 8);
1251 pdu->token[-2] = (uint8_t)(len - COAP_MESSAGE_SIZE_OFFSET_TCP16);
1252 pdu->token[-1] = pdu->code;
1253 pdu->hdr_size = 4;
1254 } else {
1255 assert(pdu->max_hdr_size >= 6);
1256 if (pdu->max_hdr_size < 6) {
1258 "coap_pdu_encode_header: not enough space for TCP32 header\n");
1259 return 0;
1260 }
1261 pdu->token[-6] = 15 << 4 | pdu->token_length;
1262 pdu->token[-5] = (uint8_t)((len - COAP_MESSAGE_SIZE_OFFSET_TCP32) >> 24);
1263 pdu->token[-4] = (uint8_t)((len - COAP_MESSAGE_SIZE_OFFSET_TCP32) >> 16);
1264 pdu->token[-3] = (uint8_t)((len - COAP_MESSAGE_SIZE_OFFSET_TCP32) >> 8);
1265 pdu->token[-2] = (uint8_t)(len - COAP_MESSAGE_SIZE_OFFSET_TCP32);
1266 pdu->token[-1] = pdu->code;
1267 pdu->hdr_size = 6;
1268 }
1269 } else {
1270 coap_log_warn("coap_pdu_encode_header: unsupported protocol\n");
1271 }
1272 return pdu->hdr_size;
1273}
1274
1277 return pdu->code;
1278}
1279
1280void
1282 assert(code <= 0xff);
1283 pdu->code = code;
1284}
1285
1287 return pdu->type;
1288}
1289
1291 assert(type <= 0x3);
1292 pdu->type = type;
1293}
1294
1296 coap_bin_const_t token;
1297
1298 token.length = pdu->token_length;
1299 token.s = pdu->token;
1300 return token;
1301}
1302
1304 return pdu->mid;
1305}
1306
1308 assert(mid >= 0 && mid <= 0xffff);
1309 pdu->mid = mid;
1310}
Pulls together all the internal only header files.
size_t coap_opt_parse(const coap_opt_t *opt, size_t length, coap_option_t *result)
Parses the option pointed to by opt into result.
Definition: coap_option.c:41
uint16_t coap_option_num_t
Definition: coap_option.h:20
uint8_t coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
Definition: coap_option.h:26
uint16_t coap_new_message_id(coap_session_t *session)
Returns a new message id and updates session->tx_mid accordingly.
#define coap_log_debug(...)
Definition: coap_debug.h:48
#define coap_log_info(...)
Definition: coap_debug.h:46
#define coap_log_warn(...)
Definition: coap_debug.h:45
#define coap_log_crit(...)
Definition: coap_debug.h:43
coap_opt_t * coap_option_next(coap_opt_iterator_t *oi)
Updates the iterator oi to point to the next option.
Definition: coap_option.c:152
size_t coap_opt_encode(coap_opt_t *opt, size_t maxlen, uint16_t delta, const uint8_t *val, size_t length)
Encodes option with given delta into opt.
Definition: coap_option.c:371
uint32_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
Definition: coap_option.c:211
coap_opt_iterator_t * coap_option_iterator_init(const coap_pdu_t *pdu, coap_opt_iterator_t *oi, const coap_opt_filter_t *filter)
Initializes the given option iterator oi to point to the beginning of the pdu's option list.
Definition: coap_option.c:116
size_t coap_opt_encode_size(uint16_t delta, size_t length)
Compute storage bytes needed for an option with given delta and length.
Definition: coap_option.c:350
#define COAP_OPT_ALL
Pre-defined filter that includes all options.
Definition: coap_option.h:108
coap_opt_t * coap_check_option(const coap_pdu_t *pdu, coap_option_num_t number, coap_opt_iterator_t *oi)
Retrieves the first option of number number from pdu.
Definition: coap_option.c:198
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
Definition: coap_option.c:248
int coap_option_filter_get(coap_opt_filter_t *filter, coap_option_num_t option)
Checks if number is contained in filter.
Definition: coap_option.c:503
#define COAP_MESSAGE_SIZE_OFFSET_TCP8
size_t coap_insert_option(coap_pdu_t *pdu, coap_option_num_t number, size_t len, const uint8_t *data)
Inserts option of given number in the pdu with the appropriate data.
Definition: pdu.c:487
int coap_remove_option(coap_pdu_t *pdu, coap_option_num_t number)
Removes (first) option of given number from the pdu.
Definition: pdu.c:343
int coap_update_token(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Updates token in pdu with length len and data.
Definition: pdu.c:309
#define COAP_PDU_MAX_UDP_HEADER_SIZE
int coap_pdu_parse_header(coap_pdu_t *pdu, coap_proto_t proto)
Decode the protocol specific header for the specified PDU.
Definition: pdu.c:894
size_t coap_pdu_parse_header_size(coap_proto_t proto, const uint8_t *data)
Interprets data to determine the number of bytes in the header.
Definition: pdu.c:839
#define COAP_PDU_MAX_TCP_HEADER_SIZE
#define COAP_MAX_MESSAGE_SIZE_TCP8
#define COAP_PDU_IS_SIGNALING(pdu)
#define COAP_MAX_MESSAGE_SIZE_TCP0
int coap_option_check_repeatable(coap_option_num_t number)
Check whether the option is allowed to be repeated or not.
Definition: pdu.c:443
#define COAP_MESSAGE_SIZE_OFFSET_TCP16
void coap_pdu_clear(coap_pdu_t *pdu, size_t size)
Clears any contents from pdu and resets used_size, and data pointers.
Definition: pdu.c:48
#define COAP_MESSAGE_SIZE_OFFSET_TCP32
int coap_pdu_parse_opt(coap_pdu_t *pdu)
Verify consistency in the given CoAP PDU structure and locate the data.
Definition: pdu.c:1048
size_t coap_update_option(coap_pdu_t *pdu, coap_option_num_t number, size_t len, const uint8_t *data)
Updates existing first option of given number in the pdu with the new data.
Definition: pdu.c:575
size_t coap_pdu_encode_header(coap_pdu_t *pdu, coap_proto_t proto)
Compose the protocol specific header for the specified PDU.
Definition: pdu.c:1197
#define COAP_DEFAULT_VERSION
#define COAP_PAYLOAD_START
int coap_pdu_check_resize(coap_pdu_t *pdu, size_t size)
Dynamically grows the size of pdu to new_size if needed.
Definition: pdu.c:268
size_t coap_pdu_parse_size(coap_proto_t proto, const uint8_t *data, size_t length)
Parses data to extract the message size.
Definition: pdu.c:862
int coap_pdu_resize(coap_pdu_t *pdu, size_t new_size)
Dynamically grows the size of pdu to new_size.
Definition: pdu.c:233
#define COAP_PDU_IS_REQUEST(pdu)
#define COAP_MAX_MESSAGE_SIZE_TCP16
size_t coap_add_option_internal(coap_pdu_t *pdu, coap_option_num_t number, size_t len, const uint8_t *data)
Adds option of given number to pdu that is passed as first parameter.
Definition: pdu.c:625
#define COAP_OPTION_HOP_LIMIT
Definition: pdu.h:125
#define COAP_OPTION_NORESPONSE
Definition: pdu.h:135
#define COAP_OPTION_URI_HOST
Definition: pdu.h:112
#define COAP_OPTION_IF_MATCH
Definition: pdu.h:111
coap_pdu_code_t coap_pdu_get_code(const coap_pdu_t *pdu)
Gets the PDU code associated with pdu.
Definition: pdu.c:1276
#define COAP_OPTION_BLOCK2
Definition: pdu.h:128
const char * coap_response_phrase(unsigned char code)
Returns a human-readable response phrase for the specified CoAP response code.
Definition: pdu.c:798
#define COAP_OPTION_CONTENT_FORMAT
Definition: pdu.h:120
#define COAP_SIGNALING_OPTION_ALTERNATIVE_ADDRESS
Definition: pdu.h:188
#define COAP_OPTION_SIZE2
Definition: pdu.h:130
#define COAP_OPTION_BLOCK1
Definition: pdu.h:129
#define COAP_OPTION_PROXY_SCHEME
Definition: pdu.h:132
uint8_t * coap_add_data_after(coap_pdu_t *pdu, size_t len)
Adds given data to the pdu that is passed as first parameter but does not.
Definition: pdu.c:704
#define COAP_OPTION_URI_QUERY
Definition: pdu.h:124
void coap_delete_pdu(coap_pdu_t *pdu)
Dispose of an CoAP PDU and frees associated storage.
Definition: pdu.c:164
void coap_pdu_set_code(coap_pdu_t *pdu, coap_pdu_code_t code)
Sets the PDU code in the pdu.
Definition: pdu.c:1281
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition: pdu.h:243
#define COAP_OPTION_IF_NONE_MATCH
Definition: pdu.h:114
#define COAP_OPTION_LOCATION_PATH
Definition: pdu.h:117
#define COAP_OPTION_URI_PATH
Definition: pdu.h:119
#define COAP_RESPONSE_CODE(N)
Definition: pdu.h:146
coap_proto_t
CoAP protocol types.
Definition: pdu.h:292
coap_pdu_t * coap_new_pdu(coap_pdu_type_t type, coap_pdu_code_t code, coap_session_t *session)
Creates a new CoAP PDU.
Definition: pdu.c:154
coap_pdu_code_t
Set of codes available for a PDU.
Definition: pdu.h:303
#define COAP_OPTION_OSCORE
Definition: pdu.h:118
#define COAP_OPTION_SIZE1
Definition: pdu.h:133
coap_pdu_type_t
CoAP PDU message type definitions.
Definition: pdu.h:60
#define COAP_SIGNALING_OPTION_BLOCK_WISE_TRANSFER
Definition: pdu.h:184
int coap_add_token(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Adds token of length len to pdu.
Definition: pdu.c:285
#define COAP_OPTION_LOCATION_QUERY
Definition: pdu.h:127
void coap_pdu_set_type(coap_pdu_t *pdu, coap_pdu_type_t type)
Sets the PDU type in the pdu.
Definition: pdu.c:1290
size_t coap_add_option(coap_pdu_t *pdu, coap_option_num_t number, size_t len, const uint8_t *data)
Adds option of given number to pdu that is passed as first parameter.
Definition: pdu.c:615
#define COAP_SIGNALING_OPTION_CUSTODY
Definition: pdu.h:186
coap_pdu_signaling_proto_t
Definition: pdu.h:174
coap_pdu_type_t coap_pdu_get_type(const coap_pdu_t *pdu)
Gets the PDU type associated with pdu.
Definition: pdu.c:1286
int coap_get_data(const coap_pdu_t *pdu, size_t *len, const uint8_t **data)
Retrieves the length and data pointer of specified PDU.
Definition: pdu.c:723
int coap_pdu_parse(coap_proto_t proto, const uint8_t *data, size_t length, coap_pdu_t *pdu)
Parses data into the CoAP PDU structure given in result.
Definition: pdu.c:1173
#define COAP_MAX_OPT
the highest option number we know
Definition: pdu.h:138
void coap_pdu_set_mid(coap_pdu_t *pdu, coap_mid_t mid)
Sets the message id in the pdu.
Definition: pdu.c:1307
#define COAP_OPTION_RTAG
Definition: pdu.h:136
coap_pdu_t * coap_pdu_duplicate(const coap_pdu_t *old_pdu, coap_session_t *session, size_t token_length, const uint8_t *token, coap_opt_filter_t *drop_options)
Duplicate an existing PDU.
Definition: pdu.c:177
#define COAP_OPTION_URI_PORT
Definition: pdu.h:116
coap_pdu_t * coap_pdu_init(coap_pdu_type_t type, coap_pdu_code_t code, coap_mid_t mid, size_t size)
Creates a new CoAP PDU with at least enough storage space for the given size maximum message size.
Definition: pdu.c:99
#define COAP_OPTION_ACCEPT
Definition: pdu.h:126
int coap_get_data_large(const coap_pdu_t *pdu, size_t *len, const uint8_t **data, size_t *offset, size_t *total)
Retrieves the data from a PDU, with support for large bodies of data that spans multiple PDUs.
Definition: pdu.c:731
coap_mid_t coap_pdu_get_mid(const coap_pdu_t *pdu)
Gets the message id associated with pdu.
Definition: pdu.c:1303
#define COAP_OPTION_MAXAGE
Definition: pdu.h:123
#define COAP_OPTION_ETAG
Definition: pdu.h:113
#define COAP_OPTION_PROXY_URI
Definition: pdu.h:131
#define COAP_OPTION_OBSERVE
Definition: pdu.h:115
#define COAP_SIGNALING_OPTION_HOLD_OFF
Definition: pdu.h:189
#define COAP_OPTION_ECHO
Definition: pdu.h:134
#define COAP_SIGNALING_OPTION_BAD_CSM_OPTION
Definition: pdu.h:191
#define COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE
Definition: pdu.h:183
int coap_add_data(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Adds given data to the pdu that is passed as first parameter.
Definition: pdu.c:692
coap_bin_const_t coap_pdu_get_token(const coap_pdu_t *pdu)
Gets the token associated with pdu.
Definition: pdu.c:1295
@ COAP_PROTO_DTLS
Definition: pdu.h:295
@ COAP_PROTO_UDP
Definition: pdu.h:294
@ COAP_PROTO_TLS
Definition: pdu.h:297
@ COAP_PROTO_TCP
Definition: pdu.h:296
@ COAP_MESSAGE_CON
Definition: pdu.h:61
@ COAP_SIGNALING_RELEASE
Definition: pdu.h:178
@ COAP_SIGNALING_CSM
Definition: pdu.h:175
@ COAP_SIGNALING_PONG
Definition: pdu.h:177
@ COAP_SIGNALING_PING
Definition: pdu.h:176
@ COAP_SIGNALING_ABORT
Definition: pdu.h:179
size_t coap_session_max_pdu_size(const coap_session_t *session)
Get maximum acceptable PDU size.
Definition: coap_session.c:372
@ COAP_PDU
Definition: mem.h:44
@ COAP_PDU_BUF
Definition: mem.h:45
void * coap_malloc_type(coap_memory_tag_t type, size_t size)
Allocates a chunk of size bytes and returns a pointer to the newly allocated memory.
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
static size_t next_option_safe(coap_opt_t **optp, size_t *length, uint16_t *max_opt)
Advances *optp to next option if still in PDU.
Definition: pdu.c:814
static int write_char(char **obp, size_t *len, char c, int printable)
Definition: pdu.c:1028
static int coap_pdu_parse_opt_csm(coap_pdu_t *pdu, uint16_t len)
Definition: pdu.c:926
error_desc_t coap_error[]
Definition: pdu.c:766
static int write_prefix(char **obp, size_t *len, const char *prf, size_t prflen)
Definition: pdu.c:1015
#define PRIu32
Definition: pdu.c:36
static int coap_pdu_parse_opt_base(coap_pdu_t *pdu, uint16_t len)
Definition: pdu.c:980
#define min(a, b)
Definition: pdu.c:40
#define max(a, b)
Definition: pdu.c:44
CoAP binary data definition with const data.
Definition: coap_str.h:64
size_t length
length of binary data
Definition: coap_str.h:65
const uint8_t * s
read-only binary data
Definition: coap_str.h:66
Iterator to run through PDU options.
Definition: coap_option.h:169
coap_option_num_t number
decoded option number
Definition: coap_option.h:171
Representation of CoAP options.
Definition: coap_option.h:32
uint16_t delta
Definition: coap_option.h:33
structure for CoAP PDUs
uint8_t max_hdr_size
space reserved for protocol-specific header
uint16_t max_opt
highest option number in PDU
uint8_t * token
first byte of token, if any, or options
coap_lg_xmit_t * lg_xmit
Holds ptr to lg_xmit if sending a set of blocks.
size_t body_length
Holds body data length.
size_t max_size
maximum size for token, options and payload, or zero for variable size pdu
const uint8_t * body_data
Holds ptr to re-assembled data or NULL.
size_t body_offset
Holds body data offset.
coap_pdu_code_t code
request method (value 1–31) or response code (value 64-255)
uint8_t token_length
length of Token
uint8_t hdr_size
actual size used for protocol-specific header (0 until header is encoded)
uint8_t * data
first byte of payload, if any
coap_mid_t mid
message id, if any, in regular host byte order
size_t used_size
used bytes of storage for token, options and payload
uint8_t crit_opt
Set if unknown critical option for proxy.
size_t alloc_size
allocated storage for token, options and payload
size_t body_total
Holds body data total size.
coap_pdu_type_t type
message type
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
unsigned char code
Definition: pdu.c:760
const char * phrase
Definition: pdu.c:761