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