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