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