libcoap 4.3.5-develop-109842b
Loading...
Searching...
No Matches
coap_block.c
Go to the documentation of this file.
1/* coap_block.c -- block transfer
2 *
3 * Copyright (C) 2010--2012,2015-2024 Olaf Bergmann <bergmann@tzi.org> and others
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 *
7 * This file is part of the CoAP library libcoap. Please see
8 * README for terms of use.
9 */
10
17
18#ifndef min
19#define min(a,b) ((a) < (b) ? (a) : (b))
20#endif
21
22/* 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);
556 }
557#else /* ! COAP_Q_BLOCK_SUPPORT */
558 mid = coap_send_internal(session, pdu);
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);
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 = -1;
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);
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 = -1;
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 = -1;
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);
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);
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);
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)) == COAP_INVALID_MID) {
1932 /* Not expected, underlying issue somewhere */
1933 coap_delete_pdu(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(block_pdu);
1977 coap_delete_pdu(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(block_pdu);
1988 coap_delete_pdu(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);
1995 if (mid == COAP_INVALID_MID) {
1996 /* Not expected, underlying issue somewhere */
1997 coap_delete_pdu(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
2012coap_block_check_q_block1_xmit(coap_session_t *session, coap_tick_t now) {
2013 coap_lg_xmit_t *lg_xmit;
2014 coap_lg_xmit_t *q;
2015 coap_tick_t timed_out;
2016 coap_tick_t tim_rem = (coap_tick_t)-1;
2017
2018 LL_FOREACH_SAFE(session->lg_xmit, lg_xmit, q) {
2019 coap_tick_t non_timeout = lg_xmit->non_timeout_random_ticks;
2020
2021 if (now < non_timeout)
2022 return non_timeout - now;
2023 timed_out = now - non_timeout;
2024
2025 if (lg_xmit->last_payload) {
2026 if (lg_xmit->last_payload <= timed_out) {
2027 /* Send off the next MAX_PAYLOAD set */
2028 coap_block_b_t block;
2029 size_t chunk = (size_t)1 << (lg_xmit->blk_size + 4);
2030
2031 memset(&block, 0, sizeof(block));
2032 block.num = (uint32_t)(lg_xmit->offset / chunk);
2033 block.m = lg_xmit->offset + chunk < lg_xmit->length;
2034 block.szx = lg_xmit->blk_size;
2035 coap_send_q_blocks(session, lg_xmit, block, &lg_xmit->pdu, COAP_SEND_SKIP_PDU);
2036 if (tim_rem > non_timeout)
2037 tim_rem = non_timeout;
2038 } else {
2039 /* Delay until the next MAX_PAYLOAD needs to be sent off */
2040 if (tim_rem > lg_xmit->last_payload - timed_out)
2041 tim_rem = lg_xmit->last_payload - timed_out;
2042 }
2043 } else if (lg_xmit->last_all_sent) {
2044 non_timeout = COAP_NON_TIMEOUT_TICKS(session);
2045 if (lg_xmit->last_all_sent + 4 * non_timeout <= now) {
2046 /* Expire this entry */
2047 LL_DELETE(session->lg_xmit, lg_xmit);
2048 coap_block_delete_lg_xmit(session, lg_xmit);
2049 } else {
2050 /* Delay until the lg_xmit needs to expire */
2051 if (tim_rem > lg_xmit->last_all_sent + 4 * non_timeout - now)
2052 tim_rem = lg_xmit->last_all_sent + 4 * non_timeout - now;
2053 }
2054 }
2055 }
2056 return tim_rem;
2057}
2058#endif /* COAP_CLIENT_SUPPORT */
2059
2060#if COAP_SERVER_SUPPORT
2062coap_block_check_q_block2_xmit(coap_session_t *session, coap_tick_t now) {
2063 coap_lg_xmit_t *lg_xmit;
2064 coap_lg_xmit_t *q;
2065 coap_tick_t timed_out;
2066 coap_tick_t tim_rem = (coap_tick_t)-1;
2067
2068 LL_FOREACH_SAFE(session->lg_xmit, lg_xmit, q) {
2069 coap_tick_t non_timeout = lg_xmit->non_timeout_random_ticks;
2070
2071 if (now < non_timeout)
2072 return non_timeout - now;
2073 timed_out = now - non_timeout;
2074
2075 if (lg_xmit->last_payload) {
2076 if (lg_xmit->last_payload <= timed_out) {
2077 /* Send off the next MAX_PAYLOAD set */
2078 coap_block_b_t block;
2079 size_t chunk = (size_t)1 << (lg_xmit->blk_size + 4);
2080
2081 memset(&block, 0, sizeof(block));
2082 block.num = (uint32_t)(lg_xmit->offset / chunk);
2083 block.m = lg_xmit->offset + chunk < lg_xmit->length;
2084 block.szx = lg_xmit->blk_size;
2085 if (block.num == (uint32_t)lg_xmit->last_block)
2086 coap_send_q_blocks(session, lg_xmit, block, &lg_xmit->pdu, COAP_SEND_SKIP_PDU);
2087 if (tim_rem > non_timeout)
2088 tim_rem = non_timeout;
2089 } else {
2090 /* Delay until the next MAX_PAYLOAD needs to be sent off */
2091 if (tim_rem > lg_xmit->last_payload - timed_out)
2092 tim_rem = lg_xmit->last_payload - timed_out;
2093 }
2094 } else if (lg_xmit->last_all_sent) {
2095 non_timeout = COAP_NON_TIMEOUT_TICKS(session);
2096 if (lg_xmit->last_all_sent + 4 * non_timeout <= now) {
2097 /* Expire this entry */
2098 LL_DELETE(session->lg_xmit, lg_xmit);
2099 coap_block_delete_lg_xmit(session, lg_xmit);
2100 } else {
2101 /* Delay until the lg_xmit needs to expire */
2102 if (tim_rem > lg_xmit->last_all_sent + 4 * non_timeout - now)
2103 tim_rem = lg_xmit->last_all_sent + 4 * non_timeout - now;
2104 }
2105 }
2106 }
2107 return tim_rem;
2108}
2109#endif /* COAP_SERVER_SUPPORT */
2110#endif /* COAP_Q_BLOCK_SUPPORT */
2111
2112#if COAP_CLIENT_SUPPORT
2113/*
2114 * If Observe = 0, save the token away and return NULL
2115 * Else If Observe = 1, return the saved token for this block
2116 * Else, return NULL
2117 */
2118static coap_bin_const_t *
2119track_fetch_observe(coap_pdu_t *pdu, coap_lg_crcv_t *lg_crcv,
2120 uint32_t block_num, coap_bin_const_t *token) {
2121 /* Need to handle Observe for large FETCH */
2122 coap_opt_iterator_t opt_iter;
2124 &opt_iter);
2125
2126 if (opt && lg_crcv) {
2127 int observe_action = -1;
2128 coap_bin_const_t **tmp;
2129
2130 observe_action = coap_decode_var_bytes(coap_opt_value(opt),
2131 coap_opt_length(opt));
2132 if (observe_action == COAP_OBSERVE_ESTABLISH) {
2133 /* Save the token in lg_crcv */
2134 if (lg_crcv->obs_token_cnt <= block_num) {
2135 size_t i;
2136
2137 tmp = coap_realloc_type(COAP_STRING, lg_crcv->obs_token,
2138 (block_num + 1) * sizeof(lg_crcv->obs_token[0]));
2139 if (tmp == NULL)
2140 return NULL;
2141 lg_crcv->obs_token = tmp;
2142 for (i = lg_crcv->obs_token_cnt; i < block_num + 1; i++) {
2143 lg_crcv->obs_token[i] = NULL;
2144 }
2145 }
2146 coap_delete_bin_const(lg_crcv->obs_token[block_num]);
2147
2148 lg_crcv->obs_token_cnt = block_num + 1;
2149 lg_crcv->obs_token[block_num] = coap_new_bin_const(token->s,
2150 token->length);
2151 if (lg_crcv->obs_token[block_num] == NULL)
2152 return NULL;
2153 } else if (observe_action == COAP_OBSERVE_CANCEL) {
2154 /* Use the token in lg_crcv */
2155 if (block_num < lg_crcv->obs_token_cnt) {
2156 return lg_crcv->obs_token[block_num];
2157 }
2158 }
2159 }
2160 return NULL;
2161}
2162
2163#if COAP_Q_BLOCK_SUPPORT
2165coap_send_q_block1(coap_session_t *session,
2166 coap_block_b_t block,
2167 coap_pdu_t *request,
2168 coap_send_pdu_t send_request) {
2169 /* Need to send up to MAX_PAYLOAD blocks if this is a Q_BLOCK1 */
2170 coap_lg_xmit_t *lg_xmit;
2171 uint64_t token_match =
2173 request->actual_token.length));
2174
2175 LL_FOREACH(session->lg_xmit, lg_xmit) {
2176 if (lg_xmit->option == COAP_OPTION_Q_BLOCK1 &&
2177 (token_match == STATE_TOKEN_BASE(lg_xmit->b.b1.state_token) ||
2178 token_match ==
2180 lg_xmit->b.b1.app_token->length))))
2181 break;
2182 /* try out the next one */
2183 }
2184 return coap_send_q_blocks(session, lg_xmit, block, request, send_request);
2185}
2186#endif /* COAP_Q_BLOCK_SUPPORT */
2187#endif /* COAP_CLIENT_SUPPORT */
2188
2189#if COAP_SERVER_SUPPORT
2190#if COAP_Q_BLOCK_SUPPORT
2191/*
2192 * response is always released before return IF COAP_SEND_INC_PDU
2193 */
2195coap_send_q_block2(coap_session_t *session,
2196 coap_resource_t *resource,
2197 const coap_string_t *query,
2198 coap_pdu_code_t request_method,
2199 coap_block_b_t block,
2200 coap_pdu_t *response,
2201 coap_send_pdu_t send_response) {
2202 /* Need to send up to MAX_PAYLOAD blocks if this is a Q_BLOCK2 */
2203 coap_lg_xmit_t *lg_xmit;
2204 coap_string_t empty = { 0, NULL};
2205
2206 LL_FOREACH(session->lg_xmit, lg_xmit) {
2207 if (lg_xmit->option == COAP_OPTION_Q_BLOCK2 &&
2208 resource == lg_xmit->b.b2.resource &&
2209 request_method == lg_xmit->b.b2.request_method &&
2210 coap_string_equal(query ? query : &empty,
2211 lg_xmit->b.b2.query ? lg_xmit->b.b2.query : &empty))
2212 break;
2213 }
2214 return coap_send_q_blocks(session, lg_xmit, block, response, send_response);
2215}
2216#endif /* COAP_Q_BLOCK_SUPPORT */
2217#endif /* COAP_SERVER_SUPPORT */
2218
2219#if COAP_CLIENT_SUPPORT
2220#if COAP_Q_BLOCK_SUPPORT
2221/*
2222 * Send out a test PDU for Q-Block.
2223 */
2225coap_block_test_q_block(coap_session_t *session, coap_pdu_t *actual) {
2226 coap_pdu_t *pdu;
2227 uint8_t token[8];
2228 size_t token_len;
2229 uint8_t buf[4];
2230 coap_mid_t mid;
2231
2232#if NDEBUG
2233 (void)actual;
2234#endif /* NDEBUG */
2235 assert(session->block_mode & COAP_BLOCK_TRY_Q_BLOCK &&
2236 session->type == COAP_SESSION_TYPE_CLIENT &&
2237 COAP_PDU_IS_REQUEST(actual));
2238
2239 coap_log_debug("Testing for Q-Block support\n");
2240 /* RFC9177 Section 4.1 when checking if available */
2242 coap_new_message_id_lkd(session),
2244 if (!pdu) {
2245 return COAP_INVALID_MID;
2246 }
2247
2248 coap_session_new_token(session, &token_len, token);
2249 coap_add_token(pdu, token_len, token);
2250 /* Use a resource that the server MUST support (.well-known/core) */
2252 11, (const u_char *)".well-known");
2254 4, (const u_char *)"core");
2255 /*
2256 * M needs to be unset as 'asking' for only the first block using
2257 * Q-Block2 as a test for server support.
2258 * See RFC9177 Section 4.4 Using the Q-Block2 Option.
2259 *
2260 * As the client is asking for 16 byte chunks, it is unlikely that
2261 * the .well-known/core response will be 16 bytes or less, so
2262 * if the server supports Q-Block, it will be forced to respond with
2263 * a Q-Block2, so the client can detect the server Q-Block support.
2264 */
2266 coap_encode_var_safe(buf, sizeof(buf),
2267 (0 << 4) | (0 << 3) | 0),
2268 buf);
2269 set_block_mode_probe_q(session->block_mode);
2270 mid = coap_send_internal(session, pdu);
2271 if (mid == COAP_INVALID_MID)
2272 return COAP_INVALID_MID;
2273 session->remote_test_mid = mid;
2274 return mid;
2275}
2276#endif /* COAP_Q_BLOCK_SUPPORT */
2277
2280 coap_lg_xmit_t *lg_xmit) {
2281 coap_block_b_t block;
2282 coap_lg_crcv_t *lg_crcv;
2283 uint64_t state_token = STATE_TOKEN_FULL(++session->tx_token, 1);
2284 size_t token_options = pdu->data ? (size_t)(pdu->data - pdu->token) :
2285 pdu->used_size;
2286 size_t data_len = lg_xmit ? lg_xmit->length :
2287 pdu->data ?
2288 pdu->used_size - (pdu->data - pdu->token) : 0;
2289
2290 lg_crcv = coap_malloc_type(COAP_LG_CRCV, sizeof(coap_lg_crcv_t));
2291
2292 if (lg_crcv == NULL)
2293 return NULL;
2294
2295 coap_log_debug("** %s: lg_crcv %p initialized - stateless token xxxx%012llx\n",
2296 coap_session_str(session), (void *)lg_crcv,
2297 STATE_TOKEN_BASE(state_token));
2298 memset(lg_crcv, 0, sizeof(coap_lg_crcv_t));
2299 lg_crcv->initial = 1;
2300 coap_ticks(&lg_crcv->last_used);
2301 /* Set up skeletal PDU to use as a basis for all the subsequent blocks */
2302 memcpy(&lg_crcv->pdu, pdu, sizeof(lg_crcv->pdu));
2303 /* Make sure that there is space for increased token + option change */
2304 lg_crcv->pdu.max_size = token_options + data_len + 9;
2305 lg_crcv->pdu.used_size = token_options + data_len;
2306 lg_crcv->pdu.token = coap_malloc_type(COAP_PDU_BUF,
2307 token_options + data_len + lg_crcv->pdu.max_hdr_size);
2308 if (!lg_crcv->pdu.token) {
2309 coap_block_delete_lg_crcv(session, lg_crcv);
2310 return NULL;
2311 }
2312 lg_crcv->pdu.token += lg_crcv->pdu.max_hdr_size;
2313 memcpy(lg_crcv->pdu.token, pdu->token, token_options);
2314 if (lg_crcv->pdu.data) {
2315 lg_crcv->pdu.data = lg_crcv->pdu.token + token_options;
2316 assert(pdu->data);
2317 memcpy(lg_crcv->pdu.data, lg_xmit ? lg_xmit->data : pdu->data, data_len);
2318 }
2319
2320 /* Need to keep original token for updating response PDUs */
2321 lg_crcv->app_token = coap_new_binary(pdu->actual_token.length);
2322 if (!lg_crcv->app_token) {
2323 coap_block_delete_lg_crcv(session, lg_crcv);
2324 return NULL;
2325 }
2326 memcpy(lg_crcv->app_token->s, pdu->actual_token.s, pdu->actual_token.length);
2327
2328 /* Need to set up a base token for actual communications if retries needed */
2329 lg_crcv->retry_counter = 1;
2330 lg_crcv->state_token = state_token;
2331
2332 if (pdu->code == COAP_REQUEST_CODE_FETCH) {
2333 coap_bin_const_t *new_token;
2334
2335 /* Need to save/restore Observe Token for large FETCH */
2336 new_token = track_fetch_observe(pdu, lg_crcv, 0, &pdu->actual_token);
2337 if (new_token)
2338 coap_update_token(pdu, new_token->length, new_token->s);
2339 }
2340
2341 if (coap_get_block_b(session, pdu, COAP_OPTION_BLOCK1, &block)) {
2342 /* In case it is there - must not be in continuing request PDUs */
2343 lg_crcv->o_block_option = COAP_OPTION_BLOCK1;
2344 lg_crcv->o_blk_size = block.aszx;
2345 coap_remove_option(&lg_crcv->pdu, COAP_OPTION_BLOCK1);
2346 }
2347
2348 if (lg_xmit)
2349 lg_xmit->lg_crcv = lg_crcv;
2350
2351 return lg_crcv;
2352}
2353
2354void
2356 coap_lg_crcv_t *lg_crcv) {
2357 size_t i;
2358 coap_lg_xmit_t *lg_xmit;
2359
2360#if (COAP_MAX_LOGGING_LEVEL < _COAP_LOG_DEBUG)
2361 (void)session;
2362#endif
2363 if (lg_crcv == NULL)
2364 return;
2365
2366 LL_FOREACH(session->lg_xmit, lg_xmit) {
2367 if (lg_xmit->lg_crcv == lg_crcv) {
2368 lg_xmit->lg_crcv = NULL;
2369 break;
2370 }
2371 }
2372
2373 if (lg_crcv->pdu.token)
2374 coap_free_type(COAP_PDU_BUF, lg_crcv->pdu.token - lg_crcv->pdu.max_hdr_size);
2376 coap_log_debug("** %s: lg_crcv %p released\n",
2377 coap_session_str(session), (void *)lg_crcv);
2378 coap_delete_binary(lg_crcv->app_token);
2379 for (i = 0; i < lg_crcv->obs_token_cnt; i++) {
2380 coap_delete_bin_const(lg_crcv->obs_token[i]);
2381 }
2383 coap_free_type(COAP_LG_CRCV, lg_crcv);
2384}
2385#endif /* COAP_CLIENT_SUPPORT */
2386
2387#if COAP_SERVER_SUPPORT
2388void
2390 coap_lg_srcv_t *lg_srcv) {
2391#if (COAP_MAX_LOGGING_LEVEL < _COAP_LOG_DEBUG)
2392 (void)session;
2393#endif
2394 if (lg_srcv == NULL)
2395 return;
2396
2400 coap_log_debug("** %s: lg_srcv %p released\n",
2401 coap_session_str(session), (void *)lg_srcv);
2402 coap_free_type(COAP_LG_SRCV, lg_srcv);
2403}
2404#endif /* COAP_SERVER_SUPPORT */
2405
2406void
2408 coap_lg_xmit_t *lg_xmit) {
2409 if (lg_xmit == NULL)
2410 return;
2411
2412 if (lg_xmit->release_func) {
2413 coap_lock_callback(session->context, lg_xmit->release_func(session, lg_xmit->app_ptr));
2414 }
2415 if (lg_xmit->pdu.token) {
2416 coap_free_type(COAP_PDU_BUF, lg_xmit->pdu.token - lg_xmit->pdu.max_hdr_size);
2417 }
2418 if (COAP_PDU_IS_REQUEST(&lg_xmit->pdu))
2419 coap_delete_binary(lg_xmit->b.b1.app_token);
2420 else
2421 coap_delete_string(lg_xmit->b.b2.query);
2422
2423 coap_log_debug("** %s: lg_xmit %p released\n",
2424 coap_session_str(session), (void *)lg_xmit);
2425 coap_free_type(COAP_LG_XMIT, lg_xmit);
2426}
2427
2428#if COAP_SERVER_SUPPORT
2429typedef struct {
2430 uint32_t num;
2431 int is_continue;
2432} send_track;
2433
2434static int
2435add_block_send(uint32_t num, int is_continue, send_track *out_blocks,
2436 uint32_t *count, uint32_t max_count) {
2437 uint32_t i;
2438
2439 for (i = 0; i < *count && *count < max_count; i++) {
2440 if (num == out_blocks[i].num)
2441 return 0;
2442 else if (num < out_blocks[i].num) {
2443 if (*count - i > 1)
2444 memmove(&out_blocks[i], &out_blocks[i+1], *count - i -1);
2445 out_blocks[i].num = num;
2446 out_blocks[i].is_continue = is_continue;
2447 (*count)++;
2448 return 1;
2449 }
2450 }
2451 if (*count < max_count) {
2452 out_blocks[i].num = num;
2453 out_blocks[i].is_continue = is_continue;
2454 (*count)++;
2455 return 1;
2456 }
2457 return 0;
2458}
2459
2460/*
2461 * Need to see if this is a request for the next block of a large body
2462 * transfer. If so, need to initiate the response with the next blocks
2463 * and not trouble the application.
2464 *
2465 * If additional responses needed, then these are expicitly sent out and
2466 * 'response' is updated to be the last response to be sent. There can be
2467 * multiple Q-Block2 in the request, as well as the 'Continue' Q-Block2
2468 * request.
2469 *
2470 * This is set up using coap_add_data_large_response_lkd()
2471 *
2472 * Server is sending a large data response to GET / observe (Block2)
2473 *
2474 * Return: 0 Call application handler
2475 * 1 Do not call application handler - just send the built response
2476 */
2477int
2479 coap_pdu_t *pdu,
2480 coap_pdu_t *response,
2481 coap_resource_t *resource,
2482 coap_string_t *query) {
2483 coap_lg_xmit_t *lg_xmit = NULL;
2484 coap_block_b_t block;
2485 coap_block_b_t alt_block;
2486 uint16_t block_opt = 0;
2487 send_track *out_blocks = NULL;
2488 const char *error_phrase;
2489 coap_opt_iterator_t opt_iter;
2490 size_t chunk;
2491 coap_opt_iterator_t opt_b_iter;
2492 coap_opt_t *option;
2493 uint32_t request_cnt, i;
2494 coap_opt_t *etag_opt = NULL;
2495 coap_pdu_t *out_pdu = response;
2496#if COAP_Q_BLOCK_SUPPORT
2497 size_t max_block;
2498
2499 /* Is client indicating that it supports Q_BLOCK2 ? */
2500 if (coap_get_block_b(session, pdu, COAP_OPTION_Q_BLOCK2, &block)) {
2501 if (!(session->block_mode & COAP_BLOCK_HAS_Q_BLOCK))
2502 set_block_mode_has_q(session->block_mode);
2503 block_opt = COAP_OPTION_Q_BLOCK2;
2504 }
2505#endif /* COAP_Q_BLOCK_SUPPORT */
2506 if (coap_get_block_b(session, pdu, COAP_OPTION_BLOCK2, &alt_block)) {
2507 if (block_opt) {
2508 coap_log_warn("Block2 and Q-Block2 cannot be in the same request\n");
2509 coap_add_data(response, sizeof("Both Block2 and Q-Block2 invalid")-1,
2510 (const uint8_t *)"Both Block2 and Q-Block2 invalid");
2511 response->code = COAP_RESPONSE_CODE(400);
2512 goto skip_app_handler;
2513 }
2514 block = alt_block;
2515 block_opt = COAP_OPTION_BLOCK2;
2516 }
2517 if (block_opt == 0)
2518 return 0;
2519 if (block.num == 0) {
2520 /* Get a fresh copy of the data */
2521 return 0;
2522 }
2523 lg_xmit = coap_find_lg_xmit_response(session, pdu, resource, query);
2524 if (lg_xmit == NULL)
2525 return 0;
2526
2527#if COAP_Q_BLOCK_SUPPORT
2528 out_blocks = coap_malloc_type(COAP_STRING, sizeof(send_track) * COAP_MAX_PAYLOADS(session));
2529#else /* ! COAP_Q_BLOCK_SUPPORT */
2530 out_blocks = coap_malloc_type(COAP_STRING, sizeof(send_track));
2531#endif /* ! COAP_Q_BLOCK_SUPPORT */
2532 if (!out_blocks) {
2533 goto internal_issue;
2534 }
2535
2536 /* lg_xmit (response) found */
2537
2538 etag_opt = coap_check_option(pdu, COAP_OPTION_ETAG, &opt_iter);
2539 if (etag_opt) {
2540 /* There may be multiple ETag - need to check each one */
2541 coap_option_iterator_init(pdu, &opt_iter, COAP_OPT_ALL);
2542 while ((etag_opt = coap_option_next(&opt_iter))) {
2543 if (opt_iter.number == COAP_OPTION_ETAG) {
2544 uint64_t etag = coap_decode_var_bytes8(coap_opt_value(etag_opt),
2545 coap_opt_length(etag_opt));
2546 if (etag == lg_xmit->b.b2.etag) {
2547 break;
2548 }
2549 }
2550 }
2551 if (!etag_opt) {
2552 /* Not a match - pass up to a higher level */
2553 return 0;
2554 }
2555 }
2556 out_pdu->code = lg_xmit->pdu.code;
2557 coap_ticks(&lg_xmit->last_obs);
2558 lg_xmit->last_all_sent = 0;
2559
2560 chunk = (size_t)1 << (lg_xmit->blk_size + 4);
2561 if (block_opt) {
2562 if (block.bert) {
2563 coap_log_debug("found Block option, block is BERT, block nr. %u, M %d\n",
2564 block.num, block.m);
2565 } else {
2566 coap_log_debug("found Block option, block size is %u, block nr. %u, M %d\n",
2567 1 << (block.szx + 4), block.num, block.m);
2568 }
2569 if (block.bert == 0 && block.szx != lg_xmit->blk_size) {
2570 if (block.num == 0) {
2571 if ((lg_xmit->offset + chunk) % ((size_t)1 << (block.szx + 4)) == 0) {
2572 /*
2573 * Recompute the block number of the previous packet given
2574 * the new block size
2575 */
2576 block.num = (uint32_t)(((lg_xmit->offset + chunk) >> (block.szx + 4)) - 1);
2577 lg_xmit->blk_size = block.szx;
2578 chunk = (size_t)1 << (lg_xmit->blk_size + 4);
2579 lg_xmit->offset = block.num * chunk;
2580 coap_log_debug("new Block size is %u, block number %u completed\n",
2581 1 << (block.szx + 4), block.num);
2582 } else {
2583 coap_log_debug("ignoring request to increase Block size, "
2584 "next block is not aligned on requested block size "
2585 "boundary. (%zu x %u mod %u = %zu (which is not 0)\n",
2586 lg_xmit->offset/chunk + 1, (1 << (lg_xmit->blk_size + 4)),
2587 (1 << (block.szx + 4)),
2588 (lg_xmit->offset + chunk) % ((size_t)1 << (block.szx + 4)));
2589 }
2590 } else {
2591 coap_log_debug("ignoring request to change Block size from %u to %u\n",
2592 (1 << (lg_xmit->blk_size + 4)), (1 << (block.szx + 4)));
2593 block.szx = block.aszx = lg_xmit->blk_size;
2594 }
2595 }
2596 }
2597
2598 /*
2599 * Need to check if there are multiple Q-Block2 requests. If so, they
2600 * need to be sent out in order of requests with the final request being
2601 * handled as per singular Block 2 request.
2602 */
2603 request_cnt = 0;
2604#if COAP_Q_BLOCK_SUPPORT
2605 max_block = (lg_xmit->length + chunk - 1)/chunk;
2606#endif /* COAP_Q_BLOCK_SUPPORT */
2607 coap_option_iterator_init(pdu, &opt_b_iter, COAP_OPT_ALL);
2608 while ((option = coap_option_next(&opt_b_iter))) {
2609 uint32_t num;
2610 if (opt_b_iter.number != lg_xmit->option)
2611 continue;
2612 num = coap_opt_block_num(option);
2613 if (num > 0xFFFFF) /* 20 bits max for num */
2614 continue;
2615 if (block.aszx != COAP_OPT_BLOCK_SZX(option)) {
2616 coap_add_data(response,
2617 sizeof("Changing blocksize during request invalid")-1,
2618 (const uint8_t *)"Changing blocksize during request invalid");
2619 response->code = COAP_RESPONSE_CODE(400);
2620 goto skip_app_handler;
2621 }
2622#if COAP_Q_BLOCK_SUPPORT
2623 if (COAP_OPT_BLOCK_MORE(option) && lg_xmit->option == COAP_OPTION_Q_BLOCK2) {
2624 if ((num % COAP_MAX_PAYLOADS(session)) == 0) {
2625 if (num == 0) {
2626 /* This is a repeat request for everything - hmm */
2627 goto call_app_handler;
2628 }
2629 /* 'Continue' request */
2630 for (i = 0; i < COAP_MAX_PAYLOADS(session) &&
2631 num + i < max_block; i++) {
2632 add_block_send(num + i, 1, out_blocks, &request_cnt,
2633 COAP_MAX_PAYLOADS(session));
2634 lg_xmit->last_block = num + i;
2635 }
2636 } else {
2637 /* Requesting remaining payloads in this MAX_PAYLOADS */
2638 for (i = 0; i < COAP_MAX_PAYLOADS(session) -
2639 num % COAP_MAX_PAYLOADS(session) &&
2640 num + i < max_block; i++) {
2641 add_block_send(num + i, 0, out_blocks, &request_cnt,
2642 COAP_MAX_PAYLOADS(session));
2643 }
2644 }
2645 } else
2646 add_block_send(num, 0, out_blocks, &request_cnt,
2647 COAP_MAX_PAYLOADS(session));
2648#else /* ! COAP_Q_BLOCK_SUPPORT */
2649 add_block_send(num, 0, out_blocks, &request_cnt, 1);
2650 break;
2651#endif /* ! COAP_Q_BLOCK_SUPPORT */
2652 }
2653 if (request_cnt == 0) {
2654 /* Block2 or Q-Block2 not found - give them the first block */
2655 block.szx = lg_xmit->blk_size;
2656 lg_xmit->offset = 0;
2657 out_blocks[0].num = 0;
2658 out_blocks[0].is_continue = 0;
2659 request_cnt = 1;
2660 }
2661
2662 for (i = 0; i < request_cnt; i++) {
2663 uint8_t buf[8];
2664
2665 block.num = out_blocks[i].num;
2666 lg_xmit->offset = block.num * chunk;
2667
2668 if (i + 1 < request_cnt) {
2669 /* Need to set up a copy of the pdu to send */
2670 coap_opt_filter_t drop_options;
2671
2672 memset(&drop_options, 0, sizeof(coap_opt_filter_t));
2673 if (block.num != 0)
2675 if (out_blocks[i].is_continue) {
2676 out_pdu = coap_pdu_duplicate_lkd(&lg_xmit->pdu, session, lg_xmit->pdu.actual_token.length,
2677 lg_xmit->pdu.actual_token.s, &drop_options);
2678 } else {
2679 out_pdu = coap_pdu_duplicate_lkd(&lg_xmit->pdu, session, pdu->actual_token.length,
2680 pdu->actual_token.s, &drop_options);
2681 }
2682 if (!out_pdu) {
2683 goto internal_issue;
2684 }
2685 } else {
2686 if (out_blocks[i].is_continue)
2687 coap_update_token(response, lg_xmit->pdu.actual_token.length,
2688 lg_xmit->pdu.actual_token.s);
2689 /*
2690 * Copy the options across and then fix the block option
2691 *
2692 * Need to drop Observe option if Block2 and block.num != 0
2693 */
2694 coap_option_iterator_init(&lg_xmit->pdu, &opt_iter, COAP_OPT_ALL);
2695 while ((option = coap_option_next(&opt_iter))) {
2696 if (opt_iter.number == COAP_OPTION_OBSERVE && block.num != 0)
2697 continue;
2698 if (!coap_insert_option(response, opt_iter.number,
2699 coap_opt_length(option),
2700 coap_opt_value(option))) {
2701 goto internal_issue;
2702 }
2703 }
2704 out_pdu = response;
2705 }
2706 if (pdu->type == COAP_MESSAGE_NON)
2707 out_pdu->type = COAP_MESSAGE_NON;
2708 if (block.bert) {
2709 size_t token_options = pdu->data ? (size_t)(pdu->data - pdu->token) : pdu->used_size;
2710 block.m = (lg_xmit->length - lg_xmit->offset) >
2711 ((out_pdu->max_size - token_options) /1024) * 1024;
2712 } else {
2713 block.m = (lg_xmit->offset + chunk) < lg_xmit->length;
2714 }
2715 if (!coap_update_option(out_pdu, lg_xmit->option,
2717 sizeof(buf),
2718 (block.num << 4) |
2719 (block.m << 3) |
2720 block.aszx),
2721 buf)) {
2722 goto internal_issue;
2723 }
2724 if (!(lg_xmit->offset + chunk < lg_xmit->length)) {
2725 /* Last block - keep in cache for 4 * ACK_TIMOUT */
2726 coap_ticks(&lg_xmit->last_all_sent);
2727 }
2728 if (lg_xmit->b.b2.maxage_expire) {
2729 coap_tick_t now;
2730 coap_time_t rem;
2731
2732 if (!(lg_xmit->offset + chunk < lg_xmit->length)) {
2733 /* Last block - keep in cache for 4 * ACK_TIMOUT */
2734 coap_ticks(&lg_xmit->last_all_sent);
2735 }
2736 coap_ticks(&now);
2737 rem = coap_ticks_to_rt(now);
2738 if (lg_xmit->b.b2.maxage_expire > rem) {
2739 rem = lg_xmit->b.b2.maxage_expire - rem;
2740 } else {
2741 rem = 0;
2742 /* Entry needs to be expired */
2743 coap_ticks(&lg_xmit->last_all_sent);
2744 }
2747 sizeof(buf),
2748 rem),
2749 buf)) {
2750 goto internal_issue;
2751 }
2752 }
2753
2754 if (!coap_add_block_b_data(out_pdu,
2755 lg_xmit->length,
2756 lg_xmit->data,
2757 &block)) {
2758 goto internal_issue;
2759 }
2760 if (i + 1 < request_cnt) {
2761 coap_ticks(&lg_xmit->last_sent);
2762 coap_send_internal(session, out_pdu);
2763 }
2764 }
2765 coap_ticks(&lg_xmit->last_payload);
2766 goto skip_app_handler;
2767#if COAP_Q_BLOCK_SUPPORT
2768call_app_handler:
2769 coap_free_type(COAP_STRING, out_blocks);
2770 return 0;
2771#endif /* COAP_Q_BLOCK_SUPPORT */
2772
2773internal_issue:
2774 response->code = COAP_RESPONSE_CODE(500);
2775 error_phrase = coap_response_phrase(response->code);
2776 coap_add_data(response, strlen(error_phrase),
2777 (const uint8_t *)error_phrase);
2778 /* Keep in cache for 4 * ACK_TIMOUT incase of retry */
2779 if (lg_xmit)
2780 coap_ticks(&lg_xmit->last_all_sent);
2781
2782skip_app_handler:
2783 coap_free_type(COAP_STRING, out_blocks);
2784 return 1;
2785}
2786#endif /* COAP_SERVER_SUPPORT */
2787
2788static int
2789update_received_blocks(coap_rblock_t *rec_blocks, uint32_t block_num, uint32_t block_m) {
2790 uint32_t i;
2791
2792 if (rec_blocks->total_blocks && block_num + 1 > rec_blocks->total_blocks) {
2793 /* received block number greater than Block No defined when More bit unset */
2794 return 0;
2795 }
2796
2797 /* Reset as there is activity */
2798 rec_blocks->retry = 0;
2799
2800 for (i = 0; i < rec_blocks->used; i++) {
2801 if (block_num >= rec_blocks->range[i].begin &&
2802 block_num <= rec_blocks->range[i].end)
2803 break;
2804
2805 if (block_num < rec_blocks->range[i].begin) {
2806 if (block_num + 1 == rec_blocks->range[i].begin) {
2807 rec_blocks->range[i].begin = block_num;
2808 } else {
2809 /* Need to insert a new range */
2810 if (rec_blocks->used == COAP_RBLOCK_CNT -1)
2811 /* Too many losses */
2812 return 0;
2813 memmove(&rec_blocks->range[i+1], &rec_blocks->range[i],
2814 (rec_blocks->used - i) * sizeof(rec_blocks->range[0]));
2815 rec_blocks->range[i].begin = rec_blocks->range[i].end = block_num;
2816 rec_blocks->used++;
2817 }
2818 break;
2819 }
2820 if (block_num == rec_blocks->range[i].end + 1) {
2821 rec_blocks->range[i].end = block_num;
2822 if (i + 1 < rec_blocks->used) {
2823 if (rec_blocks->range[i+1].begin == block_num + 1) {
2824 /* Merge the 2 ranges */
2825 rec_blocks->range[i].end = rec_blocks->range[i+1].end;
2826 if (i+2 < rec_blocks->used) {
2827 memmove(&rec_blocks->range[i+1], &rec_blocks->range[i+2],
2828 (rec_blocks->used - (i+2)) * sizeof(rec_blocks->range[0]));
2829 }
2830 rec_blocks->used--;
2831 }
2832 }
2833 break;
2834 }
2835 }
2836 if (i == rec_blocks->used) {
2837 if (rec_blocks->used == COAP_RBLOCK_CNT -1)
2838 /* Too many losses */
2839 return 0;
2840 rec_blocks->range[i].begin = rec_blocks->range[i].end = block_num;
2841 rec_blocks->used++;
2842 }
2843 if (!block_m)
2844 rec_blocks->total_blocks = block_num + 1;
2845
2846 coap_ticks(&rec_blocks->last_seen);
2847 return 1;
2848}
2849
2850#if COAP_SERVER_SUPPORT
2851/*
2852 * Need to check if this is a large PUT / POST etc. using multiple blocks
2853 *
2854 * Server receiving PUT/POST etc. of a large amount of data (Block1)
2855 *
2856 * Return: 0 Call application handler
2857 * 1 Do not call application handler - just send the built response
2858 */
2859int
2861 coap_session_t *session,
2862 coap_pdu_t *pdu,
2863 coap_pdu_t *response,
2864 coap_resource_t *resource,
2865 coap_string_t *uri_path,
2866 coap_opt_t *observe,
2867 int *added_block,
2868 coap_lg_srcv_t **pfree_lg_srcv) {
2869 size_t length = 0;
2870 const uint8_t *data = NULL;
2871 size_t offset = 0;
2872 size_t total = 0;
2873 coap_block_b_t block;
2874 coap_opt_iterator_t opt_iter;
2875 uint16_t block_option = 0;
2876 coap_lg_srcv_t *lg_srcv;
2877 coap_opt_t *size_opt;
2878 coap_opt_t *fmt_opt;
2879 uint16_t fmt;
2880 coap_opt_t *rtag_opt;
2881 size_t rtag_length;
2882 const uint8_t *rtag;
2883 uint32_t max_block_szx;
2884 int update_data;
2885 unsigned int saved_num;
2886 size_t saved_offset;
2887
2888 *added_block = 0;
2889 *pfree_lg_srcv = NULL;
2890 coap_get_data_large(pdu, &length, &data, &offset, &total);
2891 pdu->body_offset = 0;
2892 pdu->body_total = length;
2893
2894 if (coap_get_block_b(session, pdu, COAP_OPTION_BLOCK1, &block)) {
2895 block_option = COAP_OPTION_BLOCK1;
2896#if COAP_Q_BLOCK_SUPPORT
2897 if (coap_check_option(pdu, COAP_OPTION_Q_BLOCK1, &opt_iter)) {
2898 /* Cannot handle Q-Block1 as well */
2899 coap_add_data(response, sizeof("Block1 + Q-Block1 together")-1,
2900 (const uint8_t *)"Block1 + Q-Block1 together");
2901 response->code = COAP_RESPONSE_CODE(402);
2902 goto skip_app_handler;
2903 }
2904#endif /* COAP_Q_BLOCK_SUPPORT */
2905 }
2906#if COAP_Q_BLOCK_SUPPORT
2907 else if (coap_get_block_b(session, pdu, COAP_OPTION_Q_BLOCK1, &block)) {
2908 block_option = COAP_OPTION_Q_BLOCK1;
2909 set_block_mode_has_q(session->block_mode);
2910 }
2911#endif /* COAP_Q_BLOCK_SUPPORT */
2912 if (!block_option ||
2913 (block_option == COAP_OPTION_BLOCK1 && block.num == 0 && block.m == 0)) {
2914 /* Not blocked, or a single block */
2915 goto call_app_handler;
2916 }
2917
2918 size_opt = coap_check_option(pdu,
2920 &opt_iter);
2921 fmt_opt = coap_check_option(pdu,
2923 &opt_iter);
2924 fmt = fmt_opt ? coap_decode_var_bytes(coap_opt_value(fmt_opt),
2925 coap_opt_length(fmt_opt)) :
2927 rtag_opt = coap_check_option(pdu,
2929 &opt_iter);
2930 rtag_length = rtag_opt ? coap_opt_length(rtag_opt) : 0;
2931 rtag = rtag_opt ? coap_opt_value(rtag_opt) : NULL;
2932
2933 if (length > block.chunk_size) {
2934 coap_log_debug("block: Oversized packet - reduced to %"PRIu32" from %zu\n",
2935 block.chunk_size, length);
2936 length = block.chunk_size;
2937 } else if (!block.bert && block.m && length != block.chunk_size) {
2938 coap_log_info("block: Undersized packet chunk %"PRIu32" got %zu\n",
2939 block.chunk_size, length);
2940 response->code = COAP_RESPONSE_CODE(400);
2941 goto skip_app_handler;
2942 }
2943 total = size_opt ? coap_decode_var_bytes(coap_opt_value(size_opt),
2944 coap_opt_length(size_opt)) : 0;
2945 offset = block.num << (block.szx + 4);
2946
2947 if (!(session->block_mode &
2948#if COAP_Q_BLOCK_SUPPORT
2950#else /* COAP_Q_BLOCK_SUPPORT */
2952#endif /* COAP_Q_BLOCK_SUPPORT */
2953 && !block.bert) {
2954 uint8_t buf[4];
2955
2956 /* Ask for the next block */
2957 coap_insert_option(response, block_option,
2958 coap_encode_var_safe(buf, sizeof(buf),
2959 (block.num << 4) |
2960 (block.m << 3) |
2961 block.aszx),
2962 buf);
2963 /* Not re-assembling or checking for receipt order */
2964 pdu->body_data = data;
2965 pdu->body_length = length;
2966 pdu->body_offset = offset;
2967 if (total < (length + offset + (block.m ? 1 : 0)))
2968 total = length + offset + (block.m ? 1 : 0);
2969 pdu->body_total = total;
2970 *added_block = block.m;
2971 /* The application is responsible for returning the correct 2.01/2.04/2.31 etc. */
2972 goto call_app_handler;
2973 }
2974
2975 /*
2976 * locate the lg_srcv
2977 */
2978 LL_FOREACH(session->lg_srcv, lg_srcv) {
2979 if (rtag_opt || lg_srcv->rtag_set == 1) {
2980 if (!(rtag_opt && lg_srcv->rtag_set == 1))
2981 continue;
2982 if (lg_srcv->rtag_length != rtag_length ||
2983 memcmp(lg_srcv->rtag, rtag, rtag_length) != 0)
2984 continue;
2985 }
2986 if (resource == lg_srcv->resource) {
2987 break;
2988 }
2989 if ((lg_srcv->resource == context->unknown_resource ||
2990 resource == context->proxy_uri_resource) &&
2991 coap_string_equal(uri_path, lg_srcv->uri_path))
2992 break;
2993 }
2994
2995 if (!lg_srcv && block.num != 0 && session->block_mode & COAP_BLOCK_NOT_RANDOM_BLOCK1) {
2996 coap_add_data(response, sizeof("Missing block 0")-1,
2997 (const uint8_t *)"Missing block 0");
2998 response->code = COAP_RESPONSE_CODE(408);
2999 goto skip_app_handler;
3000 }
3001
3002 if (!lg_srcv) {
3003 /* Allocate lg_srcv to use for tracking */
3004 lg_srcv = coap_malloc_type(COAP_LG_SRCV, sizeof(coap_lg_srcv_t));
3005 if (lg_srcv == NULL) {
3006 coap_add_data(response, sizeof("Memory issue")-1,
3007 (const uint8_t *)"Memory issue");
3008 response->code = COAP_RESPONSE_CODE(500);
3009 goto skip_app_handler;
3010 }
3011 coap_log_debug("** %s: lg_srcv %p initialized\n",
3012 coap_session_str(session), (void *)lg_srcv);
3013 memset(lg_srcv, 0, sizeof(coap_lg_srcv_t));
3014 coap_ticks(&lg_srcv->last_used);
3015 lg_srcv->resource = resource;
3016 if (resource == context->unknown_resource ||
3017 resource == context->proxy_uri_resource)
3018 lg_srcv->uri_path = coap_new_str_const(uri_path->s, uri_path->length);
3019 lg_srcv->content_format = fmt;
3020 lg_srcv->total_len = total;
3021 max_block_szx = COAP_BLOCK_MAX_SIZE_GET(session->block_mode);
3022 if (!block.bert && block.num == 0 && max_block_szx != 0 &&
3023 max_block_szx < block.szx) {
3024 lg_srcv->szx = max_block_szx;
3025 } else {
3026 lg_srcv->szx = block.szx;
3027 }
3028 lg_srcv->block_option = block_option;
3029 if (observe) {
3030 lg_srcv->observe_length = min(coap_opt_length(observe), 3);
3031 memcpy(lg_srcv->observe, coap_opt_value(observe), lg_srcv->observe_length);
3032 lg_srcv->observe_set = 1;
3033 }
3034 if (rtag_opt) {
3035 lg_srcv->rtag_length = coap_opt_length(rtag_opt);
3036 memcpy(lg_srcv->rtag, coap_opt_value(rtag_opt), lg_srcv->rtag_length);
3037 lg_srcv->rtag_set = 1;
3038 }
3039 lg_srcv->body_data = NULL;
3040 LL_PREPEND(session->lg_srcv, lg_srcv);
3041 }
3042
3043 if (block_option == COAP_OPTION_BLOCK1 &&
3045 !check_if_next_block(&lg_srcv->rec_blocks, block.num)) {
3046 coap_add_data(response, sizeof("Missing interim block")-1,
3047 (const uint8_t *)"Missing interim block");
3048 response->code = COAP_RESPONSE_CODE(408);
3049 goto skip_app_handler;
3050 }
3051
3052 if (fmt != lg_srcv->content_format) {
3053 coap_add_data(response, sizeof("Content-Format mismatch")-1,
3054 (const uint8_t *)"Content-Format mismatch");
3055 response->code = COAP_RESPONSE_CODE(408);
3056 goto free_lg_srcv;
3057 }
3058
3059#if COAP_Q_BLOCK_SUPPORT
3060 if (block_option == COAP_OPTION_Q_BLOCK1) {
3061 if (total != lg_srcv->total_len) {
3062 coap_add_data(response, sizeof("Size1 mismatch")-1,
3063 (const uint8_t *)"Size1 mismatch");
3064 response->code = COAP_RESPONSE_CODE(408);
3065 goto free_lg_srcv;
3066 }
3069 pdu->actual_token.length);
3070 }
3071#endif /* COAP_Q_BLOCK_SUPPORT */
3072
3073 lg_srcv->last_mid = pdu->mid;
3074 lg_srcv->last_type = pdu->type;
3075
3076 update_data = 0;
3077 saved_num = block.num;
3078 saved_offset = offset;
3079
3080 while (offset < saved_offset + length) {
3081 if (!check_if_received_block(&lg_srcv->rec_blocks, block.num)) {
3082 /* Update list of blocks received */
3083 if (!update_received_blocks(&lg_srcv->rec_blocks, block.num, block.m)) {
3085 coap_add_data(response, sizeof("Too many missing blocks")-1,
3086 (const uint8_t *)"Too many missing blocks");
3087 response->code = COAP_RESPONSE_CODE(408);
3088 goto free_lg_srcv;
3089 }
3090 update_data = 1;
3091 }
3092 block.num++;
3093 offset = block.num << (block.szx + 4);
3094 }
3095 block.num--;
3096
3097 if (update_data) {
3098 /* Update saved data */
3099#if COAP_Q_BLOCK_SUPPORT
3100 lg_srcv->rec_blocks.processing_payload_set =
3101 block.num / COAP_MAX_PAYLOADS(session);
3102#endif /* COAP_Q_BLOCK_SUPPORT */
3103 if (lg_srcv->total_len < saved_offset + length) {
3104 lg_srcv->total_len = saved_offset + length;
3105 }
3106 lg_srcv->body_data = coap_block_build_body(lg_srcv->body_data, length, data,
3107 saved_offset, lg_srcv->total_len);
3108 if (!lg_srcv->body_data) {
3109 coap_add_data(response, sizeof("Memory issue")-1,
3110 (const uint8_t *)"Memory issue");
3111 response->code = COAP_RESPONSE_CODE(500);
3112 goto skip_app_handler;
3113 }
3114 }
3115
3116 if (block.m ||
3117 !check_all_blocks_in(&lg_srcv->rec_blocks)) {
3118 /* Not all the payloads of the body have arrived */
3119 if (block.m) {
3120 uint8_t buf[4];
3121
3122#if COAP_Q_BLOCK_SUPPORT
3123 if (block_option == COAP_OPTION_Q_BLOCK1) {
3124 if (check_all_blocks_in(&lg_srcv->rec_blocks)) {
3125 goto give_app_data;
3126 }
3127 if (lg_srcv->rec_blocks.used == 1 &&
3128 (lg_srcv->rec_blocks.range[0].end % COAP_MAX_PAYLOADS(session)) + 1
3129 == COAP_MAX_PAYLOADS(session)) {
3130 /* Blocks could arrive in wrong order */
3131 block.num = lg_srcv->rec_blocks.range[0].end;
3132 } else {
3133 /* The remote end will be sending the next one unless this
3134 is a MAX_PAYLOADS and all previous have been received */
3135 goto skip_app_handler;
3136 }
3137 if (COAP_PROTO_RELIABLE(session->proto) ||
3138 pdu->type != COAP_MESSAGE_NON)
3139 goto skip_app_handler;
3140 }
3141#endif /* COAP_Q_BLOCK_SUPPORT */
3142
3143 /* Check to see if block size is getting forced down */
3144 max_block_szx = COAP_BLOCK_MAX_SIZE_GET(session->block_mode);
3145 if (!block.bert && saved_num == 0 && max_block_szx != 0 &&
3146 max_block_szx < block.aszx) {
3147 block.aszx = max_block_szx;
3148 }
3149
3150 /*
3151 * If the last block has been seen, packets are coming in in
3152 * random order. If all blocks are now in, then need to send
3153 * complete payload to application and acknowledge this current
3154 * block.
3155 */
3156 if ((total == 0 && block.m) || !check_all_blocks_in(&lg_srcv->rec_blocks)) {
3157 /* Ask for the next block */
3158 coap_insert_option(response, block_option,
3159 coap_encode_var_safe(buf, sizeof(buf),
3160 (saved_num << 4) |
3161 (block.m << 3) |
3162 block.aszx),
3163 buf);
3164 response->code = COAP_RESPONSE_CODE(231);
3165 } else {
3166 /* Need to separately respond to this request */
3167 coap_pdu_t *tmp_pdu = coap_pdu_duplicate_lkd(response,
3168 session,
3169 response->actual_token.length,
3170 response->actual_token.s,
3171 NULL);
3172 if (tmp_pdu) {
3173 tmp_pdu->code = COAP_RESPONSE_CODE(231);
3174 coap_send_internal(session, tmp_pdu);
3175 }
3176 if (lg_srcv->last_token) {
3177 coap_update_token(response, lg_srcv->last_token->length, lg_srcv->last_token->s);
3178 coap_update_token(pdu, lg_srcv->last_token->length, lg_srcv->last_token->s);
3179 }
3180 /* Pass the assembled pdu and body to the application */
3181 goto give_app_data;
3182 }
3183 } else {
3184 /* block.m Block More option not set. Some outstanding blocks */
3185#if COAP_Q_BLOCK_SUPPORT
3186 if (block_option != COAP_OPTION_Q_BLOCK1) {
3187#endif /* COAP_Q_BLOCK_SUPPORT */
3188 /* Last chunk - but not all in */
3189 coap_ticks(&lg_srcv->last_used);
3190 lg_srcv->no_more_seen = 1;
3193 pdu->actual_token.length);
3194
3195 /*
3196 * Need to just ACK (no response code) to handle client's NSTART.
3197 * When final missing block comes in, we will pass all the data
3198 * for processing so a 2.01, 2.04 etc. code can be generated
3199 * and responded to as a separate response "RFC7252 5.2.2. Separate"
3200 * If missing block(s) do not come in, then will generate a 4.08
3201 * when lg_srcv times out.
3202 * Fall through to skip_app_handler.
3203 */
3204#if COAP_Q_BLOCK_SUPPORT
3205 }
3206#endif /* COAP_Q_BLOCK_SUPPORT */
3207 }
3208 goto skip_app_handler;
3209 }
3210
3211 /*
3212 * Entire payload received.
3213 * Remove the Block1 option as passing all of the data to
3214 * application layer. Add back in observe option if appropriate.
3215 * Adjust all other information.
3216 */
3217give_app_data:
3218 if (lg_srcv->observe_set) {
3220 lg_srcv->observe_length, lg_srcv->observe);
3221 }
3222 coap_remove_option(pdu, block_option);
3223 if (lg_srcv->body_data) {
3224 pdu->body_data = lg_srcv->body_data->s;
3225 pdu->body_length = lg_srcv->total_len;
3226 } else {
3227 pdu->body_data = NULL;
3228 pdu->body_length = 0;
3229 }
3230 pdu->body_offset = 0;
3231 pdu->body_total = lg_srcv->total_len;
3232 coap_log_debug("Server app version of updated PDU\n");
3234 *pfree_lg_srcv = lg_srcv;
3235
3236call_app_handler:
3237 return 0;
3238
3239free_lg_srcv:
3240 LL_DELETE(session->lg_srcv, lg_srcv);
3241 coap_block_delete_lg_srcv(session, lg_srcv);
3242
3243skip_app_handler:
3244 return 1;
3245}
3246#endif /* COAP_SERVER_SUPPORT */
3247
3248#if COAP_CLIENT_SUPPORT
3249#if COAP_Q_BLOCK_SUPPORT
3250static uint32_t
3251derive_cbor_value(const uint8_t **bp, size_t rem_len) {
3252 uint32_t value = **bp & 0x1f;
3253 (*bp)++;
3254 if (value < 24) {
3255 return value;
3256 } else if (value == 24) {
3257 if (rem_len < 2)
3258 return (uint32_t)-1;
3259 value = **bp;
3260 (*bp)++;
3261 return value;
3262 } else if (value == 25) {
3263 if (rem_len < 3)
3264 return (uint32_t)-1;
3265 value = **bp << 8;
3266 (*bp)++;
3267 value |= **bp;
3268 (*bp)++;
3269 return value;
3270 }
3271 if (rem_len < 4)
3272 return (uint32_t)-1;
3273 value = **bp << 24;
3274 (*bp)++;
3275 value |= **bp << 16;
3276 (*bp)++;
3277 value |= **bp << 8;
3278 (*bp)++;
3279 value |= **bp;
3280 (*bp)++;
3281 return value;
3282}
3283#endif /* COAP_Q_BLOCK_SUPPORT */
3284
3285static int
3286check_freshness(coap_session_t *session, coap_pdu_t *rcvd, coap_pdu_t *sent,
3287 coap_lg_xmit_t *lg_xmit, coap_lg_crcv_t *lg_crcv) {
3288 /* Check for Echo option for freshness */
3289 coap_opt_iterator_t opt_iter;
3290 coap_opt_t *opt = coap_check_option(rcvd, COAP_OPTION_ECHO, &opt_iter);
3291
3292 if (opt) {
3293 if (sent || lg_xmit || lg_crcv) {
3294 /* Need to retransmit original request with Echo option added */
3295 coap_pdu_t *echo_pdu;
3296 coap_mid_t mid;
3297 const uint8_t *data;
3298 size_t data_len;
3299 int have_data = 0;
3300 uint8_t ltoken[8];
3301 size_t ltoken_len;
3302 uint64_t token;
3303
3304 if (sent) {
3305 if (coap_get_data(sent, &data_len, &data))
3306 have_data = 1;
3307 } else if (lg_xmit) {
3308 sent = &lg_xmit->pdu;
3309 if (lg_xmit->length) {
3310 size_t blk_size = (size_t)1 << (lg_xmit->blk_size + 4);
3311 size_t offset = (lg_xmit->last_block + 1) * blk_size;
3312 have_data = 1;
3313 data = &lg_xmit->data[offset];
3314 data_len = (lg_xmit->length - offset) > blk_size ? blk_size :
3315 lg_xmit->length - offset;
3316 }
3317 } else { /* lg_crcv */
3318 sent = &lg_crcv->pdu;
3319 if (coap_get_data(sent, &data_len, &data))
3320 have_data = 1;
3321 }
3322 if (lg_xmit) {
3323 token = STATE_TOKEN_FULL(lg_xmit->b.b1.state_token,
3324 ++lg_xmit->b.b1.count);
3325 } else {
3326 token = STATE_TOKEN_FULL(lg_crcv->state_token,
3327 ++lg_crcv->retry_counter);
3328 }
3329 ltoken_len = coap_encode_var_safe8(ltoken, sizeof(token), token);
3330 echo_pdu = coap_pdu_duplicate_lkd(sent, session, ltoken_len, ltoken, NULL);
3331 if (!echo_pdu)
3332 return 0;
3333 if (!coap_insert_option(echo_pdu, COAP_OPTION_ECHO,
3334 coap_opt_length(opt), coap_opt_value(opt)))
3335 goto not_sent;
3336 if (have_data) {
3337 coap_add_data(echo_pdu, data_len, data);
3338 }
3339 /* Need to track Observe token change if Observe */
3340 track_fetch_observe(echo_pdu, lg_crcv, 0, &echo_pdu->actual_token);
3341#if COAP_OSCORE_SUPPORT
3342 if (session->oscore_encryption &&
3343 (opt = coap_check_option(echo_pdu, COAP_OPTION_OBSERVE, &opt_iter)) &&
3345 /* Need to update the base PDU's Token for closing down Observe */
3346 if (lg_xmit) {
3347 lg_xmit->b.b1.state_token = token;
3348 } else {
3349 lg_crcv->state_token = token;
3350 }
3351 }
3352#endif /* COAP_OSCORE_SUPPORT */
3353 mid = coap_send_internal(session, echo_pdu);
3354 if (mid == COAP_INVALID_MID)
3355 goto not_sent;
3356 return 1;
3357 } else {
3358 /* Need to save Echo option value to add to next reansmission */
3359not_sent:
3360 coap_delete_bin_const(session->echo);
3361 session->echo = coap_new_bin_const(coap_opt_value(opt),
3362 coap_opt_length(opt));
3363 }
3364 }
3365 return 0;
3366}
3367
3368static void
3369track_echo(coap_session_t *session, coap_pdu_t *rcvd) {
3370 coap_opt_iterator_t opt_iter;
3371 coap_opt_t *opt = coap_check_option(rcvd, COAP_OPTION_ECHO, &opt_iter);
3372
3373 if (opt) {
3374 coap_delete_bin_const(session->echo);
3375 session->echo = coap_new_bin_const(coap_opt_value(opt),
3376 coap_opt_length(opt));
3377 }
3378}
3379
3380/*
3381 * Need to see if this is a response to a large body request transfer. If so,
3382 * need to initiate the request containing the next block and not trouble the
3383 * application. Note that Token must unique per request/response.
3384 *
3385 * Client receives large data acknowledgement from server (Block1)
3386 *
3387 * This is set up using coap_add_data_large_request_lkd()
3388 *
3389 * Client is using GET etc.
3390 *
3391 * Return: 0 Call application handler
3392 * 1 Do not call application handler - just send the built response
3393 */
3394int
3396 coap_pdu_t *rcvd) {
3397 coap_lg_xmit_t *lg_xmit;
3398 coap_lg_xmit_t *q;
3399 uint64_t token_match =
3401 rcvd->actual_token.length));
3402 coap_lg_crcv_t *lg_crcv = NULL;
3403
3404 LL_FOREACH_SAFE(session->lg_xmit, lg_xmit, q) {
3405 if (!COAP_PDU_IS_REQUEST(&lg_xmit->pdu) ||
3406 (token_match != STATE_TOKEN_BASE(lg_xmit->b.b1.state_token) &&
3407 !coap_binary_equal(&rcvd->actual_token, lg_xmit->b.b1.app_token))) {
3408 /* try out the next one */
3409 continue;
3410 }
3411 /* lg_xmit found */
3412 size_t chunk = (size_t)1 << (lg_xmit->blk_size + 4);
3413 coap_block_b_t block;
3414
3415 lg_crcv = lg_xmit->lg_crcv;
3416 if (lg_crcv)
3417 coap_ticks(&lg_crcv->last_used);
3418
3419 if (COAP_RESPONSE_CLASS(rcvd->code) == 2 &&
3420 coap_get_block_b(session, rcvd, lg_xmit->option, &block)) {
3421
3422 if (block.bert) {
3423 coap_log_debug("found Block option, block is BERT, block nr. %u (%zu)\n",
3424 block.num, lg_xmit->b.b1.bert_size);
3425 } else {
3426 coap_log_debug("found Block option, block size is %u, block nr. %u\n",
3427 1 << (block.szx + 4), block.num);
3428 }
3429 if (block.szx != lg_xmit->blk_size) {
3430 if (block.szx > lg_xmit->blk_size) {
3431 coap_log_info("ignoring request to increase Block size, "
3432 "(%u > %u)\n",
3433 1 << (block.szx + 4), 1 << (lg_xmit->blk_size + 4));
3434 } else if ((lg_xmit->offset + chunk) % ((size_t)1 << (block.szx + 4)) == 0) {
3435 /*
3436 * Recompute the block number of the previous packet given the
3437 * new block size
3438 */
3439 block.num = (uint32_t)(((lg_xmit->offset + chunk) >> (block.szx + 4)) - 1);
3440 lg_xmit->blk_size = block.szx;
3441 chunk = (size_t)1 << (lg_xmit->blk_size + 4);
3442 lg_xmit->offset = block.num * chunk;
3443 coap_log_debug("new Block size is %u, block number %u completed\n",
3444 1 << (block.szx + 4), block.num);
3445 block.bert = 0;
3446 block.aszx = block.szx;
3447 } else {
3448 coap_log_debug("ignoring request to increase Block size, "
3449 "next block is not aligned on requested block size boundary. "
3450 "(%zu x %u mod %u = %zu != 0)\n",
3451 lg_xmit->offset/chunk + 1, (1 << (lg_xmit->blk_size + 4)),
3452 (1 << (block.szx + 4)),
3453 (lg_xmit->offset + chunk) % ((size_t)1 << (block.szx + 4)));
3454 }
3455 }
3456 track_echo(session, rcvd);
3457 if (lg_xmit->last_block == (int)block.num &&
3458 lg_xmit->option != COAP_OPTION_Q_BLOCK1) {
3459 /*
3460 * Duplicate Block1 ACK
3461 *
3462 * RFCs not clear here, but on a lossy connection, there could
3463 * be multiple Block1 ACKs, causing the client to retransmit the
3464 * same block multiple times, or the server retransmitting the
3465 * same ACK.
3466 *
3467 * Once a block has been ACKd, there is no need to retransmit it.
3468 */
3469 return 1;
3470 }
3471 if (block.bert)
3472 block.num += (unsigned int)(lg_xmit->b.b1.bert_size / 1024 - 1);
3473 lg_xmit->last_block = block.num;
3474 lg_xmit->offset = (block.num + 1) * chunk;
3475 if (lg_xmit->offset < lg_xmit->length) {
3476 /* Build the next PDU request based off the skeletal PDU */
3477 uint8_t buf[8];
3478 coap_pdu_t *pdu;
3479 uint64_t token = STATE_TOKEN_FULL(lg_xmit->b.b1.state_token, ++lg_xmit->b.b1.count);
3480 size_t len = coap_encode_var_safe8(buf, sizeof(token), token);
3481
3482 if (lg_xmit->pdu.code == COAP_REQUEST_CODE_FETCH) {
3483 /* Need to handle Observe for large FETCH */
3484 if (lg_crcv) {
3485 if (coap_binary_equal(lg_xmit->b.b1.app_token, lg_crcv->app_token)) {
3486 coap_bin_const_t *new_token;
3487 coap_bin_const_t ctoken = { len, buf };
3488
3489 /* Need to save/restore Observe Token for large FETCH */
3490 new_token = track_fetch_observe(&lg_xmit->pdu, lg_crcv, block.num + 1,
3491 &ctoken);
3492 if (new_token) {
3493 assert(len <= sizeof(buf));
3494 len = new_token->length;
3495 memcpy(buf, new_token->s, len);
3496 }
3497 }
3498 }
3499 }
3500 pdu = coap_pdu_duplicate_lkd(&lg_xmit->pdu, session, len, buf, NULL);
3501 if (!pdu)
3502 goto fail_body;
3503
3504 block.num++;
3505 if (block.bert) {
3506 size_t token_options = pdu->data ? (size_t)(pdu->data - pdu->token) :
3507 pdu->used_size;
3508 block.m = (lg_xmit->length - lg_xmit->offset) >
3509 ((pdu->max_size - token_options) /1024) * 1024;
3510 } else {
3511 block.m = (lg_xmit->offset + chunk) < lg_xmit->length;
3512 }
3513 coap_update_option(pdu, lg_xmit->option,
3514 coap_encode_var_safe(buf, sizeof(buf),
3515 (block.num << 4) |
3516 (block.m << 3) |
3517 block.aszx),
3518 buf);
3519
3520 if (!coap_add_block_b_data(pdu,
3521 lg_xmit->length,
3522 lg_xmit->data,
3523 &block))
3524 goto fail_body;
3525 lg_xmit->b.b1.bert_size = block.chunk_size;
3526 coap_ticks(&lg_xmit->last_sent);
3527#if COAP_Q_BLOCK_SUPPORT
3528 if (lg_xmit->option == COAP_OPTION_Q_BLOCK1 &&
3529 pdu->type == COAP_MESSAGE_NON) {
3530 if (coap_send_q_block1(session, block, pdu,
3531 COAP_SEND_INC_PDU) == COAP_INVALID_MID)
3532 goto fail_body;
3533 return 1;
3534 } else if (coap_send_internal(session, pdu) == COAP_INVALID_MID)
3535 goto fail_body;
3536#else /* ! COAP_Q_BLOCK_SUPPORT */
3537 if (coap_send_internal(session, pdu) == COAP_INVALID_MID)
3538 goto fail_body;
3539#endif /* ! COAP_Q_BLOCK_SUPPORT */
3540 return 1;
3541 }
3542 } else if (COAP_RESPONSE_CLASS(rcvd->code) == 2) {
3543 /*
3544 * Not a block response asking for the next block.
3545 * Could be an Observe response overlapping with block FETCH doing
3546 * Observe cancellation.
3547 */
3548 coap_opt_iterator_t opt_iter;
3549 coap_opt_t *obs_opt;
3550 int observe_action = -1;
3551
3552 if (lg_xmit->pdu.code != COAP_REQUEST_CODE_FETCH) {
3553 goto lg_xmit_finished;
3554 }
3555 obs_opt = coap_check_option(&lg_xmit->pdu,
3557 &opt_iter);
3558 if (obs_opt) {
3559 observe_action = coap_decode_var_bytes(coap_opt_value(obs_opt),
3560 coap_opt_length(obs_opt));
3561 }
3562 if (observe_action != COAP_OBSERVE_CANCEL) {
3563 goto lg_xmit_finished;
3564 }
3565 obs_opt = coap_check_option(rcvd,
3567 &opt_iter);
3568 if (obs_opt) {
3569 return 0;
3570 }
3571 goto lg_xmit_finished;
3572 } else if (rcvd->code == COAP_RESPONSE_CODE(401)) {
3573 if (check_freshness(session, rcvd, sent, lg_xmit, NULL))
3574 return 1;
3575#if COAP_Q_BLOCK_SUPPORT
3576 } else if (rcvd->code == COAP_RESPONSE_CODE(402)) {
3577 /* Q-Block1 or Q-Block2 not present in p - duplicate error ? */
3578 if (coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK2, &block) ||
3579 coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK1, &block))
3580 return 1;
3581 } else if (rcvd->code == COAP_RESPONSE_CODE(408) &&
3582 lg_xmit->option == COAP_OPTION_Q_BLOCK1) {
3583 size_t length;
3584 const uint8_t *data;
3585 coap_opt_iterator_t opt_iter;
3586 coap_opt_t *fmt_opt = coap_check_option(rcvd,
3588 &opt_iter);
3589 uint16_t fmt = fmt_opt ?
3591 coap_opt_length(fmt_opt)) :
3593
3595 goto fail_body;
3596
3597 if (COAP_PROTO_RELIABLE(session->proto) ||
3598 rcvd->type != COAP_MESSAGE_NON) {
3599 coap_log_debug("Unexpected 4.08 - protocol violation - ignore\n");
3600 return 1;
3601 }
3602
3603 if (coap_get_data(rcvd, &length, &data)) {
3604 /* Need to decode CBOR to work out what blocks to re-send */
3605 const uint8_t *bp = data;
3606 uint32_t i;
3607 uint8_t buf[8];
3608 coap_pdu_t *pdu;
3609 uint64_t token;
3610 uint8_t ltoken[8];
3611 size_t ltoken_length;
3612
3613 for (i = 0; (bp < data + length) &&
3614 i < COAP_MAX_PAYLOADS(session); i++) {
3615 if ((*bp & 0xc0) != 0x00) /* uint(value) */
3616 goto fail_cbor;
3617 block.num = derive_cbor_value(&bp, data + length - bp);
3618 coap_log_debug("Q-Block1: Missing block %d\n", block.num);
3619 if (block.num > (1 << 20) -1)
3620 goto fail_cbor;
3621 block.m = (block.num + 1) * chunk < lg_xmit->length;
3622 block.szx = lg_xmit->blk_size;
3623
3624 /* Build the next PDU request based off the skeletal PDU */
3625 token = STATE_TOKEN_FULL(lg_xmit->b.b1.state_token,++lg_xmit->b.b1.count);
3626 ltoken_length = coap_encode_var_safe8(ltoken, sizeof(token), token);
3627 pdu = coap_pdu_duplicate_lkd(&lg_xmit->pdu, session, ltoken_length,
3628 ltoken, NULL);
3629 if (!pdu)
3630 goto fail_body;
3631
3632 coap_update_option(pdu, lg_xmit->option,
3633 coap_encode_var_safe(buf, sizeof(buf),
3634 (block.num << 4) |
3635 (block.m << 3) |
3636 block.szx),
3637 buf);
3638
3639 if (!coap_add_block(pdu,
3640 lg_xmit->length,
3641 lg_xmit->data,
3642 block.num,
3643 block.szx))
3644 goto fail_body;
3645 if (coap_send_internal(session, pdu) == COAP_INVALID_MID)
3646 goto fail_body;
3647 }
3648 return 1;
3649 }
3650fail_cbor:
3651 coap_log_info("Invalid application/missing-blocks+cbor-seq\n");
3652#endif /* COAP_Q_BLOCK_SUPPORT */
3653 }
3654 goto lg_xmit_finished;
3655 } /* end of LL_FOREACH_SAFE */
3656 return 0;
3657
3658fail_body:
3660 /* There has been an internal error of some sort */
3661 rcvd->code = COAP_RESPONSE_CODE(500);
3662lg_xmit_finished:
3663 if (lg_crcv) {
3664 if (STATE_TOKEN_BASE(lg_xmit->b.b1.state_token) ==
3665 STATE_TOKEN_BASE(lg_crcv->state_token)) {
3666 /* In case of observe */
3667 lg_crcv->state_token = lg_xmit->b.b1.state_token;
3668 lg_crcv->retry_counter = lg_xmit->b.b1.count;
3669 }
3670 }
3671 if (!lg_crcv) {
3672 /* need to put back original token into rcvd */
3673 if (lg_xmit->b.b1.app_token)
3674 coap_update_token(rcvd, lg_xmit->b.b1.app_token->length,
3675 lg_xmit->b.b1.app_token->s);
3676 coap_log_debug("Client app version of updated PDU\n");
3678 } else {
3679 lg_crcv->pdu.lg_xmit = 0;
3680 }
3681
3682 if (sent) {
3683 /* need to put back original token into sent */
3684 if (lg_xmit->b.b1.app_token)
3685 coap_update_token(sent, lg_xmit->b.b1.app_token->length,
3686 lg_xmit->b.b1.app_token->s);
3687 if (sent->lg_xmit)
3688 coap_remove_option(sent, sent->lg_xmit->option);
3689 sent->lg_xmit = NULL;
3690 }
3691 LL_DELETE(session->lg_xmit, lg_xmit);
3692 coap_block_delete_lg_xmit(session, lg_xmit);
3693 return 0;
3694}
3695#endif /* COAP_CLIENT_SUPPORT */
3696
3697/*
3698 * Re-assemble payloads into a body
3699 */
3701coap_block_build_body(coap_binary_t *body_data, size_t length,
3702 const uint8_t *data, size_t offset, size_t total) {
3703 if (data == NULL)
3704 return NULL;
3705 if (body_data == NULL && total) {
3706 body_data = coap_new_binary(total);
3707 }
3708 if (body_data == NULL)
3709 return NULL;
3710
3711 /* Update saved data */
3712 if (offset + length <= total && body_data->length >= total) {
3713 memcpy(&body_data->s[offset], data, length);
3714 } else {
3715 /*
3716 * total may be inaccurate as per
3717 * https://rfc-editor.org/rfc/rfc7959#section-4
3718 * o In a request carrying a Block1 Option, to indicate the current
3719 * estimate the client has of the total size of the resource
3720 * representation, measured in bytes ("size indication").
3721 * o In a response carrying a Block2 Option, to indicate the current
3722 * estimate the server has of the total size of the resource
3723 * representation, measured in bytes ("size indication").
3724 */
3725 coap_binary_t *new = coap_resize_binary(body_data, offset + length);
3726
3727 if (new) {
3728 body_data = new;
3729 memcpy(&body_data->s[offset], data, length);
3730 } else {
3731 coap_delete_binary(body_data);
3732 return NULL;
3733 }
3734 }
3735 return body_data;
3736}
3737
3738#if COAP_CLIENT_SUPPORT
3739/*
3740 * Need to see if this is a large body response to a request. If so,
3741 * need to initiate the request for the next block and not trouble the
3742 * application. Note that Token must be unique per request/response.
3743 *
3744 * This is set up using coap_send()
3745 * Client receives large data from server ((Q-)Block2)
3746 *
3747 * Return: 0 Call application handler
3748 * 1 Do not call application handler - just sent the next request
3749 */
3750int
3752 coap_session_t *session,
3753 coap_pdu_t *sent,
3754 coap_pdu_t *rcvd,
3755 coap_recurse_t recursive) {
3756 coap_lg_crcv_t *lg_crcv;
3757 coap_block_b_t block;
3758#if COAP_Q_BLOCK_SUPPORT
3759 coap_block_b_t qblock;
3760#endif /* COAP_Q_BLOCK_SUPPORT */
3761 int have_block = 0;
3762 uint16_t block_opt = 0;
3763 size_t offset;
3764 int ack_rst_sent = 0;
3765 uint64_t token_match =
3767 rcvd->actual_token.length));
3768
3769 coap_lock_check_locked(context);
3770 memset(&block, 0, sizeof(block));
3771#if COAP_Q_BLOCK_SUPPORT
3772 memset(&qblock, 0, sizeof(qblock));
3773#endif /* COAP_Q_BLOCK_SUPPORT */
3774 LL_FOREACH(session->lg_crcv, lg_crcv) {
3775 size_t chunk = 0;
3776 uint8_t buf[8];
3777 coap_opt_iterator_t opt_iter;
3778
3779 if (token_match != STATE_TOKEN_BASE(lg_crcv->state_token) &&
3780 !coap_binary_equal(&rcvd->actual_token, lg_crcv->app_token)) {
3781 /* try out the next one */
3782 continue;
3783 }
3784
3785 /* lg_crcv found */
3786
3787 if (COAP_RESPONSE_CLASS(rcvd->code) == 2) {
3788 size_t length;
3789 const uint8_t *data;
3791 &opt_iter);
3792 size_t size2 = size_opt ?
3794 coap_opt_length(size_opt)) : 0;
3795
3796 /* length and data are cleared on error */
3797 (void)coap_get_data(rcvd, &length, &data);
3798 rcvd->body_offset = 0;
3799 rcvd->body_total = length;
3800 if (coap_get_block_b(session, rcvd, COAP_OPTION_BLOCK2, &block)) {
3801 have_block = 1;
3802 block_opt = COAP_OPTION_BLOCK2;
3803 }
3804#if COAP_Q_BLOCK_SUPPORT
3805 if (coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK2, &qblock)) {
3806 if (have_block) {
3807 coap_log_warn("Both Block1 and Q-Block1 not supported in a response\n");
3808 }
3809 have_block = 1;
3810 block_opt = COAP_OPTION_Q_BLOCK2;
3811 block = qblock;
3812 /* server indicating that it supports Q_BLOCK */
3813 if (!(session->block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
3814 set_block_mode_has_q(session->block_mode);
3815 }
3816 }
3817#endif /* COAP_Q_BLOCK_SUPPORT */
3818 track_echo(session, rcvd);
3819 if (have_block && (block.m || length)) {
3820 coap_opt_t *fmt_opt = coap_check_option(rcvd,
3822 &opt_iter);
3823 uint16_t fmt = fmt_opt ?
3825 coap_opt_length(fmt_opt)) :
3827 coap_opt_t *etag_opt = coap_check_option(rcvd,
3829 &opt_iter);
3830 size_t saved_offset;
3831 int updated_block;
3832
3833 if (length > block.chunk_size) {
3834 coap_log_debug("block: Oversized packet - reduced to %"PRIu32" from %zu\n",
3835 block.chunk_size, length);
3836 length = block.chunk_size;
3837 }
3838 if (block.m && length != block.chunk_size) {
3839 coap_log_warn("block: Undersized packet - expected %"PRIu32", got %zu\n",
3840 block.chunk_size, length);
3841 /* Unclear how to properly handle this */
3842 rcvd->code = COAP_RESPONSE_CODE(402);
3843 goto expire_lg_crcv;
3844 }
3845 /* Possibility that Size2 not sent, or is too small */
3846 chunk = (size_t)1 << (block.szx + 4);
3847 offset = block.num * chunk;
3848 if (size2 < (offset + length)) {
3849 if (block.m)
3850 size2 = offset + length + 1;
3851 else
3852 size2 = offset + length;
3853 }
3854 saved_offset = offset;
3855
3856 if (lg_crcv->initial) {
3857#if COAP_Q_BLOCK_SUPPORT
3858reinit:
3859#endif /* COAP_Q_BLOCK_SUPPORT */
3860 lg_crcv->initial = 0;
3861 if (lg_crcv->body_data) {
3863 lg_crcv->body_data = NULL;
3864 }
3865 if (etag_opt) {
3866 lg_crcv->etag_length = coap_opt_length(etag_opt);
3867 memcpy(lg_crcv->etag, coap_opt_value(etag_opt), lg_crcv->etag_length);
3868 lg_crcv->etag_set = 1;
3869 } else {
3870 lg_crcv->etag_set = 0;
3871 }
3872 lg_crcv->total_len = size2;
3873 lg_crcv->content_format = fmt;
3874 lg_crcv->szx = block.szx;
3875 lg_crcv->block_option = block_opt;
3876 lg_crcv->last_type = rcvd->type;
3877 lg_crcv->rec_blocks.used = 0;
3878 lg_crcv->rec_blocks.total_blocks = 0;
3879#if COAP_Q_BLOCK_SUPPORT
3880 lg_crcv->rec_blocks.processing_payload_set = 0;
3881#endif /* COAP_Q_BLOCK_SUPPORT */
3882 }
3883 if (lg_crcv->total_len < size2)
3884 lg_crcv->total_len = size2;
3885
3886 if (etag_opt) {
3887 if (!full_match(coap_opt_value(etag_opt),
3888 coap_opt_length(etag_opt),
3889 lg_crcv->etag, lg_crcv->etag_length)) {
3890 /* body of data has changed - need to restart request */
3891 coap_pdu_t *pdu;
3892 uint64_t token = STATE_TOKEN_FULL(lg_crcv->state_token,
3893 ++lg_crcv->retry_counter);
3894 size_t len = coap_encode_var_safe8(buf, sizeof(token), token);
3895 coap_opt_filter_t drop_options;
3896
3897#if COAP_Q_BLOCK_SUPPORT
3898 if (block_opt == COAP_OPTION_Q_BLOCK2)
3899 goto reinit;
3900#endif /* COAP_Q_BLOCK_SUPPORT */
3901
3902 coap_log_warn("Data body updated during receipt - new request started\n");
3903 if (!(session->block_mode & COAP_BLOCK_SINGLE_BODY))
3905
3906 lg_crcv->initial = 1;
3908 lg_crcv->body_data = NULL;
3909
3910 coap_session_new_token(session, &len, buf);
3911 memset(&drop_options, 0, sizeof(coap_opt_filter_t));
3913 pdu = coap_pdu_duplicate_lkd(&lg_crcv->pdu, session, len, buf, &drop_options);
3914 if (!pdu)
3915 goto fail_resp;
3916
3917 coap_update_option(pdu, block_opt,
3918 coap_encode_var_safe(buf, sizeof(buf),
3919 (0 << 4) | (0 << 3) | block.aszx),
3920 buf);
3921
3922 if (coap_send_internal(session, pdu) == COAP_INVALID_MID)
3923 goto fail_resp;
3924
3925 goto skip_app_handler;
3926 }
3927 } else if (lg_crcv->etag_set) {
3928 /* Cannot handle this change in ETag to not being there */
3929 coap_log_warn("Not all blocks have ETag option\n");
3930 goto fail_resp;
3931 }
3932
3933 if (fmt != lg_crcv->content_format) {
3934 coap_log_warn("Content-Format option mismatch\n");
3935 goto fail_resp;
3936 }
3937#if COAP_Q_BLOCK_SUPPORT
3938 if (block_opt == COAP_OPTION_Q_BLOCK2 && size2 != lg_crcv->total_len) {
3939 coap_log_warn("Size2 option mismatch\n");
3940 goto fail_resp;
3941 }
3942#endif /* COAP_Q_BLOCK_SUPPORT */
3943 if (block.num == 0) {
3944 coap_opt_t *obs_opt = coap_check_option(rcvd,
3946 &opt_iter);
3947 if (obs_opt) {
3948 lg_crcv->observe_length = min(coap_opt_length(obs_opt), 3);
3949 memcpy(lg_crcv->observe, coap_opt_value(obs_opt), lg_crcv->observe_length);
3950 lg_crcv->observe_set = 1;
3951 } else {
3952 lg_crcv->observe_set = 0;
3953 }
3954 }
3955 updated_block = 0;
3956 while (offset < saved_offset + length) {
3957 if (!check_if_received_block(&lg_crcv->rec_blocks, block.num)) {
3958#if COAP_Q_BLOCK_SUPPORT
3959 uint32_t this_payload_set = block.num / COAP_MAX_PAYLOADS(session);
3960#endif /* COAP_Q_BLOCK_SUPPORT */
3961
3962 coap_log_debug("found Block option, block size is %u, block nr. %u\n",
3963 1 << (block.szx + 4), block.num);
3964#if COAP_Q_BLOCK_SUPPORT
3965 if (block_opt == COAP_OPTION_Q_BLOCK2 && lg_crcv->rec_blocks.used &&
3966 this_payload_set > lg_crcv->rec_blocks.processing_payload_set &&
3967 this_payload_set != lg_crcv->rec_blocks.latest_payload_set) {
3968 coap_request_missing_q_block2(session, lg_crcv);
3969 }
3970 lg_crcv->rec_blocks.latest_payload_set = this_payload_set;
3971#endif /* COAP_Q_BLOCK_SUPPORT */
3972 /* Update list of blocks received */
3973 if (!update_received_blocks(&lg_crcv->rec_blocks, block.num, block.m)) {
3975 goto fail_resp;
3976 }
3977 updated_block = 1;
3978 }
3979 block.num++;
3980 offset = block.num << (block.szx + 4);
3981 if (!block.bert && block_opt != COAP_OPTION_Q_BLOCK2)
3982 break;
3983 }
3984 block.num--;
3985 /* Only process if not duplicate block */
3986 if (updated_block) {
3987 if ((session->block_mode & COAP_SINGLE_BLOCK_OR_Q) || block.bert) {
3988 if (size2 < saved_offset + length) {
3989 size2 = saved_offset + length;
3990 }
3991 lg_crcv->body_data = coap_block_build_body(lg_crcv->body_data, length, data,
3992 saved_offset, size2);
3993 if (lg_crcv->body_data == NULL) {
3994 goto fail_resp;
3995 }
3996 }
3997 if (block.m || !check_all_blocks_in(&lg_crcv->rec_blocks)) {
3998 /* Not all the payloads of the body have arrived */
3999 size_t len;
4000 coap_pdu_t *pdu;
4001 uint64_t token;
4002
4003 if (block.m) {
4004#if COAP_Q_BLOCK_SUPPORT
4005 if (block_opt == COAP_OPTION_Q_BLOCK2) {
4006 /* Blocks could arrive in wrong order */
4007 if (check_all_blocks_in(&lg_crcv->rec_blocks)) {
4008 goto give_to_app;
4009 }
4010 if (check_all_blocks_in_for_payload_set(session,
4011 &lg_crcv->rec_blocks)) {
4012 block.num = lg_crcv->rec_blocks.range[0].end;
4013 /* Now requesting next payload */
4014 lg_crcv->rec_blocks.processing_payload_set =
4015 block.num / COAP_MAX_PAYLOADS(session) + 1;
4016 if (check_any_blocks_next_payload_set(session,
4017 &lg_crcv->rec_blocks)) {
4018 /* Need to ask for them individually */
4019 coap_request_missing_q_block2(session, lg_crcv);
4020 goto skip_app_handler;
4021 }
4022 } else {
4023 /* The remote end will be sending the next one unless this
4024 is a MAX_PAYLOADS and all previous have been received */
4025 goto skip_app_handler;
4026 }
4027 if (COAP_PROTO_RELIABLE(session->proto) ||
4028 rcvd->type != COAP_MESSAGE_NON)
4029 goto skip_app_handler;
4030
4031 } else
4032#endif /* COAP_Q_BLOCK_SUPPORT */
4033 block.m = 0;
4034
4035 /* Ask for the next block */
4036 token = STATE_TOKEN_FULL(lg_crcv->state_token, ++lg_crcv->retry_counter);
4037 len = coap_encode_var_safe8(buf, sizeof(token), token);
4038 pdu = coap_pdu_duplicate_lkd(&lg_crcv->pdu, session, len, buf, NULL);
4039 if (!pdu)
4040 goto fail_resp;
4041
4042 if (rcvd->type == COAP_MESSAGE_NON)
4043 pdu->type = COAP_MESSAGE_NON; /* Server is using NON */
4044
4045 /* Only sent with the first block */
4047
4048 coap_update_option(pdu, block_opt,
4049 coap_encode_var_safe(buf, sizeof(buf),
4050 ((block.num + 1) << 4) |
4051 (block.m << 3) | block.aszx),
4052 buf);
4053
4055 (void)coap_get_data(&lg_crcv->pdu, &length, &data);
4056 coap_add_data_large_internal(session, NULL, pdu, NULL, NULL, -1, 0, length, data, NULL, NULL, 0, 0);
4057 }
4058 if (coap_send_internal(session, pdu) == COAP_INVALID_MID)
4059 goto fail_resp;
4060 }
4061 if ((session->block_mode & COAP_SINGLE_BLOCK_OR_Q) || block.bert)
4062 goto skip_app_handler;
4063
4064 /* need to put back original token into rcvd */
4065 coap_update_token(rcvd, lg_crcv->app_token->length, lg_crcv->app_token->s);
4066 rcvd->body_offset = saved_offset;
4067#if COAP_Q_BLOCK_SUPPORT
4068 rcvd->body_total = block_opt == COAP_OPTION_Q_BLOCK2 ?
4069 lg_crcv->total_len : size2;
4070#else /* ! COAP_Q_BLOCK_SUPPORT */
4071 rcvd->body_total = size2;
4072#endif /* ! COAP_Q_BLOCK_SUPPORT */
4073 coap_log_debug("Client app version of updated PDU\n");
4075
4076 if (sent) {
4077 /* need to put back original token into sent */
4078 if (lg_crcv->app_token)
4079 coap_update_token(sent, lg_crcv->app_token->length,
4080 lg_crcv->app_token->s);
4081 coap_remove_option(sent, lg_crcv->block_option);
4082 }
4083 goto call_app_handler;
4084 }
4085#if COAP_Q_BLOCK_SUPPORT
4086give_to_app:
4087#endif /* COAP_Q_BLOCK_SUPPORT */
4088 if ((session->block_mode & COAP_SINGLE_BLOCK_OR_Q) || block.bert) {
4089 /* Pretend that there is no block */
4090 coap_remove_option(rcvd, block_opt);
4091 if (lg_crcv->observe_set) {
4093 lg_crcv->observe_length, lg_crcv->observe);
4094 }
4095 rcvd->body_data = lg_crcv->body_data->s;
4096#if COAP_Q_BLOCK_SUPPORT
4097 rcvd->body_length = block_opt == COAP_OPTION_Q_BLOCK2 ?
4098 lg_crcv->total_len : saved_offset + length;
4099#else /* ! COAP_Q_BLOCK_SUPPORT */
4100 rcvd->body_length = saved_offset + length;
4101#endif /* ! COAP_Q_BLOCK_SUPPORT */
4102 rcvd->body_offset = 0;
4103 rcvd->body_total = rcvd->body_length;
4104 } else {
4105 rcvd->body_offset = saved_offset;
4106#if COAP_Q_BLOCK_SUPPORT
4107 rcvd->body_total = block_opt == COAP_OPTION_Q_BLOCK2 ?
4108 lg_crcv->total_len : size2;
4109#else /* ! COAP_Q_BLOCK_SUPPORT */
4110 rcvd->body_total = size2;
4111#endif /* ! COAP_Q_BLOCK_SUPPORT */
4112 }
4113 if (context->response_handler || session->doing_send_recv) {
4114 coap_response_t ret;
4115
4116 /* need to put back original token into rcvd */
4117 if (!coap_binary_equal(&rcvd->actual_token, lg_crcv->app_token)) {
4118 coap_update_token(rcvd, lg_crcv->app_token->length, lg_crcv->app_token->s);
4119 coap_log_debug("Client app version of updated PDU\n");
4121 }
4122 if (sent) {
4123 /* need to put back original token into sent */
4124 if (lg_crcv->app_token)
4125 coap_update_token(sent, lg_crcv->app_token->length,
4126 lg_crcv->app_token->s);
4127 coap_remove_option(sent, lg_crcv->block_option);
4128 }
4129 if (!session->doing_send_recv || !session->req_token ||
4130 !coap_binary_equal(session->req_token, &rcvd->actual_token)) {
4131 if (context->response_handler) {
4133 context->response_handler(session, sent, rcvd,
4134 rcvd->mid),
4135 /* context is being freed off */
4136 assert(0));
4137 } else {
4138 ret = COAP_RESPONSE_OK;
4139 }
4140 if (ret == COAP_RESPONSE_FAIL && rcvd->type != COAP_MESSAGE_ACK) {
4141 coap_send_rst_lkd(session, rcvd);
4143 } else {
4144 coap_send_ack_lkd(session, rcvd);
4146 }
4147 } else {
4148 /* processing coap_send_recv() call */
4149 session->resp_pdu = rcvd;
4150 rcvd->ref++;
4151 /* Will get freed off when PDU is freed off */
4152 rcvd->data_free = lg_crcv->body_data;
4153 lg_crcv->body_data = NULL;
4154 coap_send_ack_lkd(session, rcvd);
4156 }
4157 } else {
4158 coap_send_ack_lkd(session, rcvd);
4160 }
4161 ack_rst_sent = 1;
4162 if (lg_crcv->observe_set == 0) {
4163 /* Expire this entry */
4164 LL_DELETE(session->lg_crcv, lg_crcv);
4165 coap_block_delete_lg_crcv(session, lg_crcv);
4166 goto skip_app_handler;
4167 }
4168 /* Set up for the next data body as observing */
4169 lg_crcv->initial = 1;
4170 if (lg_crcv->body_data) {
4172 lg_crcv->body_data = NULL;
4173 }
4174 }
4175 coap_ticks(&lg_crcv->last_used);
4176 goto skip_app_handler;
4177 } else {
4178 coap_opt_t *obs_opt = coap_check_option(rcvd,
4180 &opt_iter);
4181 if (obs_opt) {
4182 lg_crcv->observe_length = min(coap_opt_length(obs_opt), 3);
4183 memcpy(lg_crcv->observe, coap_opt_value(obs_opt), lg_crcv->observe_length);
4184 lg_crcv->observe_set = 1;
4185 } else {
4186 lg_crcv->observe_set = 0;
4187 if (!coap_binary_equal(&rcvd->actual_token, lg_crcv->app_token)) {
4188 /* need to put back original token into rcvd */
4189 coap_update_token(rcvd, lg_crcv->app_token->length, lg_crcv->app_token->s);
4191 coap_log_debug("PDU presented to app.\n");
4193 }
4194 /* Expire this entry */
4195 goto expire_lg_crcv;
4196 }
4197 }
4198 coap_ticks(&lg_crcv->last_used);
4199 } else if (rcvd->code == COAP_RESPONSE_CODE(401)) {
4200#if COAP_OSCORE_SUPPORT
4201 if (check_freshness(session, rcvd,
4202 (session->oscore_encryption == 0) ? sent : NULL,
4203 NULL, lg_crcv))
4204#else /* !COAP_OSCORE_SUPPORT */
4205 if (check_freshness(session, rcvd, sent, NULL, lg_crcv))
4206#endif /* !COAP_OSCORE_SUPPORT */
4207 goto skip_app_handler;
4208 goto expire_lg_crcv;
4209 } else {
4210 /* Not 2.xx or 4.01 - assume it is a failure of some sort */
4211 goto expire_lg_crcv;
4212 }
4213 if (!block.m && !lg_crcv->observe_set) {
4214fail_resp:
4215 /* lg_crcv no longer required - cache it for 1 sec */
4216 coap_ticks(&lg_crcv->last_used);
4217 lg_crcv->last_used = lg_crcv->last_used - COAP_MAX_TRANSMIT_WAIT_TICKS(session) +
4219 }
4220 /* need to put back original token into rcvd */
4221 if (!coap_binary_equal(&rcvd->actual_token, lg_crcv->app_token)) {
4222 coap_update_token(rcvd, lg_crcv->app_token->length, lg_crcv->app_token->s);
4223 coap_log_debug("Client app version of updated PDU (3)\n");
4225 }
4226 break;
4227 } /* LL_FOREACH() */
4228
4229 /* Check if receiving a block response and if blocks can be set up */
4230 if (recursive == COAP_RECURSE_OK && !lg_crcv) {
4231 if (!sent) {
4232 if (coap_get_block_b(session, rcvd, COAP_OPTION_BLOCK2, &block)
4233#if COAP_Q_BLOCK_SUPPORT
4234 ||
4235 coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK2, &block)
4236#endif /* COAP_Q_BLOCK_SUPPORT */
4237 ) {
4238 coap_log_debug("** %s: large body receive internal issue\n",
4239 coap_session_str(session));
4240 goto skip_app_handler;
4241 }
4242 } else if (COAP_RESPONSE_CLASS(rcvd->code) == 2) {
4243 if (coap_get_block_b(session, rcvd, COAP_OPTION_BLOCK2, &block)) {
4244#if COAP_Q_BLOCK_SUPPORT
4245 if (session->block_mode & COAP_BLOCK_PROBE_Q_BLOCK) {
4246 set_block_mode_drop_q(session->block_mode);
4247 coap_log_debug("Q-Block support disabled\n");
4248 }
4249#endif /* COAP_Q_BLOCK_SUPPORT */
4250 have_block = 1;
4251 if (block.num != 0) {
4252 /* Assume random access and just give the single response to app */
4253 size_t length;
4254 const uint8_t *data;
4255 size_t chunk = (size_t)1 << (block.szx + 4);
4256
4257 coap_get_data(rcvd, &length, &data);
4258 rcvd->body_offset = block.num*chunk;
4259 rcvd->body_total = block.num*chunk + length + (block.m ? 1 : 0);
4260 goto call_app_handler;
4261 }
4262 }
4263#if COAP_Q_BLOCK_SUPPORT
4264 else if (coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK2, &block)) {
4265 have_block = 1;
4266 /* server indicating that it supports Q_BLOCK2 */
4267 if (!(session->block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
4268 set_block_mode_has_q(session->block_mode);
4269 }
4270 }
4271#endif /* COAP_Q_BLOCK_SUPPORT */
4272 if (have_block) {
4273 lg_crcv = coap_block_new_lg_crcv(session, sent, NULL);
4274
4275 if (lg_crcv) {
4276 LL_PREPEND(session->lg_crcv, lg_crcv);
4277 return coap_handle_response_get_block(context, session, sent, rcvd,
4279 }
4280 }
4281 track_echo(session, rcvd);
4282 } else if (rcvd->code == COAP_RESPONSE_CODE(401)) {
4283 lg_crcv = coap_block_new_lg_crcv(session, sent, NULL);
4284
4285 if (lg_crcv) {
4286 LL_PREPEND(session->lg_crcv, lg_crcv);
4287 return coap_handle_response_get_block(context, session, sent, rcvd,
4289 }
4290 }
4291 }
4292 return 0;
4293
4294expire_lg_crcv:
4295 /* need to put back original token into rcvd */
4296 if (!coap_binary_equal(&rcvd->actual_token, lg_crcv->app_token)) {
4297 coap_update_token(rcvd, lg_crcv->app_token->length, lg_crcv->app_token->s);
4298 coap_log_debug("Client app version of updated PDU\n");
4300 }
4301
4302 if (sent) {
4303 /* need to put back original token into sent */
4304 if (lg_crcv->app_token)
4305 coap_update_token(sent, lg_crcv->app_token->length,
4306 lg_crcv->app_token->s);
4307 coap_remove_option(sent, lg_crcv->block_option);
4308 }
4309 /* Expire this entry */
4310 LL_DELETE(session->lg_crcv, lg_crcv);
4311 coap_block_delete_lg_crcv(session, lg_crcv);
4312
4313call_app_handler:
4314 return 0;
4315
4316skip_app_handler:
4317 if (!ack_rst_sent)
4318 coap_send_ack_lkd(session, rcvd);
4319 return 1;
4320}
4321#endif /* COAP_CLIENT_SUPPORT */
4322
4323#if COAP_SERVER_SUPPORT
4324/* Check if lg_xmit generated and update PDU code if so */
4325void
4327 const coap_pdu_t *request,
4328 coap_pdu_t *response, const coap_resource_t *resource,
4329 const coap_string_t *query) {
4330 coap_lg_xmit_t *lg_xmit;
4331
4332 if (response->code == 0)
4333 return;
4334 lg_xmit = coap_find_lg_xmit_response(session, request, resource, query);
4335 if (lg_xmit && lg_xmit->pdu.code == 0) {
4336 lg_xmit->pdu.code = response->code;
4337 return;
4338 }
4339}
4340#endif /* COAP_SERVER_SUPPORT */
4341
4342#if COAP_CLIENT_SUPPORT
4343void
4345 uint64_t token_match =
4347 pdu->actual_token.length));
4348 coap_lg_xmit_t *lg_xmit;
4349 coap_lg_crcv_t *lg_crcv;
4350
4351 if (session->lg_crcv) {
4352 LL_FOREACH(session->lg_crcv, lg_crcv) {
4353 if (coap_binary_equal(&pdu->actual_token, lg_crcv->app_token))
4354 return;
4355 if (token_match == STATE_TOKEN_BASE(lg_crcv->state_token)) {
4356 coap_update_token(pdu, lg_crcv->app_token->length,
4357 lg_crcv->app_token->s);
4358 coap_log_debug("Client app version of updated PDU\n");
4360 return;
4361 }
4362 }
4363 }
4364 if (COAP_PDU_IS_REQUEST(pdu) && session->lg_xmit) {
4365 LL_FOREACH(session->lg_xmit, lg_xmit) {
4366 if (coap_binary_equal(&pdu->actual_token, lg_xmit->b.b1.app_token))
4367 return;
4368 if (token_match == STATE_TOKEN_BASE(lg_xmit->b.b1.state_token)) {
4369 coap_update_token(pdu, lg_xmit->b.b1.app_token->length,
4370 lg_xmit->b.b1.app_token->s);
4371 coap_log_debug("Client app version of updated PDU\n");
4373 return;
4374 }
4375 }
4376 }
4377}
4378#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
coap_mid_t coap_send_rst_lkd(coap_session_t *session, const coap_pdu_t *request)
Sends an RST message with code 0 for the specified request to dst.
Definition coap_net.c:971
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:986
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:89
#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:286
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:85
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:80
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
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:4495
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:1226
coap_mid_t coap_send_internal(coap_session_t *session, coap_pdu_t *pdu)
Sends a CoAP message to given peer.
Definition coap_net.c:1679
coap_response_t
Definition coap_net.h:48
void coap_ticks(coap_tick_t *)
Returns the current value of an internal tick counter.
@ COAP_RESPONSE_FAIL
Response not liked - send CoAP RST packet.
Definition coap_net.h:49
@ COAP_RESPONSE_OK
Response is fine.
Definition coap_net.h:50
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_callback_ret_release(r, c, func, failed)
Dummy for no thread-safe code.
#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)
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:619
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:482
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:406
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:713
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:223
#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:332
#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:769
#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:940
#define COAP_MEDIATYPE_APPLICATION_MB_CBOR_SEQ
Definition coap_pdu.h:254
#define COAP_OPTION_CONTENT_FORMAT
Definition coap_pdu.h:128
#define COAP_OPTION_SIZE2
Definition coap_pdu.h:139
#define COAP_OPTION_BLOCK1
Definition coap_pdu.h:138
#define COAP_OPTION_Q_BLOCK1
Definition coap_pdu.h:135
void coap_delete_pdu(coap_pdu_t *pdu)
Dispose of an CoAP PDU and frees associated storage.
Definition coap_pdu.c:183
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition coap_pdu.h:263
#define COAP_OPTION_URI_PATH
Definition coap_pdu.h:127
#define COAP_RESPONSE_CODE(N)
Definition coap_pdu.h:160
#define COAP_RESPONSE_CLASS(C)
Definition coap_pdu.h:163
coap_pdu_code_t
Set of codes available for a PDU.
Definition coap_pdu.h:326
#define COAP_OPTION_SIZE1
Definition coap_pdu.h:143
coap_pdu_type_t
CoAP PDU message type definitions.
Definition coap_pdu.h:68
#define COAP_MEDIATYPE_TEXT_PLAIN
Definition coap_pdu.h:213
int coap_add_token(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Adds token of length len to pdu.
Definition coap_pdu.c:349
#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:759
#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:865
#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:873
#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:834
@ 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.
coap_response_handler_t response_handler
Called when a response is received.
uint32_t block_mode
Zero or more COAP_BLOCK_ or'd options.
coap_resource_t * proxy_uri_resource
can be used for handling proxy URI resources
coap_resource_t * unknown_resource
can be used for handling unknown resources
uint64_t state_token
state token
size_t bert_size
size of last BERT block
uint32_t count
the number of packets sent for payload
coap_binary_t * app_token
original PDU token
coap_pdu_code_t request_method
Method used to request this data.
uint8_t rtag_length
RTag length.
coap_string_t * query
Associated query for the resource.
uint64_t etag
ETag value.
coap_resource_t * resource
associated resource
coap_time_t maxage_expire
When this entry expires.
uint8_t rtag_set
Set if RTag is in receive PDU.
uint8_t rtag[8]
RTag for block checking.
Structure to hold large body (many blocks) client receive information.
uint16_t block_option
Block option in use.
uint8_t etag[8]
ETag for block checking.
uint8_t etag_length
ETag length.
uint8_t last_type
Last request type (CON/NON)
uint8_t observe_length
Length of observe data.
uint8_t observe[3]
Observe data (if observe_set) (only 24 bits)
uint8_t etag_set
Set if ETag is in receive PDU.
coap_rblock_t rec_blocks
uint8_t initial
If set, has not been used yet.
uint8_t szx
size of individual blocks
uint16_t o_block_option
Block CoAP option used when initiating Observe.
uint16_t content_format
Content format for the set of blocks.
coap_pdu_t pdu
skeletal PDU
uint8_t o_blk_size
Block size used when initiating Observe.
coap_tick_t last_used
< list of received blocks
coap_bin_const_t ** obs_token
Tokens used in setting up Observe (to handle large FETCH)
uint64_t state_token
state token
coap_binary_t * app_token
app requesting PDU token
uint16_t retry_counter
Retry counter (part of state token)
coap_binary_t * body_data
Used for re-assembling entire body.
size_t obs_token_cnt
number of tokens used to set up Observe
uint8_t observe_set
Set if this is an observe receive PDU.
size_t total_len
Length as indicated by SIZE2 option.
Structure to hold large body (many blocks) server receive information.
uint8_t rtag[8]
RTag for block checking.
coap_mid_t last_mid
Last received mid for this set of packets.
uint8_t rtag_set
Set if RTag is in receive PDU.
uint16_t block_option
Block option in use.
size_t total_len
Length as indicated by SIZE1 option.
uint8_t observe_length
Length of observe data.
coap_rblock_t rec_blocks
set to uri_path if unknown resource
uint8_t no_more_seen
Set if block with more not set seen.
coap_binary_t * body_data
Used for re-assembling entire body.
coap_resource_t * resource
associated resource
uint8_t observe_set
Set if this is an observe receive PDU.
uint8_t rtag_length
RTag length.
uint8_t last_type
Last request type (CON/NON)
uint8_t szx
size of individual blocks
coap_tick_t last_used
Last time data sent or 0.
uint8_t observe[3]
Observe data (if set) (only 24 bits)
uint16_t content_format
Content format for the set of blocks.
coap_bin_const_t * last_token
< list of received blocks
coap_str_const_t * uri_path
Structure to hold large body (many blocks) transmission information.
coap_tick_t last_all_sent
Last time all data sent or 0.
coap_release_large_data_t release_func
large data de-alloc function
uint8_t blk_size
large block transmission size
coap_tick_t last_sent
Last time any data sent.
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.
unsigned ref
reference count
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
coap_binary_t * data_free
Data to be freed off by coap_delete_pdu()
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
coap_response_t last_con_handler_res
The result of calling the response handler of the last CON.
uint8_t doing_send_recv
Set if coap_send_recv() active.
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_bin_const_t * req_token
Token in request pdu of coap_send_recv()
coap_pdu_t * resp_pdu
PDU returned in coap_send_recv() call.
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