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