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