libcoap 4.3.5b
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
15
17
18#if defined(HAVE_LIMITS_H)
19#include <limits.h>
20#endif
21
22#include <stdlib.h>
23#include <stdio.h>
24#include <string.h>
25#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 defined(WITH_CONTIKI) || defined(WITH_LWIP)
124 assert(size <= COAP_DEFAULT_MAX_PDU_RX_SIZE);
125 if (size > COAP_DEFAULT_MAX_PDU_RX_SIZE)
126 return NULL;
128#else
130#endif
131
132#ifdef WITH_LWIP
133 pdu->pbuf = pbuf_alloc(PBUF_TRANSPORT, size + pdu->max_hdr_size, PBUF_RAM);
134 if (pdu->pbuf == NULL) {
136 return NULL;
137 }
138 pdu->token = (uint8_t *)pdu->pbuf->payload + pdu->max_hdr_size;
139#else /* WITH_LWIP */
140 uint8_t *buf;
141 pdu->alloc_size = min(size, 256);
143 if (buf == NULL) {
145 return NULL;
146 }
147 pdu->token = buf + pdu->max_hdr_size;
148#endif /* WITH_LWIP */
149 coap_pdu_clear(pdu, size);
150 pdu->mid = mid;
151 pdu->type = type;
152 pdu->code = code;
153 return pdu;
154}
155
158 coap_session_t *session) {
159 coap_pdu_t *pdu;
160
161 coap_lock_lock(session->context, return NULL);
162 pdu = coap_new_pdu_lkd(type, code, session);
163 coap_lock_unlock(session->context);
164 return pdu;
165}
166
169 coap_session_t *session) {
170 coap_pdu_t *pdu;
171
173 pdu = coap_pdu_init(type, code, coap_new_message_id_lkd(session),
175 if (!pdu)
176 coap_log_crit("coap_new_pdu: cannot allocate memory for new PDU\n");
177 return pdu;
178}
179
180COAP_API void
182 coap_lock_lock(NULL, return);
184 coap_lock_unlock(NULL);
185}
186
187void
189 if (pdu != NULL) {
190 if (pdu->ref) {
191 pdu->ref--;
192 return;
193 }
194#ifdef WITH_LWIP
195 pbuf_free(pdu->pbuf);
196#else
197 if (pdu->token != NULL)
199#endif
202 }
203}
204
207 coap_session_t *session,
208 size_t token_length,
209 const uint8_t *token,
210 coap_opt_filter_t *drop_options) {
211 coap_pdu_t *new_pdu;
212
213 coap_lock_lock(session->context, return NULL);
214 new_pdu = coap_pdu_duplicate_lkd(old_pdu,
215 session,
216 token_length,
217 token,
218 drop_options);
219 coap_lock_unlock(session->context);
220 return new_pdu;
221}
222
223
224/*
225 * Note: This does not include any data, just the token and options
226 */
229 coap_session_t *session,
230 size_t token_length,
231 const uint8_t *token,
232 coap_opt_filter_t *drop_options) {
233 uint8_t doing_first = session->doing_first;
234 coap_pdu_t *pdu;
235
237 /*
238 * Need to make sure that coap_session_max_pdu_size_lkd() immediately
239 * returns, rather than wait for the first CSM response from remote
240 * that indicates BERT size (TCP/TLS only) as this may be called early
241 * the OSCORE logic.
242 */
243 session->doing_first = 0;
244 pdu = coap_pdu_init(old_pdu->type, old_pdu->code,
246 max(old_pdu->max_size,
248 /* Restore any pending waits */
249 session->doing_first = doing_first;
250 if (pdu == NULL)
251 return NULL;
252
253 coap_add_token(pdu, token_length, token);
254 pdu->lg_xmit = old_pdu->lg_xmit;
255
256 if (drop_options == NULL) {
257 /* Drop COAP_PAYLOAD_START as well if data */
258 size_t length = old_pdu->used_size - old_pdu->e_token_length -
259 (old_pdu->data ?
260 old_pdu->used_size - (old_pdu->data - old_pdu->token) +1 : 0);
261 if (!coap_pdu_resize(pdu, length + pdu->e_token_length))
262 goto fail;
263 /* Copy the options but not any data across */
264 memcpy(pdu->token + pdu->e_token_length,
265 old_pdu->token + old_pdu->e_token_length, length);
266 pdu->used_size += length;
267 pdu->max_opt = old_pdu->max_opt;
268 } else {
269 /* Copy across all the options the slow way */
270 coap_opt_iterator_t opt_iter;
271 coap_opt_t *option;
272
273 coap_option_iterator_init(old_pdu, &opt_iter, COAP_OPT_ALL);
274 while ((option = coap_option_next(&opt_iter))) {
275 if (drop_options && coap_option_filter_get(drop_options, opt_iter.number))
276 continue;
277 if (!coap_add_option_internal(pdu, opt_iter.number,
278 coap_opt_length(option),
279 coap_opt_value(option)))
280 goto fail;
281 }
282 }
283 return pdu;
284
285fail:
287 return NULL;
288}
289
290
291/*
292 * The new size does not include the coap header (max_hdr_size)
293 */
294int
295coap_pdu_resize(coap_pdu_t *pdu, size_t new_size) {
296 if (new_size > pdu->alloc_size) {
297 /* Expanding the PDU usage */
298#if !defined(WITH_LWIP)
299 uint8_t *new_hdr;
300 size_t offset;
301#endif
302
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 pdu->alloc_size = new_size;
334 }
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 if (option == NULL) {
649 /* Code is broken somewhere */
650 coap_log_warn("coap_insert_option: Broken max_opt\n");
651 return 0;
652 }
653
654 /* size of option inc header to insert */
655 shift = coap_opt_encode_size(number - prev_number, len);
656
657 /* size of next option (header may shrink in size as delta changes */
658 if (!coap_opt_parse(option, pdu->used_size - (option - pdu->token), &decode))
659 return 0;
660 opt_delta = opt_iter.number - number;
661 if (opt_delta == 0) {
662 if (!coap_option_check_repeatable(number))
663 return 0;
664 }
665
666 if (!coap_pdu_check_resize(pdu,
667 pdu->used_size + shift - shrink))
668 return 0;
669
670 /* Possible a re-size took place with a realloc() */
671 /* Need to locate where in current options to insert this one */
673 while ((option = coap_option_next(&opt_iter))) {
674 if (opt_iter.number > number) {
675 /* Found where to insert */
676 break;
677 }
678 }
679 assert(option != NULL);
680
681 if (decode.delta < 13) {
682 /* can simply patch in the new delta of next option */
683 option[0] = (option[0] & 0x0f) + (coap_opt_t)(opt_delta << 4);
684 } else if (decode.delta < 269 && opt_delta < 13) {
685 /* option header is going to shrink by one byte */
686 option[1] = (option[0] & 0x0f) + (coap_opt_t)(opt_delta << 4);
687 shrink = 1;
688 } else if (decode.delta < 269 && opt_delta < 269) {
689 /* can simply patch in the new delta of next option */
690 option[1] = (coap_opt_t)(opt_delta - 13);
691 } else if (opt_delta < 13) {
692 /* option header is going to shrink by two bytes */
693 option[2] = (option[0] & 0x0f) + (coap_opt_t)(opt_delta << 4);
694 shrink = 2;
695 } else if (opt_delta < 269) {
696 /* option header is going to shrink by one bytes */
697 option[1] = (option[0] & 0x0f) + 0xd0;
698 option[2] = (coap_opt_t)(opt_delta - 13);
699 shrink = 1;
700 } else {
701 /* can simply patch in the new delta of next option */
702 option[1] = (coap_opt_t)((opt_delta - 269) >> 8);
703 option[2] = (opt_delta - 269) & 0xff;
704 }
705
706 memmove(&option[shift], &option[shrink],
707 pdu->used_size - (option - pdu->token) - shrink);
708 if (!coap_opt_encode(option, pdu->alloc_size - pdu->used_size,
709 number - prev_number, data, len))
710 return 0;
711
712 if (shift >= shrink) {
713 pdu->used_size += shift - shrink;
714 if (pdu->data)
715 pdu->data += shift - shrink;
716 } else {
717 pdu->used_size -= shrink - shift;
718 if (pdu->data)
719 pdu->data -= shrink - shift;
720 }
721 return shift;
722}
723
724size_t
726 const uint8_t *data) {
727 coap_opt_iterator_t opt_iter;
728 coap_opt_t *option;
729 coap_option_t decode;
730 size_t new_length = 0;
731 size_t old_length = 0;
732
733 option = coap_check_option(pdu, number, &opt_iter);
734 if (!option)
735 return coap_insert_option(pdu, number, len, data);
736
737 old_length = coap_opt_parse(option, (size_t)-1, &decode);
738 if (old_length == 0)
739 return 0;
740 new_length = coap_opt_encode_size(decode.delta, len);
741
742 if (new_length > old_length) {
743 if (!coap_pdu_check_resize(pdu,
744 pdu->used_size + new_length - old_length))
745 return 0;
746 /* Possible a re-size took place with a realloc() */
747 option = coap_check_option(pdu, number, &opt_iter);
748 }
749
750 if (new_length != old_length)
751 memmove(&option[new_length], &option[old_length],
752 pdu->used_size - (option - pdu->token) - old_length);
753
754 if (!coap_opt_encode(option, new_length,
755 decode.delta, data, len))
756 return 0;
757
758 if (new_length >= old_length) {
759 pdu->used_size += new_length - old_length;
760 if (pdu->data)
761 pdu->data += new_length - old_length;
762 } else {
763 pdu->used_size -= old_length - new_length;
764 if (pdu->data)
765 pdu->data -= old_length - new_length;
766 }
767 return 1;
768}
769
770size_t
772 const uint8_t *data) {
773 if (pdu->data) {
774 coap_log_warn("coap_add_optlist_pdu: PDU already contains data\n");
775 return 0;
776 }
777 return coap_add_option_internal(pdu, number, len, data);
778}
779
780size_t
782 const uint8_t *data) {
783 size_t optsize;
784 coap_opt_t *opt;
785
786 assert(pdu);
787
788 if (number == pdu->max_opt) {
789 if (!coap_option_check_repeatable(number))
790 return 0;
791 }
792
793 if (COAP_PDU_IS_REQUEST(pdu) &&
794 (number == COAP_OPTION_PROXY_URI ||
795 number == COAP_OPTION_PROXY_SCHEME)) {
796 /*
797 * Need to check whether there is a hop-limit option. If not, it needs
798 * to be inserted by default (RFC 8768).
799 */
800 coap_opt_iterator_t opt_iter;
801
802 if (coap_check_option(pdu, COAP_OPTION_HOP_LIMIT, &opt_iter) == NULL) {
803 size_t hop_limit = COAP_OPTION_HOP_LIMIT;
804
805 coap_insert_option(pdu, COAP_OPTION_HOP_LIMIT, 1, (uint8_t *)&hop_limit);
806 }
807 }
808
809 if (number < pdu->max_opt) {
810 coap_log_debug("coap_add_option: options are not in correct order\n");
811 return coap_insert_option(pdu, number, len, data);
812 }
813
814 optsize = coap_opt_encode_size(number - pdu->max_opt, len);
815 if (!coap_pdu_check_resize(pdu,
816 pdu->used_size + optsize))
817 return 0;
818
819 if (pdu->data) {
820 /* include option delimiter */
821 memmove(&pdu->data[optsize-1], &pdu->data[-1],
822 pdu->used_size - (pdu->data - pdu->token) + 1);
823 opt = pdu->data -1;
824 pdu->data += optsize;
825 } else {
826 opt = pdu->token + pdu->used_size;
827 }
828
829 /* encode option and check length */
830 optsize = coap_opt_encode(opt, pdu->alloc_size - pdu->used_size,
831 number - pdu->max_opt, data, len);
832
833 if (!optsize) {
834 coap_log_warn("coap_add_option: cannot add option\n");
835 /* error */
836 return 0;
837 } else {
838 pdu->max_opt = number;
839 pdu->used_size += optsize;
840 }
841
842 return optsize;
843}
844
845int
846coap_add_data(coap_pdu_t *pdu, size_t len, const uint8_t *data) {
847 if (len == 0) {
848 return 1;
849 } else {
850 uint8_t *payload = coap_add_data_after(pdu, len);
851 if (payload != NULL)
852 memcpy(payload, data, len);
853 return payload != NULL;
854 }
855}
856
857uint8_t *
859 assert(pdu);
860 if (pdu->data) {
861 coap_log_warn("coap_add_data: PDU already contains data\n");
862 return 0;
863 }
864
865 if (len == 0)
866 return NULL;
867
868 if (!coap_pdu_resize(pdu, pdu->used_size + len + 1))
869 return 0;
870 pdu->token[pdu->used_size++] = COAP_PAYLOAD_START;
871 pdu->data = pdu->token + pdu->used_size;
872 pdu->used_size += len;
873 return pdu->data;
874}
875
876int
877coap_get_data(const coap_pdu_t *pdu, size_t *len, const uint8_t **data) {
878 size_t offset;
879 size_t total;
880
881 return coap_get_data_large(pdu, len, data, &offset, &total);
882}
883
884int
885coap_get_data_large(const coap_pdu_t *pdu, size_t *len, const uint8_t **data,
886 size_t *offset, size_t *total) {
887 assert(pdu);
888 assert(len);
889 assert(data);
890
891 *offset = pdu->body_offset;
892 *total = pdu->body_total;
893 if (pdu->body_data) {
894 *data = pdu->body_data;
895 *len = pdu->body_length;
896 return 1;
897 }
898 *data = pdu->data;
899 if (pdu->data == NULL) {
900 *len = 0;
901 *total = 0;
902 return 0;
903 }
904
905 *len = pdu->used_size - (pdu->data - pdu->token);
906 if (*total == 0)
907 *total = *len;
908
909 return 1;
910}
911
912#ifndef SHORT_ERROR_RESPONSE
913typedef struct {
914 unsigned char code;
915 const char *phrase;
917
918/* if you change anything here, make sure, that the longest string does not
919 * exceed COAP_ERROR_PHRASE_LENGTH. */
921 { COAP_RESPONSE_CODE(201), "Created" },
922 { COAP_RESPONSE_CODE(202), "Deleted" },
923 { COAP_RESPONSE_CODE(203), "Valid" },
924 { COAP_RESPONSE_CODE(204), "Changed" },
925 { COAP_RESPONSE_CODE(205), "Content" },
926 { COAP_RESPONSE_CODE(231), "Continue" },
927 { COAP_RESPONSE_CODE(400), "Bad Request" },
928 { COAP_RESPONSE_CODE(401), "Unauthorized" },
929 { COAP_RESPONSE_CODE(402), "Bad Option" },
930 { COAP_RESPONSE_CODE(403), "Forbidden" },
931 { COAP_RESPONSE_CODE(404), "Not Found" },
932 { COAP_RESPONSE_CODE(405), "Method Not Allowed" },
933 { COAP_RESPONSE_CODE(406), "Not Acceptable" },
934 { COAP_RESPONSE_CODE(408), "Request Entity Incomplete" },
935 { COAP_RESPONSE_CODE(409), "Conflict" },
936 { COAP_RESPONSE_CODE(412), "Precondition Failed" },
937 { COAP_RESPONSE_CODE(413), "Request Entity Too Large" },
938 { COAP_RESPONSE_CODE(415), "Unsupported Content-Format" },
939 { COAP_RESPONSE_CODE(422), "Unprocessable" },
940 { COAP_RESPONSE_CODE(429), "Too Many Requests" },
941 { COAP_RESPONSE_CODE(500), "Internal Server Error" },
942 { COAP_RESPONSE_CODE(501), "Not Implemented" },
943 { COAP_RESPONSE_CODE(502), "Bad Gateway" },
944 { COAP_RESPONSE_CODE(503), "Service Unavailable" },
945 { COAP_RESPONSE_CODE(504), "Gateway Timeout" },
946 { COAP_RESPONSE_CODE(505), "Proxying Not Supported" },
947 { COAP_RESPONSE_CODE(508), "Hop Limit Reached" },
948 { 0, NULL } /* end marker */
949};
950
951const char *
952coap_response_phrase(unsigned char code) {
953 int i;
954 for (i = 0; coap_error[i].code; ++i) {
955 if (coap_error[i].code == code)
956 return coap_error[i].phrase;
957 }
958 return NULL;
959}
960#endif
961
967static size_t
968next_option_safe(coap_opt_t **optp, size_t *length, uint16_t *max_opt) {
969 coap_option_t option;
970 size_t optsize;
971
972 assert(optp);
973 assert(*optp);
974 assert(length);
975
976 optsize = coap_opt_parse(*optp, *length, &option);
977 if (optsize) {
978 assert(optsize <= *length);
979
980 /* signal an error if this option would exceed the
981 * allowed number space */
982 if (*max_opt + option.delta > COAP_MAX_OPT) {
983 return 0;
984 }
985 *max_opt += option.delta;
986 *optp += optsize;
987 *length -= optsize;
988 }
989
990 return optsize;
991}
992
993size_t
995 const uint8_t *data) {
996 assert(data);
997 size_t header_size = 0;
998
999 if (proto == COAP_PROTO_TCP || proto==COAP_PROTO_TLS) {
1000 uint8_t len = *data >> 4;
1001 if (len < 13)
1002 header_size = 2;
1003 else if (len==13)
1004 header_size = 3;
1005 else if (len==14)
1006 header_size = 4;
1007 else
1008 header_size = 6;
1009 } else if (proto == COAP_PROTO_WS || proto==COAP_PROTO_WSS) {
1010 header_size = 2;
1011 } else if (proto == COAP_PROTO_UDP || proto==COAP_PROTO_DTLS) {
1012 header_size = 4;
1013 }
1014
1015 return header_size;
1016}
1017
1018#if !COAP_DISABLE_TCP
1019/*
1020 * strm
1021 * return +ve PDU size including token
1022 * 0 PDU does not parse
1023 */
1024size_t
1026 const uint8_t *data,
1027 size_t length) {
1028 assert(data);
1029 assert(proto == COAP_PROTO_TCP || proto == COAP_PROTO_TLS ||
1030 proto == COAP_PROTO_WS || proto == COAP_PROTO_WSS);
1031 assert(coap_pdu_parse_header_size(proto, data) <= length);
1032
1033 size_t size = 0;
1034 const uint8_t *token_start = NULL;
1035
1036 if ((proto == COAP_PROTO_TCP || proto == COAP_PROTO_TLS) && length >= 1) {
1037 uint8_t len = *data >> 4;
1038 uint8_t tkl = *data & 0x0f;
1039
1040 if (len < 13) {
1041 size = len;
1042 token_start = &data[2];
1043 } else if (length >= 2) {
1044 if (len==13) {
1045 size = (size_t)data[1] + COAP_MESSAGE_SIZE_OFFSET_TCP8;
1046 token_start = &data[3];
1047 } else if (length >= 3) {
1048 if (len==14) {
1049 size = ((size_t)data[1] << 8) + data[2] + COAP_MESSAGE_SIZE_OFFSET_TCP16;
1050 token_start = &data[4];
1051 } else if (length >= 5) {
1052 size = ((size_t)data[1] << 24) + ((size_t)data[2] << 16)
1053 + ((size_t)data[3] << 8) + data[4] + COAP_MESSAGE_SIZE_OFFSET_TCP32;
1054 token_start = &data[6];
1055 }
1056 }
1057 }
1058 if (token_start) {
1059 /* account for the token length */
1060 if (tkl < COAP_TOKEN_EXT_1B_TKL) {
1061 size += tkl;
1062 } else if (tkl == COAP_TOKEN_EXT_1B_TKL) {
1063 size += token_start[0] + COAP_TOKEN_EXT_1B_BIAS + 1;
1064 } else if (tkl == COAP_TOKEN_EXT_2B_TKL) {
1065 size += ((uint16_t)token_start[0] << 8) + token_start[1] +
1067 } else {
1068 /* Invalid at this point - caught later as undersized */
1069 }
1070 }
1071 }
1072
1073 return size;
1074}
1075#endif /* ! COAP_DISABLE_TCP */
1076
1077int
1079 uint8_t *hdr = pdu->token - pdu->hdr_size;
1080 uint8_t e_token_length;
1081
1082 if (proto == COAP_PROTO_UDP || proto == COAP_PROTO_DTLS) {
1083 assert(pdu->hdr_size == 4);
1084 if ((hdr[0] >> 6) != COAP_DEFAULT_VERSION) {
1085 coap_log_debug("coap_pdu_parse: UDP version not supported\n");
1086 return 0;
1087 }
1088 pdu->type = (hdr[0] >> 4) & 0x03;
1089 pdu->code = hdr[1];
1090 pdu->mid = (uint16_t)hdr[2] << 8 | hdr[3];
1091 } else if (proto == COAP_PROTO_TCP || proto == COAP_PROTO_TLS) {
1092 assert(pdu->hdr_size >= 2 && pdu->hdr_size <= 6);
1093 pdu->type = COAP_MESSAGE_CON;
1094 pdu->code = hdr[pdu->hdr_size-1];
1095 pdu->mid = 0;
1096 } else if (proto == COAP_PROTO_WS || proto == COAP_PROTO_WSS) {
1097 assert(pdu->hdr_size == 2);
1098 pdu->type = COAP_MESSAGE_CON;
1099 pdu->code = hdr[pdu->hdr_size-1];
1100 pdu->mid = 0;
1101 } else {
1102 coap_log_debug("coap_pdu_parse: unsupported protocol\n");
1103 return 0;
1104 }
1105
1106 e_token_length = hdr[0] & 0x0f;
1107 if (e_token_length < COAP_TOKEN_EXT_1B_TKL) {
1108 pdu->e_token_length = e_token_length;
1110 pdu->actual_token.s = &pdu->token[0];
1111 } else if (e_token_length == COAP_TOKEN_EXT_1B_TKL) {
1112 pdu->e_token_length = pdu->token[0] + COAP_TOKEN_EXT_1B_BIAS + 1;
1113 pdu->actual_token.length = pdu->e_token_length - 1;
1114 pdu->actual_token.s = &pdu->token[1];
1115 } else if (e_token_length == COAP_TOKEN_EXT_2B_TKL) {
1116 pdu->e_token_length = ((uint16_t)pdu->token[0] << 8) + pdu->token[1] +
1118 pdu->actual_token.length = pdu->e_token_length - 2;
1119 pdu->actual_token.s = &pdu->token[2];
1120 }
1121 if (pdu->e_token_length > pdu->alloc_size || e_token_length == 15) {
1122 /* Invalid PDU provided - not wise to assert here though */
1123 coap_log_debug("coap_pdu_parse: PDU header token size broken\n");
1124 pdu->e_token_length = 0;
1125 pdu->actual_token.length = 0;
1126 return 0;
1127 }
1128 return 1;
1129}
1130
1131static int
1133 switch ((coap_pdu_signaling_proto_t)pdu->code) {
1134 case COAP_SIGNALING_CSM:
1135 switch (pdu->max_opt) {
1137 if (len > 4)
1138 goto bad;
1139 break;
1141 if (len > 0)
1142 goto bad;
1143 break;
1145 if (len > 3)
1146 goto bad;
1147 break;
1148 default:
1149 if (pdu->max_opt & 0x01)
1150 goto bad; /* Critical */
1151 }
1152 break;
1155 switch (pdu->max_opt) {
1157 if (len > 0)
1158 goto bad;
1159 break;
1160 default:
1161 if (pdu->max_opt & 0x01)
1162 goto bad; /* Critical */
1163 }
1164 break;
1166 switch (pdu->max_opt) {
1168 if (len < 1 || len > 255)
1169 goto bad;
1170 break;
1172 if (len > 3)
1173 goto bad;
1174 break;
1175 default:
1176 if (pdu->max_opt & 0x01)
1177 goto bad; /* Critical */
1178 }
1179 break;
1181 switch (pdu->max_opt) {
1183 if (len > 2)
1184 goto bad;
1185 break;
1186 default:
1187 if (pdu->max_opt & 0x01)
1188 goto bad; /* Critical */
1189 }
1190 break;
1191 default:
1192 ;
1193 }
1194 return 1;
1195bad:
1196 return 0;
1197}
1198
1199static int
1201 int res = 1;
1202
1203 switch (pdu->max_opt) {
1205 if (len > 8)
1206 res = 0;
1207 break;
1209 if (len < 1 || len > 255)
1210 res = 0;
1211 break;
1212 case COAP_OPTION_ETAG:
1213 if (len < 1 || len > 8)
1214 res = 0;
1215 break;
1217 if (len != 0)
1218 res = 0;
1219 break;
1221 if (len > 3)
1222 res = 0;
1223 break;
1225 if (len > 2)
1226 res = 0;
1227 break;
1229 if (len > 255)
1230 res = 0;
1231 break;
1232 case COAP_OPTION_OSCORE:
1233 if (len > 255)
1234 res = 0;
1235 break;
1237 if (len > 255)
1238 res = 0;
1239 break;
1241 if (len > 2)
1242 res = 0;
1243 break;
1244 case COAP_OPTION_MAXAGE:
1245 if (len > 4)
1246 res = 0;
1247 break;
1249 if (len < 1 || len > 255)
1250 res = 0;
1251 break;
1253 if (len != 1)
1254 res = 0;
1255 break;
1256 case COAP_OPTION_ACCEPT:
1257 if (len > 2)
1258 res = 0;
1259 break;
1261 if (len > 255)
1262 res = 0;
1263 break;
1264 case COAP_OPTION_BLOCK2:
1265 if (len > 3)
1266 res = 0;
1267 break;
1268 case COAP_OPTION_BLOCK1:
1269 if (len > 3)
1270 res = 0;
1271 break;
1272 case COAP_OPTION_SIZE2:
1273 if (len > 4)
1274 res = 0;
1275 break;
1277 if (len < 1 || len > 1034)
1278 res = 0;
1279 break;
1281 if (len < 1 || len > 255)
1282 res = 0;
1283 break;
1284 case COAP_OPTION_SIZE1:
1285 if (len > 4)
1286 res = 0;
1287 break;
1288 case COAP_OPTION_ECHO:
1289 if (len > 40)
1290 res = 0;
1291 break;
1293 if (len > 1)
1294 res = 0;
1295 break;
1296 case COAP_OPTION_RTAG:
1297 if (len > 8)
1298 res = 0;
1299 break;
1300 default:
1301 ;
1302 }
1303 return res;
1304}
1305
1306static int
1307write_prefix(char **obp, size_t *len, const char *prf, size_t prflen) {
1308 /* Make sure space for null terminating byte */
1309 if (*len < prflen +1) {
1310 return 0;
1311 }
1312
1313 memcpy(*obp, prf, prflen);
1314 *obp += prflen;
1315 *len -= prflen;
1316 return 1;
1317}
1318
1319static int
1320write_char(char **obp, size_t *len, int c, int printable) {
1321 /* Make sure space for null terminating byte */
1322 if (*len < 2 +1) {
1323 return 0;
1324 }
1325
1326 if (!printable) {
1327 const uint8_t hex[] = "0123456789abcdef";
1328 (*obp)[0] = hex[(c & 0xf0) >> 4];
1329 (*obp)[1] = hex[c & 0x0f];
1330 } else {
1331 (*obp)[0] = isprint(c) ? c : '.';
1332 (*obp)[1] = ' ';
1333 }
1334 *obp += 2;
1335 *len -= 2;
1336 return 1;
1337}
1338
1339int
1341 int good = 1;
1342
1343 /* sanity checks */
1344 if (pdu->code == 0) {
1345 if (pdu->used_size != 0 || pdu->e_token_length) {
1346 coap_log_debug("coap_pdu_parse: empty message is not empty\n");
1347 return 0;
1348 }
1349 }
1350
1351 if (pdu->e_token_length > pdu->used_size) {
1352 coap_log_debug("coap_pdu_parse: invalid Token\n");
1353 return 0;
1354 }
1355
1356 pdu->max_opt = 0;
1357 if (pdu->code == 0) {
1358 /* empty packet */
1359 pdu->used_size = 0;
1360 pdu->data = NULL;
1361 } else {
1362 /* skip header + token */
1363 coap_opt_t *opt = pdu->token + pdu->e_token_length;
1364 size_t length = pdu->used_size - pdu->e_token_length;
1365
1366 while (length > 0 && *opt != COAP_PAYLOAD_START) {
1367#if (COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_WARN)
1368 coap_opt_t *opt_last = opt;
1369#endif
1370 size_t optsize = next_option_safe(&opt, &length, &pdu->max_opt);
1371 const uint32_t len =
1372 optsize ? coap_opt_length((const uint8_t *)opt - optsize) : 0;
1373 if (optsize == 0) {
1374 coap_log_debug("coap_pdu_parse: %d.%02d: offset %u malformed option\n",
1375 pdu->code >> 5, pdu->code & 0x1F,
1376 (int)(opt_last - pdu->token - pdu->e_token_length));
1377 good = 0;
1378 break;
1379 }
1380 if (COAP_PDU_IS_SIGNALING(pdu) ?
1381 !coap_pdu_parse_opt_csm(pdu, len) :
1382 !coap_pdu_parse_opt_base(pdu, len)) {
1383 coap_log_warn("coap_pdu_parse: %d.%02d: offset %u option %u has bad length %" PRIu32 "\n",
1384 pdu->code >> 5, pdu->code & 0x1F,
1385 (int)(opt_last - pdu->token - pdu->e_token_length), pdu->max_opt,
1386 len);
1387 good = 0;
1388 }
1389 }
1390
1391 if (!good) {
1392 /*
1393 * Dump the options in the PDU for analysis, space separated except
1394 * error options which are prefixed by *
1395 * Two rows - hex and ascii (if printable)
1396 */
1397 static char outbuf[COAP_DEBUG_BUF_SIZE];
1398 char *obp;
1399 size_t tlen;
1400 size_t outbuflen;
1401 int i;
1402 int ok;
1403
1404 for (i = 0; i < 2; i++) {
1405 opt = pdu->token + pdu->e_token_length;
1406 length = pdu->used_size - pdu->e_token_length;
1407 pdu->max_opt = 0;
1408
1409 outbuflen = sizeof(outbuf);
1410 obp = outbuf;
1411 ok = write_prefix(&obp, &outbuflen, "O: ", 3);
1412 /*
1413 * Not safe to check for 'ok' here as a lot of variables may get
1414 * partially changed due to lack of outbuflen */
1415 while (length > 0 && *opt != COAP_PAYLOAD_START) {
1416 coap_opt_t *opt_last = opt;
1417 size_t optsize = next_option_safe(&opt, &length, &pdu->max_opt);
1418 const uint32_t len =
1419 optsize ? coap_opt_length((const uint8_t *)opt - optsize) : 0;
1420 if (!optsize || (COAP_PDU_IS_SIGNALING(pdu) ?
1421 !coap_pdu_parse_opt_csm(pdu, len) :
1422 !coap_pdu_parse_opt_base(pdu, len))) {
1423 ok = ok && write_prefix(&obp, &outbuflen, "*", 1);
1424 if (!optsize) {
1425 /* Skip to end of options to output all data */
1426 opt = pdu->token + pdu->used_size;
1427 length = 0;
1428 }
1429 } else {
1430 ok = ok && write_prefix(&obp, &outbuflen, " ", 1);
1431 }
1432 tlen = opt - opt_last;
1433 while (tlen--) {
1434 ok = ok && write_char(&obp, &outbuflen, *opt_last, i);
1435 opt_last++;
1436 }
1437 }
1438 if (length && *opt == COAP_PAYLOAD_START) {
1439 write_char(&obp, &outbuflen, *opt, i);
1440 }
1441 /* write_*() always leaves a spare byte to null terminate */
1442 *obp = '\000';
1443 coap_log_debug("%s\n", outbuf);
1444 }
1445 }
1446
1447 if (length > 0) {
1448 assert(*opt == COAP_PAYLOAD_START);
1449 opt++;
1450 length--;
1451
1452 if (length == 0) {
1453 coap_log_debug("coap_pdu_parse: message ending in payload start marker\n");
1454 return 0;
1455 }
1456 }
1457 if (length > 0)
1458 pdu->data = (uint8_t *)opt;
1459 else
1460 pdu->data = NULL;
1461 }
1462
1463 return good;
1464}
1465
1466int
1468 const uint8_t *data,
1469 size_t length,
1470 coap_pdu_t *pdu) {
1471 size_t hdr_size;
1472
1473 if (length == 0)
1474 return 0;
1475 hdr_size = coap_pdu_parse_header_size(proto, data);
1476 if (!hdr_size || hdr_size > length)
1477 return 0;
1478 if (hdr_size > pdu->max_hdr_size)
1479 return 0;
1480 if (!coap_pdu_resize(pdu, length - hdr_size))
1481 return 0;
1482 if (pdu->token - hdr_size != data)
1483 memcpy(pdu->token - hdr_size, data, length);
1484 pdu->hdr_size = (uint8_t)hdr_size;
1485 pdu->used_size = length - hdr_size;
1486 return coap_pdu_parse_header(pdu, proto) && coap_pdu_parse_opt(pdu);
1487}
1488
1489size_t
1491 uint8_t e_token_length;
1492
1494 e_token_length = (uint8_t)pdu->actual_token.length;
1495 } else if (pdu->actual_token.length < COAP_TOKEN_EXT_2B_BIAS) {
1496 e_token_length = COAP_TOKEN_EXT_1B_TKL;
1497 } else if (pdu->actual_token.length <= COAP_TOKEN_EXT_MAX) {
1498 e_token_length = COAP_TOKEN_EXT_2B_TKL;
1499 } else {
1500 coap_log_warn("coap_add_token: Token size too large. PDU ignored\n");
1501 return 0;
1502 }
1503 if (COAP_PROTO_NOT_RELIABLE(proto)) {
1504 assert(pdu->max_hdr_size >= 4);
1505 if (pdu->max_hdr_size < 4) {
1506 coap_log_warn("coap_pdu_encode_header: not enough space for UDP-style header\n");
1507 return 0;
1508 }
1509 pdu->token[-4] = COAP_DEFAULT_VERSION << 6
1510 | pdu->type << 4
1511 | e_token_length;
1512 pdu->token[-3] = pdu->code;
1513 pdu->token[-2] = (uint8_t)(pdu->mid >> 8);
1514 pdu->token[-1] = (uint8_t)(pdu->mid);
1515 pdu->hdr_size = 4;
1516#if !COAP_DISABLE_TCP
1517 } else if (COAP_PROTO_RELIABLE(proto)) {
1518 size_t len;
1519 assert(pdu->used_size >= pdu->e_token_length);
1520 if (pdu->used_size < pdu->e_token_length) {
1521 coap_log_warn("coap_pdu_encode_header: corrupted PDU\n");
1522 return 0;
1523 }
1524
1525 /* A lot of the reliable code assumes type is CON */
1526 if (pdu->type != COAP_MESSAGE_CON)
1527 pdu->type = COAP_MESSAGE_CON;
1528
1529 if (proto == COAP_PROTO_WS || proto == COAP_PROTO_WSS)
1530 len = 0;
1531 else
1532 len = pdu->used_size - pdu->e_token_length;
1533 if (len <= COAP_MAX_MESSAGE_SIZE_TCP0) {
1534 assert(pdu->max_hdr_size >= 2);
1535 if (pdu->max_hdr_size < 2) {
1536 coap_log_warn("coap_pdu_encode_header: not enough space for TCP0 header\n");
1537 return 0;
1538 }
1539 pdu->token[-2] = (uint8_t)len << 4
1540 | e_token_length;
1541 pdu->token[-1] = pdu->code;
1542 pdu->hdr_size = 2;
1543 } else if (len <= COAP_MAX_MESSAGE_SIZE_TCP8) {
1544 assert(pdu->max_hdr_size >= 3);
1545 if (pdu->max_hdr_size < 3) {
1546 coap_log_warn("coap_pdu_encode_header: not enough space for TCP8 header\n");
1547 return 0;
1548 }
1549 pdu->token[-3] = 13 << 4 | e_token_length;
1550 pdu->token[-2] = (uint8_t)(len - COAP_MESSAGE_SIZE_OFFSET_TCP8);
1551 pdu->token[-1] = pdu->code;
1552 pdu->hdr_size = 3;
1553 } else if (len <= COAP_MAX_MESSAGE_SIZE_TCP16) {
1554 assert(pdu->max_hdr_size >= 4);
1555 if (pdu->max_hdr_size < 4) {
1556 coap_log_warn("coap_pdu_encode_header: not enough space for TCP16 header\n");
1557 return 0;
1558 }
1559 pdu->token[-4] = 14 << 4 | e_token_length;
1560 pdu->token[-3] = (uint8_t)((len - COAP_MESSAGE_SIZE_OFFSET_TCP16) >> 8);
1561 pdu->token[-2] = (uint8_t)(len - COAP_MESSAGE_SIZE_OFFSET_TCP16);
1562 pdu->token[-1] = pdu->code;
1563 pdu->hdr_size = 4;
1564 } else {
1565 assert(pdu->max_hdr_size >= 6);
1566 if (pdu->max_hdr_size < 6) {
1567 coap_log_warn("coap_pdu_encode_header: not enough space for TCP32 header\n");
1568 return 0;
1569 }
1570 pdu->token[-6] = 15 << 4 | e_token_length;
1571 pdu->token[-5] = (uint8_t)((len - COAP_MESSAGE_SIZE_OFFSET_TCP32) >> 24);
1572 pdu->token[-4] = (uint8_t)((len - COAP_MESSAGE_SIZE_OFFSET_TCP32) >> 16);
1573 pdu->token[-3] = (uint8_t)((len - COAP_MESSAGE_SIZE_OFFSET_TCP32) >> 8);
1574 pdu->token[-2] = (uint8_t)(len - COAP_MESSAGE_SIZE_OFFSET_TCP32);
1575 pdu->token[-1] = pdu->code;
1576 pdu->hdr_size = 6;
1577 }
1578#endif /* ! COAP_DISABLE_TCP */
1579 } else {
1580 coap_log_warn("coap_pdu_encode_header: unsupported protocol\n");
1581 }
1582 return pdu->hdr_size;
1583}
1584
1587 return pdu->code;
1588}
1589
1590void
1592#ifndef RIOT_VERSION
1593 assert(code <= 0xff);
1594#endif /* RIOT_VERSION */
1595 pdu->code = code;
1596}
1597
1600 return pdu->type;
1601}
1602
1603void
1605 assert(type <= 0x3);
1606 pdu->type = type;
1607}
1608
1611 return pdu->actual_token;
1612}
1613
1616 return pdu->mid;
1617}
1618
1619void
1621#if (UINT_MAX > 65535)
1622 assert(mid >= 0 && mid <= 0xffff);
1623#endif /* UINT_MAX > 65535 */
1624 pdu->mid = mid;
1625}
1626
1627coap_pdu_t *
1629 if (pdu != NULL) {
1630 ++pdu->ref;
1631 }
1632 return pdu;
1633}
#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:968
static int coap_pdu_parse_opt_csm(coap_pdu_t *pdu, uint16_t len)
Definition coap_pdu.c:1132
error_desc_t coap_error[]
Definition coap_pdu.c:920
static int write_prefix(char **obp, size_t *len, const char *prf, size_t prflen)
Definition coap_pdu.c:1307
static int coap_pdu_parse_opt_base(coap_pdu_t *pdu, uint16_t len)
Definition coap_pdu.c:1200
#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:1320
#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:1628
void coap_delete_pdu_lkd(coap_pdu_t *pdu)
Dispose of an CoAP PDU and free off associated storage.
Definition coap_pdu.c:188
#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:1078
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:994
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:168
#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:1340
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:725
#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:1490
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:228
#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:1025
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:295
#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:781
#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:1586
#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:952
#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:181
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:858
#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:1591
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:206
#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 Note: coap_layers_coap[] needs updating if extended.
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: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:1604
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:771
#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:1599
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:877
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:1467
#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:1620
#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:157
#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:885
coap_mid_t coap_pdu_get_mid(const coap_pdu_t *pdu)
Gets the message id associated with pdu.
Definition coap_pdu.c:1615
#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:846
coap_bin_const_t coap_pdu_get_token(const coap_pdu_t *pdu)
Gets the token associated with pdu.
Definition coap_pdu.c:1610
@ 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: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:914
const char * phrase
Definition coap_pdu.c:915