libcoap 4.3.5-develop-4c3f4af
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, uint16_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, max(size, COAP_DEFAULT_MTU) + 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 defineable 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 /* Need to locate where in current options to insert this one */
707 while ((option = coap_option_next(&opt_iter))) {
708 if (opt_iter.number > number) {
709 /* Found where to insert */
710 break;
711 }
712 prev_number = opt_iter.number;
713 }
714 if (option == NULL) {
715 /* Code is broken somewhere */
716 coap_log_warn("coap_insert_option: Broken max_opt\n");
717 return 0;
718 }
719
720 /* size of option inc header to insert */
721 shift = coap_opt_encode_size(number - prev_number, len);
722
723 /* size of next option (header may shrink in size as delta changes */
724 if (!coap_opt_parse(option, pdu->used_size - (option - pdu->token), &decode))
725 return 0;
726 opt_delta = opt_iter.number - number;
727 if (opt_delta == 0) {
728 if (!coap_option_check_repeatable(pdu, number))
729 return 0;
730 }
731
732 if (!coap_pdu_check_resize(pdu,
733 pdu->used_size + shift - shrink))
734 return 0;
735
736 /* Possible a re-size took place with a realloc() */
737 /* Need to locate where in current options to insert this one */
739 while ((option = coap_option_next(&opt_iter))) {
740 if (opt_iter.number > number) {
741 /* Found where to insert */
742 break;
743 }
744 }
745 assert(option != NULL);
746
747 if (decode.delta < 13) {
748 /* can simply patch in the new delta of next option */
749 option[0] = (option[0] & 0x0f) + (coap_opt_t)(opt_delta << 4);
750 } else if (decode.delta < 269 && opt_delta < 13) {
751 /* option header is going to shrink by one byte */
752 option[1] = (option[0] & 0x0f) + (coap_opt_t)(opt_delta << 4);
753 shrink = 1;
754 } else if (decode.delta < 269 && opt_delta < 269) {
755 /* can simply patch in the new delta of next option */
756 option[1] = (coap_opt_t)(opt_delta - 13);
757 } else if (opt_delta < 13) {
758 /* option header is going to shrink by two bytes */
759 option[2] = (option[0] & 0x0f) + (coap_opt_t)(opt_delta << 4);
760 shrink = 2;
761 } else if (opt_delta < 269) {
762 /* option header is going to shrink by one bytes */
763 option[1] = (option[0] & 0x0f) + 0xd0;
764 option[2] = (coap_opt_t)(opt_delta - 13);
765 shrink = 1;
766 } else {
767 /* can simply patch in the new delta of next option */
768 option[1] = (coap_opt_t)((opt_delta - 269) >> 8);
769 option[2] = (opt_delta - 269) & 0xff;
770 }
771
772 memmove(&option[shift], &option[shrink],
773 pdu->used_size - (option - pdu->token) - shrink);
774 if (!coap_opt_encode(option, pdu->alloc_size - pdu->used_size,
775 number - prev_number, data, len))
776 return 0;
777
778 if (shift >= shrink) {
779 pdu->used_size += shift - shrink;
780 if (pdu->data)
781 pdu->data += shift - shrink;
782 } else {
783 pdu->used_size -= shrink - shift;
784 if (pdu->data)
785 pdu->data -= shrink - shift;
786 }
787 return shift;
788}
789
790size_t
792 const uint8_t *data) {
793 coap_opt_iterator_t opt_iter;
794 coap_opt_t *option;
795 coap_option_t decode;
796 size_t new_length = 0;
797 size_t old_length = 0;
798
799 option = coap_check_option(pdu, number, &opt_iter);
800 if (!option)
801 return coap_insert_option(pdu, number, len, data);
802
803 old_length = coap_opt_parse(option, (size_t)-1, &decode);
804 if (old_length == 0)
805 return 0;
806 new_length = coap_opt_encode_size(decode.delta, len);
807
808 if (new_length > old_length) {
809 if (!coap_pdu_check_resize(pdu,
810 pdu->used_size + new_length - old_length))
811 return 0;
812 /* Possible a re-size took place with a realloc() */
813 option = coap_check_option(pdu, number, &opt_iter);
814 }
815
816 if (new_length != old_length)
817 memmove(&option[new_length], &option[old_length],
818 pdu->used_size - (option - pdu->token) - old_length);
819
820 if (!coap_opt_encode(option, new_length,
821 decode.delta, data, len))
822 return 0;
823
824 if (new_length >= old_length) {
825 pdu->used_size += new_length - old_length;
826 if (pdu->data)
827 pdu->data += new_length - old_length;
828 } else {
829 pdu->used_size -= old_length - new_length;
830 if (pdu->data)
831 pdu->data -= old_length - new_length;
832 }
833 return 1;
834}
835
836size_t
838 const uint8_t *data) {
839 if (pdu->data) {
840 coap_log_warn("coap_add_option: PDU already contains data\n");
841 return 0;
842 }
843 if (!coap_pdu_parse_opt_base(number, len, data)) {
844 coap_log_warn("qcoap_add_option: %d: Invalid option length / data\n", number);
845 return 0;
846 }
847 return coap_add_option_internal(pdu, number, len, data);
848}
849
850size_t
852 const uint8_t *data) {
853 size_t optsize;
854 coap_opt_t *opt;
855
856 assert(pdu);
857
858 if (number == pdu->max_opt) {
859 if (!coap_option_check_repeatable(pdu, number))
860 return 0;
861 }
862
863 if (COAP_PDU_IS_REQUEST(pdu) &&
864 (number == COAP_OPTION_PROXY_URI ||
865 number == COAP_OPTION_PROXY_SCHEME)) {
866 /*
867 * Need to check whether there is a hop-limit option. If not, it needs
868 * to be inserted by default (RFC 8768).
869 */
870 coap_opt_iterator_t opt_iter;
871
872 if (coap_check_option(pdu, COAP_OPTION_HOP_LIMIT, &opt_iter) == NULL) {
873 size_t hop_limit = COAP_OPTION_HOP_LIMIT;
874
875 coap_insert_option(pdu, COAP_OPTION_HOP_LIMIT, 1, (uint8_t *)&hop_limit);
876 }
877 }
878
879 if (number < pdu->max_opt) {
880 coap_log_debug("coap_add_option: options are not in correct order\n");
881 return coap_insert_option(pdu, number, len, data);
882 }
883
884 optsize = coap_opt_encode_size(number - pdu->max_opt, len);
885 if (!coap_pdu_check_resize(pdu,
886 pdu->used_size + optsize))
887 return 0;
888
889 if (pdu->data) {
890 /* include option delimiter */
891 memmove(&pdu->data[optsize-1], &pdu->data[-1],
892 pdu->used_size - (pdu->data - pdu->token) + 1);
893 opt = pdu->data -1;
894 pdu->data += optsize;
895 } else {
896 opt = pdu->token + pdu->used_size;
897 }
898
899 /* encode option and check length */
900 optsize = coap_opt_encode(opt, pdu->alloc_size - pdu->used_size,
901 number - pdu->max_opt, data, len);
902
903 if (!optsize) {
904 coap_log_warn("coap_add_option: cannot add option\n");
905 /* error */
906 return 0;
907 } else {
908 pdu->max_opt = number;
909 pdu->used_size += optsize;
910 }
911
912 return optsize;
913}
914
915int
916coap_add_data(coap_pdu_t *pdu, size_t len, const uint8_t *data) {
917 if (len == 0) {
918 return 1;
919 } else {
920 uint8_t *payload = coap_add_data_after(pdu, len);
921 if (payload != NULL)
922 memcpy(payload, data, len);
923 return payload != NULL;
924 }
925}
926
927uint8_t *
929 assert(pdu);
930 if (pdu->data) {
931 coap_log_warn("coap_add_data: PDU already contains data\n");
932 return 0;
933 }
934
935 if (len == 0)
936 return NULL;
937
938 if (!coap_pdu_resize(pdu, pdu->used_size + len + 1))
939 return 0;
940 pdu->token[pdu->used_size++] = COAP_PAYLOAD_START;
941 pdu->data = pdu->token + pdu->used_size;
942 pdu->used_size += len;
943 return pdu->data;
944}
945
946int
947coap_get_data(const coap_pdu_t *pdu, size_t *len, const uint8_t **data) {
948 size_t offset;
949 size_t total;
950
951 return coap_get_data_large(pdu, len, data, &offset, &total);
952}
953
954int
955coap_get_data_large(const coap_pdu_t *pdu, size_t *len, const uint8_t **data,
956 size_t *offset, size_t *total) {
957 assert(pdu);
958 assert(len);
959 assert(data);
960
961 *offset = pdu->body_offset;
962 *total = pdu->body_total;
963 if (pdu->body_data) {
964 *data = pdu->body_data;
965 *len = pdu->body_length;
966 return 1;
967 }
968 *data = pdu->data;
969 if (pdu->data == NULL) {
970 *len = 0;
971 *total = 0;
972 return 0;
973 }
974
975 *len = pdu->used_size - (pdu->data - pdu->token);
976 if (*total == 0)
977 *total = *len;
978
979 return 1;
980}
981
982#ifndef SHORT_ERROR_RESPONSE
983typedef struct {
984 unsigned char code;
985 const char *phrase;
987
988/* if you change anything here, make sure, that the longest string does not
989 * exceed COAP_ERROR_PHRASE_LENGTH. */
991 { COAP_RESPONSE_CODE(201), "Created" },
992 { COAP_RESPONSE_CODE(202), "Deleted" },
993 { COAP_RESPONSE_CODE(203), "Valid" },
994 { COAP_RESPONSE_CODE(204), "Changed" },
995 { COAP_RESPONSE_CODE(205), "Content" },
996 { COAP_RESPONSE_CODE(231), "Continue" },
997 { COAP_RESPONSE_CODE(400), "Bad Request" },
998 { COAP_RESPONSE_CODE(401), "Unauthorized" },
999 { COAP_RESPONSE_CODE(402), "Bad Option" },
1000 { COAP_RESPONSE_CODE(403), "Forbidden" },
1001 { COAP_RESPONSE_CODE(404), "Not Found" },
1002 { COAP_RESPONSE_CODE(405), "Method Not Allowed" },
1003 { COAP_RESPONSE_CODE(406), "Not Acceptable" },
1004 { COAP_RESPONSE_CODE(408), "Request Entity Incomplete" },
1005 { COAP_RESPONSE_CODE(409), "Conflict" },
1006 { COAP_RESPONSE_CODE(412), "Precondition Failed" },
1007 { COAP_RESPONSE_CODE(413), "Request Entity Too Large" },
1008 { COAP_RESPONSE_CODE(415), "Unsupported Content-Format" },
1009 { COAP_RESPONSE_CODE(422), "Unprocessable" },
1010 { COAP_RESPONSE_CODE(429), "Too Many Requests" },
1011 { COAP_RESPONSE_CODE(500), "Internal Server Error" },
1012 { COAP_RESPONSE_CODE(501), "Not Implemented" },
1013 { COAP_RESPONSE_CODE(502), "Bad Gateway" },
1014 { COAP_RESPONSE_CODE(503), "Service Unavailable" },
1015 { COAP_RESPONSE_CODE(504), "Gateway Timeout" },
1016 { COAP_RESPONSE_CODE(505), "Proxying Not Supported" },
1017 { COAP_RESPONSE_CODE(508), "Hop Limit Reached" },
1018 { 0, NULL } /* end marker */
1019};
1020
1021const char *
1022coap_response_phrase(unsigned char code) {
1023 int i;
1024 for (i = 0; coap_error[i].code; ++i) {
1025 if (coap_error[i].code == code)
1026 return coap_error[i].phrase;
1027 }
1028 return NULL;
1029}
1030#endif
1031
1037static size_t
1038next_option_safe(coap_opt_t **optp, size_t *length, uint16_t *max_opt) {
1039 coap_option_t option;
1040 size_t optsize;
1041
1042 assert(optp);
1043 assert(*optp);
1044 assert(length);
1045
1046 optsize = coap_opt_parse(*optp, *length, &option);
1047 assert(optsize <= *length);
1048
1049 /* signal an error if this option would exceed the
1050 * allowed number space */
1051 if ((uint32_t)(*max_opt) + option.delta > COAP_MAX_OPT) {
1052 return 0;
1053 }
1054 *max_opt += option.delta;
1055 *optp += optsize;
1056 *length -= optsize;
1057
1058 return optsize;
1059}
1060
1061size_t
1063 const uint8_t *data) {
1064 assert(data);
1065 size_t header_size = 0;
1066
1067 if (proto == COAP_PROTO_TCP || proto==COAP_PROTO_TLS) {
1068 uint8_t len = *data >> 4;
1069 if (len < 13)
1070 header_size = 2;
1071 else if (len==13)
1072 header_size = 3;
1073 else if (len==14)
1074 header_size = 4;
1075 else
1076 header_size = 6;
1077 } else if (proto == COAP_PROTO_WS || proto==COAP_PROTO_WSS) {
1078 header_size = 2;
1079 } else if (proto == COAP_PROTO_UDP || proto==COAP_PROTO_DTLS) {
1080 header_size = 4;
1081 }
1082
1083 return header_size;
1084}
1085
1086#if !COAP_DISABLE_TCP
1087/*
1088 * strm
1089 * return +ve PDU size including token
1090 * 0 PDU does not parse
1091 */
1092size_t
1094 const uint8_t *data,
1095 size_t length) {
1096 assert(data);
1097 assert(proto == COAP_PROTO_TCP || proto == COAP_PROTO_TLS ||
1098 proto == COAP_PROTO_WS || proto == COAP_PROTO_WSS);
1099 assert(coap_pdu_parse_header_size(proto, data) <= length);
1100
1101 size_t size = 0;
1102 const uint8_t *token_start = NULL;
1103
1104 if ((proto == COAP_PROTO_TCP || proto == COAP_PROTO_TLS) && length >= 1) {
1105 uint8_t len = *data >> 4;
1106 uint8_t tkl = *data & 0x0f;
1107
1108 if (len < 13) {
1109 size = len;
1110 token_start = &data[2];
1111 } else if (length >= 2) {
1112 if (len==13) {
1113 size = (size_t)data[1] + COAP_MESSAGE_SIZE_OFFSET_TCP8;
1114 token_start = &data[3];
1115 } else if (length >= 3) {
1116 if (len==14) {
1117 size = ((size_t)data[1] << 8) + data[2] + COAP_MESSAGE_SIZE_OFFSET_TCP16;
1118 token_start = &data[4];
1119 } else if (length >= 5) {
1120 size = ((size_t)data[1] << 24) + ((size_t)data[2] << 16)
1121 + ((size_t)data[3] << 8) + data[4] + COAP_MESSAGE_SIZE_OFFSET_TCP32;
1122 token_start = &data[6];
1123 }
1124 }
1125 }
1126 if (token_start) {
1127 /* account for the token length */
1128 if (tkl < COAP_TOKEN_EXT_1B_TKL) {
1129 size += tkl;
1130 } else if (tkl == COAP_TOKEN_EXT_1B_TKL) {
1131 size += token_start[0] + COAP_TOKEN_EXT_1B_BIAS + 1;
1132 } else if (tkl == COAP_TOKEN_EXT_2B_TKL) {
1133 size += ((uint16_t)token_start[0] << 8) + token_start[1] +
1135 } else {
1136 /* Invalid at this point - caught later as undersized */
1137 }
1138 }
1139 }
1140
1141 return size;
1142}
1143#endif /* ! COAP_DISABLE_TCP */
1144
1145int
1147 uint8_t *hdr = pdu->token - pdu->hdr_size;
1148 uint8_t e_token_length;
1149
1150 if (proto == COAP_PROTO_UDP || proto == COAP_PROTO_DTLS) {
1151 assert(pdu->hdr_size == 4);
1152 if ((hdr[0] >> 6) != COAP_DEFAULT_VERSION) {
1153 coap_log_debug("coap_pdu_parse: UDP version not supported\n");
1154 return 0;
1155 }
1156 pdu->type = (hdr[0] >> 4) & 0x03;
1157 pdu->code = hdr[1];
1158 pdu->mid = (uint16_t)hdr[2] << 8 | hdr[3];
1159 } else if (proto == COAP_PROTO_TCP || proto == COAP_PROTO_TLS) {
1160 assert(pdu->hdr_size >= 2 && pdu->hdr_size <= 6);
1161 pdu->type = COAP_MESSAGE_CON;
1162 pdu->code = hdr[pdu->hdr_size-1];
1163 pdu->mid = 0;
1164 } else if (proto == COAP_PROTO_WS || proto == COAP_PROTO_WSS) {
1165 assert(pdu->hdr_size == 2);
1166 pdu->type = COAP_MESSAGE_CON;
1167 pdu->code = hdr[pdu->hdr_size-1];
1168 pdu->mid = 0;
1169 } else {
1170 coap_log_debug("coap_pdu_parse: unsupported protocol\n");
1171 return 0;
1172 }
1173
1174 e_token_length = hdr[0] & 0x0f;
1175 if (e_token_length < COAP_TOKEN_EXT_1B_TKL) {
1176 pdu->e_token_length = e_token_length;
1178 pdu->actual_token.s = &pdu->token[0];
1179 } else if (e_token_length == COAP_TOKEN_EXT_1B_TKL) {
1180 if (pdu->used_size < 1)
1181 goto bad_ext_token;
1182 pdu->e_token_length = pdu->token[0] + COAP_TOKEN_EXT_1B_BIAS + 1;
1183 pdu->actual_token.length = pdu->e_token_length - 1;
1184 pdu->actual_token.s = &pdu->token[1];
1185 } else if (e_token_length == COAP_TOKEN_EXT_2B_TKL) {
1186 if (pdu->used_size < 2)
1187 goto bad_ext_token;
1188 pdu->e_token_length = ((uint16_t)pdu->token[0] << 8) + pdu->token[1] +
1190 pdu->actual_token.length = pdu->e_token_length - 2;
1191 pdu->actual_token.s = &pdu->token[2];
1192 }
1193 if (pdu->e_token_length > pdu->alloc_size || e_token_length == 15) {
1194 goto bad_ext_token;
1195 }
1196 return 1;
1197
1198bad_ext_token:
1199 /* Invalid PDU provided - not wise to assert here though */
1200 coap_log_debug("coap_pdu_parse: PDU header extended token size broken\n");
1201 pdu->e_token_length = 0;
1202 pdu->actual_token.length = 0;
1203 return 0;
1204}
1205
1206static int
1208 switch ((coap_pdu_signaling_proto_t)code) {
1209 case COAP_SIGNALING_CSM:
1210 switch ((coap_sig_csm_opt_t)option) {
1212 if (len > 4)
1213 goto bad;
1214 break;
1216 if (len > 0)
1217 goto bad;
1218 break;
1220 if (len > 3)
1221 goto bad;
1222 break;
1223 default:
1224 if (option & 0x01)
1225 goto bad; /* Critical */
1226 }
1227 break;
1230 switch ((coap_sig_ping_opt_t)option) {
1232 if (len > 0)
1233 goto bad;
1234 break;
1235 default:
1236 if (option & 0x01)
1237 goto bad; /* Critical */
1238 }
1239 break;
1241 switch ((coap_sig_release_opt_t)option) {
1243 if (len < 1 || len > 255)
1244 goto bad;
1245 break;
1247 if (len > 3)
1248 goto bad;
1249 break;
1250 default:
1251 if (option & 0x01)
1252 goto bad; /* Critical */
1253 }
1254 break;
1256 switch ((coap_sig_abort_opt_t)option) {
1258 if (len > 2)
1259 goto bad;
1260 break;
1261 default:
1262 if (option & 0x01)
1263 goto bad; /* Critical */
1264 }
1265 break;
1266 default:
1267 goto bad;
1268 }
1269 return 1;
1270bad:
1271 return 0;
1272}
1273
1274int
1275coap_pdu_parse_opt_base(coap_option_num_t number, size_t len, const uint8_t *opt_val) {
1276 int res = 1;
1277
1278 switch ((coap_code_opt_num_t)number) {
1280 if (len > 8)
1281 res = 0;
1282 break;
1284 if (len < 1 || len > 255)
1285 res = 0;
1286 break;
1287 case COAP_OPTION_ETAG:
1288 if (len < 1 || len > 8)
1289 res = 0;
1290 break;
1292 if (len != 0)
1293 res = 0;
1294 break;
1296 if (len > 3)
1297 res = 0;
1298 break;
1300 if (len > 2)
1301 res = 0;
1302 break;
1305 if (len > 255)
1306 res = 0;
1307 if (coap_check_dots(opt_val, len)) {
1308 res = 0;
1309 }
1310 break;
1311 case COAP_OPTION_OSCORE:
1312 if (len > 255)
1313 res = 0;
1314 break;
1316 if (len > 2)
1317 res = 0;
1318 break;
1320 if (len > 4)
1321 res = 0;
1322 break;
1323 case COAP_OPTION_MAXAGE:
1324 if (len > 4)
1325 res = 0;
1326 break;
1328 if (len < 1 || len > 255)
1329 res = 0;
1330 break;
1332 if (len != 1)
1333 res = 0;
1334 break;
1335 case COAP_OPTION_ACCEPT:
1336 if (len > 2)
1337 res = 0;
1338 break;
1340 if (len > 3)
1341 res = 0;
1342 break;
1344 if (len > 255)
1345 res = 0;
1346 break;
1347 case COAP_OPTION_EDHOC:
1348 if (len != 0)
1349 res = 0;
1350 break;
1351 case COAP_OPTION_BLOCK2:
1352 if (len > 3)
1353 res = 0;
1354 break;
1355 case COAP_OPTION_BLOCK1:
1356 if (len > 3)
1357 res = 0;
1358 break;
1359 case COAP_OPTION_SIZE2:
1360 if (len > 4)
1361 res = 0;
1362 break;
1364 if (len > 3)
1365 res = 0;
1366 break;
1368 if (len < 1 || len > 1034)
1369 res = 0;
1370 break;
1372 if (len < 1 || len > 255)
1373 res = 0;
1374 break;
1375 case COAP_OPTION_SIZE1:
1376 if (len > 4)
1377 res = 0;
1378 break;
1379 case COAP_OPTION_ECHO:
1380 if (len > 40)
1381 res = 0;
1382 break;
1384 if (len > 1)
1385 res = 0;
1386 break;
1387 case COAP_OPTION_RTAG:
1388 if (len > 8)
1389 res = 0;
1390 break;
1391 default:
1392 ;
1393 }
1394 return res;
1395}
1396
1397static int
1398write_prefix(char **obp, size_t *len, const char *prf, size_t prflen) {
1399 /* Make sure space for null terminating byte */
1400 if (*len < prflen +1) {
1401 return 0;
1402 }
1403
1404 memcpy(*obp, prf, prflen);
1405 *obp += prflen;
1406 *len -= prflen;
1407 return 1;
1408}
1409
1410static int
1411write_char(char **obp, size_t *len, int c, int printable) {
1412 /* Make sure space for null terminating byte */
1413 if (*len < 2 +1) {
1414 return 0;
1415 }
1416
1417 if (!printable) {
1418 const uint8_t hex[] = "0123456789abcdef";
1419 (*obp)[0] = hex[(c & 0xf0) >> 4];
1420 (*obp)[1] = hex[c & 0x0f];
1421 } else {
1422 (*obp)[0] = isprint(c) ? c : '.';
1423 (*obp)[1] = ' ';
1424 }
1425 *obp += 2;
1426 *len -= 2;
1427 return 1;
1428}
1429
1430int
1432 int good = 1;
1433
1434 coap_option_filter_clear(error_opts);
1435
1436 /* sanity checks */
1437 if (pdu->code == 0) {
1438 if (pdu->used_size != 0 || pdu->e_token_length) {
1439 coap_log_debug("coap_pdu_parse: empty message is not empty\n");
1440 return 0;
1441 }
1442 }
1443
1444 if (pdu->e_token_length > pdu->used_size) {
1445 coap_log_debug("coap_pdu_parse: invalid Token\n");
1446 return 0;
1447 }
1448
1449 pdu->max_opt = 0;
1450 if (pdu->code == 0) {
1451 /* empty packet */
1452 pdu->used_size = 0;
1453 pdu->data = NULL;
1454 } else {
1455 /* skip header + token */
1456 coap_opt_t *opt = pdu->token + pdu->e_token_length;
1457 size_t length = pdu->used_size - pdu->e_token_length;
1458
1459 while (length > 0 && *opt != COAP_PAYLOAD_START) {
1460#if (COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_WARN)
1461 coap_opt_t *opt_last = opt;
1462#endif
1463 size_t optsize = next_option_safe(&opt, &length, &pdu->max_opt);
1464 uint32_t len;
1465 const uint8_t *opt_val;
1466
1467 if (optsize == 0) {
1468 coap_log_debug("coap_pdu_parse: %d.%02d: offset %u malformed option\n",
1469 pdu->code >> 5, pdu->code & 0x1F,
1470 (int)(opt_last - pdu->token - pdu->e_token_length));
1471 coap_option_filter_set(error_opts, pdu->max_opt);
1472 good = 0;
1473 break;
1474 }
1475 len = coap_opt_length((const uint8_t *)opt - optsize);
1476 opt_val = coap_opt_value((const uint8_t *)opt - optsize);
1477 if (COAP_PDU_IS_SIGNALING(pdu) ?
1478 !coap_pdu_parse_opt_csm(pdu->code, pdu->max_opt, len) :
1479 !coap_pdu_parse_opt_base(pdu->max_opt, len, opt_val)) {
1480 coap_log_warn("coap_pdu_parse: %d.%02d: offset %u option %u has bad length %" PRIu32 " or value\n",
1481 pdu->code >> 5, pdu->code & 0x1F,
1482 (int)(opt_last - pdu->token - pdu->e_token_length), pdu->max_opt,
1483 len);
1484 coap_option_filter_set(error_opts, pdu->max_opt);
1485 good = 0;
1486 }
1487 }
1488
1489 if (!good) {
1490 /*
1491 * Dump the options in the PDU for analysis, space separated except
1492 * error options which are prefixed by *
1493 * Two rows - hex and ascii (if printable)
1494 */
1495 static char outbuf[COAP_DEBUG_BUF_SIZE];
1496 char *obp;
1497 size_t tlen;
1498 size_t outbuflen;
1499 int i;
1500 int ok;
1501
1502 for (i = 0; i < 2; i++) {
1503 opt = pdu->token + pdu->e_token_length;
1504 length = pdu->used_size - pdu->e_token_length;
1505 pdu->max_opt = 0;
1506
1507 outbuflen = sizeof(outbuf);
1508 obp = outbuf;
1509 ok = write_prefix(&obp, &outbuflen, "O: ", 3);
1510 /*
1511 * Not safe to check for 'ok' here as a lot of variables may get
1512 * partially changed due to lack of outbuflen */
1513 while (length > 0 && *opt != COAP_PAYLOAD_START) {
1514 coap_opt_t *opt_last = opt;
1515 size_t optsize = next_option_safe(&opt, &length, &pdu->max_opt);
1516 const uint32_t len =
1517 optsize ? coap_opt_length((const uint8_t *)opt - optsize) : 0;
1518 const uint8_t *opt_val =
1519 optsize ? coap_opt_value((const uint8_t *)opt - optsize) : 0;
1520
1521 if (!optsize || (COAP_PDU_IS_SIGNALING(pdu) ?
1522 !coap_pdu_parse_opt_csm(pdu->code, pdu->max_opt, len) :
1523 !coap_pdu_parse_opt_base(pdu->max_opt, len, opt_val))) {
1524 ok = ok && write_prefix(&obp, &outbuflen, "*", 1);
1525 if (!optsize) {
1526 /* Skip to end of options to output all data */
1527 opt = pdu->token + pdu->used_size;
1528 length = 0;
1529 }
1530 } else {
1531 ok = ok && write_prefix(&obp, &outbuflen, " ", 1);
1532 }
1533 tlen = opt - opt_last;
1534 while (tlen) {
1535 tlen--;
1536 ok = ok && write_char(&obp, &outbuflen, *opt_last, i);
1537 opt_last++;
1538 }
1539 }
1540 if (length && *opt == COAP_PAYLOAD_START) {
1541 write_char(&obp, &outbuflen, *opt, i);
1542 }
1543 /* write_*() always leaves a spare byte to null terminate */
1544 *obp = '\000';
1545 coap_log_debug("%s\n", outbuf);
1546 }
1547 }
1548
1549 if (length > 0) {
1550 assert(*opt == COAP_PAYLOAD_START);
1551 opt++;
1552 length--;
1553
1554 if (length == 0) {
1555 coap_log_debug("coap_pdu_parse: message ending in payload start marker\n");
1556 return 0;
1557 }
1558 }
1559 if (length > 0)
1560 pdu->data = (uint8_t *)opt;
1561 else
1562 pdu->data = NULL;
1563 }
1564
1565 return good;
1566}
1567
1568int
1570 const uint8_t *data,
1571 size_t length,
1572 coap_pdu_t *pdu) {
1573 coap_opt_filter_t error_opts;
1574
1575 return coap_pdu_parse2(proto, data, length, pdu, &error_opts);
1576}
1577
1578int
1580 const uint8_t *data,
1581 size_t length,
1582 coap_pdu_t *pdu,
1583 coap_opt_filter_t *error_opts) {
1584 size_t hdr_size;
1585
1586 if (length == 0)
1587 return 0;
1588 hdr_size = coap_pdu_parse_header_size(proto, data);
1589 if (!hdr_size || hdr_size > length)
1590 return 0;
1591 if (hdr_size > pdu->max_hdr_size)
1592 return 0;
1593 if (!coap_pdu_resize(pdu, length - hdr_size))
1594 return 0;
1595 if (pdu->token - hdr_size != data)
1596 memcpy(pdu->token - hdr_size, data, length);
1597 pdu->hdr_size = (uint8_t)hdr_size;
1598 pdu->used_size = length - hdr_size;
1599 return coap_pdu_parse_header(pdu, proto) && coap_pdu_parse_opt(pdu, error_opts);
1600}
1601
1602size_t
1604 uint8_t e_token_length;
1605
1607 e_token_length = (uint8_t)pdu->actual_token.length;
1608 } else if (pdu->actual_token.length < COAP_TOKEN_EXT_2B_BIAS) {
1609 e_token_length = COAP_TOKEN_EXT_1B_TKL;
1610 } else if (pdu->actual_token.length <= COAP_TOKEN_EXT_MAX) {
1611 e_token_length = COAP_TOKEN_EXT_2B_TKL;
1612 } else {
1613 coap_log_warn("coap_pdu_encode_header: Token size too large. PDU ignored\n");
1614 return 0;
1615 }
1616 if (COAP_PROTO_NOT_RELIABLE(proto)) {
1617 assert(pdu->max_hdr_size >= 4);
1618 if (pdu->max_hdr_size < 4) {
1619 coap_log_warn("coap_pdu_encode_header: not enough space for UDP-style header\n");
1620 return 0;
1621 }
1622 pdu->token[-4] = COAP_DEFAULT_VERSION << 6
1623 | pdu->type << 4
1624 | e_token_length;
1625 pdu->token[-3] = pdu->code;
1626 pdu->token[-2] = (uint8_t)(pdu->mid >> 8);
1627 pdu->token[-1] = (uint8_t)(pdu->mid);
1628 pdu->hdr_size = 4;
1629#if !COAP_DISABLE_TCP
1630 } else if (COAP_PROTO_RELIABLE(proto)) {
1631 size_t len;
1632 assert(pdu->used_size >= pdu->e_token_length);
1633 if (pdu->used_size < pdu->e_token_length) {
1634 coap_log_warn("coap_pdu_encode_header: corrupted PDU\n");
1635 return 0;
1636 }
1637
1638 /* A lot of the reliable code assumes type is CON */
1639 if (pdu->type != COAP_MESSAGE_CON)
1640 pdu->type = COAP_MESSAGE_CON;
1641
1642 if (proto == COAP_PROTO_WS || proto == COAP_PROTO_WSS)
1643 len = 0;
1644 else
1645 len = pdu->used_size - pdu->e_token_length;
1646 if (len <= COAP_MAX_MESSAGE_SIZE_TCP0) {
1647 assert(pdu->max_hdr_size >= 2);
1648 if (pdu->max_hdr_size < 2) {
1649 coap_log_warn("coap_pdu_encode_header: not enough space for TCP0 header\n");
1650 return 0;
1651 }
1652 pdu->token[-2] = (uint8_t)len << 4
1653 | e_token_length;
1654 pdu->token[-1] = pdu->code;
1655 pdu->hdr_size = 2;
1656 } else if (len <= COAP_MAX_MESSAGE_SIZE_TCP8) {
1657 assert(pdu->max_hdr_size >= 3);
1658 if (pdu->max_hdr_size < 3) {
1659 coap_log_warn("coap_pdu_encode_header: not enough space for TCP8 header\n");
1660 return 0;
1661 }
1662 pdu->token[-3] = 13 << 4 | e_token_length;
1663 pdu->token[-2] = (uint8_t)(len - COAP_MESSAGE_SIZE_OFFSET_TCP8);
1664 pdu->token[-1] = pdu->code;
1665 pdu->hdr_size = 3;
1666 } else if (len <= COAP_MAX_MESSAGE_SIZE_TCP16) {
1667 assert(pdu->max_hdr_size >= 4);
1668 if (pdu->max_hdr_size < 4) {
1669 coap_log_warn("coap_pdu_encode_header: not enough space for TCP16 header\n");
1670 return 0;
1671 }
1672 pdu->token[-4] = 14 << 4 | e_token_length;
1673 pdu->token[-3] = (uint8_t)((len - COAP_MESSAGE_SIZE_OFFSET_TCP16) >> 8);
1674 pdu->token[-2] = (uint8_t)(len - COAP_MESSAGE_SIZE_OFFSET_TCP16);
1675 pdu->token[-1] = pdu->code;
1676 pdu->hdr_size = 4;
1677 } else {
1678 assert(pdu->max_hdr_size >= 6);
1679 if (pdu->max_hdr_size < 6) {
1680 coap_log_warn("coap_pdu_encode_header: not enough space for TCP32 header\n");
1681 return 0;
1682 }
1683 pdu->token[-6] = 15 << 4 | e_token_length;
1684 pdu->token[-5] = (uint8_t)((len - COAP_MESSAGE_SIZE_OFFSET_TCP32) >> 24);
1685 pdu->token[-4] = (uint8_t)((len - COAP_MESSAGE_SIZE_OFFSET_TCP32) >> 16);
1686 pdu->token[-3] = (uint8_t)((len - COAP_MESSAGE_SIZE_OFFSET_TCP32) >> 8);
1687 pdu->token[-2] = (uint8_t)(len - COAP_MESSAGE_SIZE_OFFSET_TCP32);
1688 pdu->token[-1] = pdu->code;
1689 pdu->hdr_size = 6;
1690 }
1691#endif /* ! COAP_DISABLE_TCP */
1692 } else {
1693 coap_log_warn("coap_pdu_encode_header: unsupported protocol\n");
1694 }
1695 return pdu->hdr_size;
1696}
1697
1700 return pdu->code;
1701}
1702
1703void
1705#ifndef RIOT_VERSION
1706 assert(code <= 0xff);
1707#endif /* RIOT_VERSION */
1708 pdu->code = code;
1709}
1710
1713 return pdu->type;
1714}
1715
1716void
1718 assert(type <= 0x3);
1719 pdu->type = type;
1720}
1721
1724 return pdu->actual_token;
1725}
1726
1729 return pdu->mid;
1730}
1731
1732void
1734#if (UINT_MAX > 65535)
1735 assert(mid >= 0 && mid <= 0xffff);
1736#endif /* UINT_MAX > 65535 */
1737 pdu->mid = mid;
1738}
1739
1740coap_pdu_t *
1742 if (pdu != NULL) {
1743 ++pdu->ref;
1744 }
1745 return pdu;
1746}
1747
1748coap_pdu_t *
1750 coap_pdu_t *pdu_rw = NULL;
1751
1752 if (pdu != NULL) {
1753
1754 /* Need to do this to not get a compiler warning about const parameters */
1755 memcpy(&pdu_rw, &pdu, sizeof(pdu_rw));
1756 ++pdu_rw->ref;
1757 }
1758 return pdu_rw;
1759}
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 int coap_pdu_parse_opt_csm(coap_pdu_code_t code, coap_option_num_t option, uint16_t len)
Definition coap_pdu.c:1207
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:1038
error_desc_t coap_error[]
Definition coap_pdu.c:990
static int write_prefix(char **obp, size_t *len, const char *prf, size_t prflen)
Definition coap_pdu.c:1398
#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:1411
#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:1741
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:1431
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:1146
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:1062
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:1275
#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:791
#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:1603
#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:1579
#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:1093
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:1749
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:851
coap_pdu_code_t coap_pdu_get_code(const coap_pdu_t *pdu)
Gets the PDU code associated with pdu.
Definition coap_pdu.c:1699
const char * coap_response_phrase(unsigned char code)
Returns a human-readable response phrase for the specified CoAP response code.
Definition coap_pdu.c:1022
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:928
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:1704
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:1717
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:837
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:1712
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:947
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:1569
void coap_pdu_set_mid(coap_pdu_t *pdu, coap_mid_t mid)
Sets the message id in the pdu.
Definition coap_pdu.c:1733
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:955
#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:1728
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:916
coap_bin_const_t coap_pdu_get_token(const coap_pdu_t *pdu)
Gets the token associated with pdu.
Definition coap_pdu.c:1723
@ 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:984
const char * phrase
Definition coap_pdu.c:985