libcoap 4.3.5b
Loading...
Searching...
No Matches
coap_block.c
Go to the documentation of this file.
1/* coap_block.c -- block transfer
2 *
3 * Copyright (C) 2010--2012,2015-2025 Olaf Bergmann <bergmann@tzi.org> and others
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 *
7 * This file is part of the CoAP library libcoap. Please see
8 * README for terms of use.
9 */
10
15
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;
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;
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_update_token(&lg_crcv->pdu, lg_crcv->app_token->length, lg_crcv->app_token->s);
1474 coap_lock_callback(session->context,
1475 session->context->nack_handler(session, &lg_crcv->pdu,
1477 lg_crcv->pdu.mid));
1478 goto expire;
1479 }
1480 if (lg_crcv->rec_blocks.last_seen + scaled_timeout <= now) {
1481 coap_request_missing_q_block2(session, lg_crcv);
1482 } else {
1483 if (*tim_rem > lg_crcv->rec_blocks.last_seen + scaled_timeout - now) {
1484 *tim_rem = lg_crcv->rec_blocks.last_seen + scaled_timeout - now;
1485 ret = 1;
1486 }
1487 }
1488 }
1489#endif /* COAP_Q_BLOCK_SUPPORT */
1490 /* Used for Block2 and Q-Block2 */
1491check_expire:
1492 if (!lg_crcv->observe_set && lg_crcv->last_used &&
1493 lg_crcv->last_used + partial_timeout <= now) {
1494#if COAP_Q_BLOCK_SUPPORT
1495expire:
1496#endif /* COAP_Q_BLOCK_SUPPORT */
1497 /* Expire this entry */
1498 LL_DELETE(session->lg_crcv, lg_crcv);
1499 coap_block_delete_lg_crcv(session, lg_crcv);
1500 } else if (!lg_crcv->observe_set && lg_crcv->last_used) {
1501 /* Delay until the lg_crcv needs to expire */
1502 if (*tim_rem > lg_crcv->last_used + partial_timeout - now) {
1503 *tim_rem = lg_crcv->last_used + partial_timeout - now;
1504 ret = 1;
1505 }
1506 }
1507 }
1508 return ret;
1509}
1510#endif /* COAP_CLIENT_SUPPORT */
1511
1512#if COAP_SERVER_SUPPORT
1513#if COAP_Q_BLOCK_SUPPORT
1514static coap_pdu_t *
1515pdu_408_build(coap_session_t *session, coap_lg_srcv_t *lg_srcv) {
1516 coap_pdu_t *pdu;
1517 uint8_t buf[4];
1518
1520 COAP_RESPONSE_CODE(408),
1521 coap_new_message_id_lkd(session),
1523 if (!pdu)
1524 return NULL;
1525 if (lg_srcv->last_token)
1526 coap_add_token(pdu, lg_srcv->last_token->length, lg_srcv->last_token->s);
1528 coap_encode_var_safe(buf, sizeof(buf),
1530 buf);
1531 pdu->token[pdu->used_size++] = COAP_PAYLOAD_START;
1532 pdu->data = pdu->token + pdu->used_size;
1533 return pdu;
1534}
1535
1536static int
1537add_408_block(coap_pdu_t *pdu, int block) {
1538 size_t len;
1539 uint8_t val[8];
1540
1541 assert(block >= 0 && block < (1 << 20));
1542
1543 if (block < 0 || block >= (1 << 20)) {
1544 return 0;
1545 } else if (block < 24) {
1546 len = 1;
1547 val[0] = block;
1548 } else if (block < 0x100) {
1549 len = 2;
1550 val[0] = 24;
1551 val[1] = block;
1552 } else if (block < 0x10000) {
1553 len = 3;
1554 val[0] = 25;
1555 val[1] = block >> 8;
1556 val[2] = block & 0xff;
1557 } else { /* Largest block number is 2^^20 - 1 */
1558 len = 4;
1559 val[0] = 26;
1560 val[1] = block >> 16;
1561 val[2] = (block >> 8) & 0xff;
1562 val[3] = block & 0xff;
1563 }
1564 if (coap_pdu_check_resize(pdu, pdu->used_size + len)) {
1565 memcpy(&pdu->token[pdu->used_size], val, len);
1566 pdu->used_size += len;
1567 return 1;
1568 }
1569 return 0;
1570}
1571#endif /* COAP_Q_BLOCK_SUPPORT */
1572#endif /* COAP_SERVER_SUPPORT */
1573
1574static int
1575check_if_received_block(coap_rblock_t *rec_blocks, uint32_t block_num) {
1576 uint32_t i;
1577
1578 for (i = 0; i < rec_blocks->used; i++) {
1579 if (block_num < rec_blocks->range[i].begin)
1580 return 0;
1581 if (block_num <= rec_blocks->range[i].end)
1582 return 1;
1583 }
1584 return 0;
1585}
1586
1587#if COAP_SERVER_SUPPORT
1588static int
1589check_if_next_block(coap_rblock_t *rec_blocks, uint32_t block_num) {
1590 if (rec_blocks->used == 0) {
1591 return block_num == 0 ? 1 : 0;
1592 }
1593 if (rec_blocks->range[rec_blocks->used-1].end + 1 == block_num)
1594 return 1;
1595
1596 return 0;
1597}
1598#endif /* COAP_SERVER_SUPPORT */
1599
1600static int
1602 uint32_t i;
1603 uint32_t block = 0;
1604
1605 if (rec_blocks->total_blocks == 0) {
1606 /* Not seen block with More bit unset yet */
1607 return 0;
1608 }
1609
1610 for (i = 0; i < rec_blocks->used; i++) {
1611 if (block < rec_blocks->range[i].begin)
1612 return 0;
1613 if (block < rec_blocks->range[i].end)
1614 block = rec_blocks->range[i].end;
1615 }
1616 return 1;
1617}
1618
1619#if COAP_CLIENT_SUPPORT
1620#if COAP_Q_BLOCK_SUPPORT
1621static int
1622check_all_blocks_in_for_payload_set(coap_session_t *session,
1623 coap_rblock_t *rec_blocks) {
1624 if (rec_blocks->used &&
1625 (rec_blocks->range[0].end + 1) / COAP_MAX_PAYLOADS(session) >
1626 rec_blocks->processing_payload_set)
1627 return 1;
1628 return 0;
1629}
1630
1631static int
1632check_any_blocks_next_payload_set(coap_session_t *session,
1633 coap_rblock_t *rec_blocks) {
1634 if (rec_blocks->used > 1 &&
1635 rec_blocks->range[1].begin / COAP_MAX_PAYLOADS(session) ==
1636 rec_blocks->processing_payload_set)
1637 return 1;
1638 return 0;
1639}
1640#endif /* COAP_Q_BLOCK_SUPPORT */
1641#endif /* COAP_CLIENT_SUPPORT */
1642
1643#if COAP_SERVER_SUPPORT
1644/*
1645 * return 1 if there is a future expire time, else 0.
1646 * update tim_rem with remaining value if return is 1.
1647 */
1648int
1650 coap_tick_t *tim_rem) {
1651 coap_lg_srcv_t *lg_srcv;
1652 coap_lg_srcv_t *q;
1653 coap_tick_t partial_timeout;
1654#if COAP_Q_BLOCK_SUPPORT
1655 coap_tick_t receive_timeout = COAP_NON_RECEIVE_TIMEOUT_TICKS(session);
1656#endif /* COAP_Q_BLOCK_SUPPORT */
1657 int ret = 0;
1658
1659 *tim_rem = -1;
1660#if COAP_Q_BLOCK_SUPPORT
1661 if (COAP_PROTO_NOT_RELIABLE(session->proto) &&
1662 session->block_mode & COAP_BLOCK_HAS_Q_BLOCK)
1663 partial_timeout = COAP_NON_PARTIAL_TIMEOUT_TICKS(session);
1664 else
1665#endif /* COAP_Q_BLOCK_SUPPORT */
1666 partial_timeout = COAP_MAX_TRANSMIT_WAIT_TICKS(session);
1667
1668 LL_FOREACH_SAFE(session->lg_srcv, lg_srcv, q) {
1669 if (COAP_PROTO_RELIABLE(session->proto) || lg_srcv->last_type != COAP_MESSAGE_NON)
1670 goto check_expire;
1671
1672#if COAP_Q_BLOCK_SUPPORT
1673 if (lg_srcv->block_option == COAP_OPTION_Q_BLOCK1 && lg_srcv->rec_blocks.used) {
1674 size_t scaled_timeout = receive_timeout *
1675 ((size_t)1 << lg_srcv->rec_blocks.retry);
1676
1677 if (lg_srcv->rec_blocks.retry >= COAP_NON_MAX_RETRANSMIT(session)) {
1678 /* Done NON_MAX_RETRANSMIT retries */
1679 goto expire;
1680 }
1681 if (lg_srcv->rec_blocks.last_seen + scaled_timeout <= now) {
1682 uint32_t i;
1683 int block = -1; /* Last one seen */
1684 size_t block_size = (size_t)1 << (lg_srcv->szx + 4);
1685 size_t final_block = (lg_srcv->total_len + block_size - 1)/block_size - 1;
1686 size_t cur_payload;
1687 size_t last_payload_block;
1688 coap_pdu_t *pdu = NULL;
1689 size_t no_blocks = 0;
1690
1691 /* Need to count the number of missing blocks */
1692 for (i = 0; i < lg_srcv->rec_blocks.used; i++) {
1693 if (block < (int)lg_srcv->rec_blocks.range[i].begin &&
1694 lg_srcv->rec_blocks.range[i].begin != 0) {
1695 block++;
1696 no_blocks += lg_srcv->rec_blocks.range[i].begin - block;
1697 }
1698 if (block < (int)lg_srcv->rec_blocks.range[i].end) {
1699 block = lg_srcv->rec_blocks.range[i].end;
1700 }
1701 }
1702 if (no_blocks == 0 && block == (int)final_block)
1703 goto expire;
1704
1705 /* Include missing up to end of current payload or total amount */
1706 cur_payload = block / COAP_MAX_PAYLOADS(session);
1707 last_payload_block = (cur_payload + 1) * COAP_MAX_PAYLOADS(session) - 1;
1708 if (final_block > last_payload_block) {
1709 final_block = last_payload_block;
1710 }
1711 no_blocks += final_block - block;
1712 if (no_blocks == 0) {
1713 /* Add in the blocks out of the next payload */
1714 final_block = (lg_srcv->total_len + block_size - 1)/block_size - 1;
1715 last_payload_block += COAP_MAX_PAYLOADS(session);
1716 if (final_block > last_payload_block) {
1717 final_block = last_payload_block;
1718 }
1719 }
1720 /* Ask for the missing blocks */
1721 block = -1;
1722 for (i = 0; i < lg_srcv->rec_blocks.used; i++) {
1723 if (block < (int)lg_srcv->rec_blocks.range[i].begin &&
1724 lg_srcv->rec_blocks.range[i].begin != 0) {
1725 /* Report on missing blocks */
1726 if (pdu == NULL) {
1727 pdu = pdu_408_build(session, lg_srcv);
1728 if (!pdu)
1729 continue;
1730 }
1731 block++;
1732 for (; block < (int)lg_srcv->rec_blocks.range[i].begin; block++) {
1733 if (!add_408_block(pdu, block)) {
1734 break;
1735 }
1736 }
1737 }
1738 if (block < (int)lg_srcv->rec_blocks.range[i].end) {
1739 block = lg_srcv->rec_blocks.range[i].end;
1740 }
1741 }
1742 block++;
1743 for (; block <= (int)final_block; block++) {
1744 if (pdu == NULL) {
1745 pdu = pdu_408_build(session, lg_srcv);
1746 if (!pdu)
1747 continue;
1748 }
1749 if (!add_408_block(pdu, block)) {
1750 break;
1751 }
1752 }
1753 if (pdu)
1754 coap_send_internal(session, pdu);
1755 lg_srcv->rec_blocks.retry++;
1756 coap_ticks(&lg_srcv->rec_blocks.last_seen);
1757 }
1758 if (*tim_rem > lg_srcv->rec_blocks.last_seen + scaled_timeout - now) {
1759 *tim_rem = lg_srcv->rec_blocks.last_seen + scaled_timeout - now;
1760 ret = 1;
1761 }
1762 }
1763#endif /* COAP_Q_BLOCK_SUPPORT */
1764 /* Used for Block1 and Q-Block1 */
1765check_expire:
1766 if (lg_srcv->no_more_seen)
1767 partial_timeout = 10 * COAP_TICKS_PER_SECOND;
1768 if (lg_srcv->last_used && lg_srcv->last_used + partial_timeout <= now) {
1769#if COAP_Q_BLOCK_SUPPORT
1770expire:
1771#endif /* COAP_Q_BLOCK_SUPPORT */
1772 /* Expire this entry */
1773 if (lg_srcv->no_more_seen && lg_srcv->block_option == COAP_OPTION_BLOCK1) {
1774 /*
1775 * Need to send a separate 4.08 to indicate missing blocks
1776 * Using NON is permissable as per
1777 * https://datatracker.ietf.org/doc/html/rfc7252#section-5.2.3
1778 */
1779 coap_pdu_t *pdu;
1780
1782 COAP_RESPONSE_CODE(408),
1783 coap_new_message_id_lkd(session),
1785 if (pdu) {
1786 if (lg_srcv->last_token)
1787 coap_add_token(pdu, lg_srcv->last_token->length, lg_srcv->last_token->s);
1788 coap_add_data(pdu, sizeof("Missing interim block")-1,
1789 (const uint8_t *)"Missing interim block");
1790 coap_send_internal(session, pdu);
1791 }
1792 }
1793 LL_DELETE(session->lg_srcv, lg_srcv);
1794 coap_block_delete_lg_srcv(session, lg_srcv);
1795 } else if (lg_srcv->last_used) {
1796 /* Delay until the lg_srcv needs to expire */
1797 if (*tim_rem > lg_srcv->last_used + partial_timeout - now) {
1798 *tim_rem = lg_srcv->last_used + partial_timeout - now;
1799 ret = 1;
1800 }
1801 }
1802 }
1803 return ret;
1804}
1805#endif /* COAP_SERVER_SUPPORT */
1806
1807#if COAP_Q_BLOCK_SUPPORT
1808/*
1809 * pdu is always released before return IF COAP_SEND_INC_PDU
1810 */
1812coap_send_q_blocks(coap_session_t *session,
1813 coap_lg_xmit_t *lg_xmit,
1814 coap_block_b_t block,
1815 coap_pdu_t *pdu,
1816 coap_send_pdu_t send_pdu) {
1817 coap_pdu_t *block_pdu = NULL;
1818 coap_opt_filter_t drop_options;
1820 uint64_t token;
1821 const uint8_t *ptoken;
1822 uint8_t ltoken[8];
1823 size_t ltoken_length;
1824 uint32_t delayqueue_cnt = 0;
1825
1826 if (!lg_xmit) {
1827 if (send_pdu == COAP_SEND_INC_PDU)
1828 return coap_send_internal(session, pdu);
1829 return COAP_INVALID_MID;
1830 }
1831
1832 if (pdu->type == COAP_MESSAGE_CON) {
1833 coap_queue_t *delayqueue;
1834
1835 delayqueue_cnt = session->con_active +
1836 (send_pdu == COAP_SEND_INC_PDU ? 1 : 0);
1837 LL_FOREACH(session->delayqueue, delayqueue) {
1838 delayqueue_cnt++;
1839 }
1840 }
1841 pdu->lg_xmit = lg_xmit;
1842 if (block.m &&
1843 ((pdu->type == COAP_MESSAGE_NON &&
1844 ((block.num + 1) % COAP_MAX_PAYLOADS(session)) + 1 !=
1845 COAP_MAX_PAYLOADS(session)) ||
1846 (pdu->type == COAP_MESSAGE_ACK &&
1847 lg_xmit->option == COAP_OPTION_Q_BLOCK2) ||
1848 (pdu->type == COAP_MESSAGE_CON &&
1849 delayqueue_cnt < COAP_NSTART(session)) ||
1850 COAP_PROTO_RELIABLE(session->proto))) {
1851 /* Allocate next pdu if there is headroom */
1852 if (COAP_PDU_IS_RESPONSE(pdu)) {
1853 ptoken = pdu->actual_token.s;
1854 ltoken_length = pdu->actual_token.length;
1855 } else {
1856 token = STATE_TOKEN_FULL(lg_xmit->b.b1.state_token,++lg_xmit->b.b1.count);
1857 ltoken_length = coap_encode_var_safe8(ltoken, sizeof(token), token);
1858 ptoken = ltoken;
1859 }
1860
1861 memset(&drop_options, 0, sizeof(coap_opt_filter_t));
1862 coap_option_filter_set(&drop_options, lg_xmit->option);
1863 block_pdu = coap_pdu_duplicate_lkd(pdu, session,
1864 ltoken_length,
1865 ptoken, &drop_options);
1866 if (block_pdu->type == COAP_MESSAGE_ACK)
1867 block_pdu->type = COAP_MESSAGE_CON;
1868 }
1869
1870 /* Send initial pdu (which deletes 'pdu') */
1871 if (send_pdu == COAP_SEND_INC_PDU &&
1872 (mid = coap_send_internal(session, pdu)) == COAP_INVALID_MID) {
1873 /* Not expected, underlying issue somewhere */
1874 coap_delete_pdu_lkd(block_pdu);
1875 return COAP_INVALID_MID;
1876 }
1877
1878 while (block_pdu) {
1879 coap_pdu_t *t_pdu = NULL;
1880 uint8_t buf[8];
1881 size_t chunk = ((size_t)1 << (lg_xmit->blk_size + 4));
1882
1883 block.num++;
1884 lg_xmit->offset = block.num * chunk;
1885 block.m = lg_xmit->offset + chunk < lg_xmit->length;
1886 if (block.m && ((block_pdu->type == COAP_MESSAGE_NON &&
1887 (block.num % COAP_MAX_PAYLOADS(session)) + 1 !=
1888 COAP_MAX_PAYLOADS(session)) ||
1889 (block_pdu->type == COAP_MESSAGE_CON &&
1890 delayqueue_cnt + 1 < COAP_NSTART(session)) ||
1891 COAP_PROTO_RELIABLE(session->proto))) {
1892 /*
1893 * Send following block if
1894 * NON and more in MAX_PAYLOADS
1895 * CON and NSTART allows it (based on number in delayqueue)
1896 * Reliable transport
1897 */
1898 if (COAP_PDU_IS_RESPONSE(block_pdu)) {
1899 ptoken = block_pdu->actual_token.s;
1900 ltoken_length = block_pdu->actual_token.length;
1901 } else {
1902 token = STATE_TOKEN_FULL(lg_xmit->b.b1.state_token,++lg_xmit->b.b1.count);
1903 ltoken_length = coap_encode_var_safe8(ltoken, sizeof(token), token);
1904 ptoken = ltoken;
1905 }
1906 t_pdu = coap_pdu_duplicate_lkd(block_pdu, session,
1907 ltoken_length, ptoken, &drop_options);
1908 }
1909 if (!coap_update_option(block_pdu, lg_xmit->option,
1911 sizeof(buf),
1912 ((block.num) << 4) |
1913 (block.m << 3) |
1914 block.szx),
1915 buf)) {
1916 coap_log_warn("Internal update issue option\n");
1917 coap_delete_pdu_lkd(block_pdu);
1918 coap_delete_pdu_lkd(t_pdu);
1919 break;
1920 }
1921
1922 if (!coap_add_block(block_pdu,
1923 lg_xmit->length,
1924 lg_xmit->data,
1925 block.num,
1926 block.szx)) {
1927 coap_log_warn("Internal update issue data\n");
1928 coap_delete_pdu_lkd(block_pdu);
1929 coap_delete_pdu_lkd(t_pdu);
1930 break;
1931 }
1932 if (COAP_PDU_IS_RESPONSE(block_pdu)) {
1933 lg_xmit->last_block = block.num;
1934 }
1935 mid = coap_send_internal(session, block_pdu);
1936 if (mid == COAP_INVALID_MID) {
1937 /* Not expected, underlying issue somewhere */
1938 coap_delete_pdu_lkd(t_pdu);
1939 return COAP_INVALID_MID;
1940 }
1941 block_pdu = t_pdu;
1942 }
1943 if (!block.m) {
1944 lg_xmit->last_payload = 0;
1945 coap_ticks(&lg_xmit->last_all_sent);
1946 } else
1947 coap_ticks(&lg_xmit->last_payload);
1948 return mid;
1949}
1950
1951#if COAP_CLIENT_SUPPORT
1953coap_block_check_q_block1_xmit(coap_session_t *session, coap_tick_t now) {
1954 coap_lg_xmit_t *lg_xmit;
1955 coap_lg_xmit_t *q;
1956 coap_tick_t timed_out;
1957 coap_tick_t tim_rem = (coap_tick_t)-1;
1958
1959 LL_FOREACH_SAFE(session->lg_xmit, lg_xmit, q) {
1960 coap_tick_t non_timeout = lg_xmit->non_timeout_random_ticks;
1961
1962 if (now < non_timeout)
1963 return non_timeout - now;
1964 timed_out = now - non_timeout;
1965
1966 if (lg_xmit->last_payload) {
1967 if (lg_xmit->last_payload <= timed_out) {
1968 /* Send off the next MAX_PAYLOAD set */
1969 coap_block_b_t block;
1970 size_t chunk = (size_t)1 << (lg_xmit->blk_size + 4);
1971
1972 memset(&block, 0, sizeof(block));
1973 block.num = (uint32_t)(lg_xmit->offset / chunk);
1974 block.m = lg_xmit->offset + chunk < lg_xmit->length;
1975 block.szx = lg_xmit->blk_size;
1976 coap_send_q_blocks(session, lg_xmit, block, &lg_xmit->pdu, COAP_SEND_SKIP_PDU);
1977 if (tim_rem > non_timeout)
1978 tim_rem = non_timeout;
1979 } else {
1980 /* Delay until the next MAX_PAYLOAD needs to be sent off */
1981 if (tim_rem > lg_xmit->last_payload - timed_out)
1982 tim_rem = lg_xmit->last_payload - timed_out;
1983 }
1984 } else if (lg_xmit->last_all_sent) {
1985 non_timeout = COAP_NON_TIMEOUT_TICKS(session);
1986 if (lg_xmit->last_all_sent + 4 * non_timeout <= now) {
1987 /* Expire this entry */
1988 LL_DELETE(session->lg_xmit, lg_xmit);
1989 coap_block_delete_lg_xmit(session, lg_xmit);
1990 } else {
1991 /* Delay until the lg_xmit needs to expire */
1992 if (tim_rem > lg_xmit->last_all_sent + 4 * non_timeout - now)
1993 tim_rem = lg_xmit->last_all_sent + 4 * non_timeout - now;
1994 }
1995 }
1996 }
1997 return tim_rem;
1998}
1999#endif /* COAP_CLIENT_SUPPORT */
2000
2001#if COAP_SERVER_SUPPORT
2003coap_block_check_q_block2_xmit(coap_session_t *session, coap_tick_t now) {
2004 coap_lg_xmit_t *lg_xmit;
2005 coap_lg_xmit_t *q;
2006 coap_tick_t timed_out;
2007 coap_tick_t tim_rem = (coap_tick_t)-1;
2008
2009 LL_FOREACH_SAFE(session->lg_xmit, lg_xmit, q) {
2010 coap_tick_t non_timeout = lg_xmit->non_timeout_random_ticks;
2011
2012 if (now < non_timeout)
2013 return non_timeout - now;
2014 timed_out = now - non_timeout;
2015
2016 if (lg_xmit->last_payload) {
2017 if (lg_xmit->last_payload <= timed_out) {
2018 /* Send off the next MAX_PAYLOAD set */
2019 coap_block_b_t block;
2020 size_t chunk = (size_t)1 << (lg_xmit->blk_size + 4);
2021
2022 memset(&block, 0, sizeof(block));
2023 block.num = (uint32_t)(lg_xmit->offset / chunk);
2024 block.m = lg_xmit->offset + chunk < lg_xmit->length;
2025 block.szx = lg_xmit->blk_size;
2026 if (block.num == (uint32_t)lg_xmit->last_block)
2027 coap_send_q_blocks(session, lg_xmit, block, &lg_xmit->pdu, COAP_SEND_SKIP_PDU);
2028 if (tim_rem > non_timeout)
2029 tim_rem = non_timeout;
2030 } else {
2031 /* Delay until the next MAX_PAYLOAD needs to be sent off */
2032 if (tim_rem > lg_xmit->last_payload - timed_out)
2033 tim_rem = lg_xmit->last_payload - timed_out;
2034 }
2035 } else if (lg_xmit->last_all_sent) {
2036 non_timeout = COAP_NON_TIMEOUT_TICKS(session);
2037 if (lg_xmit->last_all_sent + 4 * non_timeout <= now) {
2038 /* Expire this entry */
2039 LL_DELETE(session->lg_xmit, lg_xmit);
2040 coap_block_delete_lg_xmit(session, lg_xmit);
2041 } else {
2042 /* Delay until the lg_xmit needs to expire */
2043 if (tim_rem > lg_xmit->last_all_sent + 4 * non_timeout - now)
2044 tim_rem = lg_xmit->last_all_sent + 4 * non_timeout - now;
2045 }
2046 }
2047 }
2048 return tim_rem;
2049}
2050#endif /* COAP_SERVER_SUPPORT */
2051#endif /* COAP_Q_BLOCK_SUPPORT */
2052
2053#if COAP_CLIENT_SUPPORT
2054/*
2055 * If Observe = 0, save the token away and return NULL
2056 * Else If Observe = 1, return the saved token for this block
2057 * Else, return NULL
2058 */
2059static coap_bin_const_t *
2060track_fetch_observe(coap_pdu_t *pdu, coap_lg_crcv_t *lg_crcv,
2061 uint32_t block_num, coap_bin_const_t *token) {
2062 /* Need to handle Observe for large FETCH */
2063 coap_opt_iterator_t opt_iter;
2065 &opt_iter);
2066
2067 if (opt && lg_crcv) {
2068 int observe_action = -1;
2069 coap_bin_const_t **tmp;
2070
2071 observe_action = coap_decode_var_bytes(coap_opt_value(opt),
2072 coap_opt_length(opt));
2073 if (observe_action == COAP_OBSERVE_ESTABLISH) {
2074 /* Save the token in lg_crcv */
2075 if (lg_crcv->obs_token_cnt <= block_num) {
2076 size_t i;
2077
2078 tmp = coap_realloc_type(COAP_STRING, lg_crcv->obs_token,
2079 (block_num + 1) * sizeof(lg_crcv->obs_token[0]));
2080 if (tmp == NULL)
2081 return NULL;
2082 lg_crcv->obs_token = tmp;
2083 for (i = lg_crcv->obs_token_cnt; i < block_num + 1; i++) {
2084 lg_crcv->obs_token[i] = NULL;
2085 }
2086 }
2087 coap_delete_bin_const(lg_crcv->obs_token[block_num]);
2088
2089 if (lg_crcv->obs_token_cnt <= block_num)
2090 lg_crcv->obs_token_cnt = block_num + 1;
2091 lg_crcv->obs_token[block_num] = coap_new_bin_const(token->s,
2092 token->length);
2093 if (lg_crcv->obs_token[block_num] == NULL)
2094 return NULL;
2095 } else if (observe_action == COAP_OBSERVE_CANCEL) {
2096 /* Use the token in lg_crcv */
2097 if (block_num < lg_crcv->obs_token_cnt) {
2098 return lg_crcv->obs_token[block_num];
2099 }
2100 }
2101 }
2102 return NULL;
2103}
2104
2105#if COAP_Q_BLOCK_SUPPORT
2107coap_send_q_block1(coap_session_t *session,
2108 coap_block_b_t block,
2109 coap_pdu_t *request,
2110 coap_send_pdu_t send_request) {
2111 /* Need to send up to MAX_PAYLOAD blocks if this is a Q_BLOCK1 */
2112 coap_lg_xmit_t *lg_xmit;
2113 uint64_t token_match =
2115 request->actual_token.length));
2116
2117 LL_FOREACH(session->lg_xmit, lg_xmit) {
2118 if (lg_xmit->option == COAP_OPTION_Q_BLOCK1 &&
2119 (token_match == STATE_TOKEN_BASE(lg_xmit->b.b1.state_token) ||
2120 token_match ==
2122 lg_xmit->b.b1.app_token->length))))
2123 break;
2124 /* try out the next one */
2125 }
2126 return coap_send_q_blocks(session, lg_xmit, block, request, send_request);
2127}
2128#endif /* COAP_Q_BLOCK_SUPPORT */
2129#endif /* COAP_CLIENT_SUPPORT */
2130
2131#if COAP_SERVER_SUPPORT
2132#if COAP_Q_BLOCK_SUPPORT
2133/*
2134 * response is always released before return IF COAP_SEND_INC_PDU
2135 */
2137coap_send_q_block2(coap_session_t *session,
2138 coap_resource_t *resource,
2139 const coap_string_t *query,
2140 coap_pdu_code_t request_method,
2141 coap_block_b_t block,
2142 coap_pdu_t *response,
2143 coap_send_pdu_t send_response) {
2144 /* Need to send up to MAX_PAYLOAD blocks if this is a Q_BLOCK2 */
2145 coap_lg_xmit_t *lg_xmit;
2146 coap_string_t empty = { 0, NULL};
2147
2148 LL_FOREACH(session->lg_xmit, lg_xmit) {
2149 if (lg_xmit->option == COAP_OPTION_Q_BLOCK2 &&
2150 resource == lg_xmit->b.b2.resource &&
2151 request_method == lg_xmit->b.b2.request_method &&
2152 coap_string_equal(query ? query : &empty,
2153 lg_xmit->b.b2.query ? lg_xmit->b.b2.query : &empty))
2154 break;
2155 }
2156 return coap_send_q_blocks(session, lg_xmit, block, response, send_response);
2157}
2158#endif /* COAP_Q_BLOCK_SUPPORT */
2159#endif /* COAP_SERVER_SUPPORT */
2160
2161#if COAP_CLIENT_SUPPORT
2162#if COAP_Q_BLOCK_SUPPORT
2163/*
2164 * Send out a test PDU for Q-Block.
2165 */
2167coap_block_test_q_block(coap_session_t *session, coap_pdu_t *actual) {
2168 coap_pdu_t *pdu;
2169 uint8_t token[8];
2170 size_t token_len;
2171 uint8_t buf[4];
2172 coap_mid_t mid;
2173
2174#if NDEBUG
2175 (void)actual;
2176#endif /* NDEBUG */
2177 assert(session->block_mode & COAP_BLOCK_TRY_Q_BLOCK &&
2178 session->type == COAP_SESSION_TYPE_CLIENT &&
2179 COAP_PDU_IS_REQUEST(actual));
2180
2181 coap_log_debug("Testing for Q-Block support\n");
2182 /* RFC9177 Section 4.1 when checking if available */
2184 coap_new_message_id_lkd(session),
2186 if (!pdu) {
2187 return COAP_INVALID_MID;
2188 }
2189
2190 coap_session_new_token(session, &token_len, token);
2191 coap_add_token(pdu, token_len, token);
2192 /* Use a resource that the server MUST support (.well-known/core) */
2194 11, (const u_char *)".well-known");
2196 4, (const u_char *)"core");
2197 /*
2198 * M needs to be unset as 'asking' for only the first block using
2199 * Q-Block2 as a test for server support.
2200 * See RFC9177 Section 4.4 Using the Q-Block2 Option.
2201 *
2202 * As the client is asking for 16 byte chunks, it is unlikely that
2203 * the .well-known/core response will be 16 bytes or less, so
2204 * if the server supports Q-Block, it will be forced to respond with
2205 * a Q-Block2, so the client can detect the server Q-Block support.
2206 */
2208 coap_encode_var_safe(buf, sizeof(buf),
2209 (0 << 4) | (0 << 3) | 0),
2210 buf);
2211 set_block_mode_probe_q(session->block_mode);
2212 mid = coap_send_internal(session, pdu);
2213 if (mid == COAP_INVALID_MID)
2214 return COAP_INVALID_MID;
2215 session->remote_test_mid = mid;
2216 return mid;
2217}
2218#endif /* COAP_Q_BLOCK_SUPPORT */
2219
2222 coap_lg_xmit_t *lg_xmit) {
2223 coap_block_b_t block;
2224 coap_lg_crcv_t *lg_crcv;
2225 uint64_t state_token = STATE_TOKEN_FULL(++session->tx_token, 1);
2226 size_t token_options = pdu->data ? (size_t)(pdu->data - pdu->token) :
2227 pdu->used_size;
2228 size_t data_len = lg_xmit ? lg_xmit->length :
2229 pdu->data ?
2230 pdu->used_size - (pdu->data - pdu->token) : 0;
2231
2232 lg_crcv = coap_malloc_type(COAP_LG_CRCV, sizeof(coap_lg_crcv_t));
2233
2234 if (lg_crcv == NULL)
2235 return NULL;
2236
2237 coap_log_debug("** %s: lg_crcv %p initialized - stateless token xxxx%012llx\n",
2238 coap_session_str(session), (void *)lg_crcv,
2239 STATE_TOKEN_BASE(state_token));
2240 memset(lg_crcv, 0, sizeof(coap_lg_crcv_t));
2241 lg_crcv->initial = 1;
2242 coap_ticks(&lg_crcv->last_used);
2243 /* Set up skeletal PDU to use as a basis for all the subsequent blocks */
2244 memcpy(&lg_crcv->pdu, pdu, sizeof(lg_crcv->pdu));
2245 /* Make sure that there is space for increased token + option change */
2246 lg_crcv->pdu.max_size = token_options + data_len + 9;
2247 lg_crcv->pdu.used_size = token_options + data_len;
2248 lg_crcv->pdu.token = coap_malloc_type(COAP_PDU_BUF,
2249 token_options + data_len + lg_crcv->pdu.max_hdr_size);
2250 if (!lg_crcv->pdu.token) {
2251 coap_block_delete_lg_crcv(session, lg_crcv);
2252 return NULL;
2253 }
2254 lg_crcv->pdu.token += lg_crcv->pdu.max_hdr_size;
2255 memcpy(lg_crcv->pdu.token, pdu->token, token_options);
2256 if (lg_crcv->pdu.data) {
2257 lg_crcv->pdu.data = lg_crcv->pdu.token + token_options;
2258 assert(pdu->data);
2259 memcpy(lg_crcv->pdu.data, lg_xmit ? lg_xmit->data : pdu->data, data_len);
2260 }
2261
2262 /* Need to keep original token for updating response PDUs */
2263 lg_crcv->app_token = coap_new_binary(pdu->actual_token.length);
2264 if (!lg_crcv->app_token) {
2265 coap_block_delete_lg_crcv(session, lg_crcv);
2266 return NULL;
2267 }
2268 memcpy(lg_crcv->app_token->s, pdu->actual_token.s, pdu->actual_token.length);
2269
2270 /* Need to set up a base token for actual communications if retries needed */
2271 lg_crcv->retry_counter = 1;
2272 lg_crcv->state_token = state_token;
2273
2274 if (pdu->code == COAP_REQUEST_CODE_FETCH) {
2275 coap_bin_const_t *new_token;
2276
2277 /* Need to save/restore Observe Token for large FETCH */
2278 new_token = track_fetch_observe(pdu, lg_crcv, 0, &pdu->actual_token);
2279 if (new_token)
2280 coap_update_token(pdu, new_token->length, new_token->s);
2281 }
2282
2283 if (coap_get_block_b(session, pdu, COAP_OPTION_BLOCK1, &block)) {
2284 /* In case it is there - must not be in continuing request PDUs */
2285 lg_crcv->o_block_option = COAP_OPTION_BLOCK1;
2286 lg_crcv->o_blk_size = block.aszx;
2287 coap_remove_option(&lg_crcv->pdu, COAP_OPTION_BLOCK1);
2288 }
2289
2290 if (lg_xmit)
2291 lg_xmit->lg_crcv = lg_crcv;
2292
2293 return lg_crcv;
2294}
2295
2296void
2298 coap_lg_crcv_t *lg_crcv) {
2299 size_t i;
2300 coap_lg_xmit_t *lg_xmit;
2301
2302#if (COAP_MAX_LOGGING_LEVEL < _COAP_LOG_DEBUG)
2303 (void)session;
2304#endif
2305 if (lg_crcv == NULL)
2306 return;
2307
2308 LL_FOREACH(session->lg_xmit, lg_xmit) {
2309 if (lg_xmit->lg_crcv == lg_crcv) {
2310 lg_xmit->lg_crcv = NULL;
2311 break;
2312 }
2313 }
2314
2315 if (lg_crcv->pdu.token)
2316 coap_free_type(COAP_PDU_BUF, lg_crcv->pdu.token - lg_crcv->pdu.max_hdr_size);
2318 coap_log_debug("** %s: lg_crcv %p released\n",
2319 coap_session_str(session), (void *)lg_crcv);
2320 coap_delete_binary(lg_crcv->app_token);
2321 for (i = 0; i < lg_crcv->obs_token_cnt; i++) {
2322 coap_delete_bin_const(lg_crcv->obs_token[i]);
2323 }
2325 coap_free_type(COAP_LG_CRCV, lg_crcv);
2326}
2327#endif /* COAP_CLIENT_SUPPORT */
2328
2329#if COAP_SERVER_SUPPORT
2330void
2332 coap_lg_srcv_t *lg_srcv) {
2333#if (COAP_MAX_LOGGING_LEVEL < _COAP_LOG_DEBUG)
2334 (void)session;
2335#endif
2336 if (lg_srcv == NULL)
2337 return;
2338
2342 coap_log_debug("** %s: lg_srcv %p released\n",
2343 coap_session_str(session), (void *)lg_srcv);
2344 coap_free_type(COAP_LG_SRCV, lg_srcv);
2345}
2346#endif /* COAP_SERVER_SUPPORT */
2347
2348void
2350 coap_lg_xmit_t *lg_xmit) {
2351 if (lg_xmit == NULL)
2352 return;
2353
2354 if (lg_xmit->release_func) {
2355 coap_lock_callback(session->context, lg_xmit->release_func(session, lg_xmit->app_ptr));
2356 }
2357 if (lg_xmit->pdu.token) {
2358 coap_free_type(COAP_PDU_BUF, lg_xmit->pdu.token - lg_xmit->pdu.max_hdr_size);
2359 }
2360 if (COAP_PDU_IS_REQUEST(&lg_xmit->pdu))
2361 coap_delete_binary(lg_xmit->b.b1.app_token);
2362 else
2363 coap_delete_string(lg_xmit->b.b2.query);
2364
2365 coap_log_debug("** %s: lg_xmit %p released\n",
2366 coap_session_str(session), (void *)lg_xmit);
2367 coap_free_type(COAP_LG_XMIT, lg_xmit);
2368}
2369
2370#if COAP_SERVER_SUPPORT
2371typedef struct {
2372 uint32_t num;
2373 int is_continue;
2374} send_track;
2375
2376static int
2377add_block_send(uint32_t num, int is_continue, send_track *out_blocks,
2378 uint32_t *count, uint32_t max_count) {
2379 uint32_t i;
2380
2381 for (i = 0; i < *count && *count < max_count; i++) {
2382 if (num == out_blocks[i].num)
2383 return 0;
2384 else if (num < out_blocks[i].num) {
2385 if (*count - i > 1)
2386 memmove(&out_blocks[i], &out_blocks[i+1], *count - i -1);
2387 out_blocks[i].num = num;
2388 out_blocks[i].is_continue = is_continue;
2389 (*count)++;
2390 return 1;
2391 }
2392 }
2393 if (*count < max_count) {
2394 out_blocks[i].num = num;
2395 out_blocks[i].is_continue = is_continue;
2396 (*count)++;
2397 return 1;
2398 }
2399 return 0;
2400}
2401
2402/*
2403 * Need to see if this is a request for the next block of a large body
2404 * transfer. If so, need to initiate the response with the next blocks
2405 * and not trouble the application.
2406 *
2407 * If additional responses needed, then these are expicitly sent out and
2408 * 'response' is updated to be the last response to be sent. There can be
2409 * multiple Q-Block2 in the request, as well as the 'Continue' Q-Block2
2410 * request.
2411 *
2412 * This is set up using coap_add_data_large_response_lkd()
2413 *
2414 * Server is sending a large data response to GET / observe (Block2)
2415 *
2416 * Return: 0 Call application handler
2417 * 1 Do not call application handler - just send the built response
2418 */
2419int
2421 coap_pdu_t *pdu,
2422 coap_pdu_t *response,
2423 coap_resource_t *resource,
2424 coap_string_t *query) {
2425 coap_lg_xmit_t *lg_xmit = NULL;
2426 coap_block_b_t block;
2427 coap_block_b_t alt_block;
2428 uint16_t block_opt = 0;
2429 send_track *out_blocks = NULL;
2430 const char *error_phrase;
2431 coap_opt_iterator_t opt_iter;
2432 size_t chunk;
2433 coap_opt_iterator_t opt_b_iter;
2434 coap_opt_t *option;
2435 uint32_t request_cnt, i;
2436 coap_opt_t *etag_opt = NULL;
2437 coap_pdu_t *out_pdu = response;
2438#if COAP_Q_BLOCK_SUPPORT
2439 size_t max_block;
2440
2441 /* Is client indicating that it supports Q_BLOCK2 ? */
2442 if (coap_get_block_b(session, pdu, COAP_OPTION_Q_BLOCK2, &block)) {
2443 if (!(session->block_mode & COAP_BLOCK_HAS_Q_BLOCK))
2444 set_block_mode_has_q(session->block_mode);
2445 block_opt = COAP_OPTION_Q_BLOCK2;
2446 }
2447#endif /* COAP_Q_BLOCK_SUPPORT */
2448 if (coap_get_block_b(session, pdu, COAP_OPTION_BLOCK2, &alt_block)) {
2449 if (block_opt) {
2450 coap_log_warn("Block2 and Q-Block2 cannot be in the same request\n");
2451 coap_add_data(response, sizeof("Both Block2 and Q-Block2 invalid")-1,
2452 (const uint8_t *)"Both Block2 and Q-Block2 invalid");
2453 response->code = COAP_RESPONSE_CODE(400);
2454 goto skip_app_handler;
2455 }
2456 block = alt_block;
2457 block_opt = COAP_OPTION_BLOCK2;
2458 }
2459 if (block_opt == 0)
2460 return 0;
2461 if (block.num == 0) {
2462 /* Get a fresh copy of the data */
2463 return 0;
2464 }
2465 lg_xmit = coap_find_lg_xmit_response(session, pdu, resource, query);
2466 if (lg_xmit == NULL)
2467 return 0;
2468
2469#if COAP_Q_BLOCK_SUPPORT
2470 out_blocks = coap_malloc_type(COAP_STRING, sizeof(send_track) * COAP_MAX_PAYLOADS(session));
2471#else /* ! COAP_Q_BLOCK_SUPPORT */
2472 out_blocks = coap_malloc_type(COAP_STRING, sizeof(send_track));
2473#endif /* ! COAP_Q_BLOCK_SUPPORT */
2474 if (!out_blocks) {
2475 goto internal_issue;
2476 }
2477
2478 /* lg_xmit (response) found */
2479
2480 etag_opt = coap_check_option(pdu, COAP_OPTION_ETAG, &opt_iter);
2481 if (etag_opt) {
2482 uint64_t etag = coap_decode_var_bytes8(coap_opt_value(etag_opt),
2483 coap_opt_length(etag_opt));
2484 if (etag != lg_xmit->b.b2.etag) {
2485 /* Not a match - pass up to a higher level */
2486 return 0;
2487 }
2488 out_pdu->code = COAP_RESPONSE_CODE(203);
2489 coap_ticks(&lg_xmit->last_sent);
2490 goto skip_app_handler;
2491 } else {
2492 out_pdu->code = lg_xmit->pdu.code;
2493 }
2494 coap_ticks(&lg_xmit->last_obs);
2495 lg_xmit->last_all_sent = 0;
2496
2497 chunk = (size_t)1 << (lg_xmit->blk_size + 4);
2498 if (block_opt) {
2499 if (block.bert) {
2500 coap_log_debug("found Block option, block is BERT, block nr. %u, M %d\n",
2501 block.num, block.m);
2502 } else {
2503 coap_log_debug("found Block option, block size is %u, block nr. %u, M %d\n",
2504 1 << (block.szx + 4), block.num, block.m);
2505 }
2506 if (block.bert == 0 && block.szx != lg_xmit->blk_size) {
2507 if (block.num == 0) {
2508 if ((lg_xmit->offset + chunk) % ((size_t)1 << (block.szx + 4)) == 0) {
2509 /*
2510 * Recompute the block number of the previous packet given
2511 * the new block size
2512 */
2513 block.num = (uint32_t)(((lg_xmit->offset + chunk) >> (block.szx + 4)) - 1);
2514 lg_xmit->blk_size = block.szx;
2515 chunk = (size_t)1 << (lg_xmit->blk_size + 4);
2516 lg_xmit->offset = block.num * chunk;
2517 coap_log_debug("new Block size is %u, block number %u completed\n",
2518 1 << (block.szx + 4), block.num);
2519 } else {
2520 coap_log_debug("ignoring request to increase Block size, "
2521 "next block is not aligned on requested block size "
2522 "boundary. (%zu x %u mod %u = %zu (which is not 0)\n",
2523 lg_xmit->offset/chunk + 1, (1 << (lg_xmit->blk_size + 4)),
2524 (1 << (block.szx + 4)),
2525 (lg_xmit->offset + chunk) % ((size_t)1 << (block.szx + 4)));
2526 }
2527 } else {
2528 coap_log_debug("ignoring request to change Block size from %u to %u\n",
2529 (1 << (lg_xmit->blk_size + 4)), (1 << (block.szx + 4)));
2530 block.szx = block.aszx = lg_xmit->blk_size;
2531 }
2532 }
2533 }
2534
2535 /*
2536 * Need to check if there are multiple Q-Block2 requests. If so, they
2537 * need to be sent out in order of requests with the final request being
2538 * handled as per singular Block 2 request.
2539 */
2540 request_cnt = 0;
2541#if COAP_Q_BLOCK_SUPPORT
2542 max_block = (lg_xmit->length + chunk - 1)/chunk;
2543#endif /* COAP_Q_BLOCK_SUPPORT */
2544 coap_option_iterator_init(pdu, &opt_b_iter, COAP_OPT_ALL);
2545 while ((option = coap_option_next(&opt_b_iter))) {
2546 uint32_t num;
2547 if (opt_b_iter.number != lg_xmit->option)
2548 continue;
2549 num = coap_opt_block_num(option);
2550 if (num > 0xFFFFF) /* 20 bits max for num */
2551 continue;
2552 if (block.aszx != COAP_OPT_BLOCK_SZX(option)) {
2553 coap_add_data(response,
2554 sizeof("Changing blocksize during request invalid")-1,
2555 (const uint8_t *)"Changing blocksize during request invalid");
2556 response->code = COAP_RESPONSE_CODE(400);
2557 goto skip_app_handler;
2558 }
2559#if COAP_Q_BLOCK_SUPPORT
2560 if (COAP_OPT_BLOCK_MORE(option) && lg_xmit->option == COAP_OPTION_Q_BLOCK2) {
2561 if ((num % COAP_MAX_PAYLOADS(session)) == 0) {
2562 if (num == 0) {
2563 /* This is a repeat request for everything - hmm */
2564 goto call_app_handler;
2565 }
2566 /* 'Continue' request */
2567 for (i = 0; i < COAP_MAX_PAYLOADS(session) &&
2568 num + i < max_block; i++) {
2569 add_block_send(num + i, 1, out_blocks, &request_cnt,
2570 COAP_MAX_PAYLOADS(session));
2571 lg_xmit->last_block = num + i;
2572 }
2573 } else {
2574 /* Requesting remaining payloads in this MAX_PAYLOADS */
2575 for (i = 0; i < COAP_MAX_PAYLOADS(session) -
2576 num % COAP_MAX_PAYLOADS(session) &&
2577 num + i < max_block; i++) {
2578 add_block_send(num + i, 0, out_blocks, &request_cnt,
2579 COAP_MAX_PAYLOADS(session));
2580 }
2581 }
2582 } else
2583 add_block_send(num, 0, out_blocks, &request_cnt,
2584 COAP_MAX_PAYLOADS(session));
2585#else /* ! COAP_Q_BLOCK_SUPPORT */
2586 add_block_send(num, 0, out_blocks, &request_cnt, 1);
2587 break;
2588#endif /* ! COAP_Q_BLOCK_SUPPORT */
2589 }
2590 if (request_cnt == 0) {
2591 /* Block2 or Q-Block2 not found - give them the first block */
2592 block.szx = lg_xmit->blk_size;
2593 lg_xmit->offset = 0;
2594 out_blocks[0].num = 0;
2595 out_blocks[0].is_continue = 0;
2596 request_cnt = 1;
2597 }
2598
2599 for (i = 0; i < request_cnt; i++) {
2600 uint8_t buf[8];
2601
2602 block.num = out_blocks[i].num;
2603 lg_xmit->offset = block.num * chunk;
2604
2605 if (i + 1 < request_cnt) {
2606 /* Need to set up a copy of the pdu to send */
2607 coap_opt_filter_t drop_options;
2608
2609 memset(&drop_options, 0, sizeof(coap_opt_filter_t));
2610 if (block.num != 0)
2612 if (out_blocks[i].is_continue) {
2613 out_pdu = coap_pdu_duplicate_lkd(&lg_xmit->pdu, session, lg_xmit->pdu.actual_token.length,
2614 lg_xmit->pdu.actual_token.s, &drop_options);
2615 } else {
2616 out_pdu = coap_pdu_duplicate_lkd(&lg_xmit->pdu, session, pdu->actual_token.length,
2617 pdu->actual_token.s, &drop_options);
2618 }
2619 if (!out_pdu) {
2620 goto internal_issue;
2621 }
2622 } else {
2623 if (out_blocks[i].is_continue)
2624 coap_update_token(response, lg_xmit->pdu.actual_token.length,
2625 lg_xmit->pdu.actual_token.s);
2626 /*
2627 * Copy the options across and then fix the block option
2628 *
2629 * Need to drop Observe option if Block2 and block.num != 0
2630 */
2631 coap_option_iterator_init(&lg_xmit->pdu, &opt_iter, COAP_OPT_ALL);
2632 while ((option = coap_option_next(&opt_iter))) {
2633 if (opt_iter.number == COAP_OPTION_OBSERVE && block.num != 0)
2634 continue;
2635 if (!coap_insert_option(response, opt_iter.number,
2636 coap_opt_length(option),
2637 coap_opt_value(option))) {
2638 goto internal_issue;
2639 }
2640 }
2641 out_pdu = response;
2642 }
2643 if (pdu->type == COAP_MESSAGE_NON)
2644 out_pdu->type = COAP_MESSAGE_NON;
2645 if (block.bert) {
2646 size_t token_options = pdu->data ? (size_t)(pdu->data - pdu->token) : pdu->used_size;
2647 block.m = (lg_xmit->length - lg_xmit->offset) >
2648 ((out_pdu->max_size - token_options) /1024) * 1024;
2649 } else {
2650 block.m = (lg_xmit->offset + chunk) < lg_xmit->length;
2651 }
2652 if (!coap_update_option(out_pdu, lg_xmit->option,
2654 sizeof(buf),
2655 (block.num << 4) |
2656 (block.m << 3) |
2657 block.aszx),
2658 buf)) {
2659 goto internal_issue;
2660 }
2661 if (!(lg_xmit->offset + chunk < lg_xmit->length)) {
2662 /* Last block - keep in cache for 4 * ACK_TIMOUT */
2663 coap_ticks(&lg_xmit->last_all_sent);
2664 }
2665 if (lg_xmit->b.b2.maxage_expire) {
2666 coap_tick_t now;
2667 coap_time_t rem;
2668
2669 if (!(lg_xmit->offset + chunk < lg_xmit->length)) {
2670 /* Last block - keep in cache for 4 * ACK_TIMOUT */
2671 coap_ticks(&lg_xmit->last_all_sent);
2672 }
2673 coap_ticks(&now);
2674 rem = coap_ticks_to_rt(now);
2675 if (lg_xmit->b.b2.maxage_expire > rem) {
2676 rem = lg_xmit->b.b2.maxage_expire - rem;
2677 } else {
2678 rem = 0;
2679 /* Entry needs to be expired */
2680 coap_ticks(&lg_xmit->last_all_sent);
2681 }
2684 sizeof(buf),
2685 rem),
2686 buf)) {
2687 goto internal_issue;
2688 }
2689 }
2690
2691 if (!etag_opt && !coap_add_block_b_data(out_pdu,
2692 lg_xmit->length,
2693 lg_xmit->data,
2694 &block)) {
2695 goto internal_issue;
2696 }
2697 if (i + 1 < request_cnt) {
2698 coap_ticks(&lg_xmit->last_sent);
2699 coap_send_internal(session, out_pdu);
2700 }
2701 }
2702 coap_ticks(&lg_xmit->last_payload);
2703 goto skip_app_handler;
2704#if COAP_Q_BLOCK_SUPPORT
2705call_app_handler:
2706 coap_free_type(COAP_STRING, out_blocks);
2707 return 0;
2708#endif /* COAP_Q_BLOCK_SUPPORT */
2709
2710internal_issue:
2711 response->code = COAP_RESPONSE_CODE(500);
2712 error_phrase = coap_response_phrase(response->code);
2713 coap_add_data(response, strlen(error_phrase),
2714 (const uint8_t *)error_phrase);
2715 /* Keep in cache for 4 * ACK_TIMOUT incase of retry */
2716 if (lg_xmit)
2717 coap_ticks(&lg_xmit->last_all_sent);
2718
2719skip_app_handler:
2720 coap_free_type(COAP_STRING, out_blocks);
2721 return 1;
2722}
2723#endif /* COAP_SERVER_SUPPORT */
2724
2725static int
2726update_received_blocks(coap_rblock_t *rec_blocks, uint32_t block_num, uint32_t block_m) {
2727 uint32_t i;
2728
2729 if (rec_blocks->total_blocks && block_num + 1 > rec_blocks->total_blocks) {
2730 /* received block number greater than Block No defined when More bit unset */
2731 return 0;
2732 }
2733
2734 /* Reset as there is activity */
2735 rec_blocks->retry = 0;
2736
2737 for (i = 0; i < rec_blocks->used; i++) {
2738 if (block_num >= rec_blocks->range[i].begin &&
2739 block_num <= rec_blocks->range[i].end)
2740 break;
2741
2742 if (block_num < rec_blocks->range[i].begin) {
2743 if (block_num + 1 == rec_blocks->range[i].begin) {
2744 rec_blocks->range[i].begin = block_num;
2745 } else {
2746 /* Need to insert a new range */
2747 if (rec_blocks->used == COAP_RBLOCK_CNT -1)
2748 /* Too many losses */
2749 return 0;
2750 memmove(&rec_blocks->range[i+1], &rec_blocks->range[i],
2751 (rec_blocks->used - i) * sizeof(rec_blocks->range[0]));
2752 rec_blocks->range[i].begin = rec_blocks->range[i].end = block_num;
2753 rec_blocks->used++;
2754 }
2755 break;
2756 }
2757 if (block_num == rec_blocks->range[i].end + 1) {
2758 rec_blocks->range[i].end = block_num;
2759 if (i + 1 < rec_blocks->used) {
2760 if (rec_blocks->range[i+1].begin == block_num + 1) {
2761 /* Merge the 2 ranges */
2762 rec_blocks->range[i].end = rec_blocks->range[i+1].end;
2763 if (i+2 < rec_blocks->used) {
2764 memmove(&rec_blocks->range[i+1], &rec_blocks->range[i+2],
2765 (rec_blocks->used - (i+2)) * sizeof(rec_blocks->range[0]));
2766 }
2767 rec_blocks->used--;
2768 }
2769 }
2770 break;
2771 }
2772 }
2773 if (i == rec_blocks->used) {
2774 if (rec_blocks->used == COAP_RBLOCK_CNT -1)
2775 /* Too many losses */
2776 return 0;
2777 rec_blocks->range[i].begin = rec_blocks->range[i].end = block_num;
2778 rec_blocks->used++;
2779 }
2780 if (!block_m)
2781 rec_blocks->total_blocks = block_num + 1;
2782
2783 coap_ticks(&rec_blocks->last_seen);
2784 return 1;
2785}
2786
2787#if COAP_SERVER_SUPPORT
2788/*
2789 * Need to check if this is a large PUT / POST etc. using multiple blocks
2790 *
2791 * Server receiving PUT/POST etc. of a large amount of data (Block1)
2792 *
2793 * Return: 0 Call application handler
2794 * 1 Do not call application handler - just send the built response
2795 */
2796int
2798 coap_session_t *session,
2799 coap_pdu_t *pdu,
2800 coap_pdu_t *response,
2801 coap_resource_t *resource,
2802 coap_string_t *uri_path,
2803 coap_opt_t *observe,
2804 int *added_block,
2805 coap_lg_srcv_t **pfree_lg_srcv) {
2806 size_t length = 0;
2807 const uint8_t *data = NULL;
2808 size_t offset = 0;
2809 size_t total = 0;
2810 coap_block_b_t block;
2811 coap_opt_iterator_t opt_iter;
2812 uint16_t block_option = 0;
2813 coap_lg_srcv_t *lg_srcv;
2814 coap_opt_t *size_opt;
2815 coap_opt_t *fmt_opt;
2816 uint16_t fmt;
2817 coap_opt_t *rtag_opt;
2818 size_t rtag_length;
2819 const uint8_t *rtag;
2820 uint32_t max_block_szx;
2821 int update_data;
2822 unsigned int saved_num;
2823 size_t saved_offset;
2824
2825 *added_block = 0;
2826 *pfree_lg_srcv = NULL;
2827 coap_get_data_large(pdu, &length, &data, &offset, &total);
2828 pdu->body_offset = 0;
2829 pdu->body_total = length;
2830
2831 if (coap_get_block_b(session, pdu, COAP_OPTION_BLOCK1, &block)) {
2832 block_option = COAP_OPTION_BLOCK1;
2833#if COAP_Q_BLOCK_SUPPORT
2834 if (coap_check_option(pdu, COAP_OPTION_Q_BLOCK1, &opt_iter)) {
2835 /* Cannot handle Q-Block1 as well */
2836 coap_add_data(response, sizeof("Block1 + Q-Block1 together")-1,
2837 (const uint8_t *)"Block1 + Q-Block1 together");
2838 response->code = COAP_RESPONSE_CODE(402);
2839 goto skip_app_handler;
2840 }
2841#endif /* COAP_Q_BLOCK_SUPPORT */
2842 }
2843#if COAP_Q_BLOCK_SUPPORT
2844 else if (coap_get_block_b(session, pdu, COAP_OPTION_Q_BLOCK1, &block)) {
2845 block_option = COAP_OPTION_Q_BLOCK1;
2846 set_block_mode_has_q(session->block_mode);
2847 }
2848#endif /* COAP_Q_BLOCK_SUPPORT */
2849 if (!block_option ||
2850 (block_option == COAP_OPTION_BLOCK1 && block.num == 0 && block.m == 0)) {
2851 /* Not blocked, or a single block */
2852 goto call_app_handler;
2853 }
2854
2855 size_opt = coap_check_option(pdu,
2857 &opt_iter);
2858 fmt_opt = coap_check_option(pdu,
2860 &opt_iter);
2861 fmt = fmt_opt ? coap_decode_var_bytes(coap_opt_value(fmt_opt),
2862 coap_opt_length(fmt_opt)) :
2864 rtag_opt = coap_check_option(pdu,
2866 &opt_iter);
2867 rtag_length = rtag_opt ? coap_opt_length(rtag_opt) : 0;
2868 rtag = rtag_opt ? coap_opt_value(rtag_opt) : NULL;
2869
2870 if (length > block.chunk_size) {
2871 coap_log_debug("block: Oversized packet - reduced to %"PRIu32" from %zu\n",
2872 block.chunk_size, length);
2873 length = block.chunk_size;
2874 } else if (!block.bert && block.m && length != block.chunk_size) {
2875 coap_log_info("block: Undersized packet chunk %"PRIu32" got %zu\n",
2876 block.chunk_size, length);
2877 response->code = COAP_RESPONSE_CODE(400);
2878 goto skip_app_handler;
2879 }
2880 total = size_opt ? coap_decode_var_bytes(coap_opt_value(size_opt),
2881 coap_opt_length(size_opt)) : 0;
2882 offset = block.num << (block.szx + 4);
2883
2884 if (!(session->block_mode &
2887#else /* COAP_Q_BLOCK_SUPPORT */
2889#endif /* COAP_Q_BLOCK_SUPPORT */
2890 && !block.bert) {
2891 uint8_t buf[4];
2892
2893 /* Ask for the next block */
2894 coap_insert_option(response, block_option,
2895 coap_encode_var_safe(buf, sizeof(buf),
2896 (block.num << 4) |
2897 (block.m << 3) |
2898 block.aszx),
2899 buf);
2900 /* Not re-assembling or checking for receipt order */
2901 pdu->body_data = data;
2902 pdu->body_length = length;
2903 pdu->body_offset = offset;
2904 if (total < (length + offset + (block.m ? 1 : 0)))
2905 total = length + offset + (block.m ? 1 : 0);
2906 pdu->body_total = total;
2907 *added_block = block.m;
2908 /* The application is responsible for returning the correct 2.01/2.04/2.31 etc. */
2909 goto call_app_handler;
2910 }
2911
2912 /*
2913 * locate the lg_srcv
2914 */
2915 LL_FOREACH(session->lg_srcv, lg_srcv) {
2916 if (rtag_opt || lg_srcv->rtag_set == 1) {
2917 if (!(rtag_opt && lg_srcv->rtag_set == 1))
2918 continue;
2919 if (lg_srcv->rtag_length != rtag_length ||
2920 memcmp(lg_srcv->rtag, rtag, rtag_length) != 0)
2921 continue;
2922 }
2923 if (resource == lg_srcv->resource) {
2924 break;
2925 }
2926 if ((lg_srcv->resource == context->unknown_resource ||
2927 resource == context->proxy_uri_resource) &&
2928 coap_string_equal(uri_path, lg_srcv->uri_path))
2929 break;
2930 }
2931
2932 if (!lg_srcv && block.num != 0 && session->block_mode & COAP_BLOCK_NOT_RANDOM_BLOCK1) {
2933 coap_add_data(response, sizeof("Missing block 0")-1,
2934 (const uint8_t *)"Missing block 0");
2935 response->code = COAP_RESPONSE_CODE(408);
2936 goto skip_app_handler;
2937 }
2938
2939 if (!lg_srcv) {
2940 /* Allocate lg_srcv to use for tracking */
2941 lg_srcv = coap_malloc_type(COAP_LG_SRCV, sizeof(coap_lg_srcv_t));
2942 if (lg_srcv == NULL) {
2943 coap_add_data(response, sizeof("Memory issue")-1,
2944 (const uint8_t *)"Memory issue");
2945 response->code = COAP_RESPONSE_CODE(500);
2946 goto skip_app_handler;
2947 }
2948 coap_log_debug("** %s: lg_srcv %p initialized\n",
2949 coap_session_str(session), (void *)lg_srcv);
2950 memset(lg_srcv, 0, sizeof(coap_lg_srcv_t));
2951 lg_srcv->resource = resource;
2952 if (resource == context->unknown_resource ||
2953 resource == context->proxy_uri_resource)
2954 lg_srcv->uri_path = coap_new_str_const(uri_path->s, uri_path->length);
2955 lg_srcv->content_format = fmt;
2956 lg_srcv->total_len = total;
2957 max_block_szx = COAP_BLOCK_MAX_SIZE_GET(session->block_mode);
2958 if (!block.bert && block.num == 0 && max_block_szx != 0 &&
2959 max_block_szx < block.szx) {
2960 lg_srcv->szx = max_block_szx;
2961 } else {
2962 lg_srcv->szx = block.szx;
2963 }
2964 lg_srcv->block_option = block_option;
2965 if (observe) {
2966 lg_srcv->observe_length = min(coap_opt_length(observe), 3);
2967 memcpy(lg_srcv->observe, coap_opt_value(observe), lg_srcv->observe_length);
2968 lg_srcv->observe_set = 1;
2969 }
2970 if (rtag_opt) {
2971 lg_srcv->rtag_length = coap_opt_length(rtag_opt);
2972 memcpy(lg_srcv->rtag, coap_opt_value(rtag_opt), lg_srcv->rtag_length);
2973 lg_srcv->rtag_set = 1;
2974 }
2975 lg_srcv->body_data = NULL;
2976 LL_PREPEND(session->lg_srcv, lg_srcv);
2977 }
2978 coap_ticks(&lg_srcv->last_used);
2979
2980 if (block_option == COAP_OPTION_BLOCK1 &&
2982 !check_if_next_block(&lg_srcv->rec_blocks, block.num)) {
2983 coap_add_data(response, sizeof("Missing interim block")-1,
2984 (const uint8_t *)"Missing interim block");
2985 response->code = COAP_RESPONSE_CODE(408);
2986 goto skip_app_handler;
2987 }
2988
2989 if (fmt != lg_srcv->content_format) {
2990 coap_add_data(response, sizeof("Content-Format mismatch")-1,
2991 (const uint8_t *)"Content-Format mismatch");
2992 response->code = COAP_RESPONSE_CODE(408);
2993 goto free_lg_srcv;
2994 }
2995
2996#if COAP_Q_BLOCK_SUPPORT
2997 if (block_option == COAP_OPTION_Q_BLOCK1) {
2998 if (total != lg_srcv->total_len) {
2999 coap_add_data(response, sizeof("Size1 mismatch")-1,
3000 (const uint8_t *)"Size1 mismatch");
3001 response->code = COAP_RESPONSE_CODE(408);
3002 goto free_lg_srcv;
3003 }
3006 pdu->actual_token.length);
3007 }
3008#endif /* COAP_Q_BLOCK_SUPPORT */
3009
3010 lg_srcv->last_mid = pdu->mid;
3011 lg_srcv->last_type = pdu->type;
3012
3013 update_data = 0;
3014 saved_num = block.num;
3015 saved_offset = offset;
3016
3017 while (offset < saved_offset + length) {
3018 if (!check_if_received_block(&lg_srcv->rec_blocks, block.num)) {
3019 /* Update list of blocks received */
3020 if (!update_received_blocks(&lg_srcv->rec_blocks, block.num, block.m)) {
3022 coap_add_data(response, sizeof("Too many missing blocks")-1,
3023 (const uint8_t *)"Too many missing blocks");
3024 response->code = COAP_RESPONSE_CODE(408);
3025 goto free_lg_srcv;
3026 }
3027 update_data = 1;
3028 }
3029 block.num++;
3030 offset = block.num << (block.szx + 4);
3031 }
3032 block.num--;
3033
3034 if (update_data) {
3035 /* Update saved data */
3036#if COAP_Q_BLOCK_SUPPORT
3037 lg_srcv->rec_blocks.processing_payload_set =
3038 block.num / COAP_MAX_PAYLOADS(session);
3039#endif /* COAP_Q_BLOCK_SUPPORT */
3040 if (lg_srcv->total_len < saved_offset + length) {
3041 lg_srcv->total_len = saved_offset + length;
3042 }
3043 lg_srcv->body_data = coap_block_build_body_lkd(lg_srcv->body_data, length, data,
3044 saved_offset, lg_srcv->total_len);
3045 if (!lg_srcv->body_data) {
3046 coap_add_data(response, sizeof("Memory issue")-1,
3047 (const uint8_t *)"Memory issue");
3048 response->code = COAP_RESPONSE_CODE(500);
3049 goto skip_app_handler;
3050 }
3051 }
3052
3053 if (block.m ||
3054 !check_all_blocks_in(&lg_srcv->rec_blocks)) {
3055 /* Not all the payloads of the body have arrived */
3056 if (block.m) {
3057 uint8_t buf[4];
3058
3059#if COAP_Q_BLOCK_SUPPORT
3060 if (block_option == COAP_OPTION_Q_BLOCK1) {
3061 if (check_all_blocks_in(&lg_srcv->rec_blocks)) {
3062 goto give_app_data;
3063 }
3064 if (lg_srcv->rec_blocks.used == 1 &&
3065 (lg_srcv->rec_blocks.range[0].end % COAP_MAX_PAYLOADS(session)) + 1
3066 == COAP_MAX_PAYLOADS(session)) {
3067 if (block.num != lg_srcv->rec_blocks.range[0].end) {
3068 /* Blocks could arrive in wrong order */
3069 block.num = lg_srcv->rec_blocks.range[0].end;
3070 goto skip_app_handler;
3071 }
3072 } else {
3073 /* The remote end will be sending the next one unless this
3074 is a MAX_PAYLOADS and all previous have been received */
3075 goto skip_app_handler;
3076 }
3077 if (COAP_PROTO_RELIABLE(session->proto) ||
3078 pdu->type != COAP_MESSAGE_NON)
3079 goto skip_app_handler;
3080 }
3081#endif /* COAP_Q_BLOCK_SUPPORT */
3082
3083 /* Check to see if block size is getting forced down */
3084 max_block_szx = COAP_BLOCK_MAX_SIZE_GET(session->block_mode);
3085 if (!block.bert && saved_num == 0 && max_block_szx != 0 &&
3086 max_block_szx < block.aszx) {
3087 block.aszx = max_block_szx;
3088 }
3089
3090 /*
3091 * If the last block has been seen, packets are coming in in
3092 * random order. If all blocks are now in, then need to send
3093 * complete payload to application and acknowledge this current
3094 * block.
3095 */
3096 if ((total == 0 && block.m) || !check_all_blocks_in(&lg_srcv->rec_blocks)) {
3097 /* Ask for the next block */
3098 coap_insert_option(response, block_option,
3099 coap_encode_var_safe(buf, sizeof(buf),
3100 (saved_num << 4) |
3101 (block.m << 3) |
3102 block.aszx),
3103 buf);
3104 response->code = COAP_RESPONSE_CODE(231);
3105 } else {
3106 /* Need to separately respond to this request */
3107 coap_pdu_t *tmp_pdu = coap_pdu_duplicate_lkd(response,
3108 session,
3109 response->actual_token.length,
3110 response->actual_token.s,
3111 NULL);
3112 if (tmp_pdu) {
3113 tmp_pdu->code = COAP_RESPONSE_CODE(231);
3114 coap_send_internal(session, tmp_pdu);
3115 }
3116 if (lg_srcv->last_token) {
3117 coap_update_token(response, lg_srcv->last_token->length, lg_srcv->last_token->s);
3118 coap_update_token(pdu, lg_srcv->last_token->length, lg_srcv->last_token->s);
3119 }
3120 /* Pass the assembled pdu and body to the application */
3121 goto give_app_data;
3122 }
3123 } else {
3124 /* block.m Block More option not set. Some outstanding blocks */
3125#if COAP_Q_BLOCK_SUPPORT
3126 if (block_option != COAP_OPTION_Q_BLOCK1) {
3127#endif /* COAP_Q_BLOCK_SUPPORT */
3128 /* Last chunk - but not all in */
3129 coap_ticks(&lg_srcv->last_used);
3130 lg_srcv->no_more_seen = 1;
3133 pdu->actual_token.length);
3134
3135 /*
3136 * Need to just ACK (no response code) to handle client's NSTART.
3137 * When final missing block comes in, we will pass all the data
3138 * for processing so a 2.01, 2.04 etc. code can be generated
3139 * and responded to as a separate response "RFC7252 5.2.2. Separate"
3140 * If missing block(s) do not come in, then will generate a 4.08
3141 * when lg_srcv times out.
3142 * Fall through to skip_app_handler.
3143 */
3144#if COAP_Q_BLOCK_SUPPORT
3145 }
3146#endif /* COAP_Q_BLOCK_SUPPORT */
3147 }
3148 goto skip_app_handler;
3149 }
3150
3151 /*
3152 * Entire payload received.
3153 * Remove the Block1 option as passing all of the data to
3154 * application layer. Add back in observe option if appropriate.
3155 * Adjust all other information.
3156 */
3157give_app_data:
3158 if (lg_srcv->observe_set) {
3160 lg_srcv->observe_length, lg_srcv->observe);
3161 }
3162 coap_remove_option(pdu, block_option);
3163 if (lg_srcv->body_data) {
3164 pdu->body_data = lg_srcv->body_data->s;
3165 pdu->body_length = lg_srcv->total_len;
3166 } else {
3167 pdu->body_data = NULL;
3168 pdu->body_length = 0;
3169 }
3170 pdu->body_offset = 0;
3171 pdu->body_total = lg_srcv->total_len;
3172 coap_log_debug("Server app version of updated PDU\n");
3174 *pfree_lg_srcv = lg_srcv;
3175
3176call_app_handler:
3177 return 0;
3178
3179free_lg_srcv:
3180 LL_DELETE(session->lg_srcv, lg_srcv);
3181 coap_block_delete_lg_srcv(session, lg_srcv);
3182
3183skip_app_handler:
3184 return 1;
3185}
3186#endif /* COAP_SERVER_SUPPORT */
3187
3188#if COAP_CLIENT_SUPPORT
3189#if COAP_Q_BLOCK_SUPPORT
3190static uint32_t
3191derive_cbor_value(const uint8_t **bp, size_t rem_len) {
3192 uint32_t value = **bp & 0x1f;
3193 (*bp)++;
3194 if (value < 24) {
3195 return value;
3196 } else if (value == 24) {
3197 if (rem_len < 2)
3198 return (uint32_t)-1;
3199 value = **bp;
3200 (*bp)++;
3201 return value;
3202 } else if (value == 25) {
3203 if (rem_len < 3)
3204 return (uint32_t)-1;
3205 value = **bp << 8;
3206 (*bp)++;
3207 value |= **bp;
3208 (*bp)++;
3209 return value;
3210 }
3211 if (rem_len < 4)
3212 return (uint32_t)-1;
3213 value = **bp << 24;
3214 (*bp)++;
3215 value |= **bp << 16;
3216 (*bp)++;
3217 value |= **bp << 8;
3218 (*bp)++;
3219 value |= **bp;
3220 (*bp)++;
3221 return value;
3222}
3223#endif /* COAP_Q_BLOCK_SUPPORT */
3224
3225static int
3226check_freshness(coap_session_t *session, coap_pdu_t *rcvd, coap_pdu_t *sent,
3227 coap_lg_xmit_t *lg_xmit, coap_lg_crcv_t *lg_crcv) {
3228 /* Check for Echo option for freshness */
3229 coap_opt_iterator_t opt_iter;
3230 coap_opt_t *opt = coap_check_option(rcvd, COAP_OPTION_ECHO, &opt_iter);
3231
3232 if (opt) {
3233 if (sent || lg_xmit || lg_crcv) {
3234 /* Need to retransmit original request with Echo option added */
3235 coap_pdu_t *echo_pdu;
3236 coap_mid_t mid;
3237 const uint8_t *data;
3238 size_t data_len;
3239 int have_data = 0;
3240 uint8_t ltoken[8];
3241 size_t ltoken_len;
3242 uint64_t token;
3243
3244 if (sent) {
3245 if (coap_get_data(sent, &data_len, &data))
3246 have_data = 1;
3247 } else if (lg_xmit) {
3248 sent = &lg_xmit->pdu;
3249 if (lg_xmit->length) {
3250 size_t blk_size = (size_t)1 << (lg_xmit->blk_size + 4);
3251 size_t offset = (lg_xmit->last_block + 1) * blk_size;
3252 have_data = 1;
3253 data = &lg_xmit->data[offset];
3254 data_len = (lg_xmit->length - offset) > blk_size ? blk_size :
3255 lg_xmit->length - offset;
3256 }
3257 } else { /* lg_crcv */
3258 sent = &lg_crcv->pdu;
3259 if (coap_get_data(sent, &data_len, &data))
3260 have_data = 1;
3261 }
3262 if (lg_xmit) {
3263 token = STATE_TOKEN_FULL(lg_xmit->b.b1.state_token,
3264 ++lg_xmit->b.b1.count);
3265 } else {
3266 token = STATE_TOKEN_FULL(lg_crcv->state_token,
3267 ++lg_crcv->retry_counter);
3268 }
3269 ltoken_len = coap_encode_var_safe8(ltoken, sizeof(token), token);
3270 echo_pdu = coap_pdu_duplicate_lkd(sent, session, ltoken_len, ltoken, NULL);
3271 if (!echo_pdu)
3272 return 0;
3273 if (!coap_insert_option(echo_pdu, COAP_OPTION_ECHO,
3274 coap_opt_length(opt), coap_opt_value(opt)))
3275 goto not_sent;
3276 if (have_data) {
3277 coap_add_data(echo_pdu, data_len, data);
3278 }
3279 /* Need to track Observe token change if Observe */
3280 track_fetch_observe(echo_pdu, lg_crcv, 0, &echo_pdu->actual_token);
3281#if COAP_OSCORE_SUPPORT
3282 if (session->oscore_encryption &&
3283 (opt = coap_check_option(echo_pdu, COAP_OPTION_OBSERVE, &opt_iter)) &&
3285 /* Need to update the base PDU's Token for closing down Observe */
3286 if (lg_xmit) {
3287 lg_xmit->b.b1.state_token = token;
3288 } else {
3289 lg_crcv->state_token = token;
3290 }
3291 }
3292#endif /* COAP_OSCORE_SUPPORT */
3293 mid = coap_send_internal(session, echo_pdu);
3294 if (mid == COAP_INVALID_MID)
3295 goto not_sent;
3296 return 1;
3297 } else {
3298 /* Need to save Echo option value to add to next reansmission */
3299not_sent:
3300 coap_delete_bin_const(session->echo);
3301 session->echo = coap_new_bin_const(coap_opt_value(opt),
3302 coap_opt_length(opt));
3303 }
3304 }
3305 return 0;
3306}
3307
3308static void
3309track_echo(coap_session_t *session, coap_pdu_t *rcvd) {
3310 coap_opt_iterator_t opt_iter;
3311 coap_opt_t *opt = coap_check_option(rcvd, COAP_OPTION_ECHO, &opt_iter);
3312
3313 if (opt) {
3314 coap_delete_bin_const(session->echo);
3315 session->echo = coap_new_bin_const(coap_opt_value(opt),
3316 coap_opt_length(opt));
3317 }
3318}
3319
3320/*
3321 * Need to see if this is a response to a large body request transfer. If so,
3322 * need to initiate the request containing the next block and not trouble the
3323 * application. Note that Token must unique per request/response.
3324 *
3325 * Client receives large data acknowledgement from server (Block1)
3326 *
3327 * This is set up using coap_add_data_large_request_lkd()
3328 *
3329 * Client is using GET etc.
3330 *
3331 * Return: 0 Call application handler
3332 * 1 Do not call application handler - just send the built response
3333 */
3334int
3336 coap_pdu_t *rcvd) {
3337 coap_lg_xmit_t *lg_xmit;
3338 coap_lg_xmit_t *q;
3339 uint64_t token_match =
3341 rcvd->actual_token.length));
3342 coap_lg_crcv_t *lg_crcv = NULL;
3343
3344 LL_FOREACH_SAFE(session->lg_xmit, lg_xmit, q) {
3345 if (!COAP_PDU_IS_REQUEST(&lg_xmit->pdu) ||
3346 (token_match != STATE_TOKEN_BASE(lg_xmit->b.b1.state_token) &&
3347 !coap_binary_equal(&rcvd->actual_token, lg_xmit->b.b1.app_token))) {
3348 /* try out the next one */
3349 continue;
3350 }
3351 /* lg_xmit found */
3352 size_t chunk = (size_t)1 << (lg_xmit->blk_size + 4);
3353 coap_block_b_t block;
3354
3355 lg_crcv = lg_xmit->lg_crcv;
3356 if (lg_crcv)
3357 coap_ticks(&lg_crcv->last_used);
3358
3359 if (COAP_RESPONSE_CLASS(rcvd->code) == 2 &&
3360 coap_get_block_b(session, rcvd, lg_xmit->option, &block)) {
3361
3362 if (block.bert) {
3363 coap_log_debug("found Block option, block is BERT, block nr. %u (%zu)\n",
3364 block.num, lg_xmit->b.b1.bert_size);
3365 } else {
3366 coap_log_debug("found Block option, block size is %u, block nr. %u\n",
3367 1 << (block.szx + 4), block.num);
3368 }
3369 if (block.szx != lg_xmit->blk_size) {
3370 if (block.szx > lg_xmit->blk_size) {
3371 coap_log_info("ignoring request to increase Block size, "
3372 "(%u > %u)\n",
3373 1 << (block.szx + 4), 1 << (lg_xmit->blk_size + 4));
3374 } else if ((lg_xmit->offset + chunk) % ((size_t)1 << (block.szx + 4)) == 0) {
3375 /*
3376 * Recompute the block number of the previous packet given the
3377 * new block size
3378 */
3379 block.num = (uint32_t)(((lg_xmit->offset + chunk) >> (block.szx + 4)) - 1);
3380 lg_xmit->blk_size = block.szx;
3381 chunk = (size_t)1 << (lg_xmit->blk_size + 4);
3382 lg_xmit->offset = block.num * chunk;
3383 coap_log_debug("new Block size is %u, block number %u completed\n",
3384 1 << (block.szx + 4), block.num);
3385 block.bert = 0;
3386 block.aszx = block.szx;
3387 } else {
3388 coap_log_debug("ignoring request to increase Block size, "
3389 "next block is not aligned on requested block size boundary. "
3390 "(%zu x %u mod %u = %zu != 0)\n",
3391 lg_xmit->offset/chunk + 1, (1 << (lg_xmit->blk_size + 4)),
3392 (1 << (block.szx + 4)),
3393 (lg_xmit->offset + chunk) % ((size_t)1 << (block.szx + 4)));
3394 }
3395 }
3396 track_echo(session, rcvd);
3397 if (lg_xmit->last_block == (int)block.num &&
3398 lg_xmit->option != COAP_OPTION_Q_BLOCK1) {
3399 /*
3400 * Duplicate Block1 ACK
3401 *
3402 * RFCs not clear here, but on a lossy connection, there could
3403 * be multiple Block1 ACKs, causing the client to retransmit the
3404 * same block multiple times, or the server retransmitting the
3405 * same ACK.
3406 *
3407 * Once a block has been ACKd, there is no need to retransmit it.
3408 */
3409 return 1;
3410 }
3411 if (block.bert)
3412 block.num += (unsigned int)(lg_xmit->b.b1.bert_size / 1024 - 1);
3413 lg_xmit->last_block = block.num;
3414 lg_xmit->offset = (block.num + 1) * chunk;
3415 if (lg_xmit->offset < lg_xmit->length) {
3416 /* Build the next PDU request based off the skeletal PDU */
3417 uint8_t buf[8];
3418 coap_pdu_t *pdu;
3419 uint64_t token = STATE_TOKEN_FULL(lg_xmit->b.b1.state_token, ++lg_xmit->b.b1.count);
3420 size_t len = coap_encode_var_safe8(buf, sizeof(token), token);
3421
3422 if (lg_xmit->pdu.code == COAP_REQUEST_CODE_FETCH) {
3423 /* Need to handle Observe for large FETCH */
3424 if (lg_crcv) {
3425 if (coap_binary_equal(lg_xmit->b.b1.app_token, lg_crcv->app_token)) {
3426 coap_bin_const_t *new_token;
3427 coap_bin_const_t ctoken = { len, buf };
3428
3429 /* Need to save/restore Observe Token for large FETCH */
3430 new_token = track_fetch_observe(&lg_xmit->pdu, lg_crcv, block.num + 1,
3431 &ctoken);
3432 if (new_token) {
3433 assert(len <= sizeof(buf));
3434 len = new_token->length;
3435 memcpy(buf, new_token->s, len);
3436 }
3437 }
3438 }
3439 }
3440 pdu = coap_pdu_duplicate_lkd(&lg_xmit->pdu, session, len, buf, NULL);
3441 if (!pdu)
3442 goto fail_body;
3443
3444 block.num++;
3445 if (block.bert) {
3446 size_t token_options = pdu->data ? (size_t)(pdu->data - pdu->token) :
3447 pdu->used_size;
3448 block.m = (lg_xmit->length - lg_xmit->offset) >
3449 ((pdu->max_size - token_options) /1024) * 1024;
3450 } else {
3451 block.m = (lg_xmit->offset + chunk) < lg_xmit->length;
3452 }
3453 coap_update_option(pdu, lg_xmit->option,
3454 coap_encode_var_safe(buf, sizeof(buf),
3455 (block.num << 4) |
3456 (block.m << 3) |
3457 block.aszx),
3458 buf);
3459
3460 if (!coap_add_block_b_data(pdu,
3461 lg_xmit->length,
3462 lg_xmit->data,
3463 &block))
3464 goto fail_body;
3465 lg_xmit->b.b1.bert_size = block.chunk_size;
3466 coap_ticks(&lg_xmit->last_sent);
3467#if COAP_Q_BLOCK_SUPPORT
3468 if (lg_xmit->option == COAP_OPTION_Q_BLOCK1 &&
3469 pdu->type == COAP_MESSAGE_NON) {
3470 if (coap_send_q_block1(session, block, pdu,
3471 COAP_SEND_INC_PDU) == COAP_INVALID_MID)
3472 goto fail_body;
3473 return 1;
3474 } else if (coap_send_internal(session, pdu) == COAP_INVALID_MID)
3475 goto fail_body;
3476#else /* ! COAP_Q_BLOCK_SUPPORT */
3477 if (coap_send_internal(session, pdu) == COAP_INVALID_MID)
3478 goto fail_body;
3479#endif /* ! COAP_Q_BLOCK_SUPPORT */
3480 return 1;
3481 }
3482 } else if (COAP_RESPONSE_CLASS(rcvd->code) == 2) {
3483 /*
3484 * Not a block response asking for the next block.
3485 * Could be an Observe response overlapping with block FETCH doing
3486 * Observe cancellation.
3487 */
3488 coap_opt_iterator_t opt_iter;
3489 coap_opt_t *obs_opt;
3490 int observe_action = -1;
3491
3492 if (lg_xmit->pdu.code != COAP_REQUEST_CODE_FETCH) {
3493 goto lg_xmit_finished;
3494 }
3495 obs_opt = coap_check_option(&lg_xmit->pdu,
3497 &opt_iter);
3498 if (obs_opt) {
3499 observe_action = coap_decode_var_bytes(coap_opt_value(obs_opt),
3500 coap_opt_length(obs_opt));
3501 }
3502 if (observe_action != COAP_OBSERVE_CANCEL) {
3503 goto lg_xmit_finished;
3504 }
3505 obs_opt = coap_check_option(rcvd,
3507 &opt_iter);
3508 if (obs_opt) {
3509 return 0;
3510 }
3511 goto lg_xmit_finished;
3512 } else if (rcvd->code == COAP_RESPONSE_CODE(401)) {
3513 if (check_freshness(session, rcvd, sent, lg_xmit, NULL))
3514 return 1;
3515#if COAP_Q_BLOCK_SUPPORT
3516 } else if (rcvd->code == COAP_RESPONSE_CODE(402)) {
3517 /* Q-Block1 or Q-Block2 not present in p - duplicate error ? */
3518 if (coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK2, &block) ||
3519 coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK1, &block))
3520 return 1;
3521 } else if (rcvd->code == COAP_RESPONSE_CODE(408) &&
3522 lg_xmit->option == COAP_OPTION_Q_BLOCK1) {
3523 size_t length;
3524 const uint8_t *data;
3525 coap_opt_iterator_t opt_iter;
3526 coap_opt_t *fmt_opt = coap_check_option(rcvd,
3528 &opt_iter);
3529 uint16_t fmt = fmt_opt ?
3531 coap_opt_length(fmt_opt)) :
3533
3535 goto fail_body;
3536
3537 if (COAP_PROTO_RELIABLE(session->proto) ||
3538 rcvd->type != COAP_MESSAGE_NON) {
3539 coap_log_debug("Unexpected 4.08 - protocol violation - ignore\n");
3540 return 1;
3541 }
3542
3543 if (coap_get_data(rcvd, &length, &data)) {
3544 /* Need to decode CBOR to work out what blocks to re-send */
3545 const uint8_t *bp = data;
3546 uint32_t i;
3547 uint8_t buf[8];
3548 coap_pdu_t *pdu;
3549 uint64_t token;
3550 uint8_t ltoken[8];
3551 size_t ltoken_length;
3552
3553 for (i = 0; (bp < data + length) &&
3554 i < COAP_MAX_PAYLOADS(session); i++) {
3555 if ((*bp & 0xc0) != 0x00) /* uint(value) */
3556 goto fail_cbor;
3557 block.num = derive_cbor_value(&bp, data + length - bp);
3558 coap_log_debug("Q-Block1: Missing block %d\n", block.num);
3559 if (block.num > (1 << 20) -1)
3560 goto fail_cbor;
3561 block.m = (block.num + 1) * chunk < lg_xmit->length;
3562 block.szx = lg_xmit->blk_size;
3563
3564 /* Build the next PDU request based off the skeletal PDU */
3565 token = STATE_TOKEN_FULL(lg_xmit->b.b1.state_token,++lg_xmit->b.b1.count);
3566 ltoken_length = coap_encode_var_safe8(ltoken, sizeof(token), token);
3567 pdu = coap_pdu_duplicate_lkd(&lg_xmit->pdu, session, ltoken_length,
3568 ltoken, NULL);
3569 if (!pdu)
3570 goto fail_body;
3571
3572 coap_update_option(pdu, lg_xmit->option,
3573 coap_encode_var_safe(buf, sizeof(buf),
3574 (block.num << 4) |
3575 (block.m << 3) |
3576 block.szx),
3577 buf);
3578
3579 if (!coap_add_block(pdu,
3580 lg_xmit->length,
3581 lg_xmit->data,
3582 block.num,
3583 block.szx))
3584 goto fail_body;
3585 if (coap_send_internal(session, pdu) == COAP_INVALID_MID)
3586 goto fail_body;
3587 }
3588 return 1;
3589 }
3590fail_cbor:
3591 coap_log_info("Invalid application/missing-blocks+cbor-seq\n");
3592#endif /* COAP_Q_BLOCK_SUPPORT */
3593 }
3594 goto lg_xmit_finished;
3595 } /* end of LL_FOREACH_SAFE */
3596 return 0;
3597
3598fail_body:
3600 /* There has been an internal error of some sort */
3601 rcvd->code = COAP_RESPONSE_CODE(500);
3602lg_xmit_finished:
3603 if (lg_crcv) {
3604 if (STATE_TOKEN_BASE(lg_xmit->b.b1.state_token) ==
3605 STATE_TOKEN_BASE(lg_crcv->state_token)) {
3606 /* In case of observe */
3607 lg_crcv->state_token = lg_xmit->b.b1.state_token;
3608 lg_crcv->retry_counter = lg_xmit->b.b1.count;
3609 }
3610 }
3611 if (!lg_crcv) {
3612 /* need to put back original token into rcvd */
3613 if (lg_xmit->b.b1.app_token)
3614 coap_update_token(rcvd, lg_xmit->b.b1.app_token->length,
3615 lg_xmit->b.b1.app_token->s);
3616 coap_log_debug("Client app version of updated PDU\n");
3618 } else {
3619 lg_crcv->pdu.lg_xmit = 0;
3620 }
3621
3622 if (sent) {
3623 /* need to put back original token into sent */
3624 if (lg_xmit->b.b1.app_token)
3625 coap_update_token(sent, lg_xmit->b.b1.app_token->length,
3626 lg_xmit->b.b1.app_token->s);
3627 if (sent->lg_xmit)
3628 coap_remove_option(sent, sent->lg_xmit->option);
3629 sent->lg_xmit = NULL;
3630 }
3631 LL_DELETE(session->lg_xmit, lg_xmit);
3632 coap_block_delete_lg_xmit(session, lg_xmit);
3633 return 0;
3634}
3635#endif /* COAP_CLIENT_SUPPORT */
3636
3638coap_block_build_body(coap_binary_t *body_data, size_t length,
3639 const uint8_t *data, size_t offset, size_t total) {
3640 coap_binary_t *ret;
3641
3642 coap_lock_lock(NULL, return NULL);
3643 ret = coap_block_build_body_lkd(body_data, length, data, offset, total);
3644 coap_lock_unlock(NULL);
3645 return ret;
3646}
3647
3648/*
3649 * Re-assemble payloads into a body
3650 */
3653 const uint8_t *data, size_t offset, size_t total) {
3654 if (data == NULL)
3655 return NULL;
3656 if (body_data == NULL && total) {
3657 body_data = coap_new_binary(total);
3658 }
3659 if (body_data == NULL)
3660 return NULL;
3661
3662 /* Check no overflow (including a 8 byte small headroom) */
3663 if (SIZE_MAX - length < 8 || offset > SIZE_MAX - length - 8) {
3664 coap_delete_binary(body_data);
3665 return NULL;
3666 }
3667
3668 /* Update saved data */
3669 if (offset + length <= total && body_data->length >= total) {
3670 memcpy(&body_data->s[offset], data, length);
3671 } else {
3672 /*
3673 * total may be inaccurate as per
3674 * https://rfc-editor.org/rfc/rfc7959#section-4
3675 * o In a request carrying a Block1 Option, to indicate the current
3676 * estimate the client has of the total size of the resource
3677 * representation, measured in bytes ("size indication").
3678 * o In a response carrying a Block2 Option, to indicate the current
3679 * estimate the server has of the total size of the resource
3680 * representation, measured in bytes ("size indication").
3681 */
3682 coap_binary_t *new = coap_resize_binary(body_data, offset + length);
3683
3684 if (new) {
3685 body_data = new;
3686 memcpy(&body_data->s[offset], data, length);
3687 } else {
3688 coap_delete_binary(body_data);
3689 return NULL;
3690 }
3691 }
3692 return body_data;
3693}
3694
3695#if COAP_CLIENT_SUPPORT
3696/*
3697 * Need to see if this is a large body response to a request. If so,
3698 * need to initiate the request for the next block and not trouble the
3699 * application. Note that Token must be unique per request/response.
3700 *
3701 * This is set up using coap_send()
3702 * Client receives large data from server ((Q-)Block2)
3703 *
3704 * Return: 0 Call application handler
3705 * 1 Do not call application handler - just sent the next request
3706 */
3707int
3709 coap_session_t *session,
3710 coap_pdu_t *sent,
3711 coap_pdu_t *rcvd,
3712 coap_recurse_t recursive) {
3713 coap_lg_crcv_t *lg_crcv;
3714 coap_block_b_t block;
3715#if COAP_Q_BLOCK_SUPPORT
3716 coap_block_b_t qblock;
3717#endif /* COAP_Q_BLOCK_SUPPORT */
3718 int have_block = 0;
3719 uint16_t block_opt = 0;
3720 size_t offset;
3721 int ack_rst_sent = 0;
3722 uint64_t token_match =
3724 rcvd->actual_token.length));
3725
3726 coap_lock_check_locked(context);
3727 memset(&block, 0, sizeof(block));
3728#if COAP_Q_BLOCK_SUPPORT
3729 memset(&qblock, 0, sizeof(qblock));
3730#endif /* COAP_Q_BLOCK_SUPPORT */
3731 LL_FOREACH(session->lg_crcv, lg_crcv) {
3732 size_t chunk = 0;
3733 uint8_t buf[8];
3734 coap_opt_iterator_t opt_iter;
3735
3736 if (token_match != STATE_TOKEN_BASE(lg_crcv->state_token) &&
3737 !coap_binary_equal(&rcvd->actual_token, lg_crcv->app_token)) {
3738 /* try out the next one */
3739 continue;
3740 }
3741
3742 /* lg_crcv found */
3743
3744 if (COAP_RESPONSE_CLASS(rcvd->code) == 2) {
3745 size_t length;
3746 const uint8_t *data;
3748 &opt_iter);
3749 size_t size2 = size_opt ?
3751 coap_opt_length(size_opt)) : 0;
3752
3753 /* length and data are cleared on error */
3754 (void)coap_get_data(rcvd, &length, &data);
3755 rcvd->body_offset = 0;
3756 rcvd->body_total = length;
3757 if (coap_get_block_b(session, rcvd, COAP_OPTION_BLOCK2, &block)) {
3758 have_block = 1;
3759 block_opt = COAP_OPTION_BLOCK2;
3760 }
3761#if COAP_Q_BLOCK_SUPPORT
3762 if (coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK2, &qblock)) {
3763 if (have_block) {
3764 coap_log_warn("Both Block1 and Q-Block1 not supported in a response\n");
3765 }
3766 have_block = 1;
3767 block_opt = COAP_OPTION_Q_BLOCK2;
3768 block = qblock;
3769 /* server indicating that it supports Q_BLOCK */
3770 if (!(session->block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
3771 set_block_mode_has_q(session->block_mode);
3772 }
3773 }
3774#endif /* COAP_Q_BLOCK_SUPPORT */
3775 track_echo(session, rcvd);
3776 if (have_block && (block.m || length)) {
3777 coap_opt_t *fmt_opt = coap_check_option(rcvd,
3779 &opt_iter);
3780 uint16_t fmt = fmt_opt ?
3782 coap_opt_length(fmt_opt)) :
3784 coap_opt_t *etag_opt = coap_check_option(rcvd,
3786 &opt_iter);
3787 size_t saved_offset;
3788 int updated_block;
3789
3790 if (length > block.chunk_size) {
3791 coap_log_debug("block: Oversized packet - reduced to %"PRIu32" from %zu\n",
3792 block.chunk_size, length);
3793 length = block.chunk_size;
3794 }
3795 if (block.m && length != block.chunk_size) {
3796 coap_log_warn("block: Undersized packet - expected %"PRIu32", got %zu\n",
3797 block.chunk_size, length);
3798 /* Unclear how to properly handle this */
3799 rcvd->code = COAP_RESPONSE_CODE(402);
3800 goto expire_lg_crcv;
3801 }
3802 /* Possibility that Size2 not sent, or is too small */
3803 chunk = (size_t)1 << (block.szx + 4);
3804 offset = block.num * chunk;
3805 if (size2 < (offset + length)) {
3806 if (block.m)
3807 size2 = offset + length + 1;
3808 else
3809 size2 = offset + length;
3810 }
3811 saved_offset = offset;
3812
3813 if (lg_crcv->initial) {
3814#if COAP_Q_BLOCK_SUPPORT
3815reinit:
3816#endif /* COAP_Q_BLOCK_SUPPORT */
3817 lg_crcv->initial = 0;
3818 if (lg_crcv->body_data) {
3820 lg_crcv->body_data = NULL;
3821 }
3822 if (etag_opt) {
3823 lg_crcv->etag_length = coap_opt_length(etag_opt);
3824 memcpy(lg_crcv->etag, coap_opt_value(etag_opt), lg_crcv->etag_length);
3825 lg_crcv->etag_set = 1;
3826 } else {
3827 lg_crcv->etag_set = 0;
3828 }
3829 lg_crcv->total_len = size2;
3830 lg_crcv->content_format = fmt;
3831 lg_crcv->szx = block.szx;
3832 lg_crcv->block_option = block_opt;
3833 lg_crcv->last_type = rcvd->type;
3834 lg_crcv->rec_blocks.used = 0;
3835 lg_crcv->rec_blocks.total_blocks = 0;
3836#if COAP_Q_BLOCK_SUPPORT
3837 lg_crcv->rec_blocks.processing_payload_set = 0;
3838#endif /* COAP_Q_BLOCK_SUPPORT */
3839 }
3840 if (lg_crcv->total_len < size2)
3841 lg_crcv->total_len = size2;
3842
3843 if (etag_opt) {
3844 if (!full_match(coap_opt_value(etag_opt),
3845 coap_opt_length(etag_opt),
3846 lg_crcv->etag, lg_crcv->etag_length)) {
3847 /* body of data has changed - need to restart request */
3848 coap_pdu_t *pdu;
3849 uint64_t token = STATE_TOKEN_FULL(lg_crcv->state_token,
3850 ++lg_crcv->retry_counter);
3851 size_t len = coap_encode_var_safe8(buf, sizeof(token), token);
3852 coap_opt_filter_t drop_options;
3853
3854#if COAP_Q_BLOCK_SUPPORT
3855 if (block_opt == COAP_OPTION_Q_BLOCK2)
3856 goto reinit;
3857#endif /* COAP_Q_BLOCK_SUPPORT */
3858
3859 coap_log_warn("Data body updated during receipt - new request started\n");
3860 if (!(session->block_mode & COAP_BLOCK_SINGLE_BODY))
3862
3863 lg_crcv->initial = 1;
3865 lg_crcv->body_data = NULL;
3866
3867 coap_session_new_token(session, &len, buf);
3868 memset(&drop_options, 0, sizeof(coap_opt_filter_t));
3870 pdu = coap_pdu_duplicate_lkd(&lg_crcv->pdu, session, len, buf, &drop_options);
3871 if (!pdu)
3872 goto fail_resp;
3873
3874 coap_update_option(pdu, block_opt,
3875 coap_encode_var_safe(buf, sizeof(buf),
3876 (0 << 4) | (0 << 3) | block.aszx),
3877 buf);
3878
3879 if (coap_send_internal(session, pdu) == COAP_INVALID_MID)
3880 goto fail_resp;
3881
3882 goto skip_app_handler;
3883 }
3884 } else if (lg_crcv->etag_set) {
3885 /* Cannot handle this change in ETag to not being there */
3886 coap_log_warn("Not all blocks have ETag option\n");
3887 goto fail_resp;
3888 }
3889
3890 if (fmt != lg_crcv->content_format) {
3891 coap_log_warn("Content-Format option mismatch\n");
3892 goto fail_resp;
3893 }
3894#if COAP_Q_BLOCK_SUPPORT
3895 if (block_opt == COAP_OPTION_Q_BLOCK2 && size2 != lg_crcv->total_len) {
3896 coap_log_warn("Size2 option mismatch\n");
3897 goto fail_resp;
3898 }
3899#endif /* COAP_Q_BLOCK_SUPPORT */
3900 if (block.num == 0) {
3901 coap_opt_t *obs_opt = coap_check_option(rcvd,
3903 &opt_iter);
3904 if (obs_opt) {
3905 lg_crcv->observe_length = min(coap_opt_length(obs_opt), 3);
3906 memcpy(lg_crcv->observe, coap_opt_value(obs_opt), lg_crcv->observe_length);
3907 lg_crcv->observe_set = 1;
3908 } else {
3909 lg_crcv->observe_set = 0;
3910 }
3911 }
3912 updated_block = 0;
3913 while (offset < saved_offset + length) {
3914 if (!check_if_received_block(&lg_crcv->rec_blocks, block.num)) {
3915#if COAP_Q_BLOCK_SUPPORT
3916 uint32_t this_payload_set = block.num / COAP_MAX_PAYLOADS(session);
3917#endif /* COAP_Q_BLOCK_SUPPORT */
3918
3919 coap_log_debug("found Block option, block size is %u, block nr. %u\n",
3920 1 << (block.szx + 4), block.num);
3921#if COAP_Q_BLOCK_SUPPORT
3922 if (block_opt == COAP_OPTION_Q_BLOCK2 && lg_crcv->rec_blocks.used &&
3923 this_payload_set > lg_crcv->rec_blocks.processing_payload_set &&
3924 this_payload_set != lg_crcv->rec_blocks.latest_payload_set) {
3925 coap_request_missing_q_block2(session, lg_crcv);
3926 }
3927 lg_crcv->rec_blocks.latest_payload_set = this_payload_set;
3928#endif /* COAP_Q_BLOCK_SUPPORT */
3929 /* Update list of blocks received */
3930 if (!update_received_blocks(&lg_crcv->rec_blocks, block.num, block.m)) {
3932 goto fail_resp;
3933 }
3934 updated_block = 1;
3935 }
3936 block.num++;
3937 offset = block.num << (block.szx + 4);
3938 if (!block.bert && block_opt != COAP_OPTION_Q_BLOCK2)
3939 break;
3940 }
3941 block.num--;
3942 /* Only process if not duplicate block */
3943 if (updated_block) {
3944 if ((session->block_mode & COAP_SINGLE_BLOCK_OR_Q) || block.bert) {
3945 if (size2 < saved_offset + length) {
3946 size2 = saved_offset + length;
3947 }
3948 lg_crcv->body_data = coap_block_build_body_lkd(lg_crcv->body_data, length, data,
3949 saved_offset, size2);
3950 if (lg_crcv->body_data == NULL) {
3951 goto fail_resp;
3952 }
3953 }
3954 if (block.m || !check_all_blocks_in(&lg_crcv->rec_blocks)) {
3955 /* Not all the payloads of the body have arrived */
3956 size_t len;
3957 coap_pdu_t *pdu;
3958 uint64_t token;
3959
3960 if (block.m) {
3961#if COAP_Q_BLOCK_SUPPORT
3962 if (block_opt == COAP_OPTION_Q_BLOCK2) {
3963 /* Blocks could arrive in wrong order */
3964 if (check_all_blocks_in(&lg_crcv->rec_blocks)) {
3965 goto give_to_app;
3966 }
3967 if (check_all_blocks_in_for_payload_set(session,
3968 &lg_crcv->rec_blocks)) {
3969 block.num = lg_crcv->rec_blocks.range[0].end;
3970 /* Now requesting next payload */
3971 lg_crcv->rec_blocks.processing_payload_set =
3972 block.num / COAP_MAX_PAYLOADS(session) + 1;
3973 if (check_any_blocks_next_payload_set(session,
3974 &lg_crcv->rec_blocks)) {
3975 /* Need to ask for them individually */
3976 coap_request_missing_q_block2(session, lg_crcv);
3977 goto skip_app_handler;
3978 }
3979 } else {
3980 /* The remote end will be sending the next one unless this
3981 is a MAX_PAYLOADS and all previous have been received */
3982 goto skip_app_handler;
3983 }
3984 if (COAP_PROTO_RELIABLE(session->proto) ||
3985 rcvd->type != COAP_MESSAGE_NON)
3986 goto skip_app_handler;
3987
3988 } else
3989#endif /* COAP_Q_BLOCK_SUPPORT */
3990 block.m = 0;
3991
3992 /* Ask for the next block */
3993 token = STATE_TOKEN_FULL(lg_crcv->state_token, ++lg_crcv->retry_counter);
3994 len = coap_encode_var_safe8(buf, sizeof(token), token);
3995 pdu = coap_pdu_duplicate_lkd(&lg_crcv->pdu, session, len, buf, NULL);
3996 if (!pdu)
3997 goto fail_resp;
3998
3999 if (rcvd->type == COAP_MESSAGE_NON)
4000 pdu->type = COAP_MESSAGE_NON; /* Server is using NON */
4001
4002 /* Only sent with the first block */
4004
4005 coap_update_option(pdu, block_opt,
4006 coap_encode_var_safe(buf, sizeof(buf),
4007 ((block.num + 1) << 4) |
4008 (block.m << 3) | block.aszx),
4009 buf);
4010
4012 (void)coap_get_data(&lg_crcv->pdu, &length, &data);
4013 coap_add_data_large_internal(session, NULL, pdu, NULL, NULL, -1, 0, length, data, NULL, NULL, 0, 0);
4014 }
4015 if (coap_send_internal(session, pdu) == COAP_INVALID_MID)
4016 goto fail_resp;
4017 }
4018 if ((session->block_mode & COAP_SINGLE_BLOCK_OR_Q) || block.bert)
4019 goto skip_app_handler;
4020
4021 /* need to put back original token into rcvd */
4022 coap_update_token(rcvd, lg_crcv->app_token->length, lg_crcv->app_token->s);
4023 rcvd->body_offset = saved_offset;
4024#if COAP_Q_BLOCK_SUPPORT
4025 rcvd->body_total = block_opt == COAP_OPTION_Q_BLOCK2 ?
4026 lg_crcv->total_len : size2;
4027#else /* ! COAP_Q_BLOCK_SUPPORT */
4028 rcvd->body_total = size2;
4029#endif /* ! COAP_Q_BLOCK_SUPPORT */
4030 coap_log_debug("Client app version of updated PDU\n");
4032
4033 if (sent) {
4034 /* need to put back original token into sent */
4035 if (lg_crcv->app_token)
4036 coap_update_token(sent, lg_crcv->app_token->length,
4037 lg_crcv->app_token->s);
4038 coap_remove_option(sent, lg_crcv->block_option);
4039 }
4040 goto call_app_handler;
4041 }
4042#if COAP_Q_BLOCK_SUPPORT
4043give_to_app:
4044#endif /* COAP_Q_BLOCK_SUPPORT */
4045 if ((session->block_mode & COAP_SINGLE_BLOCK_OR_Q) || block.bert) {
4046 /* Pretend that there is no block */
4047 coap_remove_option(rcvd, block_opt);
4048 if (lg_crcv->observe_set) {
4050 lg_crcv->observe_length, lg_crcv->observe);
4051 }
4052 rcvd->body_data = lg_crcv->body_data->s;
4053#if COAP_Q_BLOCK_SUPPORT
4054 rcvd->body_length = block_opt == COAP_OPTION_Q_BLOCK2 ?
4055 lg_crcv->total_len : saved_offset + length;
4056#else /* ! COAP_Q_BLOCK_SUPPORT */
4057 rcvd->body_length = saved_offset + length;
4058#endif /* ! COAP_Q_BLOCK_SUPPORT */
4059 rcvd->body_offset = 0;
4060 rcvd->body_total = rcvd->body_length;
4061 } else {
4062 rcvd->body_offset = saved_offset;
4063#if COAP_Q_BLOCK_SUPPORT
4064 rcvd->body_total = block_opt == COAP_OPTION_Q_BLOCK2 ?
4065 lg_crcv->total_len : size2;
4066#else /* ! COAP_Q_BLOCK_SUPPORT */
4067 rcvd->body_total = size2;
4068#endif /* ! COAP_Q_BLOCK_SUPPORT */
4069 }
4070 if (context->response_handler) {
4071 coap_response_t ret;
4072
4073 /* need to put back original token into rcvd */
4074 if (!coap_binary_equal(&rcvd->actual_token, lg_crcv->app_token)) {
4075 coap_update_token(rcvd, lg_crcv->app_token->length, lg_crcv->app_token->s);
4076 coap_log_debug("Client app version of updated PDU\n");
4078 }
4079 if (sent) {
4080 /* need to put back original token into sent */
4081 if (lg_crcv->app_token)
4082 coap_update_token(sent, lg_crcv->app_token->length,
4083 lg_crcv->app_token->s);
4084 coap_remove_option(sent, lg_crcv->block_option);
4085 }
4087 context->response_handler(session, sent, rcvd,
4088 rcvd->mid),
4089 /* context is being freed off */
4090 assert(0));
4091 if (ret == COAP_RESPONSE_FAIL) {
4092 coap_send_rst_lkd(session, rcvd);
4093 } else {
4094 coap_send_ack_lkd(session, rcvd);
4095 }
4096 } else {
4097 coap_send_ack_lkd(session, rcvd);
4098 }
4099 ack_rst_sent = 1;
4100 if (lg_crcv->observe_set == 0) {
4101 /* Expire this entry */
4102 LL_DELETE(session->lg_crcv, lg_crcv);
4103 coap_block_delete_lg_crcv(session, lg_crcv);
4104 goto skip_app_handler;
4105 }
4106 /* Set up for the next data body as observing */
4107 lg_crcv->initial = 1;
4108 if (lg_crcv->body_data) {
4110 lg_crcv->body_data = NULL;
4111 }
4112 }
4113 coap_ticks(&lg_crcv->last_used);
4114 goto skip_app_handler;
4115 } else {
4116 coap_opt_t *obs_opt = coap_check_option(rcvd,
4118 &opt_iter);
4119 if (obs_opt) {
4120 lg_crcv->observe_length = min(coap_opt_length(obs_opt), 3);
4121 memcpy(lg_crcv->observe, coap_opt_value(obs_opt), lg_crcv->observe_length);
4122 lg_crcv->observe_set = 1;
4123 } else {
4124 lg_crcv->observe_set = 0;
4125 if (!coap_binary_equal(&rcvd->actual_token, lg_crcv->app_token)) {
4126 /* need to put back original token into rcvd */
4127 coap_update_token(rcvd, lg_crcv->app_token->length, lg_crcv->app_token->s);
4128 coap_log_debug("PDU presented to app.\n");
4130 }
4131 /* Expire this entry */
4132 goto expire_lg_crcv;
4133 }
4134 }
4135 coap_ticks(&lg_crcv->last_used);
4136 } else if (rcvd->code == COAP_RESPONSE_CODE(401)) {
4137#if COAP_OSCORE_SUPPORT
4138 if (check_freshness(session, rcvd,
4139 (session->oscore_encryption == 0) ? sent : NULL,
4140 NULL, lg_crcv))
4141#else /* !COAP_OSCORE_SUPPORT */
4142 if (check_freshness(session, rcvd, sent, NULL, lg_crcv))
4143#endif /* !COAP_OSCORE_SUPPORT */
4144 goto skip_app_handler;
4145 goto expire_lg_crcv;
4146 } else {
4147 /* Not 2.xx or 4.01 - assume it is a failure of some sort */
4148 goto expire_lg_crcv;
4149 }
4150 if (!block.m && !lg_crcv->observe_set) {
4151fail_resp:
4152 /* lg_crcv no longer required - cache it for 1 sec */
4153 coap_ticks(&lg_crcv->last_used);
4154 lg_crcv->last_used = lg_crcv->last_used - COAP_MAX_TRANSMIT_WAIT_TICKS(session) +
4156 }
4157 /* need to put back original token into rcvd */
4158 if (!coap_binary_equal(&rcvd->actual_token, lg_crcv->app_token)) {
4159 coap_update_token(rcvd, lg_crcv->app_token->length, lg_crcv->app_token->s);
4160 coap_log_debug("Client app version of updated PDU (3)\n");
4162 }
4163 break;
4164 } /* LL_FOREACH() */
4165
4166 /* Check if receiving a block response and if blocks can be set up */
4167 if (recursive == COAP_RECURSE_OK && !lg_crcv) {
4168 if (!sent) {
4169 if (coap_get_block_b(session, rcvd, COAP_OPTION_BLOCK2, &block)
4171 ||
4172 coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK2, &block)
4173#endif /* COAP_Q_BLOCK_SUPPORT */
4174 ) {
4175 coap_log_debug("** %s: large body receive internal issue\n",
4176 coap_session_str(session));
4177 goto skip_app_handler;
4178 }
4179 } else if (COAP_RESPONSE_CLASS(rcvd->code) == 2) {
4180 if (coap_get_block_b(session, rcvd, COAP_OPTION_BLOCK2, &block)) {
4181#if COAP_Q_BLOCK_SUPPORT
4182 if (session->block_mode & COAP_BLOCK_PROBE_Q_BLOCK) {
4183 set_block_mode_drop_q(session->block_mode);
4184 coap_log_debug("Q-Block support disabled\n");
4185 }
4186#endif /* COAP_Q_BLOCK_SUPPORT */
4187 have_block = 1;
4188 if (block.num != 0) {
4189 /* Assume random access and just give the single response to app */
4190 size_t length;
4191 const uint8_t *data;
4192 size_t chunk = (size_t)1 << (block.szx + 4);
4193
4194 coap_get_data(rcvd, &length, &data);
4195 rcvd->body_offset = block.num*chunk;
4196 rcvd->body_total = block.num*chunk + length + (block.m ? 1 : 0);
4197 goto call_app_handler;
4198 }
4199 }
4200#if COAP_Q_BLOCK_SUPPORT
4201 else if (coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK2, &block)) {
4202 have_block = 1;
4203 /* server indicating that it supports Q_BLOCK2 */
4204 if (!(session->block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
4205 set_block_mode_has_q(session->block_mode);
4206 }
4207 }
4208#endif /* COAP_Q_BLOCK_SUPPORT */
4209 if (have_block) {
4210 lg_crcv = coap_block_new_lg_crcv(session, sent, NULL);
4211
4212 if (lg_crcv) {
4213 LL_PREPEND(session->lg_crcv, lg_crcv);
4214 return coap_handle_response_get_block(context, session, sent, rcvd,
4216 }
4217 }
4218 track_echo(session, rcvd);
4219 } else if (rcvd->code == COAP_RESPONSE_CODE(401)) {
4220 lg_crcv = coap_block_new_lg_crcv(session, sent, NULL);
4221
4222 if (lg_crcv) {
4223 LL_PREPEND(session->lg_crcv, lg_crcv);
4224 return coap_handle_response_get_block(context, session, sent, rcvd,
4226 }
4227 }
4228 }
4229 return 0;
4230
4231expire_lg_crcv:
4232 /* need to put back original token into rcvd */
4233 if (!coap_binary_equal(&rcvd->actual_token, lg_crcv->app_token)) {
4234 coap_update_token(rcvd, lg_crcv->app_token->length, lg_crcv->app_token->s);
4235 coap_log_debug("Client app version of updated PDU\n");
4237 }
4238
4239 if (sent) {
4240 /* need to put back original token into sent */
4241 if (lg_crcv->app_token)
4242 coap_update_token(sent, lg_crcv->app_token->length,
4243 lg_crcv->app_token->s);
4244 coap_remove_option(sent, lg_crcv->block_option);
4245 }
4246 /* Expire this entry */
4247 LL_DELETE(session->lg_crcv, lg_crcv);
4248 coap_block_delete_lg_crcv(session, lg_crcv);
4249
4250call_app_handler:
4251 return 0;
4252
4253skip_app_handler:
4254 if (!ack_rst_sent)
4255 coap_send_ack_lkd(session, rcvd);
4256 return 1;
4257}
4258#endif /* COAP_CLIENT_SUPPORT */
4259
4260#if COAP_SERVER_SUPPORT
4261/* Check if lg_xmit generated and update PDU code if so */
4262void
4264 const coap_pdu_t *request,
4265 coap_pdu_t *response, const coap_resource_t *resource,
4266 const coap_string_t *query) {
4267 coap_lg_xmit_t *lg_xmit;
4268
4269 if (response->code == 0)
4270 return;
4271 lg_xmit = coap_find_lg_xmit_response(session, request, resource, query);
4272 if (lg_xmit && lg_xmit->pdu.code == 0) {
4273 lg_xmit->pdu.code = response->code;
4274 return;
4275 }
4276}
4277#endif /* COAP_SERVER_SUPPORT */
4278
4279#if COAP_CLIENT_SUPPORT
4280void
4282 uint64_t token_match =
4284 pdu->actual_token.length));
4285 coap_lg_xmit_t *lg_xmit;
4286 coap_lg_crcv_t *lg_crcv;
4287
4288 if (session->lg_crcv) {
4289 LL_FOREACH(session->lg_crcv, lg_crcv) {
4290 if (coap_binary_equal(&pdu->actual_token, lg_crcv->app_token))
4291 return;
4292 if (token_match == STATE_TOKEN_BASE(lg_crcv->state_token)) {
4293 coap_update_token(pdu, lg_crcv->app_token->length,
4294 lg_crcv->app_token->s);
4295 coap_log_debug("Client app version of updated PDU\n");
4297 return;
4298 }
4299 }
4300 }
4301 if (COAP_PDU_IS_REQUEST(pdu) && session->lg_xmit) {
4302 LL_FOREACH(session->lg_xmit, lg_xmit) {
4303 if (coap_binary_equal(&pdu->actual_token, lg_xmit->b.b1.app_token))
4304 return;
4305 if (token_match == STATE_TOKEN_BASE(lg_xmit->b.b1.state_token)) {
4306 coap_update_token(pdu, lg_xmit->b.b1.app_token->length,
4307 lg_xmit->b.b1.app_token->s);
4308 coap_log_debug("Client app version of updated PDU\n");
4310 return;
4311 }
4312 }
4313 }
4314}
4315#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 update_received_blocks(coap_rblock_t *rec_blocks, uint32_t block_num, uint32_t block_m)
static int check_all_blocks_in(coap_rblock_t *rec_blocks)
static int coap_add_data_large_internal(coap_session_t *session, const coap_pdu_t *request, coap_pdu_t *pdu, coap_resource_t *resource, const coap_string_t *query, int maxage, uint64_t etag, size_t length, const uint8_t *data, coap_release_large_data_t release_func, void *app_ptr, int single_request, coap_pdu_code_t request_method)
Definition coap_block.c:635
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)
#define COAP_Q_BLOCK_SUPPORT
int coap_flsll(long long j)
Definition coap_encode.c:28
int coap_fls(unsigned int i)
Definition coap_encode.c:21
struct coap_resource_t coap_resource_t
unsigned char coap_key_t[4]
#define coap_hash(String, Length, Result)
#define PRIu32
@ COAP_NACK_TOO_MANY_RETRIES
Definition coap_io.h:63
Library specific build wrapper for coap_internal.h.
#define COAP_API
@ COAP_LG_XMIT
Definition coap_mem.h:55
@ COAP_LG_CRCV
Definition coap_mem.h:56
@ COAP_LG_SRCV
Definition coap_mem.h:57
@ COAP_STRING
Definition coap_mem.h:39
@ COAP_PDU_BUF
Definition coap_mem.h:47
void * coap_realloc_type(coap_memory_tag_t type, void *p, size_t size)
Reallocates a chunk p of bytes created by coap_malloc_type() or coap_realloc_type() and returns a poi...
void * coap_malloc_type(coap_memory_tag_t type, size_t size)
Allocates a chunk of size bytes and returns a pointer to the newly allocated memory.
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
uint16_t coap_option_num_t
Definition coap_option.h:20
uint8_t coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
Definition coap_option.h:26
coap_mid_t coap_send_rst_lkd(coap_session_t *session, const coap_pdu_t *request)
Sends an RST message with code 0 for the specified request to dst.
Definition coap_net.c:969
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:984
#define COAP_BLOCK_MAX_SIZE_MASK
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...
void coap_block_delete_lg_xmit(coap_session_t *session, coap_lg_xmit_t *lg_xmit)
#define STATE_TOKEN_FULL(t, r)
#define COAP_SINGLE_BLOCK_OR_Q
int coap_handle_request_put_block(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu, coap_pdu_t *response, coap_resource_t *resource, coap_string_t *uri_path, coap_opt_t *observe, int *added_block, coap_lg_srcv_t **free_lg_srcv)
#define STATE_TOKEN_BASE(t)
coap_binary_t * coap_block_build_body_lkd(coap_binary_t *body_data, size_t length, const uint8_t *data, size_t offset, size_t total)
Re-assemble payloads into a body.
#define COAP_BLOCK_SET_MASK
coap_lg_xmit_t * coap_find_lg_xmit_response(const coap_session_t *session, const coap_pdu_t *request, const coap_resource_t *resource, const coap_string_t *query)
coap_lg_crcv_t * coap_block_new_lg_crcv(coap_session_t *session, coap_pdu_t *pdu, coap_lg_xmit_t *lg_xmit)
int coap_handle_request_send_block(coap_session_t *session, coap_pdu_t *pdu, coap_pdu_t *response, coap_resource_t *resource, coap_string_t *query)
#define COAP_BLOCK_MAX_SIZE_GET(a)
int coap_block_check_lg_xmit_timeouts(coap_session_t *session, coap_tick_t now, coap_tick_t *tim_rem)
@ COAP_RECURSE_OK
@ COAP_RECURSE_NO
COAP_API void coap_context_set_block_mode(coap_context_t *context, uint32_t block_mode)
Set the context level CoAP block handling bits for handling RFC7959.
Definition coap_block.c: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
COAP_API 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.
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
#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:4378
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:1224
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:1677
coap_response_t
Definition coap_net.h:48
void coap_ticks(coap_tick_t *)
Returns the current value of an internal tick counter.
@ COAP_RESPONSE_FAIL
Response not liked - send CoAP RST packet.
Definition coap_net.h:49
unsigned int coap_encode_var_safe(uint8_t *buf, size_t length, unsigned int val)
Encodes multiple-length byte sequences.
Definition coap_encode.c:47
unsigned int coap_decode_var_bytes(const uint8_t *buf, size_t len)
Decodes multiple-length byte sequences.
Definition coap_encode.c:38
uint64_t coap_decode_var_bytes8(const uint8_t *buf, size_t len)
Decodes multiple-length byte sequences.
Definition coap_encode.c:67
unsigned int coap_encode_var_safe8(uint8_t *buf, size_t length, uint64_t val)
Encodes multiple-length byte sequences.
Definition coap_encode.c:77
@ COAP_EVENT_PARTIAL_BLOCK
Triggered when not all of a large body has been received.
Definition coap_event.h:71
@ COAP_EVENT_XMIT_BLOCK_FAIL
Triggered when not all of a large body has been transmitted.
Definition coap_event.h:73
#define coap_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:778
const char * coap_session_str(const coap_session_t *session)
Get session description.
#define coap_log_info(...)
Definition coap_debug.h:108
#define coap_log_warn(...)
Definition coap_debug.h:102
@ COAP_LOG_DEBUG
Definition coap_debug.h:58
#define COAP_OBSERVE_CANCEL
The value COAP_OBSERVE_CANCEL in a GET/FETCH request option COAP_OPTION_OBSERVE indicates that the ob...
#define COAP_OBSERVE_ESTABLISH
The value COAP_OBSERVE_ESTABLISH in a GET/FETCH request option COAP_OPTION_OBSERVE indicates a new ob...
COAP_API int coap_cancel_observe(coap_session_t *session, coap_binary_t *token, coap_pdu_type_t message_type)
Cancel an observe that is being tracked by the client large receive logic.
coap_opt_t * coap_option_next(coap_opt_iterator_t *oi)
Updates the iterator oi to point to the next option.
uint32_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
coap_opt_iterator_t * coap_option_iterator_init(const coap_pdu_t *pdu, coap_opt_iterator_t *oi, const coap_opt_filter_t *filter)
Initializes the given option iterator oi to point to the beginning of the pdu's option list.
size_t coap_opt_encode_size(uint16_t delta, size_t length)
Compute storage bytes needed for an option with given delta and length.
#define COAP_OPT_ALL
Pre-defined filter that includes all options.
coap_opt_t * coap_check_option(const coap_pdu_t *pdu, coap_option_num_t number, coap_opt_iterator_t *oi)
Retrieves the first option of number number from pdu.
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
int coap_option_filter_set(coap_opt_filter_t *filter, coap_option_num_t option)
Sets the corresponding entry for number in filter.
int coap_rebuild_pdu_for_proxy(coap_pdu_t *pdu)
Convert PDU to use Proxy-Scheme option if Proxy-Uri option is present.
size_t coap_oscore_overhead(coap_session_t *session, coap_pdu_t *pdu)
Determine the additional data size requirements for adding in OSCORE.
#define COAP_PDU_IS_RESPONSE(pdu)
void coap_delete_pdu_lkd(coap_pdu_t *pdu)
Dispose of an CoAP PDU and free off associated storage.
Definition coap_pdu.c:188
size_t coap_insert_option(coap_pdu_t *pdu, coap_option_num_t number, size_t len, const uint8_t *data)
Inserts option of given number in the pdu with the appropriate data.
Definition coap_pdu.c:626
int coap_remove_option(coap_pdu_t *pdu, coap_option_num_t number)
Removes (first) option of given number from the pdu.
Definition coap_pdu.c:489
int coap_update_token(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Updates token in pdu with length len and data.
Definition coap_pdu.c:413
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:725
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:228
#define COAP_PAYLOAD_START
int coap_pdu_check_resize(coap_pdu_t *pdu, size_t size)
Dynamically grows the size of pdu to new_size if needed.
Definition coap_pdu.c:339
#define COAP_PDU_IS_REQUEST(pdu)
size_t coap_add_option_internal(coap_pdu_t *pdu, coap_option_num_t number, size_t len, const uint8_t *data)
Adds option of given number to pdu that is passed as first parameter.
Definition coap_pdu.c:781
#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:952
#define COAP_MEDIATYPE_APPLICATION_MB_CBOR_SEQ
Definition coap_pdu.h:254
#define COAP_OPTION_CONTENT_FORMAT
Definition coap_pdu.h:128
#define COAP_OPTION_SIZE2
Definition coap_pdu.h:139
#define COAP_OPTION_BLOCK1
Definition coap_pdu.h:138
#define COAP_OPTION_Q_BLOCK1
Definition coap_pdu.h:135
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition coap_pdu.h:263
#define COAP_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:327
#define COAP_OPTION_SIZE1
Definition coap_pdu.h:143
coap_pdu_type_t
CoAP PDU message type definitions.
Definition coap_pdu.h:68
#define COAP_MEDIATYPE_TEXT_PLAIN
Definition coap_pdu.h:213
int coap_add_token(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Adds token of length len to pdu.
Definition coap_pdu.c:356
#define COAP_OPTION_CONTENT_TYPE
Definition coap_pdu.h:129
size_t coap_add_option(coap_pdu_t *pdu, coap_option_num_t number, size_t len, const uint8_t *data)
Adds option of given number to pdu that is passed as first parameter.
Definition coap_pdu.c:771
#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:877
#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:885
#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:846
@ COAP_REQUEST_CODE_GET
Definition coap_pdu.h:330
@ COAP_REQUEST_CODE_FETCH
Definition coap_pdu.h:334
@ COAP_MESSAGE_NON
Definition coap_pdu.h:70
@ COAP_MESSAGE_ACK
Definition coap_pdu.h:71
@ COAP_MESSAGE_CON
Definition coap_pdu.h:69
#define COAP_NON_RECEIVE_TIMEOUT_TICKS(s)
The NON_RECEIVE_TIMEOUT definition for the session (s).
#define COAP_NON_TIMEOUT_TICKS(s)
#define COAP_MAX_TRANSMIT_WAIT_TICKS(s)
#define COAP_NON_PARTIAL_TIMEOUT_TICKS(s)
The NON_PARTIAL_TIMEOUT definition for the session (s).
coap_tick_t coap_get_non_timeout_random_ticks(coap_session_t *session)
#define COAP_NSTART(s)
#define COAP_MAX_PAYLOADS(s)
size_t coap_session_max_pdu_size_lkd(const coap_session_t *session)
Get maximum acceptable PDU size.
#define COAP_NON_MAX_RETRANSMIT(s)
#define COAP_PROTO_NOT_RELIABLE(p)
#define COAP_PROTO_RELIABLE(p)
void coap_session_new_token(coap_session_t *session, size_t *len, uint8_t *data)
Creates a new token for use.
@ COAP_SESSION_TYPE_CLIENT
client-side
void coap_delete_bin_const(coap_bin_const_t *s)
Deletes the given const binary data and releases any memory allocated.
Definition coap_str.c:121
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
Definition coap_block.h:58
unsigned int bert
Operating as BERT.
Definition coap_block.h:57
unsigned int aszx
block size (0-7 including BERT
Definition coap_block.h:55
unsigned int defined
Set if block found.
Definition coap_block.h:56
unsigned int m
1 if more blocks follow, 0 otherwise
Definition coap_block.h:53
unsigned int szx
block size (0-6)
Definition coap_block.h:54
Structure of Block options.
Definition coap_block.h:42
unsigned int num
block number
Definition coap_block.h:43
unsigned int szx
block size
Definition coap_block.h:45
unsigned int m
1 if more blocks follow, 0 otherwise
Definition coap_block.h:44
The CoAP stack's global state is stored in a coap_context_t object.
uint64_t etag
Next ETag to use.
coap_nack_handler_t nack_handler
Called when a response issue has occurred.
coap_response_handler_t response_handler
Called when a response is received.
uint32_t block_mode
Zero or more COAP_BLOCK_ or'd options.
coap_resource_t * proxy_uri_resource
can be used for handling proxy URI resources
coap_resource_t * unknown_resource
can be used for handling unknown resources
uint64_t state_token
state token
size_t bert_size
size of last BERT block
uint32_t count
the number of packets sent for payload
coap_binary_t * app_token
original PDU token
coap_pdu_code_t request_method
Method used to request this data.
uint8_t rtag_length
RTag length.
coap_string_t * query
Associated query for the resource.
uint64_t etag
ETag value.
coap_resource_t * resource
associated resource
coap_time_t maxage_expire
When this entry expires.
uint8_t rtag_set
Set if RTag is in receive PDU.
uint8_t rtag[8]
RTag for block checking.
Structure to hold large body (many blocks) client receive information.
uint16_t block_option
Block option in use.
uint8_t etag[8]
ETag for block checking.
uint8_t etag_length
ETag length.
uint8_t last_type
Last request type (CON/NON).
uint8_t observe_length
Length of observe data.
uint8_t observe[3]
Observe data (if observe_set) (only 24 bits).
uint8_t etag_set
Set if ETag is in receive PDU.
coap_rblock_t rec_blocks
uint8_t initial
If set, has not been used yet.
uint8_t szx
size of individual blocks
uint16_t o_block_option
Block CoAP option used when initiating Observe.
uint16_t content_format
Content format for the set of blocks.
coap_pdu_t pdu
skeletal PDU
uint8_t o_blk_size
Block size used when initiating Observe.
coap_tick_t last_used
< list of received blocks
coap_bin_const_t ** obs_token
Tokens used in setting up Observe (to handle large FETCH).
uint64_t state_token
state token
coap_binary_t * app_token
app requesting PDU token
uint16_t retry_counter
Retry counter (part of state token).
coap_binary_t * body_data
Used for re-assembling entire body.
size_t obs_token_cnt
number of tokens used to set up Observe
uint8_t observe_set
Set if this is an observe receive PDU.
size_t total_len
Length as indicated by SIZE2 option.
Structure to hold large body (many blocks) server receive information.
uint8_t rtag[8]
RTag for block checking.
coap_mid_t last_mid
Last received mid for this set of packets.
uint8_t rtag_set
Set if RTag is in receive PDU.
uint16_t block_option
Block option in use.
size_t total_len
Length as indicated by SIZE1 option.
uint8_t observe_length
Length of observe data.
coap_rblock_t rec_blocks
set to uri_path if unknown resource
uint8_t no_more_seen
Set if block with more not set seen.
coap_binary_t * body_data
Used for re-assembling entire body.
coap_resource_t * resource
associated resource
uint8_t observe_set
Set if this is an observe receive PDU.
uint8_t rtag_length
RTag length.
uint8_t last_type
Last request type (CON/NON).
uint8_t szx
size of individual blocks
coap_tick_t last_used
Last time data sent or 0.
uint8_t observe[3]
Observe data (if set) (only 24 bits).
uint16_t content_format
Content format for the set of blocks.
coap_bin_const_t * last_token
< list of received blocks
coap_str_const_t * uri_path
Structure to hold large body (many blocks) transmission information.
coap_tick_t last_all_sent
Last time all data sent or 0.
coap_release_large_data_t release_func
large data de-alloc function
uint8_t blk_size
large block transmission size
coap_tick_t last_sent
Last time any data sent.
coap_lg_crcv_t * lg_crcv
The lg_crcv associated with this blocked xmit.
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
union coap_lg_xmit_t::@152016206261220260141173172226260025274141240323 b
uint16_t option
large block transmisson CoAP option
void * app_ptr
applicaton provided ptr for de-alloc function
coap_tick_t last_obs
Last time used (Observe tracking) or 0.
Iterator to run through PDU options.
coap_option_num_t number
decoded option number
structure for CoAP PDUs
uint8_t max_hdr_size
space reserved for protocol-specific header
uint8_t * token
first byte of token (or extended length bytes prefix), if any, or options
coap_lg_xmit_t * lg_xmit
Holds ptr to lg_xmit if sending a set of blocks.
size_t body_length
Holds body data length.
size_t max_size
maximum size for token, options and payload, or zero for variable size pdu
const uint8_t * body_data
Holds ptr to re-assembled data or NULL.
size_t body_offset
Holds body data offset.
coap_pdu_code_t code
request method (value 1–31) or response code (value 64-255)
coap_bin_const_t actual_token
Actual token in pdu.
uint8_t * data
first byte of payload, if any
coap_mid_t mid
message id, if any, in regular host byte order
uint32_t e_token_length
length of Token space (includes leading extended bytes
size_t used_size
used bytes of storage for token, options and payload
size_t body_total
Holds body data total size.
coap_pdu_type_t type
message type
Queue entry.
Structure to keep track of received blocks.
uint32_t total_blocks
Set to block no + 1 when More bit unset.
uint32_t used
Number of range blocks in use.
struct coap_lg_range range[COAP_RBLOCK_CNT]
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
coap_lg_xmit_t * lg_xmit
list of large transmissions
uint32_t block_mode
Zero or more COAP_BLOCK_ or'd options.
uint8_t csm_bert_rem_support
CSM TCP BERT blocks supported (remote).
uint64_t tx_token
Next token number to use.
coap_mid_t remote_test_mid
mid used for checking remote support
uint8_t csm_bert_loc_support
CSM TCP BERT blocks supported (local).
coap_proto_t proto
protocol used
uint8_t con_active
Active CON request sent.
coap_queue_t * delayqueue
list of delayed messages waiting to be sent
uint32_t tx_rtag
Next Request-Tag number to use.
coap_lg_srcv_t * lg_srcv
Server list of expected large receives.
coap_lg_crcv_t * lg_crcv
Client list of expected large receives.
coap_session_type_t type
client or server side socket
coap_context_t * context
session's context
coap_bin_const_t * echo
last token used to make a request
CoAP string data definition.
Definition coap_str.h:38
uint8_t * s
string data
Definition coap_str.h:40
size_t length
length of string
Definition coap_str.h:39