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