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