libcoap 4.3.2rc2
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
1634/*
1635 * pdu is always released before return IF COAP_SEND_INC_PDU
1636 */
1638coap_send_q_blocks(coap_session_t *session,
1639 coap_lg_xmit_t *lg_xmit,
1640 coap_block_b_t block,
1641 coap_pdu_t *pdu,
1642 coap_send_pdu_t send_pdu) {
1643 coap_pdu_t *block_pdu = NULL;
1644 coap_opt_filter_t drop_options;
1646 uint64_t token = coap_decode_var_bytes8(pdu->actual_token.s,
1647 pdu->actual_token.length);
1648 const uint8_t *ptoken;
1649 uint8_t ltoken[8];
1650 size_t ltoken_length;
1651 uint32_t delayqueue_cnt = 0;
1652
1653 if (!lg_xmit) {
1654 if (send_pdu == COAP_SEND_INC_PDU)
1655 return coap_send_internal(session, pdu);
1656 return COAP_INVALID_MID;
1657 }
1658
1659 if (pdu->type == COAP_MESSAGE_CON) {
1660 coap_queue_t *delayqueue;
1661
1662 delayqueue_cnt = session->con_active +
1663 (send_pdu == COAP_SEND_INC_PDU ? 1 : 0);
1664 LL_FOREACH(session->delayqueue, delayqueue) {
1665 delayqueue_cnt++;
1666 }
1667 }
1668 pdu->lg_xmit = lg_xmit;
1669 if (block.m &&
1670 ((pdu->type == COAP_MESSAGE_NON &&
1671 ((block.num + 1) % COAP_MAX_PAYLOADS(session)) + 1 !=
1672 COAP_MAX_PAYLOADS(session)) ||
1673 (pdu->type == COAP_MESSAGE_ACK &&
1674 lg_xmit->option == COAP_OPTION_Q_BLOCK2) ||
1675 (pdu->type == COAP_MESSAGE_CON &&
1676 delayqueue_cnt < COAP_NSTART(session)) ||
1677 COAP_PROTO_RELIABLE(session->proto))) {
1678 /* Allocate next pdu if there is headroom */
1679 if (COAP_PDU_IS_RESPONSE(pdu)) {
1680 ptoken = pdu->actual_token.s;
1681 ltoken_length = pdu->actual_token.length;
1682 } else {
1683 token = STATE_TOKEN_FULL(lg_xmit->b.b1.state_token,++lg_xmit->b.b1.count);
1684 ltoken_length = coap_encode_var_safe8(ltoken, sizeof(token), token);
1685 ptoken = ltoken;
1686 }
1687
1688 memset(&drop_options, 0, sizeof(coap_opt_filter_t));
1689 coap_option_filter_set(&drop_options, lg_xmit->option);
1690 block_pdu = coap_pdu_duplicate(pdu, session,
1691 ltoken_length,
1692 ptoken, &drop_options);
1693 if (block_pdu->type == COAP_MESSAGE_ACK)
1694 block_pdu->type = COAP_MESSAGE_CON;
1695 }
1696
1697 /* Send initial pdu (which deletes 'pdu') */
1698 if (send_pdu == COAP_SEND_INC_PDU &&
1699 (mid = coap_send_internal(session, pdu)) == COAP_INVALID_MID) {
1700 /* Not expected, underlying issue somewhere */
1701 coap_delete_pdu(block_pdu);
1702 return COAP_INVALID_MID;
1703 }
1704
1705 while (block_pdu) {
1706 coap_pdu_t *t_pdu = NULL;
1707 uint8_t buf[8];
1708 size_t chunk = ((size_t)1 << (lg_xmit->blk_size + 4));
1709
1710 block.num++;
1711 lg_xmit->offset = block.num * chunk;
1712 block.m = lg_xmit->offset + chunk < lg_xmit->length;
1713 if (block.m && ((block_pdu->type == COAP_MESSAGE_NON &&
1714 (block.num % COAP_MAX_PAYLOADS(session)) + 1 !=
1715 COAP_MAX_PAYLOADS(session)) ||
1716 (block_pdu->type == COAP_MESSAGE_CON &&
1717 delayqueue_cnt + 1 < COAP_NSTART(session)) ||
1718 COAP_PROTO_RELIABLE(session->proto))) {
1719 /*
1720 * Send following block if
1721 * NON and more in MAX_PAYLOADS
1722 * CON and NSTART allows it (based on number in delayqueue)
1723 * Reliable transport
1724 */
1725 if (COAP_PDU_IS_RESPONSE(block_pdu)) {
1726 ptoken = block_pdu->actual_token.s;
1727 ltoken_length = block_pdu->actual_token.length;
1728 } else {
1729 token = STATE_TOKEN_FULL(lg_xmit->b.b1.state_token,++lg_xmit->b.b1.count);
1730 ltoken_length = coap_encode_var_safe8(ltoken, sizeof(token), token);
1731 ptoken = ltoken;
1732 }
1733 t_pdu = coap_pdu_duplicate(block_pdu, session,
1734 ltoken_length, ptoken, &drop_options);
1735 }
1736 if (!coap_update_option(block_pdu, lg_xmit->option,
1738 sizeof(buf),
1739 ((block.num) << 4) |
1740 (block.m << 3) |
1741 block.szx),
1742 buf)) {
1743 coap_log_warn("Internal update issue option\n");
1744 coap_delete_pdu(block_pdu);
1745 coap_delete_pdu(t_pdu);
1746 break;
1747 }
1748
1749 if (!coap_add_block(block_pdu,
1750 lg_xmit->length,
1751 lg_xmit->data,
1752 block.num,
1753 block.szx)) {
1754 coap_log_warn("Internal update issue data\n");
1755 coap_delete_pdu(block_pdu);
1756 coap_delete_pdu(t_pdu);
1757 break;
1758 }
1759 if (COAP_PDU_IS_RESPONSE(block_pdu)) {
1760 lg_xmit->last_block = block.num;
1761 }
1762 mid = coap_send_internal(session, block_pdu);
1763 if (mid == COAP_INVALID_MID) {
1764 /* Not expected, underlying issue somewhere */
1765 coap_delete_pdu(t_pdu);
1766 return COAP_INVALID_MID;
1767 }
1768 block_pdu = t_pdu;
1769 }
1770 if (!block.m) {
1771 lg_xmit->last_payload = 0;
1772 coap_ticks(&lg_xmit->last_all_sent);
1773 } else
1774 coap_ticks(&lg_xmit->last_payload);
1775 return mid;
1776}
1777
1778#if COAP_CLIENT_SUPPORT
1780coap_block_check_q_block1_xmit(coap_session_t *session, coap_tick_t now) {
1781 coap_lg_xmit_t *lg_xmit;
1782 coap_lg_xmit_t *q;
1783 coap_tick_t timed_out;
1784 coap_tick_t tim_rem = (coap_tick_t)-1;
1785
1786 LL_FOREACH_SAFE(session->lg_xmit, lg_xmit, q) {
1787 coap_tick_t non_timeout = lg_xmit->non_timeout_random_ticks;
1788
1789 if (now < non_timeout)
1790 return non_timeout - now;
1791 timed_out = now - non_timeout;
1792
1793 if (lg_xmit->last_payload) {
1794 if (lg_xmit->last_payload <= timed_out) {
1795 /* Send off the next MAX_PAYLOAD set */
1796 coap_block_b_t block;
1797 size_t chunk = (size_t)1 << (lg_xmit->blk_size + 4);
1798
1799 memset(&block, 0, sizeof(block));
1800 block.num = (uint32_t)(lg_xmit->offset / chunk);
1801 block.m = lg_xmit->offset + chunk < lg_xmit->length;
1802 block.szx = lg_xmit->blk_size;
1803 coap_send_q_blocks(session, lg_xmit, block, &lg_xmit->pdu, COAP_SEND_SKIP_PDU);
1804 if (tim_rem > non_timeout)
1805 tim_rem = non_timeout;
1806 } else {
1807 /* Delay until the next MAX_PAYLOAD needs to be sent off */
1808 if (tim_rem > lg_xmit->last_payload - timed_out)
1809 tim_rem = lg_xmit->last_payload - timed_out;
1810 }
1811 } else if (lg_xmit->last_all_sent) {
1812 non_timeout = COAP_NON_TIMEOUT_TICKS(session);
1813 if (lg_xmit->last_all_sent + 4 * non_timeout <= now) {
1814 /* Expire this entry */
1815 LL_DELETE(session->lg_xmit, lg_xmit);
1816 coap_block_delete_lg_xmit(session, lg_xmit);
1817 } else {
1818 /* Delay until the lg_xmit needs to expire */
1819 if (tim_rem > lg_xmit->last_all_sent + 4 * non_timeout - now)
1820 tim_rem = lg_xmit->last_all_sent + 4 * non_timeout - now;
1821 }
1822 }
1823 }
1824 return tim_rem;
1825}
1826#endif /* COAP_CLIENT_SUPPORT */
1827
1828#if COAP_SERVER_SUPPORT
1830coap_block_check_q_block2_xmit(coap_session_t *session, coap_tick_t now) {
1831 coap_lg_xmit_t *lg_xmit;
1832 coap_lg_xmit_t *q;
1833 coap_tick_t timed_out;
1834 coap_tick_t tim_rem = (coap_tick_t)-1;
1835
1836 LL_FOREACH_SAFE(session->lg_xmit, lg_xmit, q) {
1837 coap_tick_t non_timeout = lg_xmit->non_timeout_random_ticks;
1838
1839 if (now < non_timeout)
1840 return non_timeout - now;
1841 timed_out = now - non_timeout;
1842
1843 if (lg_xmit->last_payload) {
1844 if (lg_xmit->last_payload <= timed_out) {
1845 /* Send off the next MAX_PAYLOAD set */
1846 coap_block_b_t block;
1847 size_t chunk = (size_t)1 << (lg_xmit->blk_size + 4);
1848
1849 memset(&block, 0, sizeof(block));
1850 block.num = (uint32_t)(lg_xmit->offset / chunk);
1851 block.m = lg_xmit->offset + chunk < lg_xmit->length;
1852 block.szx = lg_xmit->blk_size;
1853 if (block.num == (uint32_t)lg_xmit->last_block)
1854 coap_send_q_blocks(session, lg_xmit, block, &lg_xmit->pdu, COAP_SEND_SKIP_PDU);
1855 if (tim_rem > non_timeout)
1856 tim_rem = non_timeout;
1857 } else {
1858 /* Delay until the next MAX_PAYLOAD needs to be sent off */
1859 if (tim_rem > lg_xmit->last_payload - timed_out)
1860 tim_rem = lg_xmit->last_payload - timed_out;
1861 }
1862 } else if (lg_xmit->last_all_sent) {
1863 non_timeout = COAP_NON_TIMEOUT_TICKS(session);
1864 if (lg_xmit->last_all_sent + 4 * non_timeout <= now) {
1865 /* Expire this entry */
1866 LL_DELETE(session->lg_xmit, lg_xmit);
1867 coap_block_delete_lg_xmit(session, lg_xmit);
1868 } else {
1869 /* Delay until the lg_xmit needs to expire */
1870 if (tim_rem > lg_xmit->last_all_sent + 4 * non_timeout - now)
1871 tim_rem = lg_xmit->last_all_sent + 4 * non_timeout - now;
1872 }
1873 }
1874 }
1875 return tim_rem;
1876}
1877#endif /* COAP_SERVER_SUPPORT */
1878#endif /* COAP_Q_BLOCK_SUPPORT */
1879
1880#if COAP_CLIENT_SUPPORT
1881/*
1882 * If Observe = 0, save the token away and return NULL
1883 * Else If Observe = 1, return the saved token for this block
1884 * Else, return NULL
1885 */
1886static coap_bin_const_t *
1887track_fetch_observe(coap_pdu_t *pdu, coap_lg_crcv_t *lg_crcv,
1888 uint32_t block_num, coap_bin_const_t *token) {
1889 /* Need to handle Observe for large FETCH */
1890 coap_opt_iterator_t opt_iter;
1892 &opt_iter);
1893
1894 if (opt && lg_crcv) {
1895 int observe_action = -1;
1896 coap_bin_const_t **tmp;
1897
1898 observe_action = coap_decode_var_bytes(coap_opt_value(opt),
1899 coap_opt_length(opt));
1900 if (observe_action == COAP_OBSERVE_ESTABLISH) {
1901 /* Save the token in lg_crcv */
1902 tmp = coap_realloc_type(COAP_STRING, lg_crcv->obs_token,
1903 (block_num + 1) * sizeof(lg_crcv->obs_token[0]));
1904 if (tmp == NULL)
1905 return NULL;
1906 lg_crcv->obs_token = tmp;
1907 if (block_num + 1 == lg_crcv->obs_token_cnt)
1908 coap_delete_bin_const(lg_crcv->obs_token[block_num]);
1909
1910 lg_crcv->obs_token_cnt = block_num + 1;
1911 lg_crcv->obs_token[block_num] = coap_new_bin_const(token->s,
1912 token->length);
1913 if (lg_crcv->obs_token[block_num] == NULL)
1914 return NULL;
1915 } else if (observe_action == COAP_OBSERVE_CANCEL) {
1916 /* Use the token in lg_crcv */
1917 if (block_num < lg_crcv->obs_token_cnt) {
1918 if (lg_crcv->obs_token[block_num]) {
1919 return lg_crcv->obs_token[block_num];
1920 }
1921 }
1922 }
1923 }
1924 return NULL;
1925}
1926
1927#if COAP_Q_BLOCK_SUPPORT
1929coap_send_q_block1(coap_session_t *session,
1930 coap_block_b_t block,
1931 coap_pdu_t *request,
1932 coap_send_pdu_t send_request) {
1933 /* Need to send up to MAX_PAYLOAD blocks if this is a Q_BLOCK1 */
1934 coap_lg_xmit_t *lg_xmit;
1935 uint64_t token_match =
1937 request->actual_token.length));
1938
1939 LL_FOREACH(session->lg_xmit, lg_xmit) {
1940 if (lg_xmit->option == COAP_OPTION_Q_BLOCK1 &&
1941 (token_match == STATE_TOKEN_BASE(lg_xmit->b.b1.state_token) ||
1942 token_match ==
1944 lg_xmit->b.b1.app_token->length))))
1945 break;
1946 /* try out the next one */
1947 }
1948 return coap_send_q_blocks(session, lg_xmit, block, request, send_request);
1949}
1950#endif /* COAP_Q_BLOCK_SUPPORT */
1951#endif /* COAP_CLIENT_SUPPORT */
1952
1953#if COAP_SERVER_SUPPORT
1954#if COAP_Q_BLOCK_SUPPORT
1955/*
1956 * response is always released before return IF COAP_SEND_INC_PDU
1957 */
1959coap_send_q_block2(coap_session_t *session,
1960 coap_resource_t *resource,
1961 const coap_string_t *query,
1962 coap_pdu_code_t request_method,
1963 coap_block_b_t block,
1964 coap_pdu_t *response,
1965 coap_send_pdu_t send_response) {
1966 /* Need to send up to MAX_PAYLOAD blocks if this is a Q_BLOCK2 */
1967 coap_lg_xmit_t *lg_xmit;
1968 coap_string_t empty = { 0, NULL};
1969
1970 LL_FOREACH(session->lg_xmit, lg_xmit) {
1971 if (lg_xmit->option == COAP_OPTION_Q_BLOCK2 &&
1972 resource == lg_xmit->b.b2.resource &&
1973 request_method == lg_xmit->b.b2.request_method &&
1974 coap_string_equal(query ? query : &empty,
1975 lg_xmit->b.b2.query ? lg_xmit->b.b2.query : &empty))
1976 break;
1977 }
1978 return coap_send_q_blocks(session, lg_xmit, block, response, send_response);
1979}
1980#endif /* COAP_Q_BLOCK_SUPPORT */
1981#endif /* COAP_SERVER_SUPPORT */
1982
1983#if COAP_CLIENT_SUPPORT
1984#if COAP_Q_BLOCK_SUPPORT
1985/*
1986 * Send out a test PDU for Q-Block.
1987 */
1989coap_block_test_q_block(coap_session_t *session, coap_pdu_t *actual) {
1990 coap_pdu_t *pdu;
1991 uint8_t token[8];
1992 size_t token_len;
1993 uint8_t buf[4];
1994 coap_mid_t mid;
1995
1996 assert(session->block_mode & COAP_BLOCK_TRY_Q_BLOCK &&
1997 session->type == COAP_SESSION_TYPE_CLIENT &&
1998 COAP_PDU_IS_REQUEST(actual));
1999
2000 coap_log_debug("Testing for Q-Block support\n");
2001 /* RFC9177 Section 3.1 when checking if available */
2003 coap_new_message_id(session),
2004 coap_session_max_pdu_size(session));
2005 if (!pdu) {
2006 return COAP_INVALID_MID;
2007 }
2008
2009 coap_session_new_token(session, &token_len, token);
2010 coap_add_token(pdu, token_len, token);
2011 /* M needs to be unset as 'asking' for only the first block */
2013 coap_encode_var_safe(buf, sizeof(buf),
2014 (0 << 4) | (0 << 3) | 0),
2015 buf);
2016 set_block_mode_probe_q(session->block_mode);
2017 mid = coap_send_internal(session, pdu);
2018 if (mid == COAP_INVALID_MID)
2019 return COAP_INVALID_MID;
2020 session->remote_test_mid = mid;
2021 return mid;
2022}
2023#endif /* COAP_Q_BLOCK_SUPPORT */
2024
2027 coap_lg_xmit_t *lg_xmit) {
2028 coap_lg_crcv_t *lg_crcv;
2029 uint64_t state_token = STATE_TOKEN_FULL(++session->tx_token, 1);
2030 size_t token_options = pdu->data ? (size_t)(pdu->data - pdu->token) :
2031 pdu->used_size;
2032 size_t data_len = lg_xmit ? lg_xmit->length :
2033 pdu->data ?
2034 pdu->used_size - (pdu->data - pdu->token) : 0;
2035
2036 lg_crcv = coap_malloc_type(COAP_LG_CRCV, sizeof(coap_lg_crcv_t));
2037
2038 if (lg_crcv == NULL)
2039 return NULL;
2040
2041 coap_log_debug("** %s: lg_crcv %p initialized - stateless token xxxx%012llx\n",
2042 coap_session_str(session), (void *)lg_crcv,
2043 STATE_TOKEN_BASE(state_token));
2044 memset(lg_crcv, 0, sizeof(coap_lg_crcv_t));
2045 lg_crcv->initial = 1;
2046 coap_ticks(&lg_crcv->last_used);
2047 /* Set up skeletal PDU to use as a basis for all the subsequent blocks */
2048 memcpy(&lg_crcv->pdu, pdu, sizeof(lg_crcv->pdu));
2049 /* Make sure that there is space for increased token + option change */
2050 lg_crcv->pdu.max_size = token_options + data_len + 9;
2051 lg_crcv->pdu.used_size = token_options + data_len;
2052 lg_crcv->pdu.token = coap_malloc_type(COAP_PDU_BUF,
2053 token_options + data_len + lg_crcv->pdu.max_hdr_size);
2054 if (!lg_crcv->pdu.token) {
2055 coap_block_delete_lg_crcv(session, lg_crcv);
2056 return NULL;
2057 }
2058 lg_crcv->pdu.token += lg_crcv->pdu.max_hdr_size;
2059 memcpy(lg_crcv->pdu.token, pdu->token, token_options);
2060 if (lg_crcv->pdu.data) {
2061 lg_crcv->pdu.data = lg_crcv->pdu.token + token_options;
2062 memcpy(lg_crcv->pdu.data, lg_xmit ? lg_xmit->data : pdu->data, data_len);
2063 }
2064
2065 /* Need to keep original token for updating response PDUs */
2066 lg_crcv->app_token = coap_new_binary(pdu->actual_token.length);
2067 if (!lg_crcv->app_token) {
2068 coap_block_delete_lg_crcv(session, lg_crcv);
2069 return NULL;
2070 }
2071 memcpy(lg_crcv->app_token->s, pdu->actual_token.s, pdu->actual_token.length);
2072
2073 /* Need to set up a base token for actual communications if retries needed */
2074 lg_crcv->retry_counter = 1;
2075 lg_crcv->state_token = state_token;
2076
2077 if (pdu->code == COAP_REQUEST_CODE_FETCH) {
2078 coap_bin_const_t *new_token;
2079
2080 /* Need to save/restore Observe Token for large FETCH */
2081 new_token = track_fetch_observe(pdu, lg_crcv, 0, &pdu->actual_token);
2082 if (new_token)
2083 coap_update_token(pdu, new_token->length, new_token->s);
2084 }
2085
2086 /* In case it is there - must not be in continuing request PDUs */
2087 coap_remove_option(&lg_crcv->pdu, COAP_OPTION_BLOCK1);
2088
2089 return lg_crcv;
2090}
2091
2092void
2094 coap_lg_crcv_t *lg_crcv) {
2095 size_t i;
2096
2097#if (COAP_MAX_LOGGING_LEVEL < _COAP_LOG_DEBUG)
2098 (void)session;
2099#endif
2100 if (lg_crcv == NULL)
2101 return;
2102
2103 if (lg_crcv->pdu.token)
2104 coap_free_type(COAP_PDU_BUF, lg_crcv->pdu.token - lg_crcv->pdu.max_hdr_size);
2106 coap_log_debug("** %s: lg_crcv %p released\n",
2107 coap_session_str(session), (void *)lg_crcv);
2108 coap_delete_binary(lg_crcv->app_token);
2109 for (i = 0; i < lg_crcv->obs_token_cnt; i++) {
2110 coap_delete_bin_const(lg_crcv->obs_token[i]);
2111 }
2113 coap_free_type(COAP_LG_CRCV, lg_crcv);
2114}
2115#endif /* COAP_CLIENT_SUPPORT */
2116
2117#if COAP_SERVER_SUPPORT
2118void
2120 coap_lg_srcv_t *lg_srcv) {
2121#if (COAP_MAX_LOGGING_LEVEL < _COAP_LOG_DEBUG)
2122 (void)session;
2123#endif
2124 if (lg_srcv == NULL)
2125 return;
2126
2128#if COAP_Q_BLOCK_SUPPORT
2129 coap_delete_bin_const(lg_srcv->last_token);
2130#endif /* COAP_Q_BLOCK_SUPPORT */
2132 coap_log_debug("** %s: lg_srcv %p released\n",
2133 coap_session_str(session), (void *)lg_srcv);
2134 coap_free_type(COAP_LG_SRCV, lg_srcv);
2135}
2136#endif /* COAP_SERVER_SUPPORT */
2137
2138void
2140 coap_lg_xmit_t *lg_xmit) {
2141 if (lg_xmit == NULL)
2142 return;
2143
2144 if (lg_xmit->release_func) {
2145 lg_xmit->release_func(session, lg_xmit->app_ptr);
2146 }
2147 if (lg_xmit->pdu.token) {
2148 coap_free_type(COAP_PDU_BUF, lg_xmit->pdu.token - lg_xmit->pdu.max_hdr_size);
2149 }
2150 if (COAP_PDU_IS_REQUEST(&lg_xmit->pdu))
2151 coap_delete_binary(lg_xmit->b.b1.app_token);
2152 else
2153 coap_delete_string(lg_xmit->b.b2.query);
2154
2155 coap_log_debug("** %s: lg_xmit %p released\n",
2156 coap_session_str(session), (void *)lg_xmit);
2157 coap_free_type(COAP_LG_XMIT, lg_xmit);
2158}
2159
2160#if COAP_SERVER_SUPPORT
2161typedef struct {
2162 uint32_t num;
2163 int is_continue;
2164} send_track;
2165
2166static int
2167add_block_send(uint32_t num, int is_continue, send_track *out_blocks,
2168 uint32_t *count, uint32_t max_count) {
2169 uint32_t i;
2170
2171 for (i = 0; i < *count && *count < max_count; i++) {
2172 if (num == out_blocks[i].num)
2173 return 0;
2174 else if (num < out_blocks[i].num) {
2175 if (*count - i > 1)
2176 memmove(&out_blocks[i], &out_blocks[i+1], *count - i -1);
2177 out_blocks[i].num = num;
2178 out_blocks[i].is_continue = is_continue;
2179 (*count)++;
2180 return 1;
2181 }
2182 }
2183 if (*count < max_count) {
2184 out_blocks[i].num = num;
2185 out_blocks[i].is_continue = is_continue;
2186 (*count)++;
2187 return 1;
2188 }
2189 return 0;
2190}
2191
2192/*
2193 * Need to see if this is a request for the next block of a large body
2194 * transfer. If so, need to initiate the response with the next blocks
2195 * and not trouble the application.
2196 *
2197 * If additional responses needed, then these are expicitly sent out and
2198 * 'response' is updated to be the last response to be sent. There can be
2199 * multiple Q-Block2 in the request, as well as the 'Continue' Q-Block2
2200 * request.
2201 *
2202 * This is set up using coap_add_data_large_response()
2203 *
2204 * Server is sending a large data response to GET / observe (Block2)
2205 *
2206 * Return: 0 Call application handler
2207 * 1 Do not call application handler - just send the built response
2208 */
2209int
2211 coap_pdu_t *pdu,
2212 coap_pdu_t *response,
2213 coap_resource_t *resource,
2214 coap_string_t *query) {
2215 coap_lg_xmit_t *p = NULL;
2216 coap_block_b_t block;
2217 coap_block_b_t alt_block;
2218 uint16_t block_opt = 0;
2219 send_track *out_blocks = NULL;
2220 const char *error_phrase;
2221 coap_opt_iterator_t opt_iter;
2222 size_t chunk;
2223 coap_opt_iterator_t opt_b_iter;
2224 coap_opt_t *option;
2225 uint32_t request_cnt, i;
2226 coap_opt_t *etag_opt = NULL;
2227 coap_pdu_t *out_pdu = response;
2228#if COAP_Q_BLOCK_SUPPORT
2229 size_t max_block;
2230
2231 /* Is client indicating that it supports Q_BLOCK2 ? */
2232 if (coap_get_block_b(session, pdu, COAP_OPTION_Q_BLOCK2, &block)) {
2233 if (!(session->block_mode & COAP_BLOCK_HAS_Q_BLOCK))
2234 set_block_mode_has_q(session->block_mode);
2235 block_opt = COAP_OPTION_Q_BLOCK2;
2236 }
2237#endif /* COAP_Q_BLOCK_SUPPORT */
2238 if (coap_get_block_b(session, pdu, COAP_OPTION_BLOCK2, &alt_block)) {
2239 if (block_opt) {
2240 coap_log_warn("Block2 and Q-Block2 cannot be in the same request\n");
2241 coap_add_data(response, sizeof("Both Block2 and Q-Block2 invalid")-1,
2242 (const uint8_t *)"Both Block2 and Q-Block2 invalid");
2243 response->code = COAP_RESPONSE_CODE(400);
2244 goto skip_app_handler;
2245 }
2246 block = alt_block;
2247 block_opt = COAP_OPTION_BLOCK2;
2248 }
2249 if (block_opt == 0)
2250 return 0;
2251 if (block.num == 0) {
2252 /* Get a fresh copy of the data */
2253 return 0;
2254 }
2255 p = coap_find_lg_xmit_response(session, pdu, resource, query);
2256 if (p == NULL)
2257 return 0;
2258
2259#if COAP_Q_BLOCK_SUPPORT
2260 out_blocks = coap_malloc_type(COAP_STRING, sizeof(send_track) * COAP_MAX_PAYLOADS(session));
2261#else /* ! COAP_Q_BLOCK_SUPPORT */
2262 out_blocks = coap_malloc_type(COAP_STRING, sizeof(send_track));
2263#endif /* ! COAP_Q_BLOCK_SUPPORT */
2264 if (!out_blocks) {
2265 goto internal_issue;
2266 }
2267
2268 /* lg_xmit (response) found */
2269
2270 etag_opt = coap_check_option(pdu, COAP_OPTION_ETAG, &opt_iter);
2271 if (etag_opt) {
2272 uint64_t etag = coap_decode_var_bytes8(coap_opt_value(etag_opt),
2273 coap_opt_length(etag_opt));
2274 if (etag != p->b.b2.etag) {
2275 /* Not a match - pass up to a higher level */
2276 return 0;
2277 }
2278 out_pdu->code = COAP_RESPONSE_CODE(203);
2279 coap_ticks(&p->last_sent);
2280 goto skip_app_handler;
2281 } else {
2282 out_pdu->code = p->pdu.code;
2283 }
2284 coap_ticks(&p->last_obs);
2285 p->last_all_sent = 0;
2286
2287 chunk = (size_t)1 << (p->blk_size + 4);
2288 if (block_opt) {
2289 if (block.bert) {
2290 coap_log_debug("found Block option, block is BERT, block nr. %u, M %d\n",
2291 block.num, block.m);
2292 } else {
2293 coap_log_debug("found Block option, block size is %u, block nr. %u, M %d\n",
2294 1 << (block.szx + 4), block.num, block.m);
2295 }
2296 if (block.bert == 0 && block.szx != p->blk_size) {
2297 if ((p->offset + chunk) % ((size_t)1 << (block.szx + 4)) == 0) {
2298 /*
2299 * Recompute the block number of the previous packet given
2300 * the new block size
2301 */
2302 block.num = (uint32_t)(((p->offset + chunk) >> (block.szx + 4)) - 1);
2303 p->blk_size = block.szx;
2304 chunk = (size_t)1 << (p->blk_size + 4);
2305 p->offset = block.num * chunk;
2306 coap_log_debug("new Block size is %u, block number %u completed\n",
2307 1 << (block.szx + 4), block.num);
2308 } else {
2309 coap_log_debug("ignoring request to increase Block size, "
2310 "next block is not aligned on requested block size "
2311 "boundary. (%zu x %u mod %u = %zu (which is not 0)\n",
2312 p->offset/chunk + 1, (1 << (p->blk_size + 4)),
2313 (1 << (block.szx + 4)),
2314 (p->offset + chunk) % ((size_t)1 << (block.szx + 4)));
2315 }
2316 }
2317 }
2318
2319 /*
2320 * Need to check if there are multiple Q-Block2 requests. If so, they
2321 * need to be sent out in order of requests with the final request being
2322 * handled as per singular Block 2 request.
2323 */
2324 request_cnt = 0;
2325#if COAP_Q_BLOCK_SUPPORT
2326 max_block = (p->length + chunk - 1)/chunk;
2327#endif /* COAP_Q_BLOCK_SUPPORT */
2328 coap_option_iterator_init(pdu, &opt_b_iter, COAP_OPT_ALL);
2329 while ((option = coap_option_next(&opt_b_iter))) {
2330 unsigned int num;
2331 if (opt_b_iter.number != p->option)
2332 continue;
2333 num = coap_opt_block_num(option);
2334 if (num > 0xFFFFF) /* 20 bits max for num */
2335 continue;
2336 if (block.aszx != COAP_OPT_BLOCK_SZX(option)) {
2337 coap_add_data(response,
2338 sizeof("Changing blocksize during request invalid")-1,
2339 (const uint8_t *)"Changing blocksize during request invalid");
2340 response->code = COAP_RESPONSE_CODE(400);
2341 goto skip_app_handler;
2342 }
2343#if COAP_Q_BLOCK_SUPPORT
2344 if (COAP_OPT_BLOCK_MORE(option) && p->option == COAP_OPTION_Q_BLOCK2) {
2345 if ((num % COAP_MAX_PAYLOADS(session)) == 0) {
2346 if (num == 0) {
2347 /* This is a repeat request for everything - hmm */
2348 goto call_app_handler;
2349 }
2350 /* 'Continue' request */
2351 for (i = 0; i < COAP_MAX_PAYLOADS(session) &&
2352 num + i < max_block; i++) {
2353 add_block_send(num + i, 1, out_blocks, &request_cnt,
2354 COAP_MAX_PAYLOADS(session));
2355 p->last_block = num + i;
2356 }
2357 } else {
2358 /* Requesting remaining payloads in this MAX_PAYLOADS */
2359 for (i = 0; i < COAP_MAX_PAYLOADS(session) -
2360 num % COAP_MAX_PAYLOADS(session) &&
2361 num + i < max_block; i++) {
2362 add_block_send(num + i, 0, out_blocks, &request_cnt,
2363 COAP_MAX_PAYLOADS(session));
2364 }
2365 }
2366 } else
2367 add_block_send(num, 0, out_blocks, &request_cnt,
2368 COAP_MAX_PAYLOADS(session));
2369#else /* ! COAP_Q_BLOCK_SUPPORT */
2370 add_block_send(num, 0, out_blocks, &request_cnt, 1);
2371 break;
2372#endif /* ! COAP_Q_BLOCK_SUPPORT */
2373 }
2374 if (request_cnt == 0) {
2375 /* Block2 or Q-Block2 not found - give them the first block */
2376 block.szx = p->blk_size;
2377 p->offset = 0;
2378 out_blocks[0].num = 0;
2379 out_blocks[0].is_continue = 0;
2380 request_cnt = 1;
2381 }
2382
2383 for (i = 0; i < request_cnt; i++) {
2384 uint8_t buf[8];
2385
2386 block.num = out_blocks[i].num;
2387 p->offset = block.num * chunk;
2388
2389 if (i + 1 < request_cnt) {
2390 /* Need to set up a copy of the pdu to send */
2391 coap_opt_filter_t drop_options;
2392
2393 memset(&drop_options, 0, sizeof(coap_opt_filter_t));
2394 if (block.num != 0)
2396 if (out_blocks[i].is_continue) {
2397 out_pdu = coap_pdu_duplicate(&p->pdu, session, p->pdu.actual_token.length,
2398 p->pdu.actual_token.s, &drop_options);
2399 } else {
2400 out_pdu = coap_pdu_duplicate(&p->pdu, session, pdu->actual_token.length,
2401 pdu->actual_token.s, &drop_options);
2402 }
2403 if (!out_pdu) {
2404 goto internal_issue;
2405 }
2406 } else {
2407 if (out_blocks[i].is_continue)
2409 p->pdu.actual_token.s);
2410 /*
2411 * Copy the options across and then fix the block option
2412 *
2413 * Need to drop Observe option if Block2 and block.num != 0
2414 */
2416 while ((option = coap_option_next(&opt_iter))) {
2417 if (opt_iter.number == COAP_OPTION_OBSERVE && block.num != 0)
2418 continue;
2419 if (!coap_insert_option(response, opt_iter.number,
2420 coap_opt_length(option),
2421 coap_opt_value(option))) {
2422 goto internal_issue;
2423 }
2424 }
2425 out_pdu = response;
2426 }
2427 if (pdu->type == COAP_MESSAGE_NON)
2428 out_pdu->type = COAP_MESSAGE_NON;
2429 if (block.bert) {
2430 size_t token_options = pdu->data ? (size_t)(pdu->data - pdu->token) : pdu->used_size;
2431 block.m = (p->length - p->offset) >
2432 ((out_pdu->max_size - token_options) /1024) * 1024;
2433 } else {
2434 block.m = (p->offset + chunk) < p->length;
2435 }
2436 if (!coap_update_option(out_pdu, p->option,
2438 sizeof(buf),
2439 (block.num << 4) |
2440 (block.m << 3) |
2441 block.aszx),
2442 buf)) {
2443 goto internal_issue;
2444 }
2445 if (!(p->offset + chunk < p->length)) {
2446 /* Last block - keep in cache for 4 * ACK_TIMOUT */
2448 }
2449 if (p->b.b2.maxage_expire) {
2450 coap_tick_t now;
2451 coap_time_t rem;
2452
2453 if (!(p->offset + chunk < p->length)) {
2454 /* Last block - keep in cache for 4 * ACK_TIMOUT */
2456 }
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 if (!etag_opt && !coap_add_block_b_data(out_pdu,
2476 p->length,
2477 p->data,
2478 &block)) {
2479 goto internal_issue;
2480 }
2481 if (i + 1 < request_cnt) {
2482 coap_ticks(&p->last_sent);
2483 coap_send_internal(session, out_pdu);
2484 }
2485 }
2487 goto skip_app_handler;
2488#if COAP_Q_BLOCK_SUPPORT
2489call_app_handler:
2490 coap_free_type(COAP_STRING, out_blocks);
2491 return 0;
2492#endif /* COAP_Q_BLOCK_SUPPORT */
2493
2494internal_issue:
2495 response->code = COAP_RESPONSE_CODE(500);
2496 error_phrase = coap_response_phrase(response->code);
2497 coap_add_data(response, strlen(error_phrase),
2498 (const uint8_t *)error_phrase);
2499 /* Keep in cache for 4 * ACK_TIMOUT incase of retry */
2500 if (p)
2502
2503skip_app_handler:
2504 coap_free_type(COAP_STRING, out_blocks);
2505 return 1;
2506}
2507#endif /* COAP_SERVER_SUPPORT */
2508
2509static int
2510update_received_blocks(coap_rblock_t *rec_blocks, uint32_t block_num) {
2511 uint32_t i;
2512
2513 /* Reset as there is activity */
2514 rec_blocks->retry = 0;
2515
2516 for (i = 0; i < rec_blocks->used; i++) {
2517 if (block_num >= rec_blocks->range[i].begin &&
2518 block_num <= rec_blocks->range[i].end)
2519 break;
2520
2521 if (block_num < rec_blocks->range[i].begin) {
2522 if (block_num + 1 == rec_blocks->range[i].begin) {
2523 rec_blocks->range[i].begin = block_num;
2524 } else {
2525 /* Need to insert a new range */
2526 if (rec_blocks->used == COAP_RBLOCK_CNT -1)
2527 /* Too many losses */
2528 return 0;
2529 memmove(&rec_blocks->range[i+1], &rec_blocks->range[i],
2530 (rec_blocks->used - i) * sizeof(rec_blocks->range[0]));
2531 rec_blocks->range[i].begin = rec_blocks->range[i].end = block_num;
2532 rec_blocks->used++;
2533 }
2534 break;
2535 }
2536 if (block_num == rec_blocks->range[i].end + 1) {
2537 rec_blocks->range[i].end = block_num;
2538 if (i + 1 < rec_blocks->used) {
2539 if (rec_blocks->range[i+1].begin == block_num + 1) {
2540 /* Merge the 2 ranges */
2541 rec_blocks->range[i].end = rec_blocks->range[i+1].end;
2542 if (i+2 < rec_blocks->used) {
2543 memmove(&rec_blocks->range[i+1], &rec_blocks->range[i+2],
2544 (rec_blocks->used - (i+2)) * sizeof(rec_blocks->range[0]));
2545 }
2546 rec_blocks->used--;
2547 }
2548 }
2549 break;
2550 }
2551 }
2552 if (i == rec_blocks->used) {
2553 if (rec_blocks->used == COAP_RBLOCK_CNT -1)
2554 /* Too many losses */
2555 return 0;
2556 rec_blocks->range[i].begin = rec_blocks->range[i].end = block_num;
2557 rec_blocks->used++;
2558 }
2559 coap_ticks(&rec_blocks->last_seen);
2560 return 1;
2561}
2562
2563#if COAP_SERVER_SUPPORT
2564/*
2565 * Need to check if this is a large PUT / POST using multiple blocks
2566 *
2567 * Server receiving PUT/POST etc. of a large amount of data (Block1)
2568 *
2569 * Return: 0 Call application handler
2570 * 1 Do not call application handler - just send the built response
2571 */
2572int
2574 coap_session_t *session,
2575 coap_pdu_t *pdu,
2576 coap_pdu_t *response,
2577 coap_resource_t *resource,
2578 coap_string_t *uri_path,
2579 coap_opt_t *observe,
2580 int *added_block,
2581 coap_lg_srcv_t **pfree_lg_srcv) {
2582 size_t length = 0;
2583 const uint8_t *data = NULL;
2584 size_t offset = 0;
2585 size_t total = 0;
2586 coap_block_b_t block;
2587 coap_opt_iterator_t opt_iter;
2588 uint16_t block_option = 0;
2589
2590 *added_block = 0;
2591 *pfree_lg_srcv = NULL;
2592 coap_get_data_large(pdu, &length, &data, &offset, &total);
2593 pdu->body_offset = 0;
2594 pdu->body_total = length;
2595
2596 if (coap_get_block_b(session, pdu, COAP_OPTION_BLOCK1, &block)) {
2597 block_option = COAP_OPTION_BLOCK1;
2598#if COAP_Q_BLOCK_SUPPORT
2599 if (coap_check_option(pdu, COAP_OPTION_Q_BLOCK1, &opt_iter)) {
2600 /* Cannot handle Q-Block1 as well */
2601 coap_add_data(response, sizeof("Block1 + Q-Block1 together")-1,
2602 (const uint8_t *)"Block1 + Q-Block1 together");
2603 response->code = COAP_RESPONSE_CODE(402);
2604 goto skip_app_handler;
2605 }
2606#endif /* COAP_Q_BLOCK_SUPPORT */
2607 }
2608#if COAP_Q_BLOCK_SUPPORT
2609 else if (coap_get_block_b(session, pdu, COAP_OPTION_Q_BLOCK1, &block)) {
2610 block_option = COAP_OPTION_Q_BLOCK1;
2611 set_block_mode_has_q(session->block_mode);
2612 }
2613#endif /* COAP_Q_BLOCK_SUPPORT */
2614 if (block_option) {
2615 coap_lg_srcv_t *p;
2616 coap_opt_t *size_opt = coap_check_option(pdu,
2618 &opt_iter);
2619 coap_opt_t *fmt_opt = coap_check_option(pdu,
2621 &opt_iter);
2622 uint16_t fmt = fmt_opt ? coap_decode_var_bytes(coap_opt_value(fmt_opt),
2623 coap_opt_length(fmt_opt)) :
2625 coap_opt_t *rtag_opt = coap_check_option(pdu,
2627 &opt_iter);
2628 size_t rtag_length = rtag_opt ? coap_opt_length(rtag_opt) : 0;
2629 const uint8_t *rtag = rtag_opt ? coap_opt_value(rtag_opt) : NULL;
2630
2631 if (length > block.chunk_size) {
2632 coap_log_debug("block: Oversized packet - reduced to %u from %zu\n",
2633 block.chunk_size, length);
2634 length = block.chunk_size;
2635 }
2636 total = size_opt ? coap_decode_var_bytes(coap_opt_value(size_opt),
2637 coap_opt_length(size_opt)) : 0;
2638 offset = block.num << (block.szx + 4);
2639
2640 LL_FOREACH(session->lg_srcv, p) {
2641 if (rtag_opt || p->rtag_set == 1) {
2642 if (!(rtag_opt && p->rtag_set == 1))
2643 continue;
2644 if (p->rtag_length != rtag_length ||
2645 memcmp(p->rtag, rtag, rtag_length) != 0)
2646 continue;
2647 }
2648 if (resource == p->resource) {
2649 break;
2650 }
2651 if ((p->resource == context->unknown_resource ||
2652 resource == context->proxy_uri_resource) &&
2653 coap_string_equal(uri_path, p->uri_path))
2654 break;
2655 }
2656 if (!p && block.num != 0) {
2657 /* random access - no need to track */
2658 pdu->body_data = data;
2659 pdu->body_length = length;
2660 pdu->body_offset = offset;
2661 pdu->body_total = length + offset + (block.m ? 1 : 0);
2662 }
2663 /* Do not do this if this is a single block */
2664 else if (!p && !(offset == 0 && block.m == 0)) {
2666 if (p == NULL) {
2667 coap_add_data(response, sizeof("Memory issue")-1,
2668 (const uint8_t *)"Memory issue");
2669 response->code = COAP_RESPONSE_CODE(500);
2670 goto skip_app_handler;
2671 }
2672 coap_log_debug("** %s: lg_srcv %p initialized\n",
2673 coap_session_str(session), (void *)p);
2674 memset(p, 0, sizeof(coap_lg_srcv_t));
2675 coap_ticks(&p->last_used);
2676 p->resource = resource;
2677 if (resource == context->unknown_resource ||
2678 resource == context->proxy_uri_resource)
2679 p->uri_path = coap_new_str_const(uri_path->s, uri_path->length);
2680 p->content_format = fmt;
2681 p->total_len = total;
2682 p->amount_so_far = length;
2683 p->szx = block.szx;
2684 p->block_option = block_option;
2685 if (observe) {
2686 p->observe_length = min(coap_opt_length(observe), 3);
2687 memcpy(p->observe, coap_opt_value(observe), p->observe_length);
2688 p->observe_set = 1;
2689 }
2690 if (rtag_opt) {
2691 p->rtag_length = coap_opt_length(rtag_opt);
2692 memcpy(p->rtag, coap_opt_value(rtag_opt), p->rtag_length);
2693 p->rtag_set = 1;
2694 }
2695 p->body_data = NULL;
2696 LL_PREPEND(session->lg_srcv, p);
2697 }
2698 if (p) {
2699 if (fmt != p->content_format) {
2700 coap_add_data(response, sizeof("Content-Format mismatch")-1,
2701 (const uint8_t *)"Content-Format mismatch");
2702 response->code = COAP_RESPONSE_CODE(408);
2703 goto free_lg_srcv;
2704 }
2705#if COAP_Q_BLOCK_SUPPORT
2706 if (block_option == COAP_OPTION_Q_BLOCK1) {
2707 if (total != p->total_len) {
2708 coap_add_data(response, sizeof("Size1 mismatch")-1,
2709 (const uint8_t *)"Size1 mismatch");
2710 response->code = COAP_RESPONSE_CODE(408);
2711 goto free_lg_srcv;
2712 }
2713 }
2714#endif /* COAP_Q_BLOCK_SUPPORT */
2715 p->last_mid = pdu->mid;
2716 p->last_type = pdu->type;
2717#if COAP_Q_BLOCK_SUPPORT
2718 coap_delete_bin_const(p->last_token);
2719 p->last_token = coap_new_bin_const(pdu->actual_token.s,
2720 pdu->actual_token.length);
2721#endif /* COAP_Q_BLOCK_SUPPORT */
2722 if (session->block_mode &
2723#if COAP_Q_BLOCK_SUPPORT
2724 (COAP_BLOCK_SINGLE_BODY|COAP_BLOCK_HAS_Q_BLOCK) ||
2725#else /* ! COAP_Q_BLOCK_SUPPORT */
2727#endif /* ! COAP_Q_BLOCK_SUPPORT */
2728 block.bert) {
2729 size_t chunk = (size_t)1 << (block.szx + 4);
2730 int update_data = 0;
2731 unsigned int saved_num = block.num;
2732 size_t saved_offset = offset;
2733
2734 while (offset < saved_offset + length) {
2735 if (!check_if_received_block(&p->rec_blocks, block.num)) {
2736 /* Update list of blocks received */
2737 if (!update_received_blocks(&p->rec_blocks, block.num)) {
2739 coap_add_data(response, sizeof("Too many missing blocks")-1,
2740 (const uint8_t *)"Too many missing blocks");
2741 response->code = COAP_RESPONSE_CODE(408);
2742 goto free_lg_srcv;
2743 }
2744 update_data = 1;
2745 }
2746 block.num++;
2747 offset = block.num << (block.szx + 4);
2748 }
2749 block.num--;
2750 if (update_data) {
2751#if COAP_Q_BLOCK_SUPPORT
2752 p->rec_blocks.processing_payload_set =
2753 block.num / COAP_MAX_PAYLOADS(session);
2754#endif /* COAP_Q_BLOCK_SUPPORT */
2755 /* Update saved data */
2756 if (p->total_len < saved_offset + length) {
2757 p->total_len = saved_offset + length;
2758 }
2759 p->body_data = coap_block_build_body(p->body_data, length, data,
2760 saved_offset, p->total_len);
2761 if (!p->body_data)
2762 goto call_app_handler;
2763
2764 }
2765 if (block.m ||
2767 (uint32_t)(p->total_len + chunk -1)/chunk)) {
2768 /* Not all the payloads of the body have arrived */
2769 if (block.m) {
2770 uint8_t buf[4];
2771
2772#if COAP_Q_BLOCK_SUPPORT
2773 if (block_option == COAP_OPTION_Q_BLOCK1) {
2775 (uint32_t)(p->total_len + chunk -1)/chunk)) {
2776 goto give_app_data;
2777 }
2778 if (p->rec_blocks.used == 1 &&
2779 (p->rec_blocks.range[0].end % COAP_MAX_PAYLOADS(session)) + 1
2780 == COAP_MAX_PAYLOADS(session)) {
2781 /* Blocks could arrive in wrong order */
2782 block.num = p->rec_blocks.range[0].end;
2783 } else {
2784 /* The remote end will be sending the next one unless this
2785 is a MAX_PAYLOADS and all previous have been received */
2786 goto skip_app_handler;
2787 }
2788 if (COAP_PROTO_RELIABLE(session->proto) ||
2789 pdu->type != COAP_MESSAGE_NON)
2790 goto skip_app_handler;
2791 }
2792#endif /* COAP_Q_BLOCK_SUPPORT */
2793 /* Ask for the next block */
2794 coap_insert_option(response, block_option,
2795 coap_encode_var_safe(buf, sizeof(buf),
2796 (saved_num << 4) |
2797 (block.m << 3) |
2798 block.aszx),
2799 buf);
2800 response->code = COAP_RESPONSE_CODE(231);
2801 goto skip_app_handler;
2802 }
2803 goto skip_app_handler;
2804 }
2805
2806 /*
2807 * Remove the Block1 option as passing all of the data to
2808 * application layer. Add back in observe option if appropriate.
2809 * Adjust all other information.
2810 */
2811#if COAP_Q_BLOCK_SUPPORT
2812give_app_data:
2813#endif /* COAP_Q_BLOCK_SUPPORT */
2814 if (p->observe_set) {
2816 p->observe_length, p->observe);
2817 }
2818 coap_remove_option(pdu, block_option);
2819 pdu->body_data = p->body_data->s;
2820 pdu->body_length = p->total_len;
2821 pdu->body_offset = 0;
2822 pdu->body_total = p->total_len;
2823 coap_log_debug("Server app version of updated PDU\n");
2825 *pfree_lg_srcv = p;
2826 goto call_app_handler;
2827 } else {
2828 /* No need to update body_data and body_length as a single PDU */
2829 pdu->body_offset = offset;
2830 /* Exact match if last block */
2831 if (block.m) {
2832 uint8_t buf[4];
2833
2834 if (total > offset + length + block.m)
2835 pdu->body_total = total;
2836 else
2837 pdu->body_total = offset + length + block.m;
2838
2839 coap_insert_option(response, block_option,
2840 coap_encode_var_safe(buf, sizeof(buf),
2841 (block.num << 4) |
2842 (block.m << 3) |
2843 block.aszx),
2844 buf);
2845 *added_block = 1;
2846 goto call_app_handler;
2847 } else {
2848 pdu->body_total = offset + length + block.m;
2849 }
2850 }
2851
2852 if (block.m == 0) {
2853 /* Last chunk - free off all */
2854 coap_ticks(&p->last_used);
2855 }
2856 goto call_app_handler;
2857
2858free_lg_srcv:
2859 LL_DELETE(session->lg_srcv, p);
2860 coap_block_delete_lg_srcv(session, p);
2861 goto skip_app_handler;
2862 }
2863 }
2864call_app_handler:
2865 return 0;
2866
2867skip_app_handler:
2868 return 1;
2869}
2870#endif /* COAP_SERVER_SUPPORT */
2871
2872#if COAP_CLIENT_SUPPORT
2873#if COAP_Q_BLOCK_SUPPORT
2874static uint32_t
2875derive_cbor_value(const uint8_t **bp, size_t rem_len) {
2876 uint32_t value = **bp & 0x1f;
2877 (*bp)++;
2878 if (value < 24) {
2879 return value;
2880 } else if (value == 24) {
2881 if (rem_len < 2)
2882 return (uint32_t)-1;
2883 value = **bp;
2884 (*bp)++;
2885 return value;
2886 } else if (value == 25) {
2887 if (rem_len < 3)
2888 return (uint32_t)-1;
2889 value = **bp << 8;
2890 (*bp)++;
2891 value |= **bp;
2892 (*bp)++;
2893 return value;
2894 }
2895 if (rem_len < 4)
2896 return (uint32_t)-1;
2897 value = **bp << 24;
2898 (*bp)++;
2899 value = **bp << 16;
2900 (*bp)++;
2901 value = **bp << 8;
2902 (*bp)++;
2903 value |= **bp;
2904 (*bp)++;
2905 return value;
2906}
2907#endif /* COAP_Q_BLOCK_SUPPORT */
2908
2909static int
2910check_freshness(coap_session_t *session, coap_pdu_t *rcvd, coap_pdu_t *sent,
2911 coap_lg_xmit_t *lg_xmit, coap_lg_crcv_t *lg_crcv) {
2912 /* Check for Echo option for freshness */
2913 coap_opt_iterator_t opt_iter;
2914 coap_opt_t *opt = coap_check_option(rcvd, COAP_OPTION_ECHO, &opt_iter);
2915
2916 if (opt) {
2917 if (sent || lg_xmit || lg_crcv) {
2918 /* Need to retransmit original request with Echo option added */
2919 coap_pdu_t *echo_pdu;
2920 coap_mid_t mid;
2921 const uint8_t *data;
2922 size_t data_len;
2923 int have_data = 0;
2924 uint8_t ltoken[8];
2925 size_t ltoken_len;
2926 uint64_t token;
2927
2928 if (sent) {
2929 if (coap_get_data(sent, &data_len, &data))
2930 have_data = 1;
2931 } else if (lg_xmit) {
2932 sent = &lg_xmit->pdu;
2933 if (lg_xmit->length) {
2934 size_t blk_size = (size_t)1 << (lg_xmit->blk_size + 4);
2935 size_t offset = (lg_xmit->last_block + 1) * blk_size;
2936 have_data = 1;
2937 data = &lg_xmit->data[offset];
2938 data_len = (lg_xmit->length - offset) > blk_size ? blk_size :
2939 lg_xmit->length - offset;
2940 }
2941 } else { /* lg_crcv */
2942 sent = &lg_crcv->pdu;
2943 if (coap_get_data(sent, &data_len, &data))
2944 have_data = 1;
2945 }
2946 if (lg_xmit) {
2947 token = STATE_TOKEN_FULL(lg_xmit->b.b1.state_token,
2948 ++lg_xmit->b.b1.count);
2949 } else {
2950 token = STATE_TOKEN_FULL(lg_crcv->state_token,
2951 ++lg_crcv->retry_counter);
2952 }
2953 ltoken_len = coap_encode_var_safe8(ltoken, sizeof(token), token);
2954 echo_pdu = coap_pdu_duplicate(sent, session, ltoken_len, ltoken, NULL);
2955 if (!echo_pdu)
2956 return 0;
2957 if (!coap_insert_option(echo_pdu, COAP_OPTION_ECHO,
2958 coap_opt_length(opt), coap_opt_value(opt)))
2959 goto not_sent;
2960 if (have_data) {
2961 coap_add_data(echo_pdu, data_len, data);
2962 }
2963 /* Need to track Observe token change if Observe */
2964 track_fetch_observe(echo_pdu, lg_crcv, 0, &echo_pdu->actual_token);
2965#if COAP_OSCORE_SUPPORT
2966 if (session->oscore_encryption &&
2967 (opt = coap_check_option(echo_pdu, COAP_OPTION_OBSERVE, &opt_iter)) &&
2969 /* Need to update the base PDU's Token for closing down Observe */
2970 if (lg_xmit) {
2971 lg_xmit->b.b1.state_token = token;
2972 } else {
2973 lg_crcv->state_token = token;
2974 }
2975 }
2976#endif /* COAP_OSCORE_SUPPORT */
2977 mid = coap_send_internal(session, echo_pdu);
2978 if (mid == COAP_INVALID_MID)
2979 goto not_sent;
2980 return 1;
2981 } else {
2982 /* Need to save Echo option value to add to next reansmission */
2983not_sent:
2984 coap_delete_bin_const(session->echo);
2985 session->echo = coap_new_bin_const(coap_opt_value(opt),
2986 coap_opt_length(opt));
2987 }
2988 }
2989 return 0;
2990}
2991
2992static void
2993track_echo(coap_session_t *session, coap_pdu_t *rcvd) {
2994 coap_opt_iterator_t opt_iter;
2995 coap_opt_t *opt = coap_check_option(rcvd, COAP_OPTION_ECHO, &opt_iter);
2996
2997 if (opt) {
2998 coap_delete_bin_const(session->echo);
2999 session->echo = coap_new_bin_const(coap_opt_value(opt),
3000 coap_opt_length(opt));
3001 }
3002}
3003
3004/*
3005 * Need to see if this is a response to a large body request transfer. If so,
3006 * need to initiate the request containing the next block and not trouble the
3007 * application. Note that Token must unique per request/response.
3008 *
3009 * Client receives large data acknowledgement from server (Block1)
3010 *
3011 * This is set up using coap_add_data_large_request()
3012 *
3013 * Client is using GET etc.
3014 *
3015 * Return: 0 Call application handler
3016 * 1 Do not call application handler - just send the built response
3017 */
3018int
3020 coap_pdu_t *rcvd) {
3021 coap_lg_xmit_t *p;
3022 coap_lg_xmit_t *q;
3023 uint64_t token_match =
3025 rcvd->actual_token.length));
3026 coap_lg_crcv_t *lg_crcv = NULL;
3027
3028 LL_FOREACH_SAFE(session->lg_xmit, p, q) {
3029 if (!COAP_PDU_IS_REQUEST(&p->pdu) ||
3030 (token_match != STATE_TOKEN_BASE(p->b.b1.state_token) &&
3031 token_match !=
3033 p->b.b1.app_token->length)))) {
3034 /* try out the next one */
3035 continue;
3036 }
3037 /* lg_xmit found */
3038 size_t chunk = (size_t)1 << (p->blk_size + 4);
3039 coap_block_b_t block;
3040
3041 if (COAP_RESPONSE_CLASS(rcvd->code) == 2 &&
3042 coap_get_block_b(session, rcvd, p->option, &block)) {
3043
3044 if (block.bert) {
3045 coap_log_debug("found Block option, block is BERT, block nr. %u (%zu)\n",
3046 block.num, p->b.b1.bert_size);
3047 } else {
3048 coap_log_debug("found Block option, block size is %u, block nr. %u\n",
3049 1 << (block.szx + 4), block.num);
3050 }
3051 if (block.szx != p->blk_size) {
3052 if ((p->offset + chunk) % ((size_t)1 << (block.szx + 4)) == 0) {
3053 /*
3054 * Recompute the block number of the previous packet given the
3055 * new block size
3056 */
3057 block.num = (uint32_t)(((p->offset + chunk) >> (block.szx + 4)) - 1);
3058 p->blk_size = block.szx;
3059 chunk = (size_t)1 << (p->blk_size + 4);
3060 p->offset = block.num * chunk;
3061 coap_log_debug("new Block size is %u, block number %u completed\n",
3062 1 << (block.szx + 4), block.num);
3063 block.bert = 0;
3064 block.aszx = block.szx;
3065 } else {
3066 coap_log_debug("ignoring request to increase Block size, "
3067 "next block is not aligned on requested block size boundary. "
3068 "(%zu x %u mod %u = %zu != 0)\n",
3069 p->offset/chunk + 1, (1 << (p->blk_size + 4)),
3070 (1 << (block.szx + 4)),
3071 (p->offset + chunk) % ((size_t)1 << (block.szx + 4)));
3072 }
3073 }
3074 track_echo(session, rcvd);
3075 if (p->last_block == (int)block.num &&
3077 /*
3078 * Duplicate Block1 ACK
3079 *
3080 * RFCs not clear here, but on a lossy connection, there could
3081 * be multiple Block1 ACKs, causing the client to retransmit the
3082 * same block multiple times, or the server retransmitting the
3083 * same ACK.
3084 *
3085 * Once a block has been ACKd, there is no need to retransmit it.
3086 */
3087 return 1;
3088 }
3089 if (block.bert)
3090 block.num += (unsigned int)(p->b.b1.bert_size / 1024 - 1);
3091 p->last_block = block.num;
3092 p->offset = (block.num + 1) * chunk;
3093 if (p->offset < p->length) {
3094 /* Build the next PDU request based off the skeletal PDU */
3095 uint8_t buf[8];
3096 coap_pdu_t *pdu;
3097 uint64_t token = STATE_TOKEN_FULL(p->b.b1.state_token, ++p->b.b1.count);
3098 size_t len = coap_encode_var_safe8(buf, sizeof(token), token);
3099
3100 if (p->pdu.code == COAP_REQUEST_CODE_FETCH) {
3101 /* Need to handle Observe for large FETCH */
3102 LL_FOREACH(session->lg_crcv, lg_crcv) {
3103 if (coap_binary_equal(p->b.b1.app_token, lg_crcv->app_token)) {
3104 coap_bin_const_t *new_token;
3105 coap_bin_const_t ctoken = { len, buf };
3106
3107 /* Need to save/restore Observe Token for large FETCH */
3108 new_token = track_fetch_observe(&p->pdu, lg_crcv, block.num + 1,
3109 &ctoken);
3110 if (new_token) {
3111 assert(len <= sizeof(buf));
3112 len = new_token->length;
3113 memcpy(buf, new_token->s, len);
3114 }
3115 break;
3116 }
3117 }
3118 }
3119 pdu = coap_pdu_duplicate(&p->pdu, session, len, buf, NULL);
3120 if (!pdu)
3121 goto fail_body;
3122
3123 block.num++;
3124 if (block.bert) {
3125 size_t token_options = pdu->data ? (size_t)(pdu->data - pdu->token) :
3126 pdu->used_size;
3127 block.m = (p->length - p->offset) >
3128 ((pdu->max_size - token_options) /1024) * 1024;
3129 } else {
3130 block.m = (p->offset + chunk) < p->length;
3131 }
3132 coap_update_option(pdu, p->option,
3133 coap_encode_var_safe(buf, sizeof(buf),
3134 (block.num << 4) |
3135 (block.m << 3) |
3136 block.aszx),
3137 buf);
3138
3139 if (!coap_add_block_b_data(pdu,
3140 p->length,
3141 p->data,
3142 &block))
3143 goto fail_body;
3144 p->b.b1.bert_size = block.chunk_size;
3145 coap_ticks(&p->last_sent);
3146#if COAP_Q_BLOCK_SUPPORT
3147 if (p->option == COAP_OPTION_Q_BLOCK1 &&
3148 pdu->type == COAP_MESSAGE_NON) {
3149 if (coap_send_q_block1(session, block, pdu,
3150 COAP_SEND_INC_PDU) == COAP_INVALID_MID)
3151 goto fail_body;
3152 return 1;
3153 } else if (coap_send_internal(session, pdu) == COAP_INVALID_MID)
3154 goto fail_body;
3155#else /* ! COAP_Q_BLOCK_SUPPORT */
3156 if (coap_send_internal(session, pdu) == COAP_INVALID_MID)
3157 goto fail_body;
3158#endif /* ! COAP_Q_BLOCK_SUPPORT */
3159 return 1;
3160 }
3161 } else if (rcvd->code == COAP_RESPONSE_CODE(401)) {
3162 if (check_freshness(session, rcvd, sent, p, NULL))
3163 return 1;
3164#if COAP_Q_BLOCK_SUPPORT
3165 } else if (rcvd->code == COAP_RESPONSE_CODE(402)) {
3166 /* Q-Block1 or Q-Block2 not present in p - duplicate error ? */
3167 if (coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK2, &block) ||
3168 coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK1, &block))
3169 return 1;
3170 } else if (rcvd->code == COAP_RESPONSE_CODE(408) &&
3172 size_t length;
3173 const uint8_t *data;
3174 coap_opt_iterator_t opt_iter;
3175 coap_opt_t *fmt_opt = coap_check_option(rcvd,
3177 &opt_iter);
3178 uint16_t fmt = fmt_opt ?
3180 coap_opt_length(fmt_opt)) :
3182
3184 goto fail_body;
3185
3186 if (COAP_PROTO_RELIABLE(session->proto) ||
3187 rcvd->type != COAP_MESSAGE_NON) {
3188 coap_log_debug("Unexpected 4.08 - protocol violation - ignore\n");
3189 return 1;
3190 }
3191
3192 if (coap_get_data(rcvd, &length, &data)) {
3193 /* Need to decode CBOR to work out what blocks to re-send */
3194 const uint8_t *bp = data;
3195 uint32_t i;
3196 uint8_t buf[8];
3197 coap_pdu_t *pdu;
3198 uint64_t token = coap_decode_var_bytes8(rcvd->actual_token.s,
3199 rcvd->actual_token.length);
3200 uint8_t ltoken[8];
3201 size_t ltoken_length;
3202
3203 for (i = 0; (bp < data + length) &&
3204 i < COAP_MAX_PAYLOADS(session); i++) {
3205 if ((*bp & 0xc0) != 0x00) /* uint(value) */
3206 goto fail_cbor;
3207 block.num = derive_cbor_value(&bp, data + length - bp);
3208 coap_log_debug("Q-Block1: Missing block %d\n", block.num);
3209 if (block.num > (1 << 20) -1)
3210 goto fail_cbor;
3211 block.m = (block.num + 1) * chunk < p->length;
3212 block.szx = p->blk_size;
3213
3214 /* Build the next PDU request based off the skeletal PDU */
3215 token = STATE_TOKEN_FULL(p->b.b1.state_token,++p->b.b1.count);
3216 ltoken_length = coap_encode_var_safe8(ltoken, sizeof(token), token);
3217 pdu = coap_pdu_duplicate(&p->pdu, session, ltoken_length,
3218 ltoken, NULL);
3219 if (!pdu)
3220 goto fail_body;
3221
3222 coap_update_option(pdu, p->option,
3223 coap_encode_var_safe(buf, sizeof(buf),
3224 (block.num << 4) |
3225 (block.m << 3) |
3226 block.szx),
3227 buf);
3228
3229 if (!coap_add_block(pdu,
3230 p->length,
3231 p->data,
3232 block.num,
3233 block.szx))
3234 goto fail_body;
3235 if (coap_send_internal(session, pdu) == COAP_INVALID_MID)
3236 goto fail_body;
3237 }
3238 return 1;
3239 }
3240fail_cbor:
3241 coap_log_info("Invalid application/missing-blocks+cbor-seq\n");
3242#endif /* COAP_Q_BLOCK_SUPPORT */
3243 }
3244 goto lg_xmit_finished;
3245 } /* end of LL_FOREACH_SAFE */
3246 return 0;
3247
3248fail_body:
3250 /* There has been an internal error of some sort */
3251 rcvd->code = COAP_RESPONSE_CODE(500);
3252lg_xmit_finished:
3253 if (session->lg_crcv) {
3254 LL_FOREACH(session->lg_crcv, lg_crcv) {
3255 if (STATE_TOKEN_BASE(p->b.b1.state_token) ==
3256 STATE_TOKEN_BASE(lg_crcv->state_token)) {
3257 /* In case of observe */
3258 lg_crcv->state_token = p->b.b1.state_token;
3259 break;
3260 }
3261 }
3262 }
3263 if (!lg_crcv) {
3264 /* need to put back original token into rcvd */
3265 if (p->b.b1.app_token)
3267 p->b.b1.app_token->s);
3268 coap_log_debug("Client app version of updated PDU\n");
3270 }
3271
3272 LL_DELETE(session->lg_xmit, p);
3273 coap_block_delete_lg_xmit(session, p);
3274 return 0;
3275}
3276#endif /* COAP_CLIENT_SUPPORT */
3277
3278/*
3279 * Re-assemble payloads into a body
3280 */
3282coap_block_build_body(coap_binary_t *body_data, size_t length,
3283 const uint8_t *data, size_t offset, size_t total) {
3284 if (data == NULL)
3285 return NULL;
3286 if (body_data == NULL && total) {
3287 body_data = coap_new_binary(total);
3288 }
3289 if (body_data == NULL)
3290 return NULL;
3291
3292 /* Update saved data */
3293 if (offset + length <= total && body_data->length >= total) {
3294 memcpy(&body_data->s[offset], data, length);
3295 } else {
3296 /*
3297 * total may be inaccurate as per
3298 * https://rfc-editor.org/rfc/rfc7959#section-4
3299 * o In a request carrying a Block1 Option, to indicate the current
3300 * estimate the client has of the total size of the resource
3301 * representation, measured in bytes ("size indication").
3302 * o In a response carrying a Block2 Option, to indicate the current
3303 * estimate the server has of the total size of the resource
3304 * representation, measured in bytes ("size indication").
3305 */
3306 coap_binary_t *new = coap_resize_binary(body_data, offset + length);
3307
3308 if (new) {
3309 body_data = new;
3310 memcpy(&body_data->s[offset], data, length);
3311 } else {
3312 coap_delete_binary(body_data);
3313 return NULL;
3314 }
3315 }
3316 return body_data;
3317}
3318
3319#if COAP_CLIENT_SUPPORT
3320/*
3321 * Need to see if this is a large body response to a request. If so,
3322 * need to initiate the request for the next block and not trouble the
3323 * application. Note that Token must be unique per request/response.
3324 *
3325 * This is set up using coap_send()
3326 * Client receives large data from server ((Q-)Block2)
3327 *
3328 * Return: 0 Call application handler
3329 * 1 Do not call application handler - just sent the next request
3330 */
3331int
3333 coap_session_t *session,
3334 coap_pdu_t *sent,
3335 coap_pdu_t *rcvd,
3336 coap_recurse_t recursive) {
3337 coap_lg_crcv_t *p;
3338 coap_block_b_t block;
3339#if COAP_Q_BLOCK_SUPPORT
3340 coap_block_b_t qblock;
3341#endif /* COAP_Q_BLOCK_SUPPORT */
3342 int have_block = 0;
3343 uint16_t block_opt = 0;
3344 size_t offset;
3345 int ack_rst_sent = 0;
3346 uint64_t token_match =
3348 rcvd->actual_token.length));
3349
3350 memset(&block, 0, sizeof(block));
3351#if COAP_Q_BLOCK_SUPPORT
3352 memset(&qblock, 0, sizeof(qblock));
3353#endif /* COAP_Q_BLOCK_SUPPORT */
3354 LL_FOREACH(session->lg_crcv, p) {
3355 size_t chunk = 0;
3356 uint8_t buf[8];
3357 coap_opt_iterator_t opt_iter;
3358
3359 if (token_match != STATE_TOKEN_BASE(p->state_token) &&
3361 /* try out the next one */
3362 continue;
3363 }
3364
3365 /* lg_crcv found */
3366
3367 if (COAP_RESPONSE_CLASS(rcvd->code) == 2) {
3368 size_t length;
3369 const uint8_t *data;
3371 &opt_iter);
3372 size_t size2 = size_opt ?
3374 coap_opt_length(size_opt)) : 0;
3375
3376 /* length and data are cleared on error */
3377 (void)coap_get_data(rcvd, &length, &data);
3378 rcvd->body_offset = 0;
3379 rcvd->body_total = length;
3380 if (coap_get_block_b(session, rcvd, COAP_OPTION_BLOCK2, &block)) {
3381 have_block = 1;
3382 block_opt = COAP_OPTION_BLOCK2;
3383 }
3384#if COAP_Q_BLOCK_SUPPORT
3385 if (coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK2, &qblock)) {
3386 if (have_block) {
3387 coap_log_warn("Both Block1 and Q-Block1 not supported in a response\n");
3388 }
3389 have_block = 1;
3390 block_opt = COAP_OPTION_Q_BLOCK2;
3391 block = qblock;
3392 /* server indicating that it supports Q_BLOCK */
3393 if (!(session->block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
3394 set_block_mode_has_q(session->block_mode);
3395 }
3396 }
3397#endif /* COAP_Q_BLOCK_SUPPORT */
3398 track_echo(session, rcvd);
3399 if (have_block && (block.m || length)) {
3400 coap_opt_t *fmt_opt = coap_check_option(rcvd,
3402 &opt_iter);
3403 uint16_t fmt = fmt_opt ?
3405 coap_opt_length(fmt_opt)) :
3407 coap_opt_t *etag_opt = coap_check_option(rcvd,
3409 &opt_iter);
3410 size_t saved_offset;
3411 int updated_block;
3412
3413 if (length > block.chunk_size) {
3414 coap_log_debug("block: Oversized packet - reduced to %u from %zu\n",
3415 block.chunk_size, length);
3416 length = block.chunk_size;
3417 }
3418 /* Possibility that Size2 not sent, or is too small */
3419 chunk = (size_t)1 << (block.szx + 4);
3420 offset = block.num * chunk;
3421 if (size2 < (offset + length)) {
3422 if (block.m)
3423 size2 = offset + length + 1;
3424 else
3425 size2 = offset + length;
3426 }
3427 saved_offset = offset;
3428
3429 if (p->initial) {
3430#if COAP_Q_BLOCK_SUPPORT
3431reinit:
3432#endif /* COAP_Q_BLOCK_SUPPORT */
3433 p->initial = 0;
3434 if (p->body_data) {
3436 p->body_data = NULL;
3437 }
3438 if (etag_opt) {
3439 p->etag_length = coap_opt_length(etag_opt);
3440 memcpy(p->etag, coap_opt_value(etag_opt), p->etag_length);
3441 p->etag_set = 1;
3442 } else {
3443 p->etag_set = 0;
3444 }
3445 p->total_len = size2;
3446 p->content_format = fmt;
3447 p->szx = block.szx;
3448 p->block_option = block_opt;
3449 p->last_type = rcvd->type;
3450 p->rec_blocks.used = 0;
3451#if COAP_Q_BLOCK_SUPPORT
3452 p->rec_blocks.processing_payload_set = 0;
3453#endif /* COAP_Q_BLOCK_SUPPORT */
3454 }
3455 if (p->total_len < size2)
3456 p->total_len = size2;
3457
3458 if (etag_opt) {
3459 if (!full_match(coap_opt_value(etag_opt),
3460 coap_opt_length(etag_opt),
3461 p->etag, p->etag_length)) {
3462 /* body of data has changed - need to restart request */
3463 coap_pdu_t *pdu;
3464 uint64_t token = STATE_TOKEN_FULL(p->state_token,
3465 ++p->retry_counter);
3466 size_t len = coap_encode_var_safe8(buf, sizeof(token), token);
3467 coap_opt_filter_t drop_options;
3468
3469#if COAP_Q_BLOCK_SUPPORT
3470 if (block_opt == COAP_OPTION_Q_BLOCK2)
3471 goto reinit;
3472#endif /* COAP_Q_BLOCK_SUPPORT */
3473
3474 coap_log_warn("Data body updated during receipt - new request started\n");
3475 if (!(session->block_mode & COAP_BLOCK_SINGLE_BODY))
3477
3478 p->initial = 1;
3480 p->body_data = NULL;
3481
3482 coap_session_new_token(session, &len, buf);
3483 memset(&drop_options, 0, sizeof(coap_opt_filter_t));
3485 pdu = coap_pdu_duplicate(&p->pdu, session, len, buf, &drop_options);
3486 if (!pdu)
3487 goto fail_resp;
3488
3489 coap_update_option(pdu, block_opt,
3490 coap_encode_var_safe(buf, sizeof(buf),
3491 (0 << 4) | (0 << 3) | block.aszx),
3492 buf);
3493
3494 if (coap_send_internal(session, pdu) == COAP_INVALID_MID)
3495 goto fail_resp;
3496
3497 goto skip_app_handler;
3498 }
3499 } else if (p->etag_set) {
3500 /* Cannot handle this change in ETag to not being there */
3501 coap_log_warn("Not all blocks have ETag option\n");
3502 goto fail_resp;
3503 }
3504
3505 if (fmt != p->content_format) {
3506 coap_log_warn("Content-Format option mismatch\n");
3507 goto fail_resp;
3508 }
3509#if COAP_Q_BLOCK_SUPPORT
3510 if (block_opt == COAP_OPTION_Q_BLOCK2 && size2 != p->total_len) {
3511 coap_log_warn("Size2 option mismatch\n");
3512 goto fail_resp;
3513 }
3514#endif /* COAP_Q_BLOCK_SUPPORT */
3515 if (block.num == 0) {
3516 coap_opt_t *obs_opt = coap_check_option(rcvd,
3518 &opt_iter);
3519 if (obs_opt) {
3520 p->observe_length = min(coap_opt_length(obs_opt), 3);
3521 memcpy(p->observe, coap_opt_value(obs_opt), p->observe_length);
3522 p->observe_set = 1;
3523 } else {
3524 p->observe_set = 0;
3525 }
3526 }
3527 updated_block = 0;
3528 while (offset < saved_offset + length) {
3529 if (!check_if_received_block(&p->rec_blocks, block.num)) {
3530#if COAP_Q_BLOCK_SUPPORT
3531 uint32_t this_payload_set = block.num / COAP_MAX_PAYLOADS(session);
3532#endif /* COAP_Q_BLOCK_SUPPORT */
3533
3534 coap_log_debug("found Block option, block size is %u, block nr. %u\n",
3535 1 << (block.szx + 4), block.num);
3536#if COAP_Q_BLOCK_SUPPORT
3537 if (block_opt == COAP_OPTION_Q_BLOCK2 && p->rec_blocks.used &&
3538 this_payload_set > p->rec_blocks.processing_payload_set &&
3539 this_payload_set != p->rec_blocks.latest_payload_set) {
3540 coap_request_missing_q_block2(session, p);
3541 }
3542 p->rec_blocks.latest_payload_set = this_payload_set;
3543#endif /* COAP_Q_BLOCK_SUPPORT */
3544 /* Update list of blocks received */
3545 if (!update_received_blocks(&p->rec_blocks, block.num)) {
3547 goto fail_resp;
3548 }
3549 updated_block = 1;
3550 }
3551 block.num++;
3552 offset = block.num << (block.szx + 4);
3553 if (!block.bert && block_opt != COAP_OPTION_Q_BLOCK2)
3554 break;
3555 }
3556 block.num--;
3557 /* Only process if not duplicate block */
3558 if (updated_block) {
3559 if ((session->block_mode & COAP_SINGLE_BLOCK_OR_Q) || block.bert) {
3560 if (size2 < saved_offset + length) {
3561 size2 = saved_offset + length;
3562 }
3563 p->body_data = coap_block_build_body(p->body_data, length, data,
3564 saved_offset, size2);
3565 if (p->body_data == NULL) {
3566 goto fail_resp;
3567 }
3568 }
3569 if (block.m || !check_all_blocks_in(&p->rec_blocks,
3570 (size2 + chunk -1) / chunk)) {
3571 /* Not all the payloads of the body have arrived */
3572 size_t len;
3573 coap_pdu_t *pdu;
3574 uint64_t token;
3575
3576 if (block.m) {
3577#if COAP_Q_BLOCK_SUPPORT
3578 if (block_opt == COAP_OPTION_Q_BLOCK2) {
3579 /* Blocks could arrive in wrong order */
3581 (size2 + chunk -1) / chunk)) {
3582 goto give_to_app;
3583 }
3584 if (check_all_blocks_in_for_payload_set(session,
3585 &p->rec_blocks)) {
3586 block.num = p->rec_blocks.range[0].end;
3587 /* Now requesting next payload */
3588 p->rec_blocks.processing_payload_set =
3589 block.num / COAP_MAX_PAYLOADS(session) + 1;
3590 if (check_any_blocks_next_payload_set(session,
3591 &p->rec_blocks)) {
3592 /* Need to ask for them individually */
3593 coap_request_missing_q_block2(session, p);
3594 goto skip_app_handler;
3595 }
3596 } else {
3597 /* The remote end will be sending the next one unless this
3598 is a MAX_PAYLOADS and all previous have been received */
3599 goto skip_app_handler;
3600 }
3601 if (COAP_PROTO_RELIABLE(session->proto) ||
3602 rcvd->type != COAP_MESSAGE_NON)
3603 goto skip_app_handler;
3604
3605 } else
3606#endif /* COAP_Q_BLOCK_SUPPORT */
3607 block.m = 0;
3608
3609 /* Ask for the next block */
3610 token = STATE_TOKEN_FULL(p->state_token, ++p->retry_counter);
3611 len = coap_encode_var_safe8(buf, sizeof(token), token);
3612 pdu = coap_pdu_duplicate(&p->pdu, session, len, buf, NULL);
3613 if (!pdu)
3614 goto fail_resp;
3615
3616 if (rcvd->type == COAP_MESSAGE_NON)
3617 pdu->type = COAP_MESSAGE_NON; /* Server is using NON */
3618
3619 /* Only sent with the first block */
3621
3622 coap_update_option(pdu, block_opt,
3623 coap_encode_var_safe(buf, sizeof(buf),
3624 ((block.num + 1) << 4) |
3625 (block.m << 3) | block.aszx),
3626 buf);
3627
3628 if (coap_send_internal(session, pdu) == COAP_INVALID_MID)
3629 goto fail_resp;
3630 }
3631 if ((session->block_mode & COAP_SINGLE_BLOCK_OR_Q) || block.bert)
3632 goto skip_app_handler;
3633
3634 /* need to put back original token into rcvd */
3636 rcvd->body_offset = saved_offset;
3637#if COAP_Q_BLOCK_SUPPORT
3638 rcvd->body_total = block_opt == COAP_OPTION_Q_BLOCK2 ?
3639 p->total_len : size2;
3640#else /* ! COAP_Q_BLOCK_SUPPORT */
3641 rcvd->body_total = size2;
3642#endif /* ! COAP_Q_BLOCK_SUPPORT */
3643 coap_log_debug("Client app version of updated PDU\n");
3645 goto call_app_handler;
3646 }
3647#if COAP_Q_BLOCK_SUPPORT
3648give_to_app:
3649#endif /* COAP_Q_BLOCK_SUPPORT */
3650 if ((session->block_mode & COAP_SINGLE_BLOCK_OR_Q) || block.bert) {
3651 /* Pretend that there is no block */
3652 coap_remove_option(rcvd, block_opt);
3653 if (p->observe_set) {
3655 p->observe_length, p->observe);
3656 }
3657 rcvd->body_data = p->body_data->s;
3658#if COAP_Q_BLOCK_SUPPORT
3659 rcvd->body_length = block_opt == COAP_OPTION_Q_BLOCK2 ?
3660 p->total_len : saved_offset + length;
3661#else /* ! COAP_Q_BLOCK_SUPPORT */
3662 rcvd->body_length = saved_offset + length;
3663#endif /* ! COAP_Q_BLOCK_SUPPORT */
3664 rcvd->body_offset = 0;
3665 rcvd->body_total = rcvd->body_length;
3666 } else {
3667 rcvd->body_offset = saved_offset;
3668#if COAP_Q_BLOCK_SUPPORT
3669 rcvd->body_total = block_opt == COAP_OPTION_Q_BLOCK2 ?
3670 p->total_len : size2;
3671#else /* ! COAP_Q_BLOCK_SUPPORT */
3672 rcvd->body_total = size2;
3673#endif /* ! COAP_Q_BLOCK_SUPPORT */
3674 }
3675 if (context->response_handler) {
3676 /* need to put back original token into rcvd */
3677 if (!coap_binary_equal(&rcvd->actual_token, p->app_token)) {
3679 coap_log_debug("Client app version of updated PDU\n");
3681 }
3682 if (context->response_handler(session, sent, rcvd,
3683 rcvd->mid) == COAP_RESPONSE_FAIL)
3684 coap_send_rst(session, rcvd);
3685 else
3686 coap_send_ack(session, rcvd);
3687 } else {
3688 coap_send_ack(session, rcvd);
3689 }
3690 ack_rst_sent = 1;
3691 if (p->observe_set == 0) {
3692 /* Expire this entry */
3693 LL_DELETE(session->lg_crcv, p);
3694 coap_block_delete_lg_crcv(session, p);
3695 goto skip_app_handler;
3696 }
3697 /* Set up for the next data body as observing */
3698 p->initial = 1;
3699 if (p->body_data) {
3701 p->body_data = NULL;
3702 }
3703 }
3704 coap_ticks(&p->last_used);
3705 goto skip_app_handler;
3706 } else {
3707 coap_opt_t *obs_opt = coap_check_option(rcvd,
3709 &opt_iter);
3710 if (obs_opt) {
3711 p->observe_length = min(coap_opt_length(obs_opt), 3);
3712 memcpy(p->observe, coap_opt_value(obs_opt), p->observe_length);
3713 p->observe_set = 1;
3714 } else {
3715 p->observe_set = 0;
3716 if (!coap_binary_equal(&rcvd->actual_token, p->app_token)) {
3717 /* need to put back original token into rcvd */
3719 coap_log_debug("PDU presented to app.\n");
3721 }
3722 /* Expire this entry */
3723 goto expire_lg_crcv;
3724 }
3725 }
3726 coap_ticks(&p->last_used);
3727 } else if (rcvd->code == COAP_RESPONSE_CODE(401)) {
3728#if COAP_OSCORE_SUPPORT
3729 if (check_freshness(session, rcvd,
3730 (session->oscore_encryption == 0) ? sent : NULL,
3731 NULL, p))
3732#else /* !COAP_OSCORE_SUPPORT */
3733 if (check_freshness(session, rcvd, sent, NULL, p))
3734#endif /* !COAP_OSCORE_SUPPORT */
3735 goto skip_app_handler;
3736 goto expire_lg_crcv;
3737 } else {
3738 /* Not 2.xx or 4.01 - assume it is a failure of some sort */
3739 goto expire_lg_crcv;
3740 }
3741 if (!block.m && !p->observe_set) {
3742fail_resp:
3743 /* lg_crcv no longer required - cache it for 1 sec */
3744 coap_ticks(&p->last_used);
3747 }
3748 /* need to put back original token into rcvd */
3749 if (!coap_binary_equal(&rcvd->actual_token, p->app_token)) {
3751 coap_log_debug("Client app version of updated PDU (3)\n");
3753 }
3754 break;
3755 } /* LL_FOREACH() */
3756
3757 /* Check if receiving a block response and if blocks can be set up */
3758 if (recursive == COAP_RECURSE_OK && !p) {
3759 if (!sent) {
3760 if (coap_get_block_b(session, rcvd, COAP_OPTION_BLOCK2, &block)
3761#if COAP_Q_BLOCK_SUPPORT
3762 ||
3763 coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK2, &block)
3764#endif /* COAP_Q_BLOCK_SUPPORT */
3765 ) {
3766 coap_log_debug("** %s: large body receive internal issue\n",
3767 coap_session_str(session));
3768 goto skip_app_handler;
3769 }
3770 } else if (COAP_RESPONSE_CLASS(rcvd->code) == 2) {
3771 if (coap_get_block_b(session, rcvd, COAP_OPTION_BLOCK2, &block)) {
3772#if COAP_Q_BLOCK_SUPPORT
3773 if (session->block_mode & COAP_BLOCK_PROBE_Q_BLOCK) {
3774 set_block_mode_drop_q(session->block_mode);
3775 coap_log_debug("Q-Block support disabled\n");
3776 }
3777#endif /* COAP_Q_BLOCK_SUPPORT */
3778 have_block = 1;
3779 block_opt = COAP_OPTION_BLOCK2;
3780 if (block.num != 0) {
3781 /* Assume random access and just give the single response to app */
3782 size_t length;
3783 const uint8_t *data;
3784 size_t chunk = (size_t)1 << (block.szx + 4);
3785
3786 coap_get_data(rcvd, &length, &data);
3787 rcvd->body_offset = block.num*chunk;
3788 rcvd->body_total = block.num*chunk + length + (block.m ? 1 : 0);
3789 goto call_app_handler;
3790 }
3791 }
3792#if COAP_Q_BLOCK_SUPPORT
3793 else if (coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK2, &block)) {
3794 have_block = 1;
3795 block_opt = COAP_OPTION_Q_BLOCK2;
3796 /* server indicating that it supports Q_BLOCK2 */
3797 if (!(session->block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
3798 set_block_mode_has_q(session->block_mode);
3799 }
3800 }
3801#endif /* COAP_Q_BLOCK_SUPPORT */
3802 if (have_block) {
3803 coap_lg_crcv_t *lg_crcv = coap_block_new_lg_crcv(session, sent, NULL);
3804
3805 if (lg_crcv) {
3806 LL_PREPEND(session->lg_crcv, lg_crcv);
3807 return coap_handle_response_get_block(context, session, sent, rcvd,
3809 }
3810 }
3811 track_echo(session, rcvd);
3812 } else if (rcvd->code == COAP_RESPONSE_CODE(401)) {
3813 coap_lg_crcv_t *lg_crcv = coap_block_new_lg_crcv(session, sent, NULL);
3814
3815 if (lg_crcv) {
3816 LL_PREPEND(session->lg_crcv, lg_crcv);
3817 return coap_handle_response_get_block(context, session, sent, rcvd,
3819 }
3820 }
3821 }
3822 return 0;
3823
3824expire_lg_crcv:
3825 /* need to put back original token into rcvd */
3826 if (!coap_binary_equal(&rcvd->actual_token, p->app_token)) {
3828 coap_log_debug("Client app version of updated PDU\n");
3830 }
3831 /* Expire this entry */
3832 LL_DELETE(session->lg_crcv, p);
3833 coap_block_delete_lg_crcv(session, p);
3834
3835call_app_handler:
3836 return 0;
3837
3838skip_app_handler:
3839 if (!ack_rst_sent)
3840 coap_send_ack(session, rcvd);
3841 return 1;
3842}
3843#endif /* COAP_CLIENT_SUPPORT */
3844
3845#if COAP_SERVER_SUPPORT
3846/* Check if lg_xmit generated and update PDU code if so */
3847void
3849 const coap_pdu_t *request,
3850 coap_pdu_t *response, const coap_resource_t *resource,
3851 const coap_string_t *query) {
3852 coap_lg_xmit_t *lg_xmit;
3853
3854 if (response->code == 0)
3855 return;
3856 lg_xmit = coap_find_lg_xmit_response(session, request, resource, query);
3857 if (lg_xmit && lg_xmit->pdu.code == 0) {
3858 lg_xmit->pdu.code = response->code;
3859 return;
3860 }
3861}
3862#endif /* COAP_SERVER_SUPPORT */
3863
3864#if COAP_CLIENT_SUPPORT
3865void
3867 uint64_t token_match =
3869 pdu->actual_token.length));
3870 coap_lg_xmit_t *lg_xmit;
3871 coap_lg_crcv_t *lg_crcv;
3872
3873 if (session->lg_crcv) {
3874 LL_FOREACH(session->lg_crcv, lg_crcv) {
3875 if (coap_binary_equal(&pdu->actual_token, lg_crcv->app_token))
3876 return;
3877 if (token_match == STATE_TOKEN_BASE(lg_crcv->state_token)) {
3878 coap_update_token(pdu, lg_crcv->app_token->length,
3879 lg_crcv->app_token->s);
3880 coap_log_debug("Client app version of updated PDU\n");
3882 return;
3883 }
3884 }
3885 }
3886 if (COAP_PDU_IS_REQUEST(pdu) && session->lg_xmit) {
3887 LL_FOREACH(session->lg_xmit, lg_xmit) {
3888 if (coap_binary_equal(&pdu->actual_token, lg_xmit->b.b1.app_token))
3889 return;
3890 if (token_match == STATE_TOKEN_BASE(lg_xmit->b.b1.state_token)) {
3891 coap_update_token(pdu, lg_xmit->b.b1.app_token->length,
3892 lg_xmit->b.b1.app_token->s);
3893 coap_log_debug("Client app version of updated PDU\n");
3895 return;
3896 }
3897 }
3898 }
3899}
3900#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)
Definition: coap_block.c:1450
#define STATE_TOKEN_BASE(t)
Definition: coap_block.c:22
static int update_received_blocks(coap_rblock_t *rec_blocks, uint32_t block_num)
Definition: coap_block.c:2510
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)
Definition: coap_block.c:1437
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...
coap_recurse_t
void coap_block_delete_lg_xmit(coap_session_t *session, coap_lg_xmit_t *lg_xmit)
Definition: coap_block.c:2139
#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)
Definition: coap_block.c:1138
@ 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.
Definition: coap_block.c:3282
#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:986
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:1381
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:770
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:3917
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.
Definition: coap_option.c:153
uint32_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
Definition: coap_option.c:212
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.
Definition: coap_option.c:117
size_t coap_opt_encode_size(uint16_t delta, size_t length)
Compute storage bytes needed for an option with given delta and length.
Definition: coap_option.c:351
#define COAP_OPT_ALL
Pre-defined filter that includes all options.
Definition: coap_option.h:108
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.
Definition: coap_option.c:199
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
Definition: coap_option.c:249
int coap_option_filter_set(coap_opt_filter_t *filter, coap_option_num_t option)
Sets the corresponding entry for number in filter.
Definition: coap_option.c:496
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.
Definition: coap_session.c:608
#define COAP_PROTO_NOT_RELIABLE(p)
Definition: coap_session.h:36
#define COAP_PROTO_RELIABLE(p)
Definition: coap_session.h:37
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
Definition: coap_session.h:45
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.
Definition: coap_option.h:168
coap_option_num_t number
decoded option number
Definition: coap_option.h:170
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