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