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