libcoap 4.3.5-develop-efec6cf
Loading...
Searching...
No Matches
coap_block.c
Go to the documentation of this file.
1/* coap_block.c -- block transfer
2 *
3 * Copyright (C) 2010--2012,2015-2026 Olaf Bergmann <bergmann@tzi.org> and others
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#include <stdio.h>
19
20#ifndef min
21#define min(a,b) ((a) < (b) ? (a) : (b))
22#endif
23
24/* Can be 1 - 8 bytes long */
25#ifndef COAP_ETAG_MAX_BYTES
26#define COAP_ETAG_MAX_BYTES 4
27#endif
28#if COAP_ETAG_MAX_BYTES < 1 || COAP_ETAG_MAX_BYTES > 8
29#error COAP_ETAG_MAX_BYTES byte size invalid
30#endif
31
32#if COAP_Q_BLOCK_SUPPORT
33int
35 return 1;
36}
37#else /* ! COAP_Q_BLOCK_SUPPORT */
38int
40 return 0;
41}
42#endif /* ! COAP_Q_BLOCK_SUPPORT */
43
44unsigned int
45coap_opt_block_num(const coap_opt_t *block_opt) {
46 unsigned int num = 0;
47 uint16_t len;
48
49 len = coap_opt_length(block_opt);
50
51 if (len == 0) {
52 return 0;
53 }
54
55 if (len > 1) {
57 coap_opt_length(block_opt) - 1);
58 }
59
60 return (num << 4) | ((COAP_OPT_BLOCK_END_BYTE(block_opt) & 0xF0) >> 4);
61}
62
63int
64coap_get_block_b(const coap_session_t *session, const coap_pdu_t *pdu,
65 coap_option_num_t number, coap_block_b_t *block) {
66 coap_opt_iterator_t opt_iter;
67 coap_opt_t *option;
68
69 assert(block);
70 memset(block, 0, sizeof(coap_block_b_t));
71
72 if (pdu && (option = coap_check_option(pdu, number, &opt_iter)) != NULL) {
73 uint32_t num;
74
75 if (COAP_OPT_BLOCK_MORE(option))
76 block->m = 1;
77 block->aszx = block->szx = COAP_OPT_BLOCK_SZX(option);
78 if (block->szx == 7) {
79 size_t length;
80 const uint8_t *data;
81
82 if (session == NULL || COAP_PROTO_NOT_RELIABLE(session->proto) ||
83 !(session->csm_bert_rem_support && session->csm_bert_loc_support))
84 /* No BERT support */
85 return 0;
86
87 block->szx = 6; /* BERT is 1024 block chunks */
88 block->bert = 1;
89 if (coap_get_data(pdu, &length, &data)) {
90 if (block->m && (length % 1024) != 0) {
91 coap_log_debug("block: Oversized packet - reduced to %" PRIuS " from %" PRIuS "\n",
92 length - (length % 1024), length);
93 length -= length % 1024;
94 }
95 block->chunk_size = (uint32_t)length;
96 } else
97 block->chunk_size = 0;
98 } else {
99 block->chunk_size = (size_t)1 << (block->szx + 4);
100 }
101 block->defined = 1;
102
103 /* The block number is at most 20 bits, so values above 2^20 - 1
104 * are illegal. */
105 num = coap_opt_block_num(option);
106 if (num > 0xFFFFF) {
107 return 0;
108 }
109 block->num = num;
110 return 1;
111 }
112
113 return 0;
114}
115
116int
118 coap_block_t *block) {
119 coap_block_b_t block_b;
120
121 assert(block);
122 memset(block, 0, sizeof(coap_block_t));
123
124 if (coap_get_block_b(NULL, pdu, number, &block_b)) {
125 block->num = block_b.num;
126 block->m = block_b.m;
127 block->szx = block_b.szx;
128 return 1;
129 }
130 return 0;
131}
132
133static int
135 unsigned int num,
136 unsigned int blk_size, size_t total) {
137 size_t token_options = pdu->data ? (size_t)(pdu->data - pdu->token) : pdu->used_size;
138 size_t avail = pdu->max_size - token_options;
139 unsigned int start = num << (blk_size + 4);
140 unsigned int can_use_bert = block->defined == 0 || block->bert;
141
142 assert(start <= total);
143 memset(block, 0, sizeof(*block));
144 block->num = num;
145 block->szx = block->aszx = blk_size;
146 if (can_use_bert && blk_size == 6 && avail >= 1024 && session != NULL &&
147 COAP_PROTO_RELIABLE(session->proto) &&
148 session->csm_bert_rem_support && session->csm_bert_loc_support) {
149 block->bert = 1;
150 block->aszx = 7;
151 block->chunk_size = (uint32_t)((avail / 1024) * 1024);
152 } else {
153 block->chunk_size = (size_t)1 << (blk_size + 4);
154 if (avail < block->chunk_size && (total - start) >= avail) {
155 /* Need to reduce block size */
156 unsigned int szx;
157 int new_blk_size;
158
159 if (avail < 16) { /* bad luck, this is the smallest block size */
160 coap_log_debug("not enough space, even the smallest block does not fit (1)\n");
161 return 0;
162 }
163 new_blk_size = coap_flsll((long long)avail) - 5;
164 coap_log_debug("decrease block size for %" PRIuS " to %d\n", avail, new_blk_size);
165 szx = block->szx;
166 block->szx = new_blk_size;
167 block->num <<= szx - block->szx;
168 block->chunk_size = (size_t)1 << (new_blk_size + 4);
169 }
170 }
171 block->m = block->chunk_size < total - start;
172 return 1;
173}
174
175int
177 coap_pdu_t *pdu, size_t data_length) {
178 size_t start;
179 unsigned char buf[4];
180 coap_block_b_t block_b;
181
182 assert(pdu);
183
184 start = block->num << (block->szx + 4);
185 if (block->num != 0 && data_length <= start) {
186 coap_log_debug("illegal block requested\n");
187 return -2;
188 }
189
190 assert(pdu->max_size > 0);
191
192 block_b.defined = 1;
193 block_b.bert = 0;
194 if (!setup_block_b(NULL, pdu, &block_b, block->num,
195 block->szx, data_length))
196 return -3;
197
198 /* to re-encode the block option */
199 coap_update_option(pdu, number, coap_encode_var_safe(buf, sizeof(buf),
200 ((block_b.num << 4) |
201 (block_b.m << 3) |
202 block_b.szx)),
203 buf);
204
205 return 1;
206}
207
208int
210 coap_option_num_t number,
211 coap_pdu_t *pdu, size_t data_length) {
212 size_t start;
213 unsigned char buf[4];
214
215 assert(pdu);
216
217 start = block->num << (block->szx + 4);
218 if (block->num != 0 && data_length <= start) {
219 coap_log_debug("illegal block requested\n");
220 return -2;
221 }
222
223 assert(pdu->max_size > 0);
224
225 if (!setup_block_b(session, pdu, block, block->num,
226 block->szx, data_length))
227 return -3;
228
229 /* to re-encode the block option */
230 coap_update_option(pdu, number, coap_encode_var_safe(buf, sizeof(buf),
231 ((block->num << 4) |
232 (block->m << 3) |
233 block->aszx)),
234 buf);
235
236 return 1;
237}
238
239int
240coap_add_block(coap_pdu_t *pdu, size_t len, const uint8_t *data,
241 unsigned int block_num, unsigned char block_szx) {
242 unsigned int start;
243 start = block_num << (block_szx + 4);
244
245 if (len <= start)
246 return 0;
247
248 return coap_add_data(pdu,
249 min(len - start, ((size_t)1 << (block_szx + 4))),
250 data + start);
251}
252
253int
254coap_add_block_b_data(coap_pdu_t *pdu, size_t len, const uint8_t *data,
255 coap_block_b_t *block) {
256 unsigned int start = block->num << (block->szx + 4);
257 size_t max_size;
258
259 if (len <= start)
260 return 0;
261
262 if (block->bert) {
263 size_t token_options = pdu->data ? (size_t)(pdu->data - pdu->token) : pdu->used_size;
264 max_size = ((pdu->max_size - token_options) / 1024) * 1024;
265 } else {
266 max_size = (size_t)1 << (block->szx + 4);
267 }
268 block->chunk_size = (uint32_t)max_size;
269
270 return coap_add_data(pdu,
271 min(len - start, max_size),
272 data + start);
273}
274
275/*
276 * Note that the COAP_OPTION_ have to be added in the correct order
277 */
278void
280 coap_pdu_t *response,
281 uint16_t media_type,
282 int maxage,
283 size_t length,
284 const uint8_t *data
285 ) {
286 unsigned char buf[4];
287 coap_block_t block2;
288 int block2_requested = 0;
289#if COAP_SERVER_SUPPORT
290 uint64_t etag = 0;
291 coap_digest_t digest;
292 coap_digest_ctx_t *dctx = NULL;
293#endif /* COAP_SERVER_SUPPORT */
294
295 memset(&block2, 0, sizeof(block2));
296 /*
297 * Need to check that a valid block is getting asked for so that the
298 * correct options are put into the PDU.
299 */
300 if (request) {
301 if (coap_get_block(request, COAP_OPTION_BLOCK2, &block2)) {
302 block2_requested = 1;
303 if (block2.num != 0 && length <= (block2.num << (block2.szx + 4))) {
304 coap_log_debug("Illegal block requested (%d > last = %" PRIuS ")\n",
305 block2.num,
306 length >> (block2.szx + 4));
307 response->code = COAP_RESPONSE_CODE(400);
308 goto error;
309 }
310 }
311 }
312 response->code = COAP_RESPONSE_CODE(205);
313
314#if COAP_SERVER_SUPPORT
315 /* add ETag for the resource data */
316 if (length) {
317 dctx = coap_digest_setup();
318 if (!dctx)
319 goto error;
320 if (request && request->session &&
321 coap_is_mcast(&request->session->addr_info.local)) {
322 coap_digest_update(dctx, coap_unique_id, sizeof(coap_unique_id));
323 }
324 if (!coap_digest_update(dctx, data, length))
325 goto error;
326 if (!coap_digest_final(dctx, &digest))
327 goto error;
328 dctx = NULL;
329 memcpy(&etag, digest.key, sizeof(etag));
330#if COAP_ETAG_MAX_BYTES != 8
331 etag = etag >> 8*(8 - COAP_ETAG_MAX_BYTES);
332#endif
333 if (!etag)
334 etag = 1;
335 coap_update_option(response,
337 coap_encode_var_safe8(buf, sizeof(buf), etag),
338 buf);
339 }
340#endif /* COAP_SERVER_SUPPORT */
341
343 coap_encode_var_safe(buf, sizeof(buf),
344 media_type),
345 buf);
346
347 if (maxage >= 0) {
348 coap_insert_option(response,
350 coap_encode_var_safe(buf, sizeof(buf), maxage), buf);
351 }
352
353 if (block2_requested) {
354 int res;
355
356 res = coap_write_block_opt(&block2, COAP_OPTION_BLOCK2, response, length);
357
358 switch (res) {
359 case -2: /* illegal block (caught above) */
360 response->code = COAP_RESPONSE_CODE(400);
361 goto error;
362 case -1: /* should really not happen */
363 assert(0);
364 /* fall through if assert is a no-op */
365 case -3: /* cannot handle request */
366 response->code = COAP_RESPONSE_CODE(500);
367 goto error;
368 default: /* everything is good */
369 ;
370 }
371
374 coap_encode_var_safe8(buf, sizeof(buf), length),
375 buf);
376
377 coap_add_block(response, length, data,
378 block2.num, block2.szx);
379 return;
380 }
381
382 /*
383 * Block2 not requested
384 */
385 if (!coap_add_data(response, length, data)) {
386 /*
387 * Insufficient space to add in data - use block mode
388 * set initial block size, will be lowered by
389 * coap_write_block_opt() automatically
390 */
391 block2.num = 0;
392 block2.szx = 6;
393 coap_write_block_opt(&block2, COAP_OPTION_BLOCK2, response, length);
394
397 coap_encode_var_safe8(buf, sizeof(buf), length),
398 buf);
399
400 coap_add_block(response, length, data,
401 block2.num, block2.szx);
402 }
403 return;
404
405error:
406#if COAP_SERVER_SUPPORT
407 coap_digest_free(dctx);
408#endif /* COAP_SERVER_SUPPORT */
409 coap_add_data(response,
410 strlen(coap_response_phrase(response->code)),
411 (const unsigned char *)coap_response_phrase(response->code));
412}
413
414COAP_API void
416 uint32_t block_mode) {
417 coap_lock_lock(return);
418 coap_context_set_block_mode_lkd(context, block_mode);
420}
421
422void
424 uint32_t block_mode) {
426 if (!(block_mode & COAP_BLOCK_USE_LIBCOAP))
427 block_mode = 0;
428 context->block_mode &= ~COAP_BLOCK_SET_MASK;
429 context->block_mode |= block_mode & COAP_BLOCK_SET_MASK;
430#if ! COAP_Q_BLOCK_SUPPORT
432 coap_log_debug("Q-Block support not compiled in - ignored\n");
433#endif /* ! COAP_Q_BLOCK_SUPPORT */
434}
435
436COAP_API int
438 size_t max_block_size) {
439 int ret;
440
441 coap_lock_lock(return 0);
442 ret = coap_context_set_max_block_size_lkd(context, max_block_size);
444 return ret;
445}
446
447int
448coap_context_set_max_block_size_lkd(coap_context_t *context, size_t max_block_size) {
449 switch (max_block_size) {
450 case 0:
451 case 16:
452 case 32:
453 case 64:
454 case 128:
455 case 256:
456 case 512:
457 case 1024:
458 break;
459 default:
460 coap_log_info("coap_context_set_max_block_size: Invalid max block size (%" PRIuS ")\n",
461 max_block_size);
462 return 0;
463 }
465 max_block_size = (coap_fls((uint32_t)max_block_size >> 4) - 1) & 0x07;
466 context->block_mode &= ~COAP_BLOCK_MAX_SIZE_MASK;
467 context->block_mode |= COAP_BLOCK_MAX_SIZE_SET((uint32_t)max_block_size);
468 return 1;
469}
470
472full_match(const uint8_t *a, size_t alen,
473 const uint8_t *b, size_t blen) {
474 return alen == blen && (alen == 0 || memcmp(a, b, alen) == 0);
475}
476
479 coap_lg_xmit_t *lg_xmit = NULL;
480 coap_lg_xmit_t *m_lg_xmit = NULL;
481 uint64_t token_match =
483 pdu->actual_token.length));
484
485 LL_FOREACH(session->lg_xmit, lg_xmit) {
486 if (token_match != STATE_TOKEN_BASE(lg_xmit->b.b1.state_token) &&
487 !coap_binary_equal(&pdu->actual_token, lg_xmit->b.b1.app_token)) {
488 /* try out the next one */
489 continue;
490 }
491#if COAP_CLIENT_SUPPORT
492 if (COAP_PDU_IS_RESPONSE(pdu)) {
493 if (coap_is_mcast(&lg_xmit->b.b1.upstream)) {
494 m_lg_xmit = lg_xmit;
495 }
496 if (!coap_address_equals(&lg_xmit->b.b1.upstream, &session->addr_info.remote)) {
497 /* try out the next one */
498 continue;
499 }
500 }
501 /* Have a match */
502 return lg_xmit;
503#endif /* COAP_CLIENT_SUPPORT */
504 }
505 if (m_lg_xmit && (session->sock.flags & COAP_SOCKET_MULTICAST)) {
506 /* Need to set up unicast version of mcast lg_xmit entry */
507 lg_xmit = coap_malloc_type(COAP_LG_XMIT, sizeof(coap_lg_xmit_t));
508 if (!lg_xmit)
509 return NULL;
510 memcpy(lg_xmit, m_lg_xmit, sizeof(coap_lg_xmit_t));
511 lg_xmit->next = NULL;
512 lg_xmit->b.b1.app_token = NULL;
513 lg_xmit->data_info->ref++;
514 lg_xmit->sent_pdu = coap_pdu_reference_lkd(m_lg_xmit->sent_pdu);
515 coap_address_copy(&lg_xmit->b.b1.upstream, &session->addr_info.remote);
516 lg_xmit->b.b1.app_token = coap_new_binary(m_lg_xmit->b.b1.app_token->length);
517 if (!lg_xmit->b.b1.app_token)
518 goto fail;
519 if (m_lg_xmit->b.b1.app_token->length)
520 memcpy(lg_xmit->b.b1.app_token->s, m_lg_xmit->b.b1.app_token->s,
521 m_lg_xmit->b.b1.app_token->length);
522 LL_PREPEND(session->lg_xmit, lg_xmit);
523 coap_log_debug("** %s: lg_xmit %p mcast slave initialized\n",
524 coap_session_str(session), (void *)lg_xmit);
525 /* Allow the mcast lg_xmit to time out earlier */
526 coap_ticks(&m_lg_xmit->last_all_sent);
527#if COAP_CLIENT_SUPPORT
528 if (COAP_PDU_IS_RESPONSE(pdu)) {
529 coap_lg_crcv_t *lg_crcv;
530
531 lg_crcv = coap_find_lg_crcv(session, pdu);
532 if (lg_crcv) {
533 lg_xmit->b.b1.state_token = lg_crcv->state_token;
534 }
535 }
536#endif /* COAP_CLIENT_SUPPORT */
537 }
538 return lg_xmit;
539
540fail:
541 coap_block_delete_lg_xmit(session, lg_xmit);
542 return NULL;
543}
544
545#if COAP_CLIENT_SUPPORT
546
547COAP_API int
549 coap_pdu_type_t type) {
550 int ret;
551
552 coap_lock_lock(return 0);
553 ret = coap_cancel_observe_lkd(session, token, type);
555 return ret;
556}
557
558int
559coap_cancel_observe_lkd(coap_session_t *session, coap_binary_t *token,
560 coap_pdu_type_t type) {
561 coap_lg_crcv_t *lg_crcv, *q;
562
563 assert(session);
564 if (!session)
565 return 0;
566
568 if (!(session->block_mode & COAP_BLOCK_USE_LIBCOAP)) {
569 coap_log_debug("** %s: coap_cancel_observe: COAP_BLOCK_USE_LIBCOAP not enabled\n",
570 coap_session_str(session));
571 return 0;
572 }
573
574 LL_FOREACH_SAFE(session->lg_crcv, lg_crcv, q) {
575 if (lg_crcv->observe_set) {
576 if ((!token && !lg_crcv->app_token->length) || (token &&
577 coap_binary_equal(token, lg_crcv->app_token))) {
578 uint8_t buf[8];
579 coap_mid_t mid;
580 size_t size;
581 const uint8_t *data;
582#if COAP_Q_BLOCK_SUPPORT
583 coap_block_b_t block;
584 int using_q_block1 = coap_get_block_b(session, lg_crcv->sent_pdu,
585 COAP_OPTION_Q_BLOCK1, &block);
586#endif /* COAP_Q_BLOCK_SUPPORT */
587 coap_bin_const_t *otoken = lg_crcv->obs_token ?
588 lg_crcv->obs_token[0] ?
589 lg_crcv->obs_token[0] :
590 (coap_bin_const_t *)lg_crcv->app_token :
591 (coap_bin_const_t *)lg_crcv->app_token;
592 coap_pdu_t *pdu = coap_pdu_duplicate_lkd(lg_crcv->sent_pdu,
593 session,
594 otoken->length,
595 otoken->s,
596 NULL);
597
598 lg_crcv->observe_set = 0;
599 if (pdu == NULL)
600 return 0;
601 /* Need to make sure that this is the correct requested type */
602 pdu->type = type;
603
605 coap_encode_var_safe(buf, sizeof(buf),
607 buf);
608 if (lg_crcv->o_block_option) {
609 coap_update_option(pdu, lg_crcv->o_block_option,
610 coap_encode_var_safe(buf, sizeof(buf),
611 lg_crcv->o_blk_size),
612 buf);
613 }
614 if (lg_crcv->obs_data) {
615 coap_add_data_large_request_lkd(session, pdu,
616 lg_crcv->obs_data->length,
617 lg_crcv->obs_data->data, NULL, NULL);
618 } else if (coap_get_data(lg_crcv->sent_pdu, &size, &data)) {
619 coap_add_data_large_request_lkd(session, pdu, size, data, NULL, NULL);
620 }
621
622 /*
623 * Need to fix lg_xmit stateless token as using tokens from
624 * observe setup
625 */
626 if (pdu->lg_xmit)
627 pdu->lg_xmit->b.b1.state_token = lg_crcv->state_token;
628
629 coap_address_copy(&session->addr_info.remote, &lg_crcv->upstream);
630#if COAP_Q_BLOCK_SUPPORT
631 /* See if large xmit using Q-Block1 (but not testing Q-Block1) */
632 if (using_q_block1) {
633 mid = coap_send_q_block1(session, block, pdu, COAP_SEND_INC_PDU);
634 } else {
635 mid = coap_send_internal(session, pdu, NULL);
636 }
637#else /* ! COAP_Q_BLOCK_SUPPORT */
638 mid = coap_send_internal(session, pdu, NULL);
639#endif /* ! COAP_Q_BLOCK_SUPPORT */
640 if (mid == COAP_INVALID_MID)
641 break;
642 }
643 }
644 }
645 return 1;
646}
647
649coap_find_lg_crcv(coap_session_t *session, coap_pdu_t *pdu) {
650 coap_lg_crcv_t *lg_crcv;
651 coap_lg_crcv_t *m_lg_crcv = NULL;
652 uint64_t token_match =
654 pdu->actual_token.length));
655
656 LL_FOREACH(session->lg_crcv, lg_crcv) {
657 if (token_match != STATE_TOKEN_BASE(lg_crcv->state_token) &&
658 !coap_binary_equal(&pdu->actual_token, lg_crcv->app_token)) {
659 /* try out the next one */
660 continue;
661 }
662 if (coap_is_mcast(&lg_crcv->upstream)) {
663 m_lg_crcv = lg_crcv;
664 }
665 if (!coap_address_equals(&lg_crcv->upstream, &session->addr_info.remote)) {
666 /* try out the next one */
667 continue;
668 }
669 /* Have a match */
670 return lg_crcv;
671 }
672 if (m_lg_crcv && (session->sock.flags & COAP_SOCKET_MULTICAST)) {
673 /* Need to set up unicast version of mcast lg_crcv entry */
674 lg_crcv = coap_block_new_lg_crcv(session, m_lg_crcv->sent_pdu, NULL);
675 if (lg_crcv) {
676 if (m_lg_crcv->obs_data) {
677 m_lg_crcv->obs_data->ref++;
678 lg_crcv->obs_data = m_lg_crcv->obs_data;
679 }
680 LL_PREPEND(session->lg_crcv, lg_crcv);
681 }
682 }
683 return lg_crcv;
684}
685
686#if COAP_OSCORE_SUPPORT
688coap_retransmit_oscore_pdu(coap_session_t *session,
689 coap_pdu_t *pdu,
690 coap_opt_t *echo) {
691 coap_lg_crcv_t *lg_crcv;
692 uint8_t ltoken[8];
693 size_t ltoken_len;
694 uint64_t token;
695 const uint8_t *data;
696 size_t data_len;
697 coap_pdu_t *resend_pdu;
698 coap_block_b_t block;
699
700 lg_crcv = coap_find_lg_crcv(session, pdu);
701 if (lg_crcv) {
702 /* Re-send request with new token */
703 token = STATE_TOKEN_FULL(lg_crcv->state_token,
704 ++lg_crcv->retry_counter);
705 ltoken_len = coap_encode_var_safe8(ltoken, sizeof(token), token);
706 /* There could be a Block option in pdu */
707 resend_pdu = coap_pdu_duplicate_lkd(pdu, session, ltoken_len,
708 ltoken, NULL);
709 if (!resend_pdu)
710 goto error;
711 if (echo) {
713 coap_opt_value(echo));
714 }
715 if (coap_get_data(lg_crcv->sent_pdu, &data_len, &data)) {
716 if (coap_get_block_b(session, resend_pdu, COAP_OPTION_BLOCK1, &block)) {
717 if (data_len > block.chunk_size && block.chunk_size != 0) {
718 data_len = block.chunk_size;
719 }
720 }
721 coap_add_data(resend_pdu, data_len, data);
722 }
723
724 return coap_send_internal(session, resend_pdu, NULL);
725 }
726error:
727 return COAP_INVALID_MID;
728}
729#endif /* COAP_OSCORE_SUPPORT */
730#endif /* COAP_CLIENT_SUPPORT */
731
732#if COAP_SERVER_SUPPORT
733/*
734 * Find the response lg_xmit
735 */
737coap_find_lg_xmit_response(const coap_session_t *session,
738 const coap_pdu_t *request,
739 const coap_resource_t *resource,
740 const coap_string_t *query) {
741 coap_lg_xmit_t *lg_xmit;
742 coap_opt_iterator_t opt_iter;
743 coap_opt_t *rtag_opt = coap_check_option(request,
745 &opt_iter);
746 size_t rtag_length = rtag_opt ? coap_opt_length(rtag_opt) : 0;
747 const uint8_t *rtag = rtag_opt ? coap_opt_value(rtag_opt) : NULL;
748
749 LL_FOREACH(session->lg_xmit, lg_xmit) {
750 static coap_string_t empty = { 0, NULL};
751
752 if (COAP_PDU_IS_REQUEST(lg_xmit->sent_pdu) ||
753 resource != lg_xmit->b.b2.resource ||
754 request->code != lg_xmit->b.b2.request_method ||
755 !coap_string_equal(query ? query : &empty,
756 lg_xmit->b.b2.query ?
757 lg_xmit->b.b2.query : &empty)) {
758 /* try out the next one */
759 continue;
760 }
761 /* lg_xmit is a response */
762 if (rtag_opt || lg_xmit->b.b2.rtag_set == 1) {
763 if (!(rtag_opt && lg_xmit->b.b2.rtag_set == 1))
764 continue;
765 if (lg_xmit->b.b2.rtag_length != rtag_length ||
766 memcmp(lg_xmit->b.b2.rtag, rtag, rtag_length) != 0)
767 continue;
768 }
769 return lg_xmit;
770 }
771 return NULL;
772}
773#endif /* COAP_SERVER_SUPPORT */
774
775static int
777 const coap_pdu_t *request,
778 coap_pdu_t *pdu,
779 coap_resource_t *resource,
780 const coap_string_t *query,
781 int maxage,
782 uint64_t etag,
783 size_t length,
784 const uint8_t *data,
785 coap_release_large_data_t release_func,
786 coap_get_large_data_t get_func,
787 void *app_ptr,
788 int single_request, coap_pdu_code_t request_method) {
789
790 ssize_t avail;
791 coap_block_b_t block;
792#if COAP_Q_BLOCK_SUPPORT
793 coap_block_b_t alt_block;
794#endif /* COAP_Q_BLOCK_SUPPORT */
795 size_t chunk;
796 coap_lg_xmit_t *lg_xmit = NULL;
797 uint8_t buf[8];
798 int have_block_defined = 0;
799 uint8_t blk_size;
800 uint8_t max_blk_size;
801 uint16_t option;
802 size_t token_options;
803 coap_opt_t *opt;
804 coap_opt_iterator_t opt_iter;
805#if COAP_Q_BLOCK_SUPPORT
806 uint16_t alt_option;
807#endif /* COAP_Q_BLOCK_SUPPORT */
808
809#if !COAP_SERVER_SUPPORT
810 (void)etag;
811#endif /* COAP_SERVER_SUPPORT */
812
813 assert(pdu);
814 if (pdu->data) {
815 coap_log_warn("coap_add_data_large: PDU already contains data\n");
816 if (release_func) {
817 coap_lock_callback(release_func(session, app_ptr));
818 }
819 return 0;
820 }
821
822 if (!(session->block_mode & COAP_BLOCK_USE_LIBCOAP)) {
823 coap_log_debug("** %s: coap_add_data_large: COAP_BLOCK_USE_LIBCOAP not enabled\n",
824 coap_session_str(session));
825 goto add_data;
826 }
827
828 /* A lot of the reliable code assumes type is CON */
829 if (COAP_PROTO_RELIABLE(session->proto) && pdu->type == COAP_MESSAGE_NON)
830 pdu->type = COAP_MESSAGE_CON;
831
832 /* Block NUM max 20 bits (starting from 0) and block size is "2**(SZX + 4)"
833 and using SZX max of 6 gives maximum size = 1,073,740,800
834 CSM Max-Message-Size theoretical maximum = 4,294,967,295
835 So, if using blocks, we are limited to 1,073,740,800.
836 */
837#define MAX_BLK_LEN ((1UL << 20) * (1 << (6 + 4)))
838#if UINT_MAX < MAX_BLK_LEN
839#undef MAX_BLK_LEN
840#define MAX_BLK_LEN UINT_MAX
841#endif
842
843 if (length > MAX_BLK_LEN) {
844 coap_log_warn("Size of large buffer restricted to 0x%lx bytes\n", MAX_BLK_LEN);
845 length = MAX_BLK_LEN;
846 }
847
848#if COAP_SERVER_SUPPORT
849 /* Possible response code not yet set, so check if not request */
850 if (!COAP_PDU_IS_REQUEST(pdu) && length) {
851 coap_opt_t *etag_opt = coap_check_option(pdu, COAP_OPTION_ETAG, &opt_iter);
852
853 if (etag_opt) {
854 /* Have to use ETag as supplied in the response PDU */
855 etag = coap_decode_var_bytes8(coap_opt_value(etag_opt),
856 coap_opt_length(etag_opt));
857 } else {
858 if (!etag) {
859 /* calculate ETag for the response */
860 coap_digest_t digest;
861 coap_digest_ctx_t *dctx = coap_digest_setup();
862
863 if (dctx) {
864 if (coap_is_mcast(&session->addr_info.local)) {
865 (void)coap_digest_update(dctx, coap_unique_id, sizeof(coap_unique_id));
866 }
867 if (coap_digest_update(dctx, data, length)) {
868 if (coap_digest_final(dctx, &digest)) {
869 memcpy(&etag, digest.key, sizeof(etag));
870#if COAP_ETAG_MAX_BYTES != 8
871 etag = etag >> 8*(8 - COAP_ETAG_MAX_BYTES);
872#endif
873 dctx = NULL;
874 }
875 }
876 coap_digest_free(dctx);
877 }
878 if (!etag)
879 etag = 1;
880 }
883 coap_encode_var_safe8(buf, sizeof(buf), etag),
884 buf);
885 }
886 if (request) {
887 etag_opt = coap_check_option(request, COAP_OPTION_ETAG, &opt_iter);
888 if (etag_opt) {
889 /* There may be multiple ETag - need to check each one */
890 coap_option_iterator_init(request, &opt_iter, COAP_OPT_ALL);
891 while ((etag_opt = coap_option_next(&opt_iter))) {
892 if (opt_iter.number == COAP_OPTION_ETAG) {
893 uint64_t etag_r = coap_decode_var_bytes8(coap_opt_value(etag_opt),
894 coap_opt_length(etag_opt));
895
896 if (etag == etag_r) {
897 pdu->code = COAP_RESPONSE_CODE(203);
898 return 1;
899 }
900 }
901 }
902 }
903 }
904 }
905#endif /* COAP_SERVER_SUPPORT */
906
907 /* Determine the block size to use, adding in sensible options if needed */
908 if (COAP_PDU_IS_REQUEST(pdu)) {
910
911#if COAP_Q_BLOCK_SUPPORT
912 if (session->block_mode & (COAP_BLOCK_HAS_Q_BLOCK|COAP_BLOCK_TRY_Q_BLOCK)) {
913 option = COAP_OPTION_Q_BLOCK1;
914 alt_option = COAP_OPTION_BLOCK1;
915 } else {
916 option = COAP_OPTION_BLOCK1;
917 alt_option = COAP_OPTION_Q_BLOCK1;
918 }
919#else /* ! COAP_Q_BLOCK_SUPPORT */
920 if (coap_get_block_b(session, pdu, COAP_OPTION_Q_BLOCK1, &block)) {
922 }
923 option = COAP_OPTION_BLOCK1;
924#endif /* ! COAP_Q_BLOCK_SUPPORT */
925
926 /* See if this token is already in use for large bodies (unlikely) */
927 LL_FOREACH_SAFE(session->lg_xmit, lg_xmit, q) {
928 if (coap_binary_equal(&pdu->actual_token, lg_xmit->b.b1.app_token)) {
929 /* Unfortunately need to free this off as potential size change */
930 int is_mcast = 0;
931#if COAP_CLIENT_SUPPORT
932 is_mcast = coap_is_mcast(&lg_xmit->b.b1.upstream);
933#endif /* COAP_CLIENT_SUPPORT */
934 LL_DELETE(session->lg_xmit, lg_xmit);
935 coap_block_delete_lg_xmit(session, lg_xmit);
936 lg_xmit = NULL;
937 if (!is_mcast)
939 break;
940 }
941 }
942 } else {
943 /* Have to assume that it is a response even if code is 0.00 */
944 assert(resource);
945#if COAP_Q_BLOCK_SUPPORT
946 if (session->block_mode & COAP_BLOCK_HAS_Q_BLOCK) {
947 option = COAP_OPTION_Q_BLOCK2;
948 alt_option = COAP_OPTION_BLOCK2;
949 } else {
950 option = COAP_OPTION_BLOCK2;
951 alt_option = COAP_OPTION_Q_BLOCK2;
952 }
953#else /* ! COAP_Q_BLOCK_SUPPORT */
954 if (coap_get_block_b(session, pdu, COAP_OPTION_Q_BLOCK2, &block)) {
956 }
957 option = COAP_OPTION_BLOCK2;
958#endif /* ! COAP_Q_BLOCK_SUPPORT */
959#if COAP_SERVER_SUPPORT
960 /*
961 * Check if resource+query+rtag is already in use for large bodies
962 * (unlikely)
963 */
964 lg_xmit = coap_find_lg_xmit_response(session, request, resource, query);
965 if (lg_xmit) {
966 /* Unfortunately need to free this off as potential size change */
967 LL_DELETE(session->lg_xmit, lg_xmit);
968 coap_block_delete_lg_xmit(session, lg_xmit);
969 lg_xmit = NULL;
971 }
972#endif /* COAP_SERVER_SUPPORT */
973 }
974#if COAP_OSCORE_SUPPORT
975 if (session->oscore_encryption) {
976 /* Need to convert Proxy-Uri to Proxy-Scheme option if needed */
978 goto fail;
979 }
980#endif /* COAP_OSCORE_SUPPORT */
981
982 token_options = pdu->data ? (size_t)(pdu->data - pdu->token) : pdu->used_size;
983 avail = pdu->max_size - token_options;
984 /* There may be a response with Echo option */
986#if COAP_OSCORE_SUPPORT
987 avail -= coap_oscore_overhead(session, pdu);
988#endif /* COAP_OSCORE_SUPPORT */
989 /* May need token of length 8, so account for this */
990 avail -= (pdu->actual_token.length < 8) ? 8 - pdu->actual_token.length : 0;
991
992 if (avail < 16) {
993 blk_size = 0;
994 } else {
995 blk_size = coap_flsll((long long)avail) - 4 - 1;
996 }
997 if (blk_size > 6)
998 blk_size = 6;
999
1000 max_blk_size = COAP_BLOCK_MAX_SIZE_GET(session->block_mode);
1001 if (max_blk_size && blk_size > max_blk_size)
1002 blk_size = max_blk_size;
1003
1004 /* see if BlockX defined - if so update blk_size as given by app */
1005 if (coap_get_block_b(session, pdu, option, &block)) {
1006 if (block.szx < blk_size)
1007 blk_size = block.szx;
1008 have_block_defined = 1;
1009 }
1010#if COAP_Q_BLOCK_SUPPORT
1011 /* see if alternate BlockX defined */
1012 if (coap_get_block_b(session, pdu, alt_option, &alt_block)) {
1013 if (have_block_defined) {
1014 /* Cannot have both options set */
1015 coap_log_warn("Both BlockX and Q-BlockX cannot be set at the same time\n");
1016 coap_remove_option(pdu, alt_option);
1017 } else {
1018 block = alt_block;
1019 if (block.szx < blk_size)
1020 blk_size = block.szx;
1021 have_block_defined = 1;
1022 option = alt_option;
1023 }
1024 }
1025#endif /* COAP_Q_BLOCK_SUPPORT */
1026
1027 if (avail < 16 && ((ssize_t)length > avail || have_block_defined)) {
1028 /* bad luck, this is the smallest block size */
1029 coap_log_debug("not enough space, even the smallest block does not fit (2)\n");
1030 goto fail;
1031 }
1032
1033 chunk = (size_t)1 << (blk_size + 4);
1034 if ((have_block_defined && block.num != 0) || single_request ||
1035 ((session->block_mode & COAP_BLOCK_STLESS_BLOCK2) && session->type != COAP_SESSION_TYPE_CLIENT)) {
1036 /* App is defining a single block to send or we are stateless */
1037 size_t rem;
1038
1039 if (length >= block.num * chunk) {
1040#if COAP_SERVER_SUPPORT
1041 if (session->block_mode & COAP_BLOCK_STLESS_BLOCK2 && session->type != COAP_SESSION_TYPE_CLIENT) {
1042 /* We are running server stateless */
1045 coap_encode_var_safe(buf, sizeof(buf),
1046 (unsigned int)length),
1047 buf);
1048 if (request) {
1049 if (!coap_get_block_b(session, request, option, &block))
1050 block.num = 0;
1051 }
1052 if (!setup_block_b(session, pdu, &block, block.num,
1053 blk_size, length))
1054 goto fail;
1055
1056 /* Add in with requested block num, more bit and block size */
1058 option,
1059 coap_encode_var_safe(buf, sizeof(buf),
1060 (block.num << 4) | (block.m << 3) | block.aszx),
1061 buf);
1062 }
1063#endif /* COAP_SERVER_SUPPORT */
1064 rem = chunk;
1065 if (chunk > length - block.num * chunk)
1066 rem = length - block.num * chunk;
1067 if (!coap_add_data(pdu, rem, &data[block.num * chunk]))
1068 goto fail;
1069 }
1070 if (release_func) {
1071 coap_lock_callback(release_func(session, app_ptr));
1072 }
1073 } else if ((have_block_defined && length > chunk) || (ssize_t)length > avail) {
1074 /* Only add in lg_xmit if more than one block needs to be handled */
1075 size_t rem;
1076
1077 lg_xmit = coap_malloc_type(COAP_LG_XMIT, sizeof(coap_lg_xmit_t));
1078 if (!lg_xmit)
1079 goto fail;
1080
1081 /* Set up for displaying all the data in the pdu */
1082 if (!get_func) {
1083 pdu->body_data = data;
1084 pdu->body_length = length;
1085 coap_log_debug("PDU presented by app.\n");
1087 pdu->body_data = NULL;
1088 pdu->body_length = 0;
1089 } else {
1090 coap_log_debug("PDU presented by app without data.\n");
1092 }
1093
1094 coap_log_debug("** %s: lg_xmit %p initialized\n",
1095 coap_session_str(session), (void *)lg_xmit);
1096 /* Update lg_xmit with large data information */
1097 memset(lg_xmit, 0, sizeof(coap_lg_xmit_t));
1098 lg_xmit->blk_size = blk_size;
1099 lg_xmit->option = option;
1100 lg_xmit->data_info = coap_malloc_type(COAP_STRING, sizeof(coap_lg_xmit_data_t));
1101 if (!lg_xmit->data_info)
1102 goto fail;
1103 lg_xmit->data_info->ref = 0;
1104 lg_xmit->data_info->data = data;
1105 lg_xmit->data_info->length = length;
1106#if COAP_Q_BLOCK_SUPPORT
1107 lg_xmit->non_timeout_random_ticks =
1109#endif /* COAP_Q_BLOCK_SUPPORT */
1110 lg_xmit->data_info->get_func = get_func;
1111 lg_xmit->data_info->release_func = release_func;
1112 lg_xmit->data_info->app_ptr = app_ptr;
1113 pdu->lg_xmit = lg_xmit;
1114 coap_ticks(&lg_xmit->last_obs);
1115 coap_ticks(&lg_xmit->last_sent);
1116 if (COAP_PDU_IS_REQUEST(pdu)) {
1117 /* Need to keep original token for updating response PDUs */
1118 lg_xmit->b.b1.app_token = coap_new_binary(pdu->actual_token.length);
1119 if (!lg_xmit->b.b1.app_token)
1120 goto fail;
1121 memcpy(lg_xmit->b.b1.app_token->s, pdu->actual_token.s,
1122 pdu->actual_token.length);
1123 /*
1124 * Need to set up new token for use during transmits
1125 * RFC9177#section-5
1126 */
1127 lg_xmit->b.b1.count = 1;
1128 lg_xmit->b.b1.state_token = STATE_TOKEN_FULL(++session->tx_token,
1129 lg_xmit->b.b1.count);
1130 coap_address_copy(&lg_xmit->b.b1.upstream, &session->addr_info.remote);
1131 /*
1132 * Token will be updated in pdu later as original pdu may be needed in
1133 * coap_send()
1134 */
1137 coap_encode_var_safe(buf, sizeof(buf),
1138 (unsigned int)length),
1139 buf);
1140 if (!coap_check_option(pdu, COAP_OPTION_RTAG, &opt_iter))
1143 coap_encode_var_safe(buf, sizeof(buf),
1144 ++session->tx_rtag),
1145 buf);
1146 } else {
1147 /*
1148 * resource+query+rtag match is used for Block2 large body transmissions
1149 * token match is used for Block1 large body transmissions
1150 */
1151 lg_xmit->b.b2.resource = resource;
1152 if (query) {
1153 lg_xmit->b.b2.query = coap_new_string(query->length);
1154 if (lg_xmit->b.b2.query) {
1155 memcpy(lg_xmit->b.b2.query->s, query->s, query->length);
1156 }
1157 } else {
1158 lg_xmit->b.b2.query = NULL;
1159 }
1160 opt = coap_check_option(request, COAP_OPTION_RTAG, &opt_iter);
1161 if (opt) {
1162 lg_xmit->b.b2.rtag_length = (uint8_t)min(coap_opt_length(opt),
1163 sizeof(lg_xmit->b.b2.rtag));
1164 memcpy(lg_xmit->b.b2.rtag, coap_opt_value(opt), coap_opt_length(opt));
1165 lg_xmit->b.b2.rtag_set = 1;
1166 } else {
1167 lg_xmit->b.b2.rtag_set = 0;
1168 }
1169 lg_xmit->b.b2.etag = etag;
1170 lg_xmit->b.b2.request_method = request_method;
1171 if (maxage >= 0) {
1172 coap_tick_t now;
1173
1174 coap_ticks(&now);
1175 lg_xmit->b.b2.maxage_expire = coap_ticks_to_rt(now) + maxage;
1176 } else {
1177 lg_xmit->b.b2.maxage_expire = 0;
1178 }
1181 coap_encode_var_safe(buf, sizeof(buf),
1182 (unsigned int)length),
1183 buf);
1184 }
1185
1186 if (!setup_block_b(session, pdu, &block, block.num,
1187 blk_size, lg_xmit->data_info->length))
1188 goto fail;
1189
1190 /* Add in with requested block num, more bit and block size */
1192 lg_xmit->option,
1193 coap_encode_var_safe(buf, sizeof(buf),
1194 (block.num << 4) | (block.m << 3) | block.aszx),
1195 buf);
1196
1197 /* Reference PDU to use as a basis for all the subsequent blocks */
1198 lg_xmit->sent_pdu = coap_pdu_reference_lkd(pdu);
1199
1200 /* Check we still have space after adding in some options */
1201 token_options = pdu->data ? (size_t)(pdu->data - pdu->token) : pdu->used_size;
1202 avail = pdu->max_size - token_options;
1203 /* There may be a response with Echo option */
1205 /* May need token of length 8, so account for this */
1206 avail -= (pdu->actual_token.length < 8) ? 8 - pdu->actual_token.length : 0;
1207#if COAP_OSCORE_SUPPORT
1208 avail -= coap_oscore_overhead(session, pdu);
1209#endif /* COAP_OSCORE_SUPPORT */
1210 if (avail < (ssize_t)chunk) {
1211 /* chunk size change down */
1212 if (avail < 16) {
1213 coap_log_warn("not enough space, even the smallest block does not fit (3)\n");
1214 goto fail;
1215 }
1216 blk_size = coap_flsll((long long)avail) - 4 - 1;
1217 block.num = block.num << (lg_xmit->blk_size - blk_size);
1218 lg_xmit->blk_size = blk_size;
1219 chunk = (size_t)1 << (lg_xmit->blk_size + 4);
1220 block.chunk_size = (uint32_t)chunk;
1221 block.bert = 0;
1223 lg_xmit->option,
1224 coap_encode_var_safe(buf, sizeof(buf),
1225 (block.num << 4) | (block.m << 3) | lg_xmit->blk_size),
1226 buf);
1227 }
1228
1229 rem = block.chunk_size;
1230 if (rem > lg_xmit->data_info->length - block.num * chunk)
1231 rem = lg_xmit->data_info->length - block.num * chunk;
1232 if (get_func) {
1233#if COAP_CONSTRAINED_STACK
1234 /* Protected by global_lock if needed */
1235 static uint8_t l_data[1024];
1236#else /* ! COAP_CONSTRAINED_STACK */
1237 uint8_t l_data[1024];
1238#endif /* ! COAP_CONSTRAINED_STACK */
1239 size_t l_length;
1240
1241 assert(rem <= 1024);
1242 if (get_func(session, rem, block.num * chunk, l_data, &l_length, lg_xmit->data_info->app_ptr)) {
1243 if (!coap_add_data(pdu, l_length, l_data)) {
1244 goto fail;
1245 }
1246 }
1247 } else {
1248 if (!coap_add_data(pdu, rem, &data[block.num * chunk]))
1249 goto fail;
1250 }
1251
1252 if (COAP_PDU_IS_REQUEST(pdu))
1253 lg_xmit->b.b1.bert_size = rem;
1254
1255 lg_xmit->last_block = -1;
1256
1257 /* Link the new lg_xmit in */
1258 LL_PREPEND(session->lg_xmit,lg_xmit);
1259 } else {
1260 /* No need to use blocks */
1261 if (have_block_defined) {
1263 option,
1264 coap_encode_var_safe(buf, sizeof(buf),
1265 (0 << 4) | (0 << 3) | blk_size), buf);
1266 }
1267add_data:
1268 if (get_func) {
1269 uint8_t *l_data = coap_malloc_type(COAP_STRING, length);
1270 size_t l_length;
1271
1272 if (get_func(session, length, 0, l_data, &l_length, app_ptr)) {
1273 if (!coap_add_data(pdu, l_length, l_data)) {
1274 coap_free_type(COAP_STRING, l_data);
1275 goto fail;
1276 }
1277 coap_free_type(COAP_STRING, l_data);
1278 }
1279 } else {
1280 if (!coap_add_data(pdu, length, data))
1281 goto fail;
1282 }
1283
1284 if (release_func) {
1285 coap_lock_callback(release_func(session, app_ptr));
1286 }
1287 }
1288 return 1;
1289
1290fail:
1291 if (lg_xmit) {
1292 coap_block_delete_lg_xmit(session, lg_xmit);
1293 } else if (release_func) {
1294 coap_lock_callback(release_func(session, app_ptr));
1295 }
1296 return 0;
1297}
1298
1299#if COAP_CLIENT_SUPPORT
1300COAP_API int
1302 coap_pdu_t *pdu,
1303 size_t length,
1304 const uint8_t *data,
1305 coap_release_large_data_t release_func,
1306 void *app_ptr
1307 ) {
1308 int ret;
1309
1310 coap_lock_lock(return 0);
1311 ret = coap_add_data_large_request_lkd(session, pdu, length, data,
1312 release_func, app_ptr);
1314 return ret;
1315}
1316
1317int
1318coap_add_data_large_request_lkd(coap_session_t *session,
1319 coap_pdu_t *pdu,
1320 size_t length,
1321 const uint8_t *data,
1322 coap_release_large_data_t release_func,
1323 void *app_ptr) {
1324 /*
1325 * Delay if session->doing_first is set.
1326 * E.g. Reliable and CSM not in yet for checking block support
1327 */
1328 if (coap_client_delay_first(session) == 0) {
1329 if (release_func) {
1330 coap_lock_callback(release_func(session, app_ptr));
1331 }
1332 return 0;
1333 }
1334 return coap_add_data_large_internal(session, NULL, pdu, NULL, NULL, -1, 0,
1335 length, data, release_func, NULL, app_ptr, 0, 0);
1336}
1337
1338COAP_API int
1340 coap_pdu_t *pdu,
1341 size_t length,
1342 coap_release_large_data_t release_func,
1343 coap_get_large_data_t get_func,
1344 void *app_ptr) {
1345 int ret;
1346
1347 coap_lock_lock(return 0);
1348 ret = coap_add_data_large_request_app_lkd(session, pdu, length,
1349 release_func, get_func, app_ptr);
1351 return ret;
1352}
1353
1354int
1355coap_add_data_large_request_app_lkd(coap_session_t *session,
1356 coap_pdu_t *pdu,
1357 size_t length,
1358 coap_release_large_data_t release_func,
1359 coap_get_large_data_t get_func,
1360 void *app_ptr) {
1361 /*
1362 * Delay if session->doing_first is set.
1363 * E.g. Reliable and CSM not in yet for checking block support
1364 */
1365 if (coap_client_delay_first(session) == 0) {
1366 return 0;
1367 }
1368 return coap_add_data_large_internal(session, NULL, pdu, NULL, NULL, -1, 0,
1369 length, NULL, release_func, get_func,
1370 app_ptr, 0, 0);
1371}
1372#endif /* ! COAP_CLIENT_SUPPORT */
1373
1374#if COAP_SERVER_SUPPORT
1375COAP_API int
1377 coap_session_t *session,
1378 const coap_pdu_t *request,
1379 coap_pdu_t *response,
1380 const coap_string_t *query,
1381 uint16_t media_type,
1382 int maxage,
1383 uint64_t etag,
1384 size_t length,
1385 const uint8_t *data,
1386 coap_release_large_data_t release_func,
1387 void *app_ptr) {
1388 int ret;
1389
1390 coap_lock_lock(return 0);
1391 ret = coap_add_data_large_response_lkd(resource, session, request,
1392 response, query, media_type, maxage, etag,
1393 length, data, release_func, app_ptr);
1395 return ret;
1396}
1397
1398int
1399coap_add_data_large_response_lkd(coap_resource_t *resource,
1400 coap_session_t *session,
1401 const coap_pdu_t *request,
1402 coap_pdu_t *response,
1403 const coap_string_t *query,
1404 uint16_t media_type,
1405 int maxage,
1406 uint64_t etag,
1407 size_t length,
1408 const uint8_t *data,
1409 coap_release_large_data_t release_func,
1410 void *app_ptr
1411 ) {
1412 unsigned char buf[4];
1413 coap_block_b_t block;
1414 int block_requested = 0;
1415 int single_request = 0;
1416#if COAP_Q_BLOCK_SUPPORT
1417 uint32_t block_opt = (session->block_mode & COAP_BLOCK_HAS_Q_BLOCK) ?
1419#else /* ! COAP_Q_BLOCK_SUPPORT */
1420 uint16_t block_opt = COAP_OPTION_BLOCK2;
1421#endif /* ! COAP_Q_BLOCK_SUPPORT */
1422
1423 memset(&block, 0, sizeof(block));
1424 /*
1425 * Need to check that a valid block is getting asked for so that the
1426 * correct options are put into the PDU.
1427 */
1428 if (request) {
1429 if (coap_get_block_b(session, request, COAP_OPTION_BLOCK2, &block)) {
1430 block_requested = 1;
1431 if (block.num != 0 && length <= (block.num << (block.szx + 4))) {
1432 coap_log_debug("Illegal block requested (%d > last = %" PRIuS ")\n",
1433 block.num,
1434 length >> (block.szx + 4));
1435 response->code = COAP_RESPONSE_CODE(400);
1436 goto error;
1437 }
1438 }
1439#if COAP_Q_BLOCK_SUPPORT
1440 else if (coap_get_block_b(session, request, COAP_OPTION_Q_BLOCK2, &block)) {
1441 block_requested = 1;
1442 if (block.num != 0 && length <= (block.num << (block.szx + 4))) {
1443 coap_log_debug("Illegal block requested (%d > last = %" PRIuS ")\n",
1444 block.num,
1445 length >> (block.szx + 4));
1446 response->code = COAP_RESPONSE_CODE(400);
1447 goto error;
1448 }
1449 if (!(session->block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
1450 set_block_mode_has_q(session->block_mode);
1451 block_opt = COAP_OPTION_Q_BLOCK2;
1452 }
1453 if (block.m == 0)
1454 single_request = 1;
1455 }
1456#endif /* COAP_Q_BLOCK_SUPPORT */
1457 }
1458
1460 coap_encode_var_safe(buf, sizeof(buf),
1461 media_type),
1462 buf);
1463
1464 if (maxage >= 0) {
1465 coap_update_option(response,
1467 coap_encode_var_safe(buf, sizeof(buf), maxage), buf);
1468 }
1469
1470 if (block_requested) {
1471 int res;
1472
1473 res = coap_write_block_b_opt(session, &block, block_opt, response,
1474 length);
1475
1476 switch (res) {
1477 case -2: /* illegal block (caught above) */
1478 response->code = COAP_RESPONSE_CODE(400);
1479 goto error;
1480 case -1: /* should really not happen */
1481 assert(0);
1482 /* fall through if assert is a no-op */
1483 case -3: /* cannot handle request */
1484 response->code = COAP_RESPONSE_CODE(500);
1485 goto error;
1486 default: /* everything is good */
1487 ;
1488 }
1489 }
1490
1491 /* add data body */
1492 if (request &&
1493 !coap_add_data_large_internal(session, request, response, resource,
1494 query, maxage, etag, length, data,
1495 release_func, NULL, app_ptr, single_request,
1496 request->code)) {
1497 response->code = COAP_RESPONSE_CODE(500);
1498 goto error_released;
1499 }
1500
1501 return 1;
1502
1503error:
1504 if (release_func) {
1505 coap_lock_callback(release_func(session, app_ptr));
1506 }
1507error_released:
1508#if COAP_ERROR_PHRASE_LENGTH > 0
1509 coap_add_data(response,
1510 strlen(coap_response_phrase(response->code)),
1511 (const unsigned char *)coap_response_phrase(response->code));
1512#endif /* COAP_ERROR_PHRASE_LENGTH > 0 */
1513 return 0;
1514}
1515#endif /* ! COAP_SERVER_SUPPORT */
1516
1517/*
1518 * return 1 if there is a future expire time, else 0.
1519 * update tim_rem with remaining value if return is 1.
1520 */
1521int
1523 coap_tick_t *tim_rem) {
1524 coap_lg_xmit_t *lg_xmit;
1525 coap_lg_xmit_t *q;
1526#if COAP_Q_BLOCK_SUPPORT
1527 coap_tick_t idle_timeout = 4 * COAP_NON_TIMEOUT_TICKS(session);
1528#else /* ! COAP_Q_BLOCK_SUPPORT */
1529 coap_tick_t idle_timeout = 8 * COAP_TICKS_PER_SECOND;
1530#endif /* ! COAP_Q_BLOCK_SUPPORT */
1531 coap_tick_t partial_timeout = COAP_MAX_TRANSMIT_WAIT_TICKS(session);
1532 int ret = 0;
1533
1534 *tim_rem = COAP_MAX_DELAY_TICKS;
1535
1536 LL_FOREACH_SAFE(session->lg_xmit, lg_xmit, q) {
1537 if (lg_xmit->last_all_sent) {
1538 if (lg_xmit->last_all_sent + idle_timeout <= now) {
1539 /* Expire this entry */
1540 LL_DELETE(session->lg_xmit, lg_xmit);
1541 coap_block_delete_lg_xmit(session, lg_xmit);
1542 } else {
1543 /* Delay until the lg_xmit needs to expire */
1544 if (*tim_rem > lg_xmit->last_all_sent + idle_timeout - now) {
1545 *tim_rem = lg_xmit->last_all_sent + idle_timeout - now;
1546 ret = 1;
1547 }
1548 }
1549 } else if (lg_xmit->last_sent) {
1550 if (lg_xmit->last_sent + partial_timeout <= now) {
1551 /* Expire this entry */
1552 int is_mcast = 0;
1553#if COAP_CLIENT_SUPPORT
1554 is_mcast = COAP_PDU_IS_REQUEST(lg_xmit->sent_pdu) &&
1555 coap_is_mcast(&lg_xmit->b.b1.upstream);
1556#endif /* COAP_CLIENT_SUPPORT */
1557 LL_DELETE(session->lg_xmit, lg_xmit);
1558
1559 coap_block_delete_lg_xmit(session, lg_xmit);
1560 if (!is_mcast)
1562 } else {
1563 /* Delay until the lg_xmit needs to expire */
1564 if (*tim_rem > lg_xmit->last_sent + partial_timeout - now) {
1565 *tim_rem = lg_xmit->last_sent + partial_timeout - now;
1566 ret = 1;
1567 }
1568 }
1569 }
1570 }
1571 return ret;
1572}
1573
1574#if COAP_CLIENT_SUPPORT
1575#if COAP_Q_BLOCK_SUPPORT
1576static coap_pdu_t *
1577coap_build_missing_pdu(coap_session_t *session, coap_lg_crcv_t *lg_crcv) {
1578 coap_pdu_t *pdu;
1579 coap_opt_filter_t drop_options;
1580 uint64_t token = STATE_TOKEN_FULL(lg_crcv->state_token, ++lg_crcv->retry_counter);
1581 uint8_t buf[8];
1582 size_t len = coap_encode_var_safe8(buf, sizeof(token), token);
1583
1584 memset(&drop_options, 0, sizeof(coap_opt_filter_t));
1588 pdu = coap_pdu_duplicate_lkd(lg_crcv->sent_pdu, session, len, buf,
1589 &drop_options);
1590 if (!pdu)
1591 return NULL;
1592 pdu->type = lg_crcv->last_type;
1593 return pdu;
1594}
1595
1596static void
1597coap_request_missing_q_block2(coap_session_t *session, coap_lg_crcv_t *lg_crcv) {
1598 uint8_t buf[8];
1599 uint32_t i;
1600 int block = -1; /* Last one seen */
1601 size_t sofar;
1602 size_t block_size;
1603 coap_pdu_t *pdu = NULL;
1604 int block_payload_set = -1;
1605
1606 if (session->block_mode & COAP_BLOCK_USE_M_Q_BLOCK) {
1607 /*
1608 * See if it is safe to use the single 'M' block variant of request
1609 *
1610 * If any blocks seen, then missing blocks are after range[0].end and
1611 * terminate on the last block or before range[1].begin if set.
1612 * If not defined or range[1].begin is in a different payload set then
1613 * safe to use M bit.
1614 */
1615 if (lg_crcv->rec_blocks.used &&
1616 (lg_crcv->rec_blocks.used < 2 ||
1617 ((lg_crcv->rec_blocks.range[0].end + 1) / COAP_MAX_PAYLOADS(session) !=
1618 (lg_crcv->rec_blocks.range[1].begin -1) / COAP_MAX_PAYLOADS(session)))) {
1619 block = lg_crcv->rec_blocks.range[0].end + 1;
1620 block_size = (size_t)1 << (lg_crcv->szx + 4);
1621 sofar = block * block_size;
1622 if (sofar < lg_crcv->total_len) {
1623 /* Ask for missing blocks */
1624 if (pdu == NULL) {
1625 pdu = coap_build_missing_pdu(session, lg_crcv);
1626 if (!pdu)
1627 return;
1628 }
1630 coap_encode_var_safe(buf, sizeof(buf),
1631 (block << 4) | (1 << 3) | lg_crcv->szx),
1632 buf);
1633 block_payload_set = block / COAP_MAX_PAYLOADS(session);
1634 goto send_it;
1635 }
1636 }
1637 }
1638 block = -1;
1639 for (i = 0; i < lg_crcv->rec_blocks.used; i++) {
1640 if (block < (int)lg_crcv->rec_blocks.range[i].begin &&
1641 lg_crcv->rec_blocks.range[i].begin != 0) {
1642 /* Ask for missing blocks */
1643 if (pdu == NULL) {
1644 pdu = coap_build_missing_pdu(session, lg_crcv);
1645 if (!pdu)
1646 continue;
1647 }
1648 block++;
1649 if (block_payload_set == -1)
1650 block_payload_set = block / COAP_MAX_PAYLOADS(session);
1651 for (; block < (int)lg_crcv->rec_blocks.range[i].begin &&
1652 block_payload_set == (block / COAP_MAX_PAYLOADS(session)); block++) {
1654 coap_encode_var_safe(buf, sizeof(buf),
1655 (block << 4) | (0 << 3) | lg_crcv->szx),
1656 buf);
1657 }
1658 }
1659 if (block < (int)lg_crcv->rec_blocks.range[i].end) {
1660 block = lg_crcv->rec_blocks.range[i].end;
1661 }
1662 }
1663 block_size = (size_t)1 << (lg_crcv->szx + 4);
1664 sofar = (block + 1) * block_size;
1665 if (sofar < lg_crcv->total_len) {
1666 /* Ask for trailing missing blocks */
1667 if (pdu == NULL) {
1668 pdu = coap_build_missing_pdu(session, lg_crcv);
1669 if (!pdu)
1670 return;
1671 }
1672 sofar = (lg_crcv->total_len + block_size - 1)/block_size;
1673 block++;
1674 if (block_payload_set == -1)
1675 block_payload_set = block / COAP_MAX_PAYLOADS(session);
1676 for (; block < (ssize_t)sofar &&
1677 block_payload_set == (block / COAP_MAX_PAYLOADS(session)); block++) {
1679 coap_encode_var_safe(buf, sizeof(buf),
1680 (block << 4) | (0 << 3) | lg_crcv->szx),
1681 buf);
1682 }
1683 }
1684send_it:
1685 if (pdu)
1686 coap_send_internal(session, pdu, NULL);
1687 lg_crcv->rec_blocks.retry++;
1688 if (block_payload_set != -1)
1689 lg_crcv->rec_blocks.processing_payload_set = block_payload_set;
1690 coap_ticks(&lg_crcv->rec_blocks.last_seen);
1691}
1692#endif /* COAP_Q_BLOCK_SUPPORT */
1693
1694/*
1695 * return 1 if there is a future expire time, else 0.
1696 * update tim_rem with remaining value if return is 1.
1697 */
1698int
1699coap_block_check_lg_crcv_timeouts(coap_session_t *session, coap_tick_t now,
1700 coap_tick_t *tim_rem) {
1701 coap_lg_crcv_t *lg_crcv;
1702 coap_lg_crcv_t *q;
1703 coap_tick_t partial_timeout;
1704#if COAP_Q_BLOCK_SUPPORT
1705 coap_tick_t receive_timeout = COAP_NON_RECEIVE_TIMEOUT_TICKS(session);
1706#endif /* COAP_Q_BLOCK_SUPPORT */
1707 int ret = 0;
1708
1709 *tim_rem = COAP_MAX_DELAY_TICKS;
1710#if COAP_Q_BLOCK_SUPPORT
1711 if (COAP_PROTO_NOT_RELIABLE(session->proto) &&
1712 session->block_mode & COAP_BLOCK_HAS_Q_BLOCK)
1713 partial_timeout = COAP_NON_PARTIAL_TIMEOUT_TICKS(session);
1714 else
1715#endif /* COAP_Q_BLOCK_SUPPORT */
1716 partial_timeout = COAP_MAX_TRANSMIT_WAIT_TICKS(session);
1717
1718 LL_FOREACH_SAFE(session->lg_crcv, lg_crcv, q) {
1719 if (COAP_PROTO_RELIABLE(session->proto) || lg_crcv->last_type != COAP_MESSAGE_NON)
1720 goto check_expire;
1721
1722#if COAP_Q_BLOCK_SUPPORT
1723 if (lg_crcv->block_option == COAP_OPTION_Q_BLOCK2 && lg_crcv->rec_blocks.used) {
1724 size_t scaled_timeout = receive_timeout *
1725 ((size_t)1 << lg_crcv->rec_blocks.retry);
1726
1727 if (lg_crcv->rec_blocks.retry >= COAP_NON_MAX_RETRANSMIT(session)) {
1728 /* Done NON_MAX_RETRANSMIT retries */
1729 coap_handle_nack(session, lg_crcv->sent_pdu,
1730 COAP_NACK_TOO_MANY_RETRIES, lg_crcv->sent_pdu->mid);
1731 goto expire;
1732 }
1733 if (lg_crcv->rec_blocks.last_seen + scaled_timeout <= now) {
1734 coap_request_missing_q_block2(session, lg_crcv);
1735 } else {
1736 if (*tim_rem > lg_crcv->rec_blocks.last_seen + scaled_timeout - now) {
1737 *tim_rem = lg_crcv->rec_blocks.last_seen + scaled_timeout - now;
1738 ret = 1;
1739 }
1740 }
1741 }
1742#endif /* COAP_Q_BLOCK_SUPPORT */
1743 /* Used for Block2 and Q-Block2 */
1744check_expire:
1745 if (!lg_crcv->observe_set && lg_crcv->last_used &&
1746 lg_crcv->last_used + partial_timeout <= now) {
1747#if COAP_Q_BLOCK_SUPPORT
1748expire:
1749#endif /* COAP_Q_BLOCK_SUPPORT */
1750 /* Expire this entry */
1751 LL_DELETE(session->lg_crcv, lg_crcv);
1752 coap_block_delete_lg_crcv(session, lg_crcv);
1753 } else if (!lg_crcv->observe_set && lg_crcv->last_used) {
1754 /* Delay until the lg_crcv needs to expire */
1755 if (*tim_rem > lg_crcv->last_used + partial_timeout - now) {
1756 *tim_rem = lg_crcv->last_used + partial_timeout - now;
1757 ret = 1;
1758 }
1759 }
1760 }
1761 return ret;
1762}
1763#endif /* COAP_CLIENT_SUPPORT */
1764
1765#if COAP_SERVER_SUPPORT
1766#if COAP_Q_BLOCK_SUPPORT
1767static coap_pdu_t *
1768pdu_408_build(coap_session_t *session, coap_lg_srcv_t *lg_srcv) {
1769 coap_pdu_t *pdu;
1770 uint8_t buf[4];
1771
1773 COAP_RESPONSE_CODE(408),
1774 coap_new_message_id_lkd(session),
1776 if (!pdu)
1777 return NULL;
1778 if (lg_srcv->last_token)
1779 coap_add_token(pdu, lg_srcv->last_token->length, lg_srcv->last_token->s);
1781 coap_encode_var_safe(buf, sizeof(buf),
1783 buf);
1784 pdu->token[pdu->used_size++] = COAP_PAYLOAD_START;
1785 pdu->data = pdu->token + pdu->used_size;
1786 return pdu;
1787}
1788
1789static int
1790add_408_block(coap_pdu_t *pdu, int block) {
1791 size_t len;
1792 uint8_t val[8];
1793
1794 assert(block >= 0 && block < (1 << 20));
1795
1796 if (block < 0 || block >= (1 << 20)) {
1797 return 0;
1798 } else if (block < 24) {
1799 len = 1;
1800 val[0] = block;
1801 } else if (block < 0x100) {
1802 len = 2;
1803 val[0] = 24;
1804 val[1] = block;
1805 } else if (block < 0x10000) {
1806 len = 3;
1807 val[0] = 25;
1808 val[1] = block >> 8;
1809 val[2] = block & 0xff;
1810 } else { /* Largest block number is 2^^20 - 1 */
1811 len = 4;
1812 val[0] = 26;
1813 val[1] = block >> 16;
1814 val[2] = (block >> 8) & 0xff;
1815 val[3] = block & 0xff;
1816 }
1817 if (coap_pdu_check_resize(pdu, pdu->used_size + len)) {
1818 memcpy(&pdu->token[pdu->used_size], val, len);
1819 pdu->used_size += len;
1820 return 1;
1821 }
1822 return 0;
1823}
1824#endif /* COAP_Q_BLOCK_SUPPORT */
1825#endif /* COAP_SERVER_SUPPORT */
1826
1827static int
1828check_if_received_block(coap_rblock_t *rec_blocks, uint32_t block_num) {
1829 uint32_t i;
1830
1831 for (i = 0; i < rec_blocks->used; i++) {
1832 if (block_num < rec_blocks->range[i].begin)
1833 return 0;
1834 if (block_num <= rec_blocks->range[i].end)
1835 return 1;
1836 }
1837 return 0;
1838}
1839
1840#if COAP_SERVER_SUPPORT
1841static int
1842check_if_next_block(coap_rblock_t *rec_blocks, uint32_t block_num) {
1843 if (rec_blocks->used == 0) {
1844 return block_num == 0 ? 1 : 0;
1845 }
1846 if (rec_blocks->range[rec_blocks->used-1].end + 1 == block_num)
1847 return 1;
1848
1849 return 0;
1850}
1851#endif /* COAP_SERVER_SUPPORT */
1852
1853static int
1855 uint32_t i;
1856 uint32_t block = 0;
1857
1858 if (rec_blocks->total_blocks == 0) {
1859 /* Not seen block with More bit unset yet */
1860 return 0;
1861 }
1862
1863 for (i = 0; i < rec_blocks->used; i++) {
1864 if (block < rec_blocks->range[i].begin)
1865 return 0;
1866 if (block < rec_blocks->range[i].end)
1867 block = rec_blocks->range[i].end;
1868 }
1869 return 1;
1870}
1871
1872#if COAP_CLIENT_SUPPORT
1873#if COAP_Q_BLOCK_SUPPORT
1874static int
1875check_all_blocks_in_for_payload_set(coap_session_t *session,
1876 coap_rblock_t *rec_blocks) {
1877 if (rec_blocks->used &&
1878 (rec_blocks->range[0].end + 1) / COAP_MAX_PAYLOADS(session) >
1879 rec_blocks->processing_payload_set)
1880 return 1;
1881 return 0;
1882}
1883
1884static int
1885check_any_blocks_next_payload_set(coap_session_t *session,
1886 coap_rblock_t *rec_blocks) {
1887 if (rec_blocks->used > 1 &&
1888 rec_blocks->range[1].begin / COAP_MAX_PAYLOADS(session) ==
1889 rec_blocks->processing_payload_set)
1890 return 1;
1891 return 0;
1892}
1893#endif /* COAP_Q_BLOCK_SUPPORT */
1894#endif /* COAP_CLIENT_SUPPORT */
1895
1896#if COAP_SERVER_SUPPORT
1897/*
1898 * return 1 if there is a future expire time, else 0.
1899 * update tim_rem with remaining value if return is 1.
1900 */
1901int
1902coap_block_check_lg_srcv_timeouts(coap_session_t *session, coap_tick_t now,
1903 coap_tick_t *tim_rem) {
1904 coap_lg_srcv_t *lg_srcv;
1905 coap_lg_srcv_t *q;
1906 coap_tick_t partial_timeout;
1907#if COAP_Q_BLOCK_SUPPORT
1908 coap_tick_t receive_timeout = COAP_NON_RECEIVE_TIMEOUT_TICKS(session);
1909#endif /* COAP_Q_BLOCK_SUPPORT */
1910 int ret = 0;
1911
1912 *tim_rem = COAP_MAX_DELAY_TICKS;
1913#if COAP_Q_BLOCK_SUPPORT
1914 if (COAP_PROTO_NOT_RELIABLE(session->proto) &&
1915 session->block_mode & COAP_BLOCK_HAS_Q_BLOCK)
1916 partial_timeout = COAP_NON_PARTIAL_TIMEOUT_TICKS(session);
1917 else
1918#endif /* COAP_Q_BLOCK_SUPPORT */
1919 partial_timeout = COAP_MAX_TRANSMIT_WAIT_TICKS(session);
1920
1921 LL_FOREACH_SAFE(session->lg_srcv, lg_srcv, q) {
1922 if (lg_srcv->dont_timeout) {
1923 /* Not safe to timeout at present */
1924 continue;
1925 }
1926 if (COAP_PROTO_RELIABLE(session->proto) || lg_srcv->last_type != COAP_MESSAGE_NON)
1927 goto check_expire;
1928
1929#if COAP_Q_BLOCK_SUPPORT
1930 if (lg_srcv->block_option == COAP_OPTION_Q_BLOCK1 && lg_srcv->rec_blocks.used) {
1931 size_t scaled_timeout = receive_timeout *
1932 ((size_t)1 << lg_srcv->rec_blocks.retry);
1933
1934 if (lg_srcv->rec_blocks.retry >= COAP_NON_MAX_RETRANSMIT(session)) {
1935 /* Done NON_MAX_RETRANSMIT retries */
1936 goto expire;
1937 }
1938 if (lg_srcv->rec_blocks.last_seen + scaled_timeout <= now) {
1939 uint32_t i;
1940 int block = -1; /* Last one seen */
1941 size_t block_size = (size_t)1 << (lg_srcv->szx + 4);
1942 size_t final_block = (lg_srcv->total_len + block_size - 1)/block_size - 1;
1943 size_t cur_payload;
1944 size_t last_payload_block;
1945 coap_pdu_t *pdu = NULL;
1946 size_t no_blocks = 0;
1947
1948 /* Need to count the number of missing blocks */
1949 for (i = 0; i < lg_srcv->rec_blocks.used; i++) {
1950 if (block < (int)lg_srcv->rec_blocks.range[i].begin &&
1951 lg_srcv->rec_blocks.range[i].begin != 0) {
1952 block++;
1953 no_blocks += lg_srcv->rec_blocks.range[i].begin - block;
1954 }
1955 if (block < (int)lg_srcv->rec_blocks.range[i].end) {
1956 block = lg_srcv->rec_blocks.range[i].end;
1957 }
1958 }
1959 if (no_blocks == 0 && block == (int)final_block)
1960 goto expire;
1961
1962 /* Include missing up to end of current payload or total amount */
1963 cur_payload = block / COAP_MAX_PAYLOADS(session);
1964 last_payload_block = (cur_payload + 1) * COAP_MAX_PAYLOADS(session) - 1;
1965 if (final_block > last_payload_block) {
1966 final_block = last_payload_block;
1967 }
1968 no_blocks += final_block - block;
1969 if (no_blocks == 0) {
1970 /* Add in the blocks out of the next payload */
1971 final_block = (lg_srcv->total_len + block_size - 1)/block_size - 1;
1972 last_payload_block += COAP_MAX_PAYLOADS(session);
1973 if (final_block > last_payload_block) {
1974 final_block = last_payload_block;
1975 }
1976 }
1977 /* Ask for the missing blocks */
1978 block = -1;
1979 for (i = 0; i < lg_srcv->rec_blocks.used; i++) {
1980 if (block < (int)lg_srcv->rec_blocks.range[i].begin &&
1981 lg_srcv->rec_blocks.range[i].begin != 0) {
1982 /* Report on missing blocks */
1983 if (pdu == NULL) {
1984 pdu = pdu_408_build(session, lg_srcv);
1985 if (!pdu)
1986 continue;
1987 }
1988 block++;
1989 for (; block < (int)lg_srcv->rec_blocks.range[i].begin; block++) {
1990 if (!add_408_block(pdu, block)) {
1991 break;
1992 }
1993 }
1994 }
1995 if (block < (int)lg_srcv->rec_blocks.range[i].end) {
1996 block = lg_srcv->rec_blocks.range[i].end;
1997 }
1998 }
1999 block++;
2000 for (; block <= (int)final_block; block++) {
2001 if (pdu == NULL) {
2002 pdu = pdu_408_build(session, lg_srcv);
2003 if (!pdu)
2004 continue;
2005 }
2006 if (!add_408_block(pdu, block)) {
2007 break;
2008 }
2009 }
2010 if (pdu)
2011 coap_send_internal(session, pdu, NULL);
2012 lg_srcv->rec_blocks.retry++;
2013 coap_ticks(&lg_srcv->rec_blocks.last_seen);
2014 }
2015 if (*tim_rem > lg_srcv->rec_blocks.last_seen + scaled_timeout - now) {
2016 *tim_rem = lg_srcv->rec_blocks.last_seen + scaled_timeout - now;
2017 ret = 1;
2018 }
2019 }
2020#endif /* COAP_Q_BLOCK_SUPPORT */
2021 /* Used for Block1 and Q-Block1 */
2022check_expire:
2023 if (lg_srcv->no_more_seen)
2024 partial_timeout = 10 * COAP_TICKS_PER_SECOND;
2025 if (lg_srcv->last_used && lg_srcv->last_used + partial_timeout <= now) {
2026#if COAP_Q_BLOCK_SUPPORT
2027expire:
2028#endif /* COAP_Q_BLOCK_SUPPORT */
2029 /* Expire this entry */
2030 if (lg_srcv->no_more_seen && lg_srcv->block_option == COAP_OPTION_BLOCK1) {
2031 /*
2032 * Need to send a separate 4.08 to indicate missing blocks
2033 * Using NON is permissable as per
2034 * https://datatracker.ietf.org/doc/html/rfc7252#section-5.2.3
2035 */
2036 coap_pdu_t *pdu;
2037
2039 COAP_RESPONSE_CODE(408),
2040 coap_new_message_id_lkd(session),
2042 if (pdu) {
2043 if (lg_srcv->last_token)
2044 coap_add_token(pdu, lg_srcv->last_token->length, lg_srcv->last_token->s);
2045 coap_add_data(pdu, sizeof("Missing interim block")-1,
2046 (const uint8_t *)"Missing interim block");
2047 coap_send_internal(session, pdu, NULL);
2048 }
2049 }
2050 LL_DELETE(session->lg_srcv, lg_srcv);
2051 coap_block_delete_lg_srcv(session, lg_srcv);
2052 } else if (lg_srcv->last_used) {
2053 /* Delay until the lg_srcv needs to expire */
2054 if (*tim_rem > lg_srcv->last_used + partial_timeout - now) {
2055 *tim_rem = lg_srcv->last_used + partial_timeout - now;
2056 ret = 1;
2057 }
2058 }
2059 }
2060 return ret;
2061}
2062#endif /* COAP_SERVER_SUPPORT */
2063
2064#if COAP_Q_BLOCK_SUPPORT
2065/*
2066 * pdu is always released before return IF COAP_SEND_INC_PDU
2067 */
2069coap_send_q_blocks(coap_session_t *session,
2070 coap_lg_xmit_t *lg_xmit,
2071 coap_block_b_t block,
2072 coap_pdu_t *pdu,
2073 coap_send_pdu_t send_pdu) {
2074 coap_pdu_t *block_pdu = NULL;
2075 coap_opt_filter_t drop_options;
2077 uint64_t token;
2078 const uint8_t *ptoken;
2079 uint8_t ltoken[8];
2080 size_t ltoken_length;
2081 uint32_t delayqueue_cnt = 0;
2082
2083 if (!lg_xmit) {
2084 if (send_pdu == COAP_SEND_INC_PDU)
2085 return coap_send_internal(session, pdu, NULL);
2086 return COAP_INVALID_MID;
2087 }
2088
2089 if (pdu->type == COAP_MESSAGE_CON) {
2090 coap_queue_t *delayqueue;
2091
2092 delayqueue_cnt = session->con_active +
2093 (send_pdu == COAP_SEND_INC_PDU ? 1 : 0);
2094 LL_FOREACH(session->delayqueue, delayqueue) {
2095 delayqueue_cnt++;
2096 }
2097 }
2098 pdu->lg_xmit = lg_xmit;
2099 if (block.m &&
2100 ((pdu->type == COAP_MESSAGE_NON &&
2101 ((block.num + 1) % COAP_MAX_PAYLOADS(session)) + 1 !=
2102 COAP_MAX_PAYLOADS(session)) ||
2103 (pdu->type == COAP_MESSAGE_ACK &&
2104 lg_xmit->option == COAP_OPTION_Q_BLOCK2) ||
2105 (pdu->type == COAP_MESSAGE_CON &&
2106 delayqueue_cnt < COAP_NSTART(session)) ||
2107 COAP_PROTO_RELIABLE(session->proto))) {
2108 /* Allocate next pdu if there is headroom */
2109 if (COAP_PDU_IS_RESPONSE(pdu)) {
2110 ptoken = pdu->actual_token.s;
2111 ltoken_length = pdu->actual_token.length;
2112 } else {
2113 token = STATE_TOKEN_FULL(lg_xmit->b.b1.state_token,++lg_xmit->b.b1.count);
2114 ltoken_length = coap_encode_var_safe8(ltoken, sizeof(token), token);
2115 ptoken = ltoken;
2116 }
2117
2118 memset(&drop_options, 0, sizeof(coap_opt_filter_t));
2119 coap_option_filter_set(&drop_options, lg_xmit->option);
2120 block_pdu = coap_pdu_duplicate_lkd(pdu, session,
2121 ltoken_length,
2122 ptoken, &drop_options);
2123 if (block_pdu->type == COAP_MESSAGE_ACK)
2124 block_pdu->type = COAP_MESSAGE_CON;
2125 }
2126
2127 /* Send initial pdu (which deletes 'pdu') */
2128 if (send_pdu == COAP_SEND_INC_PDU &&
2129 (mid = coap_send_internal(session, pdu, NULL)) == COAP_INVALID_MID) {
2130 /* Not expected, underlying issue somewhere */
2131 coap_delete_pdu_lkd(block_pdu);
2132 return COAP_INVALID_MID;
2133 }
2134
2135 while (block_pdu) {
2136 coap_pdu_t *t_pdu = NULL;
2137 uint8_t buf[8];
2138 size_t chunk = ((size_t)1 << (lg_xmit->blk_size + 4));
2139
2140 block.num++;
2141 lg_xmit->offset = block.num * chunk;
2142 block.m = lg_xmit->offset + chunk < lg_xmit->data_info->length;
2143 if (block.m && ((block_pdu->type == COAP_MESSAGE_NON &&
2144 (block.num % COAP_MAX_PAYLOADS(session)) + 1 !=
2145 COAP_MAX_PAYLOADS(session)) ||
2146 (block_pdu->type == COAP_MESSAGE_CON &&
2147 delayqueue_cnt + 1 < COAP_NSTART(session)) ||
2148 COAP_PROTO_RELIABLE(session->proto))) {
2149 /*
2150 * Send following block if
2151 * NON and more in MAX_PAYLOADS
2152 * CON and NSTART allows it (based on number in delayqueue)
2153 * Reliable transport
2154 */
2155 if (COAP_PDU_IS_RESPONSE(block_pdu)) {
2156 ptoken = block_pdu->actual_token.s;
2157 ltoken_length = block_pdu->actual_token.length;
2158 } else {
2159 token = STATE_TOKEN_FULL(lg_xmit->b.b1.state_token,++lg_xmit->b.b1.count);
2160 ltoken_length = coap_encode_var_safe8(ltoken, sizeof(token), token);
2161 ptoken = ltoken;
2162 }
2163 t_pdu = coap_pdu_duplicate_lkd(block_pdu, session,
2164 ltoken_length, ptoken, &drop_options);
2165 }
2166 if (!coap_update_option(block_pdu, lg_xmit->option,
2168 sizeof(buf),
2169 ((block.num) << 4) |
2170 (block.m << 3) |
2171 block.szx),
2172 buf)) {
2173 coap_log_warn("Internal update issue option\n");
2174 coap_delete_pdu_lkd(block_pdu);
2175 coap_delete_pdu_lkd(t_pdu);
2176 break;
2177 }
2178
2179 if (lg_xmit->data_info->get_func) {
2180#if COAP_CONSTRAINED_STACK
2181 /* Protected by global_lock if needed */
2182 static uint8_t l_data[1024];
2183#else /* ! COAP_CONSTRAINED_STACK */
2184 uint8_t l_data[1024];
2185#endif /* ! COAP_CONSTRAINED_STACK */
2186 size_t l_length;
2187
2188 assert(chunk <= 1024);
2189 if (lg_xmit->data_info->get_func(session, chunk,
2190 block.num * chunk, l_data, &l_length,
2191 lg_xmit->data_info->app_ptr)) {
2192 if (!coap_add_data(block_pdu, l_length, l_data)) {
2193 coap_log_warn("Internal update issue data (1)\n");
2194 coap_delete_pdu_lkd(block_pdu);
2195 coap_delete_pdu_lkd(t_pdu);
2196 break;
2197 }
2198 }
2199 } else {
2200 if (!coap_add_block(block_pdu,
2201 lg_xmit->data_info->length,
2202 lg_xmit->data_info->data,
2203 block.num,
2204 block.szx)) {
2205 coap_log_warn("Internal update issue data (2)\n");
2206 coap_delete_pdu_lkd(block_pdu);
2207 coap_delete_pdu_lkd(t_pdu);
2208 break;
2209 }
2210 }
2211 if (COAP_PDU_IS_RESPONSE(block_pdu)) {
2212 lg_xmit->last_block = block.num;
2213 }
2214 mid = coap_send_internal(session, block_pdu, NULL);
2215 if (mid == COAP_INVALID_MID) {
2216 /* Not expected, underlying issue somewhere */
2217 coap_delete_pdu_lkd(t_pdu);
2218 return COAP_INVALID_MID;
2219 }
2220 block_pdu = t_pdu;
2221 }
2222 if (!block.m) {
2223 lg_xmit->last_payload = 0;
2224 coap_ticks(&lg_xmit->last_all_sent);
2225 } else
2226 coap_ticks(&lg_xmit->last_payload);
2227 return mid;
2228}
2229
2230#if COAP_CLIENT_SUPPORT
2231/*
2232 * Return 1 if there is a future expire time, else 0.
2233 * Update tim_rem with remaining value if return is 1.
2234 */
2235int
2236coap_block_check_q_block1_xmit(coap_session_t *session, coap_tick_t now, coap_tick_t *tim_rem) {
2237 coap_lg_xmit_t *lg_xmit;
2238 coap_lg_xmit_t *q;
2239 coap_tick_t timed_out;
2240 int ret = 0;
2241
2242 *tim_rem = COAP_MAX_DELAY_TICKS;
2243 LL_FOREACH_SAFE(session->lg_xmit, lg_xmit, q) {
2244 coap_tick_t non_timeout = lg_xmit->non_timeout_random_ticks;
2245
2246 if (now <= non_timeout) {
2247 /* Too early in the startup cycle to have an accurate response */
2248 *tim_rem = non_timeout - now;
2249 return 1;
2250 }
2251 timed_out = now - non_timeout;
2252
2253 if (lg_xmit->last_payload) {
2254 if (lg_xmit->last_payload <= timed_out) {
2255 /* Send off the next MAX_PAYLOAD set */
2256 coap_block_b_t block;
2257 size_t chunk = (size_t)1 << (lg_xmit->blk_size + 4);
2258
2259 memset(&block, 0, sizeof(block));
2260 block.num = (uint32_t)(lg_xmit->offset / chunk);
2261 block.m = lg_xmit->offset + chunk < lg_xmit->data_info->length;
2262 block.szx = lg_xmit->blk_size;
2263 coap_send_q_blocks(session, lg_xmit, block, lg_xmit->sent_pdu, COAP_SEND_SKIP_PDU);
2264 if (*tim_rem > non_timeout) {
2265 *tim_rem = non_timeout;
2266 ret = 1;
2267 }
2268 } else {
2269 /* Delay until the next MAX_PAYLOAD needs to be sent off */
2270 if (*tim_rem > lg_xmit->last_payload - timed_out) {
2271 *tim_rem = lg_xmit->last_payload - timed_out;
2272 ret = 1;
2273 }
2274 }
2275 } else if (lg_xmit->last_all_sent) {
2276 non_timeout = COAP_NON_TIMEOUT_TICKS(session);
2277 if (lg_xmit->last_all_sent + 4 * non_timeout <= now) {
2278 /* Expire this entry */
2279 LL_DELETE(session->lg_xmit, lg_xmit);
2280 coap_block_delete_lg_xmit(session, lg_xmit);
2281 } else {
2282 /* Delay until the lg_xmit needs to expire */
2283 if (*tim_rem > lg_xmit->last_all_sent + 4 * non_timeout - now) {
2284 *tim_rem = lg_xmit->last_all_sent + 4 * non_timeout - now;
2285 ret = 1;
2286 }
2287 }
2288 }
2289 }
2290 return ret;
2291}
2292#endif /* COAP_CLIENT_SUPPORT */
2293
2294#if COAP_SERVER_SUPPORT
2295/*
2296 * Return 1 if there is a future expire time, else 0.
2297 * Update tim_rem with remaining value if return is 1.
2298 */
2299int
2300coap_block_check_q_block2_xmit(coap_session_t *session, coap_tick_t now, coap_tick_t *tim_rem) {
2301 coap_lg_xmit_t *lg_xmit;
2302 coap_lg_xmit_t *q;
2303 coap_tick_t timed_out;
2304 int ret = 0;
2305
2306 *tim_rem = (coap_tick_t)-1;
2307 LL_FOREACH_SAFE(session->lg_xmit, lg_xmit, q) {
2308 coap_tick_t non_timeout = lg_xmit->non_timeout_random_ticks;
2309
2310 if (now <= non_timeout) {
2311 /* Too early in the startup cycle to have an accurate response */
2312 *tim_rem = non_timeout - now;
2313 return 1;
2314 }
2315 timed_out = now - non_timeout;
2316
2317 if (lg_xmit->last_payload) {
2318 if (lg_xmit->last_payload <= timed_out) {
2319 /* Send off the next MAX_PAYLOAD set */
2320 coap_block_b_t block;
2321 size_t chunk = (size_t)1 << (lg_xmit->blk_size + 4);
2322
2323 memset(&block, 0, sizeof(block));
2324 block.num = (uint32_t)(lg_xmit->offset / chunk);
2325 block.m = lg_xmit->offset + chunk < lg_xmit->data_info->length;
2326 block.szx = lg_xmit->blk_size;
2327 if (block.num == (uint32_t)lg_xmit->last_block)
2328 coap_send_q_blocks(session, lg_xmit, block, lg_xmit->sent_pdu, COAP_SEND_SKIP_PDU);
2329 if (*tim_rem > non_timeout) {
2330 *tim_rem = non_timeout;
2331 ret = 1;
2332 }
2333 } else {
2334 /* Delay until the next MAX_PAYLOAD needs to be sent off */
2335 if (*tim_rem > lg_xmit->last_payload - timed_out) {
2336 *tim_rem = lg_xmit->last_payload - timed_out;
2337 ret = 1;
2338 }
2339 }
2340 } else if (lg_xmit->last_all_sent) {
2341 non_timeout = COAP_NON_TIMEOUT_TICKS(session);
2342 if (lg_xmit->last_all_sent + 4 * non_timeout <= now) {
2343 /* Expire this entry */
2344 LL_DELETE(session->lg_xmit, lg_xmit);
2345 coap_block_delete_lg_xmit(session, lg_xmit);
2346 } else {
2347 /* Delay until the lg_xmit needs to expire */
2348 if (*tim_rem > lg_xmit->last_all_sent + 4 * non_timeout - now) {
2349 *tim_rem = lg_xmit->last_all_sent + 4 * non_timeout - now;
2350 ret = 1;
2351 }
2352 }
2353 }
2354 }
2355 return ret;
2356}
2357#endif /* COAP_SERVER_SUPPORT */
2358#endif /* COAP_Q_BLOCK_SUPPORT */
2359
2360#if COAP_CLIENT_SUPPORT
2361/*
2362 * If Observe = 0, save the token away and return NULL
2363 * Else If Observe = 1, return the saved token for this block
2364 * Else, return NULL
2365 */
2366static coap_bin_const_t *
2367track_fetch_observe(coap_pdu_t *pdu, coap_lg_crcv_t *lg_crcv,
2368 uint32_t block_num, coap_bin_const_t *token) {
2369 /* Need to handle Observe for large FETCH */
2370 coap_opt_iterator_t opt_iter;
2372 &opt_iter);
2373
2374 if (opt && lg_crcv) {
2375 int observe_action = -1;
2376 coap_bin_const_t **tmp;
2377
2378 observe_action = coap_decode_var_bytes(coap_opt_value(opt),
2379 coap_opt_length(opt));
2380 if (observe_action == COAP_OBSERVE_ESTABLISH) {
2381 /* Save the token in lg_crcv */
2382 if (lg_crcv->obs_token_cnt <= block_num) {
2383 size_t i;
2384
2385 tmp = coap_realloc_type(COAP_STRING, lg_crcv->obs_token,
2386 (block_num + 1) * sizeof(lg_crcv->obs_token[0]));
2387 if (tmp == NULL)
2388 return NULL;
2389 lg_crcv->obs_token = tmp;
2390 for (i = lg_crcv->obs_token_cnt; i < block_num + 1; i++) {
2391 lg_crcv->obs_token[i] = NULL;
2392 }
2393 }
2394 coap_delete_bin_const(lg_crcv->obs_token[block_num]);
2395
2396 lg_crcv->obs_token_cnt = block_num + 1;
2397 lg_crcv->obs_token[block_num] = coap_new_bin_const(token->s,
2398 token->length);
2399 if (lg_crcv->obs_token[block_num] == NULL)
2400 return NULL;
2401 } else if (observe_action == COAP_OBSERVE_CANCEL) {
2402 /* Use the token in lg_crcv */
2403 if (block_num < lg_crcv->obs_token_cnt) {
2404 return lg_crcv->obs_token[block_num];
2405 }
2406 }
2407 }
2408 return NULL;
2409}
2410
2411#if COAP_Q_BLOCK_SUPPORT
2413coap_send_q_block1(coap_session_t *session,
2414 coap_block_b_t block,
2415 coap_pdu_t *request,
2416 coap_send_pdu_t send_request) {
2417 /* Need to send up to MAX_PAYLOAD blocks if this is a Q_BLOCK1 */
2418 coap_lg_xmit_t *lg_xmit;
2419 uint64_t token_match =
2421 request->actual_token.length));
2422
2423 LL_FOREACH(session->lg_xmit, lg_xmit) {
2424 if (lg_xmit->option == COAP_OPTION_Q_BLOCK1 &&
2425 (token_match == STATE_TOKEN_BASE(lg_xmit->b.b1.state_token) ||
2426 token_match ==
2428 lg_xmit->b.b1.app_token->length))))
2429 break;
2430 /* try out the next one */
2431 }
2432 return coap_send_q_blocks(session, lg_xmit, block, request, send_request);
2433}
2434#endif /* COAP_Q_BLOCK_SUPPORT */
2435#endif /* COAP_CLIENT_SUPPORT */
2436
2437#if COAP_SERVER_SUPPORT
2438#if COAP_Q_BLOCK_SUPPORT
2439/*
2440 * response is always released before return IF COAP_SEND_INC_PDU
2441 */
2443coap_send_q_block2(coap_session_t *session,
2444 coap_resource_t *resource,
2445 const coap_string_t *query,
2446 coap_pdu_code_t request_method,
2447 coap_block_b_t block,
2448 coap_pdu_t *response,
2449 coap_send_pdu_t send_response) {
2450 /* Need to send up to MAX_PAYLOAD blocks if this is a Q_BLOCK2 */
2451 coap_lg_xmit_t *lg_xmit;
2452 coap_string_t empty = { 0, NULL};
2453
2454 LL_FOREACH(session->lg_xmit, lg_xmit) {
2455 if (lg_xmit->option == COAP_OPTION_Q_BLOCK2 &&
2456 resource == lg_xmit->b.b2.resource &&
2457 request_method == lg_xmit->b.b2.request_method &&
2458 coap_string_equal(query ? query : &empty,
2459 lg_xmit->b.b2.query ? lg_xmit->b.b2.query : &empty))
2460 break;
2461 }
2462 return coap_send_q_blocks(session, lg_xmit, block, response, send_response);
2463}
2464#endif /* COAP_Q_BLOCK_SUPPORT */
2465#endif /* COAP_SERVER_SUPPORT */
2466
2467static void
2469 coap_lg_xmit_data_t *data_info) {
2470 if (!data_info)
2471 return;
2472 if (data_info->ref > 0) {
2473 data_info->ref--;
2474 return;
2475 }
2476 if (data_info->release_func) {
2477 coap_lock_callback(data_info->release_func(session,
2478 data_info->app_ptr));
2479 data_info->release_func = NULL;
2480 }
2481 coap_free_type(COAP_STRING, data_info);
2482}
2483
2484#if COAP_CLIENT_SUPPORT
2485#if COAP_Q_BLOCK_SUPPORT
2486/*
2487 * Send out a test PDU for Q-Block.
2488 */
2490coap_block_test_q_block(coap_session_t *session, coap_pdu_t *actual) {
2491 coap_pdu_t *pdu;
2492 uint8_t token[8];
2493 size_t token_len;
2494 uint8_t buf[4];
2495 coap_mid_t mid;
2496
2497#if NDEBUG
2498 (void)actual;
2499#endif /* NDEBUG */
2500 assert(session->block_mode & COAP_BLOCK_TRY_Q_BLOCK &&
2501 session->type == COAP_SESSION_TYPE_CLIENT &&
2502 COAP_PDU_IS_REQUEST(actual));
2503
2504 coap_log_debug("Testing for Q-Block support\n");
2505 /* RFC9177 Section 4.1 when checking if available */
2507 coap_new_message_id_lkd(session),
2509 if (!pdu) {
2510 return COAP_INVALID_MID;
2511 }
2512
2513 coap_session_new_token(session, &token_len, token);
2514 coap_add_token(pdu, token_len, token);
2515 /* Use a resource that the server MUST support (.well-known/core) */
2517 11, (const uint8_t *)".well-known");
2519 4, (const uint8_t *)"core");
2520 /*
2521 * M needs to be unset as 'asking' for only the first block using
2522 * Q-Block2 as a test for server support.
2523 * See RFC9177 Section 4.4 Using the Q-Block2 Option.
2524 *
2525 * As the client is asking for 16 byte chunks, it is unlikely that
2526 * the .well-known/core response will be 16 bytes or less, so
2527 * if the server supports Q-Block, it will be forced to respond with
2528 * a Q-Block2, so the client can detect the server Q-Block support.
2529 */
2531 coap_encode_var_safe(buf, sizeof(buf),
2532 (0 << 4) | (0 << 3) | 0),
2533 buf);
2534 set_block_mode_probe_q(session->block_mode);
2535 mid = coap_send_internal(session, pdu, NULL);
2536 if (mid == COAP_INVALID_MID)
2537 return COAP_INVALID_MID;
2538 session->remote_test_mid = mid;
2539 return mid;
2540}
2541#endif /* COAP_Q_BLOCK_SUPPORT */
2542
2544coap_block_new_lg_crcv(coap_session_t *session, coap_pdu_t *pdu,
2545 coap_lg_xmit_t *lg_xmit) {
2546 coap_block_b_t block;
2547 coap_lg_crcv_t *lg_crcv;
2548 uint64_t state_token = STATE_TOKEN_FULL(++session->tx_token, 1);
2549
2550 lg_crcv = coap_malloc_type(COAP_LG_CRCV, sizeof(coap_lg_crcv_t));
2551
2552 if (lg_crcv == NULL)
2553 return NULL;
2554
2555 coap_log_debug("** %s: lg_crcv %p initialized - stateless token xxxxx%011llx\n",
2556 coap_session_str(session), (void *)lg_crcv,
2557 STATE_TOKEN_BASE(state_token));
2558 memset(lg_crcv, 0, sizeof(coap_lg_crcv_t));
2559 lg_crcv->initial = 1;
2560 coap_ticks(&lg_crcv->last_used);
2561 /* Keep a copy of the sent pdu */
2562 lg_crcv->sent_pdu = coap_pdu_reference_lkd(pdu);
2563 if (lg_xmit) {
2564 coap_opt_iterator_t opt_iter;
2565 coap_opt_t *opt;
2566
2567 opt = coap_check_option(pdu, COAP_OPTION_OBSERVE, &opt_iter);
2568
2569 if (opt) {
2570 int observe_action;
2571
2572 observe_action = coap_decode_var_bytes(coap_opt_value(opt),
2573 coap_opt_length(opt));
2574 if (observe_action == COAP_OBSERVE_ESTABLISH) {
2575 /* Need to keep information for Observe Cancel */
2576 size_t data_len;
2577 const uint8_t *data;
2578
2579 if (coap_get_data(pdu, &data_len, &data)) {
2580 if (data_len < lg_xmit->data_info->length) {
2581 lg_xmit->data_info->ref++;
2582 lg_crcv->obs_data = lg_xmit->data_info;
2583 }
2584 }
2585 }
2586 }
2587 }
2588
2589 /* Need to keep original token for updating response PDUs */
2590 lg_crcv->app_token = coap_new_binary(pdu->actual_token.length);
2591 if (!lg_crcv->app_token) {
2592 coap_block_delete_lg_crcv(session, lg_crcv);
2593 return NULL;
2594 }
2595 memcpy(lg_crcv->app_token->s, pdu->actual_token.s, pdu->actual_token.length);
2596
2597 /* Need to set up a base token for actual communications if retries needed */
2598 lg_crcv->retry_counter = 1;
2599 lg_crcv->state_token = state_token;
2600 coap_address_copy(&lg_crcv->upstream, &session->addr_info.remote);
2601
2602 if (pdu->code == COAP_REQUEST_CODE_FETCH) {
2603 coap_bin_const_t *new_token;
2604
2605 /* Need to save/restore Observe Token for large FETCH */
2606 new_token = track_fetch_observe(pdu, lg_crcv, 0, &pdu->actual_token);
2607 if (new_token)
2608 coap_update_token(pdu, new_token->length, new_token->s);
2609 }
2610
2611 if (coap_get_block_b(session, pdu, COAP_OPTION_BLOCK1, &block)) {
2612 /* In case it is there - must not be in continuing request PDUs */
2613 lg_crcv->o_block_option = COAP_OPTION_BLOCK1;
2614 lg_crcv->o_blk_size = block.aszx;
2615 }
2616
2617 return lg_crcv;
2618}
2619
2620void
2621coap_block_delete_lg_crcv(coap_session_t *session,
2622 coap_lg_crcv_t *lg_crcv) {
2623 size_t i;
2624
2625#if (COAP_MAX_LOGGING_LEVEL < _COAP_LOG_DEBUG)
2626 (void)session;
2627#endif
2628 if (lg_crcv == NULL)
2629 return;
2630
2631 coap_free_type(COAP_STRING, lg_crcv->body_data);
2632 if (lg_crcv->obs_data) {
2633 coap_block_release_lg_xmit_data(session, lg_crcv->obs_data);
2634 lg_crcv->obs_data = NULL;
2635 }
2636 coap_address_copy(&session->addr_info.remote, &lg_crcv->upstream);
2637 coap_log_debug("** %s: lg_crcv %p released\n",
2638 coap_session_str(session), (void *)lg_crcv);
2639 coap_delete_binary(lg_crcv->app_token);
2640 for (i = 0; i < lg_crcv->obs_token_cnt; i++) {
2641 coap_delete_bin_const(lg_crcv->obs_token[i]);
2642 }
2643 coap_free_type(COAP_STRING, lg_crcv->obs_token);
2644 coap_delete_pdu_lkd(lg_crcv->sent_pdu);
2645 coap_free_type(COAP_LG_CRCV, lg_crcv);
2646}
2647#endif /* COAP_CLIENT_SUPPORT */
2648
2649#if COAP_SERVER_SUPPORT
2650void
2651coap_block_delete_lg_srcv(coap_session_t *session,
2652 coap_lg_srcv_t *lg_srcv) {
2653#if (COAP_MAX_LOGGING_LEVEL < _COAP_LOG_DEBUG)
2654 (void)session;
2655#endif
2656 if (lg_srcv == NULL)
2657 return;
2658
2659 coap_delete_str_const(lg_srcv->uri_path);
2660 coap_delete_bin_const(lg_srcv->last_token);
2661 coap_free_type(COAP_STRING, lg_srcv->body_data);
2662 coap_log_debug("** %s: lg_srcv %p released\n",
2663 coap_session_str(session), (void *)lg_srcv);
2664 coap_free_type(COAP_LG_SRCV, lg_srcv);
2665}
2666#endif /* COAP_SERVER_SUPPORT */
2667
2668void
2670 coap_lg_xmit_t *lg_xmit) {
2671 if (lg_xmit == NULL)
2672 return;
2673
2674 coap_block_release_lg_xmit_data(session, lg_xmit->data_info);
2675 if (COAP_PDU_IS_REQUEST(lg_xmit->sent_pdu))
2676 coap_delete_binary(lg_xmit->b.b1.app_token);
2677 else
2678 coap_delete_string(lg_xmit->b.b2.query);
2679 coap_delete_pdu_lkd(lg_xmit->sent_pdu);
2680
2681 coap_log_debug("** %s: lg_xmit %p released\n",
2682 coap_session_str(session), (void *)lg_xmit);
2683 coap_free_type(COAP_LG_XMIT, lg_xmit);
2684}
2685
2686#if COAP_SERVER_SUPPORT
2687typedef struct {
2688 uint32_t num;
2689 int is_continue;
2690} send_track;
2691
2692static int
2693add_block_send(uint32_t num, int is_continue, send_track *out_blocks,
2694 uint32_t *count, uint32_t max_count) {
2695 uint32_t i;
2696
2697 for (i = 0; i < *count && *count < max_count; i++) {
2698 if (num == out_blocks[i].num)
2699 return 0;
2700 else if (num < out_blocks[i].num) {
2701 if (*count - i > 1)
2702 memmove(&out_blocks[i], &out_blocks[i+1], *count - i -1);
2703 out_blocks[i].num = num;
2704 out_blocks[i].is_continue = is_continue;
2705 (*count)++;
2706 return 1;
2707 }
2708 }
2709 if (*count < max_count) {
2710 out_blocks[i].num = num;
2711 out_blocks[i].is_continue = is_continue;
2712 (*count)++;
2713 return 1;
2714 }
2715 return 0;
2716}
2717
2718/*
2719 * Need to see if this is a request for the next block of a large body
2720 * transfer. If so, need to initiate the response with the next blocks
2721 * and not trouble the application.
2722 *
2723 * If additional responses needed, then these are expicitly sent out and
2724 * 'response' is updated to be the last response to be sent. There can be
2725 * multiple Q-Block2 in the request, as well as the 'Continue' Q-Block2
2726 * request.
2727 *
2728 * This is set up using coap_add_data_large_response_lkd()
2729 *
2730 * Server is sending a large data response to GET / observe (Block2)
2731 *
2732 * Return: 0 Call application handler
2733 * 1 Do not call application handler - just send the built response
2734 */
2735int
2736coap_handle_request_send_block(coap_session_t *session,
2737 coap_pdu_t *pdu,
2738 coap_pdu_t *response,
2739 coap_resource_t *resource,
2740 coap_string_t *query) {
2741 coap_lg_xmit_t *lg_xmit = NULL;
2742 coap_block_b_t block;
2743 coap_block_b_t alt_block;
2744 uint16_t block_opt = 0;
2745 send_track *out_blocks = NULL;
2746 const char *error_phrase;
2747 coap_opt_iterator_t opt_iter;
2748 size_t chunk;
2749 coap_opt_iterator_t opt_b_iter;
2750 coap_opt_t *option;
2751 uint32_t request_cnt, i;
2752 coap_opt_t *etag_opt = NULL;
2753 coap_pdu_t *out_pdu = response;
2754#if COAP_Q_BLOCK_SUPPORT
2755 size_t max_block;
2756
2757 /* Is client indicating that it supports Q_BLOCK2 ? */
2758 if (coap_get_block_b(session, pdu, COAP_OPTION_Q_BLOCK2, &block)) {
2759 if (!(session->block_mode & COAP_BLOCK_HAS_Q_BLOCK))
2760 set_block_mode_has_q(session->block_mode);
2761 block_opt = COAP_OPTION_Q_BLOCK2;
2762 }
2763#endif /* COAP_Q_BLOCK_SUPPORT */
2764 if (coap_get_block_b(session, pdu, COAP_OPTION_BLOCK2, &alt_block)) {
2765 if (block_opt) {
2766 coap_log_warn("Block2 and Q-Block2 cannot be in the same request\n");
2767 coap_add_data(response, sizeof("Both Block2 and Q-Block2 invalid")-1,
2768 (const uint8_t *)"Both Block2 and Q-Block2 invalid");
2769 response->code = COAP_RESPONSE_CODE(400);
2770 goto skip_app_handler;
2771 }
2772 block = alt_block;
2773 block_opt = COAP_OPTION_BLOCK2;
2774 }
2775 if (block_opt == 0)
2776 return 0;
2777 if (block.num == 0) {
2778 /* Get a fresh copy of the data */
2779 return 0;
2780 }
2781 lg_xmit = coap_find_lg_xmit_response(session, pdu, resource, query);
2782 if (lg_xmit == NULL)
2783 return 0;
2784
2785#if COAP_Q_BLOCK_SUPPORT
2786 out_blocks = coap_malloc_type(COAP_STRING, sizeof(send_track) * COAP_MAX_PAYLOADS(session));
2787#else /* ! COAP_Q_BLOCK_SUPPORT */
2788 out_blocks = coap_malloc_type(COAP_STRING, sizeof(send_track));
2789#endif /* ! COAP_Q_BLOCK_SUPPORT */
2790 if (!out_blocks) {
2791 goto internal_issue;
2792 }
2793
2794 /* lg_xmit (response) found */
2795
2796 etag_opt = coap_check_option(pdu, COAP_OPTION_ETAG, &opt_iter);
2797 if (etag_opt) {
2798 /* There may be multiple ETag - need to check each one */
2799 coap_option_iterator_init(pdu, &opt_iter, COAP_OPT_ALL);
2800 while ((etag_opt = coap_option_next(&opt_iter))) {
2801 if (opt_iter.number == COAP_OPTION_ETAG) {
2802 uint64_t etag = coap_decode_var_bytes8(coap_opt_value(etag_opt),
2803 coap_opt_length(etag_opt));
2804 if (etag == lg_xmit->b.b2.etag) {
2805 break;
2806 }
2807 }
2808 }
2809 if (!etag_opt) {
2810 /* Not a match - pass up to a higher level */
2811 return 0;
2812 }
2813 }
2814 out_pdu->code = lg_xmit->sent_pdu->code;
2815 coap_ticks(&lg_xmit->last_obs);
2816 lg_xmit->last_all_sent = 0;
2817
2818 chunk = (size_t)1 << (lg_xmit->blk_size + 4);
2819 if (block_opt) {
2820 if (block.bert) {
2821 coap_log_debug("found Block option, block is BERT, block nr. %u, M %d\n",
2822 block.num, block.m);
2823 } else {
2824 coap_log_debug("found Block option, block size is %u, block nr. %u, M %d\n",
2825 1 << (block.szx + 4), block.num, block.m);
2826 }
2827 if (block.bert == 0 && block.szx != lg_xmit->blk_size) {
2828 if (block.num == 0) {
2829 if ((lg_xmit->offset + chunk) % ((size_t)1 << (block.szx + 4)) == 0) {
2830 /*
2831 * Recompute the block number of the previous packet given
2832 * the new block size
2833 */
2834 block.num = (uint32_t)(((lg_xmit->offset + chunk) >> (block.szx + 4)) - 1);
2835 lg_xmit->blk_size = block.szx;
2836 chunk = (size_t)1 << (lg_xmit->blk_size + 4);
2837 lg_xmit->offset = block.num * chunk;
2838 coap_log_debug("new Block size is %u, block number %u completed\n",
2839 1 << (block.szx + 4), block.num);
2840 } else {
2841 coap_log_debug("ignoring request to increase Block size, "
2842 "next block is not aligned on requested block size "
2843 "boundary. (%" PRIuS " x %u mod %u = %" PRIuS " (which is not 0)\n",
2844 lg_xmit->offset/chunk + 1, (1 << (lg_xmit->blk_size + 4)),
2845 (1 << (block.szx + 4)),
2846 (lg_xmit->offset + chunk) % ((size_t)1 << (block.szx + 4)));
2847 }
2848 } else {
2849 coap_log_debug("ignoring request to change Block size from %u to %u\n",
2850 (1 << (lg_xmit->blk_size + 4)), (1 << (block.szx + 4)));
2851 block.szx = block.aszx = lg_xmit->blk_size;
2852 }
2853 }
2854 }
2855
2856 /*
2857 * Need to check if there are multiple Q-Block2 requests. If so, they
2858 * need to be sent out in order of requests with the final request being
2859 * handled as per singular Block 2 request.
2860 */
2861 request_cnt = 0;
2862#if COAP_Q_BLOCK_SUPPORT
2863 max_block = (lg_xmit->data_info->length + chunk - 1)/chunk;
2864#endif /* COAP_Q_BLOCK_SUPPORT */
2865 coap_option_iterator_init(pdu, &opt_b_iter, COAP_OPT_ALL);
2866 while ((option = coap_option_next(&opt_b_iter))) {
2867 uint32_t num;
2868 if (opt_b_iter.number != lg_xmit->option)
2869 continue;
2870 num = coap_opt_block_num(option);
2871 if (num > 0xFFFFF) /* 20 bits max for num */
2872 continue;
2873 if (block.aszx != COAP_OPT_BLOCK_SZX(option)) {
2874 coap_add_data(response,
2875 sizeof("Changing blocksize during request invalid")-1,
2876 (const uint8_t *)"Changing blocksize during request invalid");
2877 response->code = COAP_RESPONSE_CODE(400);
2878 goto skip_app_handler;
2879 }
2880#if COAP_Q_BLOCK_SUPPORT
2881 if (COAP_OPT_BLOCK_MORE(option) && lg_xmit->option == COAP_OPTION_Q_BLOCK2) {
2882 if ((num % COAP_MAX_PAYLOADS(session)) == 0) {
2883 if (num == 0) {
2884 /* This is a repeat request for everything - hmm */
2885 goto call_app_handler;
2886 }
2887 /* 'Continue' request */
2888 for (i = 0; i < COAP_MAX_PAYLOADS(session) &&
2889 num + i < max_block; i++) {
2890 add_block_send(num + i, 1, out_blocks, &request_cnt,
2891 COAP_MAX_PAYLOADS(session));
2892 lg_xmit->last_block = num + i;
2893 }
2894 } else {
2895 /* Requesting remaining payloads in this MAX_PAYLOADS */
2896 for (i = 0; i < COAP_MAX_PAYLOADS(session) -
2897 num % COAP_MAX_PAYLOADS(session) &&
2898 num + i < max_block; i++) {
2899 add_block_send(num + i, 0, out_blocks, &request_cnt,
2900 COAP_MAX_PAYLOADS(session));
2901 }
2902 }
2903 } else
2904 add_block_send(num, 0, out_blocks, &request_cnt,
2905 COAP_MAX_PAYLOADS(session));
2906#else /* ! COAP_Q_BLOCK_SUPPORT */
2907 add_block_send(num, 0, out_blocks, &request_cnt, 1);
2908 break;
2909#endif /* ! COAP_Q_BLOCK_SUPPORT */
2910 }
2911 if (request_cnt == 0) {
2912 /* Block2 or Q-Block2 not found - give them the first block */
2913 block.szx = lg_xmit->blk_size;
2914 lg_xmit->offset = 0;
2915 out_blocks[0].num = 0;
2916 out_blocks[0].is_continue = 0;
2917 request_cnt = 1;
2918 }
2919
2920 for (i = 0; i < request_cnt; i++) {
2921 uint8_t buf[8];
2922
2923 block.num = out_blocks[i].num;
2924 lg_xmit->offset = block.num * chunk;
2925
2926 if (i + 1 < request_cnt) {
2927 /* Need to set up a copy of the pdu to send */
2928 coap_opt_filter_t drop_options;
2929
2930 memset(&drop_options, 0, sizeof(coap_opt_filter_t));
2931 if (block.num != 0)
2933 if (out_blocks[i].is_continue) {
2934 out_pdu = coap_pdu_duplicate_lkd(lg_xmit->sent_pdu, session,
2935 lg_xmit->sent_pdu->actual_token.length,
2936 lg_xmit->sent_pdu->actual_token.s, &drop_options);
2937 } else {
2938 out_pdu = coap_pdu_duplicate_lkd(lg_xmit->sent_pdu, session, pdu->actual_token.length,
2939 pdu->actual_token.s, &drop_options);
2940 }
2941 if (!out_pdu) {
2942 goto internal_issue;
2943 }
2944 } else {
2945 if (out_blocks[i].is_continue)
2946 coap_update_token(response, lg_xmit->sent_pdu->actual_token.length,
2947 lg_xmit->sent_pdu->actual_token.s);
2948 /*
2949 * Copy the options across and then fix the block option
2950 *
2951 * Need to drop Observe option if Block2 and block.num != 0
2952 */
2953 coap_option_iterator_init(lg_xmit->sent_pdu, &opt_iter, COAP_OPT_ALL);
2954 while ((option = coap_option_next(&opt_iter))) {
2955 if (opt_iter.number == COAP_OPTION_OBSERVE && block.num != 0)
2956 continue;
2957 if (!coap_insert_option(response, opt_iter.number,
2958 coap_opt_length(option),
2959 coap_opt_value(option))) {
2960 goto internal_issue;
2961 }
2962 }
2963 out_pdu = response;
2964 }
2965 if (pdu->type == COAP_MESSAGE_NON)
2966 out_pdu->type = COAP_MESSAGE_NON;
2967 if (block.bert) {
2968 size_t token_options = pdu->data ? (size_t)(pdu->data - pdu->token) : pdu->used_size;
2969 block.m = (lg_xmit->data_info->length - lg_xmit->offset) >
2970 ((out_pdu->max_size - token_options) /1024) * 1024;
2971 } else {
2972 block.m = (lg_xmit->offset + chunk) < lg_xmit->data_info->length;
2973 }
2974 if (!coap_update_option(out_pdu, lg_xmit->option,
2976 sizeof(buf),
2977 (block.num << 4) |
2978 (block.m << 3) |
2979 block.aszx),
2980 buf)) {
2981 goto internal_issue;
2982 }
2983 if (!(lg_xmit->offset + chunk < lg_xmit->data_info->length)) {
2984 /* Last block - keep in cache for 4 * ACK_TIMOUT */
2985 coap_ticks(&lg_xmit->last_all_sent);
2986 }
2987 if (lg_xmit->b.b2.maxage_expire) {
2988 coap_tick_t now;
2989 coap_time_t rem;
2990
2991 if (!(lg_xmit->offset + chunk < lg_xmit->data_info->length)) {
2992 /* Last block - keep in cache for 4 * ACK_TIMOUT */
2993 coap_ticks(&lg_xmit->last_all_sent);
2994 }
2995 coap_ticks(&now);
2996 rem = coap_ticks_to_rt(now);
2997 if (lg_xmit->b.b2.maxage_expire > rem) {
2998 rem = lg_xmit->b.b2.maxage_expire - rem;
2999 } else {
3000 rem = 0;
3001 /* Entry needs to be expired */
3002 coap_ticks(&lg_xmit->last_all_sent);
3003 }
3006 sizeof(buf),
3007 rem),
3008 buf)) {
3009 goto internal_issue;
3010 }
3011 }
3012
3013 if (!coap_add_block_b_data(out_pdu,
3014 lg_xmit->data_info->length,
3015 lg_xmit->data_info->data,
3016 &block)) {
3017 goto internal_issue;
3018 }
3019 if (i + 1 < request_cnt) {
3020 coap_ticks(&lg_xmit->last_sent);
3021 coap_send_internal(session, out_pdu, NULL);
3022 }
3023 }
3024 coap_ticks(&lg_xmit->last_payload);
3025 goto skip_app_handler;
3026#if COAP_Q_BLOCK_SUPPORT
3027call_app_handler:
3028 coap_free_type(COAP_STRING, out_blocks);
3029 return 0;
3030#endif /* COAP_Q_BLOCK_SUPPORT */
3031
3032internal_issue:
3033 response->code = COAP_RESPONSE_CODE(500);
3034 error_phrase = coap_response_phrase(response->code);
3035 coap_add_data(response, strlen(error_phrase),
3036 (const uint8_t *)error_phrase);
3037 /* Keep in cache for 4 * ACK_TIMOUT incase of retry */
3038 if (lg_xmit)
3039 coap_ticks(&lg_xmit->last_all_sent);
3040
3041skip_app_handler:
3042 coap_free_type(COAP_STRING, out_blocks);
3043 return 1;
3044}
3045#endif /* COAP_SERVER_SUPPORT */
3046
3047static int
3048update_received_blocks(coap_rblock_t *rec_blocks, uint32_t block_num, uint32_t block_m) {
3049 uint32_t i;
3050
3051 if (rec_blocks->total_blocks && block_num + 1 > rec_blocks->total_blocks) {
3052 /* received block number greater than Block No defined when More bit unset */
3053 return 0;
3054 }
3055
3056 /* Reset as there is activity */
3057 rec_blocks->retry = 0;
3058
3059 for (i = 0; i < rec_blocks->used; i++) {
3060 if (block_num >= rec_blocks->range[i].begin &&
3061 block_num <= rec_blocks->range[i].end)
3062 break;
3063
3064 if (block_num < rec_blocks->range[i].begin) {
3065 if (block_num + 1 == rec_blocks->range[i].begin) {
3066 rec_blocks->range[i].begin = block_num;
3067 } else {
3068 /* Need to insert a new range */
3069 if (rec_blocks->used == COAP_RBLOCK_CNT -1)
3070 /* Too many losses */
3071 return 0;
3072 memmove(&rec_blocks->range[i+1], &rec_blocks->range[i],
3073 (rec_blocks->used - i) * sizeof(rec_blocks->range[0]));
3074 rec_blocks->range[i].begin = rec_blocks->range[i].end = block_num;
3075 rec_blocks->used++;
3076 }
3077 break;
3078 }
3079 if (block_num == rec_blocks->range[i].end + 1) {
3080 rec_blocks->range[i].end = block_num;
3081 if (i + 1 < rec_blocks->used) {
3082 if (rec_blocks->range[i+1].begin == block_num + 1) {
3083 /* Merge the 2 ranges */
3084 rec_blocks->range[i].end = rec_blocks->range[i+1].end;
3085 if (i+2 < rec_blocks->used) {
3086 memmove(&rec_blocks->range[i+1], &rec_blocks->range[i+2],
3087 (rec_blocks->used - (i+2)) * sizeof(rec_blocks->range[0]));
3088 }
3089 rec_blocks->used--;
3090 }
3091 }
3092 break;
3093 }
3094 }
3095 if (i == rec_blocks->used) {
3096 if (rec_blocks->used == COAP_RBLOCK_CNT -1)
3097 /* Too many losses */
3098 return 0;
3099 rec_blocks->range[i].begin = rec_blocks->range[i].end = block_num;
3100 rec_blocks->used++;
3101 }
3102 if (!block_m)
3103 rec_blocks->total_blocks = block_num + 1;
3104
3105 coap_ticks(&rec_blocks->last_seen);
3106 return 1;
3107}
3108
3109#if COAP_SERVER_SUPPORT
3110/*
3111 * Need to check if this is a large PUT / POST etc. using multiple blocks
3112 *
3113 * Server receiving PUT/POST etc. of a large amount of data (Block1)
3114 *
3115 * Return: 0 Call application handler
3116 * 1 Do not call application handler - just send the built response
3117 */
3118int
3119coap_handle_request_put_block(coap_context_t *context,
3120 coap_session_t *session,
3121 coap_pdu_t *pdu,
3122 coap_pdu_t *response,
3123 coap_resource_t *resource,
3124 coap_string_t *uri_path,
3125 coap_opt_t *observe,
3126 int *added_block,
3127 coap_lg_srcv_t **pfree_lg_srcv) {
3128 size_t length = 0;
3129 const uint8_t *data = NULL;
3130 size_t offset = 0;
3131 size_t total = 0;
3132 coap_block_b_t block;
3133 coap_opt_iterator_t opt_iter;
3134 uint16_t block_option = 0;
3135 coap_lg_srcv_t *lg_srcv;
3136 coap_opt_t *size_opt;
3137 coap_opt_t *fmt_opt;
3138 uint16_t fmt;
3139 coap_opt_t *rtag_opt;
3140 size_t rtag_length;
3141 const uint8_t *rtag;
3142 uint32_t max_block_szx;
3143 int update_data;
3144 unsigned int saved_num;
3145 size_t saved_offset;
3146
3147 *added_block = 0;
3148 *pfree_lg_srcv = NULL;
3149 coap_get_data_large(pdu, &length, &data, &offset, &total);
3150 pdu->body_offset = 0;
3151 pdu->body_total = length;
3152
3153 if (coap_get_block_b(session, pdu, COAP_OPTION_BLOCK1, &block)) {
3154 block_option = COAP_OPTION_BLOCK1;
3155#if COAP_Q_BLOCK_SUPPORT
3156 if (coap_check_option(pdu, COAP_OPTION_Q_BLOCK1, &opt_iter)) {
3157 /* Cannot handle Q-Block1 as well */
3158 coap_add_data(response, sizeof("Block1 + Q-Block1 together")-1,
3159 (const uint8_t *)"Block1 + Q-Block1 together");
3160 response->code = COAP_RESPONSE_CODE(402);
3161 goto skip_app_handler;
3162 }
3163#endif /* COAP_Q_BLOCK_SUPPORT */
3164 }
3165#if COAP_Q_BLOCK_SUPPORT
3166 else if (coap_get_block_b(session, pdu, COAP_OPTION_Q_BLOCK1, &block)) {
3167 block_option = COAP_OPTION_Q_BLOCK1;
3168 set_block_mode_has_q(session->block_mode);
3169 }
3170#endif /* COAP_Q_BLOCK_SUPPORT */
3171 if (!block_option ||
3172 (block_option == COAP_OPTION_BLOCK1 && block.num == 0 && block.m == 0)) {
3173 /* Not blocked, or a single block */
3174 if (context->max_body_size && total > context->max_body_size) {
3175 uint8_t buf[4];
3176
3177 coap_update_option(response,
3179 coap_encode_var_safe((uint8_t *)buf, sizeof(buf),
3180 context->max_body_size),
3181 (uint8_t *)buf);
3182 response->code = COAP_RESPONSE_CODE(413);
3183 coap_log_warn("Unable to handle data size %" PRIuS " (max %" PRIu32 ")\n", total,
3184 context->max_body_size);
3185 goto skip_app_handler;
3186 }
3187 goto call_app_handler;
3188 }
3189
3190 size_opt = coap_check_option(pdu,
3192 &opt_iter);
3193 fmt_opt = coap_check_option(pdu,
3195 &opt_iter);
3196 fmt = fmt_opt ? coap_decode_var_bytes(coap_opt_value(fmt_opt),
3197 coap_opt_length(fmt_opt)) :
3199 rtag_opt = coap_check_option(pdu,
3201 &opt_iter);
3202 rtag_length = rtag_opt ? coap_opt_length(rtag_opt) : 0;
3203 rtag = rtag_opt ? coap_opt_value(rtag_opt) : NULL;
3204
3205 if (length > block.chunk_size) {
3206 coap_log_debug("block: Oversized packet - reduced to %"PRIu32" from %" PRIuS "\n",
3207 block.chunk_size, length);
3208 length = block.chunk_size;
3209 } else if (!block.bert && block.m && length != block.chunk_size) {
3210 coap_log_info("block: Undersized packet chunk %"PRIu32" got %" PRIuS "\n",
3211 block.chunk_size, length);
3212 response->code = COAP_RESPONSE_CODE(400);
3213 goto skip_app_handler;
3214 }
3215 total = size_opt ? coap_decode_var_bytes(coap_opt_value(size_opt),
3216 coap_opt_length(size_opt)) : 0;
3217 if (total) {
3218 uint32_t max_body;
3219
3220 max_block_szx = COAP_BLOCK_MAX_SIZE_GET(session->block_mode);
3221 if (max_block_szx == 0 || max_block_szx > block.szx) {
3222 max_block_szx = block.szx;
3223 }
3224 max_body = ((1UL << 20) * (1 << (max_block_szx + 4)));
3225 if (max_body > MAX_BLK_LEN)
3226 max_body = MAX_BLK_LEN;
3227 if ((context->max_body_size && total > context->max_body_size) ||
3228 (total > max_body)) {
3229 /* Suggested body size larger than allowed */
3230 char buf[32];
3231 uint32_t max_body_size = context->max_body_size;
3232
3233 if (max_body_size == 0 || max_body < max_body_size) {
3234 max_body_size = max_body;
3235 }
3236 coap_update_option(response,
3238 coap_encode_var_safe((uint8_t *)buf, sizeof(buf),
3239 max_body_size),
3240 (uint8_t *)buf);
3241 snprintf(buf, sizeof(buf), "Max body size %" PRIu32, max_body_size);
3242 coap_add_data(response, strlen(buf), (uint8_t *)buf);
3243 response->code = COAP_RESPONSE_CODE(413);
3244 coap_log_warn("Unable to handle body size %" PRIuS " (max %" PRIu32 ")\n", total, max_body_size);
3245 goto skip_app_handler;
3246 }
3247 }
3248 offset = block.num << (block.szx + 4);
3249
3250 if (!(session->block_mode &
3251#if COAP_Q_BLOCK_SUPPORT
3253#else /* COAP_Q_BLOCK_SUPPORT */
3255#endif /* COAP_Q_BLOCK_SUPPORT */
3256 && !block.bert) {
3257 uint8_t buf[4];
3258
3259 /* Ask for the next block */
3260 coap_insert_option(response, block_option,
3261 coap_encode_var_safe(buf, sizeof(buf),
3262 (block.num << 4) |
3263 (block.m << 3) |
3264 block.aszx),
3265 buf);
3266 /* Not re-assembling or checking for receipt order */
3267 pdu->body_data = data;
3268 pdu->body_length = length;
3269 pdu->body_offset = offset;
3270 if (total < (length + offset + (block.m ? 1 : 0)))
3271 total = length + offset + (block.m ? 1 : 0);
3272 pdu->body_total = total;
3273 *added_block = block.m;
3274 /* The application is responsible for returning the correct 2.01/2.04/2.31 etc. */
3275 goto call_app_handler;
3276 }
3277
3278 /*
3279 * locate the lg_srcv
3280 */
3281 LL_FOREACH(session->lg_srcv, lg_srcv) {
3282 if (rtag_opt || lg_srcv->rtag_set == 1) {
3283 if (!(rtag_opt && lg_srcv->rtag_set == 1))
3284 continue;
3285 if (lg_srcv->rtag_length != rtag_length ||
3286 memcmp(lg_srcv->rtag, rtag, rtag_length) != 0)
3287 continue;
3288 }
3289 if (resource == lg_srcv->resource) {
3290 break;
3291 }
3292 if ((lg_srcv->resource == context->unknown_resource ||
3293 resource == context->proxy_uri_resource) &&
3294 coap_string_equal(uri_path, lg_srcv->uri_path))
3295 break;
3296 }
3297
3298 if (!lg_srcv && block.num != 0 && session->block_mode & COAP_BLOCK_NOT_RANDOM_BLOCK1) {
3299 coap_add_data(response, sizeof("Missing block 0")-1,
3300 (const uint8_t *)"Missing block 0");
3301 response->code = COAP_RESPONSE_CODE(408);
3302 goto skip_app_handler;
3303 }
3304
3305 if (!lg_srcv) {
3306 /* Allocate lg_srcv to use for tracking */
3307 lg_srcv = coap_malloc_type(COAP_LG_SRCV, sizeof(coap_lg_srcv_t));
3308 if (lg_srcv == NULL) {
3309 coap_add_data(response, sizeof("Memory issue")-1,
3310 (const uint8_t *)"Memory issue");
3311 response->code = COAP_RESPONSE_CODE(500);
3312 goto skip_app_handler;
3313 }
3314 coap_log_debug("** %s: lg_srcv %p initialized\n",
3315 coap_session_str(session), (void *)lg_srcv);
3316 memset(lg_srcv, 0, sizeof(coap_lg_srcv_t));
3317 lg_srcv->resource = resource;
3318 if (resource == context->unknown_resource ||
3319 resource == context->proxy_uri_resource)
3320 lg_srcv->uri_path = coap_new_str_const(uri_path->s, uri_path->length);
3321 lg_srcv->content_format = fmt;
3322 lg_srcv->total_len = total;
3323 max_block_szx = COAP_BLOCK_MAX_SIZE_GET(session->block_mode);
3324 if (!block.bert && block.num == 0 && max_block_szx != 0 &&
3325 max_block_szx < block.szx) {
3326 lg_srcv->szx = max_block_szx;
3327 } else {
3328 lg_srcv->szx = block.szx;
3329 }
3330 lg_srcv->block_option = block_option;
3331 if (observe) {
3332 lg_srcv->observe_length = min(coap_opt_length(observe), 3);
3333 memcpy(lg_srcv->observe, coap_opt_value(observe), lg_srcv->observe_length);
3334 lg_srcv->observe_set = 1;
3335 }
3336 if (rtag_opt) {
3337 lg_srcv->rtag_length = coap_opt_length(rtag_opt);
3338 memcpy(lg_srcv->rtag, coap_opt_value(rtag_opt), lg_srcv->rtag_length);
3339 lg_srcv->rtag_set = 1;
3340 }
3341 lg_srcv->body_data = NULL;
3342 LL_PREPEND(session->lg_srcv, lg_srcv);
3343 }
3344 coap_ticks(&lg_srcv->last_used);
3345
3346 if (block_option == COAP_OPTION_BLOCK1 &&
3348 !check_if_next_block(&lg_srcv->rec_blocks, block.num)) {
3349 coap_add_data(response, sizeof("Missing interim block")-1,
3350 (const uint8_t *)"Missing interim block");
3351 response->code = COAP_RESPONSE_CODE(408);
3352 goto skip_app_handler;
3353 }
3354
3355 if (fmt != lg_srcv->content_format) {
3356 coap_add_data(response, sizeof("Content-Format mismatch")-1,
3357 (const uint8_t *)"Content-Format mismatch");
3358 response->code = COAP_RESPONSE_CODE(408);
3359 goto free_lg_srcv;
3360 }
3361
3362#if COAP_Q_BLOCK_SUPPORT
3363 if (block_option == COAP_OPTION_Q_BLOCK1) {
3364 if (total != lg_srcv->total_len) {
3365 coap_add_data(response, sizeof("Size1 mismatch")-1,
3366 (const uint8_t *)"Size1 mismatch");
3367 response->code = COAP_RESPONSE_CODE(408);
3368 goto free_lg_srcv;
3369 }
3370 coap_delete_bin_const(lg_srcv->last_token);
3371 lg_srcv->last_token = coap_new_bin_const(pdu->actual_token.s,
3372 pdu->actual_token.length);
3373 }
3374#endif /* COAP_Q_BLOCK_SUPPORT */
3375
3376 lg_srcv->last_mid = pdu->mid;
3377 lg_srcv->last_type = pdu->type;
3378
3379 update_data = 0;
3380 saved_num = block.num;
3381 saved_offset = offset;
3382
3383 while (offset < saved_offset + length) {
3384 if (!check_if_received_block(&lg_srcv->rec_blocks, block.num)) {
3385 /* Update list of blocks received */
3386 if (!update_received_blocks(&lg_srcv->rec_blocks, block.num, block.m)) {
3388 coap_add_data(response, sizeof("Too many missing blocks")-1,
3389 (const uint8_t *)"Too many missing blocks");
3390 response->code = COAP_RESPONSE_CODE(408);
3391 goto free_lg_srcv;
3392 }
3393 update_data = 1;
3394 }
3395 block.num++;
3396 offset = block.num << (block.szx + 4);
3397 }
3398 block.num--;
3399
3400 if (update_data) {
3401 /* Update saved data */
3402#if COAP_Q_BLOCK_SUPPORT
3403 lg_srcv->rec_blocks.processing_payload_set =
3404 block.num / COAP_MAX_PAYLOADS(session);
3405#endif /* COAP_Q_BLOCK_SUPPORT */
3406 if (lg_srcv->total_len < saved_offset + length) {
3407 lg_srcv->total_len = saved_offset + length;
3408 }
3409
3410#define USE_BLOCK_DATA_HANDLER (context && context->block_data_cb && \
3411 !resource->is_proxy_uri && \
3412 !resource->is_reverse_proxy && \
3413 ((session->block_mode & COAP_SINGLE_BLOCK_OR_Q) || block.bert) && \
3414 (resource->flags & COAP_RESOURCE_USE_BLOCK_DATA_HANDLER))
3415
3416 if (USE_BLOCK_DATA_HANDLER) {
3417 coap_response_t resp;
3418
3420 context->block_data_cb(session, pdu, resource,
3421 &lg_srcv->body_data,
3422 length, data, saved_offset,
3423 lg_srcv->total_len));
3424 if (resp != COAP_RESPONSE_OK) {
3425 response->code = COAP_RESPONSE_CODE(500);
3426 goto skip_app_handler;
3427 }
3428 } else {
3429 lg_srcv->body_data = coap_block_build_body(lg_srcv->body_data, length, data,
3430 saved_offset, lg_srcv->total_len);
3431 if (!lg_srcv->body_data) {
3432 coap_add_data(response, sizeof("Memory issue")-1,
3433 (const uint8_t *)"Memory issue");
3434 response->code = COAP_RESPONSE_CODE(500);
3435 goto skip_app_handler;
3436 }
3437 }
3438 }
3439
3440 if (block.m ||
3441 !check_all_blocks_in(&lg_srcv->rec_blocks)) {
3442 /* Not all the payloads of the body have arrived */
3443 if (block.m) {
3444 uint8_t buf[4];
3445
3446#if COAP_Q_BLOCK_SUPPORT
3447 if (block_option == COAP_OPTION_Q_BLOCK1) {
3448 if (check_all_blocks_in(&lg_srcv->rec_blocks)) {
3449 goto give_app_data;
3450 }
3451 if (lg_srcv->rec_blocks.used == 1 &&
3452 (lg_srcv->rec_blocks.range[0].end % COAP_MAX_PAYLOADS(session)) + 1
3453 == COAP_MAX_PAYLOADS(session)) {
3454 /* Blocks could arrive in wrong order */
3455 block.num = lg_srcv->rec_blocks.range[0].end;
3456 } else {
3457 /* The remote end will be sending the next one unless this
3458 is a MAX_PAYLOADS and all previous have been received */
3459 goto skip_app_handler;
3460 }
3461 if (COAP_PROTO_RELIABLE(session->proto) ||
3462 pdu->type != COAP_MESSAGE_NON)
3463 goto skip_app_handler;
3464 }
3465#endif /* COAP_Q_BLOCK_SUPPORT */
3466
3467 /* Check to see if block size is getting forced down */
3468 max_block_szx = COAP_BLOCK_MAX_SIZE_GET(session->block_mode);
3469 if (!block.bert && saved_num == 0 && max_block_szx != 0 &&
3470 max_block_szx < block.aszx) {
3471 block.aszx = max_block_szx;
3472 }
3473
3474 /*
3475 * If the last block has been seen, packets are coming in in
3476 * random order. If all blocks are now in, then need to send
3477 * complete payload to application and acknowledge this current
3478 * block.
3479 */
3480 if ((total == 0 && block.m) || !check_all_blocks_in(&lg_srcv->rec_blocks)) {
3481 /* Ask for the next block */
3482 coap_insert_option(response, block_option,
3483 coap_encode_var_safe(buf, sizeof(buf),
3484 (saved_num << 4) |
3485 (block.m << 3) |
3486 block.aszx),
3487 buf);
3488 response->code = COAP_RESPONSE_CODE(231);
3489 } else {
3490 /* Need to separately respond to this request */
3491 coap_pdu_t *tmp_pdu = coap_pdu_duplicate_lkd(response,
3492 session,
3493 response->actual_token.length,
3494 response->actual_token.s,
3495 NULL);
3496 if (tmp_pdu) {
3497 tmp_pdu->code = COAP_RESPONSE_CODE(231);
3498 if (coap_send_internal(session, tmp_pdu, NULL) == COAP_INVALID_MID) {
3499 /* lg_srcv may have got deleted */
3500 coap_lg_srcv_t *sg;
3501
3502 LL_FOREACH(session->lg_srcv, sg) {
3503 if (lg_srcv == sg) {
3504 /* Still there */
3505 break;
3506 }
3507 }
3508 if (!sg)
3509 goto skip_app_handler;
3510 }
3511 }
3512 if (lg_srcv->last_token) {
3513 coap_update_token(response, lg_srcv->last_token->length, lg_srcv->last_token->s);
3514 coap_update_token(pdu, lg_srcv->last_token->length, lg_srcv->last_token->s);
3515 }
3516 /* Pass the assembled pdu and body to the application */
3517 goto give_app_data;
3518 }
3519 } else {
3520 /* block.m Block More option not set. Some outstanding blocks */
3521#if COAP_Q_BLOCK_SUPPORT
3522 if (block_option != COAP_OPTION_Q_BLOCK1) {
3523#endif /* COAP_Q_BLOCK_SUPPORT */
3524 /* Last chunk - but not all in */
3525 coap_ticks(&lg_srcv->last_used);
3526 lg_srcv->no_more_seen = 1;
3527 coap_delete_bin_const(lg_srcv->last_token);
3528 lg_srcv->last_token = coap_new_bin_const(pdu->actual_token.s,
3529 pdu->actual_token.length);
3530
3531 /*
3532 * Need to just ACK (no response code) to handle client's NSTART.
3533 * When final missing block comes in, we will pass all the data
3534 * for processing so a 2.01, 2.04 etc. code can be generated
3535 * and responded to as a separate response "RFC7252 5.2.2. Separate"
3536 * If missing block(s) do not come in, then will generate a 4.08
3537 * when lg_srcv times out.
3538 * Fall through to skip_app_handler.
3539 */
3540#if COAP_Q_BLOCK_SUPPORT
3541 }
3542#endif /* COAP_Q_BLOCK_SUPPORT */
3543 }
3544 goto skip_app_handler;
3545 }
3546
3547 /*
3548 * Entire payload received.
3549 * Remove the Block1 option as passing all of the data to
3550 * application layer. Add back in observe option if appropriate.
3551 * Adjust all other information.
3552 */
3553give_app_data:
3554 if (lg_srcv->observe_set) {
3556 lg_srcv->observe_length, lg_srcv->observe);
3557 }
3558 coap_remove_option(pdu, block_option);
3559 if (lg_srcv->body_data) {
3560 pdu->body_data = lg_srcv->body_data->s;
3561 pdu->body_length = lg_srcv->total_len;
3562 } else {
3563 pdu->body_data = NULL;
3564 pdu->body_length = 0;
3565 }
3566 pdu->body_offset = 0;
3567 pdu->body_total = lg_srcv->total_len;
3568 if (USE_BLOCK_DATA_HANDLER) {
3569 /* Data has already been provided - do not duplicate */
3570 if (pdu->data) {
3571 pdu->used_size = pdu->data - pdu->token - 1;
3572 pdu->data = NULL;
3573 }
3574 }
3575 coap_log_debug("Server app version of updated PDU\n");
3577 lg_srcv->dont_timeout = 1;
3578 *pfree_lg_srcv = lg_srcv;
3579
3580call_app_handler:
3581 return 0;
3582
3583free_lg_srcv:
3584 LL_DELETE(session->lg_srcv, lg_srcv);
3585 coap_block_delete_lg_srcv(session, lg_srcv);
3586
3587skip_app_handler:
3588 return 1;
3589}
3590#endif /* COAP_SERVER_SUPPORT */
3591
3592#if COAP_CLIENT_SUPPORT
3593#if COAP_Q_BLOCK_SUPPORT
3594static uint32_t
3595derive_cbor_value(const uint8_t **bp, size_t rem_len) {
3596 uint32_t value = **bp & 0x1f;
3597 (*bp)++;
3598 if (value < 24) {
3599 return value;
3600 } else if (value == 24) {
3601 if (rem_len < 2)
3602 return (uint32_t)-1;
3603 value = **bp;
3604 (*bp)++;
3605 return value;
3606 } else if (value == 25) {
3607 if (rem_len < 3)
3608 return (uint32_t)-1;
3609 value = **bp << 8;
3610 (*bp)++;
3611 value |= **bp;
3612 (*bp)++;
3613 return value;
3614 }
3615 if (rem_len < 4)
3616 return (uint32_t)-1;
3617 value = (uint32_t)(**bp) << 24;
3618 (*bp)++;
3619 value |= **bp << 16;
3620 (*bp)++;
3621 value |= **bp << 8;
3622 (*bp)++;
3623 value |= **bp;
3624 (*bp)++;
3625 return value;
3626}
3627#endif /* COAP_Q_BLOCK_SUPPORT */
3628
3629static int
3630check_freshness(coap_session_t *session, coap_pdu_t *rcvd, coap_pdu_t *sent,
3631 coap_lg_xmit_t *lg_xmit, coap_lg_crcv_t *lg_crcv) {
3632 /* Check for Echo option for freshness */
3633 coap_opt_iterator_t opt_iter;
3634 coap_opt_t *opt = coap_check_option(rcvd, COAP_OPTION_ECHO, &opt_iter);
3635
3636 if (opt) {
3637 if (sent || lg_xmit || lg_crcv) {
3638 /* Need to retransmit original request with Echo option added */
3639 coap_pdu_t *echo_pdu;
3640 coap_mid_t mid;
3641 const uint8_t *data;
3642 size_t data_len;
3643 int have_data = 0;
3644 uint8_t ltoken[8];
3645 size_t ltoken_len;
3646 uint64_t token;
3647
3648 if (sent) {
3649 if (coap_get_data(sent, &data_len, &data))
3650 have_data = 1;
3651 } else if (lg_xmit) {
3652 sent = lg_xmit->sent_pdu;
3653 if (lg_xmit->data_info->length) {
3654 size_t blk_size = (size_t)1 << (lg_xmit->blk_size + 4);
3655 size_t offset = (lg_xmit->last_block + 1) * blk_size;
3656 have_data = 1;
3657 data = &lg_xmit->data_info->data[offset];
3658 data_len = (lg_xmit->data_info->length - offset) > blk_size ? blk_size :
3659 lg_xmit->data_info->length - offset;
3660 }
3661 } else { /* lg_crcv */
3662 sent = lg_crcv->sent_pdu;
3663 if (coap_get_data(sent, &data_len, &data))
3664 have_data = 1;
3665 }
3666 if (lg_xmit) {
3667 token = STATE_TOKEN_FULL(lg_xmit->b.b1.state_token,
3668 ++lg_xmit->b.b1.count);
3669 } else {
3670 token = STATE_TOKEN_FULL(lg_crcv->state_token,
3671 ++lg_crcv->retry_counter);
3672 }
3673 ltoken_len = coap_encode_var_safe8(ltoken, sizeof(token), token);
3674 echo_pdu = coap_pdu_duplicate_lkd(sent, session, ltoken_len, ltoken, NULL);
3675 if (!echo_pdu)
3676 return 0;
3677 if (!coap_insert_option(echo_pdu, COAP_OPTION_ECHO,
3678 coap_opt_length(opt), coap_opt_value(opt)))
3679 goto not_sent;
3680 if (have_data) {
3681 coap_add_data(echo_pdu, data_len, data);
3682 }
3683 /* Need to track Observe token change if Observe */
3684 track_fetch_observe(echo_pdu, lg_crcv, 0, &echo_pdu->actual_token);
3685#if COAP_OSCORE_SUPPORT
3686 if (session->oscore_encryption &&
3687 (opt = coap_check_option(echo_pdu, COAP_OPTION_OBSERVE, &opt_iter)) &&
3689 /* Need to update the base PDU's Token for closing down Observe */
3690 if (lg_xmit) {
3691 lg_xmit->b.b1.state_token = token;
3692 } else {
3693 lg_crcv->state_token = token;
3694 }
3695 }
3696#endif /* COAP_OSCORE_SUPPORT */
3697 mid = coap_send_internal(session, echo_pdu, NULL);
3698 if (mid == COAP_INVALID_MID)
3699 goto not_sent;
3700 return 1;
3701 } else {
3702 /* Need to save Echo option value to add to next reansmission */
3703not_sent:
3704 coap_delete_bin_const(session->echo);
3705 session->echo = coap_new_bin_const(coap_opt_value(opt),
3706 coap_opt_length(opt));
3707 }
3708 }
3709 return 0;
3710}
3711
3712static void
3713track_echo(coap_session_t *session, coap_pdu_t *rcvd) {
3714 coap_opt_iterator_t opt_iter;
3715 coap_opt_t *opt = coap_check_option(rcvd, COAP_OPTION_ECHO, &opt_iter);
3716
3717 if (opt) {
3718 coap_delete_bin_const(session->echo);
3719 session->echo = coap_new_bin_const(coap_opt_value(opt),
3720 coap_opt_length(opt));
3721 }
3722}
3723
3724/*
3725 * Need to see if this is a response to a large body request transfer. If so,
3726 * need to initiate the request containing the next block and not trouble the
3727 * application. Note that Token must unique per request/response.
3728 *
3729 * Client receives large data acknowledgement from server (Block1)
3730 *
3731 * This is set up using coap_add_data_large_request_lkd()
3732 *
3733 * Client is using GET etc.
3734 *
3735 * Return: 0 Call application handler
3736 * 1 Do not call application handler - just send the built response
3737 */
3738int
3739coap_handle_response_send_block(coap_session_t *session, coap_pdu_t *sent,
3740 coap_pdu_t *rcvd) {
3741 coap_lg_xmit_t *lg_xmit;
3742 coap_lg_crcv_t *lg_crcv = NULL;
3743
3744 lg_xmit = coap_find_lg_xmit(session, rcvd);
3745 if (lg_xmit) {
3746 /* lg_xmit found */
3747 size_t chunk = (size_t)1 << (lg_xmit->blk_size + 4);
3748 coap_block_b_t block;
3749
3750 lg_crcv = coap_find_lg_crcv(session, rcvd);
3751 if (lg_crcv)
3752 coap_ticks(&lg_crcv->last_used);
3753
3754 if (COAP_RESPONSE_CLASS(rcvd->code) == 2 &&
3755 coap_get_block_b(session, rcvd, lg_xmit->option, &block)) {
3756
3757 if (block.bert) {
3758 coap_log_debug("found Block option, block is BERT, block nr. %u (%" PRIuS ")\n",
3759 block.num, lg_xmit->b.b1.bert_size);
3760 } else {
3761 coap_log_debug("found Block option, block size is %u, block nr. %u\n",
3762 1 << (block.szx + 4), block.num);
3763 }
3764 if (block.szx != lg_xmit->blk_size) {
3765 if (block.szx > lg_xmit->blk_size) {
3766 coap_log_info("ignoring request to increase Block size, "
3767 "(%u > %u)\n",
3768 1 << (block.szx + 4), 1 << (lg_xmit->blk_size + 4));
3769 } else if ((lg_xmit->offset + chunk) % ((size_t)1 << (block.szx + 4)) == 0) {
3770 /*
3771 * Recompute the block number of the previous packet given the
3772 * new block size
3773 */
3774 block.num = (uint32_t)(((lg_xmit->offset + chunk) >> (block.szx + 4)) - 1);
3775 lg_xmit->blk_size = block.szx;
3776 chunk = (size_t)1 << (lg_xmit->blk_size + 4);
3777 lg_xmit->offset = block.num * chunk;
3778 coap_log_debug("new Block size is %u, block number %u completed\n",
3779 1 << (block.szx + 4), block.num);
3780 block.bert = 0;
3781 block.aszx = block.szx;
3782 } else {
3783 coap_log_debug("ignoring request to increase Block size, "
3784 "next block is not aligned on requested block size boundary. "
3785 "(%" PRIuS " x %u mod %u = %" PRIuS " != 0)\n",
3786 lg_xmit->offset/chunk + 1, (1 << (lg_xmit->blk_size + 4)),
3787 (1 << (block.szx + 4)),
3788 (lg_xmit->offset + chunk) % ((size_t)1 << (block.szx + 4)));
3789 }
3790 }
3791 track_echo(session, rcvd);
3792 if (lg_xmit->last_block == (int)block.num &&
3793 lg_xmit->option != COAP_OPTION_Q_BLOCK1) {
3794 /*
3795 * Duplicate Block1 ACK
3796 *
3797 * RFCs not clear here, but on a lossy connection, there could
3798 * be multiple Block1 ACKs, causing the client to retransmit the
3799 * same block multiple times, or the server retransmitting the
3800 * same ACK.
3801 *
3802 * Once a block has been ACKd, there is no need to retransmit it.
3803 */
3804 return 1;
3805 }
3806 if (block.bert)
3807 block.num += (unsigned int)(lg_xmit->b.b1.bert_size / 1024 - 1);
3808 lg_xmit->last_block = block.num;
3809 lg_xmit->offset = (block.num + 1) * chunk;
3810 if (lg_xmit->offset < lg_xmit->data_info->length) {
3811 /* Build the next PDU request based off the skeletal PDU */
3812 uint8_t buf[8];
3813 coap_pdu_t *pdu;
3814 uint64_t token = STATE_TOKEN_FULL(lg_xmit->b.b1.state_token, ++lg_xmit->b.b1.count);
3815 size_t len = coap_encode_var_safe8(buf, sizeof(token), token);
3816
3817 if (lg_xmit->sent_pdu->code == COAP_REQUEST_CODE_FETCH) {
3818 /* Need to handle Observe for large FETCH */
3819 if (lg_crcv) {
3820 if (coap_binary_equal(lg_xmit->b.b1.app_token, lg_crcv->app_token)) {
3821 coap_bin_const_t *new_token;
3822 coap_bin_const_t ctoken = { len, buf };
3823
3824 /* Need to save/restore Observe Token for large FETCH */
3825 new_token = track_fetch_observe(lg_xmit->sent_pdu, lg_crcv, block.num + 1,
3826 &ctoken);
3827 if (new_token) {
3828 assert(len <= sizeof(buf));
3829 len = new_token->length;
3830 memcpy(buf, new_token->s, len);
3831 }
3832 }
3833 }
3834 }
3835 pdu = coap_pdu_duplicate_lkd(lg_xmit->sent_pdu, session, len, buf, NULL);
3836 if (!pdu)
3837 goto fail_body;
3838
3839 /*
3840 * If initial transmit was multicast, that would have been NON.
3841 * Make subsequent traffic CON for reliability.
3842 */
3843 if (session->sock.flags & COAP_SOCKET_MULTICAST) {
3844 pdu->type = COAP_MESSAGE_CON;
3845 }
3846
3847 block.num++;
3848 if (block.bert) {
3849 size_t token_options = pdu->data ? (size_t)(pdu->data - pdu->token) :
3850 pdu->used_size;
3851 block.m = (lg_xmit->data_info->length - lg_xmit->offset) >
3852 ((pdu->max_size - token_options) /1024) * 1024;
3853 } else {
3854 block.m = (lg_xmit->offset + chunk) < lg_xmit->data_info->length;
3855 }
3856 coap_update_option(pdu, lg_xmit->option,
3857 coap_encode_var_safe(buf, sizeof(buf),
3858 (block.num << 4) |
3859 (block.m << 3) |
3860 block.aszx),
3861 buf);
3862
3863 if (lg_xmit->data_info->get_func) {
3864#if COAP_CONSTRAINED_STACK
3865 /* Protected by global_lock if needed */
3866 static uint8_t l_data[1024];
3867#else /* ! COAP_CONSTRAINED_STACK */
3868 uint8_t l_data[1024];
3869#endif /* ! COAP_CONSTRAINED_STACK */
3870 size_t l_length;
3871
3872 assert(chunk <= 1024);
3873 if (lg_xmit->data_info->get_func(session, chunk,
3874 block.num * chunk, l_data, &l_length,
3875 lg_xmit->data_info->app_ptr)) {
3876 if (!coap_add_data(pdu, l_length, l_data)) {
3877 goto fail_body;
3878 }
3879 }
3880 } else {
3881 if (!coap_add_block_b_data(pdu,
3882 lg_xmit->data_info->length,
3883 lg_xmit->data_info->data,
3884 &block))
3885 goto fail_body;
3886 }
3887 lg_xmit->b.b1.bert_size = block.chunk_size;
3888 coap_ticks(&lg_xmit->last_sent);
3889#if COAP_Q_BLOCK_SUPPORT
3890 if (lg_xmit->option == COAP_OPTION_Q_BLOCK1 &&
3891 pdu->type == COAP_MESSAGE_NON) {
3892 if (coap_send_q_block1(session, block, pdu,
3893 COAP_SEND_INC_PDU) == COAP_INVALID_MID)
3894 goto fail_body;
3895 return 1;
3896 } else if (coap_send_internal(session, pdu, NULL) == COAP_INVALID_MID)
3897 goto fail_body;
3898#else /* ! COAP_Q_BLOCK_SUPPORT */
3899 if (coap_send_internal(session, pdu, NULL) == COAP_INVALID_MID)
3900 goto fail_body;
3901#endif /* ! COAP_Q_BLOCK_SUPPORT */
3902 return 1;
3903 }
3904 } else if (COAP_RESPONSE_CLASS(rcvd->code) == 2) {
3905 /*
3906 * Not a block response asking for the next block.
3907 * Could be an Observe response overlapping with block FETCH doing
3908 * Observe cancellation.
3909 */
3910 coap_opt_iterator_t opt_iter;
3911 coap_opt_t *obs_opt;
3912 int observe_action = -1;
3913
3914 if (lg_xmit->sent_pdu->code != COAP_REQUEST_CODE_FETCH) {
3915 goto lg_xmit_finished;
3916 }
3917 obs_opt = coap_check_option(lg_xmit->sent_pdu,
3919 &opt_iter);
3920 if (obs_opt) {
3921 observe_action = coap_decode_var_bytes(coap_opt_value(obs_opt),
3922 coap_opt_length(obs_opt));
3923 }
3924 if (observe_action != COAP_OBSERVE_CANCEL) {
3925 goto lg_xmit_finished;
3926 }
3927 obs_opt = coap_check_option(rcvd,
3929 &opt_iter);
3930 if (obs_opt) {
3931 return 0;
3932 }
3933 goto lg_xmit_finished;
3934 } else if (rcvd->code == COAP_RESPONSE_CODE(401)) {
3935 if (check_freshness(session, rcvd, sent, lg_xmit, NULL))
3936 return 1;
3937#if COAP_Q_BLOCK_SUPPORT
3938 } else if (rcvd->code == COAP_RESPONSE_CODE(402)) {
3939 /* Q-Block1 or Q-Block2 not present in p - duplicate error ? */
3940 if (coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK2, &block) ||
3941 coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK1, &block))
3942 return 1;
3943 } else if (rcvd->code == COAP_RESPONSE_CODE(408) &&
3944 lg_xmit->option == COAP_OPTION_Q_BLOCK1) {
3945 size_t length;
3946 const uint8_t *data;
3947 coap_opt_iterator_t opt_iter;
3948 coap_opt_t *fmt_opt = coap_check_option(rcvd,
3950 &opt_iter);
3951 uint16_t fmt = fmt_opt ?
3953 coap_opt_length(fmt_opt)) :
3955
3957 goto fail_body;
3958
3959 if (COAP_PROTO_RELIABLE(session->proto) ||
3960 rcvd->type != COAP_MESSAGE_NON) {
3961 coap_log_debug("Unexpected 4.08 - protocol violation - ignore\n");
3962 return 1;
3963 }
3964
3965 if (coap_get_data(rcvd, &length, &data)) {
3966 /* Need to decode CBOR to work out what blocks to re-send */
3967 const uint8_t *bp = data;
3968 uint32_t i;
3969 uint8_t buf[8];
3970 coap_pdu_t *pdu;
3971 uint64_t token;
3972 uint8_t ltoken[8];
3973 size_t ltoken_length;
3974
3975 for (i = 0; (bp < data + length) &&
3976 i < COAP_MAX_PAYLOADS(session); i++) {
3977 if ((*bp & 0xc0) != 0x00) /* uint(value) */
3978 goto fail_cbor;
3979 block.num = derive_cbor_value(&bp, data + length - bp);
3980 coap_log_debug("Q-Block1: Missing block %d\n", block.num);
3981 if (block.num > (1 << 20) -1)
3982 goto fail_cbor;
3983 block.m = (block.num + 1) * chunk < lg_xmit->data_info->length;
3984 block.szx = lg_xmit->blk_size;
3985
3986 /* Build the next PDU request based off the skeletal PDU */
3987 token = STATE_TOKEN_FULL(lg_xmit->b.b1.state_token,++lg_xmit->b.b1.count);
3988 ltoken_length = coap_encode_var_safe8(ltoken, sizeof(token), token);
3989 pdu = coap_pdu_duplicate_lkd(lg_xmit->sent_pdu, session, ltoken_length,
3990 ltoken, NULL);
3991 if (!pdu)
3992 goto fail_body;
3993
3994 coap_update_option(pdu, lg_xmit->option,
3995 coap_encode_var_safe(buf, sizeof(buf),
3996 (block.num << 4) |
3997 (block.m << 3) |
3998 block.szx),
3999 buf);
4000
4001 if (!coap_add_block(pdu,
4002 lg_xmit->data_info->length,
4003 lg_xmit->data_info->data,
4004 block.num,
4005 block.szx))
4006 goto fail_body;
4007 if (coap_send_internal(session, pdu, NULL) == COAP_INVALID_MID)
4008 goto fail_body;
4009 }
4010 return 1;
4011 }
4012fail_cbor:
4013 coap_log_info("Invalid application/missing-blocks+cbor-seq\n");
4014#endif /* COAP_Q_BLOCK_SUPPORT */
4015 }
4016 goto lg_xmit_finished;
4017 }
4018 return 0;
4019
4020fail_body:
4022 /* There has been an internal error of some sort */
4023 rcvd->code = COAP_RESPONSE_CODE(500);
4024lg_xmit_finished:
4025 if (lg_crcv) {
4026 if (STATE_TOKEN_BASE(lg_xmit->b.b1.state_token) ==
4027 STATE_TOKEN_BASE(lg_crcv->state_token)) {
4028 /* In case of observe */
4029 lg_crcv->state_token = lg_xmit->b.b1.state_token;
4030 lg_crcv->retry_counter = lg_xmit->b.b1.count;
4031 }
4032 }
4033 if (!lg_crcv) {
4034 /* need to put back original token into rcvd */
4035 if (lg_xmit->b.b1.app_token)
4036 coap_update_token(rcvd, lg_xmit->b.b1.app_token->length,
4037 lg_xmit->b.b1.app_token->s);
4038 coap_log_debug("Client app version of updated PDU (1)\n");
4040 } else {
4041 lg_crcv->sent_pdu->lg_xmit = 0;
4042 }
4043
4044 if (sent) {
4045 /* need to put back original token into sent */
4046 if (lg_xmit->b.b1.app_token)
4047 coap_update_token(sent, lg_xmit->b.b1.app_token->length,
4048 lg_xmit->b.b1.app_token->s);
4049 if (sent->lg_xmit)
4050 coap_remove_option(sent, sent->lg_xmit->option);
4051 sent->lg_xmit = NULL;
4052 }
4053 LL_DELETE(session->lg_xmit, lg_xmit);
4054 coap_block_delete_lg_xmit(session, lg_xmit);
4055 return 0;
4056}
4057#endif /* COAP_CLIENT_SUPPORT */
4058
4059void
4061 coap_block_data_handler_t block_data_handler) {
4062 context->block_data_cb = block_data_handler;
4063}
4064
4065/*
4066 * Re-assemble payloads into a body
4067 */
4069coap_block_build_body(coap_binary_t *body_data, size_t length,
4070 const uint8_t *data, size_t offset, size_t total) {
4071 if (data == NULL)
4072 return NULL;
4073 if (body_data == NULL && total) {
4074 body_data = coap_new_binary(total);
4075 }
4076 if (body_data == NULL)
4077 return NULL;
4078
4079 /* Check no overflow (including a 8 byte small headroom) */
4080 if (SIZE_MAX - length < 8 || offset > SIZE_MAX - length - 8) {
4081 return NULL;
4082 }
4083
4084 /* Update saved data */
4085 if (offset + length <= total && body_data->length >= total) {
4086 memcpy(&body_data->s[offset], data, length);
4087 } else {
4088 /*
4089 * total may be inaccurate as per
4090 * https://rfc-editor.org/rfc/rfc7959#section-4
4091 * o In a request carrying a Block1 Option, to indicate the current
4092 * estimate the client has of the total size of the resource
4093 * representation, measured in bytes ("size indication").
4094 * o In a response carrying a Block2 Option, to indicate the current
4095 * estimate the server has of the total size of the resource
4096 * representation, measured in bytes ("size indication").
4097 */
4098 coap_binary_t *new = coap_resize_binary(body_data, offset + length);
4099
4100 if (new) {
4101 body_data = new;
4102 memcpy(&body_data->s[offset], data, length);
4103 } else {
4104 coap_delete_binary(body_data);
4105 return NULL;
4106 }
4107 }
4108 return body_data;
4109}
4110
4111#if COAP_CLIENT_SUPPORT
4112/*
4113 * Need to see if this is a large body response to a request. If so,
4114 * need to initiate the request for the next block and not trouble the
4115 * application. Note that Token must be unique per request/response.
4116 *
4117 * This is set up using coap_send()
4118 * Client receives large data from server ((Q-)Block2)
4119 *
4120 * Return: 0 Call application handler
4121 * 1 Do not call application handler - just sent the next request
4122 */
4123int
4124coap_handle_response_get_block(coap_context_t *context,
4125 coap_session_t *session,
4126 coap_pdu_t *sent,
4127 coap_pdu_t *rcvd,
4128 coap_recurse_t recursive) {
4129 coap_lg_crcv_t *lg_crcv;
4130 coap_block_b_t block;
4131#if COAP_Q_BLOCK_SUPPORT
4132 coap_block_b_t qblock;
4133#endif /* COAP_Q_BLOCK_SUPPORT */
4134 int have_block = 0;
4135 uint16_t block_opt = 0;
4136 size_t offset;
4137 int ack_rst_sent = 0;
4138
4140 memset(&block, 0, sizeof(block));
4141#if COAP_Q_BLOCK_SUPPORT
4142 memset(&qblock, 0, sizeof(qblock));
4143#endif /* COAP_Q_BLOCK_SUPPORT */
4144 lg_crcv = coap_find_lg_crcv(session, rcvd);
4145 if (lg_crcv) {
4146 size_t chunk = 0;
4147 uint8_t buf[8];
4148 coap_opt_iterator_t opt_iter;
4149
4150 if (COAP_RESPONSE_CLASS(rcvd->code) == 2) {
4151 size_t length;
4152 const uint8_t *data;
4154 &opt_iter);
4155 size_t size2 = size_opt ?
4157 coap_opt_length(size_opt)) : 0;
4158
4159 /* length and data are cleared on error */
4160 (void)coap_get_data(rcvd, &length, &data);
4161 rcvd->body_offset = 0;
4162 rcvd->body_total = length;
4163 if (coap_get_block_b(session, rcvd, COAP_OPTION_BLOCK2, &block)) {
4164 have_block = 1;
4165 block_opt = COAP_OPTION_BLOCK2;
4166 }
4167#if COAP_Q_BLOCK_SUPPORT
4168 if (coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK2, &qblock)) {
4169 if (have_block) {
4170 coap_log_warn("Both Block1 and Q-Block1 not supported in a response\n");
4171 }
4172 have_block = 1;
4173 block_opt = COAP_OPTION_Q_BLOCK2;
4174 block = qblock;
4175 /* server indicating that it supports Q_BLOCK */
4176 if (!(session->block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
4177 set_block_mode_has_q(session->block_mode);
4178 }
4179 }
4180#endif /* COAP_Q_BLOCK_SUPPORT */
4181 track_echo(session, rcvd);
4182 if (have_block && (block.m || length)) {
4183 coap_opt_t *fmt_opt = coap_check_option(rcvd,
4185 &opt_iter);
4186 uint16_t fmt = fmt_opt ?
4188 coap_opt_length(fmt_opt)) :
4190 coap_opt_t *etag_opt = coap_check_option(rcvd,
4192 &opt_iter);
4193 size_t saved_offset;
4194 int updated_block;
4195
4196 if (length > block.chunk_size) {
4197 coap_log_debug("block: Oversized packet - reduced to %"PRIu32" from %" PRIuS "\n",
4198 block.chunk_size, length);
4199 length = block.chunk_size;
4200 }
4201 if (block.m && length != block.chunk_size) {
4202 coap_log_warn("block: Undersized packet - expected %"PRIu32", got %" PRIuS "\n",
4203 block.chunk_size, length);
4204 /* Unclear how to properly handle this */
4205 rcvd->code = COAP_RESPONSE_CODE(402);
4206 goto expire_lg_crcv;
4207 }
4208 /* Possibility that Size2 not sent, or is too small */
4209 chunk = (size_t)1 << (block.szx + 4);
4210 offset = block.num * chunk;
4211 if (size2 < (offset + length)) {
4212 if (block.m)
4213 size2 = offset + length + 1;
4214 else
4215 size2 = offset + length;
4216 }
4217 saved_offset = offset;
4218
4219 if (lg_crcv->initial) {
4220#if COAP_Q_BLOCK_SUPPORT
4221reinit:
4222#endif /* COAP_Q_BLOCK_SUPPORT */
4223 lg_crcv->initial = 0;
4224 if (lg_crcv->body_data) {
4225 coap_free_type(COAP_STRING, lg_crcv->body_data);
4226 lg_crcv->body_data = NULL;
4227 }
4228 if (etag_opt) {
4229 lg_crcv->etag_length = coap_opt_length(etag_opt);
4230 memcpy(lg_crcv->etag, coap_opt_value(etag_opt), lg_crcv->etag_length);
4231 lg_crcv->etag_set = 1;
4232 } else {
4233 lg_crcv->etag_set = 0;
4234 }
4235 lg_crcv->total_len = size2;
4236 lg_crcv->content_format = fmt;
4237 lg_crcv->szx = block.szx;
4238 lg_crcv->block_option = block_opt;
4239 lg_crcv->last_type = rcvd->type;
4240 lg_crcv->rec_blocks.used = 0;
4241 lg_crcv->rec_blocks.total_blocks = 0;
4242#if COAP_Q_BLOCK_SUPPORT
4243 lg_crcv->rec_blocks.processing_payload_set = 0;
4244#endif /* COAP_Q_BLOCK_SUPPORT */
4245 }
4246 if (lg_crcv->total_len < size2)
4247 lg_crcv->total_len = size2;
4248
4249 /* Check whether we can handle this size */
4250 uint32_t max_body;
4251 uint8_t max_block_szx;
4252
4253 max_block_szx = COAP_BLOCK_MAX_SIZE_GET(session->block_mode);
4254 if (max_block_szx == 0 || max_block_szx > block.szx) {
4255 max_block_szx = block.szx;
4256 }
4257 max_body = ((1UL << 20) * (1 << (max_block_szx + 4)));
4258 if (max_body > MAX_BLK_LEN)
4259 max_body = MAX_BLK_LEN;
4260 if ((context->max_body_size && size2 > context->max_body_size) ||
4261 (size2 > max_body)) {
4262 uint32_t max_body_size = context->max_body_size;
4263
4264 if (max_body_size == 0 || max_body < max_body_size) {
4265 max_body_size = max_body;
4266 }
4267 coap_log_warn("Unable to handle body size %" PRIuS " (max %" PRIu32 ")\n", size2, max_body_size);
4268 /* Try to hint to the server thare is an issue */
4269 coap_send_rst_lkd(session, rcvd);
4271 return 1;
4272 }
4273
4274 if (etag_opt) {
4275 if (!full_match(coap_opt_value(etag_opt),
4276 coap_opt_length(etag_opt),
4277 lg_crcv->etag, lg_crcv->etag_length)) {
4278 /* body of data has changed - need to restart request */
4279 coap_pdu_t *pdu;
4280 uint64_t token = STATE_TOKEN_FULL(lg_crcv->state_token,
4281 ++lg_crcv->retry_counter);
4282 size_t len = coap_encode_var_safe8(buf, sizeof(token), token);
4283 coap_opt_filter_t drop_options;
4284
4285#if COAP_Q_BLOCK_SUPPORT
4286 if (block_opt == COAP_OPTION_Q_BLOCK2)
4287 goto reinit;
4288#endif /* COAP_Q_BLOCK_SUPPORT */
4289
4290 coap_log_warn("Data body updated during receipt - new request started\n");
4291 if (!(session->block_mode & COAP_BLOCK_SINGLE_BODY))
4293
4294 lg_crcv->initial = 1;
4295 coap_free_type(COAP_STRING, lg_crcv->body_data);
4296 lg_crcv->body_data = NULL;
4297
4298 coap_session_new_token(session, &len, buf);
4299 memset(&drop_options, 0, sizeof(coap_opt_filter_t));
4302 pdu = coap_pdu_duplicate_lkd(lg_crcv->sent_pdu, session, len, buf, &drop_options);
4303 if (!pdu)
4304 goto fail_resp;
4305
4306 coap_update_option(pdu, block_opt,
4307 coap_encode_var_safe(buf, sizeof(buf),
4308 (0 << 4) | (0 << 3) | block.aszx),
4309 buf);
4310
4311 if (coap_send_internal(session, pdu, NULL) == COAP_INVALID_MID)
4312 goto fail_resp;
4313
4314 goto skip_app_handler;
4315 }
4316 } else if (lg_crcv->etag_set) {
4317 /* Cannot handle this change in ETag to not being there */
4318 coap_log_warn("Not all blocks have ETag option\n");
4319 goto fail_resp;
4320 }
4321
4322 if (fmt != lg_crcv->content_format) {
4323 coap_log_warn("Content-Format option mismatch\n");
4324 goto fail_resp;
4325 }
4326#if COAP_Q_BLOCK_SUPPORT
4327 if (block_opt == COAP_OPTION_Q_BLOCK2 && size2 != lg_crcv->total_len) {
4328 coap_log_warn("Size2 option mismatch\n");
4329 goto fail_resp;
4330 }
4331#endif /* COAP_Q_BLOCK_SUPPORT */
4332 if (block.num == 0) {
4333 coap_opt_t *obs_opt = coap_check_option(rcvd,
4335 &opt_iter);
4336 if (obs_opt) {
4337 lg_crcv->observe_length = min(coap_opt_length(obs_opt), 3);
4338 memcpy(lg_crcv->observe, coap_opt_value(obs_opt), lg_crcv->observe_length);
4339 lg_crcv->observe_set = 1;
4340 } else {
4341 lg_crcv->observe_set = 0;
4342 }
4343 }
4344 updated_block = 0;
4345 while (offset < saved_offset + length) {
4346 if (!check_if_received_block(&lg_crcv->rec_blocks, block.num)) {
4347#if COAP_Q_BLOCK_SUPPORT
4348 uint32_t this_payload_set = block.num / COAP_MAX_PAYLOADS(session);
4349#endif /* COAP_Q_BLOCK_SUPPORT */
4350
4351 coap_log_debug("found Block option, block size is %u, block nr. %u\n",
4352 1 << (block.szx + 4), block.num);
4353#if COAP_Q_BLOCK_SUPPORT
4354 if (block_opt == COAP_OPTION_Q_BLOCK2 && lg_crcv->rec_blocks.used &&
4355 this_payload_set > lg_crcv->rec_blocks.processing_payload_set &&
4356 this_payload_set != lg_crcv->rec_blocks.latest_payload_set) {
4357 coap_request_missing_q_block2(session, lg_crcv);
4358 }
4359 lg_crcv->rec_blocks.latest_payload_set = this_payload_set;
4360#endif /* COAP_Q_BLOCK_SUPPORT */
4361 /* Update list of blocks received */
4362 if (!update_received_blocks(&lg_crcv->rec_blocks, block.num, block.m)) {
4364 goto fail_resp;
4365 }
4366 updated_block = 1;
4367 }
4368 block.num++;
4369 offset = block.num << (block.szx + 4);
4370 if (!block.bert && block_opt != COAP_OPTION_Q_BLOCK2)
4371 break;
4372 }
4373 block.num--;
4374 /* Only process if not duplicate block */
4375 if (updated_block) {
4376 void *body_free;
4377
4378 /* Update last_used to prevent premature timeout during long transfers */
4379 coap_ticks(&lg_crcv->last_used);
4380
4381 if ((session->block_mode & COAP_SINGLE_BLOCK_OR_Q) || block.bert) {
4382 if (size2 < saved_offset + length) {
4383 size2 = saved_offset + length;
4384 }
4385 if (context && context->block_data_cb) {
4386 coap_response_t resp;
4387
4389 context->block_data_cb(session, rcvd, 0,
4390 &lg_crcv->body_data,
4391 length, data,
4392 saved_offset, size2));
4393 if (resp != COAP_RESPONSE_OK) {
4394 goto fail_resp;
4395 }
4396 } else {
4397 lg_crcv->body_data = coap_block_build_body(lg_crcv->body_data, length, data,
4398 saved_offset, size2);
4399 if (lg_crcv->body_data == NULL) {
4400 goto fail_resp;
4401 }
4402 }
4403 }
4404 if (block.m || !check_all_blocks_in(&lg_crcv->rec_blocks)) {
4405 /* Not all the payloads of the body have arrived */
4406 size_t len;
4407 coap_pdu_t *pdu;
4408 uint64_t token;
4409 coap_opt_filter_t drop_options;
4410
4411 if (block.m) {
4412#if COAP_Q_BLOCK_SUPPORT
4413 if (block_opt == COAP_OPTION_Q_BLOCK2) {
4414 /* Blocks could arrive in wrong order */
4415 if (check_all_blocks_in(&lg_crcv->rec_blocks)) {
4416 goto give_to_app;
4417 }
4418 if (check_all_blocks_in_for_payload_set(session,
4419 &lg_crcv->rec_blocks)) {
4420 block.num = lg_crcv->rec_blocks.range[0].end;
4421 /* Now requesting next payload */
4422 lg_crcv->rec_blocks.processing_payload_set =
4423 block.num / COAP_MAX_PAYLOADS(session) + 1;
4424 if (check_any_blocks_next_payload_set(session,
4425 &lg_crcv->rec_blocks)) {
4426 /* Need to ask for them individually */
4427 coap_request_missing_q_block2(session, lg_crcv);
4428 goto skip_app_handler;
4429 }
4430 } else {
4431 /* The remote end will be sending the next one unless this
4432 is a MAX_PAYLOADS and all previous have been received */
4433 goto skip_app_handler;
4434 }
4435 if (COAP_PROTO_RELIABLE(session->proto) ||
4436 rcvd->type != COAP_MESSAGE_NON)
4437 goto skip_app_handler;
4438
4439 } else
4440#endif /* COAP_Q_BLOCK_SUPPORT */
4441 block.m = 0;
4442
4443 /* Ask for the next block */
4444 token = STATE_TOKEN_FULL(lg_crcv->state_token, ++lg_crcv->retry_counter);
4445 len = coap_encode_var_safe8(buf, sizeof(token), token);
4446 memset(&drop_options, 0, sizeof(coap_opt_filter_t));
4448 pdu = coap_pdu_duplicate_lkd(lg_crcv->sent_pdu, session, len, buf, &drop_options);
4449 if (!pdu)
4450 goto fail_resp;
4451
4452 if (rcvd->type == COAP_MESSAGE_NON)
4453 pdu->type = COAP_MESSAGE_NON; /* Server is using NON */
4454
4455 /* Only sent with the first block */
4457
4458 coap_update_option(pdu, block_opt,
4459 coap_encode_var_safe(buf, sizeof(buf),
4460 ((block.num + 1) << 4) |
4461 (block.m << 3) | block.aszx),
4462 buf);
4463
4465 (void)coap_get_data(lg_crcv->sent_pdu, &length, &data);
4466 coap_add_data_large_internal(session, NULL, pdu, NULL, NULL, -1, 0, length, data, NULL, NULL, NULL,
4467 0, 0);
4468 }
4469 if (coap_send_internal(session, pdu, NULL) == COAP_INVALID_MID)
4470 /* Session could now be disconnected, so no lg_crcv */
4471 goto skip_app_handler;
4472 }
4473 if ((session->block_mode & COAP_SINGLE_BLOCK_OR_Q) || block.bert)
4474 goto skip_app_handler;
4475
4476 /* need to put back original token into rcvd */
4477 coap_update_token(rcvd, lg_crcv->app_token->length, lg_crcv->app_token->s);
4478 rcvd->body_offset = saved_offset;
4479#if COAP_Q_BLOCK_SUPPORT
4480 rcvd->body_total = block_opt == COAP_OPTION_Q_BLOCK2 ?
4481 lg_crcv->total_len : size2;
4482#else /* ! COAP_Q_BLOCK_SUPPORT */
4483 rcvd->body_total = size2;
4484#endif /* ! COAP_Q_BLOCK_SUPPORT */
4485 coap_log_debug("Client app version of updated PDU (2)\n");
4487
4488 if (sent) {
4489 /* need to put back original token into sent */
4490 if (lg_crcv->app_token)
4491 coap_update_token(sent, lg_crcv->app_token->length,
4492 lg_crcv->app_token->s);
4493 coap_remove_option(sent, lg_crcv->block_option);
4494 }
4495 goto call_app_handler;
4496 }
4497#if COAP_Q_BLOCK_SUPPORT
4498give_to_app:
4499#endif /* COAP_Q_BLOCK_SUPPORT */
4500 if ((session->block_mode & COAP_SINGLE_BLOCK_OR_Q) || block.bert) {
4501 /* Pretend that there is no block */
4502 coap_remove_option(rcvd, block_opt);
4503 if (lg_crcv->observe_set) {
4505 lg_crcv->observe_length, lg_crcv->observe);
4506 }
4507 rcvd->body_data = lg_crcv->body_data ? lg_crcv->body_data->s : NULL;
4508#if COAP_Q_BLOCK_SUPPORT
4509 if (context && context->block_data_cb) {
4510 /* Data has already been provided - do not duplicate */
4511 if (rcvd->data) {
4512 rcvd->used_size = rcvd->data - rcvd->token - 1;
4513 rcvd->data = NULL;
4514 }
4515 }
4516 rcvd->body_length = block_opt == COAP_OPTION_Q_BLOCK2 ?
4517 lg_crcv->total_len : saved_offset + length;
4518#else /* ! COAP_Q_BLOCK_SUPPORT */
4519 rcvd->body_length = saved_offset + length;
4520#endif /* ! COAP_Q_BLOCK_SUPPORT */
4521 rcvd->body_offset = 0;
4522 rcvd->body_total = rcvd->body_length;
4523 } else {
4524 rcvd->body_offset = saved_offset;
4525#if COAP_Q_BLOCK_SUPPORT
4526 rcvd->body_total = block_opt == COAP_OPTION_Q_BLOCK2 ?
4527 lg_crcv->total_len : size2;
4528#else /* ! COAP_Q_BLOCK_SUPPORT */
4529 rcvd->body_total = size2;
4530#endif /* ! COAP_Q_BLOCK_SUPPORT */
4531 }
4532 /* need to put back original token into rcvd */
4533 if (!coap_binary_equal(&rcvd->actual_token, lg_crcv->app_token)) {
4534 coap_update_token(rcvd, lg_crcv->app_token->length, lg_crcv->app_token->s);
4535 coap_log_debug("Client app version of updated PDU (3)\n");
4537 }
4538 if (sent) {
4539 /* need to put back original token into sent */
4540 if (lg_crcv->app_token)
4541 coap_update_token(sent, lg_crcv->app_token->length,
4542 lg_crcv->app_token->s);
4543 coap_remove_option(sent, lg_crcv->block_option);
4544 }
4545 body_free = lg_crcv->body_data;
4546 lg_crcv->body_data = NULL;
4547 coap_call_response_handler(session, sent, rcvd, body_free);
4548
4549 ack_rst_sent = 1;
4550 if (lg_crcv->observe_set == 0) {
4551 /* Expire this entry */
4552 LL_DELETE(session->lg_crcv, lg_crcv);
4553 coap_block_delete_lg_crcv(session, lg_crcv);
4554 goto skip_app_handler;
4555 }
4556 /* Set up for the next data body as observing */
4557 lg_crcv->initial = 1;
4558 }
4559 coap_ticks(&lg_crcv->last_used);
4560 goto skip_app_handler;
4561 } else {
4562 coap_opt_t *obs_opt = coap_check_option(rcvd,
4564 &opt_iter);
4565 if (context->max_body_size && length > context->max_body_size) {
4566 coap_log_warn("Unable to handle body size %" PRIuS " (max %" PRIu32 ")\n", length,
4567 context->max_body_size);
4568 /* Try to hint to the server thare is an issue */
4569 coap_send_rst_lkd(session, rcvd);
4571 return 1;
4572 }
4573 if (obs_opt) {
4574 lg_crcv->observe_length = min(coap_opt_length(obs_opt), 3);
4575 memcpy(lg_crcv->observe, coap_opt_value(obs_opt), lg_crcv->observe_length);
4576 lg_crcv->observe_set = 1;
4577 } else {
4578 lg_crcv->observe_set = 0;
4579 if (!coap_binary_equal(&rcvd->actual_token, lg_crcv->app_token)) {
4580 /* need to put back original token into rcvd */
4581 coap_update_token(rcvd, lg_crcv->app_token->length, lg_crcv->app_token->s);
4583 coap_log_debug("PDU presented to app.\n");
4585 }
4586 /* Expire this entry */
4587 goto expire_lg_crcv;
4588 }
4589 }
4590 coap_ticks(&lg_crcv->last_used);
4591 } else if (rcvd->code == COAP_RESPONSE_CODE(401)) {
4592#if COAP_OSCORE_SUPPORT
4593 if (check_freshness(session, rcvd,
4594 (session->oscore_encryption == 0) ? sent : NULL,
4595 NULL, lg_crcv))
4596#else /* !COAP_OSCORE_SUPPORT */
4597 if (check_freshness(session, rcvd, sent, NULL, lg_crcv))
4598#endif /* !COAP_OSCORE_SUPPORT */
4599 goto skip_app_handler;
4600 goto expire_lg_crcv;
4601 } else {
4602 /* Not 2.xx or 4.01 - assume it is a failure of some sort */
4603 goto expire_lg_crcv;
4604 }
4605 if (!block.m && !lg_crcv->observe_set) {
4606fail_resp:
4607 /* lg_crcv no longer required - cache it for 1 sec */
4608 coap_ticks(&lg_crcv->last_used);
4609 lg_crcv->last_used = lg_crcv->last_used - COAP_MAX_TRANSMIT_WAIT_TICKS(session) +
4611 }
4612 /* need to put back original token into rcvd */
4613 if (!coap_binary_equal(&rcvd->actual_token, lg_crcv->app_token)) {
4614 coap_update_token(rcvd, lg_crcv->app_token->length, lg_crcv->app_token->s);
4615 coap_log_debug("Client app version of updated PDU (4)\n");
4617 }
4618 }
4619
4620 /* Check if receiving a block response and if blocks can be set up */
4621 if (recursive == COAP_RECURSE_OK && !lg_crcv) {
4622 if (!sent) {
4623 if (coap_get_block_b(session, rcvd, COAP_OPTION_BLOCK2, &block)
4624#if COAP_Q_BLOCK_SUPPORT
4625 ||
4626 coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK2, &block)
4627#endif /* COAP_Q_BLOCK_SUPPORT */
4628 ) {
4629 coap_log_debug("** %s: large body receive internal issue\n",
4630 coap_session_str(session));
4631 goto skip_app_handler;
4632 }
4633 } else if (COAP_RESPONSE_CLASS(rcvd->code) == 2) {
4634 const uint8_t *data;
4635 size_t length;
4636
4637 if (coap_get_block_b(session, rcvd, COAP_OPTION_BLOCK2, &block)) {
4638#if COAP_Q_BLOCK_SUPPORT
4639 if (session->block_mode & COAP_BLOCK_PROBE_Q_BLOCK) {
4640 set_block_mode_drop_q(session->block_mode);
4641 coap_log_debug("Q-Block support disabled\n");
4642 }
4643#endif /* COAP_Q_BLOCK_SUPPORT */
4644 have_block = 1;
4645 if (block.num != 0) {
4646 /* Assume random access and just give the single response to app */
4647 size_t chunk = (size_t)1 << (block.szx + 4);
4648
4649 coap_get_data(rcvd, &length, &data);
4650 rcvd->body_offset = block.num*chunk;
4651 rcvd->body_total = block.num*chunk + length + (block.m ? 1 : 0);
4652 goto call_app_handler;
4653 }
4654 }
4655#if COAP_Q_BLOCK_SUPPORT
4656 else if (coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK2, &block)) {
4657 have_block = 1;
4658 /* server indicating that it supports Q_BLOCK2 */
4659 if (!(session->block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
4660 set_block_mode_has_q(session->block_mode);
4661 }
4662 }
4663#endif /* COAP_Q_BLOCK_SUPPORT */
4664 if (have_block) {
4665 lg_crcv = coap_block_new_lg_crcv(session, sent, NULL);
4666
4667 if (lg_crcv) {
4668 LL_PREPEND(session->lg_crcv, lg_crcv);
4669 return coap_handle_response_get_block(context, session, sent, rcvd,
4671 }
4672 }
4673 coap_get_data(rcvd, &length, &data);
4674 if (context->max_body_size && length > context->max_body_size) {
4675 coap_log_warn("Unable to handle body size %" PRIuS " (max %" PRIu32 ")\n", length,
4676 context->max_body_size);
4677 /* Try to hint to the server thare is an issue */
4678 coap_send_rst_lkd(session, rcvd);
4680 return 1;
4681 }
4682 track_echo(session, rcvd);
4683 } else if (rcvd->code == COAP_RESPONSE_CODE(401)) {
4684 lg_crcv = coap_block_new_lg_crcv(session, sent, NULL);
4685
4686 if (lg_crcv) {
4687 LL_PREPEND(session->lg_crcv, lg_crcv);
4688 return coap_handle_response_get_block(context, session, sent, rcvd,
4690 }
4691 }
4692 }
4693 return 0;
4694
4695expire_lg_crcv:
4696 /* need to put back original token into rcvd */
4697 if (!coap_binary_equal(&rcvd->actual_token, lg_crcv->app_token)) {
4698 coap_update_token(rcvd, lg_crcv->app_token->length, lg_crcv->app_token->s);
4699 coap_log_debug("Client app version of updated PDU (5)\n");
4701 }
4702
4703 if (sent) {
4704 /* need to put back original token into sent */
4705 if (lg_crcv->app_token)
4706 coap_update_token(sent, lg_crcv->app_token->length,
4707 lg_crcv->app_token->s);
4708 coap_remove_option(sent, lg_crcv->block_option);
4709 }
4710 /* Expire this entry */
4711 LL_DELETE(session->lg_crcv, lg_crcv);
4712 coap_block_delete_lg_crcv(session, lg_crcv);
4713
4714call_app_handler:
4715 return 0;
4716
4717skip_app_handler:
4718 if (!ack_rst_sent)
4719 coap_send_ack_lkd(session, rcvd);
4720 return 1;
4721}
4722#endif /* COAP_CLIENT_SUPPORT */
4723
4724#if COAP_SERVER_SUPPORT
4725/* Check if lg_xmit generated and update PDU code if so */
4726void
4728 const coap_pdu_t *request,
4729 coap_pdu_t *response, const coap_resource_t *resource,
4730 const coap_string_t *query) {
4731 coap_lg_xmit_t *lg_xmit;
4732
4733 if (response->code == 0)
4734 return;
4735 lg_xmit = coap_find_lg_xmit_response(session, request, resource, query);
4736 if (lg_xmit && lg_xmit->sent_pdu && lg_xmit->sent_pdu->code == 0) {
4737 lg_xmit->sent_pdu->code = response->code;
4738 return;
4739 }
4740}
4741#endif /* COAP_SERVER_SUPPORT */
4742
4743#if COAP_CLIENT_SUPPORT
4744void
4746 uint64_t token_match =
4748 pdu->actual_token.length));
4749 coap_lg_xmit_t *lg_xmit;
4750 coap_lg_crcv_t *lg_crcv;
4751
4752 if (session->lg_crcv) {
4753 LL_FOREACH(session->lg_crcv, lg_crcv) {
4754 if (coap_binary_equal(&pdu->actual_token, lg_crcv->app_token))
4755 return;
4756 if (token_match == STATE_TOKEN_BASE(lg_crcv->state_token)) {
4757 coap_update_token(pdu, lg_crcv->app_token->length,
4758 lg_crcv->app_token->s);
4759 coap_log_debug("Client app version of updated PDU (6)\n");
4761 return;
4762 }
4763 }
4764 }
4765 if (COAP_PDU_IS_REQUEST(pdu) && session->lg_xmit) {
4766 LL_FOREACH(session->lg_xmit, lg_xmit) {
4767 if (coap_binary_equal(&pdu->actual_token, lg_xmit->b.b1.app_token))
4768 return;
4769 if (token_match == STATE_TOKEN_BASE(lg_xmit->b.b1.state_token)) {
4770 coap_update_token(pdu, lg_xmit->b.b1.app_token->length,
4771 lg_xmit->b.b1.app_token->s);
4772 coap_log_debug("Client app version of updated PDU (7)\n");
4774 return;
4775 }
4776 }
4777 }
4778}
4779#endif /* ! COAP_CLIENT_SUPPORT */
int coap_is_mcast(const coap_address_t *a)
Checks if given address a denotes a multicast address.
void coap_address_copy(coap_address_t *dst, const coap_address_t *src)
int coap_address_equals(const coap_address_t *a, const coap_address_t *b)
Compares given address objects a and b.
static void coap_block_release_lg_xmit_data(coap_session_t *session, coap_lg_xmit_data_t *data_info)
#define COAP_ETAG_MAX_BYTES
Definition coap_block.c:26
COAP_STATIC_INLINE int full_match(const uint8_t *a, size_t alen, const uint8_t *b, size_t blen)
Definition coap_block.c:472
#define MAX_BLK_LEN
static int coap_add_data_large_internal(coap_session_t *session, const coap_pdu_t *request, coap_pdu_t *pdu, coap_resource_t *resource, const coap_string_t *query, int maxage, uint64_t etag, size_t length, const uint8_t *data, coap_release_large_data_t release_func, coap_get_large_data_t get_func, void *app_ptr, int single_request, coap_pdu_code_t request_method)
Definition coap_block.c:776
static int update_received_blocks(coap_rblock_t *rec_blocks, uint32_t block_num, uint32_t block_m)
static int check_all_blocks_in(coap_rblock_t *rec_blocks)
static int setup_block_b(coap_session_t *session, coap_pdu_t *pdu, coap_block_b_t *block, unsigned int num, unsigned int blk_size, size_t total)
Definition coap_block.c:134
#define min(a, b)
Definition coap_block.c:21
static int check_if_received_block(coap_rblock_t *rec_blocks, uint32_t block_num)
int coap_flsll(long long j)
Definition coap_encode.c:28
int coap_fls(unsigned int i)
Definition coap_encode.c:21
struct coap_lg_crcv_t coap_lg_crcv_t
struct coap_resource_t coap_resource_t
struct coap_lg_srcv_t coap_lg_srcv_t
#define PRIuS
#define PRIu32
@ COAP_NACK_TOO_MANY_RETRIES
Definition coap_io.h:65
#define COAP_SOCKET_MULTICAST
socket is used for multicast communication
Library specific build wrapper for coap_internal.h.
#define COAP_API
@ COAP_LG_XMIT
Definition coap_mem.h:49
@ COAP_LG_CRCV
Definition coap_mem.h:50
@ COAP_LG_SRCV
Definition coap_mem.h:51
@ COAP_STRING
Definition coap_mem.h:33
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().
uint8_t coap_unique_id[8]
Definition coap_net.c:5185
#define NULL
Definition coap_option.h:30
uint16_t coap_option_num_t
Definition coap_option.h:37
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:43
coap_mid_t coap_send_rst_lkd(coap_session_t *session, const coap_pdu_t *request)
Sends an RST message with code 0 for the specified request to dst.
Definition coap_net.c:1093
void coap_call_response_handler(coap_session_t *session, coap_pdu_t *sent, coap_pdu_t *rcvd, void *body_free)
coap_mid_t coap_send_ack_lkd(coap_session_t *session, const coap_pdu_t *request)
Sends an ACK message with code 0 for the specified request to dst.
Definition coap_net.c:1108
#define coap_check_update_token(a, b)
int coap_context_set_max_block_size_lkd(coap_context_t *context, size_t max_block_size)
Set the context level maximum block size that the server supports when sending or receiving packets w...
Definition coap_block.c:448
void coap_context_set_block_mode_lkd(coap_context_t *context, uint32_t block_mode)
Set the context level CoAP block handling bits for handling RFC7959.
Definition coap_block.c:423
#define COAP_BLOCK_MAX_SIZE_SET(a)
#define COAP_RBLOCK_CNT
void coap_check_code_lg_xmit(const coap_session_t *session, const coap_pdu_t *request, coap_pdu_t *response, const coap_resource_t *resource, const coap_string_t *query)
The function checks that the code in a newly formed lg_xmit created by coap_add_data_large_response_l...
void coap_block_delete_lg_xmit(coap_session_t *session, coap_lg_xmit_t *lg_xmit)
#define STATE_TOKEN_FULL(t, r)
#define COAP_SINGLE_BLOCK_OR_Q
#define STATE_TOKEN_BASE(t)
#define COAP_BLOCK_SET_MASK
coap_lg_xmit_t * coap_find_lg_xmit(coap_session_t *session, coap_pdu_t *pdu)
Find the current lg_xmit for the session that matches the pdu.
Definition coap_block.c:478
#define COAP_BLOCK_MAX_SIZE_GET(a)
int coap_block_check_lg_xmit_timeouts(coap_session_t *session, coap_tick_t now, coap_tick_t *tim_rem)
@ COAP_RECURSE_OK
@ COAP_RECURSE_NO
COAP_API void coap_context_set_block_mode(coap_context_t *context, uint32_t block_mode)
Set the context level CoAP block handling bits for handling RFC7959.
Definition coap_block.c:415
COAP_API int coap_add_data_large_response(coap_resource_t *resource, coap_session_t *session, const coap_pdu_t *request, coap_pdu_t *response, const coap_string_t *query, uint16_t media_type, int maxage, uint64_t etag, size_t length, const uint8_t *data, coap_release_large_data_t release_func, void *app_ptr)
Associates given data with the response pdu that is passed as fourth parameter.
int(* coap_get_large_data_t)(coap_session_t *session, size_t max, size_t offset, uint8_t *data, size_t *length, void *app_ptr)
Callback handler for getting the data based on app_ptr provided to coap_add_data_large_request_app() ...
Definition coap_block.h:361
#define COAP_BLOCK_USE_M_Q_BLOCK
Definition coap_block.h:68
#define COAP_OPT_BLOCK_SZX(opt)
Returns the value of the SZX-field of a Block option opt.
Definition coap_block.h:95
#define COAP_BLOCK_STLESS_BLOCK2
Definition coap_block.h:71
COAP_API int coap_add_data_large_request(coap_session_t *session, coap_pdu_t *pdu, size_t length, const uint8_t *data, coap_release_large_data_t release_func, void *app_ptr)
Associates given data with the pdu that is passed as second parameter.
#define COAP_BLOCK_TRY_Q_BLOCK
Definition coap_block.h:67
#define COAP_BLOCK_STLESS_FETCH
Definition coap_block.h:70
COAP_API int coap_context_set_max_block_size(coap_context_t *context, size_t max_block_size)
Set the context level maximum block size that the server supports when sending or receiving packets w...
Definition coap_block.c:437
int coap_add_block_b_data(coap_pdu_t *pdu, size_t len, const uint8_t *data, coap_block_b_t *block)
Adds the appropriate payload data of the body to the pdu.
Definition coap_block.c:254
#define COAP_BLOCK_SINGLE_BODY
Definition coap_block.h:66
int coap_write_block_b_opt(coap_session_t *session, coap_block_b_t *block, coap_option_num_t number, coap_pdu_t *pdu, size_t data_length)
Writes a block option of type number to message pdu.
Definition coap_block.c:209
int coap_add_block(coap_pdu_t *pdu, size_t len, const uint8_t *data, unsigned int block_num, unsigned char block_szx)
Adds the block_num block of size 1 << (block_szx + 4) from source data to pdu.
Definition coap_block.c:240
void(* coap_release_large_data_t)(coap_session_t *session, void *app_ptr)
Callback handler for de-allocating the data based on app_ptr provided to coap_add_data_large_*() func...
Definition coap_block.h:292
void coap_add_data_blocked_response(const coap_pdu_t *request, coap_pdu_t *response, uint16_t media_type, int maxage, size_t length, const uint8_t *data)
Adds the appropriate part of data to the response pdu.
Definition coap_block.c:279
int coap_get_block_b(const coap_session_t *session, const coap_pdu_t *pdu, coap_option_num_t number, coap_block_b_t *block)
Initializes block from pdu.
Definition coap_block.c:64
#define COAP_OPT_BLOCK_MORE(opt)
Returns the value of the More-bit of a Block option opt.
Definition coap_block.h:91
unsigned int coap_opt_block_num(const coap_opt_t *block_opt)
Returns the value of field num in the given block option block_opt.
Definition coap_block.c:45
int coap_get_block(const coap_pdu_t *pdu, coap_option_num_t number, coap_block_t *block)
Initializes block from pdu.
Definition coap_block.c:117
#define COAP_BLOCK_NOT_RANDOM_BLOCK1
Definition coap_block.h:72
#define COAP_OPT_BLOCK_END_BYTE(opt)
Returns the value of the last byte of opt.
Definition coap_block.h:86
int coap_write_block_opt(coap_block_t *block, coap_option_num_t number, coap_pdu_t *pdu, size_t data_length)
Writes a block option of type number to message pdu.
Definition coap_block.c:176
COAP_API int coap_add_data_large_request_app(coap_session_t *session, coap_pdu_t *pdu, size_t length, coap_release_large_data_t release_func, coap_get_large_data_t get_func, void *app_ptr)
Associates given data callback with the pdu that is passed as second parameter.
#define COAP_BLOCK_FORCE_Q_BLOCK
Definition coap_block.h:74
coap_binary_t * coap_block_build_body(coap_binary_t *body_data, size_t length, const uint8_t *data, size_t offset, size_t total)
Re-assemble payloads into a body.
#define COAP_BLOCK_USE_LIBCOAP
Definition coap_block.h:65
time_t coap_time_t
CoAP time in seconds since epoch.
Definition coap_time.h:154
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition coap_time.h:149
coap_time_t coap_ticks_to_rt(coap_tick_t t)
Helper function that converts coap ticks to wallclock time.
Definition coap_time.c:123
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition coap_time.h:164
#define COAP_MAX_DELAY_TICKS
Definition coap_time.h:231
int coap_handle_event_lkd(coap_context_t *context, coap_event_t event, coap_session_t *session)
Invokes the event handler of context for the given event and data.
Definition coap_net.c:5026
uint16_t coap_new_message_id_lkd(coap_session_t *session)
Returns a new message id and updates session->tx_mid accordingly.
int coap_client_delay_first(coap_session_t *session)
Delay the sending of the first client request until some other negotiation has completed.
Definition coap_net.c:1365
coap_mid_t coap_send_internal(coap_session_t *session, coap_pdu_t *pdu, coap_pdu_t *request_pdu)
Sends a CoAP message to given peer.
Definition coap_net.c:1885
void coap_register_block_data_handler(coap_context_t *context, coap_block_data_handler_t block_data_handler)
Sets up a handler that is called for each received block during a block-wise transfer when COAP_BLOCK...
coap_response_t
Definition coap_net.h:51
coap_response_t(* coap_block_data_handler_t)(coap_session_t *session, coap_pdu_t *pdu, coap_resource_t *resource, coap_binary_t **body_data, size_t length, const uint8_t *data, size_t offset, size_t total)
Definition of the block data handler function.
Definition coap_net.h:133
void coap_ticks(coap_tick_t *t)
Returns the current value of an internal tick counter.
Definition coap_time.c:90
@ COAP_RESPONSE_OK
Response is fine.
Definition coap_net.h:53
unsigned int coap_encode_var_safe(uint8_t *buf, size_t length, unsigned int val)
Encodes multiple-length byte sequences.
Definition coap_encode.c:47
unsigned int coap_decode_var_bytes(const uint8_t *buf, size_t len)
Decodes multiple-length byte sequences.
Definition coap_encode.c:38
uint64_t coap_decode_var_bytes8(const uint8_t *buf, size_t len)
Decodes multiple-length byte sequences.
Definition coap_encode.c:71
unsigned int coap_encode_var_safe8(uint8_t *buf, size_t length, uint64_t val)
Encodes multiple-length byte sequences.
Definition coap_encode.c:81
@ COAP_EVENT_BLOCK_ISSUE
Triggered when a block transfer could not be handled.
Definition coap_event.h:77
@ COAP_EVENT_PARTIAL_BLOCK
Triggered when not all of a large body has been received.
Definition coap_event.h:73
@ COAP_EVENT_XMIT_BLOCK_FAIL
Triggered when not all of a large body has been transmitted.
Definition coap_event.h:75
#define coap_lock_callback(func)
Dummy for no thread-safe code.
#define coap_lock_callback_ret(r, func)
Dummy for no thread-safe code.
#define coap_lock_unlock()
Dummy for no thread-safe code.
#define coap_lock_check_locked()
Dummy for no thread-safe code.
#define coap_lock_lock(failed)
Dummy for no thread-safe code.
#define coap_log_debug(...)
Definition coap_debug.h:126
void coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu)
Display the contents of the specified pdu.
Definition coap_debug.c:793
const char * coap_session_str(const coap_session_t *session)
Get session description.
#define coap_log_info(...)
Definition coap_debug.h:114
#define coap_log_warn(...)
Definition coap_debug.h:108
@ COAP_LOG_DEBUG
Definition coap_debug.h:64
#define COAP_OBSERVE_CANCEL
The value COAP_OBSERVE_CANCEL in a GET/FETCH request option COAP_OPTION_OBSERVE indicates that the ob...
#define COAP_OBSERVE_ESTABLISH
The value COAP_OBSERVE_ESTABLISH in a GET/FETCH request option COAP_OPTION_OBSERVE indicates a new ob...
COAP_API int coap_cancel_observe(coap_session_t *session, coap_binary_t *token, coap_pdu_type_t message_type)
Cancel an observe that is being tracked by the client large receive logic.
coap_opt_t * coap_option_next(coap_opt_iterator_t *oi)
Updates the iterator oi to point to the next option.
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_set(coap_opt_filter_t *filter, coap_option_num_t option)
Sets the corresponding entry for number in filter.
int coap_rebuild_pdu_for_proxy(coap_pdu_t *pdu)
Convert PDU to use Proxy-Scheme option if Proxy-Uri option is present.
size_t coap_oscore_overhead(coap_session_t *session, coap_pdu_t *pdu)
Determine the additional data size requirements for adding in OSCORE.
#define COAP_PDU_IS_RESPONSE(pdu)
coap_pdu_t * coap_pdu_reference_lkd(coap_pdu_t *pdu)
Increment reference counter on a pdu to stop it prematurely getting freed off when coap_delete_pdu() ...
Definition coap_pdu.c:1662
void coap_delete_pdu_lkd(coap_pdu_t *pdu)
Dispose of an CoAP PDU and free off associated storage.
Definition coap_pdu.c:194
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:639
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:499
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:423
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:733
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:234
#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:349
#define COAP_PDU_IS_REQUEST(pdu)
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:789
#define COAP_OPTION_BLOCK2
Definition coap_pdu.h:140
const char * coap_response_phrase(unsigned char code)
Returns a human-readable response phrase for the specified CoAP response code.
Definition coap_pdu.c:960
#define COAP_MEDIATYPE_APPLICATION_MB_CBOR_SEQ
Definition coap_pdu.h:257
#define COAP_OPTION_CONTENT_FORMAT
Definition coap_pdu.h:130
#define COAP_OPTION_SIZE2
Definition coap_pdu.h:142
#define COAP_OPTION_BLOCK1
Definition coap_pdu.h:141
#define COAP_OPTION_Q_BLOCK1
Definition coap_pdu.h:137
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition coap_pdu.h:266
#define COAP_OPTION_URI_PATH
Definition coap_pdu.h:129
#define COAP_RESPONSE_CODE(N)
Definition coap_pdu.h:163
#define COAP_RESPONSE_CLASS(C)
Definition coap_pdu.h:166
coap_pdu_code_t
Set of codes available for a PDU.
Definition coap_pdu.h:330
#define COAP_OPTION_SIZE1
Definition coap_pdu.h:146
coap_pdu_type_t
CoAP PDU message type definitions.
Definition coap_pdu.h:70
#define COAP_MEDIATYPE_TEXT_PLAIN
Definition coap_pdu.h:216
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:366
#define COAP_OPTION_CONTENT_TYPE
Definition coap_pdu.h:131
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:779
#define COAP_OPTION_Q_BLOCK2
Definition coap_pdu.h:143
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:885
#define COAP_OPTION_RTAG
Definition coap_pdu.h:149
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:102
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:893
#define COAP_INVALID_MID
Indicates an invalid message id.
Definition coap_pdu.h:269
#define COAP_OPTION_MAXAGE
Definition coap_pdu.h:133
#define COAP_OPTION_ETAG
Definition coap_pdu.h:123
#define COAP_OPTION_OBSERVE
Definition coap_pdu.h:125
#define COAP_OPTION_ECHO
Definition coap_pdu.h:147
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:854
@ COAP_REQUEST_CODE_GET
Definition coap_pdu.h:333
@ COAP_REQUEST_CODE_FETCH
Definition coap_pdu.h:337
@ COAP_MESSAGE_NON
Definition coap_pdu.h:72
@ COAP_MESSAGE_ACK
Definition coap_pdu.h:73
@ COAP_MESSAGE_CON
Definition coap_pdu.h:71
#define COAP_NON_RECEIVE_TIMEOUT_TICKS(s)
The NON_RECEIVE_TIMEOUT definition for the session (s).
#define COAP_NON_TIMEOUT_TICKS(s)
void coap_handle_nack(coap_session_t *session, coap_pdu_t *sent, const coap_nack_reason_t reason, const coap_mid_t mid)
#define COAP_MAX_TRANSMIT_WAIT_TICKS(s)
#define COAP_NON_PARTIAL_TIMEOUT_TICKS(s)
The NON_PARTIAL_TIMEOUT definition for the session (s).
coap_tick_t coap_get_non_timeout_random_ticks(coap_session_t *session)
#define COAP_NSTART(s)
#define COAP_MAX_PAYLOADS(s)
size_t coap_session_max_pdu_size_lkd(const coap_session_t *session)
Get maximum acceptable PDU size.
#define COAP_NON_MAX_RETRANSMIT(s)
#define COAP_PROTO_NOT_RELIABLE(p)
#define COAP_PROTO_RELIABLE(p)
void coap_session_new_token(coap_session_t *session, size_t *len, uint8_t *data)
Creates a new token for use.
@ COAP_SESSION_TYPE_CLIENT
client-side
void coap_delete_bin_const(coap_bin_const_t *s)
Deletes the given const binary data and releases any memory allocated.
Definition coap_str.c:129
void coap_delete_str_const(coap_str_const_t *s)
Deletes the given const string and releases any memory allocated.
Definition coap_str.c:65
coap_binary_t * coap_new_binary(size_t size)
Returns a new binary object with at least size bytes storage allocated.
Definition coap_str.c:81
coap_bin_const_t * coap_new_bin_const(const uint8_t *data, size_t size)
Take the specified byte array (text) and create a coap_bin_const_t * Returns a new const binary objec...
Definition coap_str.c:119
coap_binary_t * coap_resize_binary(coap_binary_t *s, size_t size)
Resizes the given coap_binary_t object.
Definition coap_str.c:86
void coap_delete_binary(coap_binary_t *s)
Deletes the given coap_binary_t object and releases any memory allocated.
Definition coap_str.c:114
#define coap_binary_equal(binary1, binary2)
Compares the two binary data for equality.
Definition coap_str.h:222
#define coap_string_equal(string1, string2)
Compares the two strings for equality.
Definition coap_str.h:208
coap_string_t * coap_new_string(size_t size)
Returns a new string object with at least size+1 bytes storage allocated.
Definition coap_str.c:21
coap_str_const_t * coap_new_str_const(const uint8_t *data, size_t size)
Returns a new const string object with at least size+1 bytes storage allocated, and the provided data...
Definition coap_str.c:55
void coap_delete_string(coap_string_t *s)
Deletes the given string and releases any memory allocated.
Definition coap_str.c:50
int coap_q_block_is_supported(void)
Check whether Q-BlockX is available.
Definition coap_block.c:39
#define COAP_STATIC_INLINE
Definition libcoap.h:57
coap_address_t remote
remote address and port
Definition coap_io.h:58
coap_address_t local
local address and port
Definition coap_io.h:59
CoAP binary data definition with const data.
Definition coap_str.h:65
size_t length
length of binary data
Definition coap_str.h:66
const uint8_t * s
read-only binary data
Definition coap_str.h:67
CoAP binary data definition.
Definition coap_str.h:57
size_t length
length of binary data
Definition coap_str.h:58
uint8_t * s
binary data
Definition coap_str.h:59
Structure of Block options with BERT support.
Definition coap_block.h:55
unsigned int num
block number
Definition coap_block.h:56
uint32_t chunk_size
‍1024 if BERT
Definition coap_block.h:62
unsigned int bert
Operating as BERT.
Definition coap_block.h:61
unsigned int aszx
block size (0-7 including BERT
Definition coap_block.h:59
unsigned int defined
Set if block found.
Definition coap_block.h:60
unsigned int m
1 if more blocks follow, 0 otherwise
Definition coap_block.h:57
unsigned int szx
block size (0-6)
Definition coap_block.h:58
Structure of Block options.
Definition coap_block.h:46
unsigned int num
block number
Definition coap_block.h:47
unsigned int szx
block size
Definition coap_block.h:49
unsigned int m
1 if more blocks follow, 0 otherwise
Definition coap_block.h:48
The CoAP stack's global state is stored in a coap_context_t object.
coap_block_data_handler_t block_data_cb
Called with each block data during block transfers.
uint32_t max_body_size
Max supported body size or 0 is unlimited.
uint32_t block_mode
Zero or more COAP_BLOCK_ or'd options.
uint64_t state_token
state token
size_t bert_size
size of last BERT block
coap_address_t upstream
uint32_t count
the number of packets sent for payload
coap_binary_t * app_token
original PDU token
coap_pdu_code_t request_method
Method used to request this data.
uint8_t rtag_length
RTag length.
coap_string_t * query
Associated query for the resource.
uint64_t etag
ETag value.
coap_resource_t * resource
associated resource
coap_time_t maxage_expire
When this entry expires.
uint8_t rtag_set
Set if RTag is in receive PDU.
uint8_t rtag[8]
RTag for block checking.
coap_get_large_data_t get_func
Where to get data id needed.
void * app_ptr
applicaton provided ptr for de-alloc function
uint32_t ref
Reference count.
const uint8_t * data
large data ptr
size_t length
large data length
coap_release_large_data_t release_func
large data de-alloc function
Structure to hold large body (many blocks) transmission information.
coap_tick_t last_all_sent
Last time all data sent or 0.
uint8_t blk_size
large block transmission size
coap_tick_t last_sent
Last time any data sent.
union coap_lg_xmit_t::@1 b
coap_lg_xmit_data_t * data_info
Pointer to large data information.
int last_block
last acknowledged block number Block1 last transmitted Q-Block2
coap_tick_t last_payload
Last time MAX_PAYLOAD was sent or 0.
size_t offset
large data next offset to transmit
coap_pdu_t * sent_pdu
The sent pdu with all the data.
coap_l_block1_t b1
coap_l_block2_t b2
uint16_t option
large block transmisson CoAP option
struct coap_lg_xmit_t * next
coap_tick_t last_obs
Last time used (Observe tracking) or 0.
Iterator to run through PDU options.
coap_option_num_t number
decoded option number
structure for CoAP PDUs
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)
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
size_t used_size
used bytes of 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
Queue entry.
Structure to keep track of received blocks.
uint32_t total_blocks
Set to block no + 1 when More bit unset.
uint32_t used
Number of range blocks in use.
struct coap_lg_range range[COAP_RBLOCK_CNT]
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
coap_lg_xmit_t * lg_xmit
list of large transmissions
uint32_t block_mode
Zero or more COAP_BLOCK_ or'd options.
coap_socket_t sock
socket object for the session, if any
uint8_t csm_bert_rem_support
CSM TCP BERT blocks supported (remote)
uint64_t tx_token
Next token number to use.
coap_mid_t remote_test_mid
mid used for checking remote support
uint8_t csm_bert_loc_support
CSM TCP BERT blocks supported (local)
coap_addr_tuple_t addr_info
remote/local address info
coap_proto_t proto
protocol used
uint8_t con_active
Active CON request sent.
coap_queue_t * delayqueue
list of delayed messages waiting to be sent
uint32_t tx_rtag
Next Request-Tag number to use.
coap_session_type_t type
client or server side socket
coap_context_t * context
session's context
coap_bin_const_t * echo
last token used to make a request
coap_socket_flags_t flags
1 or more of COAP_SOCKET* flag values
CoAP string data definition.
Definition coap_str.h:39
uint8_t * s
string data
Definition coap_str.h:41
size_t length
length of string
Definition coap_str.h:40