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