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