libcoap 4.3.5-develop-c63c8f7
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-2026 Olaf Bergmann <bergmann@tzi.org> and others
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 *
7 * This file is part of the CoAP library libcoap. Please see
8 * README for terms of use.
9 */
10
15
17
18#include <stdio.h>
19
20#ifndef min
21#define min(a,b) ((a) < (b) ? (a) : (b))
22#endif
23
24/* Can be 1 - 8 bytes long */
25#ifndef COAP_ETAG_MAX_BYTES
26#define COAP_ETAG_MAX_BYTES 4
27#endif
28#if COAP_ETAG_MAX_BYTES < 1 || COAP_ETAG_MAX_BYTES > 8
29#error COAP_ETAG_MAX_BYTES byte size invalid
30#endif
31
32#define COAP_LG_XMIT_TXT_SCALAR (8)
33
34#if COAP_Q_BLOCK_SUPPORT
35static int blocks_delete_entry(coap_rblock_t *rec_blocks, uint32_t block_num);
36static int blocks_add_entry(coap_rblock_t *rec_blocks, uint32_t block_num, uint32_t block_m);
37#endif /* COAP_Q_BLOCK_SUPPORT */
38
39#if COAP_Q_BLOCK_SUPPORT
40int
42 return 1;
43}
44#else /* ! COAP_Q_BLOCK_SUPPORT */
45int
47 return 0;
48}
49#endif /* ! COAP_Q_BLOCK_SUPPORT */
50
51unsigned int
52coap_opt_block_num(const coap_opt_t *block_opt) {
53 unsigned int num = 0;
54 uint16_t len;
55
56 len = coap_opt_length(block_opt);
57
58 if (len == 0) {
59 return 0;
60 }
61
62 if (len > 1) {
64 coap_opt_length(block_opt) - 1);
65 }
66
67 return (num << 4) | ((COAP_OPT_BLOCK_END_BYTE(block_opt) & 0xF0) >> 4);
68}
69
70int
71coap_get_block_b(const coap_session_t *session, const coap_pdu_t *pdu,
72 coap_option_num_t number, coap_block_b_t *block) {
73 coap_opt_iterator_t opt_iter;
74 coap_opt_t *option;
75
76 assert(block);
77 memset(block, 0, sizeof(coap_block_b_t));
78
79 if (pdu && (option = coap_check_option(pdu, number, &opt_iter)) != NULL) {
80 uint32_t num;
81
82 if (COAP_OPT_BLOCK_MORE(option))
83 block->m = 1;
84 block->aszx = block->szx = COAP_OPT_BLOCK_SZX(option);
85 if (block->szx == 7) {
86 size_t length;
87 const uint8_t *data;
88
89 if (session == NULL || COAP_PROTO_NOT_RELIABLE(session->proto) ||
90 !(session->csm_bert_rem_support && session->csm_bert_loc_support))
91 /* No BERT support */
92 return 0;
93
94 block->szx = 6; /* BERT is 1024 block chunks */
95 block->bert = 1;
96 if (coap_get_data(pdu, &length, &data)) {
97 if (block->m && (length % 1024) != 0) {
98 coap_log_debug("block: Oversized packet - reduced to %" PRIuS " from %" PRIuS "\n",
99 length - (length % 1024), length);
100 length -= length % 1024;
101 }
102 block->chunk_size = (uint32_t)length;
103 } else
104 block->chunk_size = 0;
105 } else {
106 block->chunk_size = (size_t)1 << (block->szx + 4);
107 }
108 block->defined = 1;
109
110 /* The block number is at most 20 bits, so values above 2^20 - 1
111 * are illegal. */
112 num = coap_opt_block_num(option);
113 if (num > 0xFFFFF) {
114 return 0;
115 }
116 block->num = num;
117 return 1;
118 }
119
120 return 0;
121}
122
123int
125 coap_block_t *block) {
126 coap_block_b_t block_b;
127
128 assert(block);
129 memset(block, 0, sizeof(coap_block_t));
130
131 if (coap_get_block_b(NULL, pdu, number, &block_b)) {
132 block->num = block_b.num;
133 block->m = block_b.m;
134 block->szx = block_b.szx;
135 return 1;
136 }
137 return 0;
138}
139
140static int
142 unsigned int num,
143 unsigned int blk_size, size_t total) {
144 size_t token_options = pdu->data ? (size_t)(pdu->data - pdu->token) : pdu->used_size;
145 size_t avail = pdu->max_size - token_options;
146 unsigned int start = num << (blk_size + 4);
147 unsigned int can_use_bert = block->defined == 0 || block->bert;
148
149 assert(start <= total);
150 memset(block, 0, sizeof(*block));
151 block->num = num;
152 block->szx = block->aszx = blk_size;
153 if (can_use_bert && blk_size == 6 && avail >= 1024 && session != NULL &&
154 COAP_PROTO_RELIABLE(session->proto) &&
155 session->csm_bert_rem_support && session->csm_bert_loc_support) {
156 block->bert = 1;
157 block->aszx = 7;
158 block->chunk_size = (uint32_t)((avail / 1024) * 1024);
159 } else {
160 block->chunk_size = (size_t)1 << (blk_size + 4);
161 if (avail < block->chunk_size && (total - start) >= avail) {
162 /* Need to reduce block size */
163 unsigned int szx;
164 int new_blk_size;
165
166 if (avail < 16) { /* bad luck, this is the smallest block size */
167 coap_log_debug("not enough space, even the smallest block does not fit (1)\n");
168 return 0;
169 }
170 new_blk_size = coap_flsll((long long)avail) - 5;
171 coap_log_debug("decrease block size for %" PRIuS " to %d\n", avail, new_blk_size);
172 szx = block->szx;
173 block->szx = new_blk_size;
174 block->num <<= szx - block->szx;
175 block->chunk_size = (size_t)1 << (new_blk_size + 4);
176 }
177 }
178 block->m = block->chunk_size < total - start;
179 return 1;
180}
181
182int
184 coap_pdu_t *pdu, size_t data_length) {
185 size_t start;
186 unsigned char buf[4];
187 coap_block_b_t block_b;
188
189 assert(pdu);
190
191 start = block->num << (block->szx + 4);
192 if (block->num != 0 && data_length <= start) {
193 coap_log_debug("illegal block requested\n");
194 return -2;
195 }
196
197 assert(pdu->max_size > 0);
198
199 block_b.defined = 1;
200 block_b.bert = 0;
201 if (!setup_block_b(NULL, pdu, &block_b, block->num,
202 block->szx, data_length))
203 return -3;
204
205 /* to re-encode the block option */
206 coap_update_option(pdu, number, coap_encode_var_safe(buf, sizeof(buf),
207 ((block_b.num << 4) |
208 (block_b.m << 3) |
209 block_b.szx)),
210 buf);
211
212 return 1;
213}
214
215int
217 coap_option_num_t number,
218 coap_pdu_t *pdu, size_t data_length) {
219 size_t start;
220 unsigned char buf[4];
221
222 assert(pdu);
223
224 start = block->num << (block->szx + 4);
225 if (block->num != 0 && data_length <= start) {
226 coap_log_debug("illegal block requested\n");
227 return -2;
228 }
229
230 assert(pdu->max_size > 0);
231
232 if (!setup_block_b(session, pdu, block, block->num,
233 block->szx, data_length))
234 return -3;
235
236 /* to re-encode the block option */
237 coap_update_option(pdu, number, coap_encode_var_safe(buf, sizeof(buf),
238 ((block->num << 4) |
239 (block->m << 3) |
240 block->aszx)),
241 buf);
242
243 return 1;
244}
245
246int
247coap_add_block(coap_pdu_t *pdu, size_t len, const uint8_t *data,
248 unsigned int block_num, unsigned char block_szx) {
249 unsigned int start;
250 start = block_num << (block_szx + 4);
251
252 if (len <= start)
253 return 0;
254
255 return coap_add_data(pdu,
256 min(len - start, ((size_t)1 << (block_szx + 4))),
257 data + start);
258}
259
260int
261coap_add_block_b_data(coap_pdu_t *pdu, size_t len, const uint8_t *data,
262 coap_block_b_t *block) {
263 unsigned int start = block->num << (block->szx + 4);
264 size_t max_size;
265
266 if (len <= start)
267 return 0;
268
269 if (block->bert) {
270 size_t token_options = pdu->data ? (size_t)(pdu->data - pdu->token) : pdu->used_size;
271 max_size = ((pdu->max_size - token_options) / 1024) * 1024;
272 } else {
273 max_size = (size_t)1 << (block->szx + 4);
274 }
275 block->chunk_size = (uint32_t)max_size;
276
277 return coap_add_data(pdu,
278 min(len - start, max_size),
279 data + start);
280}
281
282/*
283 * Note that the COAP_OPTION_ have to be added in the correct order
284 */
285void
287 coap_pdu_t *response,
288 uint16_t media_type,
289 int maxage,
290 size_t length,
291 const uint8_t *data
292 ) {
293 unsigned char buf[4];
294 coap_block_t block2;
295 int block2_requested = 0;
296#if COAP_SERVER_SUPPORT
297 uint64_t etag = 0;
298 coap_digest_t digest;
299 coap_digest_ctx_t *dctx = NULL;
300#endif /* COAP_SERVER_SUPPORT */
301
302 memset(&block2, 0, sizeof(block2));
303 /*
304 * Need to check that a valid block is getting asked for so that the
305 * correct options are put into the PDU.
306 */
307 if (request) {
308 if (coap_get_block(request, COAP_OPTION_BLOCK2, &block2)) {
309 block2_requested = 1;
310 if (block2.num != 0 && length <= (block2.num << (block2.szx + 4))) {
311 coap_log_debug("Illegal block requested (%d > last = %" PRIuS ")\n",
312 block2.num,
313 length >> (block2.szx + 4));
314 response->code = COAP_RESPONSE_CODE(400);
315 goto error;
316 }
317 }
318 }
319 response->code = COAP_RESPONSE_CODE(205);
320
321#if COAP_SERVER_SUPPORT
322 /* add ETag for the resource data */
323 if (length) {
324 dctx = coap_digest_setup();
325 if (!dctx)
326 goto error;
327 if (request && request->session &&
328 coap_is_mcast(&request->session->addr_info.local)) {
329 coap_digest_update(dctx, coap_unique_id, sizeof(coap_unique_id));
330 }
331 if (!coap_digest_update(dctx, data, length))
332 goto error;
333 if (!coap_digest_final(dctx, &digest))
334 goto error;
335 dctx = NULL;
336 memcpy(&etag, digest.key, sizeof(etag));
337#if COAP_ETAG_MAX_BYTES != 8
338 etag = etag >> 8*(8 - COAP_ETAG_MAX_BYTES);
339#endif
340 if (!etag)
341 etag = 1;
342 coap_update_option(response,
344 coap_encode_var_safe8(buf, sizeof(buf), etag),
345 buf);
346 }
347#endif /* COAP_SERVER_SUPPORT */
348
350 coap_encode_var_safe(buf, sizeof(buf),
351 media_type),
352 buf);
353
354 if (maxage >= 0) {
355 coap_insert_option(response,
357 coap_encode_var_safe(buf, sizeof(buf), maxage), buf);
358 }
359
360 if (block2_requested) {
361 int res;
362
363 res = coap_write_block_opt(&block2, COAP_OPTION_BLOCK2, response, length);
364
365 switch (res) {
366 case -2: /* illegal block (caught above) */
367 response->code = COAP_RESPONSE_CODE(400);
368 goto error;
369 case -1: /* should really not happen */
370 assert(0);
371 /* fall through if assert is a no-op */
372 case -3: /* cannot handle request */
373 response->code = COAP_RESPONSE_CODE(500);
374 goto error;
375 default: /* everything is good */
376 ;
377 }
378
381 coap_encode_var_safe8(buf, sizeof(buf), length),
382 buf);
383
384 coap_add_block(response, length, data,
385 block2.num, block2.szx);
386 return;
387 }
388
389 /*
390 * Block2 not requested
391 */
392 if (!coap_add_data(response, length, data)) {
393 /*
394 * Insufficient space to add in data - use block mode
395 * set initial block size, will be lowered by
396 * coap_write_block_opt() automatically
397 */
398 block2.num = 0;
399 block2.szx = 6;
400 coap_write_block_opt(&block2, COAP_OPTION_BLOCK2, response, length);
401
404 coap_encode_var_safe8(buf, sizeof(buf), length),
405 buf);
406
407 coap_add_block(response, length, data,
408 block2.num, block2.szx);
409 }
410 return;
411
412error:
413#if COAP_SERVER_SUPPORT
414 coap_digest_free(dctx);
415#endif /* COAP_SERVER_SUPPORT */
416 coap_add_data(response,
417 strlen(coap_response_phrase(response->code)),
418 (const unsigned char *)coap_response_phrase(response->code));
419}
420
421COAP_API void
423 uint32_t block_mode) {
424 coap_lock_lock(return);
425 coap_context_set_block_mode_lkd(context, block_mode);
427}
428
429void
431 uint32_t block_mode) {
433 if (!(block_mode & COAP_BLOCK_USE_LIBCOAP))
434 block_mode = 0;
436 context->block_mode |= block_mode & COAP_BLOCK_SET_MASK;
437#if ! COAP_Q_BLOCK_SUPPORT
439 coap_log_debug("Q-Block support not compiled in - ignored\n");
440#endif /* ! COAP_Q_BLOCK_SUPPORT */
441}
442
443COAP_API int
445 size_t max_block_size) {
446 int ret;
447
448 coap_lock_lock(return 0);
449 ret = coap_context_set_max_block_size_lkd(context, max_block_size);
451 return ret;
452}
453
454int
455coap_context_set_max_block_size_lkd(coap_context_t *context, size_t max_block_size) {
456 switch (max_block_size) {
457 case 0:
458 case 16:
459 case 32:
460 case 64:
461 case 128:
462 case 256:
463 case 512:
464 case 1024:
465 break;
466 default:
467 coap_log_info("coap_context_set_max_block_size: Invalid max block size (%" PRIuS ")\n",
468 max_block_size);
469 return 0;
470 }
472 max_block_size = (coap_fls((uint32_t)max_block_size >> 4) - 1) & 0x07;
474 context->block_mode |= COAP_BLOCK_MAX_SIZE_SET((uint32_t)max_block_size);
475 return 1;
476}
477
478#if COAP_CLIENT_SUPPORT
480full_match(const uint8_t *a, size_t alen,
481 const uint8_t *b, size_t blen) {
482 return alen == blen && (alen == 0 || memcmp(a, b, alen) == 0);
483}
484#endif /* COAP_CLIENT_SUPPORT */
485
488 coap_lg_xmit_t *lg_xmit = NULL;
489 coap_lg_xmit_t *m_lg_xmit = NULL;
490 uint64_t token_match =
492 pdu->actual_token.length));
493
494 LL_FOREACH(session->lg_xmit, lg_xmit) {
495 if (token_match != STATE_TOKEN_BASE(lg_xmit->b.b1.state_token) &&
496 !coap_binary_equal(&pdu->actual_token, lg_xmit->b.b1.app_token)) {
497 /* try out the next one */
498 continue;
499 }
500#if COAP_CLIENT_SUPPORT
501 if (COAP_PDU_IS_RESPONSE(pdu)) {
502 if (coap_is_mcast(&lg_xmit->b.b1.upstream)) {
503 m_lg_xmit = lg_xmit;
504 }
505 if (!coap_address_equals(&lg_xmit->b.b1.upstream, &session->addr_info.remote)) {
506 /* try out the next one */
507 continue;
508 }
509 }
510 /* Have a match */
511 return lg_xmit;
512#endif /* COAP_CLIENT_SUPPORT */
513 }
514 if (m_lg_xmit && (session->sock.flags & COAP_SOCKET_MULTICAST)) {
515 /* Need to set up unicast version of mcast lg_xmit entry */
516 lg_xmit = coap_malloc_type(COAP_LG_XMIT, sizeof(coap_lg_xmit_t));
517 if (!lg_xmit)
518 return NULL;
519 memcpy(lg_xmit, m_lg_xmit, sizeof(coap_lg_xmit_t));
520 lg_xmit->next = NULL;
521 lg_xmit->b.b1.app_token = NULL;
522 lg_xmit->data_info->ref++;
523 lg_xmit->sent_pdu = coap_pdu_reference_lkd(m_lg_xmit->sent_pdu);
524 coap_address_copy(&lg_xmit->b.b1.upstream, &session->addr_info.remote);
525 lg_xmit->b.b1.app_token = coap_new_binary(m_lg_xmit->b.b1.app_token->length);
526 if (!lg_xmit->b.b1.app_token)
527 goto fail;
528 if (m_lg_xmit->b.b1.app_token->length)
529 memcpy(lg_xmit->b.b1.app_token->s, m_lg_xmit->b.b1.app_token->s,
530 m_lg_xmit->b.b1.app_token->length);
531 LL_PREPEND(session->lg_xmit, lg_xmit);
532 coap_log_debug("** %s: lg_xmit %p mcast slave initialized\n",
533 coap_session_str(session), (void *)lg_xmit);
534 /* Allow the mcast lg_xmit to time out earlier */
535 coap_ticks(&m_lg_xmit->last_all_sent);
536#if COAP_CLIENT_SUPPORT
537 if (COAP_PDU_IS_RESPONSE(pdu)) {
538 coap_lg_crcv_t *lg_crcv;
539
540 lg_crcv = coap_find_lg_crcv(session, pdu);
541 if (lg_crcv) {
542 lg_xmit->b.b1.state_token = lg_crcv->state_token;
543 }
544 }
545#endif /* COAP_CLIENT_SUPPORT */
546 }
547 return lg_xmit;
548
549fail:
550 coap_block_delete_lg_xmit(session, lg_xmit);
551 return NULL;
552}
553
554#if COAP_CLIENT_SUPPORT
555
556COAP_API int
558 coap_pdu_type_t type) {
559 int ret;
560
561 coap_lock_lock(return 0);
562 ret = coap_cancel_observe_lkd(session, token, type);
564 return ret;
565}
566
567int
568coap_cancel_observe_lkd(coap_session_t *session, coap_binary_t *token,
569 coap_pdu_type_t type) {
570 coap_lg_crcv_t *lg_crcv, *q;
571
572 assert(session);
573 if (!session)
574 return 0;
575
577 if (!(session->block_mode & COAP_BLOCK_USE_LIBCOAP)) {
578 coap_log_debug("** %s: coap_cancel_observe: COAP_BLOCK_USE_LIBCOAP not enabled\n",
579 coap_session_str(session));
580 return 0;
581 }
582
583 LL_FOREACH_SAFE(session->lg_crcv, lg_crcv, q) {
584 if (lg_crcv->observe_set) {
585 if ((!token && !lg_crcv->app_token->length) || (token &&
586 coap_binary_equal(token, lg_crcv->app_token))) {
587 uint8_t buf[8];
588 coap_mid_t mid;
589 size_t size;
590 const uint8_t *data;
591#if COAP_Q_BLOCK_SUPPORT
592 coap_block_b_t block;
593 int using_q_block1 = coap_get_block_b(session, lg_crcv->sent_pdu,
594 COAP_OPTION_Q_BLOCK1, &block);
595#endif /* COAP_Q_BLOCK_SUPPORT */
596 coap_bin_const_t *otoken = lg_crcv->obs_token ?
597 lg_crcv->obs_token[0] ?
598 lg_crcv->obs_token[0] :
599 (coap_bin_const_t *)lg_crcv->app_token :
600 (coap_bin_const_t *)lg_crcv->app_token;
601 coap_pdu_t *pdu = coap_pdu_duplicate_lkd(lg_crcv->sent_pdu,
602 session,
603 otoken->length,
604 otoken->s,
605 NULL,
607
608 lg_crcv->observe_set = 0;
609 if (pdu == NULL)
610 return 0;
611 /* Need to make sure that this is the correct requested type */
612 pdu->type = type;
613
615 coap_encode_var_safe(buf, sizeof(buf),
617 buf);
618 if (lg_crcv->o_block_option) {
619 coap_update_option(pdu, lg_crcv->o_block_option,
620 coap_encode_var_safe(buf, sizeof(buf),
621 lg_crcv->o_blk_size),
622 buf);
623 }
624 if (lg_crcv->obs_data) {
625 coap_add_data_large_request_lkd(session, pdu,
626 lg_crcv->obs_data->length,
627 lg_crcv->obs_data->data, NULL, NULL);
628 } else if (coap_get_data(lg_crcv->sent_pdu, &size, &data)) {
629 coap_add_data_large_request_lkd(session, pdu, size, data, NULL, NULL);
630 }
631
632 /*
633 * Need to fix lg_xmit stateless token as using tokens from
634 * observe setup
635 */
636 if (pdu->lg_xmit)
637 pdu->lg_xmit->b.b1.state_token = lg_crcv->state_token;
638
639 coap_address_copy(&session->addr_info.remote, &lg_crcv->upstream);
640#if COAP_Q_BLOCK_SUPPORT
641 /* See if large xmit using Q-Block1 (but not testing Q-Block1) */
642 if (using_q_block1) {
643 mid = coap_send_q_block1(session, block, pdu, COAP_SEND_INC_PDU);
644 } else {
645 mid = coap_send_internal(session, pdu, NULL);
646 }
647#else /* ! COAP_Q_BLOCK_SUPPORT */
648 mid = coap_send_internal(session, pdu, NULL);
649#endif /* ! COAP_Q_BLOCK_SUPPORT */
650 if (mid == COAP_INVALID_MID)
651 break;
652 }
653 }
654 }
655 return 1;
656}
657
659coap_find_lg_crcv(coap_session_t *session, coap_pdu_t *pdu) {
660 coap_lg_crcv_t *lg_crcv;
661 coap_lg_crcv_t *m_lg_crcv = NULL;
662 uint64_t token_match =
664 pdu->actual_token.length));
665
666 LL_FOREACH(session->lg_crcv, lg_crcv) {
667 if (token_match != STATE_TOKEN_BASE(lg_crcv->state_token) &&
668 !coap_binary_equal(&pdu->actual_token, lg_crcv->app_token)) {
669 /* try out the next one */
670 continue;
671 }
672 if (coap_is_mcast(&lg_crcv->upstream)) {
673 m_lg_crcv = lg_crcv;
674 }
675 if (!coap_address_equals(&lg_crcv->upstream, &session->addr_info.remote)) {
676 /* try out the next one */
677 continue;
678 }
679 /* Have a match */
680 return lg_crcv;
681 }
682 if (m_lg_crcv && (session->sock.flags & COAP_SOCKET_MULTICAST)) {
683 /* Need to set up unicast version of mcast lg_crcv entry */
684 lg_crcv = coap_block_new_lg_crcv(session, m_lg_crcv->sent_pdu, NULL);
685 if (lg_crcv) {
686 if (m_lg_crcv->obs_data) {
687 m_lg_crcv->obs_data->ref++;
688 lg_crcv->obs_data = m_lg_crcv->obs_data;
689 }
690 LL_PREPEND(session->lg_crcv, lg_crcv);
691 }
692 }
693 return lg_crcv;
694}
695
696#if COAP_OSCORE_SUPPORT
698coap_retransmit_oscore_pdu(coap_session_t *session,
699 coap_pdu_t *pdu,
700 coap_opt_t *echo) {
701 coap_lg_crcv_t *lg_crcv;
702 uint8_t ltoken[8];
703 size_t ltoken_len;
704 uint64_t token;
705 const uint8_t *data;
706 size_t data_len;
707 coap_pdu_t *resend_pdu;
708 coap_block_b_t block;
709
710 lg_crcv = coap_find_lg_crcv(session, pdu);
711 if (lg_crcv) {
712 coap_opt_iterator_t opt_iter;
714 &opt_iter);
715 /* Re-send request with new token */
716 token = STATE_TOKEN_FULL(lg_crcv->state_token,
717 ++lg_crcv->retry_counter);
718 ltoken_len = coap_encode_var_safe8(ltoken, sizeof(token), token);
719 /* There could be a Block option in pdu */
720 resend_pdu = coap_pdu_duplicate_lkd(pdu, session, ltoken_len,
721 ltoken, NULL, COAP_BOOL_FALSE);
722 if (!resend_pdu)
723 goto error;
724 if (echo) {
726 coap_opt_value(echo));
727 }
728 if (opt) {
729 if (!lg_crcv->obs_token) {
730 lg_crcv->obs_token = coap_malloc_type(COAP_STRING, sizeof(lg_crcv->obs_token[0]));
731 if (!lg_crcv->obs_token) {
732 return COAP_INVALID_MID;
733 }
734 lg_crcv->obs_token_cnt = 1;
735 } else {
736 coap_delete_bin_const(lg_crcv->obs_token[0]);
737 }
738 lg_crcv->obs_token[0] = coap_new_bin_const(ltoken, ltoken_len);
739 if (lg_crcv->obs_token[0] == NULL)
740 return COAP_INVALID_MID;
741 }
742 if (coap_get_data(lg_crcv->sent_pdu, &data_len, &data)) {
743 if (coap_get_block_b(session, resend_pdu, COAP_OPTION_BLOCK1, &block)) {
744 if (data_len > block.chunk_size && block.chunk_size != 0) {
745 data_len = block.chunk_size;
746 }
747 }
748 coap_add_data(resend_pdu, data_len, data);
749 }
750
751 return coap_send_internal(session, resend_pdu, NULL);
752 }
753error:
754 return COAP_INVALID_MID;
755}
756#endif /* COAP_OSCORE_SUPPORT */
757#endif /* COAP_CLIENT_SUPPORT */
758
759#if COAP_SERVER_SUPPORT
760/*
761 * Find the response lg_xmit
762 */
764coap_find_lg_xmit_response(const coap_session_t *session,
765 const coap_pdu_t *request,
766 const coap_resource_t *resource,
767 const coap_string_t *query) {
768 coap_lg_xmit_t *lg_xmit;
769 coap_opt_iterator_t opt_iter;
770 coap_opt_t *rtag_opt = coap_check_option(request,
772 &opt_iter);
773 size_t rtag_length = rtag_opt ? coap_opt_length(rtag_opt) : 0;
774 const uint8_t *rtag = rtag_opt ? coap_opt_value(rtag_opt) : NULL;
775
776 LL_FOREACH(session->lg_xmit, lg_xmit) {
777 static coap_string_t empty = { 0, NULL};
778
779 if (COAP_PDU_IS_REQUEST(lg_xmit->sent_pdu) ||
780 resource != lg_xmit->b.b2.resource ||
781 request->code != lg_xmit->b.b2.request_method ||
782 !coap_string_equal(query ? query : &empty,
783 lg_xmit->b.b2.query ?
784 lg_xmit->b.b2.query : &empty)) {
785 /* try out the next one */
786 continue;
787 }
788 /* lg_xmit is a response */
789 if (rtag_opt || lg_xmit->b.b2.rtag_set == 1) {
790 if (!(rtag_opt && lg_xmit->b.b2.rtag_set == 1))
791 continue;
792 if (lg_xmit->b.b2.rtag_length != rtag_length ||
793 memcmp(lg_xmit->b.b2.rtag, rtag, rtag_length) != 0)
794 continue;
795 }
796 return lg_xmit;
797 }
798 return NULL;
799}
800#endif /* COAP_SERVER_SUPPORT */
801
802static int
804 const coap_pdu_t *request,
805 coap_pdu_t *pdu,
806 coap_resource_t *resource,
807 const coap_string_t *query,
808 int maxage,
809 uint64_t etag,
810 size_t length,
811 const uint8_t *data,
812 coap_release_large_data_t release_func,
813 coap_get_large_data_t get_func,
814 void *app_ptr,
815 int single_request, coap_pdu_code_t request_method) {
816
817 ssize_t avail;
818 coap_block_b_t block;
819#if COAP_Q_BLOCK_SUPPORT
820 coap_block_b_t alt_block;
821#endif /* COAP_Q_BLOCK_SUPPORT */
822 size_t chunk;
823 coap_lg_xmit_t *lg_xmit = NULL;
824 uint8_t buf[8];
825 int have_block_defined = 0;
826 uint8_t blk_size;
827 uint8_t max_blk_size;
828 uint16_t option;
829 size_t token_options;
830 coap_opt_t *opt;
831 coap_opt_iterator_t opt_iter;
832#if COAP_Q_BLOCK_SUPPORT
833 uint16_t alt_option;
834#endif /* COAP_Q_BLOCK_SUPPORT */
835
836#if !COAP_SERVER_SUPPORT
837 (void)etag;
838#endif /* COAP_SERVER_SUPPORT */
839
840 assert(pdu);
841 if (pdu->data) {
842 coap_log_warn("coap_add_data_large: PDU already contains data\n");
843 if (release_func) {
844 coap_lock_callback(release_func(session, app_ptr));
845 }
846 return 0;
847 }
848
849 if (!(session->block_mode & COAP_BLOCK_USE_LIBCOAP)) {
850 coap_log_debug("** %s: coap_add_data_large: COAP_BLOCK_USE_LIBCOAP not enabled\n",
851 coap_session_str(session));
852 goto add_data;
853 }
854
855 /* A lot of the reliable code assumes type is CON */
856 if (COAP_PROTO_RELIABLE(session->proto) && pdu->type == COAP_MESSAGE_NON)
857 pdu->type = COAP_MESSAGE_CON;
858
859 /* Block NUM max 20 bits (starting from 0) and block size is "2**(SZX + 4)"
860 and using SZX max of 6 gives maximum size = 1,073,740,800
861 CSM Max-Message-Size theoretical maximum = 4,294,967,295
862 So, if using blocks, we are limited to 1,073,740,800.
863 */
864#define MAX_BLK_LEN ((1UL << 20) * (1 << (6 + 4)))
865#if UINT_MAX < MAX_BLK_LEN
866#undef MAX_BLK_LEN
867#define MAX_BLK_LEN UINT_MAX
868#endif
869
870 if (length > MAX_BLK_LEN) {
871 coap_log_warn("Size of large buffer restricted to 0x%lx bytes\n", MAX_BLK_LEN);
872 length = MAX_BLK_LEN;
873 }
874
875#if COAP_SERVER_SUPPORT
876 /* Possible response code not yet set, so check if not request */
877 if (!COAP_PDU_IS_REQUEST(pdu) && length) {
878 coap_opt_t *etag_opt = coap_check_option(pdu, COAP_OPTION_ETAG, &opt_iter);
879
880 if (etag_opt) {
881 /* Have to use ETag as supplied in the response PDU */
882 etag = coap_decode_var_bytes8(coap_opt_value(etag_opt),
883 coap_opt_length(etag_opt));
884 } else {
885 if (!etag) {
886 /* calculate ETag for the response */
887 coap_digest_t digest;
888 coap_digest_ctx_t *dctx = coap_digest_setup();
889
890 if (dctx) {
891 if (coap_is_mcast(&session->addr_info.local)) {
892 (void)coap_digest_update(dctx, coap_unique_id, sizeof(coap_unique_id));
893 }
894 if (coap_digest_update(dctx, data, length)) {
895 if (coap_digest_final(dctx, &digest)) {
896 memcpy(&etag, digest.key, sizeof(etag));
897#if COAP_ETAG_MAX_BYTES != 8
898 etag = etag >> 8*(8 - COAP_ETAG_MAX_BYTES);
899#endif
900 dctx = NULL;
901 }
902 }
903 coap_digest_free(dctx);
904 }
905 if (!etag)
906 etag = 1;
907 }
910 coap_encode_var_safe8(buf, sizeof(buf), etag),
911 buf);
912 }
913 if (request) {
914 etag_opt = coap_check_option(request, COAP_OPTION_ETAG, &opt_iter);
915 if (etag_opt) {
916 /* There may be multiple ETag - need to check each one */
917 coap_option_iterator_init(request, &opt_iter, COAP_OPT_ALL);
918 while ((etag_opt = coap_option_next(&opt_iter))) {
919 if (opt_iter.number == COAP_OPTION_ETAG) {
920 uint64_t etag_r = coap_decode_var_bytes8(coap_opt_value(etag_opt),
921 coap_opt_length(etag_opt));
922
923 if (etag == etag_r) {
924 pdu->code = COAP_RESPONSE_CODE(203);
925 return 1;
926 }
927 }
928 }
929 }
930 }
931 }
932#endif /* COAP_SERVER_SUPPORT */
933
934 /* Determine the block size to use, adding in sensible options if needed */
935 if (COAP_PDU_IS_REQUEST(pdu)) {
937
938#if COAP_Q_BLOCK_SUPPORT
939 if (session->block_mode & (COAP_BLOCK_HAS_Q_BLOCK|COAP_BLOCK_TRY_Q_BLOCK)) {
940 option = COAP_OPTION_Q_BLOCK1;
941 alt_option = COAP_OPTION_BLOCK1;
942 } else {
943 option = COAP_OPTION_BLOCK1;
944 alt_option = COAP_OPTION_Q_BLOCK1;
945 }
946#else /* ! COAP_Q_BLOCK_SUPPORT */
947 if (coap_get_block_b(session, pdu, COAP_OPTION_Q_BLOCK1, &block)) {
949 }
950 option = COAP_OPTION_BLOCK1;
951#endif /* ! COAP_Q_BLOCK_SUPPORT */
952
953 /* See if this token is already in use for large bodies (unlikely) */
954 LL_FOREACH_SAFE(session->lg_xmit, lg_xmit, q) {
955 if (coap_binary_equal(&pdu->actual_token, lg_xmit->b.b1.app_token)) {
956 /* Unfortunately need to free this off as potential size change */
957 int is_mcast = 0;
958 coap_tick_t last_all_sent = lg_xmit->last_all_sent;
959#if COAP_CLIENT_SUPPORT
960 is_mcast = coap_is_mcast(&lg_xmit->b.b1.upstream);
961#endif /* COAP_CLIENT_SUPPORT */
962 LL_DELETE(session->lg_xmit, lg_xmit);
963 coap_block_delete_lg_xmit(session, lg_xmit);
964 lg_xmit = NULL;
965 if (!is_mcast && !last_all_sent)
967 break;
968 }
969 }
970 } else {
971 /* Have to assume that it is a response even if code is 0.00 */
972 assert(resource);
973#if COAP_Q_BLOCK_SUPPORT
974 if (session->block_mode & COAP_BLOCK_HAS_Q_BLOCK) {
975 option = COAP_OPTION_Q_BLOCK2;
976 alt_option = COAP_OPTION_BLOCK2;
977 } else {
978 option = COAP_OPTION_BLOCK2;
979 alt_option = COAP_OPTION_Q_BLOCK2;
980 }
981#else /* ! COAP_Q_BLOCK_SUPPORT */
982 if (coap_get_block_b(session, pdu, COAP_OPTION_Q_BLOCK2, &block)) {
984 }
985 option = COAP_OPTION_BLOCK2;
986#endif /* ! COAP_Q_BLOCK_SUPPORT */
987#if COAP_SERVER_SUPPORT
988 /*
989 * Check if resource+query+rtag is already in use for large bodies
990 * (unlikely)
991 */
992 lg_xmit = coap_find_lg_xmit_response(session, request, resource, query);
993 if (lg_xmit) {
994 coap_tick_t last_all_sent = lg_xmit->last_all_sent;
995 /* Unfortunately need to free this off as potential size change */
996 LL_DELETE(session->lg_xmit, lg_xmit);
997 coap_block_delete_lg_xmit(session, lg_xmit);
998 lg_xmit = NULL;
999 if (!last_all_sent)
1001 }
1002#endif /* COAP_SERVER_SUPPORT */
1003 }
1004#if COAP_OSCORE_SUPPORT
1005 if (session->oscore_encryption) {
1006 /* Need to convert Proxy-Uri to Proxy-Scheme option if needed */
1008 goto fail;
1009 }
1010#endif /* COAP_OSCORE_SUPPORT */
1011
1012 token_options = pdu->data ? (size_t)(pdu->data - pdu->token) : pdu->used_size;
1013 avail = pdu->max_size - token_options;
1014 /* There may be a response with Echo option */
1016#if COAP_OSCORE_SUPPORT
1017 avail -= coap_oscore_overhead(session, pdu);
1018#endif /* COAP_OSCORE_SUPPORT */
1019 /* May need token of length 8, so account for this */
1020 avail -= (pdu->actual_token.length < 8) ? 8 - pdu->actual_token.length : 0;
1021
1022 if (avail < 16) {
1023 blk_size = 0;
1024 } else {
1025 blk_size = coap_flsll((long long)avail) - 4 - 1;
1026 }
1027 if (blk_size > 6)
1028 blk_size = 6;
1029
1030 max_blk_size = COAP_BLOCK_MAX_SIZE_GET(session->block_mode);
1031 if (max_blk_size && blk_size > max_blk_size)
1032 blk_size = max_blk_size;
1033
1034 /* see if BlockX defined - if so update blk_size as given by app */
1035 if (coap_get_block_b(session, pdu, option, &block)) {
1036 if (block.szx < blk_size)
1037 blk_size = block.szx;
1038 have_block_defined = 1;
1039 }
1040#if COAP_Q_BLOCK_SUPPORT
1041 /* see if alternate BlockX defined */
1042 if (coap_get_block_b(session, pdu, alt_option, &alt_block)) {
1043 if (have_block_defined) {
1044 /* Cannot have both options set */
1045 coap_log_warn("Both BlockX and Q-BlockX cannot be set at the same time\n");
1046 coap_remove_option(pdu, alt_option);
1047 } else {
1048 block = alt_block;
1049 if (block.szx < blk_size)
1050 blk_size = block.szx;
1051 have_block_defined = 1;
1052 option = alt_option;
1053 }
1054 }
1055#endif /* COAP_Q_BLOCK_SUPPORT */
1056
1057 if (avail < 16 && ((ssize_t)length > avail || have_block_defined)) {
1058 /* bad luck, this is the smallest block size */
1059 coap_log_debug("not enough space, even the smallest block does not fit (2)\n");
1060 goto fail;
1061 }
1062
1063 chunk = (size_t)1 << (blk_size + 4);
1064 if ((have_block_defined && block.num != 0) || single_request ||
1065 ((session->block_mode & COAP_BLOCK_STLESS_BLOCK2) && session->type != COAP_SESSION_TYPE_CLIENT)) {
1066 /* App is defining a single block to send or we are stateless */
1067 size_t rem;
1068
1069 if (length >= block.num * chunk) {
1070#if COAP_SERVER_SUPPORT
1071 if (session->block_mode & COAP_BLOCK_STLESS_BLOCK2 && session->type != COAP_SESSION_TYPE_CLIENT) {
1072 /* We are running server stateless */
1075 coap_encode_var_safe(buf, sizeof(buf),
1076 (unsigned int)length),
1077 buf);
1078 if (request) {
1079 if (!coap_get_block_b(session, request, option, &block))
1080 block.num = 0;
1081 }
1082 if (!setup_block_b(session, pdu, &block, block.num,
1083 blk_size, length))
1084 goto fail;
1085
1086 /* Add in with requested block num, more bit and block size */
1088 option,
1089 coap_encode_var_safe(buf, sizeof(buf),
1090 (block.num << 4) | (block.m << 3) | block.aszx),
1091 buf);
1092 }
1093#endif /* COAP_SERVER_SUPPORT */
1094 rem = chunk;
1095 if (chunk > length - block.num * chunk)
1096 rem = length - block.num * chunk;
1097 if (!coap_add_data(pdu, rem, &data[block.num * chunk]))
1098 goto fail;
1099 }
1100 if (release_func) {
1101 coap_lock_callback(release_func(session, app_ptr));
1102 }
1103 } else if ((have_block_defined && length > chunk) || (ssize_t)length > avail) {
1104 /* Only add in lg_xmit if more than one block needs to be handled */
1105 size_t rem;
1106
1107 lg_xmit = coap_malloc_type(COAP_LG_XMIT, sizeof(coap_lg_xmit_t));
1108 if (!lg_xmit)
1109 goto fail;
1110
1111 /* Set up for displaying all the data in the pdu */
1112 if (!get_func) {
1113 pdu->body_data = data;
1114 pdu->body_length = length;
1115 coap_log_debug("PDU presented by app.\n");
1117 pdu->body_data = NULL;
1118 pdu->body_length = 0;
1119 } else {
1120 coap_log_debug("PDU presented by app without data.\n");
1122 }
1123
1124 coap_log_debug("** %s: lg_xmit %p initialized\n",
1125 coap_session_str(session), (void *)lg_xmit);
1126 /* Update lg_xmit with large data information */
1127 memset(lg_xmit, 0, sizeof(coap_lg_xmit_t));
1128 lg_xmit->blk_size = blk_size;
1129 lg_xmit->option = option;
1130 lg_xmit->data_info = coap_malloc_type(COAP_STRING, sizeof(coap_lg_xmit_data_t));
1131 if (!lg_xmit->data_info)
1132 goto fail;
1133 lg_xmit->data_info->ref = 0;
1134 lg_xmit->data_info->data = data;
1135 lg_xmit->data_info->length = length;
1136#if COAP_Q_BLOCK_SUPPORT
1137 lg_xmit->non_timeout_random_ticks =
1139#endif /* COAP_Q_BLOCK_SUPPORT */
1140 lg_xmit->data_info->get_func = get_func;
1141 lg_xmit->data_info->release_func = release_func;
1142 lg_xmit->data_info->app_ptr = app_ptr;
1143 pdu->lg_xmit = lg_xmit;
1144 coap_ticks(&lg_xmit->last_obs);
1145 coap_ticks(&lg_xmit->last_sent);
1146 if (COAP_PDU_IS_REQUEST(pdu)) {
1147 /* Need to keep original token for updating response PDUs */
1148 lg_xmit->b.b1.app_token = coap_new_binary(pdu->actual_token.length);
1149 if (!lg_xmit->b.b1.app_token)
1150 goto fail;
1151 memcpy(lg_xmit->b.b1.app_token->s, pdu->actual_token.s,
1152 pdu->actual_token.length);
1153 /*
1154 * Need to set up new token for use during transmits
1155 * RFC9177#section-5
1156 */
1157 lg_xmit->b.b1.count = 1;
1158 lg_xmit->b.b1.state_token = STATE_TOKEN_FULL(++session->tx_token,
1159 lg_xmit->b.b1.count);
1160 coap_address_copy(&lg_xmit->b.b1.upstream, &session->addr_info.remote);
1161 /*
1162 * Token will be updated in pdu later as original pdu may be needed in
1163 * coap_send()
1164 */
1167 coap_encode_var_safe(buf, sizeof(buf),
1168 (unsigned int)length),
1169 buf);
1170 if (!coap_check_option(pdu, COAP_OPTION_RTAG, &opt_iter))
1173 coap_encode_var_safe(buf, sizeof(buf),
1174 ++session->tx_rtag),
1175 buf);
1176 } else {
1177 /*
1178 * resource+query+rtag match is used for Block2 large body transmissions
1179 * token match is used for Block1 large body transmissions
1180 */
1181 lg_xmit->b.b2.resource = resource;
1182 if (query) {
1183 lg_xmit->b.b2.query = coap_new_string(query->length);
1184 if (lg_xmit->b.b2.query) {
1185 memcpy(lg_xmit->b.b2.query->s, query->s, query->length);
1186 }
1187 } else {
1188 lg_xmit->b.b2.query = NULL;
1189 }
1190 opt = coap_check_option(request, COAP_OPTION_RTAG, &opt_iter);
1191 if (opt) {
1192 lg_xmit->b.b2.rtag_length = (uint8_t)min(coap_opt_length(opt),
1193 sizeof(lg_xmit->b.b2.rtag));
1194 memcpy(lg_xmit->b.b2.rtag, coap_opt_value(opt), lg_xmit->b.b2.rtag_length);
1195 lg_xmit->b.b2.rtag_set = 1;
1196 } else {
1197 lg_xmit->b.b2.rtag_set = 0;
1198 }
1199 lg_xmit->b.b2.etag = etag;
1200 lg_xmit->b.b2.request_method = request_method;
1201 if (maxage >= 0) {
1202 coap_tick_t now;
1203
1204 coap_ticks(&now);
1205 lg_xmit->b.b2.maxage_expire = coap_ticks_to_rt(now) + maxage;
1206 } else {
1207 lg_xmit->b.b2.maxage_expire = 0;
1208 }
1211 coap_encode_var_safe(buf, sizeof(buf),
1212 (unsigned int)length),
1213 buf);
1214 }
1215
1216 if (!setup_block_b(session, pdu, &block, block.num,
1217 blk_size, lg_xmit->data_info->length))
1218 goto fail;
1219
1220 /* Add in with requested block num, more bit and block size */
1222 lg_xmit->option,
1223 coap_encode_var_safe(buf, sizeof(buf),
1224 (block.num << 4) | (block.m << 3) | block.aszx),
1225 buf);
1226
1227 /* Reference PDU to use as a basis for all the subsequent blocks */
1228 lg_xmit->sent_pdu = coap_pdu_reference_lkd(pdu);
1229
1230 /* Check we still have space after adding in some options */
1231 token_options = pdu->data ? (size_t)(pdu->data - pdu->token) : pdu->used_size;
1232 avail = pdu->max_size - token_options;
1233 /* There may be a response with Echo option */
1235 /* May need token of length 8, so account for this */
1236 avail -= (pdu->actual_token.length < 8) ? 8 - pdu->actual_token.length : 0;
1237#if COAP_OSCORE_SUPPORT
1238 avail -= coap_oscore_overhead(session, pdu);
1239#endif /* COAP_OSCORE_SUPPORT */
1240 if (avail < (ssize_t)chunk) {
1241 /* chunk size change down */
1242 if (avail < 16) {
1243 coap_log_warn("not enough space, even the smallest block does not fit (3)\n");
1244 goto fail;
1245 }
1246 blk_size = coap_flsll((long long)avail) - 4 - 1;
1247 block.num = block.num << (lg_xmit->blk_size - blk_size);
1248 lg_xmit->blk_size = blk_size;
1249 chunk = (size_t)1 << (lg_xmit->blk_size + 4);
1250 block.chunk_size = (uint32_t)chunk;
1251 block.bert = 0;
1253 lg_xmit->option,
1254 coap_encode_var_safe(buf, sizeof(buf),
1255 (block.num << 4) | (block.m << 3) | lg_xmit->blk_size),
1256 buf);
1257 }
1258#if COAP_Q_BLOCK_SUPPORT
1259 /* Set up send_blocks */
1260 lg_xmit->send_blocks.used = 1;
1261 lg_xmit->send_blocks.range[0].end = (uint32_t)((lg_xmit->data_info->length - 1) / chunk);
1262#endif /* COAP_Q_BLOCK_SUPPORT */
1263
1264 rem = block.chunk_size;
1265 if (rem > lg_xmit->data_info->length - block.num * chunk)
1266 rem = lg_xmit->data_info->length - block.num * chunk;
1267 if (get_func) {
1268#if COAP_CONSTRAINED_STACK
1269 /* Protected by global_lock if needed */
1270 static uint8_t l_data[1024];
1271 uint8_t *data_buf = l_data;
1272#else /* ! COAP_CONSTRAINED_STACK */
1273 uint8_t l_data[1024];
1274 uint8_t *data_buf = l_data;
1275#endif /* ! COAP_CONSTRAINED_STACK */
1276 size_t l_length;
1277 int data_buf_allocated = 0;
1278
1279 if (rem > sizeof(l_data)) {
1280 data_buf = coap_malloc_type(COAP_STRING, rem);
1281 if (!data_buf) {
1282 coap_log_warn("out of memory allocating %" PRIuS " bytes for block data\n", rem);
1283 goto fail;
1284 }
1285 data_buf_allocated = 1;
1286 }
1287 if (get_func(session, rem, block.num * chunk, data_buf, &l_length,
1288 lg_xmit->data_info->app_ptr)) {
1289 if (!coap_add_data(pdu, l_length, data_buf)) {
1290 if (data_buf_allocated)
1291 coap_free_type(COAP_STRING, data_buf);
1292 goto fail;
1293 }
1294 }
1295 if (data_buf_allocated)
1296 coap_free_type(COAP_STRING, data_buf);
1297 } else {
1298 if (!coap_add_data(pdu, rem, &data[block.num * chunk]))
1299 goto fail;
1300 }
1301
1302 if (COAP_PDU_IS_REQUEST(pdu))
1303 lg_xmit->b.b1.bert_size = rem;
1304
1305 lg_xmit->last_block = -1;
1306
1307 /* Link the new lg_xmit in */
1308 LL_PREPEND(session->lg_xmit,lg_xmit);
1309 } else {
1310 /* No need to use blocks */
1311 if (have_block_defined) {
1313 option,
1314 coap_encode_var_safe(buf, sizeof(buf),
1315 (0 << 4) | (0 << 3) | blk_size), buf);
1316 if (COAP_PDU_IS_REQUEST(pdu)) {
1319 coap_encode_var_safe(buf, sizeof(buf),
1320 (unsigned int)length),
1321 buf);
1322 } else {
1325 coap_encode_var_safe(buf, sizeof(buf),
1326 (unsigned int)length),
1327 buf);
1328 }
1329 }
1330add_data:
1331 if (get_func) {
1332 uint8_t *l_data = coap_malloc_type(COAP_STRING, length);
1333 size_t l_length;
1334
1335 if (get_func(session, length, 0, l_data, &l_length, app_ptr)) {
1336 if (!coap_add_data(pdu, l_length, l_data)) {
1337 coap_free_type(COAP_STRING, l_data);
1338 goto fail;
1339 }
1340 coap_free_type(COAP_STRING, l_data);
1341 }
1342 } else {
1343 if (!coap_add_data(pdu, length, data))
1344 goto fail;
1345 }
1346
1347 if (release_func) {
1348 coap_lock_callback(release_func(session, app_ptr));
1349 }
1350 }
1351 return 1;
1352
1353fail:
1354 if (lg_xmit) {
1355 coap_block_delete_lg_xmit(session, lg_xmit);
1356 } else if (release_func) {
1357 coap_lock_callback(release_func(session, app_ptr));
1358 }
1359 return 0;
1360}
1361
1362#if COAP_CLIENT_SUPPORT
1363COAP_API int
1365 coap_pdu_t *pdu,
1366 size_t length,
1367 const uint8_t *data,
1368 coap_release_large_data_t release_func,
1369 void *app_ptr
1370 ) {
1371 int ret;
1372
1373 coap_lock_lock(return 0);
1374 ret = coap_add_data_large_request_lkd(session, pdu, length, data,
1375 release_func, app_ptr);
1377 return ret;
1378}
1379
1380int
1381coap_add_data_large_request_lkd(coap_session_t *session,
1382 coap_pdu_t *pdu,
1383 size_t length,
1384 const uint8_t *data,
1385 coap_release_large_data_t release_func,
1386 void *app_ptr) {
1387 /*
1388 * Delay if session->doing_first is set.
1389 * E.g. Reliable and CSM not in yet for checking block support
1390 */
1391 if (coap_client_delay_first(session) == 0) {
1392 if (release_func) {
1393 coap_lock_callback(release_func(session, app_ptr));
1394 }
1395 return 0;
1396 }
1397 return coap_add_data_large_internal(session, NULL, pdu, NULL, NULL, -1, 0,
1398 length, data, release_func, NULL, app_ptr, 0, 0);
1399}
1400
1401COAP_API int
1403 coap_pdu_t *pdu,
1404 size_t length,
1405 coap_release_large_data_t release_func,
1406 coap_get_large_data_t get_func,
1407 void *app_ptr) {
1408 int ret;
1409
1410 coap_lock_lock(return 0);
1411 ret = coap_add_data_large_request_app_lkd(session, pdu, length,
1412 release_func, get_func, app_ptr);
1414 return ret;
1415}
1416
1417int
1418coap_add_data_large_request_app_lkd(coap_session_t *session,
1419 coap_pdu_t *pdu,
1420 size_t length,
1421 coap_release_large_data_t release_func,
1422 coap_get_large_data_t get_func,
1423 void *app_ptr) {
1424 /*
1425 * Delay if session->doing_first is set.
1426 * E.g. Reliable and CSM not in yet for checking block support
1427 */
1428 if (coap_client_delay_first(session) == 0) {
1429 return 0;
1430 }
1431 return coap_add_data_large_internal(session, NULL, pdu, NULL, NULL, -1, 0,
1432 length, NULL, release_func, get_func,
1433 app_ptr, 0, 0);
1434}
1435#endif /* ! COAP_CLIENT_SUPPORT */
1436
1437#if COAP_SERVER_SUPPORT
1438COAP_API int
1440 coap_session_t *session,
1441 const coap_pdu_t *request,
1442 coap_pdu_t *response,
1443 const coap_string_t *query,
1444 uint16_t media_type,
1445 int maxage,
1446 uint64_t etag,
1447 size_t length,
1448 const uint8_t *data,
1449 coap_release_large_data_t release_func,
1450 void *app_ptr) {
1451 int ret;
1452
1453 coap_lock_lock(return 0);
1454 ret = coap_add_data_large_response_lkd(resource, session, request,
1455 response, query, media_type, maxage, etag,
1456 length, data, release_func, app_ptr);
1458 return ret;
1459}
1460
1461int
1462coap_add_data_large_response_lkd(coap_resource_t *resource,
1463 coap_session_t *session,
1464 const coap_pdu_t *request,
1465 coap_pdu_t *response,
1466 const coap_string_t *query,
1467 uint16_t media_type,
1468 int maxage,
1469 uint64_t etag,
1470 size_t length,
1471 const uint8_t *data,
1472 coap_release_large_data_t release_func,
1473 void *app_ptr
1474 ) {
1475 unsigned char buf[4];
1476 coap_block_b_t block;
1477 int block_requested = 0;
1478 int single_request = 0;
1479#if COAP_Q_BLOCK_SUPPORT
1480 uint32_t block_opt = (session->block_mode & COAP_BLOCK_HAS_Q_BLOCK) ?
1482#else /* ! COAP_Q_BLOCK_SUPPORT */
1483 uint16_t block_opt = COAP_OPTION_BLOCK2;
1484#endif /* ! COAP_Q_BLOCK_SUPPORT */
1485
1486 memset(&block, 0, sizeof(block));
1487 /*
1488 * Need to check that a valid block is getting asked for so that the
1489 * correct options are put into the PDU.
1490 */
1491 if (request) {
1492 if (coap_get_block_b(session, request, COAP_OPTION_BLOCK2, &block)) {
1493 block_requested = 1;
1494 if (block.num != 0 && length <= (block.num << (block.szx + 4))) {
1495 coap_log_debug("Illegal block requested (%d > last = %" PRIuS ")\n",
1496 block.num,
1497 length >> (block.szx + 4));
1498 response->code = COAP_RESPONSE_CODE(400);
1499 goto error;
1500 }
1501 }
1502#if COAP_Q_BLOCK_SUPPORT
1503 else if (coap_get_block_b(session, request, COAP_OPTION_Q_BLOCK2, &block)) {
1504 block_requested = 1;
1505 if (block.num != 0 && length <= (block.num << (block.szx + 4))) {
1506 coap_log_debug("Illegal block requested (%d > last = %" PRIuS ")\n",
1507 block.num,
1508 length >> (block.szx + 4));
1509 response->code = COAP_RESPONSE_CODE(400);
1510 goto error;
1511 }
1512 if (!(session->block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
1513 set_block_mode_has_q(session->block_mode);
1514 block_opt = COAP_OPTION_Q_BLOCK2;
1515 }
1516 if (block.m == 0)
1517 single_request = 1;
1518 }
1519#endif /* COAP_Q_BLOCK_SUPPORT */
1520 }
1521
1523 coap_encode_var_safe(buf, sizeof(buf),
1524 media_type),
1525 buf);
1526
1527 if (maxage >= 0) {
1528 coap_update_option(response,
1530 coap_encode_var_safe(buf, sizeof(buf), maxage), buf);
1531 }
1532
1533 if (block_requested) {
1534 int res;
1535
1536 res = coap_write_block_b_opt(session, &block, block_opt, response,
1537 length);
1538
1539 switch (res) {
1540 case -2: /* illegal block (caught above) */
1541 response->code = COAP_RESPONSE_CODE(400);
1542 goto error;
1543 case -1: /* should really not happen */
1544 assert(0);
1545 /* fall through if assert is a no-op */
1546 case -3: /* cannot handle request */
1547 response->code = COAP_RESPONSE_CODE(500);
1548 goto error;
1549 default: /* everything is good */
1550 ;
1551 }
1552 }
1553
1554 /* add data body */
1555 if (request &&
1556 !coap_add_data_large_internal(session, request, response, resource,
1557 query, maxage, etag, length, data,
1558 release_func, NULL, app_ptr, single_request,
1559 request->code)) {
1560 response->code = COAP_RESPONSE_CODE(500);
1561 goto error_released;
1562 }
1563
1564 return 1;
1565
1566error:
1567 if (release_func) {
1568 coap_lock_callback(release_func(session, app_ptr));
1569 }
1570error_released:
1571#if COAP_ERROR_PHRASE_LENGTH > 0
1572 coap_add_data(response,
1573 strlen(coap_response_phrase(response->code)),
1574 (const unsigned char *)coap_response_phrase(response->code));
1575#endif /* COAP_ERROR_PHRASE_LENGTH > 0 */
1576 return 0;
1577}
1578#endif /* ! COAP_SERVER_SUPPORT */
1579
1580/*
1581 * return 1 if there is a future expire time, else 0.
1582 * update tim_rem with remaining value if return is 1.
1583 */
1584int
1586 coap_tick_t *tim_rem) {
1587 coap_lg_xmit_t *lg_xmit;
1588 coap_lg_xmit_t *q;
1589#if COAP_Q_BLOCK_SUPPORT
1591#else /* ! COAP_Q_BLOCK_SUPPORT */
1592 coap_tick_t idle_timeout = 8 * COAP_TICKS_PER_SECOND;
1593#endif /* ! COAP_Q_BLOCK_SUPPORT */
1594 coap_tick_t partial_timeout = COAP_MAX_TRANSMIT_WAIT_TICKS(session);
1595 int ret = 0;
1596
1597 *tim_rem = COAP_MAX_DELAY_TICKS;
1598
1599 LL_FOREACH_SAFE(session->lg_xmit, lg_xmit, q) {
1600 if (lg_xmit->last_all_sent) {
1601 if (lg_xmit->last_all_sent + idle_timeout <= now) {
1602 /* Expire this entry */
1603 LL_DELETE(session->lg_xmit, lg_xmit);
1604 coap_block_delete_lg_xmit(session, lg_xmit);
1605 } else {
1606 /* Delay until the lg_xmit needs to expire */
1607 if (*tim_rem > lg_xmit->last_all_sent + idle_timeout - now) {
1608 *tim_rem = lg_xmit->last_all_sent + idle_timeout - now;
1609 ret = 1;
1610 }
1611 }
1612 } else if (lg_xmit->last_sent) {
1613 if (lg_xmit->last_sent + partial_timeout <= now) {
1614 /* Expire this entry */
1615 coap_tick_t last_all_sent = lg_xmit->last_all_sent;
1616 int is_mcast = 0;
1617#if COAP_CLIENT_SUPPORT
1618 is_mcast = COAP_PDU_IS_REQUEST(lg_xmit->sent_pdu) &&
1619 coap_is_mcast(&lg_xmit->b.b1.upstream);
1620#endif /* COAP_CLIENT_SUPPORT */
1621 LL_DELETE(session->lg_xmit, lg_xmit);
1622
1623 coap_block_delete_lg_xmit(session, lg_xmit);
1624 if (!is_mcast && !last_all_sent)
1626 } else {
1627 /* Delay until the lg_xmit needs to expire */
1628 if (*tim_rem > lg_xmit->last_sent + partial_timeout - now) {
1629 *tim_rem = lg_xmit->last_sent + partial_timeout - now;
1630 ret = 1;
1631 }
1632 }
1633 }
1634 }
1635 return ret;
1636}
1637
1638#if COAP_CLIENT_SUPPORT
1639#if COAP_Q_BLOCK_SUPPORT
1640static coap_pdu_t *
1641coap_build_missing_pdu(coap_session_t *session, coap_lg_crcv_t *lg_crcv) {
1642 coap_pdu_t *pdu;
1643 coap_opt_filter_t drop_options;
1644 uint64_t token = STATE_TOKEN_FULL(lg_crcv->state_token, ++lg_crcv->retry_counter);
1645 uint8_t buf[8];
1646 size_t len = coap_encode_var_safe8(buf, sizeof(token), token);
1647
1648 memset(&drop_options, 0, sizeof(coap_opt_filter_t));
1652 pdu = coap_pdu_duplicate_lkd(lg_crcv->sent_pdu, session, len, buf,
1653 &drop_options, COAP_BOOL_FALSE);
1654 if (!pdu)
1655 return NULL;
1656 pdu->type = lg_crcv->last_type;
1657 return pdu;
1658}
1659
1660static void
1661coap_request_missing_q_block2(coap_session_t *session, coap_lg_crcv_t *lg_crcv) {
1662 uint8_t buf[8];
1663 uint32_t i;
1664 int block = -1; /* Last one seen */
1665 size_t sofar;
1666 size_t block_size;
1667 coap_pdu_t *pdu = NULL;
1668 int block_payload_set = -1;
1669
1670 if (session->block_mode & COAP_BLOCK_USE_M_Q_BLOCK) {
1671 /*
1672 * See if it is safe to use the single 'M' block variant of request
1673 *
1674 * If any blocks seen, then missing blocks are after range[0].end and
1675 * terminate on the last block or before range[1].begin if set.
1676 * If not defined or range[1].begin is in a different payload set then
1677 * safe to use M bit.
1678 */
1679 if (lg_crcv->rec_blocks.used &&
1680 (lg_crcv->rec_blocks.used < 2 ||
1681 ((lg_crcv->rec_blocks.range[0].end + 1) / COAP_MAX_PAYLOADS(session) !=
1682 (lg_crcv->rec_blocks.range[1].begin -1) / COAP_MAX_PAYLOADS(session)))) {
1683 block = lg_crcv->rec_blocks.range[0].end + 1;
1684 block_size = (size_t)1 << (lg_crcv->szx + 4);
1685 sofar = block * block_size;
1686 if (sofar < lg_crcv->total_len) {
1687 /* Ask for missing blocks */
1688 if (pdu == NULL) {
1689 pdu = coap_build_missing_pdu(session, lg_crcv);
1690 if (!pdu)
1691 return;
1692 }
1694 coap_encode_var_safe(buf, sizeof(buf),
1695 (block << 4) | (1 << 3) | lg_crcv->szx),
1696 buf);
1697 block_payload_set = block / COAP_MAX_PAYLOADS(session);
1698 goto send_it;
1699 }
1700 }
1701 }
1702 block = -1;
1703 for (i = 0; i < lg_crcv->rec_blocks.used; i++) {
1704 if (block < (int)lg_crcv->rec_blocks.range[i].begin &&
1705 lg_crcv->rec_blocks.range[i].begin != 0) {
1706 /* Ask for missing blocks */
1707 if (pdu == NULL) {
1708 pdu = coap_build_missing_pdu(session, lg_crcv);
1709 if (!pdu)
1710 continue;
1711 }
1712 block++;
1713 if (block_payload_set == -1)
1714 block_payload_set = block / COAP_MAX_PAYLOADS(session);
1715 for (; block < (int)lg_crcv->rec_blocks.range[i].begin &&
1716 block_payload_set == (block / COAP_MAX_PAYLOADS(session)); block++) {
1718 coap_encode_var_safe(buf, sizeof(buf),
1719 (block << 4) | (0 << 3) | lg_crcv->szx),
1720 buf);
1721 }
1722 }
1723 if (block < (int)lg_crcv->rec_blocks.range[i].end) {
1724 block = lg_crcv->rec_blocks.range[i].end;
1725 }
1726 }
1727 block_size = (size_t)1 << (lg_crcv->szx + 4);
1728 sofar = (block + 1) * block_size;
1729 if (sofar < lg_crcv->total_len) {
1730 /* Ask for trailing missing blocks */
1731 if (pdu == NULL) {
1732 pdu = coap_build_missing_pdu(session, lg_crcv);
1733 if (!pdu)
1734 return;
1735 }
1736 sofar = (lg_crcv->total_len + block_size - 1)/block_size;
1737 block++;
1738 if (block_payload_set == -1)
1739 block_payload_set = block / COAP_MAX_PAYLOADS(session);
1740 for (; block < (ssize_t)sofar &&
1741 block_payload_set == (block / COAP_MAX_PAYLOADS(session)); block++) {
1743 coap_encode_var_safe(buf, sizeof(buf),
1744 (block << 4) | (0 << 3) | lg_crcv->szx),
1745 buf);
1746 }
1747 }
1748send_it:
1749 if (pdu)
1750 coap_send_internal(session, pdu, NULL);
1751 lg_crcv->rec_blocks.retry++;
1752 if (block_payload_set != -1)
1753 lg_crcv->rec_blocks.processing_payload_set = block_payload_set;
1754 coap_ticks(&lg_crcv->rec_blocks.last_seen);
1755}
1756#endif /* COAP_Q_BLOCK_SUPPORT */
1757
1758/*
1759 * return 1 if there is a future expire time, else 0.
1760 * update tim_rem with remaining value if return is 1.
1761 */
1762int
1763coap_block_check_lg_crcv_timeouts(coap_session_t *session, coap_tick_t now,
1764 coap_tick_t *tim_rem) {
1765 coap_lg_crcv_t *lg_crcv;
1766 coap_lg_crcv_t *q;
1767 coap_tick_t partial_timeout;
1768#if COAP_Q_BLOCK_SUPPORT
1769 coap_tick_t receive_timeout = COAP_NON_RECEIVE_TIMEOUT_TICKS(session);
1770#endif /* COAP_Q_BLOCK_SUPPORT */
1771 int ret = 0;
1772
1773 *tim_rem = COAP_MAX_DELAY_TICKS;
1774#if COAP_Q_BLOCK_SUPPORT
1775 if (COAP_PROTO_NOT_RELIABLE(session->proto) &&
1776 session->block_mode & COAP_BLOCK_HAS_Q_BLOCK)
1777 partial_timeout = COAP_NON_PARTIAL_TIMEOUT_TICKS(session);
1778 else
1779#endif /* COAP_Q_BLOCK_SUPPORT */
1780 partial_timeout = COAP_MAX_TRANSMIT_WAIT_TICKS(session);
1781
1782 LL_FOREACH_SAFE(session->lg_crcv, lg_crcv, q) {
1783 if (COAP_PROTO_RELIABLE(session->proto) || lg_crcv->last_type != COAP_MESSAGE_NON)
1784 goto check_expire;
1785
1786#if COAP_Q_BLOCK_SUPPORT
1787 if (lg_crcv->block_option == COAP_OPTION_Q_BLOCK2 && lg_crcv->rec_blocks.used) {
1788 size_t scaled_timeout = receive_timeout *
1789 ((size_t)1 << lg_crcv->rec_blocks.retry);
1790
1791 if (lg_crcv->rec_blocks.retry >= COAP_NON_MAX_RETRANSMIT(session)) {
1792 /* Done NON_MAX_RETRANSMIT retries */
1793 coap_handle_nack(session, lg_crcv->sent_pdu,
1794 COAP_NACK_TOO_MANY_RETRIES, lg_crcv->sent_pdu->mid);
1795 goto expire;
1796 }
1797 if (lg_crcv->rec_blocks.last_seen + scaled_timeout <= now) {
1798 coap_log_debug("** %s: lg_crcv %p timeout\n",
1799 coap_session_str(session), (void *)lg_crcv);
1800 coap_request_missing_q_block2(session, lg_crcv);
1801 } else {
1802 if (*tim_rem > lg_crcv->rec_blocks.last_seen + scaled_timeout - now) {
1803 *tim_rem = lg_crcv->rec_blocks.last_seen + scaled_timeout - now;
1804 ret = 1;
1805 }
1806 }
1807 }
1808#endif /* COAP_Q_BLOCK_SUPPORT */
1809 /* Used for Block2 and Q-Block2 */
1810check_expire:
1811 if (!lg_crcv->observe_set && lg_crcv->last_used &&
1812 lg_crcv->last_used + partial_timeout <= now) {
1813#if COAP_Q_BLOCK_SUPPORT
1814expire:
1815#endif /* COAP_Q_BLOCK_SUPPORT */
1816 /* Expire this entry */
1817 LL_DELETE(session->lg_crcv, lg_crcv);
1818 coap_block_delete_lg_crcv(session, lg_crcv);
1819 } else if (!lg_crcv->observe_set && lg_crcv->last_used) {
1820 /* Delay until the lg_crcv needs to expire */
1821 if (*tim_rem > lg_crcv->last_used + partial_timeout - now) {
1822 *tim_rem = lg_crcv->last_used + partial_timeout - now;
1823 ret = 1;
1824 }
1825 }
1826 }
1827 return ret;
1828}
1829#endif /* COAP_CLIENT_SUPPORT */
1830
1831#if COAP_SERVER_SUPPORT
1832#if COAP_Q_BLOCK_SUPPORT
1833static coap_pdu_t *
1834pdu_408_build(coap_session_t *session, coap_lg_srcv_t *lg_srcv) {
1835 coap_pdu_t *pdu;
1836 uint8_t buf[4];
1837
1839 COAP_RESPONSE_CODE(408),
1840 coap_new_message_id_lkd(session),
1842 if (!pdu)
1843 return NULL;
1844 if (lg_srcv->last_token)
1845 coap_add_token(pdu, lg_srcv->last_token->length, lg_srcv->last_token->s);
1847 coap_encode_var_safe(buf, sizeof(buf),
1849 buf);
1850 pdu->token[pdu->used_size++] = COAP_PAYLOAD_START;
1851 pdu->data = pdu->token + pdu->used_size;
1852 return pdu;
1853}
1854
1855static int
1856add_408_block(coap_pdu_t *pdu, uint32_t block) {
1857 size_t len;
1858 uint8_t val[8];
1859
1860 assert(block < (1U << 20));
1861 coap_log_debug("Q-Block1: Requesting missing block %" PRIu32 "\n", block);
1862
1863 if (block >= (1U << 20)) {
1864 return 0;
1865 } else if (block < 24) {
1866 len = 1;
1867 val[0] = (uint8_t)block;
1868 } else if (block < 0x100) {
1869 len = 2;
1870 val[0] = 24;
1871 val[1] = (uint8_t)block;
1872 } else if (block < 0x10000) {
1873 len = 3;
1874 val[0] = 25;
1875 val[1] = (uint8_t)((block >> 8) & 0xff);
1876 val[2] = (uint8_t)(block & 0xff);
1877 } else { /* Largest block number is 2^^20 - 1 */
1878 len = 5;
1879 val[0] = 26;
1880 val[1] = (uint8_t)((block >> 24) & 0xff); /* Will be 0 */
1881 val[2] = (uint8_t)((block >> 16) & 0xff);
1882 val[3] = (uint8_t)((block >> 8) & 0xff);
1883 val[4] = (uint8_t)(block & 0xff);
1884 }
1885 if (coap_pdu_check_resize(pdu, pdu->used_size + len)) {
1886 memcpy(&pdu->token[pdu->used_size], val, len);
1887 pdu->used_size += len;
1888 return 1;
1889 }
1890 return 0;
1891}
1892#endif /* COAP_Q_BLOCK_SUPPORT */
1893#endif /* COAP_SERVER_SUPPORT */
1894
1895static int
1896check_if_received_block(coap_rblock_t *rec_blocks, uint32_t block_num) {
1897 uint32_t i;
1898
1899 for (i = 0; i < rec_blocks->used; i++) {
1900 if (block_num < rec_blocks->range[i].begin)
1901 return 0;
1902 if (block_num <= rec_blocks->range[i].end)
1903 return 1;
1904 }
1905 return 0;
1906}
1907
1908#if COAP_SERVER_SUPPORT
1909static int
1910check_if_next_block(coap_rblock_t *rec_blocks, uint32_t block_num) {
1911 if (rec_blocks->used == 0) {
1912 return block_num == 0 ? 1 : 0;
1913 }
1914 if (rec_blocks->range[rec_blocks->used-1].end + 1 == block_num)
1915 return 1;
1916
1917 return 0;
1918}
1919#endif /* COAP_SERVER_SUPPORT */
1920
1921static int
1923 uint32_t i;
1924 uint32_t block = 0;
1925
1926 if (rec_blocks->total_blocks == 0) {
1927 /* Not seen block with More bit unset yet */
1928 return 0;
1929 }
1930
1931 for (i = 0; i < rec_blocks->used; i++) {
1932 if (block < rec_blocks->range[i].begin)
1933 return 0;
1934 if (block < rec_blocks->range[i].end)
1935 block = rec_blocks->range[i].end;
1936 }
1937 return 1;
1938}
1939
1940#if COAP_CLIENT_SUPPORT
1941#if COAP_Q_BLOCK_SUPPORT
1942static int
1943check_all_blocks_in_for_payload_set(coap_session_t *session,
1944 coap_rblock_t *rec_blocks) {
1945 if (rec_blocks->used &&
1946 (rec_blocks->range[0].end + 1) / COAP_MAX_PAYLOADS(session) >
1947 rec_blocks->processing_payload_set)
1948 return 1;
1949 return 0;
1950}
1951
1952static int
1953check_any_blocks_next_payload_set(coap_session_t *session,
1954 coap_rblock_t *rec_blocks) {
1955 if (rec_blocks->used > 1 &&
1956 rec_blocks->range[1].begin / COAP_MAX_PAYLOADS(session) ==
1957 rec_blocks->processing_payload_set)
1958 return 1;
1959 return 0;
1960}
1961#endif /* COAP_Q_BLOCK_SUPPORT */
1962#endif /* COAP_CLIENT_SUPPORT */
1963
1964#if COAP_SERVER_SUPPORT
1965#if COAP_Q_BLOCK_SUPPORT
1966/*
1967 * To reduce the number of retransmitted missing requests, if the request comes
1968 * from a inc_to_block in the same PAYLOAD_SET as the last time, the request is not
1969 * retransmitted, unless this is from a timeout request when inc_to_block is the
1970 * the last expected block based on SIZE1, not the last received block.
1971 *
1972 * If lg_srcv->rec_blocks.used == 0, then no blocks have been received (not expected).
1973 * If lg_srcv->rec_blocks.used == 1 and lg_srcv->rec_blocks.range[0].begin == 0,
1974 * then no blocks are missing, but the next block may be missing (picked up when
1975 * there is a timeout request).
1976 * If lg_srcv->rec_blocks.used > 1, then there are missing blocks.
1977 *
1978 * The final PAYLOAD_SET is a special case as it can have 1 to MAX_PAYLOADS -1 blocks.
1979 *
1980 * The first PAYLOAD_SET that has missing blocks is reported in the 4.08 code, and
1981 * is triggered when inc_to_block is in the next PAYLOAD_SET (unless in final).
1982 *
1983 * It is not safe to report missing blocks in the same PAYLOAD_SET as inc_to_block as
1984 * the individual blocks may arrive in a random order (generates a lot of duplicate
1985 * blocks).
1986 *
1987 * However, if some missing blocks, but the last 2 blocks of a PAYLOAD_SET have been
1988 * received, then immediately trigger the 4.08 missing blocks request (not suggested
1989 * in RFC9177) as this considerably reduces the risk that the penultimate block is in
1990 * flight, but received in the wrong order and hence requesting a duplicate
1991 * re-transmission.
1992 */
1993static int
1994coap_request_missing_q_block1(coap_session_t *session, coap_lg_srcv_t *lg_srcv,
1995 uint32_t inc_to_block) {
1996 uint32_t i;
1997 int32_t block = -1; /* Last one seen */
1998 uint32_t block_size = (uint32_t)1 << (lg_srcv->szx + 4);
1999 uint32_t first_missing;
2000 uint32_t missing_payload;
2001 uint32_t last_payload_block;
2002 coap_pdu_t *pdu = NULL;
2003 /* Block number starts at 0 */
2004 uint32_t end_block = ((uint32_t)lg_srcv->total_len + block_size - 1) / block_size - 1;
2005 int count = 0;
2006
2007 if (COAP_MAX_PAYLOADS(session) == 0 ||
2008 (int32_t)(inc_to_block / COAP_MAX_PAYLOADS(session)) == lg_srcv->r_m_payload_set)
2009 return 0;
2010
2011 if (inc_to_block != end_block) {
2012 lg_srcv->r_m_payload_set = inc_to_block / COAP_MAX_PAYLOADS(session);
2013 }
2014
2015 if (lg_srcv->rec_blocks.range[0].begin != 0) {
2016 first_missing = 0;
2017 } else {
2018 first_missing = lg_srcv->rec_blocks.range[0].end + 1;
2019 }
2020 if (first_missing > end_block)
2021 return 0;
2022 missing_payload = first_missing / COAP_MAX_PAYLOADS(session);
2023 last_payload_block = (missing_payload + 1) * COAP_MAX_PAYLOADS(session) - 1;
2024 if (inc_to_block != end_block && inc_to_block > last_payload_block) {
2025 inc_to_block = last_payload_block;
2026 }
2027 /* Ask for the missing blocks */
2028 block = -1;
2029 for (i = 0; i < lg_srcv->rec_blocks.used; i++) {
2030 if (block < (int32_t)lg_srcv->rec_blocks.range[i].begin &&
2031 lg_srcv->rec_blocks.range[i].begin != 0) {
2032 /* Report on missing blocks */
2033 if (pdu == NULL) {
2034 pdu = pdu_408_build(session, lg_srcv);
2035 if (!pdu)
2036 continue;
2037 }
2038 block++;
2039 for (; block < (int32_t)lg_srcv->rec_blocks.range[i].begin; block++) {
2040 if (!add_408_block(pdu, block)) {
2041 break;
2042 }
2043 count++;
2044 }
2045 }
2046 if (block < (int32_t)lg_srcv->rec_blocks.range[i].end) {
2047 block = lg_srcv->rec_blocks.range[i].end;
2048 }
2049 }
2050 if (inc_to_block == end_block && block < (int32_t)inc_to_block) {
2051 /* Need to fill to the end */
2052 if (pdu == NULL) {
2053 pdu = pdu_408_build(session, lg_srcv);
2054 if (!pdu)
2055 goto retry;
2056 }
2057 block++;
2058 for (; block <= (int32_t)inc_to_block && count < COAP_MAX_PAYLOADS(session); block++) {
2059 if (!add_408_block(pdu, block)) {
2060 break;
2061 }
2062 count++;
2063 }
2064 }
2065 if (pdu)
2066 coap_send_internal(session, pdu, NULL);
2067retry:
2068 lg_srcv->rec_blocks.retry++;
2069 coap_ticks(&lg_srcv->rec_blocks.last_seen);
2070 return 1;
2071}
2072#endif /* COAP_Q_BLOCK_SUPPORT */
2073
2074/*
2075 * return 1 if there is a future expire time, else 0.
2076 * update tim_rem with remaining value if return is 1.
2077 */
2078int
2079coap_block_check_lg_srcv_timeouts(coap_session_t *session, coap_tick_t now,
2080 coap_tick_t *tim_rem) {
2081 coap_lg_srcv_t *lg_srcv;
2082 coap_lg_srcv_t *q;
2083 coap_tick_t partial_timeout;
2084#if COAP_Q_BLOCK_SUPPORT
2085 coap_tick_t receive_timeout = COAP_NON_RECEIVE_TIMEOUT_TICKS(session);
2086#endif /* COAP_Q_BLOCK_SUPPORT */
2087 int ret = 0;
2088
2089 *tim_rem = COAP_MAX_DELAY_TICKS;
2090#if COAP_Q_BLOCK_SUPPORT
2091 if (COAP_PROTO_NOT_RELIABLE(session->proto) &&
2092 session->block_mode & COAP_BLOCK_HAS_Q_BLOCK)
2093 partial_timeout = COAP_NON_PARTIAL_TIMEOUT_TICKS(session);
2094 else
2095#endif /* COAP_Q_BLOCK_SUPPORT */
2096 partial_timeout = COAP_MAX_TRANSMIT_WAIT_TICKS(session);
2097
2098 if (session->context->lg_srcv_cnt > 5) {
2099 /* In case someone is DOSing us.
2100 * DEFAULT_NON_PARTIAL_TIMEOUT is 247 seconds.
2101 * MAX_TRANSIT_WAIT is 93 seconds.
2102 */
2103 partial_timeout /= (session->context->lg_srcv_cnt - 5);
2104 if (partial_timeout < 10 * COAP_TICKS_PER_SECOND) {
2105 /* Do not shrink it too much */
2106 partial_timeout = 10 * COAP_TICKS_PER_SECOND;
2107 }
2108 }
2109 LL_FOREACH_SAFE(session->lg_srcv, lg_srcv, q) {
2110 if (lg_srcv->dont_timeout) {
2111 /* Not safe to timeout at present */
2112 continue;
2113 }
2114 if (COAP_PROTO_RELIABLE(session->proto) || lg_srcv->block_option != COAP_OPTION_Q_BLOCK1)
2115 goto check_expire;
2116
2117#if COAP_Q_BLOCK_SUPPORT
2118 if (lg_srcv->block_option == COAP_OPTION_Q_BLOCK1 && lg_srcv->rec_blocks.used) {
2119 size_t scaled_timeout = receive_timeout *
2120 ((size_t)1 << lg_srcv->rec_blocks.retry);
2121
2122 if (lg_srcv->rec_blocks.retry >= COAP_NON_MAX_RETRANSMIT(session)) {
2123 /* Done NON_MAX_RETRANSMIT retries */
2124 goto expire;
2125 }
2126 if (lg_srcv->rec_blocks.last_seen + scaled_timeout <= now) {
2127 uint32_t block_size = (size_t)1 << (lg_srcv->szx + 4);
2128 coap_log_debug("** %s: lg_srcv %p timeout\n",
2129 coap_session_str(session), (void *)lg_srcv);
2130 if (!coap_request_missing_q_block1(session, lg_srcv,
2131 ((uint32_t)lg_srcv->total_len + block_size - 1) / block_size - 1))
2132 goto expire;
2133 }
2134 if (*tim_rem > lg_srcv->rec_blocks.last_seen + scaled_timeout - now) {
2135 *tim_rem = lg_srcv->rec_blocks.last_seen + scaled_timeout - now;
2136 ret = 1;
2137 }
2138 }
2139#endif /* COAP_Q_BLOCK_SUPPORT */
2140 /* Used for Block1 and Q-Block1 */
2141check_expire:
2142 if (lg_srcv->no_more_seen)
2143 partial_timeout = 10 * COAP_TICKS_PER_SECOND;
2144 if (lg_srcv->last_used && lg_srcv->last_used + partial_timeout <= now) {
2145#if COAP_Q_BLOCK_SUPPORT
2146expire:
2147#endif /* COAP_Q_BLOCK_SUPPORT */
2148 /* Expire this entry */
2149 if (lg_srcv->no_more_seen && lg_srcv->block_option == COAP_OPTION_BLOCK1) {
2150 /*
2151 * Need to send a separate 4.08 to indicate missing blocks
2152 * Using NON is permissible as per
2153 * https://datatracker.ietf.org/doc/html/rfc7252#section-5.2.3
2154 */
2155 coap_pdu_t *pdu;
2156
2158 COAP_RESPONSE_CODE(408),
2159 coap_new_message_id_lkd(session),
2161 if (pdu) {
2162 if (lg_srcv->last_token)
2163 coap_add_token(pdu, lg_srcv->last_token->length, lg_srcv->last_token->s);
2164 coap_add_data(pdu, sizeof("Missing interim block")-1,
2165 (const uint8_t *)"Missing interim block");
2166 coap_send_internal(session, pdu, NULL);
2167 }
2168 }
2169 LL_DELETE(session->lg_srcv, lg_srcv);
2170 coap_block_delete_lg_srcv(session, lg_srcv);
2171 } else if (lg_srcv->last_used) {
2172 /* Delay until the lg_srcv needs to expire */
2173 if (*tim_rem > lg_srcv->last_used + partial_timeout - now) {
2174 *tim_rem = lg_srcv->last_used + partial_timeout - now;
2175 ret = 1;
2176 }
2177 }
2178 }
2179 return ret;
2180}
2181#endif /* COAP_SERVER_SUPPORT */
2182
2183#if COAP_Q_BLOCK_SUPPORT
2184static coap_pdu_t *
2185coap_next_block_to_send(coap_session_t *session,
2186 coap_lg_xmit_t *lg_xmit,
2187 coap_block_b_t *block,
2188 coap_pdu_t *pdu,
2189 uint32_t delayqueue_cnt,
2190 int is_first) {
2191 const uint8_t *ptoken;
2192 uint8_t ltoken[8];
2193 size_t ltoken_length;
2194 coap_pdu_t *block_pdu = NULL;
2195 uint64_t token;
2196 coap_opt_filter_t drop_options;
2197 size_t chunk = ((size_t)1 << (lg_xmit->blk_size + 4));
2198 size_t offset;
2199 uint8_t buf[8];
2200 int need_block = 0;
2201
2202 if (COAP_MAX_PAYLOADS(session) == 0)
2203 return NULL;
2204 /* Get the next block to transmit (which could be a retry */
2205 block->num = lg_xmit->send_blocks.range[0].begin;
2206
2207 offset = block->num * chunk;
2208 block->m = offset + chunk < lg_xmit->data_info->length;
2209
2210 if (!(block->m || lg_xmit->send_blocks.used) && COAP_MAX_PAYLOADS(session)) {
2211 return NULL;
2212 }
2213 if (is_first &&
2214 ((pdu->type == COAP_MESSAGE_ACK && lg_xmit->option == COAP_OPTION_Q_BLOCK2) ||
2215 (pdu->type == COAP_MESSAGE_CON && delayqueue_cnt < COAP_NSTART(session)))) {
2216 need_block = 1;
2217 }
2218 if (COAP_PROTO_RELIABLE(session->proto)) {
2219 need_block = 1;
2220 }
2221
2222 if (need_block ||
2223 (pdu->type == COAP_MESSAGE_NON &&
2224 (lg_xmit->send_blocks.used > 1 ||
2225 (block->num % COAP_MAX_PAYLOADS(session) != 0) ||
2226 is_first))) {
2227 if (!is_first && block->num % COAP_MAX_PAYLOADS(session) == 0 &&
2228 lg_xmit->send_blocks.used == 1 && !COAP_PROTO_RELIABLE(session->proto)) {
2229 coap_log_debug("Q-Block: Stopping at block %d\n", block->num);
2230 } else {
2231 /*
2232 * Allocate next block pdu if there is headroom and if one of
2233 * NON and more in MAX_PAYLOADS
2234 * CON and NSTART allows it (based on number in delayqueue)
2235 * Reliable transport
2236 */
2237 coap_log_debug("Q-Block: %s block %d\n", is_first ? "First" : "Next", block->num);
2238 if (COAP_PDU_IS_RESPONSE(pdu)) {
2239 ptoken = pdu->actual_token.s;
2240 ltoken_length = pdu->actual_token.length;
2241 } else {
2242 token = STATE_TOKEN_FULL(lg_xmit->b.b1.state_token,++lg_xmit->b.b1.count);
2243 ltoken_length = coap_encode_var_safe8(ltoken, sizeof(token), token);
2244 ptoken = ltoken;
2245 }
2246
2247 memset(&drop_options, 0, sizeof(coap_opt_filter_t));
2248 coap_option_filter_set(&drop_options, lg_xmit->option);
2249 block_pdu = coap_pdu_duplicate_lkd(pdu, session,
2250 ltoken_length,
2251 ptoken, &drop_options, COAP_BOOL_FALSE);
2252 if (block_pdu->type == COAP_MESSAGE_ACK)
2253 block_pdu->type = COAP_MESSAGE_CON;
2254
2255 if (!coap_update_option(block_pdu, lg_xmit->option,
2257 sizeof(buf),
2258 ((block->num) << 4) |
2259 (block->m << 3) |
2260 block->szx),
2261 buf)) {
2262 coap_log_warn("Internal update issue option\n");
2263 coap_delete_pdu_lkd(block_pdu);
2264 return NULL;
2265 }
2266 }
2267 }
2268 return block_pdu;
2269}
2270
2271/*
2272 * pdu is always released before return IF COAP_SEND_INC_PDU
2273 */
2275coap_send_q_blocks(coap_session_t *session,
2276 coap_lg_xmit_t *lg_xmit,
2277 coap_block_b_t block,
2278 coap_pdu_t *pdu,
2279 coap_send_pdu_t send_pdu) {
2280 coap_pdu_t *block_pdu = NULL; /* The next block to send after any initial PDU */
2282 uint32_t delayqueue_cnt = 0;
2283 coap_block_b_t l_block;
2284 int is_non;
2285
2286 if (!pdu)
2287 return COAP_INVALID_MID;
2288
2289 is_non = pdu->type == COAP_MESSAGE_NON ? 1 : 0;
2290
2291 if (!lg_xmit || session->is_rate_limiting) {
2292 if (send_pdu == COAP_SEND_INC_PDU) {
2293 if (lg_xmit &&
2294 (coap_get_block_b(session, pdu, COAP_OPTION_Q_BLOCK1, &l_block) ||
2295 coap_get_block_b(session, pdu, COAP_OPTION_Q_BLOCK2, &l_block))) {
2296 mid = coap_send_internal(session, pdu, NULL);
2297 if (mid != COAP_INVALID_MID) {
2298 blocks_delete_entry(&lg_xmit->send_blocks, l_block.num);
2299 }
2300 } else {
2301 return coap_send_internal(session, pdu, NULL);
2302 }
2303 }
2304 return COAP_INVALID_MID;
2305 }
2306
2307 if (pdu->type == COAP_MESSAGE_CON) {
2308 coap_queue_t *delayqueue;
2309
2310 delayqueue_cnt = session->con_active +
2311 (send_pdu == COAP_SEND_INC_PDU ? 1 : 0);
2312 LL_FOREACH(session->delayqueue, delayqueue) {
2313 delayqueue_cnt++;
2314 }
2315 }
2316 pdu->lg_xmit = lg_xmit;
2317
2318 /* Send initial pdu (which deletes 'pdu') */
2319 if (send_pdu == COAP_SEND_INC_PDU) {
2320 if (coap_get_block_b(session, pdu, COAP_OPTION_Q_BLOCK1, &l_block) ||
2321 coap_get_block_b(session, pdu, COAP_OPTION_Q_BLOCK2, &l_block)) {
2322 mid = coap_send_internal(session, pdu, NULL);
2323 if (mid != COAP_INVALID_MID) {
2324 blocks_delete_entry(&lg_xmit->send_blocks, l_block.num);
2325 }
2326 } else {
2327 mid = coap_send_internal(session, pdu, NULL);
2328 }
2329 if (mid == COAP_INVALID_MID) {
2330 /* Not expected, underlying issue somewhere */
2331 coap_delete_pdu_lkd(block_pdu);
2332 return COAP_INVALID_MID;
2333 }
2334 coap_ticks(&lg_xmit->last_sent);
2335 }
2336 /* Unsafe to use pdu in later code */
2337
2339 l_block = block;
2340 block_pdu = coap_next_block_to_send(session, lg_xmit, &l_block, pdu, delayqueue_cnt, 1);
2341 if (block_pdu) {
2342 block = l_block;
2343 }
2344
2345 while (block_pdu && lg_xmit->send_blocks.used) {
2346 coap_pdu_t *t_pdu = NULL;
2347
2348 if (lg_xmit->data_info->get_func) {
2349#if COAP_CONSTRAINED_STACK
2350 /* Protected by global_lock if needed */
2351 static uint8_t l_data[1024];
2352#else /* ! COAP_CONSTRAINED_STACK */
2353 uint8_t l_data[1024];
2354#endif /* ! COAP_CONSTRAINED_STACK */
2355 size_t l_length;
2356 size_t chunk = ((size_t)1 << (lg_xmit->blk_size + 4));
2357
2358 (void)chunk;
2359 assert(chunk <= 1024);
2360 if (lg_xmit->data_info->get_func(session, chunk,
2361 block.num * chunk, l_data, &l_length,
2362 lg_xmit->data_info->app_ptr)) {
2363 if (!coap_add_data(block_pdu, l_length, l_data)) {
2364 coap_log_warn("Internal update issue data (1)\n");
2365 coap_delete_pdu_lkd(block_pdu);
2366 block_pdu = NULL;
2367 break;
2368 }
2369 }
2370 } else {
2371 if (!coap_add_block(block_pdu,
2372 lg_xmit->data_info->length,
2373 lg_xmit->data_info->data,
2374 block.num,
2375 block.szx)) {
2376 coap_log_warn("Internal update issue data (2)\n");
2377 coap_delete_pdu_lkd(block_pdu);
2378 block_pdu = NULL;
2379 break;
2380 }
2381 }
2382 if (COAP_PDU_IS_RESPONSE(block_pdu)) {
2383 lg_xmit->last_block = block.num;
2384 }
2385 /* This block will now be transmitted */
2386 blocks_delete_entry(&lg_xmit->send_blocks, block.num);
2387
2388 if (block_pdu->type == COAP_MESSAGE_NON || COAP_PROTO_RELIABLE(session->proto)) {
2389 /* Get the next block to transmit (which could be a retry) */
2390 l_block = block;
2391 t_pdu = coap_next_block_to_send(session, lg_xmit, &l_block, block_pdu, delayqueue_cnt, 0);
2392 if (t_pdu) {
2393 block = l_block;
2394 }
2395 }
2396
2397 mid = coap_send_internal(session, block_pdu, NULL);
2398 if (mid == COAP_INVALID_MID) {
2399 /* Need to resent this one ... */
2400 blocks_add_entry(&lg_xmit->send_blocks, block.num, block.m);
2401 /* Not expected, underlying issue somewhere */
2402 coap_lg_xmit_release_lkd(session, lg_xmit);
2403 coap_delete_pdu_lkd(t_pdu);
2404 return COAP_INVALID_MID;
2405 }
2406 coap_ticks(&lg_xmit->last_sent);
2407 block_pdu = t_pdu;
2408 }
2409 if (is_non) {
2410 coap_log_debug("Q-Block: Current MAX_PAYLOAD sent\n");
2411 }
2412 coap_delete_pdu_lkd(block_pdu);
2413 if (!block.m) {
2414 lg_xmit->last_payload = 0;
2415 coap_ticks(&lg_xmit->last_all_sent);
2416 } else
2417 coap_ticks(&lg_xmit->last_payload);
2418 coap_lg_xmit_release_lkd(session, lg_xmit);
2419 return mid;
2420}
2421
2422#if COAP_CLIENT_SUPPORT
2423/*
2424 * Return 1 if there is a future expire time, else 0.
2425 * Update tim_rem with remaining value if return is 1.
2426 */
2427int
2428coap_block_check_q_block1_xmit(coap_session_t *session, coap_tick_t now, coap_tick_t *tim_rem) {
2429 coap_lg_xmit_t *lg_xmit;
2430 coap_lg_xmit_t *q;
2431 coap_tick_t timed_out;
2432 int ret = 0;
2433
2434 *tim_rem = COAP_MAX_DELAY_TICKS;
2435 LL_FOREACH_SAFE(session->lg_xmit, lg_xmit, q) {
2436 coap_tick_t non_timeout = lg_xmit->non_timeout_random_ticks;
2437
2438 if (lg_xmit->sent_pdu->type != COAP_MESSAGE_NON)
2439 continue;
2440 if (now <= non_timeout) {
2441 /* Too early in the startup cycle to have an accurate response */
2442 *tim_rem = non_timeout - now;
2443 if (*tim_rem < 100)
2444 *tim_rem = 100;
2445 return 1;
2446 }
2447 timed_out = now - non_timeout;
2448
2449 if (lg_xmit->last_payload && lg_xmit->send_blocks.used) {
2450 if (lg_xmit->last_payload <= timed_out) {
2451 /* Send off the next MAX_PAYLOAD set */
2452 coap_block_b_t block;
2453 size_t chunk = (size_t)1 << (lg_xmit->blk_size + 4);
2454 size_t offset;
2455
2456 coap_log_debug("Q-Block: Timeout - start next MAX_PAYLOADS_SET\n");
2457 memset(&block, 0, sizeof(block));
2458 block.num = lg_xmit->send_blocks.range[0].begin;
2459 offset = block.num * chunk;
2460 block.m = offset + chunk < lg_xmit->data_info->length;
2461 block.szx = lg_xmit->blk_size;
2462 coap_send_q_blocks(session, lg_xmit, block, lg_xmit->sent_pdu, COAP_SEND_SKIP_PDU);
2463 if (*tim_rem > non_timeout) {
2464 *tim_rem = non_timeout;
2465 ret = 1;
2466 }
2467 } else {
2468 /* Delay until the next MAX_PAYLOAD needs to be sent off */
2469 if (*tim_rem > lg_xmit->last_payload - timed_out) {
2470 *tim_rem = lg_xmit->last_payload - timed_out;
2471 ret = 1;
2472 }
2473 }
2474 } else if (lg_xmit->last_all_sent) {
2475 non_timeout = COAP_NON_TIMEOUT_TICKS(session);
2476 if (lg_xmit->last_all_sent + COAP_LG_XMIT_TXT_SCALAR * non_timeout <= now) {
2477 /* Expire this entry */
2478 LL_DELETE(session->lg_xmit, lg_xmit);
2479 coap_block_delete_lg_xmit(session, lg_xmit);
2480 } else {
2481 /* Delay until the lg_xmit needs to expire */
2482 if (*tim_rem > lg_xmit->last_all_sent + COAP_LG_XMIT_TXT_SCALAR * non_timeout - now) {
2483 *tim_rem = lg_xmit->last_all_sent + COAP_LG_XMIT_TXT_SCALAR * non_timeout - now;
2484 ret = 1;
2485 }
2486 }
2487 }
2488 }
2489 return ret;
2490}
2491#endif /* COAP_CLIENT_SUPPORT */
2492
2493#if COAP_SERVER_SUPPORT
2494/*
2495 * Return 1 if there is a future expire time, else 0.
2496 * Update tim_rem with remaining value if return is 1.
2497 */
2498int
2499coap_block_check_q_block2_xmit(coap_session_t *session, coap_tick_t now, coap_tick_t *tim_rem) {
2500 coap_lg_xmit_t *lg_xmit;
2501 coap_lg_xmit_t *q;
2502 coap_tick_t timed_out;
2503 int ret = 0;
2504
2505 *tim_rem = (coap_tick_t)-1;
2506 LL_FOREACH_SAFE(session->lg_xmit, lg_xmit, q) {
2507 coap_tick_t non_timeout = lg_xmit->non_timeout_random_ticks;
2508
2509 if (now <= non_timeout) {
2510 /* Too early in the startup cycle to have an accurate response */
2511 *tim_rem = non_timeout - now;
2512 if (*tim_rem < 100)
2513 *tim_rem = 100;
2514 return 1;
2515 }
2516 timed_out = now - non_timeout;
2517
2518 if (lg_xmit->last_payload && lg_xmit->send_blocks.used) {
2519 if (lg_xmit->last_payload <= timed_out) {
2520 /* Send off the next MAX_PAYLOAD set */
2521 coap_block_b_t block;
2522 size_t chunk = (size_t)1 << (lg_xmit->blk_size + 4);
2523 size_t offset;
2524
2525 memset(&block, 0, sizeof(block));
2526 block.num = lg_xmit->send_blocks.range[0].begin;
2527 offset = block.num * chunk;
2528 block.m = offset + chunk < lg_xmit->data_info->length;
2529 block.szx = lg_xmit->blk_size;
2530 if (block.num == (uint32_t)lg_xmit->last_block)
2531 coap_send_q_blocks(session, lg_xmit, block, lg_xmit->sent_pdu, COAP_SEND_SKIP_PDU);
2532 if (*tim_rem > non_timeout) {
2533 *tim_rem = non_timeout;
2534 ret = 1;
2535 }
2536 } else {
2537 /* Delay until the next MAX_PAYLOAD needs to be sent off */
2538 if (*tim_rem > lg_xmit->last_payload - timed_out) {
2539 *tim_rem = lg_xmit->last_payload - timed_out;
2540 ret = 1;
2541 }
2542 }
2543 } else if (lg_xmit->last_all_sent) {
2544 non_timeout = COAP_NON_TIMEOUT_TICKS(session);
2545 if (lg_xmit->last_all_sent + 4 * non_timeout <= now) {
2546 /* Expire this entry */
2547 LL_DELETE(session->lg_xmit, lg_xmit);
2548 coap_block_delete_lg_xmit(session, lg_xmit);
2549 } else {
2550 /* Delay until the lg_xmit needs to expire */
2551 if (*tim_rem > lg_xmit->last_all_sent + 4 * non_timeout - now) {
2552 *tim_rem = lg_xmit->last_all_sent + 4 * non_timeout - now;
2553 ret = 1;
2554 }
2555 }
2556 }
2557 }
2558 return ret;
2559}
2560#endif /* COAP_SERVER_SUPPORT */
2561#endif /* COAP_Q_BLOCK_SUPPORT */
2562
2563#if COAP_CLIENT_SUPPORT
2564/*
2565 * If Observe = 0, save the token away and return NULL
2566 * Else If Observe = 1, return the saved token for this block
2567 * Else, return NULL
2568 */
2569static coap_bin_const_t *
2570track_fetch_observe(coap_pdu_t *pdu, coap_lg_crcv_t *lg_crcv,
2571 uint32_t block_num, coap_bin_const_t *token) {
2572 /* Need to handle Observe for large FETCH */
2573 coap_opt_iterator_t opt_iter;
2575 &opt_iter);
2576
2577 if (opt && lg_crcv) {
2578 int observe_action = -1;
2579 coap_bin_const_t **tmp;
2580
2581 observe_action = coap_decode_var_bytes(coap_opt_value(opt),
2582 coap_opt_length(opt));
2583 if (observe_action == COAP_OBSERVE_ESTABLISH) {
2584 /* Save the token in lg_crcv */
2585 if (lg_crcv->obs_token_cnt <= block_num) {
2586 size_t i;
2587
2588 tmp = coap_realloc_type(COAP_STRING, lg_crcv->obs_token,
2589 (block_num + 1) * sizeof(lg_crcv->obs_token[0]));
2590 if (tmp == NULL)
2591 return NULL;
2592 lg_crcv->obs_token = tmp;
2593 for (i = lg_crcv->obs_token_cnt; i < block_num + 1; i++) {
2594 lg_crcv->obs_token[i] = NULL;
2595 }
2596 }
2597 coap_delete_bin_const(lg_crcv->obs_token[block_num]);
2598
2599 if (lg_crcv->obs_token_cnt <= block_num)
2600 lg_crcv->obs_token_cnt = block_num + 1;
2601 lg_crcv->obs_token[block_num] = coap_new_bin_const(token->s,
2602 token->length);
2603 if (lg_crcv->obs_token[block_num] == NULL)
2604 return NULL;
2605 } else if (observe_action == COAP_OBSERVE_CANCEL) {
2606 /* Use the token in lg_crcv */
2607 if (block_num < lg_crcv->obs_token_cnt) {
2608 return lg_crcv->obs_token[block_num];
2609 }
2610 }
2611 }
2612 return NULL;
2613}
2614
2615#if COAP_Q_BLOCK_SUPPORT
2617coap_send_q_block1(coap_session_t *session,
2618 coap_block_b_t block,
2619 coap_pdu_t *request,
2620 coap_send_pdu_t send_request) {
2621 /* Need to send up to MAX_PAYLOAD blocks if this is a Q_BLOCK1 */
2622 coap_lg_xmit_t *lg_xmit;
2623 uint64_t token_match =
2625 request->actual_token.length));
2626
2627 LL_FOREACH(session->lg_xmit, lg_xmit) {
2628 if (lg_xmit->option == COAP_OPTION_Q_BLOCK1 &&
2629 (token_match == STATE_TOKEN_BASE(lg_xmit->b.b1.state_token) ||
2630 token_match ==
2632 lg_xmit->b.b1.app_token->length))))
2633 break;
2634 /* try out the next one */
2635 }
2636 return coap_send_q_blocks(session, lg_xmit, block, request, send_request);
2637}
2638#endif /* COAP_Q_BLOCK_SUPPORT */
2639#endif /* COAP_CLIENT_SUPPORT */
2640
2641#if COAP_SERVER_SUPPORT
2642#if COAP_Q_BLOCK_SUPPORT
2643/*
2644 * response is always released before return IF COAP_SEND_INC_PDU
2645 */
2647coap_send_q_block2(coap_session_t *session,
2648 coap_resource_t *resource,
2649 const coap_string_t *query,
2650 coap_pdu_code_t request_method,
2651 coap_block_b_t block,
2652 coap_pdu_t *response,
2653 coap_send_pdu_t send_response) {
2654 /* Need to send up to MAX_PAYLOAD blocks if this is a Q_BLOCK2 */
2655 coap_lg_xmit_t *lg_xmit;
2656 coap_string_t empty = { 0, NULL};
2657
2658 LL_FOREACH(session->lg_xmit, lg_xmit) {
2659 if (lg_xmit->option == COAP_OPTION_Q_BLOCK2 &&
2660 resource == lg_xmit->b.b2.resource &&
2661 request_method == lg_xmit->b.b2.request_method &&
2662 coap_string_equal(query ? query : &empty,
2663 lg_xmit->b.b2.query ? lg_xmit->b.b2.query : &empty))
2664 break;
2665 }
2666 return coap_send_q_blocks(session, lg_xmit, block, response, send_response);
2667}
2668#endif /* COAP_Q_BLOCK_SUPPORT */
2669#endif /* COAP_SERVER_SUPPORT */
2670
2671static void
2673 coap_lg_xmit_data_t *data_info) {
2674 if (!data_info)
2675 return;
2676 if (data_info->ref > 0) {
2677 data_info->ref--;
2678 return;
2679 }
2680 if (data_info->release_func) {
2681 coap_lock_callback(data_info->release_func(session,
2682 data_info->app_ptr));
2683 data_info->release_func = NULL;
2684 }
2685 coap_free_type(COAP_STRING, data_info);
2686}
2687
2688#if COAP_CLIENT_SUPPORT
2689#if COAP_Q_BLOCK_SUPPORT
2690/*
2691 * Send out a test PDU for Q-Block.
2692 */
2694coap_block_test_q_block(coap_session_t *session, coap_pdu_t *actual) {
2695 coap_pdu_t *pdu;
2696 uint8_t token[8];
2697 size_t token_len;
2698 uint8_t buf[4];
2699 coap_mid_t mid;
2700 coap_bin_const_t *k_token;
2701
2702#if NDEBUG
2703 (void)actual;
2704#endif /* NDEBUG */
2705 assert(session->block_mode & COAP_BLOCK_TRY_Q_BLOCK &&
2706 session->type == COAP_SESSION_TYPE_CLIENT &&
2707 COAP_PDU_IS_REQUEST(actual));
2708
2709 coap_log_debug("Testing for Q-Block support\n");
2710 /* RFC9177 Section 4.1 when checking if available */
2712 coap_new_message_id_lkd(session),
2714 if (!pdu) {
2715 return COAP_INVALID_MID;
2716 }
2717
2718 coap_session_new_token(session, &token_len, token);
2719 coap_add_token(pdu, token_len, token);
2720 /* Use a resource that the server MUST support (.well-known/core) */
2722 11, (const uint8_t *)".well-known");
2724 4, (const uint8_t *)"core");
2725 /*
2726 * M needs to be unset as 'asking' for only the first block using
2727 * Q-Block2 as a test for server support.
2728 * See RFC9177 Section 4.4 Using the Q-Block2 Option.
2729 *
2730 * As the client is asking for 16 byte chunks, it is unlikely that
2731 * the .well-known/core response will be 16 bytes or less, so
2732 * if the server supports Q-Block, it will be forced to respond with
2733 * a Q-Block2, so the client can detect the server Q-Block support.
2734 */
2736 coap_encode_var_safe(buf, sizeof(buf),
2737 (0 << 4) | (0 << 3) | 0),
2738 buf);
2739 k_token = coap_new_bin_const(pdu->actual_token.s, pdu->actual_token.length);
2740 set_block_mode_probe_q(session->block_mode);
2741 mid = coap_send_internal(session, pdu, NULL);
2742 if (mid == COAP_INVALID_MID) {
2743 coap_delete_bin_const(k_token);
2744 return COAP_INVALID_MID;
2745 }
2746 session->remote_test_mid = mid;
2748 session->last_token = k_token;
2749 return mid;
2750}
2751#endif /* COAP_Q_BLOCK_SUPPORT */
2752
2754coap_block_new_lg_crcv(coap_session_t *session, coap_pdu_t *pdu,
2755 coap_lg_xmit_t *lg_xmit) {
2756 coap_block_b_t block;
2757 coap_lg_crcv_t *lg_crcv;
2758 uint64_t state_token = STATE_TOKEN_FULL(++session->tx_token, 1);
2759
2760 lg_crcv = coap_malloc_type(COAP_LG_CRCV, sizeof(coap_lg_crcv_t));
2761
2762 if (lg_crcv == NULL)
2763 return NULL;
2764
2765 coap_log_debug("** %s: lg_crcv %p initialized - stateless token xxxxx%011llx\n",
2766 coap_session_str(session), (void *)lg_crcv,
2767 STATE_TOKEN_BASE(state_token));
2768 memset(lg_crcv, 0, sizeof(coap_lg_crcv_t));
2769 lg_crcv->initial = 1;
2770 coap_ticks(&lg_crcv->last_used);
2771 /* Keep a copy of the sent pdu */
2772 lg_crcv->sent_pdu = coap_pdu_reference_lkd(pdu);
2773 if (lg_xmit) {
2774 coap_opt_iterator_t opt_iter;
2775 coap_opt_t *opt;
2776
2777 opt = coap_check_option(pdu, COAP_OPTION_OBSERVE, &opt_iter);
2778
2779 if (opt) {
2780 int observe_action;
2781
2782 observe_action = coap_decode_var_bytes(coap_opt_value(opt),
2783 coap_opt_length(opt));
2784 if (observe_action == COAP_OBSERVE_ESTABLISH) {
2785 /* Need to keep information for Observe Cancel */
2786 size_t data_len;
2787 const uint8_t *data;
2788
2789 if (coap_get_data(pdu, &data_len, &data)) {
2790 if (data_len < lg_xmit->data_info->length) {
2791 lg_xmit->data_info->ref++;
2792 lg_crcv->obs_data = lg_xmit->data_info;
2793 }
2794 }
2795 }
2796 }
2797 }
2798
2799 /* Need to keep original token for updating response PDUs */
2800 lg_crcv->app_token = coap_new_binary(pdu->actual_token.length);
2801 if (!lg_crcv->app_token) {
2802 coap_block_delete_lg_crcv(session, lg_crcv);
2803 return NULL;
2804 }
2805 memcpy(lg_crcv->app_token->s, pdu->actual_token.s, pdu->actual_token.length);
2806
2807 /* Need to set up a base token for actual communications if retries needed */
2808 lg_crcv->retry_counter = 1;
2809 lg_crcv->state_token = state_token;
2810 coap_address_copy(&lg_crcv->upstream, &session->addr_info.remote);
2811
2812 if (pdu->code == COAP_REQUEST_CODE_FETCH) {
2813 coap_bin_const_t *new_token;
2814
2815 /* Need to save/restore Observe Token for large FETCH */
2816 new_token = track_fetch_observe(pdu, lg_crcv, 0, &pdu->actual_token);
2817 if (new_token)
2818 coap_update_token(pdu, new_token->length, new_token->s);
2819 }
2820
2821 if (coap_get_block_b(session, pdu, COAP_OPTION_BLOCK1, &block)) {
2822 /* In case it is there - must not be in continuing request PDUs */
2823 lg_crcv->o_block_option = COAP_OPTION_BLOCK1;
2824 lg_crcv->o_blk_size = block.aszx;
2825 }
2826
2827 return lg_crcv;
2828}
2829
2830void
2831coap_block_delete_lg_crcv(coap_session_t *session,
2832 coap_lg_crcv_t *lg_crcv) {
2833 size_t i;
2834
2835#if (COAP_MAX_LOGGING_LEVEL < _COAP_LOG_DEBUG)
2836 (void)session;
2837#endif
2838 if (lg_crcv == NULL)
2839 return;
2840
2841 if (lg_crcv->ref > 0) {
2842 lg_crcv->ref--;
2843 return;
2844 }
2845
2846 coap_delete_binary(lg_crcv->body_data);
2847 if (lg_crcv->obs_data) {
2848 coap_block_release_lg_xmit_data(session, lg_crcv->obs_data);
2849 lg_crcv->obs_data = NULL;
2850 }
2851 coap_address_copy(&session->addr_info.remote, &lg_crcv->upstream);
2852 coap_log_debug("** %s: lg_crcv %p released\n",
2853 coap_session_str(session), (void *)lg_crcv);
2854 coap_delete_binary(lg_crcv->app_token);
2855 for (i = 0; i < lg_crcv->obs_token_cnt; i++) {
2856 coap_delete_bin_const(lg_crcv->obs_token[i]);
2857 }
2858 coap_free_type(COAP_STRING, lg_crcv->obs_token);
2859 coap_delete_pdu_lkd(lg_crcv->sent_pdu);
2860 coap_free_type(COAP_LG_CRCV, lg_crcv);
2861}
2862#endif /* COAP_CLIENT_SUPPORT */
2863
2864#if COAP_SERVER_SUPPORT
2865void
2866coap_block_delete_lg_srcv(coap_session_t *session,
2867 coap_lg_srcv_t *lg_srcv) {
2868 if (lg_srcv == NULL)
2869 return;
2870
2871 if (lg_srcv->ref > 0) {
2872 lg_srcv->ref--;
2873 return;
2874 }
2875
2876 coap_delete_cache_key(lg_srcv->cache_key);
2877 coap_delete_bin_const(lg_srcv->last_token);
2878 if (lg_srcv->body_data)
2879 session->context->cur_bodies_ram -= lg_srcv->body_data->length;
2880 coap_delete_binary(lg_srcv->body_data);
2881 coap_log_debug("** %s: lg_srcv %p released\n",
2882 coap_session_str(session), (void *)lg_srcv);
2883 coap_free_type(COAP_LG_SRCV, lg_srcv);
2884 session->context->lg_srcv_cnt--;
2885}
2886#endif /* COAP_SERVER_SUPPORT */
2887
2888void
2890 coap_lg_xmit_t *lg_xmit) {
2891 if (lg_xmit == NULL)
2892 return;
2893
2894 if (lg_xmit->ref > 0) {
2895 lg_xmit->ref--;
2896 return;
2897 }
2898
2899 coap_block_release_lg_xmit_data(session, lg_xmit->data_info);
2900 if (COAP_PDU_IS_REQUEST(lg_xmit->sent_pdu))
2901 coap_delete_binary(lg_xmit->b.b1.app_token);
2902 else
2903 coap_delete_string(lg_xmit->b.b2.query);
2904 coap_delete_pdu_lkd(lg_xmit->sent_pdu);
2905
2906 coap_log_debug("** %s: lg_xmit %p released\n",
2907 coap_session_str(session), (void *)lg_xmit);
2908 coap_free_type(COAP_LG_XMIT, lg_xmit);
2909}
2910
2911#if COAP_SERVER_SUPPORT
2912typedef struct {
2913 uint32_t num;
2914 int is_continue;
2915} send_track;
2916
2917static int
2918add_block_send(uint32_t num, int is_continue, send_track *out_blocks,
2919 uint32_t *count, uint32_t max_count) {
2920 uint32_t i;
2921
2922 for (i = 0; i < *count && *count < max_count; i++) {
2923 if (num == out_blocks[i].num)
2924 return 0;
2925 else if (num < out_blocks[i].num) {
2926 if (*count - i > 1)
2927 memmove(&out_blocks[i], &out_blocks[i+1], *count - i -1);
2928 out_blocks[i].num = num;
2929 out_blocks[i].is_continue = is_continue;
2930 (*count)++;
2931 return 1;
2932 }
2933 }
2934 if (*count < max_count) {
2935 out_blocks[i].num = num;
2936 out_blocks[i].is_continue = is_continue;
2937 (*count)++;
2938 return 1;
2939 }
2940 return 0;
2941}
2942
2943/*
2944 * Need to see if this is a request for the next block of a large body
2945 * transfer. If so, need to initiate the response with the next blocks
2946 * and not trouble the application.
2947 *
2948 * If additional responses needed, then these are explicitly sent out and
2949 * 'response' is updated to be the last response to be sent. There can be
2950 * multiple Q-Block2 in the request, as well as the 'Continue' Q-Block2
2951 * request.
2952 *
2953 * This is set up using coap_add_data_large_response_lkd()
2954 *
2955 * Server is sending a large data response to GET / observe (Block2)
2956 *
2957 * Return: 0 Call application handler
2958 * 1 Do not call application handler - just send the built response
2959 */
2960int
2961coap_handle_request_send_block(coap_session_t *session,
2962 coap_pdu_t *pdu,
2963 coap_pdu_t *response,
2964 coap_resource_t *resource,
2965 coap_string_t *query) {
2966 coap_lg_xmit_t *lg_xmit = NULL;
2967 coap_block_b_t block;
2968 coap_block_b_t alt_block;
2969 uint16_t block_opt = 0;
2970 send_track *out_blocks = NULL;
2971 const char *error_phrase;
2972 coap_opt_iterator_t opt_iter;
2973 size_t chunk;
2974 coap_opt_iterator_t opt_b_iter;
2975 coap_opt_t *option;
2976 uint32_t request_cnt, i;
2977 coap_opt_t *etag_opt = NULL;
2978 coap_pdu_t *out_pdu = response;
2979#if COAP_Q_BLOCK_SUPPORT
2980 size_t max_block;
2981
2982 /* Is client indicating that it supports Q_BLOCK2 ? */
2983 if (coap_get_block_b(session, pdu, COAP_OPTION_Q_BLOCK2, &block)) {
2984 if (!(session->block_mode & COAP_BLOCK_HAS_Q_BLOCK))
2985 set_block_mode_has_q(session->block_mode);
2986 block_opt = COAP_OPTION_Q_BLOCK2;
2987 }
2988#endif /* COAP_Q_BLOCK_SUPPORT */
2989 if (coap_get_block_b(session, pdu, COAP_OPTION_BLOCK2, &alt_block)) {
2990 if (block_opt) {
2991 coap_log_warn("Block2 and Q-Block2 cannot be in the same request\n");
2992 coap_add_data(response, sizeof("Both Block2 and Q-Block2 invalid")-1,
2993 (const uint8_t *)"Both Block2 and Q-Block2 invalid");
2994 response->code = COAP_RESPONSE_CODE(400);
2995 goto skip_app_handler;
2996 }
2997 block = alt_block;
2998 block_opt = COAP_OPTION_BLOCK2;
2999 }
3000 if (block_opt == 0)
3001 return 0;
3002 if (block.num == 0) {
3003#if COAP_Q_BLOCK_SUPPORT
3004 if (block_opt == COAP_OPTION_Q_BLOCK2) {
3005 if (block.m) {
3006 return 0;
3007 }
3008 } else
3009#endif /* COAP_Q_BLOCK_SUPPORT */
3010 /* Get a fresh copy of the data */
3011 return 0;
3012 }
3013 lg_xmit = coap_find_lg_xmit_response(session, pdu, resource, query);
3014 if (lg_xmit == NULL)
3015 return 0;
3016
3017#if COAP_Q_BLOCK_SUPPORT
3018 out_blocks = coap_malloc_type(COAP_STRING, sizeof(send_track) * COAP_MAX_PAYLOADS(session));
3019#else /* ! COAP_Q_BLOCK_SUPPORT */
3020 out_blocks = coap_malloc_type(COAP_STRING, sizeof(send_track));
3021#endif /* ! COAP_Q_BLOCK_SUPPORT */
3022 if (!out_blocks) {
3023 goto internal_issue;
3024 }
3025
3026 /* lg_xmit (response) found */
3027
3028 etag_opt = coap_check_option(pdu, COAP_OPTION_ETAG, &opt_iter);
3029 if (etag_opt) {
3030 /* There may be multiple ETag - need to check each one */
3031 coap_option_iterator_init(pdu, &opt_iter, COAP_OPT_ALL);
3032 while ((etag_opt = coap_option_next(&opt_iter))) {
3033 if (opt_iter.number == COAP_OPTION_ETAG) {
3034 uint64_t etag = coap_decode_var_bytes8(coap_opt_value(etag_opt),
3035 coap_opt_length(etag_opt));
3036 if (etag == lg_xmit->b.b2.etag) {
3037 break;
3038 }
3039 }
3040 }
3041 if (!etag_opt) {
3042 /* Not a match - pass up to a higher level */
3043 return 0;
3044 }
3045 }
3046 out_pdu->code = lg_xmit->sent_pdu->code;
3047 coap_ticks(&lg_xmit->last_obs);
3048
3049 chunk = (size_t)1 << (lg_xmit->blk_size + 4);
3050 if (block_opt) {
3051 if (block.bert) {
3052 coap_log_debug("found Block option, block is BERT, block nr. %u, M %d\n",
3053 block.num, block.m);
3054 } else {
3055 coap_log_debug("found Block option, block size is %u, block nr. %u, M %d\n",
3056 1 << (block.szx + 4), block.num, block.m);
3057 }
3058 if (block.bert == 0 && block.szx != lg_xmit->blk_size) {
3059 if (block.num == 0) {
3060 if (block.szx < lg_xmit->blk_size) {
3061 /*
3062 * Recompute the block number of the previous packet given
3063 * the new block size
3064 */
3065 block.num = (uint32_t)((chunk >> (block.szx + 4)) - 1);
3066 chunk = (size_t)1 << (lg_xmit->blk_size + 4);
3067#if COAP_Q_BLOCK_SUPPORT
3068 lg_xmit->send_blocks.range[0].begin = block.num;
3069 lg_xmit->send_blocks.range[0].end = (uint32_t)((lg_xmit->data_info->length - 1) / chunk);
3070#endif /* COAP_Q_BLOCK_SUPPORT */
3071 lg_xmit->blk_size = block.szx;
3072 coap_log_debug("new Block size is %u, block number %u completed\n",
3073 (1 << (block.szx + 4)), block.num);
3074 } else {
3075 coap_log_debug("ignoring request to increase Block size from %u to %u\n",
3076 (1 << (lg_xmit->blk_size + 4)), (1 << (lg_xmit->blk_size + 4)));
3077 }
3078 } else {
3079 coap_log_debug("ignoring request to change Block size from %u to %u\n",
3080 (1 << (lg_xmit->blk_size + 4)), (1 << (block.szx + 4)));
3081 block.szx = block.aszx = lg_xmit->blk_size;
3082 }
3083 }
3084 }
3085
3086 /*
3087 * Need to check if there are multiple Q-Block2 requests. If so, they
3088 * need to be sent out in order of requests with the final request being
3089 * handled as per singular Block 2 request.
3090 */
3091 request_cnt = 0;
3092#if COAP_Q_BLOCK_SUPPORT
3093 max_block = (lg_xmit->data_info->length + chunk - 1)/chunk;
3094#endif /* COAP_Q_BLOCK_SUPPORT */
3095 coap_option_iterator_init(pdu, &opt_b_iter, COAP_OPT_ALL);
3096 while ((option = coap_option_next(&opt_b_iter))) {
3097 uint32_t num;
3098 if (opt_b_iter.number != lg_xmit->option)
3099 continue;
3100 num = coap_opt_block_num(option);
3101 if (num > 0xFFFFF) /* 20 bits max for num */
3102 continue;
3103 if (block.aszx != COAP_OPT_BLOCK_SZX(option)) {
3104 coap_add_data(response,
3105 sizeof("Changing blocksize during request invalid")-1,
3106 (const uint8_t *)"Changing blocksize during request invalid");
3107 response->code = COAP_RESPONSE_CODE(400);
3108 goto skip_app_handler;
3109 }
3110#if COAP_Q_BLOCK_SUPPORT
3111 if (COAP_OPT_BLOCK_MORE(option) && lg_xmit->option == COAP_OPTION_Q_BLOCK2) {
3112 if ((num % COAP_MAX_PAYLOADS(session)) == 0) {
3113 if (num == 0) {
3114 /* This is a repeat request for everything - hmm */
3115 goto call_app_handler;
3116 }
3117 /* 'Continue' request */
3118 for (i = 0; i < COAP_MAX_PAYLOADS(session) &&
3119 num + i < max_block; i++) {
3120 add_block_send(num + i, 1, out_blocks, &request_cnt,
3121 COAP_MAX_PAYLOADS(session));
3122 lg_xmit->last_block = num + i;
3123 }
3124 } else {
3125 /* Requesting remaining payloads in this MAX_PAYLOADS */
3126 for (i = 0; i < COAP_MAX_PAYLOADS(session) -
3127 num % COAP_MAX_PAYLOADS(session) &&
3128 num + i < max_block; i++) {
3129 add_block_send(num + i, 0, out_blocks, &request_cnt,
3130 COAP_MAX_PAYLOADS(session));
3131 }
3132 }
3133 } else
3134 add_block_send(num, 0, out_blocks, &request_cnt,
3135 COAP_MAX_PAYLOADS(session));
3136#else /* ! COAP_Q_BLOCK_SUPPORT */
3137 add_block_send(num, 0, out_blocks, &request_cnt, 1);
3138 break;
3139#endif /* ! COAP_Q_BLOCK_SUPPORT */
3140 }
3141 if (request_cnt == 0) {
3142 /* Block2 or Q-Block2 not found - give them the first block */
3143 block.szx = lg_xmit->blk_size;
3144 out_blocks[0].num = 0;
3145 out_blocks[0].is_continue = 0;
3146 request_cnt = 1;
3147 }
3148
3149 for (i = 0; i < request_cnt; i++) {
3150 uint8_t buf[8];
3151 size_t offset;
3152
3153 block.num = out_blocks[i].num;
3154
3155 if (i + 1 < request_cnt) {
3156 /* Need to set up a copy of the pdu to send */
3157 coap_opt_filter_t drop_options;
3158
3159 memset(&drop_options, 0, sizeof(coap_opt_filter_t));
3160 if (block.num != 0)
3162 if (out_blocks[i].is_continue) {
3163 out_pdu = coap_pdu_duplicate_lkd(lg_xmit->sent_pdu, session,
3164 lg_xmit->sent_pdu->actual_token.length,
3165 lg_xmit->sent_pdu->actual_token.s,
3166 &drop_options, COAP_BOOL_FALSE);
3167 } else {
3168 out_pdu = coap_pdu_duplicate_lkd(lg_xmit->sent_pdu, session,
3169 pdu->actual_token.length,
3170 pdu->actual_token.s,
3171 &drop_options, COAP_BOOL_FALSE);
3172 }
3173 if (!out_pdu) {
3174 goto internal_issue;
3175 }
3176 } else {
3177 if (out_blocks[i].is_continue)
3178 coap_update_token(response, lg_xmit->sent_pdu->actual_token.length,
3179 lg_xmit->sent_pdu->actual_token.s);
3180 /*
3181 * Copy the options across and then fix the block option
3182 *
3183 * Need to drop Observe option if Block2 and block.num != 0
3184 */
3185 coap_option_iterator_init(lg_xmit->sent_pdu, &opt_iter, COAP_OPT_ALL);
3186 while ((option = coap_option_next(&opt_iter))) {
3187 if (opt_iter.number == COAP_OPTION_OBSERVE && block.num != 0)
3188 continue;
3189 if (!coap_insert_option(response, opt_iter.number,
3190 coap_opt_length(option),
3191 coap_opt_value(option))) {
3192 goto internal_issue;
3193 }
3194 }
3195 out_pdu = response;
3196 }
3197 if (pdu->type == COAP_MESSAGE_NON)
3198 out_pdu->type = COAP_MESSAGE_NON;
3199 offset = block.num * chunk;
3200 if (block.bert) {
3201 size_t token_options = pdu->data ? (size_t)(pdu->data - pdu->token) : pdu->used_size;
3202 block.m = (lg_xmit->data_info->length - offset) >
3203 ((out_pdu->max_size - token_options) /1024) * 1024;
3204 } else {
3205 block.m = (offset + chunk) < lg_xmit->data_info->length;
3206 }
3207 if (!coap_update_option(out_pdu, lg_xmit->option,
3209 sizeof(buf),
3210 (block.num << 4) |
3211 (block.m << 3) |
3212 block.aszx),
3213 buf)) {
3214 goto internal_issue;
3215 }
3216 if (!(offset + chunk < lg_xmit->data_info->length)) {
3217 /* Last block - keep in cache for 4 * ACK_TIMOUT */
3218 coap_ticks(&lg_xmit->last_all_sent);
3219 }
3220 if (lg_xmit->b.b2.maxage_expire) {
3221 coap_tick_t now;
3222 coap_time_t rem;
3223
3224 if (!(offset + chunk < lg_xmit->data_info->length)) {
3225 /* Last block - keep in cache for 4 * ACK_TIMOUT */
3226 coap_ticks(&lg_xmit->last_all_sent);
3227 }
3228 coap_ticks(&now);
3229 rem = coap_ticks_to_rt(now);
3230 if (lg_xmit->b.b2.maxage_expire > rem) {
3231 rem = lg_xmit->b.b2.maxage_expire - rem;
3232 } else {
3233 rem = 0;
3234 /* Entry needs to be expired */
3235 coap_ticks(&lg_xmit->last_all_sent);
3236 }
3239 sizeof(buf),
3240 rem),
3241 buf)) {
3242 goto internal_issue;
3243 }
3244 }
3245
3246 if (!coap_add_block_b_data(out_pdu,
3247 lg_xmit->data_info->length,
3248 lg_xmit->data_info->data,
3249 &block)) {
3250 goto internal_issue;
3251 }
3252 if (i + 1 < request_cnt) {
3253 coap_ticks(&lg_xmit->last_sent);
3254 coap_send_internal(session, out_pdu, NULL);
3255 }
3256 }
3257 coap_ticks(&lg_xmit->last_payload);
3258 coap_ticks(&lg_xmit->last_sent);
3259 if (lg_xmit->last_all_sent) {
3260 coap_ticks(&lg_xmit->last_all_sent);
3261 }
3262 goto skip_app_handler;
3263#if COAP_Q_BLOCK_SUPPORT
3264call_app_handler:
3265 coap_free_type(COAP_STRING, out_blocks);
3266 return 0;
3267#endif /* COAP_Q_BLOCK_SUPPORT */
3268
3269internal_issue:
3270 response->code = COAP_RESPONSE_CODE(500);
3271 error_phrase = coap_response_phrase(response->code);
3272 coap_add_data(response, strlen(error_phrase),
3273 (const uint8_t *)error_phrase);
3274 /* Keep in cache for 4 * ACK_TIMOUT in case of retry */
3275 if (lg_xmit)
3276 coap_ticks(&lg_xmit->last_all_sent);
3277
3278skip_app_handler:
3279 coap_free_type(COAP_STRING, out_blocks);
3280 return 1;
3281}
3282#endif /* COAP_SERVER_SUPPORT */
3283
3284#if COAP_Q_BLOCK_SUPPORT
3285static int
3286blocks_delete_entry(coap_rblock_t *rec_blocks, uint32_t block_num) {
3287 uint32_t i;
3288
3289 if (rec_blocks->total_blocks && block_num + 1 > rec_blocks->total_blocks) {
3290 /* received block number greater than Block No defined when More bit unset */
3291 return 0;
3292 }
3293
3294 for (i = 0; i < rec_blocks->used; i++) {
3295 if (block_num >= rec_blocks->range[i].begin &&
3296 block_num <= rec_blocks->range[i].end) {
3297 /* In this block */
3298 if (block_num == rec_blocks->range[i].begin) {
3299 if (block_num == rec_blocks->range[i].end) {
3300 /* Need to delete this range */
3301 if (i + 1 < rec_blocks->used) {
3302 memmove(&rec_blocks->range[i], &rec_blocks->range[i+1],
3303 (rec_blocks->used - i) * sizeof(rec_blocks->range[0]));
3304 }
3305 rec_blocks->used--;
3306 break;
3307 }
3308 rec_blocks->range[i].begin++;
3309 } else if (block_num == rec_blocks->range[i].end) {
3310 rec_blocks->range[i].end--;
3311 if (rec_blocks->range[i].begin == rec_blocks->range[i].end) {
3312 /* Need to delete this range */
3313 rec_blocks->used--;
3314 if (i == rec_blocks->used)
3315 break;
3316 memmove(&rec_blocks->range[i], &rec_blocks->range[i+1],
3317 sizeof(rec_blocks->range[i]) * (rec_blocks->used - i));
3318 }
3319 } else {
3320 /* Need to split the range */
3321 if (rec_blocks->used == COAP_RBLOCK_CNT)
3322 /* Too many losses */
3323 return 0;
3324 memmove(&rec_blocks->range[i+1], &rec_blocks->range[i],
3325 (rec_blocks->used - i) * sizeof(rec_blocks->range[0]));
3326 rec_blocks->range[i].end = block_num - 1;
3327 rec_blocks->range[i+1].begin = block_num + 1;
3328 rec_blocks->used++;
3329 }
3330 break;
3331 }
3332 }
3333 coap_ticks(&rec_blocks->last_seen);
3334 return 1;
3335}
3336#endif /* COAP_Q_BLOCK_SUPPORT */
3337
3338static int
3339blocks_add_entry(coap_rblock_t *rec_blocks, uint32_t block_num, uint32_t block_m) {
3340 uint32_t i;
3341
3342 if (rec_blocks->total_blocks && block_num + 1 > rec_blocks->total_blocks) {
3343 /* received block number greater than Block No defined when More bit unset */
3344 return 0;
3345 }
3346
3347 /* Reset as there is activity */
3348 rec_blocks->retry = 0;
3349
3350 for (i = 0; i < rec_blocks->used; i++) {
3351 if (block_num >= rec_blocks->range[i].begin &&
3352 block_num <= rec_blocks->range[i].end)
3353 break;
3354
3355 if (block_num < rec_blocks->range[i].begin) {
3356 if (block_num + 1 == rec_blocks->range[i].begin) {
3357 rec_blocks->range[i].begin = block_num;
3358 } else {
3359 /* Need to insert a new range */
3360 if (rec_blocks->used == COAP_RBLOCK_CNT)
3361 /* Too many losses */
3362 return 0;
3363 memmove(&rec_blocks->range[i+1], &rec_blocks->range[i],
3364 (rec_blocks->used - i) * sizeof(rec_blocks->range[0]));
3365 rec_blocks->range[i].begin = rec_blocks->range[i].end = block_num;
3366 rec_blocks->used++;
3367 }
3368 break;
3369 }
3370 if (block_num == rec_blocks->range[i].end + 1) {
3371 rec_blocks->range[i].end = block_num;
3372 if (i + 1 < rec_blocks->used) {
3373 if (rec_blocks->range[i+1].begin == block_num + 1) {
3374 /* Merge the 2 ranges */
3375 rec_blocks->range[i].end = rec_blocks->range[i+1].end;
3376 if (i+2 < rec_blocks->used) {
3377 memmove(&rec_blocks->range[i+1], &rec_blocks->range[i+2],
3378 (rec_blocks->used - (i+2)) * sizeof(rec_blocks->range[0]));
3379 }
3380 rec_blocks->used--;
3381 }
3382 }
3383 break;
3384 }
3385 }
3386 if (i == rec_blocks->used) {
3387 if (rec_blocks->used == COAP_RBLOCK_CNT) {
3388 /* Too many losses */
3389 return 0;
3390 }
3391 rec_blocks->range[i].begin = rec_blocks->range[i].end = block_num;
3392 rec_blocks->used++;
3393 }
3394 if (!block_m)
3395 rec_blocks->total_blocks = block_num + 1;
3396
3397 coap_ticks(&rec_blocks->last_seen);
3398 return 1;
3399}
3400
3401#if COAP_SERVER_SUPPORT
3402
3403static const uint16_t coap_lg_srcv_ignore_options[] = { COAP_OPTION_ETAG,
3410 };
3411/*
3412 * Need to check if this is a large PUT / POST etc. using multiple blocks
3413 *
3414 * Server receiving PUT/POST etc. of a large amount of data (Block1)
3415 *
3416 * Return: 0 Call application handler
3417 * 1 Do not call application handler - just send the built response
3418 */
3419int
3420coap_handle_request_put_block(coap_context_t *context,
3421 coap_session_t *session,
3422 coap_pdu_t *pdu,
3423 coap_pdu_t *response,
3424 coap_resource_t *resource,
3425 coap_opt_t *observe,
3426 int *added_block,
3427 coap_lg_srcv_t **pfree_lg_srcv) {
3428 size_t length = 0;
3429 const uint8_t *data = NULL;
3430 size_t offset = 0;
3431 size_t total = 0;
3432 size_t size2 = 0;
3433 coap_block_b_t block;
3434 coap_opt_iterator_t opt_iter;
3435 uint16_t block_option = 0;
3436 coap_lg_srcv_t *lg_srcv;
3437 coap_opt_t *size_opt;
3438 coap_opt_t *fmt_opt;
3439 uint16_t fmt;
3440 coap_opt_t *rtag_opt;
3441 size_t rtag_length;
3442 const uint8_t *rtag;
3443 uint32_t max_block_szx;
3444 int update_data;
3445 unsigned int saved_num;
3446 size_t saved_offset;
3447 int lg_srcv_is_refed = 0;
3448#if COAP_Q_BLOCK_SUPPORT
3449 int request_missing = 0;
3450#endif /* COAP_Q_BLOCK_SUPPORT */
3451 coap_cache_key_t *cache_key_l = NULL;
3452
3453 *added_block = 0;
3454 *pfree_lg_srcv = NULL;
3455 coap_get_data_large(pdu, &length, &data, &offset, &total);
3456 pdu->body_offset = 0;
3457 pdu->body_total = length;
3458
3459 if (coap_get_block_b(session, pdu, COAP_OPTION_BLOCK1, &block)) {
3460 block_option = COAP_OPTION_BLOCK1;
3461#if COAP_Q_BLOCK_SUPPORT
3462 if (coap_check_option(pdu, COAP_OPTION_Q_BLOCK1, &opt_iter)) {
3463 /* Cannot handle Q-Block1 as well */
3464 coap_add_data(response, sizeof("Block1 + Q-Block1 together")-1,
3465 (const uint8_t *)"Block1 + Q-Block1 together");
3466 response->code = COAP_RESPONSE_CODE(402);
3467 goto skip_app_handler;
3468 }
3469#endif /* COAP_Q_BLOCK_SUPPORT */
3470 }
3471#if COAP_Q_BLOCK_SUPPORT
3472 else if (coap_get_block_b(session, pdu, COAP_OPTION_Q_BLOCK1, &block)) {
3473 block_option = COAP_OPTION_Q_BLOCK1;
3474 set_block_mode_has_q(session->block_mode);
3475 }
3476#endif /* COAP_Q_BLOCK_SUPPORT */
3477 if (!block_option ||
3478 (block_option == COAP_OPTION_BLOCK1 && block.num == 0 && block.m == 0)) {
3479 /* Not blocked, or a single block */
3480 if (context->max_body_size && total > context->max_body_size) {
3481 uint8_t buf[8];
3482
3483 coap_update_option(response,
3485 coap_encode_var_safe8((uint8_t *)buf, sizeof(buf),
3486 context->max_body_size),
3487 (uint8_t *)buf);
3488 response->code = COAP_RESPONSE_CODE(413);
3489 coap_log_warn("Unable to handle data size %" PRIuS " (max %" PRIuS ")\n", total,
3490 context->max_body_size);
3491 goto skip_app_handler;
3492 }
3493 goto call_app_handler;
3494 }
3495
3496 size_opt = coap_check_option(pdu,
3498 &opt_iter);
3499 fmt_opt = coap_check_option(pdu,
3501 &opt_iter);
3502 fmt = fmt_opt ? coap_decode_var_bytes(coap_opt_value(fmt_opt),
3503 coap_opt_length(fmt_opt)) :
3505 rtag_opt = coap_check_option(pdu,
3507 &opt_iter);
3508#if COAP_Q_BLOCK_SUPPORT
3509 if (block_option == COAP_OPTION_Q_BLOCK1 && (!size_opt || !rtag_opt)) {
3510 /* RFC9177 section-4.3 */
3511 coap_log_info("Q-Block1: Size1 and RTag options required\n");
3512 response->code = COAP_RESPONSE_CODE(400);
3513 goto skip_app_handler;
3514 }
3515#endif /* COAP_Q_BLOCK_SUPPORT */
3516 rtag_length = rtag_opt ? coap_opt_length(rtag_opt) : 0;
3517 rtag_length = min(rtag_length, 8);
3518 rtag = rtag_opt ? coap_opt_value(rtag_opt) : NULL;
3519
3520 if (length > block.chunk_size) {
3521 coap_log_debug("block: Oversized packet - reduced to %"PRIu32" from %" PRIuS "\n",
3522 block.chunk_size, length);
3523 length = block.chunk_size;
3524 } else if (!block.bert && block.m && length != block.chunk_size) {
3525 coap_log_info("block: Undersized packet chunk %"PRIu32" got %" PRIuS "\n",
3526 block.chunk_size, length);
3527 response->code = COAP_RESPONSE_CODE(400);
3528 goto skip_app_handler;
3529 }
3530 if (size_opt) {
3531 size2 = coap_decode_var_bytes(coap_opt_value(size_opt), coap_opt_length(size_opt));
3532 if (total < size2) {
3533 total = size2;
3534 }
3535 }
3536 if (total < (block.num << (block.szx + 4)) + length + (block.m ? 1 : 0)) {
3537 total = (block.num << (block.szx + 4)) + length + (block.m ? 1 : 0);
3538 }
3539
3540 if (total) {
3541 uint32_t max_body;
3542
3543 max_block_szx = COAP_BLOCK_MAX_SIZE_GET(session->block_mode);
3544 if (max_block_szx == 0 || max_block_szx > block.szx) {
3545 max_block_szx = block.szx;
3546 }
3547 max_body = ((1UL << 20) * (1 << (max_block_szx + 4)));
3548 if (max_body > MAX_BLK_LEN)
3549 max_body = MAX_BLK_LEN;
3550 if ((context->max_body_size && total > context->max_body_size) ||
3551 (total > max_body)) {
3552 /* Suggested body size larger than allowed */
3553 char buf[32];
3554 size_t max_body_size = context->max_body_size;
3555
3556 if (max_body_size == 0 || max_body < max_body_size) {
3557 max_body_size = max_body;
3558 }
3559 coap_update_option(response,
3561 coap_encode_var_safe8((uint8_t *)buf, sizeof(buf),
3562 max_body_size),
3563 (uint8_t *)buf);
3564 snprintf(buf, sizeof(buf), "Max body size %" PRIuS, max_body_size);
3565 coap_add_data(response, strlen(buf), (uint8_t *)buf);
3566 response->code = COAP_RESPONSE_CODE(413);
3567 coap_log_warn("Unable to handle body size %" PRIuS " (max %" PRIuS ")\n", total, max_body_size);
3568 goto skip_app_handler;
3569 }
3570 }
3571 offset = block.num << (block.szx + 4);
3572
3573 if (!(session->block_mode &
3574#if COAP_Q_BLOCK_SUPPORT
3576#else /* COAP_Q_BLOCK_SUPPORT */
3578#endif /* COAP_Q_BLOCK_SUPPORT */
3579 && !block.bert) {
3580 uint8_t buf[4];
3581
3582 /* Ask for the next block */
3583 coap_insert_option(response, block_option,
3584 coap_encode_var_safe(buf, sizeof(buf),
3585 (block.num << 4) |
3586 (block.m << 3) |
3587 block.aszx),
3588 buf);
3589 /* Not re-assembling or checking for receipt order */
3590 pdu->body_data = data;
3591 pdu->body_length = length;
3592 pdu->body_offset = offset;
3593 if (total < (length + offset + (block.m ? 1 : 0)))
3594 total = length + offset + (block.m ? 1 : 0);
3595 pdu->body_total = total;
3596 *added_block = block.m;
3597 /* The application is responsible for returning the correct 2.01/2.04/2.31 etc. */
3598 goto call_app_handler;
3599 }
3600
3601 /*
3602 * locate the lg_srcv
3603 */
3604 cache_key_l = coap_cache_derive_key_w_ignore(session, pdu,
3606 coap_lg_srcv_ignore_options,
3607 sizeof(coap_lg_srcv_ignore_options)/sizeof(coap_lg_srcv_ignore_options[0]));
3608 if (!cache_key_l) {
3609 response->code = COAP_RESPONSE_CODE(500);
3610 goto skip_app_handler;
3611 }
3612 LL_FOREACH(session->lg_srcv, lg_srcv) {
3613 if (rtag_opt || lg_srcv->rtag_set == 1) {
3614 if (!(rtag_opt && lg_srcv->rtag_set == 1))
3615 continue;
3616 if (lg_srcv->rtag_length != rtag_length ||
3617 memcmp(lg_srcv->rtag, rtag, rtag_length) != 0)
3618 continue;
3619 }
3620#if COAP_OSCORE_SUPPORT && COAP_SERVER_SUPPORT
3621 if (session->recipient_ctx == lg_srcv->recipient_ctx ||
3622 memcmp(cache_key_l, lg_srcv->cache_key, sizeof(coap_cache_key_t)) == 0)
3623 break;
3624#else /* !COAP_OSCORE_SUPPORT && COAP_SERVER_SUPPORT */
3625 if (memcmp(cache_key_l, lg_srcv->cache_key, sizeof(coap_cache_key_t)) == 0)
3626 break;
3627#endif /* !COAP_OSCORE_SUPPORT && COAP_SERVER_SUPPORT */
3628 }
3629
3630 if (!lg_srcv && block.num != 0 && session->block_mode & COAP_BLOCK_NOT_RANDOM_BLOCK1) {
3631 coap_add_data(response, sizeof("Missing block 0")-1,
3632 (const uint8_t *)"Missing block 0");
3633 response->code = COAP_RESPONSE_CODE(408);
3634 goto skip_app_handler;
3635 }
3636
3637 if (!lg_srcv) {
3638 /* Allocate lg_srcv to use for tracking */
3639 lg_srcv = coap_malloc_type(COAP_LG_SRCV, sizeof(coap_lg_srcv_t));
3640 if (lg_srcv == NULL) {
3641 coap_add_data(response, sizeof("Memory issue")-1,
3642 (const uint8_t *)"Memory issue");
3643 response->code = COAP_RESPONSE_CODE(500);
3644 goto skip_app_handler;
3645 }
3646 coap_log_debug("** %s: lg_srcv %p initialized\n",
3647 coap_session_str(session), (void *)lg_srcv);
3648 session->context->lg_srcv_cnt++;
3649 memset(lg_srcv, 0, sizeof(coap_lg_srcv_t));
3650#if COAP_OSCORE_SUPPORT && COAP_SERVER_SUPPORT
3651 lg_srcv->recipient_ctx = session->recipient_ctx;
3652#endif /* COAP_OSCORE_SUPPORT && COAP_SERVER_SUPPORT */
3653 coap_ticks(&lg_srcv->last_used);
3654 lg_srcv->resource = resource;
3655 lg_srcv->cache_key = cache_key_l;
3656 cache_key_l = NULL;
3657 lg_srcv->content_format = fmt;
3658 lg_srcv->total_len = total;
3659 max_block_szx = COAP_BLOCK_MAX_SIZE_GET(session->block_mode);
3660 if (!block.bert && block.num == 0 && max_block_szx != 0 &&
3661 max_block_szx < block.szx) {
3662 lg_srcv->szx = max_block_szx;
3663 } else {
3664 lg_srcv->szx = block.szx;
3665 }
3666 lg_srcv->block_option = block_option;
3667 if (observe) {
3668 lg_srcv->observe_length = min(coap_opt_length(observe), 3);
3669 memcpy(lg_srcv->observe, coap_opt_value(observe), lg_srcv->observe_length);
3670 lg_srcv->observe_set = 1;
3671 }
3672 if (rtag_opt) {
3673 lg_srcv->rtag_length = (uint8_t)rtag_length;
3674 memcpy(lg_srcv->rtag, coap_opt_value(rtag_opt), lg_srcv->rtag_length);
3675 lg_srcv->rtag_set = 1;
3676 }
3677#if COAP_Q_BLOCK_SUPPORT
3678 lg_srcv->r_m_payload_set = -1;
3679#endif /* COAP_Q_BLOCK_SUPPORT */
3680 LL_PREPEND(session->lg_srcv, lg_srcv);
3681 }
3682 coap_ticks(&lg_srcv->last_used);
3683 coap_lg_srcv_reference_lkd(lg_srcv);
3684 lg_srcv_is_refed = 1;
3685
3686 if (block_option == COAP_OPTION_BLOCK1 &&
3688 !check_if_next_block(&lg_srcv->rec_blocks, block.num)) {
3689 coap_add_data(response, sizeof("Missing interim block")-1,
3690 (const uint8_t *)"Missing interim block");
3691 response->code = COAP_RESPONSE_CODE(408);
3692 goto skip_app_handler;
3693 }
3694
3695 if (fmt != lg_srcv->content_format) {
3696 coap_add_data(response, sizeof("Content-Format mismatch")-1,
3697 (const uint8_t *)"Content-Format mismatch");
3698 response->code = COAP_RESPONSE_CODE(408);
3699 goto free_lg_srcv;
3700 }
3701
3702#if COAP_Q_BLOCK_SUPPORT
3703 if (block_option == COAP_OPTION_Q_BLOCK1) {
3704 if (total != lg_srcv->total_len) {
3705 coap_add_data(response, sizeof("Size1 mismatch")-1,
3706 (const uint8_t *)"Size1 mismatch");
3707 response->code = COAP_RESPONSE_CODE(408);
3708 goto free_lg_srcv;
3709 }
3710 coap_delete_bin_const(lg_srcv->last_token);
3711 lg_srcv->last_token = coap_new_bin_const(pdu->actual_token.s,
3712 pdu->actual_token.length);
3713 }
3714#endif /* COAP_Q_BLOCK_SUPPORT */
3715
3716 lg_srcv->last_type = pdu->type;
3717
3718 if (context->max_bodies_ram) {
3719 ssize_t increase = total - (lg_srcv->body_data ? lg_srcv->body_data->length : 0);
3720
3721 if (increase > 0 && context->cur_bodies_ram + increase > context->max_bodies_ram) {
3722 uint8_t buf[4];
3723
3726 sizeof(buf),
3727 5),
3728 buf);
3729 coap_add_data(response, sizeof("Memory limit hit")-1,
3730 (const uint8_t *)"Memory limit hit");
3731 response->code = COAP_RESPONSE_CODE(503);
3732 goto skip_app_handler;
3733 }
3734 }
3735
3736 update_data = 0;
3737 saved_num = block.num;
3738 saved_offset = offset;
3739
3740 while (offset < saved_offset + length) {
3741 uint32_t block_m = block.m;
3742
3743 /*
3744 * A BERT payload contains multiple logical 1024-byte blocks, but its
3745 * Block1 M bit describes the complete BERT payload. Mark intermediate
3746 * logical blocks as having more data so total_blocks is only set by the
3747 * final logical block.
3748 */
3749 if (block.bert && offset + 1024 < saved_offset + length)
3750 block_m = 1;
3751 if (!check_if_received_block(&lg_srcv->rec_blocks, block.num)) {
3752 /* Update list of blocks received */
3753 if (blocks_add_entry(&lg_srcv->rec_blocks, block.num, block_m)) {
3754 update_data = 1;
3755 } else {
3756 coap_ticks(&lg_srcv->rec_blocks.last_seen);
3757 coap_log_debug("Block nr %u ignored (too many missing blocks)\n", block.num);
3758#if COAP_Q_BLOCK_SUPPORT
3759 if (block_option == COAP_OPTION_Q_BLOCK1)
3760 request_missing = 1;
3761#endif /* COAP_Q_BLOCK_SUPPORT */
3762 }
3763 } else {
3764 coap_ticks(&lg_srcv->rec_blocks.last_seen);
3765 coap_log_debug("Duplicate block nr %u\n", block.num);
3766 }
3767 block.num++;
3768 offset = block.num << (block.szx + 4);
3769 }
3770 if (length)
3771 block.num--;
3772
3773#if COAP_Q_BLOCK_SUPPORT
3774 if (request_missing) {
3775 coap_request_missing_q_block1(session, lg_srcv, block.num);
3776 goto skip_app_handler;
3777 }
3778#endif /* COAP_Q_BLOCK_SUPPORT */
3779
3780 if (update_data) {
3781 /* Update saved data */
3782#if COAP_Q_BLOCK_SUPPORT
3783 lg_srcv->rec_blocks.processing_payload_set =
3784 block.num / COAP_MAX_PAYLOADS(session);
3785#endif /* COAP_Q_BLOCK_SUPPORT */
3786 if (lg_srcv->total_len < saved_offset + length) {
3787 lg_srcv->total_len = saved_offset + length;
3788 }
3789
3790#define USE_BLOCK_DATA_HANDLER (context && context->block_data_cb && \
3791 !resource->is_proxy_uri && \
3792 !resource->is_reverse_proxy && \
3793 ((session->block_mode & COAP_SINGLE_BLOCK_OR_Q) || block.bert) && \
3794 (resource->flags & COAP_RESOURCE_USE_BLOCK_DATA_HANDLER))
3795
3796 if (lg_srcv->body_data)
3797 session->context->cur_bodies_ram -= lg_srcv->body_data->length;
3798 if (USE_BLOCK_DATA_HANDLER) {
3799 coap_response_t resp;
3800
3802 context->block_data_cb(session, pdu, resource,
3803 &lg_srcv->body_data,
3804 length, data, saved_offset,
3805 lg_srcv->total_len));
3806 if (resp != COAP_RESPONSE_OK) {
3807 response->code = COAP_RESPONSE_CODE(500);
3808 goto skip_app_handler;
3809 }
3810 } else {
3811 lg_srcv->body_data = coap_block_build_body_lkd(lg_srcv->body_data, length, data,
3812 saved_offset, lg_srcv->total_len);
3813 if (!lg_srcv->body_data) {
3814 coap_add_data(response, sizeof("Memory issue")-1,
3815 (const uint8_t *)"Memory issue");
3816 response->code = COAP_RESPONSE_CODE(500);
3817 goto skip_app_handler;
3818 }
3819 }
3820 if (lg_srcv->body_data)
3821 context->cur_bodies_ram += lg_srcv->body_data->length;
3822 } else {
3823#if COAP_Q_BLOCK_SUPPORT
3824 if (block_option == COAP_OPTION_Q_BLOCK1) {
3825 goto q_block_1_check;
3826 }
3827#endif /* COAP_Q_BLOCK_SUPPORT */
3828 /*
3829 * Every block carried by this request had already been received, so there
3830 * is no new data and the application must not see the request twice
3831 * (RFC7252 4.5). The request still has to be answered: falling straight
3832 * through to skip_app_handler leaves response->code at 0, which goes out
3833 * as an Empty ACK.
3834 * Re-issue the 2.31 Continue this block was given the first time.
3835 */
3836 if (block.m) {
3837 uint8_t buf[4];
3838
3839 coap_insert_option(response, block_option,
3840 coap_encode_var_safe(buf, sizeof(buf),
3841 (saved_num << 4) |
3842 (1 << 3) |
3843 lg_srcv->szx),
3844 buf);
3845 response->code = COAP_RESPONSE_CODE(231);
3846 }
3847 goto skip_app_handler;
3848 }
3849#if COAP_Q_BLOCK_SUPPORT
3850q_block_1_check:
3851#endif /* COAP_Q_BLOCK_SUPPORT */
3852
3853 if (block.m ||
3854 !check_all_blocks_in(&lg_srcv->rec_blocks)) {
3855 /* Not all the payloads of the body have arrived */
3856 if (block.m) {
3857 uint8_t buf[4];
3858
3859#if COAP_Q_BLOCK_SUPPORT
3860 if (block_option == COAP_OPTION_Q_BLOCK1) {
3861 if (check_all_blocks_in(&lg_srcv->rec_blocks)) {
3862 goto give_app_data;
3863 }
3864 if (lg_srcv->rec_blocks.used == 1 &&
3865 (lg_srcv->rec_blocks.range[0].end % COAP_MAX_PAYLOADS(session)) + 1
3866 == COAP_MAX_PAYLOADS(session)) {
3867 /* Seen all packets of this PAYLOAD_SET */
3868 if (block.num != lg_srcv->rec_blocks.range[0].end) {
3869 /* Blocks could arrive in wrong order but still need to send 2.31 */
3870 saved_num = block.num = lg_srcv->rec_blocks.range[0].end;
3871 }
3872 } else if (lg_srcv->rec_blocks.used > 1 &&
3873 (block.num % COAP_MAX_PAYLOADS(session)) + 1 == COAP_MAX_PAYLOADS(session) &&
3874 lg_srcv->rec_blocks.range[lg_srcv->rec_blocks.used-1].begin <
3875 lg_srcv->rec_blocks.range[lg_srcv->rec_blocks.used-1].end &&
3876 (lg_srcv->rec_blocks.range[lg_srcv->rec_blocks.used-1].end % COAP_MAX_PAYLOADS(session)) + 1
3877 == COAP_MAX_PAYLOADS(session)) {
3878 /*
3879 * At least the last 2 of a payload set have been received, but some
3880 * missing packets, but have seen the emd of a payload set.
3881 */
3882 coap_log_debug("Fast recovery for block %u\n", block.num);
3883 coap_request_missing_q_block1(session, lg_srcv, block.num);
3884 goto skip_app_handler;
3885 } else if (lg_srcv->rec_blocks.used > 1 &&
3886 (block.num / COAP_MAX_PAYLOADS(session)) >
3887 (lg_srcv->rec_blocks.range[0].end / COAP_MAX_PAYLOADS(session))) {
3888 /*
3889 * Current MAX_PAYLOAD chunk is different to last MAX_PAYLOAD chunk
3890 * with some missing packets in last MAX_PAYLOAD chunk.
3891 * client has timed out and starting transmission of next PAYLOAD_SET.
3892 */
3893 coap_request_missing_q_block1(session, lg_srcv, block.num);
3894 goto skip_app_handler;
3895 } else {
3896 /* The remote end will be sending the next one unless this
3897 is a MAX_PAYLOADS and all previous have been received
3898 as caught above */
3899 goto skip_app_handler;
3900 }
3901 if (COAP_PROTO_RELIABLE(session->proto) ||
3902 pdu->type != COAP_MESSAGE_NON)
3903 goto skip_app_handler;
3904 }
3905#endif /* COAP_Q_BLOCK_SUPPORT */
3906
3907 /* Check to see if block size is getting forced down */
3908 max_block_szx = COAP_BLOCK_MAX_SIZE_GET(session->block_mode);
3909 if (!block.bert && saved_num == 0 && max_block_szx != 0 &&
3910 max_block_szx < block.aszx) {
3911 block.aszx = max_block_szx;
3912 }
3913
3914 /*
3915 * If the last block has been seen, packets are coming in in
3916 * random order. If all blocks are now in, then need to send
3917 * complete payload to application and acknowledge this current
3918 * block.
3919 */
3920 if ((total == 0 && block.m) || !check_all_blocks_in(&lg_srcv->rec_blocks)) {
3921 /* Ask for the next block */
3922 coap_insert_option(response, block_option,
3923 coap_encode_var_safe(buf, sizeof(buf),
3924 (saved_num << 4) |
3925 (block.m << 3) |
3926 block.aszx),
3927 buf);
3928 response->code = COAP_RESPONSE_CODE(231);
3929 } else {
3930 /* Need to separately respond to this request */
3931 coap_pdu_t *tmp_pdu = coap_pdu_duplicate_lkd(response,
3932 session,
3933 response->actual_token.length,
3934 response->actual_token.s,
3936 if (tmp_pdu) {
3937 tmp_pdu->code = COAP_RESPONSE_CODE(231);
3938 if (coap_send_internal(session, tmp_pdu, NULL) == COAP_INVALID_MID) {
3939 /* lg_srcv may have got deleted */
3940 coap_lg_srcv_t *sg;
3941
3942 LL_FOREACH(session->lg_srcv, sg) {
3943 if (lg_srcv == sg) {
3944 /* Still there */
3945 break;
3946 }
3947 }
3948 if (!sg)
3949 goto skip_app_handler;
3950 }
3951 }
3952 if (lg_srcv->last_token) {
3953 coap_update_token(response, lg_srcv->last_token->length, lg_srcv->last_token->s);
3954 coap_update_token(pdu, lg_srcv->last_token->length, lg_srcv->last_token->s);
3955 }
3956 /* Pass the assembled pdu and body to the application */
3957 goto give_app_data;
3958 }
3959 } else {
3960 /* block.m Block More option not set. Some outstanding blocks */
3961#if COAP_Q_BLOCK_SUPPORT
3962 if (block_option != COAP_OPTION_Q_BLOCK1) {
3963#endif /* COAP_Q_BLOCK_SUPPORT */
3964 /* Last chunk - but not all in */
3965 coap_ticks(&lg_srcv->last_used);
3966 lg_srcv->no_more_seen = 1;
3967 coap_delete_bin_const(lg_srcv->last_token);
3968 lg_srcv->last_token = coap_new_bin_const(pdu->actual_token.s,
3969 pdu->actual_token.length);
3970
3971 /*
3972 * Need to just ACK (no response code) to handle client's NSTART.
3973 * When final missing block comes in, we will pass all the data
3974 * for processing so a 2.01, 2.04 etc. code can be generated
3975 * and responded to as a separate response "RFC7252 5.2.2. Separate"
3976 * If missing block(s) do not come in, then will generate a 4.08
3977 * when lg_srcv times out.
3978 * Fall through to skip_app_handler.
3979 */
3980#if COAP_Q_BLOCK_SUPPORT
3981 } else {
3982 coap_request_missing_q_block1(session, lg_srcv, block.num);
3983 }
3984#endif /* COAP_Q_BLOCK_SUPPORT */
3985 }
3986 goto skip_app_handler;
3987 }
3988
3989 /*
3990 * Entire payload received.
3991 * Remove the Block1 option as passing all of the data to
3992 * application layer. Add back in observe option if appropriate.
3993 * Adjust all other information.
3994 */
3995give_app_data:
3996 if (lg_srcv->observe_set) {
3998 lg_srcv->observe_length, lg_srcv->observe);
3999 }
4000 coap_remove_option(pdu, block_option);
4001 if (lg_srcv->body_data) {
4002 pdu->body_data = lg_srcv->body_data->s;
4003 pdu->body_length = lg_srcv->total_len;
4004 } else {
4005 pdu->body_data = NULL;
4006 pdu->body_length = 0;
4007 }
4008 pdu->body_offset = 0;
4009 pdu->body_total = lg_srcv->total_len;
4010 if (USE_BLOCK_DATA_HANDLER) {
4011 /* Data has already been provided - do not duplicate */
4012 if (pdu->data) {
4013 pdu->used_size = pdu->data - pdu->token - 1;
4014 pdu->data = NULL;
4015 }
4016 }
4017 coap_log_debug("Server app version of updated PDU\n");
4019 lg_srcv->dont_timeout = 1;
4020 *pfree_lg_srcv = lg_srcv;
4021
4022call_app_handler:
4023 coap_delete_cache_key(cache_key_l);
4024 if (lg_srcv_is_refed)
4025 coap_lg_srcv_release_lkd(session, lg_srcv);
4026 return 0;
4027
4028free_lg_srcv:
4029 LL_DELETE(session->lg_srcv, lg_srcv);
4030 coap_block_delete_lg_srcv(session, lg_srcv);
4031
4032skip_app_handler:
4033 coap_delete_cache_key(cache_key_l);
4034 if (lg_srcv_is_refed)
4035 coap_lg_srcv_release_lkd(session, lg_srcv);
4036 return 1;
4037}
4038#endif /* COAP_SERVER_SUPPORT */
4039
4040#if COAP_CLIENT_SUPPORT
4041#if COAP_Q_BLOCK_SUPPORT
4042static uint32_t
4043derive_cbor_value(const uint8_t **bp, size_t rem_len) {
4044 uint32_t value = **bp & 0x1f;
4045 (*bp)++;
4046 if (value < 24) {
4047 return value;
4048 } else if (value == 24) {
4049 if (rem_len < 2)
4050 return (uint32_t)-1;
4051 value = **bp;
4052 (*bp)++;
4053 return value;
4054 } else if (value == 25) {
4055 if (rem_len < 3)
4056 return (uint32_t)-1;
4057 value = **bp << 8;
4058 (*bp)++;
4059 value |= **bp;
4060 (*bp)++;
4061 return value;
4062 }
4063 if (rem_len < 5)
4064 return (uint32_t)-1;
4065 value = (uint32_t)(**bp) << 24;
4066 (*bp)++;
4067 value |= **bp << 16;
4068 (*bp)++;
4069 value |= **bp << 8;
4070 (*bp)++;
4071 value |= **bp;
4072 (*bp)++;
4073 return value;
4074}
4075#endif /* COAP_Q_BLOCK_SUPPORT */
4076
4077static int
4078check_freshness(coap_session_t *session, coap_pdu_t *rcvd, coap_pdu_t *sent,
4079 coap_lg_xmit_t *lg_xmit, coap_lg_crcv_t *lg_crcv) {
4080 /* Check for Echo option for freshness */
4081 coap_opt_iterator_t opt_iter;
4082 coap_opt_t *opt = coap_check_option(rcvd, COAP_OPTION_ECHO, &opt_iter);
4083
4084 if (opt) {
4085 if (sent || lg_xmit || lg_crcv) {
4086 /* Need to retransmit original request with Echo option added */
4087 coap_pdu_t *echo_pdu;
4088 coap_mid_t mid;
4089 const uint8_t *data;
4090 size_t data_len;
4091 int have_data = 0;
4092 uint8_t ltoken[8];
4093 size_t ltoken_len;
4094 uint64_t token;
4095
4096 if (sent) {
4097 if (coap_get_data(sent, &data_len, &data))
4098 have_data = 1;
4099 } else if (lg_xmit) {
4100 sent = lg_xmit->sent_pdu;
4101 if (lg_xmit->data_info->length) {
4102 size_t blk_size = (size_t)1 << (lg_xmit->blk_size + 4);
4103 size_t offset = (lg_xmit->last_block + 1) * blk_size;
4104 have_data = 1;
4105 data = &lg_xmit->data_info->data[offset];
4106 data_len = (lg_xmit->data_info->length - offset) > blk_size ? blk_size :
4107 lg_xmit->data_info->length - offset;
4108 }
4109 } else { /* lg_crcv */
4110 sent = lg_crcv->sent_pdu;
4111 if (coap_get_data(sent, &data_len, &data))
4112 have_data = 1;
4113 }
4114 if (lg_xmit) {
4115 token = STATE_TOKEN_FULL(lg_xmit->b.b1.state_token,
4116 ++lg_xmit->b.b1.count);
4117 } else {
4118 token = STATE_TOKEN_FULL(lg_crcv->state_token,
4119 ++lg_crcv->retry_counter);
4120 }
4121 ltoken_len = coap_encode_var_safe8(ltoken, sizeof(token), token);
4122 echo_pdu = coap_pdu_duplicate_lkd(sent, session, ltoken_len, ltoken,
4124 if (!echo_pdu)
4125 return 0;
4126 if (!coap_insert_option(echo_pdu, COAP_OPTION_ECHO,
4127 coap_opt_length(opt), coap_opt_value(opt)))
4128 goto not_sent;
4129 if (have_data) {
4130 coap_add_data(echo_pdu, data_len, data);
4131 }
4132 /* Need to track Observe token change if Observe */
4133 track_fetch_observe(echo_pdu, lg_crcv, 0, &echo_pdu->actual_token);
4134#if COAP_OSCORE_SUPPORT
4135 if (session->oscore_encryption &&
4136 (opt = coap_check_option(echo_pdu, COAP_OPTION_OBSERVE, &opt_iter)) &&
4138 /* Need to update the base PDU's Token for closing down Observe */
4139 if (lg_xmit) {
4140 lg_xmit->b.b1.state_token = token;
4141 } else {
4142 lg_crcv->state_token = token;
4143 }
4144 }
4145#endif /* COAP_OSCORE_SUPPORT */
4146 mid = coap_send_internal(session, echo_pdu, NULL);
4147 if (mid == COAP_INVALID_MID)
4148 goto not_sent;
4149 return 1;
4150 } else {
4151 /* Need to save Echo option value to add to next reansmission */
4152not_sent:
4153 coap_delete_bin_const(session->echo);
4154 session->echo = coap_new_bin_const(coap_opt_value(opt),
4155 coap_opt_length(opt));
4156 }
4157 }
4158 return 0;
4159}
4160
4161static void
4162track_echo(coap_session_t *session, coap_pdu_t *rcvd) {
4163 coap_opt_iterator_t opt_iter;
4164 coap_opt_t *opt = coap_check_option(rcvd, COAP_OPTION_ECHO, &opt_iter);
4165
4166 if (opt) {
4167 coap_delete_bin_const(session->echo);
4168 session->echo = coap_new_bin_const(coap_opt_value(opt),
4169 coap_opt_length(opt));
4170 }
4171}
4172
4173/*
4174 * Need to see if this is a response to a large body request transfer. If so,
4175 * need to initiate the request containing the next block and not trouble the
4176 * application. Note that Token must unique per request/response.
4177 *
4178 * Client receives large data acknowledgement from server (Block1)
4179 *
4180 * This is set up using coap_add_data_large_request_lkd()
4181 *
4182 * Client is using GET etc.
4183 *
4184 * Return: 0 Call application handler
4185 * 1 Do not call application handler - just send the built response
4186 */
4187int
4188coap_handle_response_send_block(coap_session_t *session, coap_pdu_t *sent,
4189 coap_pdu_t *rcvd) {
4190 coap_lg_xmit_t *lg_xmit;
4191 coap_lg_crcv_t *lg_crcv = NULL;
4192 int lg_crcv_is_refed = 0;
4193
4194 lg_xmit = coap_find_lg_xmit(session, rcvd);
4195 if (lg_xmit) {
4196 /* lg_xmit found */
4197 size_t chunk = (size_t)1 << (lg_xmit->blk_size + 4);
4198 coap_block_b_t block;
4199
4200 lg_crcv = coap_find_lg_crcv(session, rcvd);
4201 if (lg_crcv) {
4202 coap_ticks(&lg_crcv->last_used);
4203 coap_lg_crcv_reference_lkd(lg_crcv);
4204 lg_crcv_is_refed = 1;
4205 }
4206
4207 if (COAP_RESPONSE_CLASS(rcvd->code) == 2 &&
4208 coap_get_block_b(session, rcvd, lg_xmit->option, &block)) {
4209
4210 if (block.bert) {
4211 coap_log_debug("found Block option, block is BERT, block nr. %u (%" PRIuS ")\n",
4212 block.num, lg_xmit->b.b1.bert_size);
4213 } else {
4214 coap_log_debug("found Block option, block size is %u, block nr. %u\n",
4215 1 << (block.szx + 4), block.num);
4216 }
4217 if (block.szx != lg_xmit->blk_size) {
4218 if (block.szx > lg_xmit->blk_size) {
4219 coap_log_info("ignoring request to increase Block size, "
4220 "(%u > %u)\n",
4221 1 << (block.szx + 4), 1 << (lg_xmit->blk_size + 4));
4222 } else {
4223 /*
4224 * Recompute the block number of the previous packet given the
4225 * new block size
4226 */
4227 block.num = (uint32_t)((chunk >> (block.szx + 4)) - 1);
4228 chunk = (size_t)1 << (lg_xmit->blk_size + 4);
4229#if COAP_Q_BLOCK_SUPPORT
4230 lg_xmit->send_blocks.range[0].begin = block.num;
4231 lg_xmit->send_blocks.range[0].end = (uint32_t)((lg_xmit->data_info->length -1) / chunk);
4232#endif /* COAP_Q_BLOCK_SUPPORT */
4233 lg_xmit->blk_size = block.szx;
4234 coap_log_debug("new Block size is %u, block number %u completed\n",
4235 1 << (block.szx + 4), block.num);
4236 block.bert = 0;
4237 block.aszx = block.szx;
4238 }
4239 }
4240 track_echo(session, rcvd);
4241#if COAP_Q_BLOCK_SUPPORT
4242 if (rcvd->code == COAP_RESPONSE_CODE(231) &&
4243 lg_xmit->option == COAP_OPTION_Q_BLOCK1) {
4244 coap_send_q_blocks(session, lg_xmit, block, lg_xmit->sent_pdu, COAP_SEND_SKIP_PDU);
4245 goto skip_app_handler;
4246 }
4247#endif /* COAP_Q_BLOCK_SUPPORT */
4248 if (lg_xmit->last_block == (int)block.num &&
4249 lg_xmit->option != COAP_OPTION_Q_BLOCK1) {
4250 /*
4251 * Duplicate Block1 ACK
4252 *
4253 * RFCs not clear here, but on a lossy connection, there could
4254 * be multiple Block1 ACKs, causing the client to retransmit the
4255 * same block multiple times, or the server retransmitting the
4256 * same ACK.
4257 *
4258 * Once a block has been ACKd, there is no need to retransmit it.
4259 */
4260 goto skip_app_handler;
4261 }
4262 if (block.bert)
4263 block.num += (unsigned int)(lg_xmit->b.b1.bert_size / 1024 - 1);
4264 lg_xmit->last_block = block.num;
4265 if ((block.num + 1) * chunk < lg_xmit->data_info->length) {
4266 /* Build the next PDU request based off the skeletal PDU */
4267 uint8_t buf[8];
4268 coap_pdu_t *pdu;
4269 uint64_t token = STATE_TOKEN_FULL(lg_xmit->b.b1.state_token, ++lg_xmit->b.b1.count);
4270 size_t len = coap_encode_var_safe8(buf, sizeof(token), token);
4271 size_t offset;
4272
4273 if (lg_xmit->sent_pdu->code == COAP_REQUEST_CODE_FETCH) {
4274 /* Need to handle Observe for large FETCH */
4275 if (lg_crcv) {
4276 if (coap_binary_equal(lg_xmit->b.b1.app_token, lg_crcv->app_token)) {
4277 coap_bin_const_t *new_token;
4278 coap_bin_const_t ctoken = { len, buf };
4279
4280 /* Need to save/restore Observe Token for large FETCH */
4281 new_token = track_fetch_observe(lg_xmit->sent_pdu, lg_crcv, block.num + 1,
4282 &ctoken);
4283 if (new_token) {
4284 assert(len <= sizeof(buf));
4285 len = new_token->length;
4286 memcpy(buf, new_token->s, len);
4287 }
4288 }
4289 }
4290 }
4291 pdu = coap_pdu_duplicate_lkd(lg_xmit->sent_pdu, session,
4292 len, buf, NULL, COAP_BOOL_FALSE);
4293 if (!pdu)
4294 goto fail_body;
4295
4296 /*
4297 * If initial transmit was multicast, that would have been NON.
4298 * Make subsequent traffic CON for reliability.
4299 */
4300 if (session->sock.flags & COAP_SOCKET_MULTICAST) {
4301 pdu->type = COAP_MESSAGE_CON;
4302 }
4303
4304 block.num++;
4305 offset = block.num * chunk;
4306 if (block.bert) {
4307 size_t token_options = pdu->data ? (size_t)(pdu->data - pdu->token) :
4308 pdu->used_size;
4309 block.m = (lg_xmit->data_info->length - offset) >
4310 ((pdu->max_size - token_options) /1024) * 1024;
4311 } else {
4312 block.m = (offset + chunk) < lg_xmit->data_info->length;
4313 }
4314 coap_update_option(pdu, lg_xmit->option,
4315 coap_encode_var_safe(buf, sizeof(buf),
4316 (block.num << 4) |
4317 (block.m << 3) |
4318 block.aszx),
4319 buf);
4320
4321 if (lg_xmit->data_info->get_func) {
4322#if COAP_CONSTRAINED_STACK
4323 /* Protected by global_lock if needed */
4324 static uint8_t l_data[1024];
4325#else /* ! COAP_CONSTRAINED_STACK */
4326 uint8_t l_data[1024];
4327#endif /* ! COAP_CONSTRAINED_STACK */
4328 size_t l_length;
4329
4330 assert(chunk <= 1024);
4331 if (lg_xmit->data_info->get_func(session, chunk,
4332 block.num * chunk, l_data, &l_length,
4333 lg_xmit->data_info->app_ptr)) {
4334 if (!coap_add_data(pdu, l_length, l_data)) {
4335 goto fail_body;
4336 }
4337 }
4338 } else {
4339 if (!coap_add_block_b_data(pdu,
4340 lg_xmit->data_info->length,
4341 lg_xmit->data_info->data,
4342 &block))
4343 goto fail_body;
4344 }
4345 lg_xmit->b.b1.bert_size = block.chunk_size;
4346 coap_ticks(&lg_xmit->last_sent);
4347#if COAP_Q_BLOCK_SUPPORT
4348 if (lg_xmit->option == COAP_OPTION_Q_BLOCK1 &&
4349 pdu->type == COAP_MESSAGE_NON) {
4350 if (coap_send_q_block1(session, block, pdu,
4351 COAP_SEND_INC_PDU) == COAP_INVALID_MID)
4352 goto fail_body;
4353 goto skip_app_handler;
4354 } else if (coap_send_internal(session, pdu, NULL) == COAP_INVALID_MID)
4355 goto fail_body;
4356#else /* ! COAP_Q_BLOCK_SUPPORT */
4357 if (coap_send_internal(session, pdu, NULL) == COAP_INVALID_MID)
4358 goto fail_body;
4359#endif /* ! COAP_Q_BLOCK_SUPPORT */
4360 goto skip_app_handler;
4361 }
4362 } else if (COAP_RESPONSE_CLASS(rcvd->code) == 2) {
4363 /*
4364 * Not a block response asking for the next block.
4365 * Could be an Observe response overlapping with block FETCH doing
4366 * Observe cancellation.
4367 */
4368 coap_opt_iterator_t opt_iter;
4369 coap_opt_t *obs_opt;
4370 int observe_action = -1;
4371
4372 if (lg_xmit->sent_pdu->code != COAP_REQUEST_CODE_FETCH) {
4373 goto lg_xmit_finished;
4374 }
4375 obs_opt = coap_check_option(lg_xmit->sent_pdu,
4377 &opt_iter);
4378 if (obs_opt) {
4379 observe_action = coap_decode_var_bytes(coap_opt_value(obs_opt),
4380 coap_opt_length(obs_opt));
4381 }
4382 if (observe_action != COAP_OBSERVE_CANCEL) {
4383 goto lg_xmit_finished;
4384 }
4385 obs_opt = coap_check_option(rcvd,
4387 &opt_iter);
4388 if (obs_opt) {
4389 goto call_app_handler;
4390 }
4391 goto lg_xmit_finished;
4392 } else if (rcvd->code == COAP_RESPONSE_CODE(401)) {
4393 if (check_freshness(session, rcvd, sent, lg_xmit, NULL))
4394 goto skip_app_handler;
4395#if COAP_Q_BLOCK_SUPPORT
4396 } else if (rcvd->code == COAP_RESPONSE_CODE(402)) {
4397 /* Q-Block1 or Q-Block2 not present in p - duplicate error ? */
4398 if (coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK2, &block) ||
4399 coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK1, &block))
4400 goto skip_app_handler;
4401 } else if (rcvd->code == COAP_RESPONSE_CODE(408) &&
4402 lg_xmit->option == COAP_OPTION_Q_BLOCK1) {
4403 size_t length;
4404 const uint8_t *data;
4405 coap_opt_iterator_t opt_iter;
4406 coap_opt_t *fmt_opt = coap_check_option(rcvd,
4408 &opt_iter);
4409 uint16_t fmt = fmt_opt ?
4411 coap_opt_length(fmt_opt)) :
4413
4415 goto fail_body;
4416
4417 if (COAP_PROTO_RELIABLE(session->proto) ||
4418 rcvd->type != COAP_MESSAGE_NON) {
4419 coap_log_debug("Unexpected 4.08 - protocol violation - ignore\n");
4420 goto skip_app_handler;
4421 }
4422
4423 if (coap_get_data(rcvd, &length, &data)) {
4424 /* Need to decode CBOR to work out what blocks to re-send */
4425 const uint8_t *bp = data;
4426 uint32_t i;
4427
4428 for (i = 0; (bp < data + length) &&
4429 i < COAP_MAX_PAYLOADS(session); i++) {
4430 if ((*bp & 0xc0) != 0x00) /* uint(value) */
4431 goto fail_cbor;
4432 block.num = derive_cbor_value(&bp, data + length - bp);
4433 coap_log_debug("Q-Block1: Missing block %d\n", block.num);
4434 if (block.num > (1 << 20) -1)
4435 goto fail_cbor;
4436 block.m = (block.num + 1) * chunk < lg_xmit->data_info->length;
4437 block.szx = lg_xmit->blk_size;
4438
4439 blocks_add_entry(&lg_xmit->send_blocks, block.num, block.m);
4440 }
4441 if (lg_xmit->send_blocks.used) {
4442 /* Flush out the first one */
4443 block.num = lg_xmit->send_blocks.range[0].begin;
4444 block.m = (block.num + 1) * chunk < lg_xmit->data_info->length;
4445 coap_send_q_blocks(session, lg_xmit, block, lg_xmit->sent_pdu, COAP_SEND_MISSING);
4446 }
4447 goto skip_app_handler;
4448 }
4449fail_cbor:
4450 coap_log_info("Invalid application/missing-blocks+cbor-seq\n");
4451#endif /* COAP_Q_BLOCK_SUPPORT */
4452 }
4453 goto lg_xmit_finished;
4454 }
4455 goto call_app_handler;
4456
4457fail_body:
4459 /* There has been an internal error of some sort */
4460 rcvd->code = COAP_RESPONSE_CODE(500);
4461lg_xmit_finished:
4462 if (lg_crcv) {
4463 if (STATE_TOKEN_BASE(lg_xmit->b.b1.state_token) ==
4464 STATE_TOKEN_BASE(lg_crcv->state_token)) {
4465 /* In case of observe */
4466 lg_crcv->state_token = lg_xmit->b.b1.state_token;
4467 lg_crcv->retry_counter = lg_xmit->b.b1.count;
4468 }
4469 }
4470 if (!lg_crcv) {
4471 /* need to put back original token into rcvd */
4472 if (lg_xmit->b.b1.app_token)
4473 coap_update_token(rcvd, lg_xmit->b.b1.app_token->length,
4474 lg_xmit->b.b1.app_token->s);
4475 coap_log_debug("Client app version of updated PDU (1)\n");
4477 } else {
4478 lg_crcv->sent_pdu->lg_xmit = 0;
4479 }
4480
4481 if (sent) {
4482 /* need to put back original token into sent */
4483 if (lg_xmit->b.b1.app_token)
4484 coap_update_token(sent, lg_xmit->b.b1.app_token->length,
4485 lg_xmit->b.b1.app_token->s);
4486 if (sent->lg_xmit)
4487 coap_remove_option(sent, sent->lg_xmit->option);
4488 sent->lg_xmit = NULL;
4489 }
4490 LL_DELETE(session->lg_xmit, lg_xmit);
4491 coap_block_delete_lg_xmit(session, lg_xmit);
4492
4493call_app_handler:
4494 if (lg_crcv_is_refed)
4495 coap_lg_crcv_release_lkd(session, lg_crcv);
4496 return 0;
4497
4498skip_app_handler:
4499 if (lg_crcv_is_refed)
4500 coap_lg_crcv_release_lkd(session, lg_crcv);
4501 return 1;
4502}
4503#endif /* COAP_CLIENT_SUPPORT */
4504
4505void
4507 coap_block_data_handler_t block_data_handler) {
4508 context->block_data_cb = block_data_handler;
4509}
4510
4512coap_block_build_body(coap_binary_t *body_data, size_t length,
4513 const uint8_t *data, size_t offset, size_t total) {
4514 coap_binary_t *ret;
4515
4516 coap_lock_lock(return NULL);
4517 ret = coap_block_build_body_lkd(body_data, length, data, offset, total);
4519 return ret;
4520}
4521/*
4522 * Re-assemble payloads into a body
4523 */
4526 const uint8_t *data, size_t offset, size_t total) {
4527 if (data == NULL)
4528 return NULL;
4529 if (body_data == NULL && total) {
4530 body_data = coap_new_binary(total);
4531 }
4532 if (body_data == NULL)
4533 return NULL;
4534
4535 /* Check no overflow (including a 8 byte small headroom) */
4536 if (SIZE_MAX - 8 < length || offset > SIZE_MAX - length - 8) {
4537 coap_delete_binary(body_data);
4538 return NULL;
4539 }
4540
4541 /* Update saved data */
4542 if (offset + length <= total && body_data->length >= total) {
4543 memcpy(&body_data->s[offset], data, length);
4544 } else {
4545 /*
4546 * total may be inaccurate as per
4547 * https://rfc-editor.org/rfc/rfc7959#section-4
4548 * o In a request carrying a Block1 Option, to indicate the current
4549 * estimate the client has of the total size of the resource
4550 * representation, measured in bytes ("size indication").
4551 * o In a response carrying a Block2 Option, to indicate the current
4552 * estimate the server has of the total size of the resource
4553 * representation, measured in bytes ("size indication").
4554 */
4555 coap_binary_t *new = coap_resize_binary(body_data, offset + length);
4556
4557 if (new) {
4558 body_data = new;
4559 memcpy(&body_data->s[offset], data, length);
4560 } else {
4561 coap_delete_binary(body_data);
4562 return NULL;
4563 }
4564 }
4565 return body_data;
4566}
4567
4568#if COAP_CLIENT_SUPPORT
4569/*
4570 * Need to see if this is a large body response to a request. If so,
4571 * need to initiate the request for the next block and not trouble the
4572 * application. Note that Token must be unique per request/response.
4573 *
4574 * This is set up using coap_send()
4575 * Client receives large data from server ((Q-)Block2)
4576 *
4577 * Return: 0 Call application handler
4578 * 1 Do not call application handler - just sent the next request
4579 */
4580int
4581coap_handle_response_get_block(coap_context_t *context,
4582 coap_session_t *session,
4583 coap_pdu_t *sent,
4584 coap_pdu_t *rcvd,
4585 coap_recurse_t recursive) {
4586 coap_lg_crcv_t *lg_crcv;
4587 coap_block_b_t block;
4588#if COAP_Q_BLOCK_SUPPORT
4589 coap_block_b_t qblock;
4590#endif /* COAP_Q_BLOCK_SUPPORT */
4591 int have_block = 0;
4592 uint16_t block_opt = 0;
4593 size_t offset;
4594 int ack_rst_sent = 0;
4595 coap_opt_iterator_t opt_iter;
4596
4598 memset(&block, 0, sizeof(block));
4599#if COAP_Q_BLOCK_SUPPORT
4600 memset(&qblock, 0, sizeof(qblock));
4601#endif /* COAP_Q_BLOCK_SUPPORT */
4602 lg_crcv = coap_find_lg_crcv(session, rcvd);
4603 if (lg_crcv) {
4604 size_t chunk = 0;
4605 uint8_t buf[8];
4606 coap_tick_t adjust;
4607
4608 if (COAP_RESPONSE_CLASS(rcvd->code) == 2) {
4609 size_t length;
4610 const uint8_t *data;
4612 &opt_iter);
4613 size_t size2 = size_opt ?
4615 coap_opt_length(size_opt)) : 0;
4616
4617 /* length and data are cleared on error */
4618 (void)coap_get_data(rcvd, &length, &data);
4619 rcvd->body_offset = 0;
4620 rcvd->body_total = length;
4621 if (coap_get_block_b(session, rcvd, COAP_OPTION_BLOCK2, &block)) {
4622 have_block = 1;
4623 block_opt = COAP_OPTION_BLOCK2;
4624 }
4625#if COAP_Q_BLOCK_SUPPORT
4626 if (coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK2, &qblock)) {
4627 if (have_block) {
4628 coap_log_warn("Both Block1 and Q-Block1 not supported in a response\n");
4629 }
4630 have_block = 1;
4631 block_opt = COAP_OPTION_Q_BLOCK2;
4632 block = qblock;
4633 /* server indicating that it supports Q_BLOCK */
4634 if (!(session->block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
4635 set_block_mode_has_q(session->block_mode);
4636 }
4637 }
4638#endif /* COAP_Q_BLOCK_SUPPORT */
4639 track_echo(session, rcvd);
4640 if (have_block && (block.m || length)) {
4641 coap_opt_t *fmt_opt = coap_check_option(rcvd,
4643 &opt_iter);
4644 uint16_t fmt = fmt_opt ?
4646 coap_opt_length(fmt_opt)) :
4648 coap_opt_t *etag_opt = coap_check_option(rcvd,
4650 &opt_iter);
4651 size_t saved_offset;
4652 int updated_block;
4653
4654#if COAP_Q_BLOCK_SUPPORT
4655 if (block_opt == COAP_OPTION_Q_BLOCK2 && (!size_opt || !etag_opt)) {
4656 /* RFC9177 section-4.4 */
4657 coap_log_info("Q-Block2: Size2 and ETag options required\n");
4658 /* Try to hint to the server there is an issue */
4659 coap_send_rst_lkd(session, rcvd);
4661 return 1;
4662 }
4663#endif /* COAP_Q_BLOCK_SUPPORT */
4664 if (length > block.chunk_size) {
4665 coap_log_debug("block: Oversized packet - reduced to %"PRIu32" from %" PRIuS "\n",
4666 block.chunk_size, length);
4667 length = block.chunk_size;
4668 }
4669 if (block.m && length != block.chunk_size) {
4670 coap_log_warn("block: Undersized packet - expected %"PRIu32", got %" PRIuS "\n",
4671 block.chunk_size, length);
4672 /* Unclear how to properly handle this */
4673 rcvd->code = COAP_RESPONSE_CODE(402);
4674 goto expire_lg_crcv;
4675 }
4676 /* Possibility that Size2 not sent, or is too small */
4677 chunk = (size_t)1 << (block.szx + 4);
4678 offset = block.num * chunk;
4679 if (size2 < (offset + length)) {
4680 if (block.m)
4681 size2 = offset + length + 1;
4682 else
4683 size2 = offset + length;
4684 }
4685 saved_offset = offset;
4686
4687 if (lg_crcv->initial) {
4688#if COAP_Q_BLOCK_SUPPORT
4689reinit:
4690#endif /* COAP_Q_BLOCK_SUPPORT */
4691 lg_crcv->initial = 0;
4692 if (lg_crcv->body_data) {
4693 coap_delete_binary(lg_crcv->body_data);
4694 lg_crcv->body_data = NULL;
4695 }
4696 if (etag_opt) {
4697 lg_crcv->etag_length = (uint8_t)min(coap_opt_length(etag_opt), sizeof(lg_crcv->etag));
4698 memcpy(lg_crcv->etag, coap_opt_value(etag_opt), lg_crcv->etag_length);
4699 lg_crcv->etag_set = 1;
4700 } else {
4701 lg_crcv->etag_set = 0;
4702 }
4703 lg_crcv->total_len = size2;
4704 lg_crcv->content_format = fmt;
4705 lg_crcv->szx = block.szx;
4706 lg_crcv->block_option = block_opt;
4707 lg_crcv->last_type = rcvd->type;
4708 lg_crcv->rec_blocks.used = 0;
4709 lg_crcv->rec_blocks.total_blocks = 0;
4710#if COAP_Q_BLOCK_SUPPORT
4711 lg_crcv->rec_blocks.processing_payload_set = 0;
4712#endif /* COAP_Q_BLOCK_SUPPORT */
4713 }
4714 if (lg_crcv->total_len < size2)
4715 lg_crcv->total_len = size2;
4716
4717 /* Check whether we can handle this size */
4718 uint32_t max_body;
4719 uint8_t max_block_szx;
4720
4721 max_block_szx = COAP_BLOCK_MAX_SIZE_GET(session->block_mode);
4722 if (max_block_szx == 0 || max_block_szx > block.szx) {
4723 max_block_szx = block.szx;
4724 }
4725 max_body = ((1UL << 20) * (1 << (max_block_szx + 4)));
4726 if (max_body > MAX_BLK_LEN)
4727 max_body = MAX_BLK_LEN;
4728 if ((context->max_body_size && size2 > context->max_body_size) ||
4729 (size2 > max_body)) {
4730 size_t max_body_size = context->max_body_size;
4731
4732 if (max_body_size == 0 || max_body < max_body_size) {
4733 max_body_size = max_body;
4734 }
4735 coap_log_warn("Unable to handle body size %" PRIuS " (max %" PRIuS ")\n", size2, max_body_size);
4736 /* Try to hint to the server there is an issue */
4737 coap_send_rst_lkd(session, rcvd);
4739 return 1;
4740 }
4741
4742 if (etag_opt) {
4743 if (!full_match(coap_opt_value(etag_opt),
4744 coap_opt_length(etag_opt),
4745 lg_crcv->etag, lg_crcv->etag_length)) {
4746 /* body of data has changed - need to restart request */
4747 coap_pdu_t *pdu;
4748 uint64_t token = STATE_TOKEN_FULL(lg_crcv->state_token,
4749 ++lg_crcv->retry_counter);
4750 size_t len = coap_encode_var_safe8(buf, sizeof(token), token);
4751 coap_opt_filter_t drop_options;
4752
4753#if COAP_Q_BLOCK_SUPPORT
4754 if (block_opt == COAP_OPTION_Q_BLOCK2)
4755 goto reinit;
4756#endif /* COAP_Q_BLOCK_SUPPORT */
4757
4758 coap_log_warn("Data body updated during receipt - new request started\n");
4759 if (!(session->block_mode & COAP_BLOCK_SINGLE_BODY))
4761
4762 lg_crcv->initial = 1;
4763 coap_delete_binary(lg_crcv->body_data);
4764 lg_crcv->body_data = NULL;
4765
4766 coap_session_new_token(session, &len, buf);
4767 memset(&drop_options, 0, sizeof(coap_opt_filter_t));
4770 pdu = coap_pdu_duplicate_lkd(lg_crcv->sent_pdu, session, len, buf,
4771 &drop_options, COAP_BOOL_FALSE);
4772 if (!pdu)
4773 goto fail_resp;
4774
4775 coap_update_option(pdu, block_opt,
4776 coap_encode_var_safe(buf, sizeof(buf),
4777 (0 << 4) | (0 << 3) | block.aszx),
4778 buf);
4779
4780 if (coap_send_internal(session, pdu, NULL) == COAP_INVALID_MID)
4781 goto fail_resp;
4782
4783 goto skip_app_handler;
4784 }
4785 } else if (lg_crcv->etag_set) {
4786 /* Cannot handle this change in ETag to not being there */
4787 coap_log_warn("Not all blocks have ETag option\n");
4788 goto fail_resp;
4789 }
4790
4791 if (fmt != lg_crcv->content_format) {
4792 coap_log_warn("Content-Format option mismatch\n");
4793 goto fail_resp;
4794 }
4795#if COAP_Q_BLOCK_SUPPORT
4796 if (block_opt == COAP_OPTION_Q_BLOCK2 && size2 != lg_crcv->total_len) {
4797 coap_log_warn("Size2 option mismatch (%"PRIuS " != %" PRIuS ")\n",
4798 size2, lg_crcv->total_len);
4799 goto fail_resp;
4800 }
4801#endif /* COAP_Q_BLOCK_SUPPORT */
4802 if (block.num == 0) {
4803 coap_opt_t *obs_opt = coap_check_option(rcvd,
4805 &opt_iter);
4806 if (obs_opt) {
4807 lg_crcv->observe_length = min(coap_opt_length(obs_opt), 3);
4808 memcpy(lg_crcv->observe, coap_opt_value(obs_opt), lg_crcv->observe_length);
4809 lg_crcv->observe_set = 1;
4810 } else {
4811 lg_crcv->observe_set = 0;
4812 }
4813 }
4814 updated_block = 0;
4815 while (offset < saved_offset + length) {
4816 if (!check_if_received_block(&lg_crcv->rec_blocks, block.num)) {
4817#if COAP_Q_BLOCK_SUPPORT
4818 uint32_t this_payload_set = block.num / COAP_MAX_PAYLOADS(session);
4819#endif /* COAP_Q_BLOCK_SUPPORT */
4820
4821 coap_log_debug("found Block option, block size is %u, block nr. %u\n",
4822 1 << (block.szx + 4), block.num);
4823 /* Update list of blocks received */
4824 if (blocks_add_entry(&lg_crcv->rec_blocks, block.num, block.m)) {
4825 updated_block = 1;
4826 } else {
4827 coap_log_debug("Block nr %u ignored (too many missing blocks)\n", block.num);
4828 }
4829#if COAP_Q_BLOCK_SUPPORT
4830 if (block_opt == COAP_OPTION_Q_BLOCK2) {
4831 if (lg_crcv->rec_blocks.used > 1 &&
4832 (block.num == lg_crcv->rec_blocks.range[lg_crcv->rec_blocks.used-1].end ||
4833 block.num == lg_crcv->rec_blocks.range[lg_crcv->rec_blocks.used-1].end - 1) &&
4834 lg_crcv->rec_blocks.range[lg_crcv->rec_blocks.used-1].begin <
4835 lg_crcv->rec_blocks.range[lg_crcv->rec_blocks.used-1].end &&
4836 (lg_crcv->rec_blocks.range[lg_crcv->rec_blocks.used-1].end % COAP_MAX_PAYLOADS(session)) + 1
4837 == COAP_MAX_PAYLOADS(session)) {
4838 /*
4839 * At least the last 2 of a PAYLOAD_SET have been received, but some
4840 * missing packets, but have seen the emd of a PAYLOAD_SET.
4841 */
4842 coap_log_debug("Fast recovery for block %u\n", block.num);
4843 coap_request_missing_q_block2(session, lg_crcv);
4844 } else if (lg_crcv->rec_blocks.used &&
4845 this_payload_set > lg_crcv->rec_blocks.processing_payload_set &&
4846 this_payload_set != lg_crcv->rec_blocks.latest_payload_set) {
4847 coap_request_missing_q_block2(session, lg_crcv);
4848 }
4849 }
4850 lg_crcv->rec_blocks.latest_payload_set = this_payload_set;
4851#endif /* COAP_Q_BLOCK_SUPPORT */
4852 } else {
4853 coap_log_debug("Duplicate block nr %u\n", block.num);
4854 }
4855 block.num++;
4856 offset = block.num << (block.szx + 4);
4857 if (!block.bert && block_opt != COAP_OPTION_Q_BLOCK2)
4858 break;
4859 }
4860 block.num--;
4861 /* Only process if not duplicate block */
4862 if (updated_block) {
4863 void *body_free;
4864
4865 /* Update last_used to prevent premature timeout during long transfers */
4866 coap_ticks(&lg_crcv->last_used);
4867
4868 if ((session->block_mode & COAP_SINGLE_BLOCK_OR_Q) || block.bert) {
4869 if (size2 < saved_offset + length) {
4870 size2 = saved_offset + length;
4871 }
4872 if (context && context->block_data_cb) {
4873 coap_response_t resp;
4874
4876 context->block_data_cb(session, rcvd, 0,
4877 &lg_crcv->body_data,
4878 length, data,
4879 saved_offset, size2));
4880 if (resp != COAP_RESPONSE_OK) {
4881 goto fail_resp;
4882 }
4883 } else {
4884 lg_crcv->body_data = coap_block_build_body_lkd(lg_crcv->body_data, length, data,
4885 saved_offset, size2);
4886 if (lg_crcv->body_data == NULL) {
4887 goto fail_resp;
4888 }
4889 }
4890 }
4891 if (block.m || !check_all_blocks_in(&lg_crcv->rec_blocks)) {
4892 /* Not all the payloads of the body have arrived */
4893 size_t len;
4894 coap_pdu_t *pdu;
4895 uint64_t token;
4896 coap_opt_filter_t drop_options;
4897
4898 if (block.m) {
4899#if COAP_Q_BLOCK_SUPPORT
4900 if (block_opt == COAP_OPTION_Q_BLOCK2) {
4901 /* Blocks could arrive in wrong order */
4902 if (check_all_blocks_in(&lg_crcv->rec_blocks)) {
4903 goto give_to_app;
4904 }
4905 if (check_all_blocks_in_for_payload_set(session,
4906 &lg_crcv->rec_blocks)) {
4907 block.num = lg_crcv->rec_blocks.range[0].end;
4908 /* Now requesting next payload */
4909 lg_crcv->rec_blocks.processing_payload_set =
4910 block.num / COAP_MAX_PAYLOADS(session) + 1;
4911 if (check_any_blocks_next_payload_set(session,
4912 &lg_crcv->rec_blocks)) {
4913 /* Need to ask for them individually */
4914 coap_request_missing_q_block2(session, lg_crcv);
4915 goto skip_app_handler;
4916 }
4917 } else {
4918 /* The remote end will be sending the next one unless this
4919 is a MAX_PAYLOADS and all previous have been received */
4920 goto skip_app_handler;
4921 }
4922 if (COAP_PROTO_RELIABLE(session->proto) ||
4923 rcvd->type != COAP_MESSAGE_NON)
4924 goto skip_app_handler;
4925
4926 } else
4927#endif /* COAP_Q_BLOCK_SUPPORT */
4928 block.m = 0;
4929
4930 /* Ask for the next block */
4931 token = STATE_TOKEN_FULL(lg_crcv->state_token, ++lg_crcv->retry_counter);
4932 len = coap_encode_var_safe8(buf, sizeof(token), token);
4933 memset(&drop_options, 0, sizeof(coap_opt_filter_t));
4935 pdu = coap_pdu_duplicate_lkd(lg_crcv->sent_pdu, session, len, buf,
4936 &drop_options, COAP_BOOL_FALSE);
4937 if (!pdu)
4938 goto fail_resp;
4939
4940 if (rcvd->type == COAP_MESSAGE_NON)
4941 pdu->type = COAP_MESSAGE_NON; /* Server is using NON */
4942
4943 /* Only sent with the first block */
4945
4946 coap_update_option(pdu, block_opt,
4947 coap_encode_var_safe(buf, sizeof(buf),
4948 ((block.num + 1) << 4) |
4949 (block.m << 3) | block.aszx),
4950 buf);
4951
4953 (void)coap_get_data(lg_crcv->sent_pdu, &length, &data);
4954 coap_add_data_large_internal(session, NULL, pdu, NULL, NULL, -1, 0, length, data, NULL, NULL, NULL,
4955 0, 0);
4956 }
4957 if (coap_send_internal(session, pdu, NULL) == COAP_INVALID_MID)
4958 /* Session could now be disconnected, so no lg_crcv */
4959 goto skip_app_handler;
4960 }
4961 if ((session->block_mode & COAP_SINGLE_BLOCK_OR_Q) || block.bert)
4962 goto skip_app_handler;
4963
4964 /* need to put back original token into rcvd */
4965 coap_update_token(rcvd, lg_crcv->app_token->length, lg_crcv->app_token->s);
4966 rcvd->body_offset = saved_offset;
4967#if COAP_Q_BLOCK_SUPPORT
4968 rcvd->body_total = block_opt == COAP_OPTION_Q_BLOCK2 ?
4969 lg_crcv->total_len : size2;
4970#else /* ! COAP_Q_BLOCK_SUPPORT */
4971 rcvd->body_total = size2;
4972#endif /* ! COAP_Q_BLOCK_SUPPORT */
4973 coap_log_debug("Client app version of updated PDU (2)\n");
4975
4976 if (sent) {
4977 /* need to put back original token into sent */
4978 if (lg_crcv->app_token)
4979 coap_update_token(sent, lg_crcv->app_token->length,
4980 lg_crcv->app_token->s);
4981 coap_remove_option(sent, lg_crcv->block_option);
4982 }
4983 goto call_app_handler;
4984 }
4985#if COAP_Q_BLOCK_SUPPORT
4986give_to_app:
4987#endif /* COAP_Q_BLOCK_SUPPORT */
4988 if ((session->block_mode & COAP_SINGLE_BLOCK_OR_Q) || block.bert) {
4989 /* Pretend that there is no block */
4990 coap_remove_option(rcvd, block_opt);
4991 if (lg_crcv->observe_set) {
4993 lg_crcv->observe_length, lg_crcv->observe);
4994 }
4995 rcvd->body_data = lg_crcv->body_data ? lg_crcv->body_data->s : NULL;
4996#if COAP_Q_BLOCK_SUPPORT
4997 if (context && context->block_data_cb) {
4998 /* Data has already been provided - do not duplicate */
4999 if (rcvd->data) {
5000 rcvd->used_size = rcvd->data - rcvd->token - 1;
5001 rcvd->data = NULL;
5002 }
5003 }
5004 rcvd->body_length = block_opt == COAP_OPTION_Q_BLOCK2 ?
5005 lg_crcv->total_len : saved_offset + length;
5006#else /* ! COAP_Q_BLOCK_SUPPORT */
5007 rcvd->body_length = saved_offset + length;
5008#endif /* ! COAP_Q_BLOCK_SUPPORT */
5009 rcvd->body_offset = 0;
5010 rcvd->body_total = rcvd->body_length;
5011 } else {
5012 rcvd->body_offset = saved_offset;
5013#if COAP_Q_BLOCK_SUPPORT
5014 rcvd->body_total = block_opt == COAP_OPTION_Q_BLOCK2 ?
5015 lg_crcv->total_len : size2;
5016#else /* ! COAP_Q_BLOCK_SUPPORT */
5017 rcvd->body_total = size2;
5018#endif /* ! COAP_Q_BLOCK_SUPPORT */
5019 }
5020 /* need to put back original token into rcvd */
5021 if (!coap_binary_equal(&rcvd->actual_token, lg_crcv->app_token)) {
5022 coap_update_token(rcvd, lg_crcv->app_token->length, lg_crcv->app_token->s);
5023 coap_log_debug("Client app version of updated PDU (3)\n");
5025 }
5026 if (sent) {
5027 /* need to put back original token into sent */
5028 if (lg_crcv->app_token)
5029 coap_update_token(sent, lg_crcv->app_token->length,
5030 lg_crcv->app_token->s);
5031 coap_remove_option(sent, lg_crcv->block_option);
5032 }
5033 body_free = lg_crcv->body_data;
5034 lg_crcv->body_data = NULL;
5035 coap_call_response_handler(session, sent, rcvd, body_free);
5036
5037 ack_rst_sent = 1;
5038 if (lg_crcv->observe_set == 0) {
5039 /* Expire this entry */
5040 LL_DELETE(session->lg_crcv, lg_crcv);
5041 coap_block_delete_lg_crcv(session, lg_crcv);
5042 goto skip_app_handler;
5043 }
5044 /* Set up for the next data body as observing */
5045 lg_crcv->initial = 1;
5046 }
5047 coap_ticks(&lg_crcv->last_used);
5048 goto skip_app_handler;
5049 } else {
5050 coap_opt_t *obs_opt = coap_check_option(rcvd,
5052 &opt_iter);
5053 if (context->max_body_size && length > context->max_body_size) {
5054 coap_log_warn("Unable to handle body size %" PRIuS " (max %" PRIuS ")\n", length,
5055 context->max_body_size);
5056 /* Try to hint to the server there is an issue */
5057 coap_send_rst_lkd(session, rcvd);
5059 return 1;
5060 }
5061 if (obs_opt) {
5062 lg_crcv->observe_length = min(coap_opt_length(obs_opt), 3);
5063 memcpy(lg_crcv->observe, coap_opt_value(obs_opt), lg_crcv->observe_length);
5064 lg_crcv->observe_set = 1;
5065 } else {
5066 lg_crcv->observe_set = 0;
5067 if (!coap_binary_equal(&rcvd->actual_token, lg_crcv->app_token)) {
5068 /* need to put back original token into rcvd */
5069 coap_update_token(rcvd, lg_crcv->app_token->length, lg_crcv->app_token->s);
5071 coap_log_debug("PDU presented to app.\n");
5073 }
5074 /* Expire this entry */
5075 goto expire_lg_crcv;
5076 }
5077 }
5078 coap_ticks(&lg_crcv->last_used);
5079 } else if (rcvd->code == COAP_RESPONSE_CODE(401)) {
5080#if COAP_OSCORE_SUPPORT
5081 if (check_freshness(session, rcvd,
5082 (session->oscore_encryption == 0) ? sent : NULL,
5083 NULL, lg_crcv))
5084#else /* !COAP_OSCORE_SUPPORT */
5085 if (check_freshness(session, rcvd, sent, NULL, lg_crcv))
5086#endif /* !COAP_OSCORE_SUPPORT */
5087 goto skip_app_handler;
5088 goto expire_lg_crcv;
5089 } else if (rcvd->code == COAP_RESPONSE_CODE(402)) {
5090 coap_opt_t *abb_opt = sent ? coap_check_option(sent,
5092 &opt_iter) : NULL;
5093 if (abb_opt) {
5094 /* Send the request again with the Uri-Path-Abbrev expanded out */
5095 coap_pdu_t *pdu;
5096 size_t data_len;
5097 const uint8_t *data;
5098 uint64_t token;
5099 uint8_t ltoken[8];
5100 size_t ltoken_len;
5101
5102 session->no_path_abbrev = 1;
5103 token = STATE_TOKEN_FULL(lg_crcv->state_token,
5104 ++lg_crcv->retry_counter);
5105 ltoken_len = coap_encode_var_safe8(ltoken, sizeof(token), token);
5106 pdu = coap_pdu_duplicate_lkd(lg_crcv->sent_pdu, session, ltoken_len,
5107 ltoken, NULL, COAP_BOOL_TRUE);
5108 if (pdu) {
5109 if (coap_get_data(lg_crcv->sent_pdu, &data_len, &data)) {
5110 coap_add_data(pdu, data_len, data);
5111 }
5112#if COAP_PROXY_SUPPORT
5113 coap_proxy_req_t *proxy_req = session->context->proxy_list_count ?
5114 coap_proxy_map_outgoing_request(session, rcvd, NULL) : NULL;
5115
5116 if (proxy_req) {
5117 coap_bin_const_t *new = coap_new_bin_const(ltoken, ltoken_len);
5118 if (new) {
5119 coap_delete_bin_const(proxy_req->token_used);
5120 proxy_req->token_used = new;
5121 coap_proxy_log_entry(proxy_req->incoming, proxy_req->pdu, proxy_req->token_used, "upd");
5122 }
5123 }
5124#endif /* COAP_PROXY_SUPPORT */
5125 coap_log_debug("* Retransmitting PDU with Uri-Path-Abbrev replaced (1)\n");
5126 coap_delete_pdu_lkd(lg_crcv->sent_pdu);
5127 lg_crcv->sent_pdu = coap_pdu_reference_lkd(pdu);
5128 coap_send_internal(session, pdu, NULL);
5129 goto skip_app_handler;
5130 }
5131 }
5132 goto expire_lg_crcv;
5133 } else {
5134 /* Not 2.xx, 4.01 or 4.02 - assume it is a failure of some sort */
5135 goto expire_lg_crcv;
5136 }
5137 if (!block.m && !lg_crcv->observe_set) {
5138fail_resp:
5139
5140 /* lg_crcv no longer required - cache it for 1 sec */
5143 } else {
5144 adjust = 0;
5145 }
5146 coap_ticks(&lg_crcv->last_used);
5147 lg_crcv->last_used -= adjust;
5148 }
5149 /* need to put back original token into rcvd */
5150 if (!coap_binary_equal(&rcvd->actual_token, lg_crcv->app_token)) {
5151 coap_update_token(rcvd, lg_crcv->app_token->length, lg_crcv->app_token->s);
5152 coap_log_debug("Client app version of updated PDU (4)\n");
5154 }
5155 }
5156
5157 /* Check if receiving a block response and if blocks can be set up */
5158 if (recursive == COAP_RECURSE_OK && !lg_crcv) {
5159 if (!sent) {
5160 if (coap_get_block_b(session, rcvd, COAP_OPTION_BLOCK2, &block)
5161#if COAP_Q_BLOCK_SUPPORT
5162 ||
5163 coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK2, &block)
5164#endif /* COAP_Q_BLOCK_SUPPORT */
5165 ) {
5166 coap_log_debug("** %s: large body receive internal issue\n",
5167 coap_session_str(session));
5168 goto skip_app_handler;
5169 }
5170 } else if (COAP_RESPONSE_CLASS(rcvd->code) == 2) {
5171 const uint8_t *data;
5172 size_t length;
5173
5174 if (coap_get_block_b(session, rcvd, COAP_OPTION_BLOCK2, &block)) {
5175#if COAP_Q_BLOCK_SUPPORT
5176 if (session->block_mode & COAP_BLOCK_PROBE_Q_BLOCK) {
5177 set_block_mode_drop_q(session->block_mode);
5178 coap_log_debug("Q-Block support disabled\n");
5179 }
5180#endif /* COAP_Q_BLOCK_SUPPORT */
5181 have_block = 1;
5182 if (block.num != 0) {
5183 /* Assume random access and just give the single response to app */
5184 size_t chunk = (size_t)1 << (block.szx + 4);
5185
5186 coap_get_data(rcvd, &length, &data);
5187 rcvd->body_offset = block.num*chunk;
5188 rcvd->body_total = block.num*chunk + length + (block.m ? 1 : 0);
5189 goto call_app_handler;
5190 }
5191 }
5192#if COAP_Q_BLOCK_SUPPORT
5193 else if (coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK2, &block)) {
5194 have_block = 1;
5195 /* server indicating that it supports Q_BLOCK2 */
5196 if (!(session->block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
5197 set_block_mode_has_q(session->block_mode);
5198 }
5199 }
5200#endif /* COAP_Q_BLOCK_SUPPORT */
5201 if (have_block) {
5202 lg_crcv = coap_block_new_lg_crcv(session, sent, NULL);
5203
5204 if (lg_crcv) {
5205 LL_PREPEND(session->lg_crcv, lg_crcv);
5206 return coap_handle_response_get_block(context, session, sent, rcvd,
5208 }
5209 }
5210 coap_get_data(rcvd, &length, &data);
5211 if (context->max_body_size && length > context->max_body_size) {
5212 coap_log_warn("Unable to handle body size %" PRIuS " (max %" PRIuS ")\n", length,
5213 context->max_body_size);
5214 /* Try to hint to the server there is an issue */
5215 coap_send_rst_lkd(session, rcvd);
5217 return 1;
5218 }
5219 track_echo(session, rcvd);
5220 } else if (rcvd->code == COAP_RESPONSE_CODE(401)) {
5221 lg_crcv = coap_block_new_lg_crcv(session, sent, NULL);
5222
5223 if (lg_crcv) {
5224 LL_PREPEND(session->lg_crcv, lg_crcv);
5225 return coap_handle_response_get_block(context, session, sent, rcvd,
5227 }
5228 } else if (rcvd->code == COAP_RESPONSE_CODE(402)) {
5229 coap_opt_t *abb_opt = coap_check_option(sent,
5231 &opt_iter);
5232 if (abb_opt) {
5233 /*
5234 * Send the request again with the Uri-Path-Abbrev expanded out, but need
5235 lg_crcv in place to handle the token update.
5236 */
5237 lg_crcv = coap_block_new_lg_crcv(session, sent, NULL);
5238
5239 if (lg_crcv) {
5240 LL_PREPEND(session->lg_crcv, lg_crcv);
5241 return coap_handle_response_get_block(context, session, sent, rcvd,
5243 }
5244 }
5245 }
5246 }
5247 return 0;
5248
5249expire_lg_crcv:
5250 /* need to put back original token into rcvd */
5251 if (!coap_binary_equal(&rcvd->actual_token, lg_crcv->app_token)) {
5252 coap_update_token(rcvd, lg_crcv->app_token->length, lg_crcv->app_token->s);
5253 coap_log_debug("Client app version of updated PDU (5)\n");
5255 }
5256
5257 if (sent) {
5258 /* need to put back original token into sent */
5259 if (lg_crcv->app_token)
5260 coap_update_token(sent, lg_crcv->app_token->length,
5261 lg_crcv->app_token->s);
5262 coap_remove_option(sent, lg_crcv->block_option);
5263 }
5264 /* Expire this entry */
5265 LL_DELETE(session->lg_crcv, lg_crcv);
5266 coap_block_delete_lg_crcv(session, lg_crcv);
5267
5268call_app_handler:
5269 return 0;
5270
5271skip_app_handler:
5272 if (!ack_rst_sent)
5273 coap_send_ack_lkd(session, rcvd);
5274 return 1;
5275}
5276#endif /* COAP_CLIENT_SUPPORT */
5277
5278#if COAP_SERVER_SUPPORT
5279/* Check if lg_xmit generated and update PDU code if so */
5280void
5282 const coap_pdu_t *request,
5283 coap_pdu_t *response, const coap_resource_t *resource,
5284 const coap_string_t *query) {
5285 coap_lg_xmit_t *lg_xmit;
5286
5287 if (response->code == 0)
5288 return;
5289 lg_xmit = coap_find_lg_xmit_response(session, request, resource, query);
5290 if (lg_xmit && lg_xmit->sent_pdu && lg_xmit->sent_pdu->code == 0) {
5291 lg_xmit->sent_pdu->code = response->code;
5292 return;
5293 }
5294}
5295#endif /* COAP_SERVER_SUPPORT */
5296
5297#if COAP_CLIENT_SUPPORT
5298void
5300 uint64_t token_match =
5302 pdu->actual_token.length));
5303 coap_lg_xmit_t *lg_xmit;
5304 coap_lg_crcv_t *lg_crcv;
5305
5306 if (session->lg_crcv) {
5307 LL_FOREACH(session->lg_crcv, lg_crcv) {
5308 if (coap_binary_equal(&pdu->actual_token, lg_crcv->app_token))
5309 return;
5310 if (token_match == STATE_TOKEN_BASE(lg_crcv->state_token)) {
5311 coap_update_token(pdu, lg_crcv->app_token->length,
5312 lg_crcv->app_token->s);
5313 coap_log_debug("Client app version of updated PDU (6)\n");
5315 return;
5316 }
5317 }
5318 }
5319 if (COAP_PDU_IS_REQUEST(pdu) && session->lg_xmit) {
5320 LL_FOREACH(session->lg_xmit, lg_xmit) {
5321 if (coap_binary_equal(&pdu->actual_token, lg_xmit->b.b1.app_token))
5322 return;
5323 if (token_match == STATE_TOKEN_BASE(lg_xmit->b.b1.state_token)) {
5324 coap_update_token(pdu, lg_xmit->b.b1.app_token->length,
5325 lg_xmit->b.b1.app_token->s);
5326 coap_log_debug("Client app version of updated PDU (7)\n");
5328 return;
5329 }
5330 }
5331 }
5332}
5333#endif /* ! COAP_CLIENT_SUPPORT */
int coap_is_mcast(const coap_address_t *a)
Checks if given address a denotes a multicast address.
void coap_address_copy(coap_address_t *dst, const coap_address_t *src)
int coap_address_equals(const coap_address_t *a, const coap_address_t *b)
Compares given address objects a and b.
static void coap_block_release_lg_xmit_data(coap_session_t *session, coap_lg_xmit_data_t *data_info)
#define COAP_ETAG_MAX_BYTES
Definition coap_block.c:26
#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, coap_get_large_data_t get_func, void *app_ptr, int single_request, coap_pdu_code_t request_method)
Definition coap_block.c:803
static int blocks_add_entry(coap_rblock_t *rec_blocks, uint32_t block_num, uint32_t block_m)
static int check_all_blocks_in(coap_rblock_t *rec_blocks)
static int 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:141
#define min(a, b)
Definition coap_block.c:21
#define COAP_LG_XMIT_TXT_SCALAR
Definition coap_block.c:32
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
struct coap_lg_crcv_t coap_lg_crcv_t
struct coap_cache_key_t coap_cache_key_t
struct coap_resource_t coap_resource_t
struct coap_lg_srcv_t coap_lg_srcv_t
#define PRIuS
#define PRIu32
@ COAP_NACK_TOO_MANY_RETRIES
Definition coap_io.h:65
#define COAP_SOCKET_MULTICAST
socket is used for multicast communication
Library specific build wrapper for coap_internal.h.
#define COAP_API
@ COAP_LG_XMIT
Definition coap_mem.h:49
@ COAP_LG_CRCV
Definition coap_mem.h:50
@ COAP_LG_SRCV
Definition coap_mem.h:51
@ COAP_STRING
Definition coap_mem.h:33
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().
uint8_t coap_unique_id[8]
Definition coap_net.c:5558
#define NULL
Definition coap_option.h:30
uint16_t coap_option_num_t
Definition coap_option.h:37
uint8_t coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
#define COAP_OPTION_CONTENT_TYPE
@ COAP_OPTION_OBSERVE
Definition coap_option.h:75
@ COAP_OPTION_ETAG
Definition coap_option.h:73
@ COAP_OPTION_MAXAGE
Definition coap_option.h:83
@ COAP_OPTION_SIZE2
Definition coap_option.h:92
@ COAP_OPTION_Q_BLOCK2
Definition coap_option.h:93
@ COAP_OPTION_BLOCK2
Definition coap_option.h:90
@ COAP_OPTION_SIZE1
Definition coap_option.h:96
@ COAP_OPTION_ECHO
Definition coap_option.h:97
@ COAP_OPTION_RTAG
Definition coap_option.h:99
@ COAP_OPTION_BLOCK1
Definition coap_option.h:91
@ COAP_OPTION_URI_PATH
Definition coap_option.h:79
@ COAP_OPTION_Q_BLOCK1
Definition coap_option.h:87
@ COAP_OPTION_OSCORE
Definition coap_option.h:78
@ COAP_OPTION_CONTENT_FORMAT
Definition coap_option.h:80
@ COAP_OPTION_URI_PATH_ABB
Definition coap_option.h:81
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:1222
void coap_call_response_handler(coap_session_t *session, coap_pdu_t *sent, coap_pdu_t *rcvd, void *body_free)
coap_mid_t coap_send_ack_lkd(coap_session_t *session, const coap_pdu_t *request)
Sends an ACK message with code 0 for the specified request to dst.
Definition coap_net.c:1239
#define COAP_BLOCK_MAX_SIZE_MASK
#define coap_check_update_token(a, b)
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:455
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:430
#define COAP_BLOCK_MAX_SIZE_SET(a)
#define COAP_RBLOCK_CNT
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...
COAP_STATIC_INLINE void coap_lg_xmit_reference_lkd(coap_lg_xmit_t *lg_xmit)
Increment reference counter on a lg_xmit.
void coap_block_delete_lg_xmit(coap_session_t *session, coap_lg_xmit_t *lg_xmit)
Remove a lg_xmit.
#define STATE_TOKEN_FULL(t, r)
#define COAP_SINGLE_BLOCK_OR_Q
#define STATE_TOKEN_BASE(t)
coap_binary_t * coap_block_build_body_lkd(coap_binary_t *body_data, size_t length, const uint8_t *data, size_t offset, size_t total)
Re-assemble payloads into a body.
#define COAP_BLOCK_SET_MASK
COAP_STATIC_INLINE void coap_lg_xmit_release_lkd(coap_session_t *session, coap_lg_xmit_t *lg_xmit)
Decrement reference counter on a lg_xmit.
coap_lg_xmit_t * coap_find_lg_xmit(coap_session_t *session, coap_pdu_t *pdu)
Find the current lg_xmit for the session that matches the pdu.
Definition coap_block.c:487
#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:422
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.
int(* coap_get_large_data_t)(coap_session_t *session, size_t max, size_t offset, uint8_t *data, size_t *length, void *app_ptr)
Callback handler for getting the data based on app_ptr provided to coap_add_data_large_request_app() ...
Definition coap_block.h:360
#define COAP_BLOCK_USE_M_Q_BLOCK
Definition coap_block.h:68
#define COAP_OPT_BLOCK_SZX(opt)
Returns the value of the SZX-field of a Block option opt.
Definition coap_block.h:94
#define COAP_BLOCK_STLESS_BLOCK2
Definition coap_block.h:71
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:67
#define COAP_BLOCK_STLESS_FETCH
Definition coap_block.h:70
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:444
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:261
#define COAP_BLOCK_SINGLE_BODY
Definition coap_block.h:66
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:216
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:247
COAP_API coap_binary_t * coap_block_build_body(coap_binary_t *body_data, size_t length, const uint8_t *data, size_t offset, size_t total)
Re-assemble payloads into a body.
void(* coap_release_large_data_t)(coap_session_t *session, void *app_ptr)
Callback handler for de-allocating the data based on app_ptr provided to coap_add_data_large_*() func...
Definition coap_block.h:291
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:286
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:71
#define COAP_OPT_BLOCK_MORE(opt)
Returns the value of the More-bit of a Block option opt.
Definition coap_block.h:90
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:52
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:124
#define COAP_BLOCK_NOT_RANDOM_BLOCK1
Definition coap_block.h:72
#define COAP_OPT_BLOCK_END_BYTE(opt)
Returns the value of the last byte of opt.
Definition coap_block.h:85
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:183
COAP_API int coap_add_data_large_request_app(coap_session_t *session, coap_pdu_t *pdu, size_t length, coap_release_large_data_t release_func, coap_get_large_data_t get_func, void *app_ptr)
Associates given data callback with the pdu that is passed as second parameter.
#define COAP_BLOCK_FORCE_Q_BLOCK
Definition coap_block.h:73
#define COAP_BLOCK_USE_LIBCOAP
Definition coap_block.h:65
void coap_delete_cache_key(coap_cache_key_t *cache_key)
Delete the cache-key.
coap_cache_key_t * coap_cache_derive_key_w_ignore(const coap_session_t *session, const coap_pdu_t *pdu, coap_cache_session_based_t session_based, const uint16_t *ignore_options, size_t ignore_count)
Calculates a cache-key for the given CoAP PDU.
@ COAP_CACHE_IS_SESSION_BASED_NO_DATA
Definition coap_cache.h:42
time_t coap_time_t
CoAP time in seconds since epoch.
Definition coap_time.h:154
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition coap_time.h:149
coap_time_t coap_ticks_to_rt(coap_tick_t t)
Helper function that converts coap ticks to wallclock time.
Definition coap_time.c:123
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition coap_time.h:164
#define COAP_MAX_DELAY_TICKS
Definition coap_time.h:231
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:5396
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:1497
coap_mid_t coap_send_internal(coap_session_t *session, coap_pdu_t *pdu, coap_pdu_t *request_pdu)
Sends a CoAP message to given peer.
Definition coap_net.c:2116
void coap_register_block_data_handler(coap_context_t *context, coap_block_data_handler_t block_data_handler)
Sets up a handler that is called for each received block during a block-wise transfer when COAP_BLOCK...
coap_response_t
Definition coap_net.h:51
coap_response_t(* coap_block_data_handler_t)(coap_session_t *session, coap_pdu_t *pdu, coap_resource_t *resource, coap_binary_t **body_data, size_t length, const uint8_t *data, size_t offset, size_t total)
Definition of the block data handler function.
Definition coap_net.h:133
void coap_ticks(coap_tick_t *t)
Returns the current value of an internal tick counter.
Definition coap_time.c:90
@ COAP_RESPONSE_OK
Response is fine.
Definition coap_net.h:53
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:71
unsigned int coap_encode_var_safe8(uint8_t *buf, size_t length, uint64_t val)
Encodes multiple-length byte sequences.
Definition coap_encode.c:81
@ COAP_EVENT_BLOCK_ISSUE
Triggered when a block transfer could not be handled.
Definition coap_event.h:77
@ COAP_EVENT_PARTIAL_BLOCK
Triggered when not all of a large body has been received.
Definition coap_event.h:73
@ COAP_EVENT_XMIT_BLOCK_FAIL
Triggered when not all of a large body has been transmitted.
Definition coap_event.h:75
#define coap_lock_callback(func)
Dummy for no thread-safe code.
#define coap_lock_callback_ret(r, func)
Dummy for no thread-safe code.
#define coap_lock_unlock()
Dummy for no thread-safe code.
#define coap_lock_check_locked()
Dummy for no thread-safe code.
#define coap_lock_lock(failed)
Dummy for no thread-safe code.
#define coap_log_debug(...)
Definition coap_debug.h:126
void coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu)
Display the contents of the specified pdu.
Definition coap_debug.c:813
const char * coap_session_str(const coap_session_t *session)
Get session description.
#define coap_log_info(...)
Definition coap_debug.h:114
#define coap_log_warn(...)
Definition coap_debug.h:108
@ COAP_LOG_DEBUG
Definition coap_debug.h:64
#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)
coap_pdu_t * coap_pdu_reference_lkd(coap_pdu_t *pdu)
Increment reference counter on a pdu to stop it prematurely getting freed off when coap_delete_pdu() ...
Definition coap_pdu.c:1760
void coap_delete_pdu_lkd(coap_pdu_t *pdu)
Dispose of an CoAP PDU and free off associated storage.
Definition coap_pdu.c:197
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:696
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:550
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:474
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, coap_bool_t expand_opt_abb)
Duplicate an existing PDU.
Definition coap_pdu.c:237
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:801
#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:395
#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:863
const char * coap_response_phrase(unsigned char code)
Returns a human-readable response phrase for the specified CoAP response code.
Definition coap_pdu.c:1041
#define COAP_MEDIATYPE_APPLICATION_MB_CBOR_SEQ
Definition coap_pdu.h:175
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition coap_pdu.h:184
#define COAP_RESPONSE_CODE(N)
Definition coap_pdu.h:96
#define COAP_RESPONSE_CLASS(C)
Definition coap_pdu.h:99
coap_pdu_code_t
Set of codes available for a PDU.
Definition coap_pdu.h:248
coap_pdu_type_t
CoAP PDU message type definitions.
Definition coap_pdu.h:70
#define COAP_MEDIATYPE_TEXT_PLAIN
Definition coap_pdu.h:134
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:417
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:853
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:966
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:104
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:974
#define COAP_INVALID_MID
Indicates an invalid message id.
Definition coap_pdu.h:187
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:935
@ COAP_BOOL_FALSE
Definition coap_pdu.h:295
@ COAP_BOOL_TRUE
Definition coap_pdu.h:296
@ COAP_REQUEST_CODE_GET
Definition coap_pdu.h:251
@ COAP_REQUEST_CODE_FETCH
Definition coap_pdu.h:255
@ COAP_MESSAGE_NON
Definition coap_pdu.h:72
@ COAP_MESSAGE_ACK
Definition coap_pdu.h:73
@ COAP_MESSAGE_CON
Definition coap_pdu.h:71
#define COAP_NON_RECEIVE_TIMEOUT_TICKS(s)
The NON_RECEIVE_TIMEOUT definition for the session (s).
#define COAP_NON_TIMEOUT_TICKS(s)
void coap_handle_nack(coap_session_t *session, coap_pdu_t *sent, const coap_nack_reason_t reason, const coap_mid_t mid)
#define COAP_MAX_TRANSMIT_WAIT_TICKS(s)
#define COAP_NON_PARTIAL_TIMEOUT_TICKS(s)
The NON_PARTIAL_TIMEOUT definition for the session (s).
coap_tick_t coap_get_non_timeout_random_ticks(coap_session_t *session)
#define COAP_NSTART(s)
#define COAP_MAX_PAYLOADS(s)
size_t coap_session_max_pdu_size_lkd(const coap_session_t *session)
Get maximum acceptable PDU size.
#define COAP_NON_MAX_RETRANSMIT(s)
#define COAP_PROTO_NOT_RELIABLE(p)
#define COAP_PROTO_RELIABLE(p)
void coap_session_new_token(coap_session_t *session, size_t *len, uint8_t *data)
Creates a new token for use.
@ COAP_SESSION_TYPE_CLIENT
client-side
void coap_delete_bin_const(coap_bin_const_t *s)
Deletes the given const binary data and releases any memory allocated.
Definition coap_str.c:130
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:81
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:119
coap_binary_t * coap_resize_binary(coap_binary_t *s, size_t size)
Resizes the given coap_binary_t object.
Definition coap_str.c:86
void coap_delete_binary(coap_binary_t *s)
Deletes the given coap_binary_t object and releases any memory allocated.
Definition coap_str.c:114
#define coap_binary_equal(binary1, binary2)
Compares the two binary data for equality.
Definition coap_str.h:222
#define coap_string_equal(string1, string2)
Compares the two strings for equality.
Definition coap_str.h:208
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
void coap_delete_string(coap_string_t *s)
Deletes the given string and releases any memory allocated.
Definition coap_str.c:50
int coap_q_block_is_supported(void)
Check whether Q-BlockX is available.
Definition coap_block.c:46
#define COAP_STATIC_INLINE
Definition libcoap.h:57
coap_address_t remote
remote address and port
Definition coap_io.h:58
coap_address_t local
local address and port
Definition coap_io.h:59
CoAP binary data definition with const data.
Definition coap_str.h:65
size_t length
length of binary data
Definition coap_str.h:66
const uint8_t * s
read-only binary data
Definition coap_str.h:67
CoAP binary data definition.
Definition coap_str.h:57
size_t length
length of binary data
Definition coap_str.h:58
uint8_t * s
binary data
Definition coap_str.h:59
Structure of Block options with BERT support.
Definition coap_block.h:55
unsigned int num
block number
Definition coap_block.h:56
uint32_t chunk_size
Definition coap_block.h:62
unsigned int bert
Operating as BERT.
Definition coap_block.h:61
unsigned int aszx
block size (0-7 including BERT
Definition coap_block.h:59
unsigned int defined
Set if block found.
Definition coap_block.h:60
unsigned int m
1 if more blocks follow, 0 otherwise
Definition coap_block.h:57
unsigned int szx
block size (0-6)
Definition coap_block.h:58
Structure of Block options.
Definition coap_block.h:46
unsigned int num
block number
Definition coap_block.h:47
unsigned int szx
block size
Definition coap_block.h:49
unsigned int m
1 if more blocks follow, 0 otherwise
Definition coap_block.h:48
The CoAP stack's global state is stored in a coap_context_t object.
coap_block_data_handler_t block_data_cb
Called with each block data during block transfers.
size_t max_body_size
Max supported body size or 0 is unlimited.
uint32_t block_mode
Zero or more COAP_BLOCK_ or'd options.
uint64_t state_token
state token
size_t bert_size
size of last BERT block
coap_address_t upstream
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.
coap_get_large_data_t get_func
Where to get data id needed.
void * app_ptr
application provided ptr for de-alloc function
uint32_t ref
Reference count.
const uint8_t * data
large data ptr
size_t length
large data length
coap_release_large_data_t release_func
large data de-alloc function
Structure to hold large body (many blocks) transmission information.
coap_tick_t last_all_sent
Last time all data sent or 0.
uint8_t blk_size
large block transmission size
union coap_lg_xmit_t::@133113107100342355167305061112145335023127201276 b
coap_tick_t last_sent
Last time any data sent.
coap_lg_xmit_data_t * data_info
Pointer to large data information.
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.
coap_pdu_t * sent_pdu
The sent pdu with all the data.
coap_l_block1_t b1
coap_l_block2_t b2
uint32_t ref
Reference count.
uint16_t option
large block transmission CoAP option
struct coap_lg_xmit_t * next
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 * 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
size_t used_size
used bytes of storage for token, options and payload
coap_session_t * session
Session responsible for PDU or NULL.
size_t body_total
Holds body data total size.
coap_pdu_type_t type
message type
Queue entry.
Structure to keep track of received blocks.
uint32_t total_blocks
Set to block no + 1 when More bit unset.
uint32_t used
Number of range blocks in use.
struct coap_lg_range range[COAP_RBLOCK_CNT]
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
coap_lg_xmit_t * lg_xmit
list of large transmissions
uint32_t block_mode
Zero or more COAP_BLOCK_ or'd options.
coap_socket_t sock
socket object for the session, if any
uint8_t csm_bert_rem_support
CSM TCP BERT blocks supported (remote).
uint64_t tx_token
Next token number to use.
uint8_t is_rate_limiting
Currently NON rate limiting.
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_addr_tuple_t addr_info
remote/local address info
coap_proto_t proto
protocol used
coap_bin_const_t * last_token
uint8_t no_path_abbrev
Set is remote does not support Uri-Path-Abbrev.
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_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_socket_flags_t flags
1 or more of COAP_SOCKET* flag values
CoAP string data definition.
Definition coap_str.h:39
uint8_t * s
string data
Definition coap_str.h:41
size_t length
length of string
Definition coap_str.h:40