libcoap 4.3.5-develop-3a2cdb1
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
1118 uint64_t size = 0;
1119 const uint8_t *token_start = NULL;
1120
1121 if (coap_pdu_parse_header_size(proto, data) > length)
1122 return 0;
1123
1124 if ((proto == COAP_PROTO_TCP || proto == COAP_PROTO_TLS) && length >= 1) {
1125 uint8_t len = *data >> 4;
1126 uint8_t tkl = *data & 0x0f;
1127 size_t extra_for_tkl = tkl == COAP_TOKEN_EXT_2B_TKL ? 2 :
1128 tkl == COAP_TOKEN_EXT_1B_TKL ? 1 : 0;
1129
1130 if (tkl > COAP_TOKEN_EXT_2B_TKL)
1131 return 0;
1132
1133 if (len < 13) {
1134 size = len;
1135 token_start = &data[2];
1136 } else if (length >= (2 + extra_for_tkl)) {
1137 if (len==13) {
1138 size = (size_t)data[1] + COAP_MESSAGE_SIZE_OFFSET_TCP8;
1139 token_start = &data[3];
1140 } else if (length >= (3 + extra_for_tkl)) {
1141 if (len==14) {
1142 size = ((size_t)data[1] << 8) + data[2] + COAP_MESSAGE_SIZE_OFFSET_TCP16;
1143 token_start = &data[4];
1144 } else if (length >= (5 + extra_for_tkl)) {
1145 /* Need to handle 32 bit size_t */
1146 size = ((uint64_t)data[1] << 24) + ((uint64_t)data[2] << 16)
1147 + ((uint64_t)data[3] << 8) + data[4] + COAP_MESSAGE_SIZE_OFFSET_TCP32;
1148 token_start = &data[6];
1149 }
1150 }
1151 }
1152 if (token_start) {
1153 /* account for the token length */
1154 if (tkl < COAP_TOKEN_EXT_1B_TKL) {
1155 size += tkl;
1156 } else if (tkl == COAP_TOKEN_EXT_1B_TKL) {
1157 size += token_start[0] + COAP_TOKEN_EXT_1B_BIAS + 1;
1158 } else if (tkl == COAP_TOKEN_EXT_2B_TKL) {
1159 size += ((uint16_t)token_start[0] << 8) + token_start[1] +
1161 } else {
1162 /* Invalid at this point - caught later as undersized */
1163 }
1164 }
1165 }
1166 /*
1167 * Check that there is not a 32bit size_t overflow.
1168 * The Max-Message-Size Option limits size to be 32 bits.
1169 */
1170 if (size > (size_t)-1)
1171 size = (size_t)-1;
1172
1173 return (size_t)size;
1174}
1175#endif /* ! COAP_DISABLE_TCP */
1176
1177int
1179 uint8_t *hdr = pdu->token - pdu->hdr_size;
1180 uint8_t e_token_length;
1181
1182 if (proto == COAP_PROTO_UDP || proto == COAP_PROTO_DTLS) {
1183 assert(pdu->hdr_size == 4);
1184 if ((hdr[0] >> 6) != COAP_DEFAULT_VERSION) {
1185 coap_log_debug("coap_pdu_parse: UDP version not supported\n");
1186 return 0;
1187 }
1188 pdu->type = (hdr[0] >> 4) & 0x03;
1189 pdu->code = hdr[1];
1190 pdu->mid = (uint16_t)hdr[2] << 8 | hdr[3];
1191 } else if (proto == COAP_PROTO_TCP || proto == COAP_PROTO_TLS) {
1192 assert(pdu->hdr_size >= 2 && pdu->hdr_size <= 6);
1193 pdu->type = COAP_MESSAGE_CON;
1194 pdu->code = hdr[pdu->hdr_size-1];
1195 pdu->mid = 0;
1196 } else if (proto == COAP_PROTO_WS || proto == COAP_PROTO_WSS) {
1197 assert(pdu->hdr_size == 2);
1198 pdu->type = COAP_MESSAGE_CON;
1199 pdu->code = hdr[pdu->hdr_size-1];
1200 pdu->mid = 0;
1201 } else {
1202 coap_log_debug("coap_pdu_parse: unsupported protocol\n");
1203 return 0;
1204 }
1205
1206 e_token_length = hdr[0] & 0x0f;
1207 if (e_token_length < COAP_TOKEN_EXT_1B_TKL) {
1208 pdu->e_token_length = e_token_length;
1210 pdu->actual_token.s = &pdu->token[0];
1211 } else if (e_token_length == COAP_TOKEN_EXT_1B_TKL) {
1212 if (pdu->used_size < 1)
1213 goto bad_ext_token;
1214 pdu->e_token_length = pdu->token[0] + COAP_TOKEN_EXT_1B_BIAS + 1;
1215 pdu->actual_token.length = pdu->e_token_length - 1;
1216 pdu->actual_token.s = &pdu->token[1];
1217 } else if (e_token_length == COAP_TOKEN_EXT_2B_TKL) {
1218 if (pdu->used_size < 2)
1219 goto bad_ext_token;
1220 pdu->e_token_length = ((uint16_t)pdu->token[0] << 8) + pdu->token[1] +
1222 pdu->actual_token.length = pdu->e_token_length - 2;
1223 pdu->actual_token.s = &pdu->token[2];
1224 }
1225 if (pdu->e_token_length > pdu->alloc_size || e_token_length == 15) {
1226 goto bad_ext_token;
1227 }
1228 return 1;
1229
1230bad_ext_token:
1231 /* Invalid PDU provided - not wise to assert here though */
1232 coap_log_warn("coap_pdu_parse: PDU header extended token size broken\n");
1233 pdu->e_token_length = 0;
1234 pdu->actual_token.length = 0;
1235 return 0;
1236}
1237
1238static int
1240 switch ((coap_pdu_signaling_proto_t)code) {
1241 case COAP_SIGNALING_CSM:
1242 switch ((coap_sig_csm_opt_t)option) {
1244 if (len > 4)
1245 goto bad;
1246 break;
1248 if (len > 0)
1249 goto bad;
1250 break;
1252 if (len > 3)
1253 goto bad;
1254 break;
1255 default:
1256 if (option & 0x01)
1257 goto bad; /* Critical */
1258 }
1259 break;
1262 switch ((coap_sig_ping_opt_t)option) {
1264 if (len > 0)
1265 goto bad;
1266 break;
1267 default:
1268 if (option & 0x01)
1269 goto bad; /* Critical */
1270 }
1271 break;
1273 switch ((coap_sig_release_opt_t)option) {
1275 if (len < 1 || len > 255)
1276 goto bad;
1277 break;
1279 if (len > 3)
1280 goto bad;
1281 break;
1282 default:
1283 if (option & 0x01)
1284 goto bad; /* Critical */
1285 }
1286 break;
1288 switch ((coap_sig_abort_opt_t)option) {
1290 if (len > 2)
1291 goto bad;
1292 break;
1293 default:
1294 if (option & 0x01)
1295 goto bad; /* Critical */
1296 }
1297 break;
1298 default:
1299 goto bad;
1300 }
1301 return 1;
1302bad:
1303 return 0;
1304}
1305
1306int
1307coap_pdu_parse_opt_base(coap_option_num_t number, size_t len, const uint8_t *opt_val) {
1308 int res = 1;
1309
1310 switch ((coap_code_opt_num_t)number) {
1312 if (len > 8)
1313 res = 0;
1314 break;
1316 if (len < 1 || len > 255)
1317 res = 0;
1318 break;
1319 case COAP_OPTION_ETAG:
1320 if (len < 1 || len > 8)
1321 res = 0;
1322 break;
1324 if (len != 0)
1325 res = 0;
1326 break;
1328 if (len > 3)
1329 res = 0;
1330 break;
1332 if (len > 2)
1333 res = 0;
1334 break;
1337 if (len > 255)
1338 res = 0;
1339 if (coap_check_dots(opt_val, len)) {
1340 res = 0;
1341 }
1342 break;
1343 case COAP_OPTION_OSCORE:
1344 if (len > 255)
1345 res = 0;
1346 break;
1348 if (len > 2)
1349 res = 0;
1350 break;
1352 if (len > 4)
1353 res = 0;
1354 break;
1355 case COAP_OPTION_MAXAGE:
1356 if (len > 4)
1357 res = 0;
1358 break;
1360 if (len > 255)
1361 res = 0;
1362 break;
1364 if (len != 1)
1365 res = 0;
1366 break;
1367 case COAP_OPTION_ACCEPT:
1368 if (len > 2)
1369 res = 0;
1370 break;
1372 if (len > 3)
1373 res = 0;
1374 break;
1376 if (len > 255)
1377 res = 0;
1378 break;
1379 case COAP_OPTION_EDHOC:
1380 if (len != 0)
1381 res = 0;
1382 break;
1383 case COAP_OPTION_BLOCK2:
1384 if (len > 3)
1385 res = 0;
1386 break;
1387 case COAP_OPTION_BLOCK1:
1388 if (len > 3)
1389 res = 0;
1390 break;
1391 case COAP_OPTION_SIZE2:
1392 if (len > 4)
1393 res = 0;
1394 break;
1396 if (len > 3)
1397 res = 0;
1398 break;
1400 if (len < 1 || len > 1034)
1401 res = 0;
1402 break;
1404 if (len < 1 || len > 255)
1405 res = 0;
1406 break;
1407 case COAP_OPTION_SIZE1:
1408 if (len > 4)
1409 res = 0;
1410 break;
1411 case COAP_OPTION_ECHO:
1412 if (len > 40)
1413 res = 0;
1414 break;
1416 if (len > 1)
1417 res = 0;
1418 break;
1419 case COAP_OPTION_RTAG:
1420 if (len > 8)
1421 res = 0;
1422 break;
1423 default:
1424 ;
1425 }
1426 return res;
1427}
1428
1429static int
1430write_prefix(char **obp, size_t *len, const char *prf, size_t prflen) {
1431 /* Make sure space for null terminating byte */
1432 if (*len < prflen +1) {
1433 return 0;
1434 }
1435
1436 memcpy(*obp, prf, prflen);
1437 *obp += prflen;
1438 *len -= prflen;
1439 return 1;
1440}
1441
1442static int
1443write_char(char **obp, size_t *len, int c, int printable) {
1444 /* Make sure space for null terminating byte */
1445 if (*len < 2 +1) {
1446 return 0;
1447 }
1448
1449 if (!printable) {
1450 const uint8_t hex[] = "0123456789abcdef";
1451 (*obp)[0] = hex[(c & 0xf0) >> 4];
1452 (*obp)[1] = hex[c & 0x0f];
1453 } else {
1454 (*obp)[0] = isprint(c) ? c : '.';
1455 (*obp)[1] = ' ';
1456 }
1457 *obp += 2;
1458 *len -= 2;
1459 return 1;
1460}
1461
1462int
1464 int good = 1;
1465
1466 coap_option_filter_clear(error_opts);
1467
1468 /* sanity checks */
1469 if (pdu->code == 0) {
1470 if (pdu->used_size != 0 || pdu->e_token_length) {
1471 coap_log_debug("coap_pdu_parse: empty message is not empty\n");
1472 return 0;
1473 }
1474 }
1475
1476 if (pdu->e_token_length > pdu->used_size) {
1477 coap_log_debug("coap_pdu_parse: invalid Token\n");
1478 return 0;
1479 }
1480
1481 pdu->max_opt = 0;
1482 if (pdu->code == 0) {
1483 /* empty packet */
1484 pdu->used_size = 0;
1485 pdu->data = NULL;
1486 } else {
1487 /* skip header + token */
1488 coap_opt_t *opt = pdu->token + pdu->e_token_length;
1489 size_t length = pdu->used_size - pdu->e_token_length;
1490
1491 while (length > 0 && *opt != COAP_PAYLOAD_START) {
1492#if (COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_WARN)
1493 coap_opt_t *opt_last = opt;
1494#endif
1495 size_t optsize = next_option_safe(&opt, &length, &pdu->max_opt);
1496 uint32_t len;
1497 const uint8_t *opt_val;
1498
1499 if (optsize == 0) {
1500 coap_log_debug("coap_pdu_parse: %d.%02d: offset %u malformed option\n",
1501 pdu->code >> 5, pdu->code & 0x1F,
1502 (int)(opt_last - pdu->token - pdu->e_token_length));
1503 coap_option_filter_set(error_opts, pdu->max_opt);
1504 good = 0;
1505 break;
1506 }
1507 len = coap_opt_length((const uint8_t *)opt - optsize);
1508 opt_val = coap_opt_value((const uint8_t *)opt - optsize);
1509 if (COAP_PDU_IS_SIGNALING(pdu) ?
1510 !coap_pdu_parse_opt_csm(pdu->code, pdu->max_opt, len) :
1511 !coap_pdu_parse_opt_base(pdu->max_opt, len, opt_val)) {
1512 coap_log_warn("coap_pdu_parse: %d.%02d: offset %u option %u has bad length %" PRIu32 " or value\n",
1513 pdu->code >> 5, pdu->code & 0x1F,
1514 (int)(opt_last - pdu->token - pdu->e_token_length), pdu->max_opt,
1515 len);
1516 coap_option_filter_set(error_opts, pdu->max_opt);
1517 good = 0;
1518 }
1519 }
1520
1521 if (!good) {
1522 /*
1523 * Dump the options in the PDU for analysis, space separated except
1524 * error options which are prefixed by *
1525 * Two rows - hex and ascii (if printable)
1526 */
1527 static char outbuf[COAP_DEBUG_BUF_SIZE];
1528 char *obp;
1529 size_t tlen;
1530 size_t outbuflen;
1531 int i;
1532 int ok;
1533
1534 for (i = 0; i < 2; i++) {
1535 opt = pdu->token + pdu->e_token_length;
1536 length = pdu->used_size - pdu->e_token_length;
1537 pdu->max_opt = 0;
1538
1539 outbuflen = sizeof(outbuf);
1540 obp = outbuf;
1541 ok = write_prefix(&obp, &outbuflen, "O: ", 3);
1542 /*
1543 * Not safe to check for 'ok' here as a lot of variables may get
1544 * partially changed due to lack of outbuflen */
1545 while (length > 0 && *opt != COAP_PAYLOAD_START) {
1546 coap_opt_t *opt_last = opt;
1547 size_t optsize = next_option_safe(&opt, &length, &pdu->max_opt);
1548 const uint32_t len =
1549 optsize ? coap_opt_length((const uint8_t *)opt - optsize) : 0;
1550 const uint8_t *opt_val =
1551 optsize ? coap_opt_value((const uint8_t *)opt - optsize) : 0;
1552
1553 if (!optsize || (COAP_PDU_IS_SIGNALING(pdu) ?
1554 !coap_pdu_parse_opt_csm(pdu->code, pdu->max_opt, len) :
1555 !coap_pdu_parse_opt_base(pdu->max_opt, len, opt_val))) {
1556 ok = ok && write_prefix(&obp, &outbuflen, "*", 1);
1557 if (!optsize) {
1558 /* Skip to end of options to output all data */
1559 opt = pdu->token + pdu->used_size;
1560 length = 0;
1561 }
1562 } else {
1563 ok = ok && write_prefix(&obp, &outbuflen, " ", 1);
1564 }
1565 tlen = opt - opt_last;
1566 while (tlen) {
1567 tlen--;
1568 ok = ok && write_char(&obp, &outbuflen, *opt_last, i);
1569 opt_last++;
1570 }
1571 }
1572 if (length && *opt == COAP_PAYLOAD_START) {
1573 write_char(&obp, &outbuflen, *opt, i);
1574 }
1575 /* write_*() always leaves a spare byte to null terminate */
1576 *obp = '\000';
1577 coap_log_debug("%s\n", outbuf);
1578 }
1579 }
1580
1581 if (length > 0) {
1582 assert(*opt == COAP_PAYLOAD_START);
1583 opt++;
1584 length--;
1585
1586 if (length == 0) {
1587 coap_log_debug("coap_pdu_parse: message ending in payload start marker\n");
1588 return 0;
1589 }
1590 }
1591 if (length > 0)
1592 pdu->data = (uint8_t *)opt;
1593 else
1594 pdu->data = NULL;
1595 }
1596
1597 return good;
1598}
1599
1600int
1602 const uint8_t *data,
1603 size_t length,
1604 coap_pdu_t *pdu) {
1605 coap_opt_filter_t error_opts;
1606
1607 return coap_pdu_parse2(proto, data, length, pdu, &error_opts);
1608}
1609
1610int
1612 const uint8_t *data,
1613 size_t length,
1614 coap_pdu_t *pdu,
1615 coap_opt_filter_t *error_opts) {
1616 size_t hdr_size;
1617
1618 if (length == 0)
1619 return 0;
1620 hdr_size = coap_pdu_parse_header_size(proto, data);
1621 if (!hdr_size || hdr_size > length)
1622 return 0;
1623 if (hdr_size > pdu->max_hdr_size)
1624 return 0;
1625 if (!coap_pdu_resize(pdu, length - hdr_size))
1626 return 0;
1627 if (pdu->token - hdr_size != data)
1628 memcpy(pdu->token - hdr_size, data, length);
1629 pdu->hdr_size = (uint8_t)hdr_size;
1630 pdu->used_size = length - hdr_size;
1631 return coap_pdu_parse_header(pdu, proto) && coap_pdu_parse_opt(pdu, error_opts);
1632}
1633
1634size_t
1636 uint8_t e_token_length;
1637
1639 e_token_length = (uint8_t)pdu->actual_token.length;
1640 } else if (pdu->actual_token.length < COAP_TOKEN_EXT_2B_BIAS) {
1641 e_token_length = COAP_TOKEN_EXT_1B_TKL;
1642 } else if (pdu->actual_token.length <= COAP_TOKEN_EXT_MAX) {
1643 e_token_length = COAP_TOKEN_EXT_2B_TKL;
1644 } else {
1645 coap_log_warn("coap_pdu_encode_header: Token size too large. PDU ignored\n");
1646 return 0;
1647 }
1648 if (COAP_PROTO_NOT_RELIABLE(proto)) {
1649 assert(pdu->max_hdr_size >= 4);
1650 if (pdu->max_hdr_size < 4) {
1651 coap_log_warn("coap_pdu_encode_header: not enough space for UDP-style header\n");
1652 return 0;
1653 }
1654 pdu->token[-4] = COAP_DEFAULT_VERSION << 6
1655 | pdu->type << 4
1656 | e_token_length;
1657 pdu->token[-3] = pdu->code;
1658 pdu->token[-2] = (uint8_t)(pdu->mid >> 8);
1659 pdu->token[-1] = (uint8_t)(pdu->mid);
1660 pdu->hdr_size = 4;
1661#if !COAP_DISABLE_TCP
1662 } else if (COAP_PROTO_RELIABLE(proto)) {
1663 size_t len;
1664 assert(pdu->used_size >= pdu->e_token_length);
1665 if (pdu->used_size < pdu->e_token_length) {
1666 coap_log_warn("coap_pdu_encode_header: corrupted PDU\n");
1667 return 0;
1668 }
1669
1670 /* A lot of the reliable code assumes type is CON */
1671 if (pdu->type != COAP_MESSAGE_CON)
1672 pdu->type = COAP_MESSAGE_CON;
1673
1674 if (proto == COAP_PROTO_WS || proto == COAP_PROTO_WSS)
1675 len = 0;
1676 else
1677 len = pdu->used_size - pdu->e_token_length;
1678 if (len <= COAP_MAX_MESSAGE_SIZE_TCP0) {
1679 assert(pdu->max_hdr_size >= 2);
1680 if (pdu->max_hdr_size < 2) {
1681 coap_log_warn("coap_pdu_encode_header: not enough space for TCP0 header\n");
1682 return 0;
1683 }
1684 pdu->token[-2] = (uint8_t)len << 4
1685 | e_token_length;
1686 pdu->token[-1] = pdu->code;
1687 pdu->hdr_size = 2;
1688 } else if (len <= COAP_MAX_MESSAGE_SIZE_TCP8) {
1689 assert(pdu->max_hdr_size >= 3);
1690 if (pdu->max_hdr_size < 3) {
1691 coap_log_warn("coap_pdu_encode_header: not enough space for TCP8 header\n");
1692 return 0;
1693 }
1694 pdu->token[-3] = 13 << 4 | e_token_length;
1695 pdu->token[-2] = (uint8_t)(len - COAP_MESSAGE_SIZE_OFFSET_TCP8);
1696 pdu->token[-1] = pdu->code;
1697 pdu->hdr_size = 3;
1698 } else if (len <= COAP_MAX_MESSAGE_SIZE_TCP16) {
1699 assert(pdu->max_hdr_size >= 4);
1700 if (pdu->max_hdr_size < 4) {
1701 coap_log_warn("coap_pdu_encode_header: not enough space for TCP16 header\n");
1702 return 0;
1703 }
1704 pdu->token[-4] = 14 << 4 | e_token_length;
1705 pdu->token[-3] = (uint8_t)((len - COAP_MESSAGE_SIZE_OFFSET_TCP16) >> 8);
1706 pdu->token[-2] = (uint8_t)(len - COAP_MESSAGE_SIZE_OFFSET_TCP16);
1707 pdu->token[-1] = pdu->code;
1708 pdu->hdr_size = 4;
1709 } else {
1710 assert(pdu->max_hdr_size >= 6);
1711 if (pdu->max_hdr_size < 6) {
1712 coap_log_warn("coap_pdu_encode_header: not enough space for TCP32 header\n");
1713 return 0;
1714 }
1715 pdu->token[-6] = 15 << 4 | e_token_length;
1716 pdu->token[-5] = (uint8_t)((len - COAP_MESSAGE_SIZE_OFFSET_TCP32) >> 24);
1717 pdu->token[-4] = (uint8_t)((len - COAP_MESSAGE_SIZE_OFFSET_TCP32) >> 16);
1718 pdu->token[-3] = (uint8_t)((len - COAP_MESSAGE_SIZE_OFFSET_TCP32) >> 8);
1719 pdu->token[-2] = (uint8_t)(len - COAP_MESSAGE_SIZE_OFFSET_TCP32);
1720 pdu->token[-1] = pdu->code;
1721 pdu->hdr_size = 6;
1722 }
1723#endif /* ! COAP_DISABLE_TCP */
1724 } else {
1725 coap_log_warn("coap_pdu_encode_header: unsupported protocol\n");
1726 }
1727 return pdu->hdr_size;
1728}
1729
1732 return pdu->code;
1733}
1734
1735void
1737#ifndef RIOT_VERSION
1738 assert(code <= 0xff);
1739#endif /* RIOT_VERSION */
1740 pdu->code = code;
1741}
1742
1745 return pdu->type;
1746}
1747
1748void
1750 assert(type <= 0x3);
1751 pdu->type = type;
1752}
1753
1756 return pdu->actual_token;
1757}
1758
1761 return pdu->mid;
1762}
1763
1764void
1766#if (UINT_MAX > 65535)
1767 assert(mid >= 0 && mid <= 0xffff);
1768#endif /* UINT_MAX > 65535 */
1769 pdu->mid = mid;
1770}
1771
1772coap_pdu_t *
1774 if (pdu != NULL) {
1775 ++pdu->ref;
1776 }
1777 return pdu;
1778}
1779
1780coap_pdu_t *
1782 coap_pdu_t *pdu_rw = NULL;
1783
1784 if (pdu != NULL) {
1785
1786 /* Need to do this to not get a compiler warning about const parameters */
1787 memcpy(&pdu_rw, &pdu, sizeof(pdu_rw));
1788 ++pdu_rw->ref;
1789 }
1790 return pdu_rw;
1791}
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:615
#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:1239
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:1430
#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:1443
#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:1773
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:1463
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:1178
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:1307
#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:1635
#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:1611
#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:1781
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:1731
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:1736
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:1749
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:1744
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:1601
void coap_pdu_set_mid(coap_pdu_t *pdu, coap_mid_t mid)
Sets the message id in the pdu.
Definition coap_pdu.c:1765
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:1760
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:1755
@ 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:676
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:1162
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