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