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