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