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