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