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