libcoap 4.3.5-develop-4c7ce99
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:
593 case COAP_OPTION_RTAG:
594 break;
595 /* Protest at the known non-repeatable options and ignore them */
611 case COAP_OPTION_ECHO:
613 coap_log_info("Option number %d is not defined as repeatable - dropped\n",
614 number);
615 return 0;
616 default:
617 coap_log_info("Option number %d is not defined as repeatable\n",
618 number);
619 /* Accepting it after warning as there may be user defineable options */
620 break;
621 }
622 return 1;
623}
624
625size_t
627 const uint8_t *data) {
628 coap_opt_iterator_t opt_iter;
629 coap_opt_t *option;
630 uint16_t prev_number = 0;
631 size_t shift;
632 size_t opt_delta;
633 coap_option_t decode;
634 size_t shrink = 0;
635
636 if (number >= pdu->max_opt)
637 return coap_add_option_internal(pdu, number, len, data);
638
639 /* Need to locate where in current options to insert this one */
641 while ((option = coap_option_next(&opt_iter))) {
642 if (opt_iter.number > number) {
643 /* Found where to insert */
644 break;
645 }
646 prev_number = opt_iter.number;
647 }
648 assert(option != NULL);
649 /* size of option inc header to insert */
650 shift = coap_opt_encode_size(number - prev_number, len);
651
652 /* size of next option (header may shrink in size as delta changes */
653 if (!coap_opt_parse(option, pdu->used_size - (option - pdu->token), &decode))
654 return 0;
655 opt_delta = opt_iter.number - number;
656 if (opt_delta == 0) {
657 if (!coap_option_check_repeatable(number))
658 return 0;
659 }
660
661 if (!coap_pdu_check_resize(pdu,
662 pdu->used_size + shift - shrink))
663 return 0;
664
665 /* Possible a re-size took place with a realloc() */
666 /* Need to locate where in current options to insert this one */
668 while ((option = coap_option_next(&opt_iter))) {
669 if (opt_iter.number > number) {
670 /* Found where to insert */
671 break;
672 }
673 }
674 assert(option != NULL);
675
676 if (decode.delta < 13) {
677 /* can simply patch in the new delta of next option */
678 option[0] = (option[0] & 0x0f) + (coap_opt_t)(opt_delta << 4);
679 } else if (decode.delta < 269 && opt_delta < 13) {
680 /* option header is going to shrink by one byte */
681 option[1] = (option[0] & 0x0f) + (coap_opt_t)(opt_delta << 4);
682 shrink = 1;
683 } else if (decode.delta < 269 && opt_delta < 269) {
684 /* can simply patch in the new delta of next option */
685 option[1] = (coap_opt_t)(opt_delta - 13);
686 } else if (opt_delta < 13) {
687 /* option header is going to shrink by two bytes */
688 option[2] = (option[0] & 0x0f) + (coap_opt_t)(opt_delta << 4);
689 shrink = 2;
690 } else if (opt_delta < 269) {
691 /* option header is going to shrink by one bytes */
692 option[1] = (option[0] & 0x0f) + 0xd0;
693 option[2] = (coap_opt_t)(opt_delta - 13);
694 shrink = 1;
695 } else {
696 /* can simply patch in the new delta of next option */
697 option[1] = (coap_opt_t)((opt_delta - 269) >> 8);
698 option[2] = (opt_delta - 269) & 0xff;
699 }
700
701 memmove(&option[shift], &option[shrink],
702 pdu->used_size - (option - pdu->token) - shrink);
703 if (!coap_opt_encode(option, pdu->alloc_size - pdu->used_size,
704 number - prev_number, data, len))
705 return 0;
706
707 if (shift >= shrink) {
708 pdu->used_size += shift - shrink;
709 if (pdu->data)
710 pdu->data += shift - shrink;
711 } else {
712 pdu->used_size -= shrink - shift;
713 if (pdu->data)
714 pdu->data -= shrink - shift;
715 }
716 return shift;
717}
718
719size_t
721 const uint8_t *data) {
722 coap_opt_iterator_t opt_iter;
723 coap_opt_t *option;
724 coap_option_t decode;
725 size_t new_length = 0;
726 size_t old_length = 0;
727
728 option = coap_check_option(pdu, number, &opt_iter);
729 if (!option)
730 return coap_insert_option(pdu, number, len, data);
731
732 old_length = coap_opt_parse(option, (size_t)-1, &decode);
733 if (old_length == 0)
734 return 0;
735 new_length = coap_opt_encode_size(decode.delta, len);
736
737 if (new_length > old_length) {
738 if (!coap_pdu_check_resize(pdu,
739 pdu->used_size + new_length - old_length))
740 return 0;
741 /* Possible a re-size took place with a realloc() */
742 option = coap_check_option(pdu, number, &opt_iter);
743 }
744
745 if (new_length != old_length)
746 memmove(&option[new_length], &option[old_length],
747 pdu->used_size - (option - pdu->token) - old_length);
748
749 if (!coap_opt_encode(option, new_length,
750 decode.delta, data, len))
751 return 0;
752
753 if (new_length >= old_length) {
754 pdu->used_size += new_length - old_length;
755 if (pdu->data)
756 pdu->data += new_length - old_length;
757 } else {
758 pdu->used_size -= old_length - new_length;
759 if (pdu->data)
760 pdu->data -= old_length - new_length;
761 }
762 return 1;
763}
764
765size_t
767 const uint8_t *data) {
768 if (pdu->data) {
769 coap_log_warn("coap_add_optlist_pdu: PDU already contains data\n");
770 return 0;
771 }
772 return coap_add_option_internal(pdu, number, len, data);
773}
774
775size_t
777 const uint8_t *data) {
778 size_t optsize;
779 coap_opt_t *opt;
780
781 assert(pdu);
782
783 if (number == pdu->max_opt) {
784 if (!coap_option_check_repeatable(number))
785 return 0;
786 }
787
788 if (COAP_PDU_IS_REQUEST(pdu) &&
789 (number == COAP_OPTION_PROXY_URI ||
790 number == COAP_OPTION_PROXY_SCHEME)) {
791 /*
792 * Need to check whether there is a hop-limit option. If not, it needs
793 * to be inserted by default (RFC 8768).
794 */
795 coap_opt_iterator_t opt_iter;
796
797 if (coap_check_option(pdu, COAP_OPTION_HOP_LIMIT, &opt_iter) == NULL) {
798 size_t hop_limit = COAP_OPTION_HOP_LIMIT;
799
800 coap_insert_option(pdu, COAP_OPTION_HOP_LIMIT, 1, (uint8_t *)&hop_limit);
801 }
802 }
803
804 if (number < pdu->max_opt) {
805 coap_log_debug("coap_add_option: options are not in correct order\n");
806 return coap_insert_option(pdu, number, len, data);
807 }
808
809 optsize = coap_opt_encode_size(number - pdu->max_opt, len);
810 if (!coap_pdu_check_resize(pdu,
811 pdu->used_size + optsize))
812 return 0;
813
814 if (pdu->data) {
815 /* include option delimiter */
816 memmove(&pdu->data[optsize-1], &pdu->data[-1],
817 pdu->used_size - (pdu->data - pdu->token) + 1);
818 opt = pdu->data -1;
819 pdu->data += optsize;
820 } else {
821 opt = pdu->token + pdu->used_size;
822 }
823
824 /* encode option and check length */
825 optsize = coap_opt_encode(opt, pdu->alloc_size - pdu->used_size,
826 number - pdu->max_opt, data, len);
827
828 if (!optsize) {
829 coap_log_warn("coap_add_option: cannot add option\n");
830 /* error */
831 return 0;
832 } else {
833 pdu->max_opt = number;
834 pdu->used_size += optsize;
835 }
836
837 return optsize;
838}
839
840int
841coap_add_data(coap_pdu_t *pdu, size_t len, const uint8_t *data) {
842 if (len == 0) {
843 return 1;
844 } else {
845 uint8_t *payload = coap_add_data_after(pdu, len);
846 if (payload != NULL)
847 memcpy(payload, data, len);
848 return payload != NULL;
849 }
850}
851
852uint8_t *
854 assert(pdu);
855 if (pdu->data) {
856 coap_log_warn("coap_add_data: PDU already contains data\n");
857 return 0;
858 }
859
860 if (len == 0)
861 return NULL;
862
863 if (!coap_pdu_resize(pdu, pdu->used_size + len + 1))
864 return 0;
865 pdu->token[pdu->used_size++] = COAP_PAYLOAD_START;
866 pdu->data = pdu->token + pdu->used_size;
867 pdu->used_size += len;
868 return pdu->data;
869}
870
871int
872coap_get_data(const coap_pdu_t *pdu, size_t *len, const uint8_t **data) {
873 size_t offset;
874 size_t total;
875
876 return coap_get_data_large(pdu, len, data, &offset, &total);
877}
878
879int
880coap_get_data_large(const coap_pdu_t *pdu, size_t *len, const uint8_t **data,
881 size_t *offset, size_t *total) {
882 assert(pdu);
883 assert(len);
884 assert(data);
885
886 *offset = pdu->body_offset;
887 *total = pdu->body_total;
888 if (pdu->body_data) {
889 *data = pdu->body_data;
890 *len = pdu->body_length;
891 return 1;
892 }
893 *data = pdu->data;
894 if (pdu->data == NULL) {
895 *len = 0;
896 *total = 0;
897 return 0;
898 }
899
900 *len = pdu->used_size - (pdu->data - pdu->token);
901 if (*total == 0)
902 *total = *len;
903
904 return 1;
905}
906
907#ifndef SHORT_ERROR_RESPONSE
908typedef struct {
909 unsigned char code;
910 const char *phrase;
912
913/* if you change anything here, make sure, that the longest string does not
914 * exceed COAP_ERROR_PHRASE_LENGTH. */
916 { COAP_RESPONSE_CODE(201), "Created" },
917 { COAP_RESPONSE_CODE(202), "Deleted" },
918 { COAP_RESPONSE_CODE(203), "Valid" },
919 { COAP_RESPONSE_CODE(204), "Changed" },
920 { COAP_RESPONSE_CODE(205), "Content" },
921 { COAP_RESPONSE_CODE(231), "Continue" },
922 { COAP_RESPONSE_CODE(400), "Bad Request" },
923 { COAP_RESPONSE_CODE(401), "Unauthorized" },
924 { COAP_RESPONSE_CODE(402), "Bad Option" },
925 { COAP_RESPONSE_CODE(403), "Forbidden" },
926 { COAP_RESPONSE_CODE(404), "Not Found" },
927 { COAP_RESPONSE_CODE(405), "Method Not Allowed" },
928 { COAP_RESPONSE_CODE(406), "Not Acceptable" },
929 { COAP_RESPONSE_CODE(408), "Request Entity Incomplete" },
930 { COAP_RESPONSE_CODE(409), "Conflict" },
931 { COAP_RESPONSE_CODE(412), "Precondition Failed" },
932 { COAP_RESPONSE_CODE(413), "Request Entity Too Large" },
933 { COAP_RESPONSE_CODE(415), "Unsupported Content-Format" },
934 { COAP_RESPONSE_CODE(422), "Unprocessable" },
935 { COAP_RESPONSE_CODE(429), "Too Many Requests" },
936 { COAP_RESPONSE_CODE(500), "Internal Server Error" },
937 { COAP_RESPONSE_CODE(501), "Not Implemented" },
938 { COAP_RESPONSE_CODE(502), "Bad Gateway" },
939 { COAP_RESPONSE_CODE(503), "Service Unavailable" },
940 { COAP_RESPONSE_CODE(504), "Gateway Timeout" },
941 { COAP_RESPONSE_CODE(505), "Proxying Not Supported" },
942 { COAP_RESPONSE_CODE(508), "Hop Limit Reached" },
943 { 0, NULL } /* end marker */
944};
945
946const char *
947coap_response_phrase(unsigned char code) {
948 int i;
949 for (i = 0; coap_error[i].code; ++i) {
950 if (coap_error[i].code == code)
951 return coap_error[i].phrase;
952 }
953 return NULL;
954}
955#endif
956
962static size_t
963next_option_safe(coap_opt_t **optp, size_t *length, uint16_t *max_opt) {
964 coap_option_t option;
965 size_t optsize;
966
967 assert(optp);
968 assert(*optp);
969 assert(length);
970
971 optsize = coap_opt_parse(*optp, *length, &option);
972 if (optsize) {
973 assert(optsize <= *length);
974
975 /* signal an error if this option would exceed the
976 * allowed number space */
977 if (*max_opt + option.delta > COAP_MAX_OPT) {
978 return 0;
979 }
980 *max_opt += option.delta;
981 *optp += optsize;
982 *length -= optsize;
983 }
984
985 return optsize;
986}
987
988size_t
990 const uint8_t *data) {
991 assert(data);
992 size_t header_size = 0;
993
994 if (proto == COAP_PROTO_TCP || proto==COAP_PROTO_TLS) {
995 uint8_t len = *data >> 4;
996 if (len < 13)
997 header_size = 2;
998 else if (len==13)
999 header_size = 3;
1000 else if (len==14)
1001 header_size = 4;
1002 else
1003 header_size = 6;
1004 } else if (proto == COAP_PROTO_WS || proto==COAP_PROTO_WSS) {
1005 header_size = 2;
1006 } else if (proto == COAP_PROTO_UDP || proto==COAP_PROTO_DTLS) {
1007 header_size = 4;
1008 }
1009
1010 return header_size;
1011}
1012
1013#if !COAP_DISABLE_TCP
1014/*
1015 * strm
1016 * return +ve PDU size including token
1017 * 0 PDU does not parse
1018 */
1019size_t
1021 const uint8_t *data,
1022 size_t length) {
1023 assert(data);
1024 assert(proto == COAP_PROTO_TCP || proto == COAP_PROTO_TLS ||
1025 proto == COAP_PROTO_WS || proto == COAP_PROTO_WSS);
1026 assert(coap_pdu_parse_header_size(proto, data) <= length);
1027
1028 size_t size = 0;
1029 const uint8_t *token_start = NULL;
1030
1031 if ((proto == COAP_PROTO_TCP || proto == COAP_PROTO_TLS) && length >= 1) {
1032 uint8_t len = *data >> 4;
1033 uint8_t tkl = *data & 0x0f;
1034
1035 if (len < 13) {
1036 size = len;
1037 token_start = &data[2];
1038 } else if (length >= 2) {
1039 if (len==13) {
1040 size = (size_t)data[1] + COAP_MESSAGE_SIZE_OFFSET_TCP8;
1041 token_start = &data[3];
1042 } else if (length >= 3) {
1043 if (len==14) {
1044 size = ((size_t)data[1] << 8) + data[2] + COAP_MESSAGE_SIZE_OFFSET_TCP16;
1045 token_start = &data[4];
1046 } else if (length >= 5) {
1047 size = ((size_t)data[1] << 24) + ((size_t)data[2] << 16)
1048 + ((size_t)data[3] << 8) + data[4] + COAP_MESSAGE_SIZE_OFFSET_TCP32;
1049 token_start = &data[6];
1050 }
1051 }
1052 }
1053 if (token_start) {
1054 /* account for the token length */
1055 if (tkl < COAP_TOKEN_EXT_1B_TKL) {
1056 size += tkl;
1057 } else if (tkl == COAP_TOKEN_EXT_1B_TKL) {
1058 size += token_start[0] + COAP_TOKEN_EXT_1B_BIAS + 1;
1059 } else if (tkl == COAP_TOKEN_EXT_2B_TKL) {
1060 size += ((uint16_t)token_start[0] << 8) + token_start[1] +
1062 } else {
1063 /* Invalid at this point - caught later as undersized */
1064 }
1065 }
1066 }
1067
1068 return size;
1069}
1070#endif /* ! COAP_DISABLE_TCP */
1071
1072int
1074 uint8_t *hdr = pdu->token - pdu->hdr_size;
1075 uint8_t e_token_length;
1076
1077 if (proto == COAP_PROTO_UDP || proto == COAP_PROTO_DTLS) {
1078 assert(pdu->hdr_size == 4);
1079 if ((hdr[0] >> 6) != COAP_DEFAULT_VERSION) {
1080 coap_log_debug("coap_pdu_parse: UDP version not supported\n");
1081 return 0;
1082 }
1083 pdu->type = (hdr[0] >> 4) & 0x03;
1084 pdu->code = hdr[1];
1085 pdu->mid = (uint16_t)hdr[2] << 8 | hdr[3];
1086 } else if (proto == COAP_PROTO_TCP || proto == COAP_PROTO_TLS) {
1087 assert(pdu->hdr_size >= 2 && pdu->hdr_size <= 6);
1088 pdu->type = COAP_MESSAGE_CON;
1089 pdu->code = hdr[pdu->hdr_size-1];
1090 pdu->mid = 0;
1091 } else if (proto == COAP_PROTO_WS || proto == COAP_PROTO_WSS) {
1092 assert(pdu->hdr_size == 2);
1093 pdu->type = COAP_MESSAGE_CON;
1094 pdu->code = hdr[pdu->hdr_size-1];
1095 pdu->mid = 0;
1096 } else {
1097 coap_log_debug("coap_pdu_parse: unsupported protocol\n");
1098 return 0;
1099 }
1100
1101 e_token_length = hdr[0] & 0x0f;
1102 if (e_token_length < COAP_TOKEN_EXT_1B_TKL) {
1103 pdu->e_token_length = e_token_length;
1105 pdu->actual_token.s = &pdu->token[0];
1106 } else if (e_token_length == COAP_TOKEN_EXT_1B_TKL) {
1107 pdu->e_token_length = pdu->token[0] + COAP_TOKEN_EXT_1B_BIAS + 1;
1108 pdu->actual_token.length = pdu->e_token_length - 1;
1109 pdu->actual_token.s = &pdu->token[1];
1110 } else if (e_token_length == COAP_TOKEN_EXT_2B_TKL) {
1111 pdu->e_token_length = ((uint16_t)pdu->token[0] << 8) + pdu->token[1] +
1113 pdu->actual_token.length = pdu->e_token_length - 2;
1114 pdu->actual_token.s = &pdu->token[2];
1115 }
1116 if (pdu->e_token_length > pdu->alloc_size || e_token_length == 15) {
1117 /* Invalid PDU provided - not wise to assert here though */
1118 coap_log_debug("coap_pdu_parse: PDU header token size broken\n");
1119 pdu->e_token_length = 0;
1120 pdu->actual_token.length = 0;
1121 return 0;
1122 }
1123 return 1;
1124}
1125
1126static int
1128 switch ((coap_pdu_signaling_proto_t)pdu->code) {
1129 case COAP_SIGNALING_CSM:
1130 switch (pdu->max_opt) {
1132 if (len > 4)
1133 goto bad;
1134 break;
1136 if (len > 0)
1137 goto bad;
1138 break;
1140 if (len > 3)
1141 goto bad;
1142 break;
1143 default:
1144 if (pdu->max_opt & 0x01)
1145 goto bad; /* Critical */
1146 }
1147 break;
1150 switch (pdu->max_opt) {
1152 if (len > 0)
1153 goto bad;
1154 break;
1155 default:
1156 if (pdu->max_opt & 0x01)
1157 goto bad; /* Critical */
1158 }
1159 break;
1161 switch (pdu->max_opt) {
1163 if (len < 1 || len > 255)
1164 goto bad;
1165 break;
1167 if (len > 3)
1168 goto bad;
1169 break;
1170 default:
1171 if (pdu->max_opt & 0x01)
1172 goto bad; /* Critical */
1173 }
1174 break;
1176 switch (pdu->max_opt) {
1178 if (len > 2)
1179 goto bad;
1180 break;
1181 default:
1182 if (pdu->max_opt & 0x01)
1183 goto bad; /* Critical */
1184 }
1185 break;
1186 default:
1187 ;
1188 }
1189 return 1;
1190bad:
1191 return 0;
1192}
1193
1194static int
1196 int res = 1;
1197
1198 switch (pdu->max_opt) {
1200 if (len > 8)
1201 res = 0;
1202 break;
1204 if (len < 1 || len > 255)
1205 res = 0;
1206 break;
1207 case COAP_OPTION_ETAG:
1208 if (len < 1 || len > 8)
1209 res = 0;
1210 break;
1212 if (len != 0)
1213 res = 0;
1214 break;
1216 if (len > 3)
1217 res = 0;
1218 break;
1220 if (len > 2)
1221 res = 0;
1222 break;
1224 if (len > 255)
1225 res = 0;
1226 break;
1227 case COAP_OPTION_OSCORE:
1228 if (len > 255)
1229 res = 0;
1230 break;
1232 if (len > 255)
1233 res = 0;
1234 break;
1236 if (len > 2)
1237 res = 0;
1238 break;
1239 case COAP_OPTION_MAXAGE:
1240 if (len > 4)
1241 res = 0;
1242 break;
1244 if (len < 1 || len > 255)
1245 res = 0;
1246 break;
1248 if (len != 1)
1249 res = 0;
1250 break;
1251 case COAP_OPTION_ACCEPT:
1252 if (len > 2)
1253 res = 0;
1254 break;
1256 if (len > 255)
1257 res = 0;
1258 break;
1259 case COAP_OPTION_BLOCK2:
1260 if (len > 3)
1261 res = 0;
1262 break;
1263 case COAP_OPTION_BLOCK1:
1264 if (len > 3)
1265 res = 0;
1266 break;
1267 case COAP_OPTION_SIZE2:
1268 if (len > 4)
1269 res = 0;
1270 break;
1272 if (len < 1 || len > 1034)
1273 res = 0;
1274 break;
1276 if (len < 1 || len > 255)
1277 res = 0;
1278 break;
1279 case COAP_OPTION_SIZE1:
1280 if (len > 4)
1281 res = 0;
1282 break;
1283 case COAP_OPTION_ECHO:
1284 if (len > 40)
1285 res = 0;
1286 break;
1288 if (len > 1)
1289 res = 0;
1290 break;
1291 case COAP_OPTION_RTAG:
1292 if (len > 8)
1293 res = 0;
1294 break;
1295 default:
1296 ;
1297 }
1298 return res;
1299}
1300
1301static int
1302write_prefix(char **obp, size_t *len, const char *prf, size_t prflen) {
1303 /* Make sure space for null terminating byte */
1304 if (*len < prflen +1) {
1305 return 0;
1306 }
1307
1308 memcpy(*obp, prf, prflen);
1309 *obp += prflen;
1310 *len -= prflen;
1311 return 1;
1312}
1313
1314static int
1315write_char(char **obp, size_t *len, int c, int printable) {
1316 /* Make sure space for null terminating byte */
1317 if (*len < 2 +1) {
1318 return 0;
1319 }
1320
1321 if (!printable) {
1322 const uint8_t hex[] = "0123456789abcdef";
1323 (*obp)[0] = hex[(c & 0xf0) >> 4];
1324 (*obp)[1] = hex[c & 0x0f];
1325 } else {
1326 (*obp)[0] = isprint(c) ? c : '.';
1327 (*obp)[1] = ' ';
1328 }
1329 *obp += 2;
1330 *len -= 2;
1331 return 1;
1332}
1333
1334int
1336 int good = 1;
1337
1338 /* sanity checks */
1339 if (pdu->code == 0) {
1340 if (pdu->used_size != 0 || pdu->e_token_length) {
1341 coap_log_debug("coap_pdu_parse: empty message is not empty\n");
1342 return 0;
1343 }
1344 }
1345
1346 if (pdu->e_token_length > pdu->used_size) {
1347 coap_log_debug("coap_pdu_parse: invalid Token\n");
1348 return 0;
1349 }
1350
1351 pdu->max_opt = 0;
1352 if (pdu->code == 0) {
1353 /* empty packet */
1354 pdu->used_size = 0;
1355 pdu->data = NULL;
1356 } else {
1357 /* skip header + token */
1358 coap_opt_t *opt = pdu->token + pdu->e_token_length;
1359 size_t length = pdu->used_size - pdu->e_token_length;
1360
1361 while (length > 0 && *opt != COAP_PAYLOAD_START) {
1362#if (COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_WARN)
1363 coap_opt_t *opt_last = opt;
1364#endif
1365 size_t optsize = next_option_safe(&opt, &length, &pdu->max_opt);
1366 const uint32_t len =
1367 optsize ? coap_opt_length((const uint8_t *)opt - optsize) : 0;
1368 if (optsize == 0) {
1369 coap_log_debug("coap_pdu_parse: %d.%02d: offset %u malformed option\n",
1370 pdu->code >> 5, pdu->code & 0x1F,
1371 (int)(opt_last - pdu->token - pdu->e_token_length));
1372 good = 0;
1373 break;
1374 }
1375 if (COAP_PDU_IS_SIGNALING(pdu) ?
1376 !coap_pdu_parse_opt_csm(pdu, len) :
1377 !coap_pdu_parse_opt_base(pdu, len)) {
1378 coap_log_warn("coap_pdu_parse: %d.%02d: offset %u option %u has bad length %" PRIu32 "\n",
1379 pdu->code >> 5, pdu->code & 0x1F,
1380 (int)(opt_last - pdu->token - pdu->e_token_length), pdu->max_opt,
1381 len);
1382 good = 0;
1383 }
1384 }
1385
1386 if (!good) {
1387 /*
1388 * Dump the options in the PDU for analysis, space separated except
1389 * error options which are prefixed by *
1390 * Two rows - hex and ascii (if printable)
1391 */
1392 static char outbuf[COAP_DEBUG_BUF_SIZE];
1393 char *obp;
1394 size_t tlen;
1395 size_t outbuflen;
1396 int i;
1397 int ok;
1398
1399 for (i = 0; i < 2; i++) {
1400 opt = pdu->token + pdu->e_token_length;
1401 length = pdu->used_size - pdu->e_token_length;
1402 pdu->max_opt = 0;
1403
1404 outbuflen = sizeof(outbuf);
1405 obp = outbuf;
1406 ok = write_prefix(&obp, &outbuflen, "O: ", 3);
1407 /*
1408 * Not safe to check for 'ok' here as a lot of variables may get
1409 * partially changed due to lack of outbuflen */
1410 while (length > 0 && *opt != COAP_PAYLOAD_START) {
1411 coap_opt_t *opt_last = opt;
1412 size_t optsize = next_option_safe(&opt, &length, &pdu->max_opt);
1413 const uint32_t len =
1414 optsize ? coap_opt_length((const uint8_t *)opt - optsize) : 0;
1415 if (!optsize || (COAP_PDU_IS_SIGNALING(pdu) ?
1416 !coap_pdu_parse_opt_csm(pdu, len) :
1417 !coap_pdu_parse_opt_base(pdu, len))) {
1418 ok = ok && write_prefix(&obp, &outbuflen, "*", 1);
1419 if (!optsize) {
1420 /* Skip to end of options to output all data */
1421 opt = pdu->token + pdu->used_size;
1422 length = 0;
1423 }
1424 } else {
1425 ok = ok && write_prefix(&obp, &outbuflen, " ", 1);
1426 }
1427 tlen = opt - opt_last;
1428 while (tlen--) {
1429 ok = ok && write_char(&obp, &outbuflen, *opt_last, i);
1430 opt_last++;
1431 }
1432 }
1433 if (length && *opt == COAP_PAYLOAD_START) {
1434 write_char(&obp, &outbuflen, *opt, i);
1435 }
1436 /* write_*() always leaves a spare byte to null terminate */
1437 *obp = '\000';
1438 coap_log_debug("%s\n", outbuf);
1439 }
1440 }
1441
1442 if (length > 0) {
1443 assert(*opt == COAP_PAYLOAD_START);
1444 opt++;
1445 length--;
1446
1447 if (length == 0) {
1448 coap_log_debug("coap_pdu_parse: message ending in payload start marker\n");
1449 return 0;
1450 }
1451 }
1452 if (length > 0)
1453 pdu->data = (uint8_t *)opt;
1454 else
1455 pdu->data = NULL;
1456 }
1457
1458 return good;
1459}
1460
1461int
1463 const uint8_t *data,
1464 size_t length,
1465 coap_pdu_t *pdu) {
1466 size_t hdr_size;
1467
1468 if (length == 0)
1469 return 0;
1470 hdr_size = coap_pdu_parse_header_size(proto, data);
1471 if (!hdr_size || hdr_size > length)
1472 return 0;
1473 if (hdr_size > pdu->max_hdr_size)
1474 return 0;
1475 if (!coap_pdu_resize(pdu, length - hdr_size))
1476 return 0;
1477 if (pdu->token - hdr_size != data)
1478 memcpy(pdu->token - hdr_size, data, length);
1479 pdu->hdr_size = (uint8_t)hdr_size;
1480 pdu->used_size = length - hdr_size;
1481 return coap_pdu_parse_header(pdu, proto) && coap_pdu_parse_opt(pdu);
1482}
1483
1484size_t
1486 uint8_t e_token_length;
1487
1489 e_token_length = (uint8_t)pdu->actual_token.length;
1490 } else if (pdu->actual_token.length < COAP_TOKEN_EXT_2B_BIAS) {
1491 e_token_length = COAP_TOKEN_EXT_1B_TKL;
1492 } else if (pdu->actual_token.length <= COAP_TOKEN_EXT_MAX) {
1493 e_token_length = COAP_TOKEN_EXT_2B_TKL;
1494 } else {
1495 coap_log_warn("coap_add_token: Token size too large. PDU ignored\n");
1496 return 0;
1497 }
1498 if (COAP_PROTO_NOT_RELIABLE(proto)) {
1499 assert(pdu->max_hdr_size >= 4);
1500 if (pdu->max_hdr_size < 4) {
1501 coap_log_warn("coap_pdu_encode_header: not enough space for UDP-style header\n");
1502 return 0;
1503 }
1504 pdu->token[-4] = COAP_DEFAULT_VERSION << 6
1505 | pdu->type << 4
1506 | e_token_length;
1507 pdu->token[-3] = pdu->code;
1508 pdu->token[-2] = (uint8_t)(pdu->mid >> 8);
1509 pdu->token[-1] = (uint8_t)(pdu->mid);
1510 pdu->hdr_size = 4;
1511#if !COAP_DISABLE_TCP
1512 } else if (COAP_PROTO_RELIABLE(proto)) {
1513 size_t len;
1514 assert(pdu->used_size >= pdu->e_token_length);
1515 if (pdu->used_size < pdu->e_token_length) {
1516 coap_log_warn("coap_pdu_encode_header: corrupted PDU\n");
1517 return 0;
1518 }
1519
1520 /* A lot of the reliable code assumes type is CON */
1521 if (pdu->type != COAP_MESSAGE_CON)
1522 pdu->type = COAP_MESSAGE_CON;
1523
1524 if (proto == COAP_PROTO_WS || proto == COAP_PROTO_WSS)
1525 len = 0;
1526 else
1527 len = pdu->used_size - pdu->e_token_length;
1528 if (len <= COAP_MAX_MESSAGE_SIZE_TCP0) {
1529 assert(pdu->max_hdr_size >= 2);
1530 if (pdu->max_hdr_size < 2) {
1531 coap_log_warn("coap_pdu_encode_header: not enough space for TCP0 header\n");
1532 return 0;
1533 }
1534 pdu->token[-2] = (uint8_t)len << 4
1535 | e_token_length;
1536 pdu->token[-1] = pdu->code;
1537 pdu->hdr_size = 2;
1538 } else if (len <= COAP_MAX_MESSAGE_SIZE_TCP8) {
1539 assert(pdu->max_hdr_size >= 3);
1540 if (pdu->max_hdr_size < 3) {
1541 coap_log_warn("coap_pdu_encode_header: not enough space for TCP8 header\n");
1542 return 0;
1543 }
1544 pdu->token[-3] = 13 << 4 | e_token_length;
1545 pdu->token[-2] = (uint8_t)(len - COAP_MESSAGE_SIZE_OFFSET_TCP8);
1546 pdu->token[-1] = pdu->code;
1547 pdu->hdr_size = 3;
1548 } else if (len <= COAP_MAX_MESSAGE_SIZE_TCP16) {
1549 assert(pdu->max_hdr_size >= 4);
1550 if (pdu->max_hdr_size < 4) {
1551 coap_log_warn("coap_pdu_encode_header: not enough space for TCP16 header\n");
1552 return 0;
1553 }
1554 pdu->token[-4] = 14 << 4 | e_token_length;
1555 pdu->token[-3] = (uint8_t)((len - COAP_MESSAGE_SIZE_OFFSET_TCP16) >> 8);
1556 pdu->token[-2] = (uint8_t)(len - COAP_MESSAGE_SIZE_OFFSET_TCP16);
1557 pdu->token[-1] = pdu->code;
1558 pdu->hdr_size = 4;
1559 } else {
1560 assert(pdu->max_hdr_size >= 6);
1561 if (pdu->max_hdr_size < 6) {
1562 coap_log_warn("coap_pdu_encode_header: not enough space for TCP32 header\n");
1563 return 0;
1564 }
1565 pdu->token[-6] = 15 << 4 | e_token_length;
1566 pdu->token[-5] = (uint8_t)((len - COAP_MESSAGE_SIZE_OFFSET_TCP32) >> 24);
1567 pdu->token[-4] = (uint8_t)((len - COAP_MESSAGE_SIZE_OFFSET_TCP32) >> 16);
1568 pdu->token[-3] = (uint8_t)((len - COAP_MESSAGE_SIZE_OFFSET_TCP32) >> 8);
1569 pdu->token[-2] = (uint8_t)(len - COAP_MESSAGE_SIZE_OFFSET_TCP32);
1570 pdu->token[-1] = pdu->code;
1571 pdu->hdr_size = 6;
1572 }
1573#endif /* ! COAP_DISABLE_TCP */
1574 } else {
1575 coap_log_warn("coap_pdu_encode_header: unsupported protocol\n");
1576 }
1577 return pdu->hdr_size;
1578}
1579
1582 return pdu->code;
1583}
1584
1585void
1587#ifndef RIOT_VERSION
1588 assert(code <= 0xff);
1589#endif /* RIOT_VERSION */
1590 pdu->code = code;
1591}
1592
1595 return pdu->type;
1596}
1597
1598void
1600 assert(type <= 0x3);
1601 pdu->type = type;
1602}
1603
1606 return pdu->actual_token;
1607}
1608
1611 return pdu->mid;
1612}
1613
1614void
1616#if (UINT_MAX > 65535)
1617 assert(mid >= 0 && mid <= 0xffff);
1618#endif /* UINT_MAX > 65535 */
1619 pdu->mid = mid;
1620}
1621
1622coap_pdu_t *
1624 if (pdu != NULL) {
1625 ++pdu->ref;
1626 }
1627 return pdu;
1628}
#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:963
static int coap_pdu_parse_opt_csm(coap_pdu_t *pdu, uint16_t len)
Definition coap_pdu.c:1127
error_desc_t coap_error[]
Definition coap_pdu.c:915
static int write_prefix(char **obp, size_t *len, const char *prf, size_t prflen)
Definition coap_pdu.c:1302
static int coap_pdu_parse_opt_base(coap_pdu_t *pdu, uint16_t len)
Definition coap_pdu.c:1195
#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:1315
#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.
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.
#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:1623
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:626
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_header(coap_pdu_t *pdu, coap_proto_t proto)
Decode the protocol specific header for the specified PDU.
Definition coap_pdu.c:1073
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:989
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
int coap_pdu_parse_opt(coap_pdu_t *pdu)
Verify consistency in the given CoAP PDU structure and locate the data.
Definition coap_pdu.c:1335
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:720
#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:1485
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
#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:1020
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:776
#define COAP_OPTION_HOP_LIMIT
Definition coap_pdu.h:133
#define COAP_OPTION_NORESPONSE
Definition coap_pdu.h:145
#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:1581
#define COAP_OPTION_BLOCK2
Definition coap_pdu.h:137
const char * coap_response_phrase(unsigned char code)
Returns a human-readable response phrase for the specified CoAP response code.
Definition coap_pdu.c:947
#define COAP_OPTION_CONTENT_FORMAT
Definition coap_pdu.h:128
#define COAP_SIGNALING_OPTION_ALTERNATIVE_ADDRESS
Definition coap_pdu.h:205
#define COAP_OPTION_SIZE2
Definition coap_pdu.h:139
#define COAP_OPTION_BLOCK1
Definition coap_pdu.h:138
#define COAP_OPTION_PROXY_SCHEME
Definition coap_pdu.h:142
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:853
#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:1586
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition coap_pdu.h:263
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:199
#define COAP_RESPONSE_CODE(N)
Definition coap_pdu.h:160
coap_proto_t
CoAP protocol types.
Definition coap_pdu.h:312
coap_pdu_code_t
Set of codes available for a PDU.
Definition coap_pdu.h:326
#define COAP_OPTION_OSCORE
Definition coap_pdu.h:126
#define COAP_OPTION_SIZE1
Definition coap_pdu.h:143
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:198
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:1599
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:766
#define COAP_SIGNALING_OPTION_CUSTODY
Definition coap_pdu.h:202
coap_pdu_signaling_proto_t
Definition coap_pdu.h:188
coap_pdu_type_t coap_pdu_get_type(const coap_pdu_t *pdu)
Gets the PDU type associated with pdu.
Definition coap_pdu.c:1594
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:872
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:1462
#define COAP_MAX_OPT
the highest option number we know
Definition coap_pdu.h:151
void coap_pdu_set_mid(coap_pdu_t *pdu, coap_mid_t mid)
Sets the message id in the pdu.
Definition coap_pdu.c:1615
#define COAP_OPTION_RTAG
Definition coap_pdu.h:146
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_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:880
coap_mid_t coap_pdu_get_mid(const coap_pdu_t *pdu)
Gets the message id associated with pdu.
Definition coap_pdu.c:1610
#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:141
#define COAP_OPTION_OBSERVE
Definition coap_pdu.h:123
#define COAP_SIGNALING_OPTION_HOLD_OFF
Definition coap_pdu.h:206
#define COAP_OPTION_ECHO
Definition coap_pdu.h:144
#define COAP_SIGNALING_OPTION_BAD_CSM_OPTION
Definition coap_pdu.h:209
#define COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE
Definition coap_pdu.h:197
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:841
coap_bin_const_t coap_pdu_get_token(const coap_pdu_t *pdu)
Gets the token associated with pdu.
Definition coap_pdu.c:1605
@ COAP_PROTO_WS
Definition coap_pdu.h:318
@ COAP_PROTO_DTLS
Definition coap_pdu.h:315
@ COAP_PROTO_UDP
Definition coap_pdu.h:314
@ COAP_PROTO_TLS
Definition coap_pdu.h:317
@ COAP_PROTO_WSS
Definition coap_pdu.h:319
@ COAP_PROTO_TCP
Definition coap_pdu.h:316
@ COAP_MESSAGE_CON
Definition coap_pdu.h:69
@ COAP_SIGNALING_RELEASE
Definition coap_pdu.h:192
@ COAP_SIGNALING_CSM
Definition coap_pdu.h:189
@ COAP_SIGNALING_PONG
Definition coap_pdu.h:191
@ COAP_SIGNALING_PING
Definition coap_pdu.h:190
@ COAP_SIGNALING_ABORT
Definition coap_pdu.h:193
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:909
const char * phrase
Definition coap_pdu.c:910