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