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