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