libcoap 4.3.5-develop-19f1d78
Loading...
Searching...
No Matches
coap_pdu.c
Go to the documentation of this file.
1/* coap_pdu.c -- CoAP PDU handling
2 *
3 * Copyright (C) 2010--2025 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
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#ifndef __ZEPHYR__
26#ifdef HAVE_ARPA_INET_H
27#include <arpa/inet.h>
28#endif
29#ifdef HAVE_WINSOCK2_H
30#include <winsock2.h>
31#endif
32#endif /* !__ZEPHYR__ */
33
34#include <ctype.h>
35
36#ifndef min
37#define min(a,b) ((a) < (b) ? (a) : (b))
38#endif
39
40#ifndef max
41#define max(a,b) ((a) > (b) ? (a) : (b))
42#endif
43
44void
45coap_pdu_clear(coap_pdu_t *pdu, size_t size) {
46 assert(pdu);
47 assert(pdu->token);
49 if (pdu->alloc_size > size)
50 pdu->alloc_size = size;
51 pdu->type = 0;
52 pdu->code = 0;
53 pdu->ref = 0;
54 pdu->hdr_size = 0;
55 pdu->actual_token.length = 0;
56 pdu->e_token_length = 0;
57 pdu->crit_opt = 0;
58 pdu->mid = 0;
59 pdu->max_opt = 0;
60 pdu->max_size = size;
61 pdu->used_size = 0;
62 pdu->data = NULL;
63 pdu->body_data = NULL;
64 pdu->body_length = 0;
65 pdu->body_offset = 0;
66 pdu->body_total = 0;
67 pdu->lg_xmit = NULL;
68 pdu->session = NULL;
69 pdu->data_free = NULL;
70}
71
72#ifdef WITH_LWIP
74coap_pdu_from_pbuf(struct pbuf *pbuf) {
75 coap_pdu_t *pdu;
76
77 if (pbuf == NULL)
78 return NULL;
79
80 LWIP_ASSERT("Can only deal with contiguous PBUFs (increase PBUF_POOL_BUFSIZE)",
81 pbuf->tot_len == pbuf->len);
82 LWIP_ASSERT("coap_io_do_io needs to receive an exclusive copy of the incoming pbuf",
83 pbuf->ref == 1);
84
85 pdu = coap_malloc_type(COAP_PDU, sizeof(coap_pdu_t));
86 if (!pdu) {
87 pbuf_free(pbuf);
88 return NULL;
89 }
90
92 pdu->pbuf = pbuf;
93 pdu->token = (uint8_t *)pbuf->payload + pdu->max_hdr_size;
94 pdu->alloc_size = pbuf->tot_len - pdu->max_hdr_size;
95 coap_pdu_clear(pdu, pdu->alloc_size);
96
97 return pdu;
98}
99#endif /* LWIP */
100
103 size_t size) {
104 coap_pdu_t *pdu;
105
106#ifndef RIOT_VERSION
107 assert(type <= 0x3);
108 assert(code <= 0xff);
109 assert(mid >= 0 && mid <= 0xffff);
110#endif /* RIOT_VERSION */
111
112#if defined(WITH_LWIP) && MEMP_USE_CUSTOM_POOLS
113#if MEMP_STATS
114 /* Reserve 1 PDU for a response packet */
115 if (memp_pools[MEMP_COAP_PDU]->stats->used + 1 >=
116 memp_pools[MEMP_COAP_PDU]->stats->avail) {
117 memp_pools[MEMP_COAP_PDU]->stats->err++;
118 return NULL;
119 }
120#endif /* MEMP_STATS */
121#endif /* LWIP && MEMP_USE_CUSTOM_POOLS */
122 pdu = coap_malloc_type(COAP_PDU, sizeof(coap_pdu_t));
123 if (!pdu)
124 return NULL;
125
126#if COAP_DEFAULT_MAX_PDU_RX_SIZE <= COAP_MAX_MESSAGE_SIZE_TCP16
127 /* on TCP, the CoAP header will also have a maximum length of 4 bytes */
129#else
131#endif
132 if (size > ((size_t)COAP_DEFAULT_MAX_PDU_RX_SIZE - pdu->max_hdr_size)) {
134 return NULL;
135 }
136
137#ifdef WITH_LWIP
138 pdu->pbuf = pbuf_alloc(PBUF_TRANSPORT, size + pdu->max_hdr_size, PBUF_RAM);
139 if (pdu->pbuf == NULL) {
141 return NULL;
142 }
143 pdu->token = (uint8_t *)pdu->pbuf->payload + pdu->max_hdr_size;
144#else /* WITH_LWIP */
145 uint8_t *buf;
146 pdu->alloc_size = min(size, COAP_DEFAULT_MTU);
148 if (buf == NULL) {
150 return NULL;
151 }
152 pdu->token = buf + pdu->max_hdr_size;
153#endif /* WITH_LWIP */
154 coap_pdu_clear(pdu, size);
155 pdu->mid = mid;
156 pdu->type = type;
157 pdu->code = code;
158 return pdu;
159}
160
163 coap_session_t *session) {
164 coap_pdu_t *pdu;
165
166 coap_lock_lock(return NULL);
167 pdu = coap_new_pdu_lkd(type, code, session);
169 return pdu;
170}
171
174 coap_session_t *session) {
175 coap_pdu_t *pdu;
176
178 pdu = coap_pdu_init(type, code, coap_new_message_id_lkd(session),
180 if (!pdu)
181 coap_log_crit("coap_new_pdu: cannot allocate memory for new PDU (size = %" PRIuS ")\n",
183 return pdu;
184}
185
186COAP_API void
192
193void
195 if (pdu != NULL) {
196 if (pdu->ref) {
197 pdu->ref--;
198 return;
199 }
200#ifdef WITH_LWIP
201 pbuf_free(pdu->pbuf);
202#else
203 if (pdu->token != NULL)
205#endif
208 }
209}
210
213 coap_session_t *session,
214 size_t token_length,
215 const uint8_t *token,
216 coap_opt_filter_t *drop_options) {
217 coap_pdu_t *new_pdu;
218
219 coap_lock_lock(return NULL);
220 new_pdu = coap_pdu_duplicate_lkd(old_pdu,
221 session,
222 token_length,
223 token,
224 drop_options);
226 return new_pdu;
227}
228
229
230/*
231 * Note: This does not include any data, just the token and options
232 */
235 coap_session_t *session,
236 size_t token_length,
237 const uint8_t *token,
238 coap_opt_filter_t *drop_options) {
239#if COAP_CLIENT_SUPPORT
240 uint8_t doing_first = session->doing_first;
241#endif /* COAP_CLIENT_SUPPORT */
242 coap_pdu_t *pdu;
243
245 /*
246 * Need to make sure that coap_session_max_pdu_size_lkd() immediately
247 * returns, rather than wait for the first CSM response from remote
248 * that indicates BERT size (TCP/TLS only) as this may be called early
249 * the OSCORE logic.
250 */
251#if COAP_CLIENT_SUPPORT
252 session->doing_first = 0;
253#endif /* COAP_CLIENT_SUPPORT */
254 pdu = coap_pdu_init(old_pdu->type, old_pdu->code,
256 max(old_pdu->max_size,
258#if COAP_CLIENT_SUPPORT
259 /* Restore any pending waits */
260 session->doing_first = doing_first;
261#endif /* COAP_CLIENT_SUPPORT */
262 if (pdu == NULL)
263 return NULL;
264
265 coap_add_token(pdu, token_length, token);
266 pdu->lg_xmit = old_pdu->lg_xmit;
267
268 if (drop_options == NULL) {
269 /* Drop COAP_PAYLOAD_START as well if data */
270 size_t length = old_pdu->used_size - old_pdu->e_token_length -
271 (old_pdu->data ?
272 old_pdu->used_size - (old_pdu->data - old_pdu->token) +1 : 0);
273 if (!coap_pdu_resize(pdu, length + pdu->e_token_length))
274 goto fail;
275 /* Copy the options but not any data across */
276 memcpy(pdu->token + pdu->e_token_length,
277 old_pdu->token + old_pdu->e_token_length, length);
278 pdu->used_size += length;
279 pdu->max_opt = old_pdu->max_opt;
280 } else {
281 /* Copy across all the options the slow way */
282 coap_opt_iterator_t opt_iter;
283 coap_opt_t *option;
284
285 coap_option_iterator_init(old_pdu, &opt_iter, COAP_OPT_ALL);
286 while ((option = coap_option_next(&opt_iter))) {
287 if (drop_options && coap_option_filter_get(drop_options, opt_iter.number))
288 continue;
289 if (!coap_add_option_internal(pdu, opt_iter.number,
290 coap_opt_length(option),
291 coap_opt_value(option)))
292 goto fail;
293 }
294 }
295 return pdu;
296
297fail:
299 return NULL;
300}
301
302
303/*
304 * The new size does not include the coap header (max_hdr_size)
305 */
306int
307coap_pdu_resize(coap_pdu_t *pdu, size_t new_size) {
308 if (new_size > pdu->alloc_size) {
309#if !defined(WITH_LWIP)
310 uint8_t *new_hdr;
311 size_t offset;
312#endif
313 if (pdu->max_size && new_size > pdu->max_size) {
314 coap_log_warn("coap_pdu_resize: pdu too big\n");
315 return 0;
316 }
317#if !defined(WITH_LWIP)
318 if (pdu->data != NULL) {
319 assert(pdu->data > pdu->token);
320 offset = pdu->data - pdu->token;
321 } else {
322 offset = 0;
323 }
324 new_hdr = (uint8_t *)coap_realloc_type(COAP_PDU_BUF,
325 pdu->token - pdu->max_hdr_size,
326 new_size + pdu->max_hdr_size);
327 if (new_hdr == NULL) {
328 coap_log_warn("coap_pdu_resize: realloc failed\n");
329 return 0;
330 }
331 pdu->token = new_hdr + pdu->max_hdr_size;
332 if (offset > 0)
333 pdu->data = pdu->token + offset;
334 else
335 pdu->data = NULL;
337 pdu->actual_token.s = &pdu->token[0];
339 pdu->actual_token.s = &pdu->token[1];
340 else
341 pdu->actual_token.s = &pdu->token[2];
342#endif
343 }
344 pdu->alloc_size = new_size;
345 return 1;
346}
347
348int
350 if (size > pdu->alloc_size) {
351 size_t new_size = max(256, pdu->alloc_size * 2);
352 while (size > new_size)
353 new_size *= 2;
354 if (pdu->max_size && new_size > pdu->max_size) {
355 new_size = pdu->max_size;
356 if (new_size < size)
357 return 0;
358 }
359 if (!coap_pdu_resize(pdu, new_size))
360 return 0;
361 }
362 return 1;
363}
364
365int
366coap_add_token(coap_pdu_t *pdu, size_t len, const uint8_t *data) {
367 size_t bias = 0;
368
369 /* must allow for pdu == NULL as callers may rely on this */
370 if (!pdu)
371 return 0;
372
373 if (pdu->used_size) {
374 coap_log_warn("coap_add_token: The token must defined first. Token ignored\n");
375 return 0;
376 }
377 pdu->actual_token.length = len;
378 if (len < COAP_TOKEN_EXT_1B_BIAS) {
379 bias = 0;
380 } else if (len < COAP_TOKEN_EXT_2B_BIAS) {
381 bias = 1;
382 } else if (len <= COAP_TOKEN_EXT_MAX) {
383 bias = 2;
384 } else {
385 coap_log_warn("coap_add_token: Token size too large. Token ignored\n");
386 return 0;
387 }
388 if (!coap_pdu_check_resize(pdu, len + bias)) {
389 coap_log_warn("coap_add_token: Insufficient space for token. Token ignored\n");
390 return 0;
391 }
392
393 pdu->actual_token.length = len;
394 pdu->actual_token.s = &pdu->token[bias];
395 pdu->e_token_length = (uint32_t)(len + bias);
396 if (len) {
397 switch (bias) {
398 case 0:
399 memcpy(pdu->token, data, len);
400 break;
401 case 1:
402 pdu->token[0] = (uint8_t)(len - COAP_TOKEN_EXT_1B_BIAS);
403 memcpy(&pdu->token[1], data, len);
404 break;
405 case 2:
406 pdu->token[0] = (uint8_t)((len - COAP_TOKEN_EXT_2B_BIAS) >> 8);
407 pdu->token[1] = (uint8_t)((len - COAP_TOKEN_EXT_2B_BIAS) & 0xff);
408 memcpy(&pdu->token[2], data, len);
409 break;
410 default:
411 break;
412 }
413 }
414 pdu->max_opt = 0;
415 pdu->used_size = len + bias;
416 pdu->data = NULL;
417
418 return 1;
419}
420
421/* It is assumed that coap_encode_var_safe8() has been called to reduce data */
422int
423coap_update_token(coap_pdu_t *pdu, size_t len, const uint8_t *data) {
424 size_t bias = 0;
425 size_t old_len;
426
427 /* must allow for pdu == NULL as callers may rely on this */
428 if (!pdu)
429 return 0;
430
431 if (pdu->used_size == 0) {
432 return coap_add_token(pdu, len, data);
433 }
434
435 old_len = pdu->e_token_length;
436
437 if (len < COAP_TOKEN_EXT_1B_BIAS) {
438 bias = 0;
439 } else if (len < COAP_TOKEN_EXT_2B_BIAS) {
440 bias = 1;
441 } else if (len <= COAP_TOKEN_EXT_MAX) {
442 bias = 2;
443 } else {
444 coap_log_warn("coap_add_token: Token size too large. Token ignored\n");
445 return 0;
446 }
447 if ((len + bias) == pdu->e_token_length) {
448 /* Easy case - just data has changed */
449 } else if ((len + bias) > pdu->e_token_length) {
450 if (!coap_pdu_check_resize(pdu,
451 pdu->used_size + (len + bias) - pdu->e_token_length)) {
452 coap_log_warn("Failed to update token\n");
453 return 0;
454 }
455 memmove(&pdu->token[(len + bias) - pdu->e_token_length],
456 pdu->token, pdu->used_size);
457 pdu->used_size += len + bias - pdu->e_token_length;
458 if (pdu->data) {
459 pdu->data += (len + bias) - pdu->e_token_length;
460 }
461 } else {
462 pdu->used_size -= pdu->e_token_length - (len + bias);
463 memmove(pdu->token, &pdu->token[pdu->e_token_length - (len + bias)], pdu->used_size);
464 if (pdu->data) {
465 pdu->data -= pdu->e_token_length - (len + bias);
466 }
467 }
468
469 pdu->actual_token.length = len;
470 pdu->actual_token.s = &pdu->token[bias];
471 pdu->e_token_length = (uint8_t)(len + bias);
472 if (len) {
473 switch (bias) {
474 case 0:
475 if (memcmp(pdu->token, data, len) != 0)
476 memcpy(pdu->token, data, len);
477 break;
478 case 1:
479 pdu->token[0] = (uint8_t)(len - COAP_TOKEN_EXT_1B_BIAS);
480 memcpy(&pdu->token[1], data, len);
481 break;
482 case 2:
483 pdu->token[0] = (uint8_t)((len - COAP_TOKEN_EXT_2B_BIAS) >> 8);
484 pdu->token[1] = (uint8_t)((len - COAP_TOKEN_EXT_2B_BIAS) & 0xff);
485 memcpy(&pdu->token[2], data, len);
486 break;
487 default:
488 break;
489 }
490 }
491 if (old_len != pdu->e_token_length && pdu->hdr_size && pdu->session)
492 /* Need to fix up the header */
493 if (!coap_pdu_encode_header(pdu, pdu->session->proto))
494 return 0;
495 return 1;
496}
497
498int
500 coap_opt_iterator_t opt_iter;
501 coap_opt_t *option;
502 coap_opt_t *next_option = NULL;
503 size_t opt_delta;
504 coap_option_t decode_this;
505 coap_option_t decode_next;
506
507 /* Need to locate where in current options to remove this one */
509 while ((option = coap_option_next(&opt_iter))) {
510 if (opt_iter.number == number) {
511 /* Found option to delete */
512 break;
513 }
514 }
515 if (!option)
516 return 0;
517
518 if (!coap_opt_parse(option, pdu->used_size - (option - pdu->token),
519 &decode_this))
520 return 0;
521
522 next_option = coap_option_next(&opt_iter);
523 if (next_option) {
524 if (!coap_opt_parse(next_option,
525 pdu->used_size - (next_option - pdu->token),
526 &decode_next))
527 return 0;
528 opt_delta = decode_this.delta + decode_next.delta;
529 if (opt_delta < 13) {
530 /* can simply update the delta of next option */
531 next_option[0] = (next_option[0] & 0x0f) + (coap_opt_t)(opt_delta << 4);
532 } else if (opt_delta < 269 && decode_next.delta < 13) {
533 /* next option delta size increase */
534 next_option -= 1;
535 next_option[0] = (next_option[1] & 0x0f) + (13 << 4);
536 next_option[1] = (coap_opt_t)(opt_delta - 13);
537 } else if (opt_delta < 269) {
538 /* can simply update the delta of next option */
539 next_option[1] = (coap_opt_t)(opt_delta - 13);
540 } else if (decode_next.delta < 13) { /* opt_delta >= 269 */
541 /* next option delta size increase */
542 if (next_option - option < 2) {
543 /* Need to shuffle everything up by 1 before decrement */
544 if (!coap_pdu_check_resize(pdu, pdu->used_size + 1))
545 return 0;
546 /* Possible a re-size took place with a realloc() */
547 /* Need to rediscover this and next options */
549 while ((option = coap_option_next(&opt_iter))) {
550 if (opt_iter.number == number) {
551 /* Found option to delete */
552 break;
553 }
554 }
555 next_option = coap_option_next(&opt_iter);
556 assert(option != NULL);
557 assert(next_option != NULL);
558 memmove(&next_option[1], next_option,
559 pdu->used_size - (next_option - pdu->token));
560 pdu->used_size++;
561 if (pdu->data)
562 pdu->data++;
563 next_option++;
564 }
565 next_option -= 2;
566 next_option[0] = (next_option[2] & 0x0f) + (14 << 4);
567 next_option[1] = (coap_opt_t)((opt_delta - 269) >> 8);
568 next_option[2] = (opt_delta - 269) & 0xff;
569 } else if (decode_next.delta < 269) { /* opt_delta >= 269 */
570 /* next option delta size increase */
571 next_option -= 1;
572 next_option[0] = (next_option[1] & 0x0f) + (14 << 4);
573 next_option[1] = (coap_opt_t)((opt_delta - 269) >> 8);
574 next_option[2] = (opt_delta - 269) & 0xff;
575 } else { /* decode_next.delta >= 269 && opt_delta >= 269 */
576 next_option[1] = (coap_opt_t)((opt_delta - 269) >> 8);
577 next_option[2] = (opt_delta - 269) & 0xff;
578 }
579 } else {
580 next_option = option + coap_opt_encode_size(decode_this.delta,
581 coap_opt_length(option));
582 pdu->max_opt -= decode_this.delta;
583 }
584 if (pdu->used_size - (next_option - pdu->token))
585 memmove(option, next_option, pdu->used_size - (next_option - pdu->token));
586 pdu->used_size -= next_option - option;
587 if (pdu->data)
588 pdu->data -= next_option - option;
589 return 1;
590}
591
592int
594 /* Validate that the option is repeatable */
595 switch (number) {
596 /* Ignore list of genuine repeatable */
598 case COAP_OPTION_ETAG:
604 case COAP_OPTION_RTAG:
605 break;
606 /* Protest at the known non-repeatable options and ignore them */
624 case COAP_OPTION_ECHO:
626 coap_log_info("Option number %d is not defined as repeatable - dropped\n",
627 number);
628 return 0;
629 default:
630 coap_log_info("Option number %d is not defined as repeatable\n",
631 number);
632 /* Accepting it after warning as there may be user defineable options */
633 break;
634 }
635 return 1;
636}
637
638size_t
640 const uint8_t *data) {
641 coap_opt_iterator_t opt_iter;
642 coap_opt_t *option;
643 uint16_t prev_number = 0;
644 size_t shift;
645 size_t opt_delta;
646 coap_option_t decode;
647 size_t shrink = 0;
648
649 if (number >= pdu->max_opt)
650 return coap_add_option_internal(pdu, number, len, data);
651
652 /* Need to locate where in current options to insert this one */
654 while ((option = coap_option_next(&opt_iter))) {
655 if (opt_iter.number > number) {
656 /* Found where to insert */
657 break;
658 }
659 prev_number = opt_iter.number;
660 }
661 assert(option != NULL);
662 /* size of option inc header to insert */
663 shift = coap_opt_encode_size(number - prev_number, len);
664
665 /* size of next option (header may shrink in size as delta changes */
666 if (!coap_opt_parse(option, pdu->used_size - (option - pdu->token), &decode))
667 return 0;
668 opt_delta = opt_iter.number - number;
669 if (opt_delta == 0) {
670 if (!coap_option_check_repeatable(number))
671 return 0;
672 }
673
674 if (!coap_pdu_check_resize(pdu,
675 pdu->used_size + shift - shrink))
676 return 0;
677
678 /* Possible a re-size took place with a realloc() */
679 /* Need to locate where in current options to insert this one */
681 while ((option = coap_option_next(&opt_iter))) {
682 if (opt_iter.number > number) {
683 /* Found where to insert */
684 break;
685 }
686 }
687 assert(option != NULL);
688
689 if (decode.delta < 13) {
690 /* can simply patch in the new delta of next option */
691 option[0] = (option[0] & 0x0f) + (coap_opt_t)(opt_delta << 4);
692 } else if (decode.delta < 269 && opt_delta < 13) {
693 /* option header is going to shrink by one byte */
694 option[1] = (option[0] & 0x0f) + (coap_opt_t)(opt_delta << 4);
695 shrink = 1;
696 } else if (decode.delta < 269 && opt_delta < 269) {
697 /* can simply patch in the new delta of next option */
698 option[1] = (coap_opt_t)(opt_delta - 13);
699 } else if (opt_delta < 13) {
700 /* option header is going to shrink by two bytes */
701 option[2] = (option[0] & 0x0f) + (coap_opt_t)(opt_delta << 4);
702 shrink = 2;
703 } else if (opt_delta < 269) {
704 /* option header is going to shrink by one bytes */
705 option[1] = (option[0] & 0x0f) + 0xd0;
706 option[2] = (coap_opt_t)(opt_delta - 13);
707 shrink = 1;
708 } else {
709 /* can simply patch in the new delta of next option */
710 option[1] = (coap_opt_t)((opt_delta - 269) >> 8);
711 option[2] = (opt_delta - 269) & 0xff;
712 }
713
714 memmove(&option[shift], &option[shrink],
715 pdu->used_size - (option - pdu->token) - shrink);
716 if (!coap_opt_encode(option, pdu->alloc_size - pdu->used_size,
717 number - prev_number, data, len))
718 return 0;
719
720 if (shift >= shrink) {
721 pdu->used_size += shift - shrink;
722 if (pdu->data)
723 pdu->data += shift - shrink;
724 } else {
725 pdu->used_size -= shrink - shift;
726 if (pdu->data)
727 pdu->data -= shrink - shift;
728 }
729 return shift;
730}
731
732size_t
734 const uint8_t *data) {
735 coap_opt_iterator_t opt_iter;
736 coap_opt_t *option;
737 coap_option_t decode;
738 size_t new_length = 0;
739 size_t old_length = 0;
740
741 option = coap_check_option(pdu, number, &opt_iter);
742 if (!option)
743 return coap_insert_option(pdu, number, len, data);
744
745 old_length = coap_opt_parse(option, (size_t)-1, &decode);
746 if (old_length == 0)
747 return 0;
748 new_length = coap_opt_encode_size(decode.delta, len);
749
750 if (new_length > old_length) {
751 if (!coap_pdu_check_resize(pdu,
752 pdu->used_size + new_length - old_length))
753 return 0;
754 /* Possible a re-size took place with a realloc() */
755 option = coap_check_option(pdu, number, &opt_iter);
756 }
757
758 if (new_length != old_length)
759 memmove(&option[new_length], &option[old_length],
760 pdu->used_size - (option - pdu->token) - old_length);
761
762 if (!coap_opt_encode(option, new_length,
763 decode.delta, data, len))
764 return 0;
765
766 if (new_length >= old_length) {
767 pdu->used_size += new_length - old_length;
768 if (pdu->data)
769 pdu->data += new_length - old_length;
770 } else {
771 pdu->used_size -= old_length - new_length;
772 if (pdu->data)
773 pdu->data -= old_length - new_length;
774 }
775 return 1;
776}
777
778size_t
780 const uint8_t *data) {
781 if (pdu->data) {
782 coap_log_warn("coap_add_optlist_pdu: PDU already contains data\n");
783 return 0;
784 }
785 return coap_add_option_internal(pdu, number, len, data);
786}
787
788size_t
790 const uint8_t *data) {
791 size_t optsize;
792 coap_opt_t *opt;
793
794 assert(pdu);
795
796 if (number == pdu->max_opt) {
797 if (!coap_option_check_repeatable(number))
798 return 0;
799 }
800
801 if (COAP_PDU_IS_REQUEST(pdu) &&
802 (number == COAP_OPTION_PROXY_URI ||
803 number == COAP_OPTION_PROXY_SCHEME)) {
804 /*
805 * Need to check whether there is a hop-limit option. If not, it needs
806 * to be inserted by default (RFC 8768).
807 */
808 coap_opt_iterator_t opt_iter;
809
810 if (coap_check_option(pdu, COAP_OPTION_HOP_LIMIT, &opt_iter) == NULL) {
811 size_t hop_limit = COAP_OPTION_HOP_LIMIT;
812
813 coap_insert_option(pdu, COAP_OPTION_HOP_LIMIT, 1, (uint8_t *)&hop_limit);
814 }
815 }
816
817 if (number < pdu->max_opt) {
818 coap_log_debug("coap_add_option: options are not in correct order\n");
819 return coap_insert_option(pdu, number, len, data);
820 }
821
822 optsize = coap_opt_encode_size(number - pdu->max_opt, len);
823 if (!coap_pdu_check_resize(pdu,
824 pdu->used_size + optsize))
825 return 0;
826
827 if (pdu->data) {
828 /* include option delimiter */
829 memmove(&pdu->data[optsize-1], &pdu->data[-1],
830 pdu->used_size - (pdu->data - pdu->token) + 1);
831 opt = pdu->data -1;
832 pdu->data += optsize;
833 } else {
834 opt = pdu->token + pdu->used_size;
835 }
836
837 /* encode option and check length */
838 optsize = coap_opt_encode(opt, pdu->alloc_size - pdu->used_size,
839 number - pdu->max_opt, data, len);
840
841 if (!optsize) {
842 coap_log_warn("coap_add_option: cannot add option\n");
843 /* error */
844 return 0;
845 } else {
846 pdu->max_opt = number;
847 pdu->used_size += optsize;
848 }
849
850 return optsize;
851}
852
853int
854coap_add_data(coap_pdu_t *pdu, size_t len, const uint8_t *data) {
855 if (len == 0) {
856 return 1;
857 } else {
858 uint8_t *payload = coap_add_data_after(pdu, len);
859 if (payload != NULL)
860 memcpy(payload, data, len);
861 return payload != NULL;
862 }
863}
864
865uint8_t *
867 assert(pdu);
868 if (pdu->data) {
869 coap_log_warn("coap_add_data: PDU already contains data\n");
870 return 0;
871 }
872
873 if (len == 0)
874 return NULL;
875
876 if (!coap_pdu_resize(pdu, pdu->used_size + len + 1))
877 return 0;
878 pdu->token[pdu->used_size++] = COAP_PAYLOAD_START;
879 pdu->data = pdu->token + pdu->used_size;
880 pdu->used_size += len;
881 return pdu->data;
882}
883
884int
885coap_get_data(const coap_pdu_t *pdu, size_t *len, const uint8_t **data) {
886 size_t offset;
887 size_t total;
888
889 return coap_get_data_large(pdu, len, data, &offset, &total);
890}
891
892int
893coap_get_data_large(const coap_pdu_t *pdu, size_t *len, const uint8_t **data,
894 size_t *offset, size_t *total) {
895 assert(pdu);
896 assert(len);
897 assert(data);
898
899 *offset = pdu->body_offset;
900 *total = pdu->body_total;
901 if (pdu->body_data) {
902 *data = pdu->body_data;
903 *len = pdu->body_length;
904 return 1;
905 }
906 *data = pdu->data;
907 if (pdu->data == NULL) {
908 *len = 0;
909 *total = 0;
910 return 0;
911 }
912
913 *len = pdu->used_size - (pdu->data - pdu->token);
914 if (*total == 0)
915 *total = *len;
916
917 return 1;
918}
919
920#ifndef SHORT_ERROR_RESPONSE
921typedef struct {
922 unsigned char code;
923 const char *phrase;
925
926/* if you change anything here, make sure, that the longest string does not
927 * exceed COAP_ERROR_PHRASE_LENGTH. */
929 { COAP_RESPONSE_CODE(201), "Created" },
930 { COAP_RESPONSE_CODE(202), "Deleted" },
931 { COAP_RESPONSE_CODE(203), "Valid" },
932 { COAP_RESPONSE_CODE(204), "Changed" },
933 { COAP_RESPONSE_CODE(205), "Content" },
934 { COAP_RESPONSE_CODE(231), "Continue" },
935 { COAP_RESPONSE_CODE(400), "Bad Request" },
936 { COAP_RESPONSE_CODE(401), "Unauthorized" },
937 { COAP_RESPONSE_CODE(402), "Bad Option" },
938 { COAP_RESPONSE_CODE(403), "Forbidden" },
939 { COAP_RESPONSE_CODE(404), "Not Found" },
940 { COAP_RESPONSE_CODE(405), "Method Not Allowed" },
941 { COAP_RESPONSE_CODE(406), "Not Acceptable" },
942 { COAP_RESPONSE_CODE(408), "Request Entity Incomplete" },
943 { COAP_RESPONSE_CODE(409), "Conflict" },
944 { COAP_RESPONSE_CODE(412), "Precondition Failed" },
945 { COAP_RESPONSE_CODE(413), "Request Entity Too Large" },
946 { COAP_RESPONSE_CODE(415), "Unsupported Content-Format" },
947 { COAP_RESPONSE_CODE(422), "Unprocessable" },
948 { COAP_RESPONSE_CODE(429), "Too Many Requests" },
949 { COAP_RESPONSE_CODE(500), "Internal Server Error" },
950 { COAP_RESPONSE_CODE(501), "Not Implemented" },
951 { COAP_RESPONSE_CODE(502), "Bad Gateway" },
952 { COAP_RESPONSE_CODE(503), "Service Unavailable" },
953 { COAP_RESPONSE_CODE(504), "Gateway Timeout" },
954 { COAP_RESPONSE_CODE(505), "Proxying Not Supported" },
955 { COAP_RESPONSE_CODE(508), "Hop Limit Reached" },
956 { 0, NULL } /* end marker */
957};
958
959const char *
960coap_response_phrase(unsigned char code) {
961 int i;
962 for (i = 0; coap_error[i].code; ++i) {
963 if (coap_error[i].code == code)
964 return coap_error[i].phrase;
965 }
966 return NULL;
967}
968#endif
969
975static size_t
976next_option_safe(coap_opt_t **optp, size_t *length, uint16_t *max_opt) {
977 coap_option_t option;
978 size_t optsize;
979
980 assert(optp);
981 assert(*optp);
982 assert(length);
983
984 optsize = coap_opt_parse(*optp, *length, &option);
985 assert(optsize <= *length);
986
987 /* signal an error if this option would exceed the
988 * allowed number space */
989 if ((uint32_t)(*max_opt) + option.delta > COAP_MAX_OPT) {
990 return 0;
991 }
992 *max_opt += option.delta;
993 *optp += optsize;
994 *length -= optsize;
995
996 return optsize;
997}
998
999size_t
1001 const uint8_t *data) {
1002 assert(data);
1003 size_t header_size = 0;
1004
1005 if (proto == COAP_PROTO_TCP || proto==COAP_PROTO_TLS) {
1006 uint8_t len = *data >> 4;
1007 if (len < 13)
1008 header_size = 2;
1009 else if (len==13)
1010 header_size = 3;
1011 else if (len==14)
1012 header_size = 4;
1013 else
1014 header_size = 6;
1015 } else if (proto == COAP_PROTO_WS || proto==COAP_PROTO_WSS) {
1016 header_size = 2;
1017 } else if (proto == COAP_PROTO_UDP || proto==COAP_PROTO_DTLS) {
1018 header_size = 4;
1019 }
1020
1021 return header_size;
1022}
1023
1024#if !COAP_DISABLE_TCP
1025/*
1026 * strm
1027 * return +ve PDU size including token
1028 * 0 PDU does not parse
1029 */
1030size_t
1032 const uint8_t *data,
1033 size_t length) {
1034 assert(data);
1035 assert(proto == COAP_PROTO_TCP || proto == COAP_PROTO_TLS ||
1036 proto == COAP_PROTO_WS || proto == COAP_PROTO_WSS);
1037 assert(coap_pdu_parse_header_size(proto, data) <= length);
1038
1039 size_t size = 0;
1040 const uint8_t *token_start = NULL;
1041
1042 if ((proto == COAP_PROTO_TCP || proto == COAP_PROTO_TLS) && length >= 1) {
1043 uint8_t len = *data >> 4;
1044 uint8_t tkl = *data & 0x0f;
1045
1046 if (len < 13) {
1047 size = len;
1048 token_start = &data[2];
1049 } else if (length >= 2) {
1050 if (len==13) {
1051 size = (size_t)data[1] + COAP_MESSAGE_SIZE_OFFSET_TCP8;
1052 token_start = &data[3];
1053 } else if (length >= 3) {
1054 if (len==14) {
1055 size = ((size_t)data[1] << 8) + data[2] + COAP_MESSAGE_SIZE_OFFSET_TCP16;
1056 token_start = &data[4];
1057 } else if (length >= 5) {
1058 size = ((size_t)data[1] << 24) + ((size_t)data[2] << 16)
1059 + ((size_t)data[3] << 8) + data[4] + COAP_MESSAGE_SIZE_OFFSET_TCP32;
1060 token_start = &data[6];
1061 }
1062 }
1063 }
1064 if (token_start) {
1065 /* account for the token length */
1066 if (tkl < COAP_TOKEN_EXT_1B_TKL) {
1067 size += tkl;
1068 } else if (tkl == COAP_TOKEN_EXT_1B_TKL) {
1069 size += token_start[0] + COAP_TOKEN_EXT_1B_BIAS + 1;
1070 } else if (tkl == COAP_TOKEN_EXT_2B_TKL) {
1071 size += ((uint16_t)token_start[0] << 8) + token_start[1] +
1073 } else {
1074 /* Invalid at this point - caught later as undersized */
1075 }
1076 }
1077 }
1078
1079 return size;
1080}
1081#endif /* ! COAP_DISABLE_TCP */
1082
1083int
1085 uint8_t *hdr = pdu->token - pdu->hdr_size;
1086 uint8_t e_token_length;
1087
1088 if (proto == COAP_PROTO_UDP || proto == COAP_PROTO_DTLS) {
1089 assert(pdu->hdr_size == 4);
1090 if ((hdr[0] >> 6) != COAP_DEFAULT_VERSION) {
1091 coap_log_debug("coap_pdu_parse: UDP version not supported\n");
1092 return 0;
1093 }
1094 pdu->type = (hdr[0] >> 4) & 0x03;
1095 pdu->code = hdr[1];
1096 pdu->mid = (uint16_t)hdr[2] << 8 | hdr[3];
1097 } else if (proto == COAP_PROTO_TCP || proto == COAP_PROTO_TLS) {
1098 assert(pdu->hdr_size >= 2 && pdu->hdr_size <= 6);
1099 pdu->type = COAP_MESSAGE_CON;
1100 pdu->code = hdr[pdu->hdr_size-1];
1101 pdu->mid = 0;
1102 } else if (proto == COAP_PROTO_WS || proto == COAP_PROTO_WSS) {
1103 assert(pdu->hdr_size == 2);
1104 pdu->type = COAP_MESSAGE_CON;
1105 pdu->code = hdr[pdu->hdr_size-1];
1106 pdu->mid = 0;
1107 } else {
1108 coap_log_debug("coap_pdu_parse: unsupported protocol\n");
1109 return 0;
1110 }
1111
1112 e_token_length = hdr[0] & 0x0f;
1113 if (e_token_length < COAP_TOKEN_EXT_1B_TKL) {
1114 pdu->e_token_length = e_token_length;
1116 pdu->actual_token.s = &pdu->token[0];
1117 } else if (e_token_length == COAP_TOKEN_EXT_1B_TKL) {
1118 pdu->e_token_length = pdu->token[0] + COAP_TOKEN_EXT_1B_BIAS + 1;
1119 pdu->actual_token.length = pdu->e_token_length - 1;
1120 pdu->actual_token.s = &pdu->token[1];
1121 } else if (e_token_length == COAP_TOKEN_EXT_2B_TKL) {
1122 pdu->e_token_length = ((uint16_t)pdu->token[0] << 8) + pdu->token[1] +
1124 pdu->actual_token.length = pdu->e_token_length - 2;
1125 pdu->actual_token.s = &pdu->token[2];
1126 }
1127 if (pdu->e_token_length > pdu->alloc_size || e_token_length == 15) {
1128 /* Invalid PDU provided - not wise to assert here though */
1129 coap_log_debug("coap_pdu_parse: PDU header token size broken\n");
1130 pdu->e_token_length = 0;
1131 pdu->actual_token.length = 0;
1132 return 0;
1133 }
1134 return 1;
1135}
1136
1137static int
1139 switch ((coap_pdu_signaling_proto_t)pdu->code) {
1140 case COAP_SIGNALING_CSM:
1141 switch (pdu->max_opt) {
1143 if (len > 4)
1144 goto bad;
1145 break;
1147 if (len > 0)
1148 goto bad;
1149 break;
1151 if (len > 3)
1152 goto bad;
1153 break;
1154 default:
1155 if (pdu->max_opt & 0x01)
1156 goto bad; /* Critical */
1157 }
1158 break;
1161 switch (pdu->max_opt) {
1163 if (len > 0)
1164 goto bad;
1165 break;
1166 default:
1167 if (pdu->max_opt & 0x01)
1168 goto bad; /* Critical */
1169 }
1170 break;
1172 switch (pdu->max_opt) {
1174 if (len < 1 || len > 255)
1175 goto bad;
1176 break;
1178 if (len > 3)
1179 goto bad;
1180 break;
1181 default:
1182 if (pdu->max_opt & 0x01)
1183 goto bad; /* Critical */
1184 }
1185 break;
1187 switch (pdu->max_opt) {
1189 if (len > 2)
1190 goto bad;
1191 break;
1192 default:
1193 if (pdu->max_opt & 0x01)
1194 goto bad; /* Critical */
1195 }
1196 break;
1197 default:
1198 ;
1199 }
1200 return 1;
1201bad:
1202 return 0;
1203}
1204
1205static int
1207 int res = 1;
1208
1209 switch (pdu->max_opt) {
1211 if (len > 8)
1212 res = 0;
1213 break;
1215 if (len < 1 || len > 255)
1216 res = 0;
1217 break;
1218 case COAP_OPTION_ETAG:
1219 if (len < 1 || len > 8)
1220 res = 0;
1221 break;
1223 if (len != 0)
1224 res = 0;
1225 break;
1227 if (len > 3)
1228 res = 0;
1229 break;
1231 if (len > 2)
1232 res = 0;
1233 break;
1235 if (len > 255)
1236 res = 0;
1237 break;
1238 case COAP_OPTION_OSCORE:
1239 if (len > 255)
1240 res = 0;
1241 break;
1243 if (len > 255)
1244 res = 0;
1245 break;
1247 if (len > 2)
1248 res = 0;
1249 break;
1250 case COAP_OPTION_MAXAGE:
1251 if (len > 4)
1252 res = 0;
1253 break;
1255 if (len < 1 || len > 255)
1256 res = 0;
1257 break;
1259 if (len != 1)
1260 res = 0;
1261 break;
1262 case COAP_OPTION_ACCEPT:
1263 if (len > 2)
1264 res = 0;
1265 break;
1267 if (len > 3)
1268 res = 0;
1269 break;
1271 if (len > 255)
1272 res = 0;
1273 break;
1274 case COAP_OPTION_EDHOC:
1275 if (len != 0)
1276 res = 0;
1277 break;
1278 case COAP_OPTION_BLOCK2:
1279 if (len > 3)
1280 res = 0;
1281 break;
1282 case COAP_OPTION_BLOCK1:
1283 if (len > 3)
1284 res = 0;
1285 break;
1286 case COAP_OPTION_SIZE2:
1287 if (len > 4)
1288 res = 0;
1289 break;
1291 if (len > 3)
1292 res = 0;
1293 break;
1295 if (len < 1 || len > 1034)
1296 res = 0;
1297 break;
1299 if (len < 1 || len > 255)
1300 res = 0;
1301 break;
1302 case COAP_OPTION_SIZE1:
1303 if (len > 4)
1304 res = 0;
1305 break;
1306 case COAP_OPTION_ECHO:
1307 if (len > 40)
1308 res = 0;
1309 break;
1311 if (len > 1)
1312 res = 0;
1313 break;
1314 case COAP_OPTION_RTAG:
1315 if (len > 8)
1316 res = 0;
1317 break;
1318 default:
1319 ;
1320 }
1321 return res;
1322}
1323
1324static int
1325write_prefix(char **obp, size_t *len, const char *prf, size_t prflen) {
1326 /* Make sure space for null terminating byte */
1327 if (*len < prflen +1) {
1328 return 0;
1329 }
1330
1331 memcpy(*obp, prf, prflen);
1332 *obp += prflen;
1333 *len -= prflen;
1334 return 1;
1335}
1336
1337static int
1338write_char(char **obp, size_t *len, int c, int printable) {
1339 /* Make sure space for null terminating byte */
1340 if (*len < 2 +1) {
1341 return 0;
1342 }
1343
1344 if (!printable) {
1345 const uint8_t hex[] = "0123456789abcdef";
1346 (*obp)[0] = hex[(c & 0xf0) >> 4];
1347 (*obp)[1] = hex[c & 0x0f];
1348 } else {
1349 (*obp)[0] = isprint(c) ? c : '.';
1350 (*obp)[1] = ' ';
1351 }
1352 *obp += 2;
1353 *len -= 2;
1354 return 1;
1355}
1356
1357int
1359 int good = 1;
1360
1361 coap_option_filter_clear(error_opts);
1362
1363 /* sanity checks */
1364 if (pdu->code == 0) {
1365 if (pdu->used_size != 0 || pdu->e_token_length) {
1366 coap_log_debug("coap_pdu_parse: empty message is not empty\n");
1367 return 0;
1368 }
1369 }
1370
1371 if (pdu->e_token_length > pdu->used_size) {
1372 coap_log_debug("coap_pdu_parse: invalid Token\n");
1373 return 0;
1374 }
1375
1376 pdu->max_opt = 0;
1377 if (pdu->code == 0) {
1378 /* empty packet */
1379 pdu->used_size = 0;
1380 pdu->data = NULL;
1381 } else {
1382 /* skip header + token */
1383 coap_opt_t *opt = pdu->token + pdu->e_token_length;
1384 size_t length = pdu->used_size - pdu->e_token_length;
1385
1386 while (length > 0 && *opt != COAP_PAYLOAD_START) {
1387#if (COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_WARN)
1388 coap_opt_t *opt_last = opt;
1389#endif
1390 size_t optsize = next_option_safe(&opt, &length, &pdu->max_opt);
1391 const uint32_t len =
1392 optsize ? coap_opt_length((const uint8_t *)opt - optsize) : 0;
1393 if (optsize == 0) {
1394 coap_log_debug("coap_pdu_parse: %d.%02d: offset %u malformed option\n",
1395 pdu->code >> 5, pdu->code & 0x1F,
1396 (int)(opt_last - pdu->token - pdu->e_token_length));
1397 coap_option_filter_set(error_opts, pdu->max_opt);
1398 good = 0;
1399 break;
1400 }
1401 if (COAP_PDU_IS_SIGNALING(pdu) ?
1402 !coap_pdu_parse_opt_csm(pdu, len) :
1403 !coap_pdu_parse_opt_base(pdu, len)) {
1404 coap_log_warn("coap_pdu_parse: %d.%02d: offset %u option %u has bad length %" PRIu32 "\n",
1405 pdu->code >> 5, pdu->code & 0x1F,
1406 (int)(opt_last - pdu->token - pdu->e_token_length), pdu->max_opt,
1407 len);
1408 coap_option_filter_set(error_opts, pdu->max_opt);
1409 good = 0;
1410 }
1411 }
1412
1413 if (!good) {
1414 /*
1415 * Dump the options in the PDU for analysis, space separated except
1416 * error options which are prefixed by *
1417 * Two rows - hex and ascii (if printable)
1418 */
1419 static char outbuf[COAP_DEBUG_BUF_SIZE];
1420 char *obp;
1421 size_t tlen;
1422 size_t outbuflen;
1423 int i;
1424 int ok;
1425
1426 for (i = 0; i < 2; i++) {
1427 opt = pdu->token + pdu->e_token_length;
1428 length = pdu->used_size - pdu->e_token_length;
1429 pdu->max_opt = 0;
1430
1431 outbuflen = sizeof(outbuf);
1432 obp = outbuf;
1433 ok = write_prefix(&obp, &outbuflen, "O: ", 3);
1434 /*
1435 * Not safe to check for 'ok' here as a lot of variables may get
1436 * partially changed due to lack of outbuflen */
1437 while (length > 0 && *opt != COAP_PAYLOAD_START) {
1438 coap_opt_t *opt_last = opt;
1439 size_t optsize = next_option_safe(&opt, &length, &pdu->max_opt);
1440 const uint32_t len =
1441 optsize ? coap_opt_length((const uint8_t *)opt - optsize) : 0;
1442 if (!optsize || (COAP_PDU_IS_SIGNALING(pdu) ?
1443 !coap_pdu_parse_opt_csm(pdu, len) :
1444 !coap_pdu_parse_opt_base(pdu, len))) {
1445 ok = ok && write_prefix(&obp, &outbuflen, "*", 1);
1446 if (!optsize) {
1447 /* Skip to end of options to output all data */
1448 opt = pdu->token + pdu->used_size;
1449 length = 0;
1450 }
1451 } else {
1452 ok = ok && write_prefix(&obp, &outbuflen, " ", 1);
1453 }
1454 tlen = opt - opt_last;
1455 while (tlen--) {
1456 ok = ok && write_char(&obp, &outbuflen, *opt_last, i);
1457 opt_last++;
1458 }
1459 }
1460 if (length && *opt == COAP_PAYLOAD_START) {
1461 write_char(&obp, &outbuflen, *opt, i);
1462 }
1463 /* write_*() always leaves a spare byte to null terminate */
1464 *obp = '\000';
1465 coap_log_debug("%s\n", outbuf);
1466 }
1467 }
1468
1469 if (length > 0) {
1470 assert(*opt == COAP_PAYLOAD_START);
1471 opt++;
1472 length--;
1473
1474 if (length == 0) {
1475 coap_log_debug("coap_pdu_parse: message ending in payload start marker\n");
1476 return 0;
1477 }
1478 }
1479 if (length > 0)
1480 pdu->data = (uint8_t *)opt;
1481 else
1482 pdu->data = NULL;
1483 }
1484
1485 return good;
1486}
1487
1488int
1490 const uint8_t *data,
1491 size_t length,
1492 coap_pdu_t *pdu) {
1493 coap_opt_filter_t error_opts;
1494
1495 return coap_pdu_parse2(proto, data, length, pdu, &error_opts);
1496}
1497
1498int
1500 const uint8_t *data,
1501 size_t length,
1502 coap_pdu_t *pdu,
1503 coap_opt_filter_t *error_opts) {
1504 size_t hdr_size;
1505
1506 if (length == 0)
1507 return 0;
1508 hdr_size = coap_pdu_parse_header_size(proto, data);
1509 if (!hdr_size || hdr_size > length)
1510 return 0;
1511 if (hdr_size > pdu->max_hdr_size)
1512 return 0;
1513 if (!coap_pdu_resize(pdu, length - hdr_size))
1514 return 0;
1515 if (pdu->token - hdr_size != data)
1516 memcpy(pdu->token - hdr_size, data, length);
1517 pdu->hdr_size = (uint8_t)hdr_size;
1518 pdu->used_size = length - hdr_size;
1519 return coap_pdu_parse_header(pdu, proto) && coap_pdu_parse_opt(pdu, error_opts);
1520}
1521
1522size_t
1524 uint8_t e_token_length;
1525
1527 e_token_length = (uint8_t)pdu->actual_token.length;
1528 } else if (pdu->actual_token.length < COAP_TOKEN_EXT_2B_BIAS) {
1529 e_token_length = COAP_TOKEN_EXT_1B_TKL;
1530 } else if (pdu->actual_token.length <= COAP_TOKEN_EXT_MAX) {
1531 e_token_length = COAP_TOKEN_EXT_2B_TKL;
1532 } else {
1533 coap_log_warn("coap_add_token: Token size too large. PDU ignored\n");
1534 return 0;
1535 }
1536 if (COAP_PROTO_NOT_RELIABLE(proto)) {
1537 assert(pdu->max_hdr_size >= 4);
1538 if (pdu->max_hdr_size < 4) {
1539 coap_log_warn("coap_pdu_encode_header: not enough space for UDP-style header\n");
1540 return 0;
1541 }
1542 pdu->token[-4] = COAP_DEFAULT_VERSION << 6
1543 | pdu->type << 4
1544 | e_token_length;
1545 pdu->token[-3] = pdu->code;
1546 pdu->token[-2] = (uint8_t)(pdu->mid >> 8);
1547 pdu->token[-1] = (uint8_t)(pdu->mid);
1548 pdu->hdr_size = 4;
1549#if !COAP_DISABLE_TCP
1550 } else if (COAP_PROTO_RELIABLE(proto)) {
1551 size_t len;
1552 assert(pdu->used_size >= pdu->e_token_length);
1553 if (pdu->used_size < pdu->e_token_length) {
1554 coap_log_warn("coap_pdu_encode_header: corrupted PDU\n");
1555 return 0;
1556 }
1557
1558 /* A lot of the reliable code assumes type is CON */
1559 if (pdu->type != COAP_MESSAGE_CON)
1560 pdu->type = COAP_MESSAGE_CON;
1561
1562 if (proto == COAP_PROTO_WS || proto == COAP_PROTO_WSS)
1563 len = 0;
1564 else
1565 len = pdu->used_size - pdu->e_token_length;
1566 if (len <= COAP_MAX_MESSAGE_SIZE_TCP0) {
1567 assert(pdu->max_hdr_size >= 2);
1568 if (pdu->max_hdr_size < 2) {
1569 coap_log_warn("coap_pdu_encode_header: not enough space for TCP0 header\n");
1570 return 0;
1571 }
1572 pdu->token[-2] = (uint8_t)len << 4
1573 | e_token_length;
1574 pdu->token[-1] = pdu->code;
1575 pdu->hdr_size = 2;
1576 } else if (len <= COAP_MAX_MESSAGE_SIZE_TCP8) {
1577 assert(pdu->max_hdr_size >= 3);
1578 if (pdu->max_hdr_size < 3) {
1579 coap_log_warn("coap_pdu_encode_header: not enough space for TCP8 header\n");
1580 return 0;
1581 }
1582 pdu->token[-3] = 13 << 4 | e_token_length;
1583 pdu->token[-2] = (uint8_t)(len - COAP_MESSAGE_SIZE_OFFSET_TCP8);
1584 pdu->token[-1] = pdu->code;
1585 pdu->hdr_size = 3;
1586 } else if (len <= COAP_MAX_MESSAGE_SIZE_TCP16) {
1587 assert(pdu->max_hdr_size >= 4);
1588 if (pdu->max_hdr_size < 4) {
1589 coap_log_warn("coap_pdu_encode_header: not enough space for TCP16 header\n");
1590 return 0;
1591 }
1592 pdu->token[-4] = 14 << 4 | e_token_length;
1593 pdu->token[-3] = (uint8_t)((len - COAP_MESSAGE_SIZE_OFFSET_TCP16) >> 8);
1594 pdu->token[-2] = (uint8_t)(len - COAP_MESSAGE_SIZE_OFFSET_TCP16);
1595 pdu->token[-1] = pdu->code;
1596 pdu->hdr_size = 4;
1597 } else {
1598 assert(pdu->max_hdr_size >= 6);
1599 if (pdu->max_hdr_size < 6) {
1600 coap_log_warn("coap_pdu_encode_header: not enough space for TCP32 header\n");
1601 return 0;
1602 }
1603 pdu->token[-6] = 15 << 4 | e_token_length;
1604 pdu->token[-5] = (uint8_t)((len - COAP_MESSAGE_SIZE_OFFSET_TCP32) >> 24);
1605 pdu->token[-4] = (uint8_t)((len - COAP_MESSAGE_SIZE_OFFSET_TCP32) >> 16);
1606 pdu->token[-3] = (uint8_t)((len - COAP_MESSAGE_SIZE_OFFSET_TCP32) >> 8);
1607 pdu->token[-2] = (uint8_t)(len - COAP_MESSAGE_SIZE_OFFSET_TCP32);
1608 pdu->token[-1] = pdu->code;
1609 pdu->hdr_size = 6;
1610 }
1611#endif /* ! COAP_DISABLE_TCP */
1612 } else {
1613 coap_log_warn("coap_pdu_encode_header: unsupported protocol\n");
1614 }
1615 return pdu->hdr_size;
1616}
1617
1620 return pdu->code;
1621}
1622
1623void
1625#ifndef RIOT_VERSION
1626 assert(code <= 0xff);
1627#endif /* RIOT_VERSION */
1628 pdu->code = code;
1629}
1630
1633 return pdu->type;
1634}
1635
1636void
1638 assert(type <= 0x3);
1639 pdu->type = type;
1640}
1641
1644 return pdu->actual_token;
1645}
1646
1649 return pdu->mid;
1650}
1651
1652void
1654#if (UINT_MAX > 65535)
1655 assert(mid >= 0 && mid <= 0xffff);
1656#endif /* UINT_MAX > 65535 */
1657 pdu->mid = mid;
1658}
1659
1660coap_pdu_t *
1662 if (pdu != NULL) {
1663 ++pdu->ref;
1664 }
1665 return pdu;
1666}
1667
1668coap_pdu_t *
1670 coap_pdu_t *pdu_rw = NULL;
1671
1672 if (pdu != NULL) {
1673
1674 /* Need to do this to not get a compiler warning about const parameters */
1675 memcpy(&pdu_rw, &pdu, sizeof(pdu_rw));
1676 ++pdu_rw->ref;
1677 }
1678 return pdu_rw;
1679}
#define PRIuS
#define PRIu32
Library specific build wrapper for coap_internal.h.
#define COAP_API
@ COAP_PDU
Definition coap_mem.h:41
@ COAP_PDU_BUF
Definition coap_mem.h:42
void * coap_realloc_type(coap_memory_tag_t type, void *p, size_t size)
Reallocates a chunk p of bytes created by coap_malloc_type() or coap_realloc_type() and returns a poi...
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().
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:24
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:30
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 coap_pdu.c:976
static int coap_pdu_parse_opt_csm(coap_pdu_t *pdu, uint16_t len)
Definition coap_pdu.c:1138
error_desc_t coap_error[]
Definition coap_pdu.c:928
static int write_prefix(char **obp, size_t *len, const char *prf, size_t prflen)
Definition coap_pdu.c:1325
static int coap_pdu_parse_opt_base(coap_pdu_t *pdu, uint16_t len)
Definition coap_pdu.c:1206
#define min(a, b)
Definition coap_pdu.c:37
static int write_char(char **obp, size_t *len, int c, int printable)
Definition coap_pdu.c:1338
#define max(a, b)
Definition coap_pdu.c:41
uint16_t coap_new_message_id_lkd(coap_session_t *session)
Returns a new message id and updates session->tx_mid accordingly.
#define coap_lock_unlock()
Dummy for no thread-safe code.
#define coap_lock_check_locked()
Dummy for no thread-safe code.
#define coap_lock_lock(failed)
Dummy for no thread-safe code.
#define coap_log_debug(...)
Definition coap_debug.h:126
#define coap_log_info(...)
Definition coap_debug.h:114
#define coap_log_warn(...)
Definition coap_debug.h:108
#define coap_log_crit(...)
Definition coap_debug.h:96
coap_opt_t * coap_option_next(coap_opt_iterator_t *oi)
Updates the iterator oi to point to the next option.
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.
uint32_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
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.
size_t coap_opt_encode_size(uint16_t delta, size_t length)
Compute storage bytes needed for an option with given delta and length.
#define COAP_OPT_ALL
Pre-defined filter that includes all options.
void coap_option_filter_clear(coap_opt_filter_t *filter)
Clears filter filter.
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.
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
int coap_option_filter_get(coap_opt_filter_t *filter, coap_option_num_t option)
Checks if number is contained in filter.
int coap_option_filter_set(coap_opt_filter_t *filter, coap_option_num_t option)
Sets the corresponding entry for number in filter.
#define COAP_MESSAGE_SIZE_OFFSET_TCP8
coap_pdu_t * coap_pdu_reference_lkd(coap_pdu_t *pdu)
Increment reference counter on a pdu to stop it prematurely getting freed off when coap_delete_pdu() ...
Definition coap_pdu.c:1661
void coap_delete_pdu_lkd(coap_pdu_t *pdu)
Dispose of an CoAP PDU and free off associated storage.
Definition coap_pdu.c:194
#define COAP_TOKEN_EXT_2B_TKL
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 coap_pdu.c:639
int coap_remove_option(coap_pdu_t *pdu, coap_option_num_t number)
Removes (first) option of given number from the pdu.
Definition coap_pdu.c:499
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 coap_pdu.c:423
#define COAP_TOKEN_EXT_1B_BIAS
#define COAP_PDU_MAX_UDP_HEADER_SIZE
int coap_pdu_parse_opt(coap_pdu_t *pdu, coap_opt_filter_t *error_opts)
Verify consistency in the given CoAP PDU structure and locate the data.
Definition coap_pdu.c:1358
int coap_pdu_parse_header(coap_pdu_t *pdu, coap_proto_t proto)
Decode the protocol specific header for the specified PDU.
Definition coap_pdu.c:1084
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 coap_pdu.c:1000
coap_pdu_t * coap_new_pdu_lkd(coap_pdu_type_t type, coap_pdu_code_t code, coap_session_t *session)
Creates a new CoAP PDU.
Definition coap_pdu.c:173
#define COAP_PDU_MAX_TCP_HEADER_SIZE
#define COAP_MAX_MESSAGE_SIZE_TCP8
#define COAP_PDU_IS_SIGNALING(pdu)
#define COAP_TOKEN_EXT_2B_BIAS
#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 coap_pdu.c:593
#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 coap_pdu.c:45
#define COAP_MESSAGE_SIZE_OFFSET_TCP32
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 coap_pdu.c:733
#define COAP_TOKEN_EXT_1B_TKL
size_t coap_pdu_encode_header(coap_pdu_t *pdu, coap_proto_t proto)
Compose the protocol specific header for the specified PDU.
Definition coap_pdu.c:1523
coap_pdu_t * coap_pdu_duplicate_lkd(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 coap_pdu.c:234
#define COAP_DEFAULT_VERSION
int coap_pdu_parse2(coap_proto_t proto, const uint8_t *data, size_t length, coap_pdu_t *pdu, coap_opt_filter_t *error_opts)
Parses data into the CoAP PDU structure given in result.
Definition coap_pdu.c:1499
#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 coap_pdu.c:349
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 coap_pdu.c:1031
coap_pdu_t * coap_const_pdu_reference_lkd(const coap_pdu_t *pdu)
Increment reference counter on a const pdu to stop it prematurely getting freed off when coap_delete_...
Definition coap_pdu.c:1669
int coap_pdu_resize(coap_pdu_t *pdu, size_t new_size)
Dynamically grows the size of pdu to new_size.
Definition coap_pdu.c:307
#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 coap_pdu.c:789
#define COAP_OPTION_HOP_LIMIT
Definition coap_pdu.h:137
#define COAP_OPTION_NORESPONSE
Definition coap_pdu.h:150
#define COAP_OPTION_URI_HOST
Definition coap_pdu.h:124
#define COAP_OPTION_IF_MATCH
Definition coap_pdu.h:123
coap_pdu_code_t coap_pdu_get_code(const coap_pdu_t *pdu)
Gets the PDU code associated with pdu.
Definition coap_pdu.c:1619
#define COAP_OPTION_BLOCK2
Definition coap_pdu.h:142
const char * coap_response_phrase(unsigned char code)
Returns a human-readable response phrase for the specified CoAP response code.
Definition coap_pdu.c:960
#define COAP_OPTION_CONTENT_FORMAT
Definition coap_pdu.h:132
#define COAP_SIGNALING_OPTION_ALTERNATIVE_ADDRESS
Definition coap_pdu.h:210
#define COAP_OPTION_SIZE2
Definition coap_pdu.h:144
#define COAP_OPTION_BLOCK1
Definition coap_pdu.h:143
#define COAP_OPTION_Q_BLOCK1
Definition coap_pdu.h:139
#define COAP_OPTION_PROXY_SCHEME
Definition coap_pdu.h:147
COAP_API void coap_delete_pdu(coap_pdu_t *pdu)
Dispose of an CoAP PDU and free off associated storage.
Definition coap_pdu.c:187
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 coap_pdu.c:866
#define COAP_OPTION_URI_QUERY
Definition coap_pdu.h:136
void coap_pdu_set_code(coap_pdu_t *pdu, coap_pdu_code_t code)
Sets the PDU code in the pdu.
Definition coap_pdu.c:1624
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition coap_pdu.h:268
COAP_API 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 coap_pdu.c:212
#define COAP_OPTION_IF_NONE_MATCH
Definition coap_pdu.h:126
#define COAP_OPTION_LOCATION_PATH
Definition coap_pdu.h:129
#define COAP_TOKEN_EXT_MAX
Definition coap_pdu.h:64
#define COAP_OPTION_URI_PATH
Definition coap_pdu.h:131
#define COAP_SIGNALING_OPTION_EXTENDED_TOKEN_LENGTH
Definition coap_pdu.h:204
#define COAP_RESPONSE_CODE(N)
Definition coap_pdu.h:165
coap_proto_t
CoAP protocol types Note: coap_layers_coap[] needs updating if extended.
Definition coap_pdu.h:318
coap_pdu_code_t
Set of codes available for a PDU.
Definition coap_pdu.h:332
#define COAP_OPTION_OSCORE
Definition coap_pdu.h:130
#define COAP_OPTION_SIZE1
Definition coap_pdu.h:148
coap_pdu_type_t
CoAP PDU message type definitions.
Definition coap_pdu.h:72
#define COAP_SIGNALING_OPTION_BLOCK_WISE_TRANSFER
Definition coap_pdu.h:203
int coap_add_token(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Adds token of length len to pdu.
Definition coap_pdu.c:366
#define COAP_OPTION_LOCATION_QUERY
Definition coap_pdu.h:140
void coap_pdu_set_type(coap_pdu_t *pdu, coap_pdu_type_t type)
Sets the PDU type in the pdu.
Definition coap_pdu.c:1637
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 coap_pdu.c:779
#define COAP_OPTION_Q_BLOCK2
Definition coap_pdu.h:145
#define COAP_SIGNALING_OPTION_CUSTODY
Definition coap_pdu.h:207
coap_pdu_signaling_proto_t
Definition coap_pdu.h:193
coap_pdu_type_t coap_pdu_get_type(const coap_pdu_t *pdu)
Gets the PDU type associated with pdu.
Definition coap_pdu.c:1632
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 coap_pdu.c:885
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 coap_pdu.c:1489
#define COAP_MAX_OPT
the highest option number we know
Definition coap_pdu.h:156
void coap_pdu_set_mid(coap_pdu_t *pdu, coap_mid_t mid)
Sets the message id in the pdu.
Definition coap_pdu.c:1653
#define COAP_OPTION_RTAG
Definition coap_pdu.h:151
COAP_API 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 coap_pdu.c:162
#define COAP_OPTION_URI_PORT
Definition coap_pdu.h:128
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 coap_pdu.c:102
#define COAP_OPTION_EDHOC
Definition coap_pdu.h:141
#define COAP_OPTION_ACCEPT
Definition coap_pdu.h:138
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 coap_pdu.c:893
#define COAP_DEFAULT_MTU
Definition coap_pdu.h:45
coap_mid_t coap_pdu_get_mid(const coap_pdu_t *pdu)
Gets the message id associated with pdu.
Definition coap_pdu.c:1648
#define COAP_OPTION_MAXAGE
Definition coap_pdu.h:135
#define COAP_OPTION_ETAG
Definition coap_pdu.h:125
#define COAP_OPTION_PROXY_URI
Definition coap_pdu.h:146
#define COAP_OPTION_OBSERVE
Definition coap_pdu.h:127
#define COAP_SIGNALING_OPTION_HOLD_OFF
Definition coap_pdu.h:211
#define COAP_OPTION_ECHO
Definition coap_pdu.h:149
#define COAP_SIGNALING_OPTION_BAD_CSM_OPTION
Definition coap_pdu.h:214
#define COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE
Definition coap_pdu.h:202
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 coap_pdu.c:854
coap_bin_const_t coap_pdu_get_token(const coap_pdu_t *pdu)
Gets the token associated with pdu.
Definition coap_pdu.c:1643
@ COAP_PROTO_WS
Definition coap_pdu.h:324
@ COAP_PROTO_DTLS
Definition coap_pdu.h:321
@ COAP_PROTO_UDP
Definition coap_pdu.h:320
@ COAP_PROTO_TLS
Definition coap_pdu.h:323
@ COAP_PROTO_WSS
Definition coap_pdu.h:325
@ COAP_PROTO_TCP
Definition coap_pdu.h:322
@ COAP_MESSAGE_CON
Definition coap_pdu.h:73
@ COAP_SIGNALING_RELEASE
Definition coap_pdu.h:197
@ COAP_SIGNALING_CSM
Definition coap_pdu.h:194
@ COAP_SIGNALING_PONG
Definition coap_pdu.h:196
@ COAP_SIGNALING_PING
Definition coap_pdu.h:195
@ COAP_SIGNALING_ABORT
Definition coap_pdu.h:198
size_t coap_session_max_pdu_size_lkd(const coap_session_t *session)
Get maximum acceptable PDU size.
#define COAP_PROTO_NOT_RELIABLE(p)
#define COAP_PROTO_RELIABLE(p)
void coap_delete_binary(coap_binary_t *s)
Deletes the given coap_binary_t object and releases any memory allocated.
Definition coap_str.c:105
CoAP binary data definition with const data.
Definition coap_str.h:67
size_t length
length of binary data
Definition coap_str.h:68
const uint8_t * s
read-only binary data
Definition coap_str.h:69
Iterator to run through PDU options.
coap_option_num_t number
decoded option number
Representation of CoAP options.
Definition coap_option.h:36
uint16_t delta
Definition coap_option.h:37
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 (or extended length bytes prefix), 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.
unsigned ref
reference count
coap_pdu_code_t code
request method (value 1–31) or response code (value 64-255)
uint8_t hdr_size
actual size used for protocol-specific header (0 until header is encoded)
coap_bin_const_t actual_token
Actual token in pdu.
uint8_t * data
first byte of payload, if any
coap_mid_t mid
message id, if any, in regular host byte order
uint32_t e_token_length
length of Token space (includes leading extended bytes
size_t used_size
used bytes of storage for token, options and payload
uint8_t crit_opt
Set if unknown critical option for proxy.
coap_binary_t * data_free
Data to be freed off by coap_delete_pdu()
size_t alloc_size
allocated storage for token, options and payload
coap_session_t * session
Session responsible for PDU or NULL.
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...
uint8_t doing_first
Set if doing client's first request.
coap_proto_t proto
protocol used
unsigned char code
Definition coap_pdu.c:922
const char * phrase
Definition coap_pdu.c:923