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