libcoap 4.3.5-develop-aef27f2
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 %zu from %zu\n",
90 length - (length % 1024), length);
91 length -= length % 1024;
92 }
93 block->chunk_size = (uint32_t)length;
94 } else
95 block->chunk_size = 0;
96 } else {
97 block->chunk_size = (size_t)1 << (block->szx + 4);
98 }
99 block->defined = 1;
100
101 /* The block number is at most 20 bits, so values above 2^20 - 1
102 * are illegal. */
103 num = coap_opt_block_num(option);
104 if (num > 0xFFFFF) {
105 return 0;
106 }
107 block->num = num;
108 return 1;
109 }
110
111 return 0;
112}
113
114int
116 coap_block_t *block) {
117 coap_block_b_t block_b;
118
119 assert(block);
120 memset(block, 0, sizeof(coap_block_t));
121
122 if (coap_get_block_b(NULL, pdu, number, &block_b)) {
123 block->num = block_b.num;
124 block->m = block_b.m;
125 block->szx = block_b.szx;
126 return 1;
127 }
128 return 0;
129}
130
131static int
133 unsigned int num,
134 unsigned int blk_size, size_t total) {
135 size_t token_options = pdu->data ? (size_t)(pdu->data - pdu->token) : pdu->used_size;
136 size_t avail = pdu->max_size - token_options;
137 unsigned int start = num << (blk_size + 4);
138 unsigned int can_use_bert = block->defined == 0 || block->bert;
139
140 assert(start <= total);
141 memset(block, 0, sizeof(*block));
142 block->num = num;
143 block->szx = block->aszx = blk_size;
144 if (can_use_bert && blk_size == 6 && avail >= 1024 && session != NULL &&
145 COAP_PROTO_RELIABLE(session->proto) &&
146 session->csm_bert_rem_support && session->csm_bert_loc_support) {
147 block->bert = 1;
148 block->aszx = 7;
149 block->chunk_size = (uint32_t)((avail / 1024) * 1024);
150 } else {
151 block->chunk_size = (size_t)1 << (blk_size + 4);
152 if (avail < block->chunk_size && (total - start) >= avail) {
153 /* Need to reduce block size */
154 unsigned int szx;
155 int new_blk_size;
156
157 if (avail < 16) { /* bad luck, this is the smallest block size */
158 coap_log_debug("not enough space, even the smallest block does not fit (1)\n");
159 return 0;
160 }
161 new_blk_size = coap_flsll((long long)avail) - 5;
162 coap_log_debug("decrease block size for %zu to %d\n", avail, new_blk_size);
163 szx = block->szx;
164 block->szx = new_blk_size;
165 block->num <<= szx - block->szx;
166 block->chunk_size = (size_t)1 << (new_blk_size + 4);
167 }
168 }
169 block->m = block->chunk_size < total - start;
170 return 1;
171}
172
173int
175 coap_pdu_t *pdu, size_t data_length) {
176 size_t start;
177 unsigned char buf[4];
178 coap_block_b_t block_b;
179
180 assert(pdu);
181
182 start = block->num << (block->szx + 4);
183 if (block->num != 0 && data_length <= start) {
184 coap_log_debug("illegal block requested\n");
185 return -2;
186 }
187
188 assert(pdu->max_size > 0);
189
190 block_b.defined = 1;
191 block_b.bert = 0;
192 if (!setup_block_b(NULL, pdu, &block_b, block->num,
193 block->szx, data_length))
194 return -3;
195
196 /* to re-encode the block option */
197 coap_update_option(pdu, number, coap_encode_var_safe(buf, sizeof(buf),
198 ((block_b.num << 4) |
199 (block_b.m << 3) |
200 block_b.szx)),
201 buf);
202
203 return 1;
204}
205
206int
208 coap_option_num_t number,
209 coap_pdu_t *pdu, size_t data_length) {
210 size_t start;
211 unsigned char buf[4];
212
213 assert(pdu);
214
215 start = block->num << (block->szx + 4);
216 if (block->num != 0 && data_length <= start) {
217 coap_log_debug("illegal block requested\n");
218 return -2;
219 }
220
221 assert(pdu->max_size > 0);
222
223 if (!setup_block_b(session, pdu, block, block->num,
224 block->szx, data_length))
225 return -3;
226
227 /* to re-encode the block option */
228 coap_update_option(pdu, number, coap_encode_var_safe(buf, sizeof(buf),
229 ((block->num << 4) |
230 (block->m << 3) |
231 block->aszx)),
232 buf);
233
234 return 1;
235}
236
237int
238coap_add_block(coap_pdu_t *pdu, size_t len, const uint8_t *data,
239 unsigned int block_num, unsigned char block_szx) {
240 unsigned int start;
241 start = block_num << (block_szx + 4);
242
243 if (len <= start)
244 return 0;
245
246 return coap_add_data(pdu,
247 min(len - start, ((size_t)1 << (block_szx + 4))),
248 data + start);
249}
250
251int
252coap_add_block_b_data(coap_pdu_t *pdu, size_t len, const uint8_t *data,
253 coap_block_b_t *block) {
254 unsigned int start = block->num << (block->szx + 4);
255 size_t max_size;
256
257 if (len <= start)
258 return 0;
259
260 if (block->bert) {
261 size_t token_options = pdu->data ? (size_t)(pdu->data - pdu->token) : pdu->used_size;
262 max_size = ((pdu->max_size - token_options) / 1024) * 1024;
263 } else {
264 max_size = (size_t)1 << (block->szx + 4);
265 }
266 block->chunk_size = (uint32_t)max_size;
267
268 return coap_add_data(pdu,
269 min(len - start, max_size),
270 data + start);
271}
272
273/*
274 * Note that the COAP_OPTION_ have to be added in the correct order
275 */
276void
278 coap_pdu_t *response,
279 uint16_t media_type,
280 int maxage,
281 size_t length,
282 const uint8_t *data
283 ) {
284 unsigned char buf[4];
285 coap_block_t block2;
286 int block2_requested = 0;
287#if COAP_SERVER_SUPPORT
288 uint64_t etag = 0;
289 coap_digest_t digest;
290 coap_digest_ctx_t *dctx = NULL;
291#endif /* COAP_SERVER_SUPPORT */
292
293 memset(&block2, 0, sizeof(block2));
294 /*
295 * Need to check that a valid block is getting asked for so that the
296 * correct options are put into the PDU.
297 */
298 if (request) {
299 if (coap_get_block(request, COAP_OPTION_BLOCK2, &block2)) {
300 block2_requested = 1;
301 if (block2.num != 0 && length <= (block2.num << (block2.szx + 4))) {
302 coap_log_debug("Illegal block requested (%d > last = %zu)\n",
303 block2.num,
304 length >> (block2.szx + 4));
305 response->code = COAP_RESPONSE_CODE(400);
306 goto error;
307 }
308 }
309 }
310 response->code = COAP_RESPONSE_CODE(205);
311
312#if COAP_SERVER_SUPPORT
313 /* add ETag for the resource data */
314 if (length) {
315 dctx = coap_digest_setup();
316 if (!dctx)
317 goto error;
318 if (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 (%zu)\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 void *app_ptr,
782 int single_request, coap_pdu_code_t request_method) {
783
784 ssize_t avail;
785 coap_block_b_t block;
786#if COAP_Q_BLOCK_SUPPORT
787 coap_block_b_t alt_block;
788#endif /* COAP_Q_BLOCK_SUPPORT */
789 size_t chunk;
790 coap_lg_xmit_t *lg_xmit = NULL;
791 uint8_t buf[8];
792 int have_block_defined = 0;
793 uint8_t blk_size;
794 uint8_t max_blk_size;
795 uint16_t option;
796 size_t token_options;
797 coap_opt_t *opt;
798 coap_opt_iterator_t opt_iter;
799#if COAP_Q_BLOCK_SUPPORT
800 uint16_t alt_option;
801#endif /* COAP_Q_BLOCK_SUPPORT */
802
803#if !COAP_SERVER_SUPPORT
804 (void)etag;
805#endif /* COAP_SERVER_SUPPORT */
806
807 assert(pdu);
808 if (pdu->data) {
809 coap_log_warn("coap_add_data_large: PDU already contains data\n");
810 if (release_func) {
811 coap_lock_callback(release_func(session, app_ptr));
812 }
813 return 0;
814 }
815
816 if (!(session->block_mode & COAP_BLOCK_USE_LIBCOAP)) {
817 coap_log_debug("** %s: coap_add_data_large: COAP_BLOCK_USE_LIBCOAP not enabled\n",
818 coap_session_str(session));
819 goto add_data;
820 }
821
822 /* A lot of the reliable code assumes type is CON */
823 if (COAP_PROTO_RELIABLE(session->proto) && pdu->type == COAP_MESSAGE_NON)
824 pdu->type = COAP_MESSAGE_CON;
825
826 /* Block NUM max 20 bits and block size is "2**(SZX + 4)"
827 and using SZX max of 6 gives maximum size = 1,073,740,800
828 CSM Max-Message-Size theoretical maximum = 4,294,967,295
829 So, if using blocks, we are limited to 1,073,740,800.
830 */
831#define MAX_BLK_LEN (((1UL << 20) - 1) * (1 << (6 + 4)))
832
833#if UINT_MAX > MAX_BLK_LEN
834 if (length > MAX_BLK_LEN) {
835 coap_log_warn("Size of large buffer restricted to 0x%lx bytes\n", MAX_BLK_LEN);
836 length = MAX_BLK_LEN;
837 }
838#endif /* UINT_MAX > MAX_BLK_LEN */
839
840#if COAP_SERVER_SUPPORT
841 /* Possible response code not yet set, so check if not request */
842 if (!COAP_PDU_IS_REQUEST(pdu) && length) {
843 coap_opt_t *etag_opt = coap_check_option(pdu, COAP_OPTION_ETAG, &opt_iter);
844
845 if (etag_opt) {
846 /* Have to use ETag as supplied in the response PDU */
847 etag = coap_decode_var_bytes8(coap_opt_value(etag_opt),
848 coap_opt_length(etag_opt));
849 } else {
850 if (!etag) {
851 /* calculate ETag for the response */
852 coap_digest_t digest;
854
855 if (dctx) {
856 if (coap_is_mcast(&session->addr_info.local)) {
858 }
859 if (coap_digest_update(dctx, data, length)) {
860 if (coap_digest_final(dctx, &digest)) {
861 memcpy(&etag, digest.key, sizeof(etag));
862#if COAP_ETAG_MAX_BYTES != 8
863 etag = etag >> 8*(8 - COAP_ETAG_MAX_BYTES);
864#endif
865 dctx = NULL;
866 }
867 }
868 coap_digest_free(dctx);
869 }
870 if (!etag)
871 etag = 1;
872 }
875 coap_encode_var_safe8(buf, sizeof(buf), etag),
876 buf);
877 }
878 if (request) {
879 etag_opt = coap_check_option(request, COAP_OPTION_ETAG, &opt_iter);
880 if (etag_opt) {
881 /* There may be multiple ETag - need to check each one */
882 coap_option_iterator_init(request, &opt_iter, COAP_OPT_ALL);
883 while ((etag_opt = coap_option_next(&opt_iter))) {
884 if (opt_iter.number == COAP_OPTION_ETAG) {
885 uint64_t etag_r = coap_decode_var_bytes8(coap_opt_value(etag_opt),
886 coap_opt_length(etag_opt));
887
888 if (etag == etag_r) {
889 pdu->code = COAP_RESPONSE_CODE(203);
890 return 1;
891 }
892 }
893 }
894 }
895 }
896 }
897#endif /* COAP_SERVER_SUPPORT */
898
899 /* Determine the block size to use, adding in sensible options if needed */
900 if (COAP_PDU_IS_REQUEST(pdu)) {
902
903#if COAP_Q_BLOCK_SUPPORT
904 if (session->block_mode & (COAP_BLOCK_HAS_Q_BLOCK|COAP_BLOCK_TRY_Q_BLOCK)) {
905 option = COAP_OPTION_Q_BLOCK1;
906 alt_option = COAP_OPTION_BLOCK1;
907 } else {
908 option = COAP_OPTION_BLOCK1;
909 alt_option = COAP_OPTION_Q_BLOCK1;
910 }
911#else /* ! COAP_Q_BLOCK_SUPPORT */
912 if (coap_get_block_b(session, pdu, COAP_OPTION_Q_BLOCK1, &block)) {
914 }
915 option = COAP_OPTION_BLOCK1;
916#endif /* ! COAP_Q_BLOCK_SUPPORT */
917
918 /* See if this token is already in use for large bodies (unlikely) */
919 LL_FOREACH_SAFE(session->lg_xmit, lg_xmit, q) {
920 if (coap_binary_equal(&pdu->actual_token, lg_xmit->b.b1.app_token)) {
921 /* Unfortunately need to free this off as potential size change */
922 int is_mcast = 0;
923#if COAP_CLIENT_SUPPORT
924 is_mcast = coap_is_mcast(&lg_xmit->b.b1.upstream);
925#endif /* COAP_CLIENT_SUPPORT */
926 LL_DELETE(session->lg_xmit, lg_xmit);
927 coap_block_delete_lg_xmit(session, lg_xmit);
928 lg_xmit = NULL;
929 if (!is_mcast)
931 break;
932 }
933 }
934 } else {
935 /* Have to assume that it is a response even if code is 0.00 */
936 assert(resource);
937#if COAP_Q_BLOCK_SUPPORT
938 if (session->block_mode & COAP_BLOCK_HAS_Q_BLOCK) {
939 option = COAP_OPTION_Q_BLOCK2;
940 alt_option = COAP_OPTION_BLOCK2;
941 } else {
942 option = COAP_OPTION_BLOCK2;
943 alt_option = COAP_OPTION_Q_BLOCK2;
944 }
945#else /* ! COAP_Q_BLOCK_SUPPORT */
946 if (coap_get_block_b(session, pdu, COAP_OPTION_Q_BLOCK2, &block)) {
948 }
949 option = COAP_OPTION_BLOCK2;
950#endif /* ! COAP_Q_BLOCK_SUPPORT */
951#if COAP_SERVER_SUPPORT
952 /*
953 * Check if resource+query+rtag is already in use for large bodies
954 * (unlikely)
955 */
956 lg_xmit = coap_find_lg_xmit_response(session, request, resource, query);
957 if (lg_xmit) {
958 /* Unfortunately need to free this off as potential size change */
959 LL_DELETE(session->lg_xmit, lg_xmit);
960 coap_block_delete_lg_xmit(session, lg_xmit);
961 lg_xmit = NULL;
963 }
964#endif /* COAP_SERVER_SUPPORT */
965 }
966#if COAP_OSCORE_SUPPORT
967 if (session->oscore_encryption) {
968 /* Need to convert Proxy-Uri to Proxy-Scheme option if needed */
970 goto fail;
971 }
972#endif /* COAP_OSCORE_SUPPORT */
973
974 token_options = pdu->data ? (size_t)(pdu->data - pdu->token) : pdu->used_size;
975 avail = pdu->max_size - token_options;
976 /* There may be a response with Echo option */
978#if COAP_OSCORE_SUPPORT
979 avail -= coap_oscore_overhead(session, pdu);
980#endif /* COAP_OSCORE_SUPPORT */
981 /* May need token of length 8, so account for this */
982 avail -= (pdu->actual_token.length < 8) ? 8 - pdu->actual_token.length : 0;
983 blk_size = coap_flsll((long long)avail) - 4 - 1;
984 if (blk_size > 6)
985 blk_size = 6;
986
987 max_blk_size = COAP_BLOCK_MAX_SIZE_GET(session->block_mode);
988 if (max_blk_size && blk_size > max_blk_size)
989 blk_size = max_blk_size;
990
991 /* see if BlockX defined - if so update blk_size as given by app */
992 if (coap_get_block_b(session, pdu, option, &block)) {
993 if (block.szx < blk_size)
994 blk_size = block.szx;
995 have_block_defined = 1;
996 }
997#if COAP_Q_BLOCK_SUPPORT
998 /* see if alternate BlockX defined */
999 if (coap_get_block_b(session, pdu, alt_option, &alt_block)) {
1000 if (have_block_defined) {
1001 /* Cannot have both options set */
1002 coap_log_warn("Both BlockX and Q-BlockX cannot be set at the same time\n");
1003 coap_remove_option(pdu, alt_option);
1004 } else {
1005 block = alt_block;
1006 if (block.szx < blk_size)
1007 blk_size = block.szx;
1008 have_block_defined = 1;
1009 option = alt_option;
1010 }
1011 }
1012#endif /* COAP_Q_BLOCK_SUPPORT */
1013
1014 if (avail < 16 && ((ssize_t)length > avail || have_block_defined)) {
1015 /* bad luck, this is the smallest block size */
1016 coap_log_debug("not enough space, even the smallest block does not fit (2)\n");
1017 goto fail;
1018 }
1019
1020 chunk = (size_t)1 << (blk_size + 4);
1021 if ((have_block_defined && block.num != 0) || single_request ||
1022 ((session->block_mode & COAP_BLOCK_STLESS_BLOCK2) && session->type != COAP_SESSION_TYPE_CLIENT)) {
1023 /* App is defining a single block to send or we are stateless */
1024 size_t rem;
1025
1026 if (length >= block.num * chunk) {
1027#if COAP_SERVER_SUPPORT
1028 if (session->block_mode & COAP_BLOCK_STLESS_BLOCK2 && session->type != COAP_SESSION_TYPE_CLIENT) {
1029 /* We are running server stateless */
1032 coap_encode_var_safe(buf, sizeof(buf),
1033 (unsigned int)length),
1034 buf);
1035 if (request) {
1036 if (!coap_get_block_b(session, request, option, &block))
1037 block.num = 0;
1038 }
1039 if (!setup_block_b(session, pdu, &block, block.num,
1040 blk_size, length))
1041 goto fail;
1042
1043 /* Add in with requested block num, more bit and block size */
1045 option,
1046 coap_encode_var_safe(buf, sizeof(buf),
1047 (block.num << 4) | (block.m << 3) | block.aszx),
1048 buf);
1049 }
1050#endif /* COAP_SERVER_SUPPORT */
1051 rem = chunk;
1052 if (chunk > length - block.num * chunk)
1053 rem = length - block.num * chunk;
1054 if (!coap_add_data(pdu, rem, &data[block.num * chunk]))
1055 goto fail;
1056 }
1057 if (release_func) {
1058 coap_lock_callback(release_func(session, app_ptr));
1059 }
1060 } else if ((have_block_defined && length > chunk) || (ssize_t)length > avail) {
1061 /* Only add in lg_xmit if more than one block needs to be handled */
1062 size_t rem;
1063
1064 lg_xmit = coap_malloc_type(COAP_LG_XMIT, sizeof(coap_lg_xmit_t));
1065 if (!lg_xmit)
1066 goto fail;
1067
1068 /* Set up for displaying all the data in the pdu */
1069 pdu->body_data = data;
1070 pdu->body_length = length;
1071 coap_log_debug("PDU presented by app.\n");
1073 pdu->body_data = NULL;
1074 pdu->body_length = 0;
1075
1076 coap_log_debug("** %s: lg_xmit %p initialized\n",
1077 coap_session_str(session), (void *)lg_xmit);
1078 /* Update lg_xmit with large data information */
1079 memset(lg_xmit, 0, sizeof(coap_lg_xmit_t));
1080 lg_xmit->blk_size = blk_size;
1081 lg_xmit->option = option;
1082 lg_xmit->data_info = coap_malloc_type(COAP_STRING, sizeof(coap_lg_xmit_data_t));
1083 if (!lg_xmit->data_info)
1084 goto fail;
1085 lg_xmit->data_info->ref = 0;
1086 lg_xmit->data_info->data = data;
1087 lg_xmit->data_info->length = length;
1088#if COAP_Q_BLOCK_SUPPORT
1089 lg_xmit->non_timeout_random_ticks =
1091#endif /* COAP_Q_BLOCK_SUPPORT */
1092 lg_xmit->data_info->release_func = release_func;
1093 lg_xmit->data_info->app_ptr = app_ptr;
1094 pdu->lg_xmit = lg_xmit;
1095 coap_ticks(&lg_xmit->last_obs);
1096 coap_ticks(&lg_xmit->last_sent);
1097 if (COAP_PDU_IS_REQUEST(pdu)) {
1098 /* Need to keep original token for updating response PDUs */
1099 lg_xmit->b.b1.app_token = coap_new_binary(pdu->actual_token.length);
1100 if (!lg_xmit->b.b1.app_token)
1101 goto fail;
1102 memcpy(lg_xmit->b.b1.app_token->s, pdu->actual_token.s,
1103 pdu->actual_token.length);
1104 /*
1105 * Need to set up new token for use during transmits
1106 * RFC9177#section-5
1107 */
1108 lg_xmit->b.b1.count = 1;
1109 lg_xmit->b.b1.state_token = STATE_TOKEN_FULL(++session->tx_token,
1110 lg_xmit->b.b1.count);
1111 coap_address_copy(&lg_xmit->b.b1.upstream, &session->addr_info.remote);
1112 /*
1113 * Token will be updated in pdu later as original pdu may be needed in
1114 * coap_send()
1115 */
1118 coap_encode_var_safe(buf, sizeof(buf),
1119 (unsigned int)length),
1120 buf);
1121 if (!coap_check_option(pdu, COAP_OPTION_RTAG, &opt_iter))
1124 coap_encode_var_safe(buf, sizeof(buf),
1125 ++session->tx_rtag),
1126 buf);
1127 } else {
1128 /*
1129 * resource+query+rtag match is used for Block2 large body transmissions
1130 * token match is used for Block1 large body transmissions
1131 */
1132 lg_xmit->b.b2.resource = resource;
1133 if (query) {
1134 lg_xmit->b.b2.query = coap_new_string(query->length);
1135 if (lg_xmit->b.b2.query) {
1136 memcpy(lg_xmit->b.b2.query->s, query->s, query->length);
1137 }
1138 } else {
1139 lg_xmit->b.b2.query = NULL;
1140 }
1141 opt = coap_check_option(request, COAP_OPTION_RTAG, &opt_iter);
1142 if (opt) {
1143 lg_xmit->b.b2.rtag_length = (uint8_t)min(coap_opt_length(opt),
1144 sizeof(lg_xmit->b.b2.rtag));
1145 memcpy(lg_xmit->b.b2.rtag, coap_opt_value(opt), coap_opt_length(opt));
1146 lg_xmit->b.b2.rtag_set = 1;
1147 } else {
1148 lg_xmit->b.b2.rtag_set = 0;
1149 }
1150 lg_xmit->b.b2.etag = etag;
1151 lg_xmit->b.b2.request_method = request_method;
1152 if (maxage >= 0) {
1153 coap_tick_t now;
1154
1155 coap_ticks(&now);
1156 lg_xmit->b.b2.maxage_expire = coap_ticks_to_rt(now) + maxage;
1157 } else {
1158 lg_xmit->b.b2.maxage_expire = 0;
1159 }
1162 coap_encode_var_safe(buf, sizeof(buf),
1163 (unsigned int)length),
1164 buf);
1165 }
1166
1167 if (!setup_block_b(session, pdu, &block, block.num,
1168 blk_size, lg_xmit->data_info->length))
1169 goto fail;
1170
1171 /* Add in with requested block num, more bit and block size */
1173 lg_xmit->option,
1174 coap_encode_var_safe(buf, sizeof(buf),
1175 (block.num << 4) | (block.m << 3) | block.aszx),
1176 buf);
1177
1178 /* Reference PDU to use as a basis for all the subsequent blocks */
1179 lg_xmit->sent_pdu = coap_pdu_reference_lkd(pdu);
1180
1181 /* Check we still have space after adding in some options */
1182 token_options = pdu->data ? (size_t)(pdu->data - pdu->token) : pdu->used_size;
1183 avail = pdu->max_size - token_options;
1184 /* There may be a response with Echo option */
1186 /* May need token of length 8, so account for this */
1187 avail -= (pdu->actual_token.length < 8) ? 8 - pdu->actual_token.length : 0;
1188#if COAP_OSCORE_SUPPORT
1189 avail -= coap_oscore_overhead(session, pdu);
1190#endif /* COAP_OSCORE_SUPPORT */
1191 if (avail < (ssize_t)chunk) {
1192 /* chunk size change down */
1193 if (avail < 16) {
1194 coap_log_warn("not enough space, even the smallest block does not fit (3)\n");
1195 goto fail;
1196 }
1197 blk_size = coap_flsll((long long)avail) - 4 - 1;
1198 block.num = block.num << (lg_xmit->blk_size - blk_size);
1199 lg_xmit->blk_size = blk_size;
1200 chunk = (size_t)1 << (lg_xmit->blk_size + 4);
1201 block.chunk_size = (uint32_t)chunk;
1202 block.bert = 0;
1204 lg_xmit->option,
1205 coap_encode_var_safe(buf, sizeof(buf),
1206 (block.num << 4) | (block.m << 3) | lg_xmit->blk_size),
1207 buf);
1208 }
1209
1210 rem = block.chunk_size;
1211 if (rem > lg_xmit->data_info->length - block.num * chunk)
1212 rem = lg_xmit->data_info->length - block.num * chunk;
1213 if (!coap_add_data(pdu, rem, &data[block.num * chunk]))
1214 goto fail;
1215
1216 if (COAP_PDU_IS_REQUEST(pdu))
1217 lg_xmit->b.b1.bert_size = rem;
1218
1219 lg_xmit->last_block = -1;
1220
1221 /* Link the new lg_xmit in */
1222 LL_PREPEND(session->lg_xmit,lg_xmit);
1223 } else {
1224 /* No need to use blocks */
1225 if (have_block_defined) {
1227 option,
1228 coap_encode_var_safe(buf, sizeof(buf),
1229 (0 << 4) | (0 << 3) | blk_size), buf);
1230 }
1231add_data:
1232 if (!coap_add_data(pdu, length, data))
1233 goto fail;
1234
1235 if (release_func) {
1236 coap_lock_callback(release_func(session, app_ptr));
1237 }
1238 }
1239 return 1;
1240
1241fail:
1242 if (lg_xmit) {
1243 coap_block_delete_lg_xmit(session, lg_xmit);
1244 } else if (release_func) {
1245 coap_lock_callback(release_func(session, app_ptr));
1246 }
1247 return 0;
1248}
1249
1250#if COAP_CLIENT_SUPPORT
1251COAP_API int
1253 coap_pdu_t *pdu,
1254 size_t length,
1255 const uint8_t *data,
1256 coap_release_large_data_t release_func,
1257 void *app_ptr
1258 ) {
1259 int ret;
1260
1261 coap_lock_lock(return 0);
1262 ret = coap_add_data_large_request_lkd(session, pdu, length, data,
1263 release_func, app_ptr);
1265 return ret;
1266}
1267
1268int
1270 coap_pdu_t *pdu,
1271 size_t length,
1272 const uint8_t *data,
1273 coap_release_large_data_t release_func,
1274 void *app_ptr) {
1275 /*
1276 * Delay if session->doing_first is set.
1277 * E.g. Reliable and CSM not in yet for checking block support
1278 */
1279 if (coap_client_delay_first(session) == 0) {
1280 if (release_func) {
1281 coap_lock_callback(release_func(session, app_ptr));
1282 }
1283 return 0;
1284 }
1285 return coap_add_data_large_internal(session, NULL, pdu, NULL, NULL, -1, 0,
1286 length, data, release_func, app_ptr, 0, 0);
1287}
1288#endif /* ! COAP_CLIENT_SUPPORT */
1289
1290#if COAP_SERVER_SUPPORT
1291COAP_API int
1293 coap_session_t *session,
1294 const coap_pdu_t *request,
1295 coap_pdu_t *response,
1296 const coap_string_t *query,
1297 uint16_t media_type,
1298 int maxage,
1299 uint64_t etag,
1300 size_t length,
1301 const uint8_t *data,
1302 coap_release_large_data_t release_func,
1303 void *app_ptr
1304 ) {
1305 int ret;
1306
1307 coap_lock_lock(return 0);
1308 ret = coap_add_data_large_response_lkd(resource, session, request,
1309 response, query, media_type, maxage, etag,
1310 length, data, release_func, app_ptr);
1312 return ret;
1313}
1314
1315int
1317 coap_session_t *session,
1318 const coap_pdu_t *request,
1319 coap_pdu_t *response,
1320 const coap_string_t *query,
1321 uint16_t media_type,
1322 int maxage,
1323 uint64_t etag,
1324 size_t length,
1325 const uint8_t *data,
1326 coap_release_large_data_t release_func,
1327 void *app_ptr
1328 ) {
1329 unsigned char buf[4];
1330 coap_block_b_t block;
1331 int block_requested = 0;
1332 int single_request = 0;
1333#if COAP_Q_BLOCK_SUPPORT
1334 uint32_t block_opt = (session->block_mode & COAP_BLOCK_HAS_Q_BLOCK) ?
1336#else /* ! COAP_Q_BLOCK_SUPPORT */
1337 uint16_t block_opt = COAP_OPTION_BLOCK2;
1338#endif /* ! COAP_Q_BLOCK_SUPPORT */
1339
1340 memset(&block, 0, sizeof(block));
1341 /*
1342 * Need to check that a valid block is getting asked for so that the
1343 * correct options are put into the PDU.
1344 */
1345 if (request) {
1346 if (coap_get_block_b(session, request, COAP_OPTION_BLOCK2, &block)) {
1347 block_requested = 1;
1348 if (block.num != 0 && length <= (block.num << (block.szx + 4))) {
1349 coap_log_debug("Illegal block requested (%d > last = %zu)\n",
1350 block.num,
1351 length >> (block.szx + 4));
1352 response->code = COAP_RESPONSE_CODE(400);
1353 goto error;
1354 }
1355 }
1356#if COAP_Q_BLOCK_SUPPORT
1357 else if (coap_get_block_b(session, request, COAP_OPTION_Q_BLOCK2, &block)) {
1358 block_requested = 1;
1359 if (block.num != 0 && length <= (block.num << (block.szx + 4))) {
1360 coap_log_debug("Illegal block requested (%d > last = %zu)\n",
1361 block.num,
1362 length >> (block.szx + 4));
1363 response->code = COAP_RESPONSE_CODE(400);
1364 goto error;
1365 }
1366 if (!(session->block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
1367 set_block_mode_has_q(session->block_mode);
1368 block_opt = COAP_OPTION_Q_BLOCK2;
1369 }
1370 if (block.m == 0)
1371 single_request = 1;
1372 }
1373#endif /* COAP_Q_BLOCK_SUPPORT */
1374 }
1375
1377 coap_encode_var_safe(buf, sizeof(buf),
1378 media_type),
1379 buf);
1380
1381 if (maxage >= 0) {
1382 coap_update_option(response,
1384 coap_encode_var_safe(buf, sizeof(buf), maxage), buf);
1385 }
1386
1387 if (block_requested) {
1388 int res;
1389
1390 res = coap_write_block_b_opt(session, &block, block_opt, response,
1391 length);
1392
1393 switch (res) {
1394 case -2: /* illegal block (caught above) */
1395 response->code = COAP_RESPONSE_CODE(400);
1396 goto error;
1397 case -1: /* should really not happen */
1398 assert(0);
1399 /* fall through if assert is a no-op */
1400 case -3: /* cannot handle request */
1401 response->code = COAP_RESPONSE_CODE(500);
1402 goto error;
1403 default: /* everything is good */
1404 ;
1405 }
1406 }
1407
1408 /* add data body */
1409 if (request &&
1410 !coap_add_data_large_internal(session, request, response, resource,
1411 query, maxage, etag, length, data,
1412 release_func, app_ptr, single_request,
1413 request->code)) {
1414 response->code = COAP_RESPONSE_CODE(500);
1415 goto error_released;
1416 }
1417
1418 return 1;
1419
1420error:
1421 if (release_func) {
1422 coap_lock_callback(release_func(session, app_ptr));
1423 }
1424error_released:
1425#if COAP_ERROR_PHRASE_LENGTH > 0
1426 coap_add_data(response,
1427 strlen(coap_response_phrase(response->code)),
1428 (const unsigned char *)coap_response_phrase(response->code));
1429#endif /* COAP_ERROR_PHRASE_LENGTH > 0 */
1430 return 0;
1431}
1432#endif /* ! COAP_SERVER_SUPPORT */
1433
1434/*
1435 * return 1 if there is a future expire time, else 0.
1436 * update tim_rem with remaining value if return is 1.
1437 */
1438int
1440 coap_tick_t *tim_rem) {
1441 coap_lg_xmit_t *lg_xmit;
1442 coap_lg_xmit_t *q;
1443#if COAP_Q_BLOCK_SUPPORT
1444 coap_tick_t idle_timeout = 4 * COAP_NON_TIMEOUT_TICKS(session);
1445#else /* ! COAP_Q_BLOCK_SUPPORT */
1446 coap_tick_t idle_timeout = 8 * COAP_TICKS_PER_SECOND;
1447#endif /* ! COAP_Q_BLOCK_SUPPORT */
1448 coap_tick_t partial_timeout = COAP_MAX_TRANSMIT_WAIT_TICKS(session);
1449 int ret = 0;
1450
1451 *tim_rem = COAP_MAX_DELAY_TICKS;
1452
1453 LL_FOREACH_SAFE(session->lg_xmit, lg_xmit, q) {
1454 if (lg_xmit->last_all_sent) {
1455 if (lg_xmit->last_all_sent + idle_timeout <= now) {
1456 /* Expire this entry */
1457 LL_DELETE(session->lg_xmit, lg_xmit);
1458 coap_block_delete_lg_xmit(session, lg_xmit);
1459 } else {
1460 /* Delay until the lg_xmit needs to expire */
1461 if (*tim_rem > lg_xmit->last_all_sent + idle_timeout - now) {
1462 *tim_rem = lg_xmit->last_all_sent + idle_timeout - now;
1463 ret = 1;
1464 }
1465 }
1466 } else if (lg_xmit->last_sent) {
1467 if (lg_xmit->last_sent + partial_timeout <= now) {
1468 /* Expire this entry */
1469 int is_mcast = 0;
1470#if COAP_CLIENT_SUPPORT
1471 is_mcast = COAP_PDU_IS_REQUEST(lg_xmit->sent_pdu) &&
1472 coap_is_mcast(&lg_xmit->b.b1.upstream);
1473#endif /* COAP_CLIENT_SUPPORT */
1474 LL_DELETE(session->lg_xmit, lg_xmit);
1475
1476 coap_block_delete_lg_xmit(session, lg_xmit);
1477 if (!is_mcast)
1479 } else {
1480 /* Delay until the lg_xmit needs to expire */
1481 if (*tim_rem > lg_xmit->last_sent + partial_timeout - now) {
1482 *tim_rem = lg_xmit->last_sent + partial_timeout - now;
1483 ret = 1;
1484 }
1485 }
1486 }
1487 }
1488 return ret;
1489}
1490
1491#if COAP_CLIENT_SUPPORT
1492#if COAP_Q_BLOCK_SUPPORT
1493static coap_pdu_t *
1494coap_build_missing_pdu(coap_session_t *session, coap_lg_crcv_t *lg_crcv) {
1495 coap_pdu_t *pdu;
1496 coap_opt_filter_t drop_options;
1497 uint64_t token = STATE_TOKEN_FULL(lg_crcv->state_token, ++lg_crcv->retry_counter);
1498 uint8_t buf[8];
1499 size_t len = coap_encode_var_safe8(buf, sizeof(token), token);
1500
1501 memset(&drop_options, 0, sizeof(coap_opt_filter_t));
1505 pdu = coap_pdu_duplicate_lkd(lg_crcv->sent_pdu, session, len, buf,
1506 &drop_options);
1507 if (!pdu)
1508 return NULL;
1509 pdu->type = lg_crcv->last_type;
1510 return pdu;
1511}
1512
1513static void
1514coap_request_missing_q_block2(coap_session_t *session, coap_lg_crcv_t *lg_crcv) {
1515 uint8_t buf[8];
1516 uint32_t i;
1517 int block = -1; /* Last one seen */
1518 size_t sofar;
1519 size_t block_size;
1520 coap_pdu_t *pdu = NULL;
1521 int block_payload_set = -1;
1522
1523 if (session->block_mode & COAP_BLOCK_USE_M_Q_BLOCK) {
1524 /*
1525 * See if it is safe to use the single 'M' block variant of request
1526 *
1527 * If any blocks seen, then missing blocks are after range[0].end and
1528 * terminate on the last block or before range[1].begin if set.
1529 * If not defined or range[1].begin is in a different payload set then
1530 * safe to use M bit.
1531 */
1532 if (lg_crcv->rec_blocks.used &&
1533 (lg_crcv->rec_blocks.used < 2 ||
1534 ((lg_crcv->rec_blocks.range[0].end + 1) / COAP_MAX_PAYLOADS(session) !=
1535 (lg_crcv->rec_blocks.range[1].begin -1) / COAP_MAX_PAYLOADS(session)))) {
1536 block = lg_crcv->rec_blocks.range[0].end + 1;
1537 block_size = (size_t)1 << (lg_crcv->szx + 4);
1538 sofar = block * block_size;
1539 if (sofar < lg_crcv->total_len) {
1540 /* Ask for missing blocks */
1541 if (pdu == NULL) {
1542 pdu = coap_build_missing_pdu(session, lg_crcv);
1543 if (!pdu)
1544 return;
1545 }
1547 coap_encode_var_safe(buf, sizeof(buf),
1548 (block << 4) | (1 << 3) | lg_crcv->szx),
1549 buf);
1550 block_payload_set = block / COAP_MAX_PAYLOADS(session);
1551 goto send_it;
1552 }
1553 }
1554 }
1555 block = -1;
1556 for (i = 0; i < lg_crcv->rec_blocks.used; i++) {
1557 if (block < (int)lg_crcv->rec_blocks.range[i].begin &&
1558 lg_crcv->rec_blocks.range[i].begin != 0) {
1559 /* Ask for missing blocks */
1560 if (pdu == NULL) {
1561 pdu = coap_build_missing_pdu(session, lg_crcv);
1562 if (!pdu)
1563 continue;
1564 }
1565 block++;
1566 if (block_payload_set == -1)
1567 block_payload_set = block / COAP_MAX_PAYLOADS(session);
1568 for (; block < (int)lg_crcv->rec_blocks.range[i].begin &&
1569 block_payload_set == (block / COAP_MAX_PAYLOADS(session)); block++) {
1571 coap_encode_var_safe(buf, sizeof(buf),
1572 (block << 4) | (0 << 3) | lg_crcv->szx),
1573 buf);
1574 }
1575 }
1576 if (block < (int)lg_crcv->rec_blocks.range[i].end) {
1577 block = lg_crcv->rec_blocks.range[i].end;
1578 }
1579 }
1580 block_size = (size_t)1 << (lg_crcv->szx + 4);
1581 sofar = (block + 1) * block_size;
1582 if (sofar < lg_crcv->total_len) {
1583 /* Ask for trailing missing blocks */
1584 if (pdu == NULL) {
1585 pdu = coap_build_missing_pdu(session, lg_crcv);
1586 if (!pdu)
1587 return;
1588 }
1589 sofar = (lg_crcv->total_len + block_size - 1)/block_size;
1590 block++;
1591 if (block_payload_set == -1)
1592 block_payload_set = block / COAP_MAX_PAYLOADS(session);
1593 for (; block < (ssize_t)sofar &&
1594 block_payload_set == (block / COAP_MAX_PAYLOADS(session)); block++) {
1596 coap_encode_var_safe(buf, sizeof(buf),
1597 (block << 4) | (0 << 3) | lg_crcv->szx),
1598 buf);
1599 }
1600 }
1601send_it:
1602 if (pdu)
1603 coap_send_internal(session, pdu, NULL);
1604 lg_crcv->rec_blocks.retry++;
1605 if (block_payload_set != -1)
1606 lg_crcv->rec_blocks.processing_payload_set = block_payload_set;
1607 coap_ticks(&lg_crcv->rec_blocks.last_seen);
1608}
1609#endif /* COAP_Q_BLOCK_SUPPORT */
1610
1611/*
1612 * return 1 if there is a future expire time, else 0.
1613 * update tim_rem with remaining value if return is 1.
1614 */
1615int
1617 coap_tick_t *tim_rem) {
1618 coap_lg_crcv_t *lg_crcv;
1619 coap_lg_crcv_t *q;
1620 coap_tick_t partial_timeout;
1621#if COAP_Q_BLOCK_SUPPORT
1622 coap_tick_t receive_timeout = COAP_NON_RECEIVE_TIMEOUT_TICKS(session);
1623#endif /* COAP_Q_BLOCK_SUPPORT */
1624 int ret = 0;
1625
1626 *tim_rem = COAP_MAX_DELAY_TICKS;
1627#if COAP_Q_BLOCK_SUPPORT
1628 if (COAP_PROTO_NOT_RELIABLE(session->proto) &&
1629 session->block_mode & COAP_BLOCK_HAS_Q_BLOCK)
1630 partial_timeout = COAP_NON_PARTIAL_TIMEOUT_TICKS(session);
1631 else
1632#endif /* COAP_Q_BLOCK_SUPPORT */
1633 partial_timeout = COAP_MAX_TRANSMIT_WAIT_TICKS(session);
1634
1635 LL_FOREACH_SAFE(session->lg_crcv, lg_crcv, q) {
1636 if (COAP_PROTO_RELIABLE(session->proto) || lg_crcv->last_type != COAP_MESSAGE_NON)
1637 goto check_expire;
1638
1639#if COAP_Q_BLOCK_SUPPORT
1640 if (lg_crcv->block_option == COAP_OPTION_Q_BLOCK2 && lg_crcv->rec_blocks.used) {
1641 size_t scaled_timeout = receive_timeout *
1642 ((size_t)1 << lg_crcv->rec_blocks.retry);
1643
1644 if (lg_crcv->rec_blocks.retry >= COAP_NON_MAX_RETRANSMIT(session)) {
1645 /* Done NON_MAX_RETRANSMIT retries */
1646 coap_handle_nack(session, lg_crcv->sent_pdu,
1648 goto expire;
1649 }
1650 if (lg_crcv->rec_blocks.last_seen + scaled_timeout <= now) {
1651 coap_request_missing_q_block2(session, lg_crcv);
1652 } else {
1653 if (*tim_rem > lg_crcv->rec_blocks.last_seen + scaled_timeout - now) {
1654 *tim_rem = lg_crcv->rec_blocks.last_seen + scaled_timeout - now;
1655 ret = 1;
1656 }
1657 }
1658 }
1659#endif /* COAP_Q_BLOCK_SUPPORT */
1660 /* Used for Block2 and Q-Block2 */
1661check_expire:
1662 if (!lg_crcv->observe_set && lg_crcv->last_used &&
1663 lg_crcv->last_used + partial_timeout <= now) {
1664#if COAP_Q_BLOCK_SUPPORT
1665expire:
1666#endif /* COAP_Q_BLOCK_SUPPORT */
1667 /* Expire this entry */
1668 LL_DELETE(session->lg_crcv, lg_crcv);
1669 coap_block_delete_lg_crcv(session, lg_crcv);
1670 } else if (!lg_crcv->observe_set && lg_crcv->last_used) {
1671 /* Delay until the lg_crcv needs to expire */
1672 if (*tim_rem > lg_crcv->last_used + partial_timeout - now) {
1673 *tim_rem = lg_crcv->last_used + partial_timeout - now;
1674 ret = 1;
1675 }
1676 }
1677 }
1678 return ret;
1679}
1680#endif /* COAP_CLIENT_SUPPORT */
1681
1682#if COAP_SERVER_SUPPORT
1683#if COAP_Q_BLOCK_SUPPORT
1684static coap_pdu_t *
1685pdu_408_build(coap_session_t *session, coap_lg_srcv_t *lg_srcv) {
1686 coap_pdu_t *pdu;
1687 uint8_t buf[4];
1688
1690 COAP_RESPONSE_CODE(408),
1691 coap_new_message_id_lkd(session),
1693 if (!pdu)
1694 return NULL;
1695 if (lg_srcv->last_token)
1696 coap_add_token(pdu, lg_srcv->last_token->length, lg_srcv->last_token->s);
1698 coap_encode_var_safe(buf, sizeof(buf),
1700 buf);
1701 pdu->token[pdu->used_size++] = COAP_PAYLOAD_START;
1702 pdu->data = pdu->token + pdu->used_size;
1703 return pdu;
1704}
1705
1706static int
1707add_408_block(coap_pdu_t *pdu, int block) {
1708 size_t len;
1709 uint8_t val[8];
1710
1711 assert(block >= 0 && block < (1 << 20));
1712
1713 if (block < 0 || block >= (1 << 20)) {
1714 return 0;
1715 } else if (block < 24) {
1716 len = 1;
1717 val[0] = block;
1718 } else if (block < 0x100) {
1719 len = 2;
1720 val[0] = 24;
1721 val[1] = block;
1722 } else if (block < 0x10000) {
1723 len = 3;
1724 val[0] = 25;
1725 val[1] = block >> 8;
1726 val[2] = block & 0xff;
1727 } else { /* Largest block number is 2^^20 - 1 */
1728 len = 4;
1729 val[0] = 26;
1730 val[1] = block >> 16;
1731 val[2] = (block >> 8) & 0xff;
1732 val[3] = block & 0xff;
1733 }
1734 if (coap_pdu_check_resize(pdu, pdu->used_size + len)) {
1735 memcpy(&pdu->token[pdu->used_size], val, len);
1736 pdu->used_size += len;
1737 return 1;
1738 }
1739 return 0;
1740}
1741#endif /* COAP_Q_BLOCK_SUPPORT */
1742#endif /* COAP_SERVER_SUPPORT */
1743
1744static int
1745check_if_received_block(coap_rblock_t *rec_blocks, uint32_t block_num) {
1746 uint32_t i;
1747
1748 for (i = 0; i < rec_blocks->used; i++) {
1749 if (block_num < rec_blocks->range[i].begin)
1750 return 0;
1751 if (block_num <= rec_blocks->range[i].end)
1752 return 1;
1753 }
1754 return 0;
1755}
1756
1757#if COAP_SERVER_SUPPORT
1758static int
1759check_if_next_block(coap_rblock_t *rec_blocks, uint32_t block_num) {
1760 if (rec_blocks->used == 0) {
1761 return block_num == 0 ? 1 : 0;
1762 }
1763 if (rec_blocks->range[rec_blocks->used-1].end + 1 == block_num)
1764 return 1;
1765
1766 return 0;
1767}
1768#endif /* COAP_SERVER_SUPPORT */
1769
1770static int
1772 uint32_t i;
1773 uint32_t block = 0;
1774
1775 if (rec_blocks->total_blocks == 0) {
1776 /* Not seen block with More bit unset yet */
1777 return 0;
1778 }
1779
1780 for (i = 0; i < rec_blocks->used; i++) {
1781 if (block < rec_blocks->range[i].begin)
1782 return 0;
1783 if (block < rec_blocks->range[i].end)
1784 block = rec_blocks->range[i].end;
1785 }
1786 return 1;
1787}
1788
1789#if COAP_CLIENT_SUPPORT
1790#if COAP_Q_BLOCK_SUPPORT
1791static int
1792check_all_blocks_in_for_payload_set(coap_session_t *session,
1793 coap_rblock_t *rec_blocks) {
1794 if (rec_blocks->used &&
1795 (rec_blocks->range[0].end + 1) / COAP_MAX_PAYLOADS(session) >
1796 rec_blocks->processing_payload_set)
1797 return 1;
1798 return 0;
1799}
1800
1801static int
1802check_any_blocks_next_payload_set(coap_session_t *session,
1803 coap_rblock_t *rec_blocks) {
1804 if (rec_blocks->used > 1 &&
1805 rec_blocks->range[1].begin / COAP_MAX_PAYLOADS(session) ==
1806 rec_blocks->processing_payload_set)
1807 return 1;
1808 return 0;
1809}
1810#endif /* COAP_Q_BLOCK_SUPPORT */
1811#endif /* COAP_CLIENT_SUPPORT */
1812
1813#if COAP_SERVER_SUPPORT
1814/*
1815 * return 1 if there is a future expire time, else 0.
1816 * update tim_rem with remaining value if return is 1.
1817 */
1818int
1820 coap_tick_t *tim_rem) {
1821 coap_lg_srcv_t *lg_srcv;
1822 coap_lg_srcv_t *q;
1823 coap_tick_t partial_timeout;
1824#if COAP_Q_BLOCK_SUPPORT
1825 coap_tick_t receive_timeout = COAP_NON_RECEIVE_TIMEOUT_TICKS(session);
1826#endif /* COAP_Q_BLOCK_SUPPORT */
1827 int ret = 0;
1828
1829 *tim_rem = COAP_MAX_DELAY_TICKS;
1830#if COAP_Q_BLOCK_SUPPORT
1831 if (COAP_PROTO_NOT_RELIABLE(session->proto) &&
1832 session->block_mode & COAP_BLOCK_HAS_Q_BLOCK)
1833 partial_timeout = COAP_NON_PARTIAL_TIMEOUT_TICKS(session);
1834 else
1835#endif /* COAP_Q_BLOCK_SUPPORT */
1836 partial_timeout = COAP_MAX_TRANSMIT_WAIT_TICKS(session);
1837
1838 LL_FOREACH_SAFE(session->lg_srcv, lg_srcv, q) {
1839 if (lg_srcv->dont_timeout) {
1840 /* Not safe to timeout at present */
1841 continue;
1842 }
1843 if (COAP_PROTO_RELIABLE(session->proto) || lg_srcv->last_type != COAP_MESSAGE_NON)
1844 goto check_expire;
1845
1846#if COAP_Q_BLOCK_SUPPORT
1847 if (lg_srcv->block_option == COAP_OPTION_Q_BLOCK1 && lg_srcv->rec_blocks.used) {
1848 size_t scaled_timeout = receive_timeout *
1849 ((size_t)1 << lg_srcv->rec_blocks.retry);
1850
1851 if (lg_srcv->rec_blocks.retry >= COAP_NON_MAX_RETRANSMIT(session)) {
1852 /* Done NON_MAX_RETRANSMIT retries */
1853 goto expire;
1854 }
1855 if (lg_srcv->rec_blocks.last_seen + scaled_timeout <= now) {
1856 uint32_t i;
1857 int block = -1; /* Last one seen */
1858 size_t block_size = (size_t)1 << (lg_srcv->szx + 4);
1859 size_t final_block = (lg_srcv->total_len + block_size - 1)/block_size - 1;
1860 size_t cur_payload;
1861 size_t last_payload_block;
1862 coap_pdu_t *pdu = NULL;
1863 size_t no_blocks = 0;
1864
1865 /* Need to count the number of missing blocks */
1866 for (i = 0; i < lg_srcv->rec_blocks.used; i++) {
1867 if (block < (int)lg_srcv->rec_blocks.range[i].begin &&
1868 lg_srcv->rec_blocks.range[i].begin != 0) {
1869 block++;
1870 no_blocks += lg_srcv->rec_blocks.range[i].begin - block;
1871 }
1872 if (block < (int)lg_srcv->rec_blocks.range[i].end) {
1873 block = lg_srcv->rec_blocks.range[i].end;
1874 }
1875 }
1876 if (no_blocks == 0 && block == (int)final_block)
1877 goto expire;
1878
1879 /* Include missing up to end of current payload or total amount */
1880 cur_payload = block / COAP_MAX_PAYLOADS(session);
1881 last_payload_block = (cur_payload + 1) * COAP_MAX_PAYLOADS(session) - 1;
1882 if (final_block > last_payload_block) {
1883 final_block = last_payload_block;
1884 }
1885 no_blocks += final_block - block;
1886 if (no_blocks == 0) {
1887 /* Add in the blocks out of the next payload */
1888 final_block = (lg_srcv->total_len + block_size - 1)/block_size - 1;
1889 last_payload_block += COAP_MAX_PAYLOADS(session);
1890 if (final_block > last_payload_block) {
1891 final_block = last_payload_block;
1892 }
1893 }
1894 /* Ask for the missing blocks */
1895 block = -1;
1896 for (i = 0; i < lg_srcv->rec_blocks.used; i++) {
1897 if (block < (int)lg_srcv->rec_blocks.range[i].begin &&
1898 lg_srcv->rec_blocks.range[i].begin != 0) {
1899 /* Report on missing blocks */
1900 if (pdu == NULL) {
1901 pdu = pdu_408_build(session, lg_srcv);
1902 if (!pdu)
1903 continue;
1904 }
1905 block++;
1906 for (; block < (int)lg_srcv->rec_blocks.range[i].begin; block++) {
1907 if (!add_408_block(pdu, block)) {
1908 break;
1909 }
1910 }
1911 }
1912 if (block < (int)lg_srcv->rec_blocks.range[i].end) {
1913 block = lg_srcv->rec_blocks.range[i].end;
1914 }
1915 }
1916 block++;
1917 for (; block <= (int)final_block; block++) {
1918 if (pdu == NULL) {
1919 pdu = pdu_408_build(session, lg_srcv);
1920 if (!pdu)
1921 continue;
1922 }
1923 if (!add_408_block(pdu, block)) {
1924 break;
1925 }
1926 }
1927 if (pdu)
1928 coap_send_internal(session, pdu, NULL);
1929 lg_srcv->rec_blocks.retry++;
1930 coap_ticks(&lg_srcv->rec_blocks.last_seen);
1931 }
1932 if (*tim_rem > lg_srcv->rec_blocks.last_seen + scaled_timeout - now) {
1933 *tim_rem = lg_srcv->rec_blocks.last_seen + scaled_timeout - now;
1934 ret = 1;
1935 }
1936 }
1937#endif /* COAP_Q_BLOCK_SUPPORT */
1938 /* Used for Block1 and Q-Block1 */
1939check_expire:
1940 if (lg_srcv->no_more_seen)
1941 partial_timeout = 10 * COAP_TICKS_PER_SECOND;
1942 if (lg_srcv->last_used && lg_srcv->last_used + partial_timeout <= now) {
1943#if COAP_Q_BLOCK_SUPPORT
1944expire:
1945#endif /* COAP_Q_BLOCK_SUPPORT */
1946 /* Expire this entry */
1947 if (lg_srcv->no_more_seen && lg_srcv->block_option == COAP_OPTION_BLOCK1) {
1948 /*
1949 * Need to send a separate 4.08 to indicate missing blocks
1950 * Using NON is permissable as per
1951 * https://datatracker.ietf.org/doc/html/rfc7252#section-5.2.3
1952 */
1953 coap_pdu_t *pdu;
1954
1956 COAP_RESPONSE_CODE(408),
1957 coap_new_message_id_lkd(session),
1959 if (pdu) {
1960 if (lg_srcv->last_token)
1961 coap_add_token(pdu, lg_srcv->last_token->length, lg_srcv->last_token->s);
1962 coap_add_data(pdu, sizeof("Missing interim block")-1,
1963 (const uint8_t *)"Missing interim block");
1964 coap_send_internal(session, pdu, NULL);
1965 }
1966 }
1967 LL_DELETE(session->lg_srcv, lg_srcv);
1968 coap_block_delete_lg_srcv(session, lg_srcv);
1969 } else if (lg_srcv->last_used) {
1970 /* Delay until the lg_srcv needs to expire */
1971 if (*tim_rem > lg_srcv->last_used + partial_timeout - now) {
1972 *tim_rem = lg_srcv->last_used + partial_timeout - now;
1973 ret = 1;
1974 }
1975 }
1976 }
1977 return ret;
1978}
1979#endif /* COAP_SERVER_SUPPORT */
1980
1981#if COAP_Q_BLOCK_SUPPORT
1982/*
1983 * pdu is always released before return IF COAP_SEND_INC_PDU
1984 */
1986coap_send_q_blocks(coap_session_t *session,
1987 coap_lg_xmit_t *lg_xmit,
1988 coap_block_b_t block,
1989 coap_pdu_t *pdu,
1990 coap_send_pdu_t send_pdu) {
1991 coap_pdu_t *block_pdu = NULL;
1992 coap_opt_filter_t drop_options;
1994 uint64_t token;
1995 const uint8_t *ptoken;
1996 uint8_t ltoken[8];
1997 size_t ltoken_length;
1998 uint32_t delayqueue_cnt = 0;
1999
2000 if (!lg_xmit) {
2001 if (send_pdu == COAP_SEND_INC_PDU)
2002 return coap_send_internal(session, pdu, NULL);
2003 return COAP_INVALID_MID;
2004 }
2005
2006 if (pdu->type == COAP_MESSAGE_CON) {
2007 coap_queue_t *delayqueue;
2008
2009 delayqueue_cnt = session->con_active +
2010 (send_pdu == COAP_SEND_INC_PDU ? 1 : 0);
2011 LL_FOREACH(session->delayqueue, delayqueue) {
2012 delayqueue_cnt++;
2013 }
2014 }
2015 pdu->lg_xmit = lg_xmit;
2016 if (block.m &&
2017 ((pdu->type == COAP_MESSAGE_NON &&
2018 ((block.num + 1) % COAP_MAX_PAYLOADS(session)) + 1 !=
2019 COAP_MAX_PAYLOADS(session)) ||
2020 (pdu->type == COAP_MESSAGE_ACK &&
2021 lg_xmit->option == COAP_OPTION_Q_BLOCK2) ||
2022 (pdu->type == COAP_MESSAGE_CON &&
2023 delayqueue_cnt < COAP_NSTART(session)) ||
2024 COAP_PROTO_RELIABLE(session->proto))) {
2025 /* Allocate next pdu if there is headroom */
2026 if (COAP_PDU_IS_RESPONSE(pdu)) {
2027 ptoken = pdu->actual_token.s;
2028 ltoken_length = pdu->actual_token.length;
2029 } else {
2030 token = STATE_TOKEN_FULL(lg_xmit->b.b1.state_token,++lg_xmit->b.b1.count);
2031 ltoken_length = coap_encode_var_safe8(ltoken, sizeof(token), token);
2032 ptoken = ltoken;
2033 }
2034
2035 memset(&drop_options, 0, sizeof(coap_opt_filter_t));
2036 coap_option_filter_set(&drop_options, lg_xmit->option);
2037 block_pdu = coap_pdu_duplicate_lkd(pdu, session,
2038 ltoken_length,
2039 ptoken, &drop_options);
2040 if (block_pdu->type == COAP_MESSAGE_ACK)
2041 block_pdu->type = COAP_MESSAGE_CON;
2042 }
2043
2044 /* Send initial pdu (which deletes 'pdu') */
2045 if (send_pdu == COAP_SEND_INC_PDU &&
2046 (mid = coap_send_internal(session, pdu, NULL)) == COAP_INVALID_MID) {
2047 /* Not expected, underlying issue somewhere */
2048 coap_delete_pdu_lkd(block_pdu);
2049 return COAP_INVALID_MID;
2050 }
2051
2052 while (block_pdu) {
2053 coap_pdu_t *t_pdu = NULL;
2054 uint8_t buf[8];
2055 size_t chunk = ((size_t)1 << (lg_xmit->blk_size + 4));
2056
2057 block.num++;
2058 lg_xmit->offset = block.num * chunk;
2059 block.m = lg_xmit->offset + chunk < lg_xmit->data_info->length;
2060 if (block.m && ((block_pdu->type == COAP_MESSAGE_NON &&
2061 (block.num % COAP_MAX_PAYLOADS(session)) + 1 !=
2062 COAP_MAX_PAYLOADS(session)) ||
2063 (block_pdu->type == COAP_MESSAGE_CON &&
2064 delayqueue_cnt + 1 < COAP_NSTART(session)) ||
2065 COAP_PROTO_RELIABLE(session->proto))) {
2066 /*
2067 * Send following block if
2068 * NON and more in MAX_PAYLOADS
2069 * CON and NSTART allows it (based on number in delayqueue)
2070 * Reliable transport
2071 */
2072 if (COAP_PDU_IS_RESPONSE(block_pdu)) {
2073 ptoken = block_pdu->actual_token.s;
2074 ltoken_length = block_pdu->actual_token.length;
2075 } else {
2076 token = STATE_TOKEN_FULL(lg_xmit->b.b1.state_token,++lg_xmit->b.b1.count);
2077 ltoken_length = coap_encode_var_safe8(ltoken, sizeof(token), token);
2078 ptoken = ltoken;
2079 }
2080 t_pdu = coap_pdu_duplicate_lkd(block_pdu, session,
2081 ltoken_length, ptoken, &drop_options);
2082 }
2083 if (!coap_update_option(block_pdu, lg_xmit->option,
2085 sizeof(buf),
2086 ((block.num) << 4) |
2087 (block.m << 3) |
2088 block.szx),
2089 buf)) {
2090 coap_log_warn("Internal update issue option\n");
2091 coap_delete_pdu_lkd(block_pdu);
2092 coap_delete_pdu_lkd(t_pdu);
2093 break;
2094 }
2095
2096 if (!coap_add_block(block_pdu,
2097 lg_xmit->data_info->length,
2098 lg_xmit->data_info->data,
2099 block.num,
2100 block.szx)) {
2101 coap_log_warn("Internal update issue data\n");
2102 coap_delete_pdu_lkd(block_pdu);
2103 coap_delete_pdu_lkd(t_pdu);
2104 break;
2105 }
2106 if (COAP_PDU_IS_RESPONSE(block_pdu)) {
2107 lg_xmit->last_block = block.num;
2108 }
2109 mid = coap_send_internal(session, block_pdu, NULL);
2110 if (mid == COAP_INVALID_MID) {
2111 /* Not expected, underlying issue somewhere */
2112 coap_delete_pdu_lkd(t_pdu);
2113 return COAP_INVALID_MID;
2114 }
2115 block_pdu = t_pdu;
2116 }
2117 if (!block.m) {
2118 lg_xmit->last_payload = 0;
2119 coap_ticks(&lg_xmit->last_all_sent);
2120 } else
2121 coap_ticks(&lg_xmit->last_payload);
2122 return mid;
2123}
2124
2125#if COAP_CLIENT_SUPPORT
2126/*
2127 * Return 1 if there is a future expire time, else 0.
2128 * Update tim_rem with remaining value if return is 1.
2129 */
2130int
2131coap_block_check_q_block1_xmit(coap_session_t *session, coap_tick_t now, coap_tick_t *tim_rem) {
2132 coap_lg_xmit_t *lg_xmit;
2133 coap_lg_xmit_t *q;
2134 coap_tick_t timed_out;
2135 int ret = 0;
2136
2137 *tim_rem = COAP_MAX_DELAY_TICKS;
2138 LL_FOREACH_SAFE(session->lg_xmit, lg_xmit, q) {
2139 coap_tick_t non_timeout = lg_xmit->non_timeout_random_ticks;
2140
2141 if (now <= non_timeout) {
2142 /* Too early in the startup cycle to have an accurate response */
2143 *tim_rem = non_timeout - now;
2144 return 1;
2145 }
2146 timed_out = now - non_timeout;
2147
2148 if (lg_xmit->last_payload) {
2149 if (lg_xmit->last_payload <= timed_out) {
2150 /* Send off the next MAX_PAYLOAD set */
2151 coap_block_b_t block;
2152 size_t chunk = (size_t)1 << (lg_xmit->blk_size + 4);
2153
2154 memset(&block, 0, sizeof(block));
2155 block.num = (uint32_t)(lg_xmit->offset / chunk);
2156 block.m = lg_xmit->offset + chunk < lg_xmit->data_info->length;
2157 block.szx = lg_xmit->blk_size;
2158 coap_send_q_blocks(session, lg_xmit, block, lg_xmit->sent_pdu, COAP_SEND_SKIP_PDU);
2159 if (*tim_rem > non_timeout) {
2160 *tim_rem = non_timeout;
2161 ret = 1;
2162 }
2163 } else {
2164 /* Delay until the next MAX_PAYLOAD needs to be sent off */
2165 if (*tim_rem > lg_xmit->last_payload - timed_out) {
2166 *tim_rem = lg_xmit->last_payload - timed_out;
2167 ret = 1;
2168 }
2169 }
2170 } else if (lg_xmit->last_all_sent) {
2171 non_timeout = COAP_NON_TIMEOUT_TICKS(session);
2172 if (lg_xmit->last_all_sent + 4 * non_timeout <= now) {
2173 /* Expire this entry */
2174 LL_DELETE(session->lg_xmit, lg_xmit);
2175 coap_block_delete_lg_xmit(session, lg_xmit);
2176 } else {
2177 /* Delay until the lg_xmit needs to expire */
2178 if (*tim_rem > lg_xmit->last_all_sent + 4 * non_timeout - now) {
2179 *tim_rem = lg_xmit->last_all_sent + 4 * non_timeout - now;
2180 ret = 1;
2181 }
2182 }
2183 }
2184 }
2185 return ret;
2186}
2187#endif /* COAP_CLIENT_SUPPORT */
2188
2189#if COAP_SERVER_SUPPORT
2190/*
2191 * Return 1 if there is a future expire time, else 0.
2192 * Update tim_rem with remaining value if return is 1.
2193 */
2194int
2195coap_block_check_q_block2_xmit(coap_session_t *session, coap_tick_t now, coap_tick_t *tim_rem) {
2196 coap_lg_xmit_t *lg_xmit;
2197 coap_lg_xmit_t *q;
2198 coap_tick_t timed_out;
2199 int ret = 0;
2200
2201 *tim_rem = (coap_tick_t)-1;
2202 LL_FOREACH_SAFE(session->lg_xmit, lg_xmit, q) {
2203 coap_tick_t non_timeout = lg_xmit->non_timeout_random_ticks;
2204
2205 if (now <= non_timeout) {
2206 /* Too early in the startup cycle to have an accurate response */
2207 *tim_rem = non_timeout - now;
2208 return 1;
2209 }
2210 timed_out = now - non_timeout;
2211
2212 if (lg_xmit->last_payload) {
2213 if (lg_xmit->last_payload <= timed_out) {
2214 /* Send off the next MAX_PAYLOAD set */
2215 coap_block_b_t block;
2216 size_t chunk = (size_t)1 << (lg_xmit->blk_size + 4);
2217
2218 memset(&block, 0, sizeof(block));
2219 block.num = (uint32_t)(lg_xmit->offset / chunk);
2220 block.m = lg_xmit->offset + chunk < lg_xmit->data_info->length;
2221 block.szx = lg_xmit->blk_size;
2222 if (block.num == (uint32_t)lg_xmit->last_block)
2223 coap_send_q_blocks(session, lg_xmit, block, lg_xmit->sent_pdu, COAP_SEND_SKIP_PDU);
2224 if (*tim_rem > non_timeout) {
2225 *tim_rem = non_timeout;
2226 ret = 1;
2227 }
2228 } else {
2229 /* Delay until the next MAX_PAYLOAD needs to be sent off */
2230 if (*tim_rem > lg_xmit->last_payload - timed_out) {
2231 *tim_rem = lg_xmit->last_payload - timed_out;
2232 ret = 1;
2233 }
2234 }
2235 } else if (lg_xmit->last_all_sent) {
2236 non_timeout = COAP_NON_TIMEOUT_TICKS(session);
2237 if (lg_xmit->last_all_sent + 4 * non_timeout <= now) {
2238 /* Expire this entry */
2239 LL_DELETE(session->lg_xmit, lg_xmit);
2240 coap_block_delete_lg_xmit(session, lg_xmit);
2241 } else {
2242 /* Delay until the lg_xmit needs to expire */
2243 if (*tim_rem > lg_xmit->last_all_sent + 4 * non_timeout - now) {
2244 *tim_rem = lg_xmit->last_all_sent + 4 * non_timeout - now;
2245 ret = 1;
2246 }
2247 }
2248 }
2249 }
2250 return ret;
2251}
2252#endif /* COAP_SERVER_SUPPORT */
2253#endif /* COAP_Q_BLOCK_SUPPORT */
2254
2255#if COAP_CLIENT_SUPPORT
2256/*
2257 * If Observe = 0, save the token away and return NULL
2258 * Else If Observe = 1, return the saved token for this block
2259 * Else, return NULL
2260 */
2261static coap_bin_const_t *
2262track_fetch_observe(coap_pdu_t *pdu, coap_lg_crcv_t *lg_crcv,
2263 uint32_t block_num, coap_bin_const_t *token) {
2264 /* Need to handle Observe for large FETCH */
2265 coap_opt_iterator_t opt_iter;
2267 &opt_iter);
2268
2269 if (opt && lg_crcv) {
2270 int observe_action = -1;
2271 coap_bin_const_t **tmp;
2272
2273 observe_action = coap_decode_var_bytes(coap_opt_value(opt),
2274 coap_opt_length(opt));
2275 if (observe_action == COAP_OBSERVE_ESTABLISH) {
2276 /* Save the token in lg_crcv */
2277 if (lg_crcv->obs_token_cnt <= block_num) {
2278 size_t i;
2279
2280 tmp = coap_realloc_type(COAP_STRING, lg_crcv->obs_token,
2281 (block_num + 1) * sizeof(lg_crcv->obs_token[0]));
2282 if (tmp == NULL)
2283 return NULL;
2284 lg_crcv->obs_token = tmp;
2285 for (i = lg_crcv->obs_token_cnt; i < block_num + 1; i++) {
2286 lg_crcv->obs_token[i] = NULL;
2287 }
2288 }
2289 coap_delete_bin_const(lg_crcv->obs_token[block_num]);
2290
2291 lg_crcv->obs_token_cnt = block_num + 1;
2292 lg_crcv->obs_token[block_num] = coap_new_bin_const(token->s,
2293 token->length);
2294 if (lg_crcv->obs_token[block_num] == NULL)
2295 return NULL;
2296 } else if (observe_action == COAP_OBSERVE_CANCEL) {
2297 /* Use the token in lg_crcv */
2298 if (block_num < lg_crcv->obs_token_cnt) {
2299 return lg_crcv->obs_token[block_num];
2300 }
2301 }
2302 }
2303 return NULL;
2304}
2305
2306#if COAP_Q_BLOCK_SUPPORT
2308coap_send_q_block1(coap_session_t *session,
2309 coap_block_b_t block,
2310 coap_pdu_t *request,
2311 coap_send_pdu_t send_request) {
2312 /* Need to send up to MAX_PAYLOAD blocks if this is a Q_BLOCK1 */
2313 coap_lg_xmit_t *lg_xmit;
2314 uint64_t token_match =
2316 request->actual_token.length));
2317
2318 LL_FOREACH(session->lg_xmit, lg_xmit) {
2319 if (lg_xmit->option == COAP_OPTION_Q_BLOCK1 &&
2320 (token_match == STATE_TOKEN_BASE(lg_xmit->b.b1.state_token) ||
2321 token_match ==
2323 lg_xmit->b.b1.app_token->length))))
2324 break;
2325 /* try out the next one */
2326 }
2327 return coap_send_q_blocks(session, lg_xmit, block, request, send_request);
2328}
2329#endif /* COAP_Q_BLOCK_SUPPORT */
2330#endif /* COAP_CLIENT_SUPPORT */
2331
2332#if COAP_SERVER_SUPPORT
2333#if COAP_Q_BLOCK_SUPPORT
2334/*
2335 * response is always released before return IF COAP_SEND_INC_PDU
2336 */
2338coap_send_q_block2(coap_session_t *session,
2339 coap_resource_t *resource,
2340 const coap_string_t *query,
2341 coap_pdu_code_t request_method,
2342 coap_block_b_t block,
2343 coap_pdu_t *response,
2344 coap_send_pdu_t send_response) {
2345 /* Need to send up to MAX_PAYLOAD blocks if this is a Q_BLOCK2 */
2346 coap_lg_xmit_t *lg_xmit;
2347 coap_string_t empty = { 0, NULL};
2348
2349 LL_FOREACH(session->lg_xmit, lg_xmit) {
2350 if (lg_xmit->option == COAP_OPTION_Q_BLOCK2 &&
2351 resource == lg_xmit->b.b2.resource &&
2352 request_method == lg_xmit->b.b2.request_method &&
2353 coap_string_equal(query ? query : &empty,
2354 lg_xmit->b.b2.query ? lg_xmit->b.b2.query : &empty))
2355 break;
2356 }
2357 return coap_send_q_blocks(session, lg_xmit, block, response, send_response);
2358}
2359#endif /* COAP_Q_BLOCK_SUPPORT */
2360#endif /* COAP_SERVER_SUPPORT */
2361
2362static void
2364 coap_lg_xmit_data_t *data_info) {
2365 if (!data_info)
2366 return;
2367 if (data_info->ref > 0) {
2368 data_info->ref--;
2369 return;
2370 }
2371 if (data_info->release_func) {
2372 coap_lock_callback(data_info->release_func(session,
2373 data_info->app_ptr));
2374 }
2375 coap_free_type(COAP_STRING, data_info);
2376}
2377
2378#if COAP_CLIENT_SUPPORT
2379#if COAP_Q_BLOCK_SUPPORT
2380/*
2381 * Send out a test PDU for Q-Block.
2382 */
2384coap_block_test_q_block(coap_session_t *session, coap_pdu_t *actual) {
2385 coap_pdu_t *pdu;
2386 uint8_t token[8];
2387 size_t token_len;
2388 uint8_t buf[4];
2389 coap_mid_t mid;
2390
2391#if NDEBUG
2392 (void)actual;
2393#endif /* NDEBUG */
2394 assert(session->block_mode & COAP_BLOCK_TRY_Q_BLOCK &&
2395 session->type == COAP_SESSION_TYPE_CLIENT &&
2396 COAP_PDU_IS_REQUEST(actual));
2397
2398 coap_log_debug("Testing for Q-Block support\n");
2399 /* RFC9177 Section 4.1 when checking if available */
2401 coap_new_message_id_lkd(session),
2403 if (!pdu) {
2404 return COAP_INVALID_MID;
2405 }
2406
2407 coap_session_new_token(session, &token_len, token);
2408 coap_add_token(pdu, token_len, token);
2409 /* Use a resource that the server MUST support (.well-known/core) */
2411 11, (const uint8_t *)".well-known");
2413 4, (const uint8_t *)"core");
2414 /*
2415 * M needs to be unset as 'asking' for only the first block using
2416 * Q-Block2 as a test for server support.
2417 * See RFC9177 Section 4.4 Using the Q-Block2 Option.
2418 *
2419 * As the client is asking for 16 byte chunks, it is unlikely that
2420 * the .well-known/core response will be 16 bytes or less, so
2421 * if the server supports Q-Block, it will be forced to respond with
2422 * a Q-Block2, so the client can detect the server Q-Block support.
2423 */
2425 coap_encode_var_safe(buf, sizeof(buf),
2426 (0 << 4) | (0 << 3) | 0),
2427 buf);
2428 set_block_mode_probe_q(session->block_mode);
2429 mid = coap_send_internal(session, pdu, NULL);
2430 if (mid == COAP_INVALID_MID)
2431 return COAP_INVALID_MID;
2432 session->remote_test_mid = mid;
2433 return mid;
2434}
2435#endif /* COAP_Q_BLOCK_SUPPORT */
2436
2439 coap_lg_xmit_t *lg_xmit) {
2440 coap_block_b_t block;
2441 coap_lg_crcv_t *lg_crcv;
2442 uint64_t state_token = STATE_TOKEN_FULL(++session->tx_token, 1);
2443
2444 lg_crcv = coap_malloc_type(COAP_LG_CRCV, sizeof(coap_lg_crcv_t));
2445
2446 if (lg_crcv == NULL)
2447 return NULL;
2448
2449 coap_log_debug("** %s: lg_crcv %p initialized - stateless token xxxxx%011llx\n",
2450 coap_session_str(session), (void *)lg_crcv,
2451 STATE_TOKEN_BASE(state_token));
2452 memset(lg_crcv, 0, sizeof(coap_lg_crcv_t));
2453 lg_crcv->initial = 1;
2454 coap_ticks(&lg_crcv->last_used);
2455 /* Keep a copy of the sent pdu */
2456 lg_crcv->sent_pdu = coap_pdu_reference_lkd(pdu);
2457 if (lg_xmit) {
2458 coap_opt_iterator_t opt_iter;
2459 coap_opt_t *opt;
2460
2461 opt = coap_check_option(pdu, COAP_OPTION_OBSERVE, &opt_iter);
2462
2463 if (opt) {
2464 int observe_action;
2465
2466 observe_action = coap_decode_var_bytes(coap_opt_value(opt),
2467 coap_opt_length(opt));
2468 if (observe_action == COAP_OBSERVE_ESTABLISH) {
2469 /* Need to keep information for Observe Cancel */
2470 size_t data_len;
2471 const uint8_t *data;
2472
2473 if (coap_get_data(pdu, &data_len, &data)) {
2474 if (data_len < lg_xmit->data_info->length) {
2475 lg_xmit->data_info->ref++;
2476 lg_crcv->obs_data = lg_xmit->data_info;
2477 }
2478 }
2479 }
2480 }
2481 }
2482
2483 /* Need to keep original token for updating response PDUs */
2484 lg_crcv->app_token = coap_new_binary(pdu->actual_token.length);
2485 if (!lg_crcv->app_token) {
2486 coap_block_delete_lg_crcv(session, lg_crcv);
2487 return NULL;
2488 }
2489 memcpy(lg_crcv->app_token->s, pdu->actual_token.s, pdu->actual_token.length);
2490
2491 /* Need to set up a base token for actual communications if retries needed */
2492 lg_crcv->retry_counter = 1;
2493 lg_crcv->state_token = state_token;
2494 coap_address_copy(&lg_crcv->upstream, &session->addr_info.remote);
2495
2496 if (pdu->code == COAP_REQUEST_CODE_FETCH) {
2497 coap_bin_const_t *new_token;
2498
2499 /* Need to save/restore Observe Token for large FETCH */
2500 new_token = track_fetch_observe(pdu, lg_crcv, 0, &pdu->actual_token);
2501 if (new_token)
2502 coap_update_token(pdu, new_token->length, new_token->s);
2503 }
2504
2505 if (coap_get_block_b(session, pdu, COAP_OPTION_BLOCK1, &block)) {
2506 /* In case it is there - must not be in continuing request PDUs */
2507 lg_crcv->o_block_option = COAP_OPTION_BLOCK1;
2508 lg_crcv->o_blk_size = block.aszx;
2509 }
2510
2511 return lg_crcv;
2512}
2513
2514void
2516 coap_lg_crcv_t *lg_crcv) {
2517 size_t i;
2518
2519#if (COAP_MAX_LOGGING_LEVEL < _COAP_LOG_DEBUG)
2520 (void)session;
2521#endif
2522 if (lg_crcv == NULL)
2523 return;
2524
2526 if (lg_crcv->obs_data) {
2527 coap_block_release_lg_xmit_data(session, lg_crcv->obs_data);
2528 lg_crcv->obs_data = NULL;
2529 }
2530 coap_address_copy(&session->addr_info.remote, &lg_crcv->upstream);
2531 coap_log_debug("** %s: lg_crcv %p released\n",
2532 coap_session_str(session), (void *)lg_crcv);
2533 coap_delete_binary(lg_crcv->app_token);
2534 for (i = 0; i < lg_crcv->obs_token_cnt; i++) {
2535 coap_delete_bin_const(lg_crcv->obs_token[i]);
2536 }
2538 coap_delete_pdu_lkd(lg_crcv->sent_pdu);
2539 coap_free_type(COAP_LG_CRCV, lg_crcv);
2540}
2541#endif /* COAP_CLIENT_SUPPORT */
2542
2543#if COAP_SERVER_SUPPORT
2544void
2546 coap_lg_srcv_t *lg_srcv) {
2547#if (COAP_MAX_LOGGING_LEVEL < _COAP_LOG_DEBUG)
2548 (void)session;
2549#endif
2550 if (lg_srcv == NULL)
2551 return;
2552
2556 coap_log_debug("** %s: lg_srcv %p released\n",
2557 coap_session_str(session), (void *)lg_srcv);
2558 coap_free_type(COAP_LG_SRCV, lg_srcv);
2559}
2560#endif /* COAP_SERVER_SUPPORT */
2561
2562void
2564 coap_lg_xmit_t *lg_xmit) {
2565 if (lg_xmit == NULL)
2566 return;
2567
2568 coap_block_release_lg_xmit_data(session, lg_xmit->data_info);
2569 if (COAP_PDU_IS_REQUEST(lg_xmit->sent_pdu))
2570 coap_delete_binary(lg_xmit->b.b1.app_token);
2571 else
2572 coap_delete_string(lg_xmit->b.b2.query);
2573 coap_delete_pdu_lkd(lg_xmit->sent_pdu);
2574
2575 coap_log_debug("** %s: lg_xmit %p released\n",
2576 coap_session_str(session), (void *)lg_xmit);
2577 coap_free_type(COAP_LG_XMIT, lg_xmit);
2578}
2579
2580#if COAP_SERVER_SUPPORT
2581typedef struct {
2582 uint32_t num;
2583 int is_continue;
2584} send_track;
2585
2586static int
2587add_block_send(uint32_t num, int is_continue, send_track *out_blocks,
2588 uint32_t *count, uint32_t max_count) {
2589 uint32_t i;
2590
2591 for (i = 0; i < *count && *count < max_count; i++) {
2592 if (num == out_blocks[i].num)
2593 return 0;
2594 else if (num < out_blocks[i].num) {
2595 if (*count - i > 1)
2596 memmove(&out_blocks[i], &out_blocks[i+1], *count - i -1);
2597 out_blocks[i].num = num;
2598 out_blocks[i].is_continue = is_continue;
2599 (*count)++;
2600 return 1;
2601 }
2602 }
2603 if (*count < max_count) {
2604 out_blocks[i].num = num;
2605 out_blocks[i].is_continue = is_continue;
2606 (*count)++;
2607 return 1;
2608 }
2609 return 0;
2610}
2611
2612/*
2613 * Need to see if this is a request for the next block of a large body
2614 * transfer. If so, need to initiate the response with the next blocks
2615 * and not trouble the application.
2616 *
2617 * If additional responses needed, then these are expicitly sent out and
2618 * 'response' is updated to be the last response to be sent. There can be
2619 * multiple Q-Block2 in the request, as well as the 'Continue' Q-Block2
2620 * request.
2621 *
2622 * This is set up using coap_add_data_large_response_lkd()
2623 *
2624 * Server is sending a large data response to GET / observe (Block2)
2625 *
2626 * Return: 0 Call application handler
2627 * 1 Do not call application handler - just send the built response
2628 */
2629int
2631 coap_pdu_t *pdu,
2632 coap_pdu_t *response,
2633 coap_resource_t *resource,
2634 coap_string_t *query) {
2635 coap_lg_xmit_t *lg_xmit = NULL;
2636 coap_block_b_t block;
2637 coap_block_b_t alt_block;
2638 uint16_t block_opt = 0;
2639 send_track *out_blocks = NULL;
2640 const char *error_phrase;
2641 coap_opt_iterator_t opt_iter;
2642 size_t chunk;
2643 coap_opt_iterator_t opt_b_iter;
2644 coap_opt_t *option;
2645 uint32_t request_cnt, i;
2646 coap_opt_t *etag_opt = NULL;
2647 coap_pdu_t *out_pdu = response;
2648#if COAP_Q_BLOCK_SUPPORT
2649 size_t max_block;
2650
2651 /* Is client indicating that it supports Q_BLOCK2 ? */
2652 if (coap_get_block_b(session, pdu, COAP_OPTION_Q_BLOCK2, &block)) {
2653 if (!(session->block_mode & COAP_BLOCK_HAS_Q_BLOCK))
2654 set_block_mode_has_q(session->block_mode);
2655 block_opt = COAP_OPTION_Q_BLOCK2;
2656 }
2657#endif /* COAP_Q_BLOCK_SUPPORT */
2658 if (coap_get_block_b(session, pdu, COAP_OPTION_BLOCK2, &alt_block)) {
2659 if (block_opt) {
2660 coap_log_warn("Block2 and Q-Block2 cannot be in the same request\n");
2661 coap_add_data(response, sizeof("Both Block2 and Q-Block2 invalid")-1,
2662 (const uint8_t *)"Both Block2 and Q-Block2 invalid");
2663 response->code = COAP_RESPONSE_CODE(400);
2664 goto skip_app_handler;
2665 }
2666 block = alt_block;
2667 block_opt = COAP_OPTION_BLOCK2;
2668 }
2669 if (block_opt == 0)
2670 return 0;
2671 if (block.num == 0) {
2672 /* Get a fresh copy of the data */
2673 return 0;
2674 }
2675 lg_xmit = coap_find_lg_xmit_response(session, pdu, resource, query);
2676 if (lg_xmit == NULL)
2677 return 0;
2678
2679#if COAP_Q_BLOCK_SUPPORT
2680 out_blocks = coap_malloc_type(COAP_STRING, sizeof(send_track) * COAP_MAX_PAYLOADS(session));
2681#else /* ! COAP_Q_BLOCK_SUPPORT */
2682 out_blocks = coap_malloc_type(COAP_STRING, sizeof(send_track));
2683#endif /* ! COAP_Q_BLOCK_SUPPORT */
2684 if (!out_blocks) {
2685 goto internal_issue;
2686 }
2687
2688 /* lg_xmit (response) found */
2689
2690 etag_opt = coap_check_option(pdu, COAP_OPTION_ETAG, &opt_iter);
2691 if (etag_opt) {
2692 /* There may be multiple ETag - need to check each one */
2693 coap_option_iterator_init(pdu, &opt_iter, COAP_OPT_ALL);
2694 while ((etag_opt = coap_option_next(&opt_iter))) {
2695 if (opt_iter.number == COAP_OPTION_ETAG) {
2696 uint64_t etag = coap_decode_var_bytes8(coap_opt_value(etag_opt),
2697 coap_opt_length(etag_opt));
2698 if (etag == lg_xmit->b.b2.etag) {
2699 break;
2700 }
2701 }
2702 }
2703 if (!etag_opt) {
2704 /* Not a match - pass up to a higher level */
2705 return 0;
2706 }
2707 }
2708 out_pdu->code = lg_xmit->sent_pdu->code;
2709 coap_ticks(&lg_xmit->last_obs);
2710 lg_xmit->last_all_sent = 0;
2711
2712 chunk = (size_t)1 << (lg_xmit->blk_size + 4);
2713 if (block_opt) {
2714 if (block.bert) {
2715 coap_log_debug("found Block option, block is BERT, block nr. %u, M %d\n",
2716 block.num, block.m);
2717 } else {
2718 coap_log_debug("found Block option, block size is %u, block nr. %u, M %d\n",
2719 1 << (block.szx + 4), block.num, block.m);
2720 }
2721 if (block.bert == 0 && block.szx != lg_xmit->blk_size) {
2722 if (block.num == 0) {
2723 if ((lg_xmit->offset + chunk) % ((size_t)1 << (block.szx + 4)) == 0) {
2724 /*
2725 * Recompute the block number of the previous packet given
2726 * the new block size
2727 */
2728 block.num = (uint32_t)(((lg_xmit->offset + chunk) >> (block.szx + 4)) - 1);
2729 lg_xmit->blk_size = block.szx;
2730 chunk = (size_t)1 << (lg_xmit->blk_size + 4);
2731 lg_xmit->offset = block.num * chunk;
2732 coap_log_debug("new Block size is %u, block number %u completed\n",
2733 1 << (block.szx + 4), block.num);
2734 } else {
2735 coap_log_debug("ignoring request to increase Block size, "
2736 "next block is not aligned on requested block size "
2737 "boundary. (%zu x %u mod %u = %zu (which is not 0)\n",
2738 lg_xmit->offset/chunk + 1, (1 << (lg_xmit->blk_size + 4)),
2739 (1 << (block.szx + 4)),
2740 (lg_xmit->offset + chunk) % ((size_t)1 << (block.szx + 4)));
2741 }
2742 } else {
2743 coap_log_debug("ignoring request to change Block size from %u to %u\n",
2744 (1 << (lg_xmit->blk_size + 4)), (1 << (block.szx + 4)));
2745 block.szx = block.aszx = lg_xmit->blk_size;
2746 }
2747 }
2748 }
2749
2750 /*
2751 * Need to check if there are multiple Q-Block2 requests. If so, they
2752 * need to be sent out in order of requests with the final request being
2753 * handled as per singular Block 2 request.
2754 */
2755 request_cnt = 0;
2756#if COAP_Q_BLOCK_SUPPORT
2757 max_block = (lg_xmit->data_info->length + chunk - 1)/chunk;
2758#endif /* COAP_Q_BLOCK_SUPPORT */
2759 coap_option_iterator_init(pdu, &opt_b_iter, COAP_OPT_ALL);
2760 while ((option = coap_option_next(&opt_b_iter))) {
2761 uint32_t num;
2762 if (opt_b_iter.number != lg_xmit->option)
2763 continue;
2764 num = coap_opt_block_num(option);
2765 if (num > 0xFFFFF) /* 20 bits max for num */
2766 continue;
2767 if (block.aszx != COAP_OPT_BLOCK_SZX(option)) {
2768 coap_add_data(response,
2769 sizeof("Changing blocksize during request invalid")-1,
2770 (const uint8_t *)"Changing blocksize during request invalid");
2771 response->code = COAP_RESPONSE_CODE(400);
2772 goto skip_app_handler;
2773 }
2774#if COAP_Q_BLOCK_SUPPORT
2775 if (COAP_OPT_BLOCK_MORE(option) && lg_xmit->option == COAP_OPTION_Q_BLOCK2) {
2776 if ((num % COAP_MAX_PAYLOADS(session)) == 0) {
2777 if (num == 0) {
2778 /* This is a repeat request for everything - hmm */
2779 goto call_app_handler;
2780 }
2781 /* 'Continue' request */
2782 for (i = 0; i < COAP_MAX_PAYLOADS(session) &&
2783 num + i < max_block; i++) {
2784 add_block_send(num + i, 1, out_blocks, &request_cnt,
2785 COAP_MAX_PAYLOADS(session));
2786 lg_xmit->last_block = num + i;
2787 }
2788 } else {
2789 /* Requesting remaining payloads in this MAX_PAYLOADS */
2790 for (i = 0; i < COAP_MAX_PAYLOADS(session) -
2791 num % COAP_MAX_PAYLOADS(session) &&
2792 num + i < max_block; i++) {
2793 add_block_send(num + i, 0, out_blocks, &request_cnt,
2794 COAP_MAX_PAYLOADS(session));
2795 }
2796 }
2797 } else
2798 add_block_send(num, 0, out_blocks, &request_cnt,
2799 COAP_MAX_PAYLOADS(session));
2800#else /* ! COAP_Q_BLOCK_SUPPORT */
2801 add_block_send(num, 0, out_blocks, &request_cnt, 1);
2802 break;
2803#endif /* ! COAP_Q_BLOCK_SUPPORT */
2804 }
2805 if (request_cnt == 0) {
2806 /* Block2 or Q-Block2 not found - give them the first block */
2807 block.szx = lg_xmit->blk_size;
2808 lg_xmit->offset = 0;
2809 out_blocks[0].num = 0;
2810 out_blocks[0].is_continue = 0;
2811 request_cnt = 1;
2812 }
2813
2814 for (i = 0; i < request_cnt; i++) {
2815 uint8_t buf[8];
2816
2817 block.num = out_blocks[i].num;
2818 lg_xmit->offset = block.num * chunk;
2819
2820 if (i + 1 < request_cnt) {
2821 /* Need to set up a copy of the pdu to send */
2822 coap_opt_filter_t drop_options;
2823
2824 memset(&drop_options, 0, sizeof(coap_opt_filter_t));
2825 if (block.num != 0)
2827 if (out_blocks[i].is_continue) {
2828 out_pdu = coap_pdu_duplicate_lkd(lg_xmit->sent_pdu, session,
2829 lg_xmit->sent_pdu->actual_token.length,
2830 lg_xmit->sent_pdu->actual_token.s, &drop_options);
2831 } else {
2832 out_pdu = coap_pdu_duplicate_lkd(lg_xmit->sent_pdu, session, pdu->actual_token.length,
2833 pdu->actual_token.s, &drop_options);
2834 }
2835 if (!out_pdu) {
2836 goto internal_issue;
2837 }
2838 } else {
2839 if (out_blocks[i].is_continue)
2840 coap_update_token(response, lg_xmit->sent_pdu->actual_token.length,
2841 lg_xmit->sent_pdu->actual_token.s);
2842 /*
2843 * Copy the options across and then fix the block option
2844 *
2845 * Need to drop Observe option if Block2 and block.num != 0
2846 */
2847 coap_option_iterator_init(lg_xmit->sent_pdu, &opt_iter, COAP_OPT_ALL);
2848 while ((option = coap_option_next(&opt_iter))) {
2849 if (opt_iter.number == COAP_OPTION_OBSERVE && block.num != 0)
2850 continue;
2851 if (!coap_insert_option(response, opt_iter.number,
2852 coap_opt_length(option),
2853 coap_opt_value(option))) {
2854 goto internal_issue;
2855 }
2856 }
2857 out_pdu = response;
2858 }
2859 if (pdu->type == COAP_MESSAGE_NON)
2860 out_pdu->type = COAP_MESSAGE_NON;
2861 if (block.bert) {
2862 size_t token_options = pdu->data ? (size_t)(pdu->data - pdu->token) : pdu->used_size;
2863 block.m = (lg_xmit->data_info->length - lg_xmit->offset) >
2864 ((out_pdu->max_size - token_options) /1024) * 1024;
2865 } else {
2866 block.m = (lg_xmit->offset + chunk) < lg_xmit->data_info->length;
2867 }
2868 if (!coap_update_option(out_pdu, lg_xmit->option,
2870 sizeof(buf),
2871 (block.num << 4) |
2872 (block.m << 3) |
2873 block.aszx),
2874 buf)) {
2875 goto internal_issue;
2876 }
2877 if (!(lg_xmit->offset + chunk < lg_xmit->data_info->length)) {
2878 /* Last block - keep in cache for 4 * ACK_TIMOUT */
2879 coap_ticks(&lg_xmit->last_all_sent);
2880 }
2881 if (lg_xmit->b.b2.maxage_expire) {
2882 coap_tick_t now;
2883 coap_time_t rem;
2884
2885 if (!(lg_xmit->offset + chunk < lg_xmit->data_info->length)) {
2886 /* Last block - keep in cache for 4 * ACK_TIMOUT */
2887 coap_ticks(&lg_xmit->last_all_sent);
2888 }
2889 coap_ticks(&now);
2890 rem = coap_ticks_to_rt(now);
2891 if (lg_xmit->b.b2.maxage_expire > rem) {
2892 rem = lg_xmit->b.b2.maxage_expire - rem;
2893 } else {
2894 rem = 0;
2895 /* Entry needs to be expired */
2896 coap_ticks(&lg_xmit->last_all_sent);
2897 }
2900 sizeof(buf),
2901 rem),
2902 buf)) {
2903 goto internal_issue;
2904 }
2905 }
2906
2907 if (!coap_add_block_b_data(out_pdu,
2908 lg_xmit->data_info->length,
2909 lg_xmit->data_info->data,
2910 &block)) {
2911 goto internal_issue;
2912 }
2913 if (i + 1 < request_cnt) {
2914 coap_ticks(&lg_xmit->last_sent);
2915 coap_send_internal(session, out_pdu, NULL);
2916 }
2917 }
2918 coap_ticks(&lg_xmit->last_payload);
2919 goto skip_app_handler;
2920#if COAP_Q_BLOCK_SUPPORT
2921call_app_handler:
2922 coap_free_type(COAP_STRING, out_blocks);
2923 return 0;
2924#endif /* COAP_Q_BLOCK_SUPPORT */
2925
2926internal_issue:
2927 response->code = COAP_RESPONSE_CODE(500);
2928 error_phrase = coap_response_phrase(response->code);
2929 coap_add_data(response, strlen(error_phrase),
2930 (const uint8_t *)error_phrase);
2931 /* Keep in cache for 4 * ACK_TIMOUT incase of retry */
2932 if (lg_xmit)
2933 coap_ticks(&lg_xmit->last_all_sent);
2934
2935skip_app_handler:
2936 coap_free_type(COAP_STRING, out_blocks);
2937 return 1;
2938}
2939#endif /* COAP_SERVER_SUPPORT */
2940
2941static int
2942update_received_blocks(coap_rblock_t *rec_blocks, uint32_t block_num, uint32_t block_m) {
2943 uint32_t i;
2944
2945 if (rec_blocks->total_blocks && block_num + 1 > rec_blocks->total_blocks) {
2946 /* received block number greater than Block No defined when More bit unset */
2947 return 0;
2948 }
2949
2950 /* Reset as there is activity */
2951 rec_blocks->retry = 0;
2952
2953 for (i = 0; i < rec_blocks->used; i++) {
2954 if (block_num >= rec_blocks->range[i].begin &&
2955 block_num <= rec_blocks->range[i].end)
2956 break;
2957
2958 if (block_num < rec_blocks->range[i].begin) {
2959 if (block_num + 1 == rec_blocks->range[i].begin) {
2960 rec_blocks->range[i].begin = block_num;
2961 } else {
2962 /* Need to insert a new range */
2963 if (rec_blocks->used == COAP_RBLOCK_CNT -1)
2964 /* Too many losses */
2965 return 0;
2966 memmove(&rec_blocks->range[i+1], &rec_blocks->range[i],
2967 (rec_blocks->used - i) * sizeof(rec_blocks->range[0]));
2968 rec_blocks->range[i].begin = rec_blocks->range[i].end = block_num;
2969 rec_blocks->used++;
2970 }
2971 break;
2972 }
2973 if (block_num == rec_blocks->range[i].end + 1) {
2974 rec_blocks->range[i].end = block_num;
2975 if (i + 1 < rec_blocks->used) {
2976 if (rec_blocks->range[i+1].begin == block_num + 1) {
2977 /* Merge the 2 ranges */
2978 rec_blocks->range[i].end = rec_blocks->range[i+1].end;
2979 if (i+2 < rec_blocks->used) {
2980 memmove(&rec_blocks->range[i+1], &rec_blocks->range[i+2],
2981 (rec_blocks->used - (i+2)) * sizeof(rec_blocks->range[0]));
2982 }
2983 rec_blocks->used--;
2984 }
2985 }
2986 break;
2987 }
2988 }
2989 if (i == rec_blocks->used) {
2990 if (rec_blocks->used == COAP_RBLOCK_CNT -1)
2991 /* Too many losses */
2992 return 0;
2993 rec_blocks->range[i].begin = rec_blocks->range[i].end = block_num;
2994 rec_blocks->used++;
2995 }
2996 if (!block_m)
2997 rec_blocks->total_blocks = block_num + 1;
2998
2999 coap_ticks(&rec_blocks->last_seen);
3000 return 1;
3001}
3002
3003#if COAP_SERVER_SUPPORT
3004/*
3005 * Need to check if this is a large PUT / POST etc. using multiple blocks
3006 *
3007 * Server receiving PUT/POST etc. of a large amount of data (Block1)
3008 *
3009 * Return: 0 Call application handler
3010 * 1 Do not call application handler - just send the built response
3011 */
3012int
3014 coap_session_t *session,
3015 coap_pdu_t *pdu,
3016 coap_pdu_t *response,
3017 coap_resource_t *resource,
3018 coap_string_t *uri_path,
3019 coap_opt_t *observe,
3020 int *added_block,
3021 coap_lg_srcv_t **pfree_lg_srcv) {
3022 size_t length = 0;
3023 const uint8_t *data = NULL;
3024 size_t offset = 0;
3025 size_t total = 0;
3026 coap_block_b_t block;
3027 coap_opt_iterator_t opt_iter;
3028 uint16_t block_option = 0;
3029 coap_lg_srcv_t *lg_srcv;
3030 coap_opt_t *size_opt;
3031 coap_opt_t *fmt_opt;
3032 uint16_t fmt;
3033 coap_opt_t *rtag_opt;
3034 size_t rtag_length;
3035 const uint8_t *rtag;
3036 uint32_t max_block_szx;
3037 int update_data;
3038 unsigned int saved_num;
3039 size_t saved_offset;
3040
3041 *added_block = 0;
3042 *pfree_lg_srcv = NULL;
3043 coap_get_data_large(pdu, &length, &data, &offset, &total);
3044 pdu->body_offset = 0;
3045 pdu->body_total = length;
3046
3047 if (coap_get_block_b(session, pdu, COAP_OPTION_BLOCK1, &block)) {
3048 block_option = COAP_OPTION_BLOCK1;
3049#if COAP_Q_BLOCK_SUPPORT
3050 if (coap_check_option(pdu, COAP_OPTION_Q_BLOCK1, &opt_iter)) {
3051 /* Cannot handle Q-Block1 as well */
3052 coap_add_data(response, sizeof("Block1 + Q-Block1 together")-1,
3053 (const uint8_t *)"Block1 + Q-Block1 together");
3054 response->code = COAP_RESPONSE_CODE(402);
3055 goto skip_app_handler;
3056 }
3057#endif /* COAP_Q_BLOCK_SUPPORT */
3058 }
3059#if COAP_Q_BLOCK_SUPPORT
3060 else if (coap_get_block_b(session, pdu, COAP_OPTION_Q_BLOCK1, &block)) {
3061 block_option = COAP_OPTION_Q_BLOCK1;
3062 set_block_mode_has_q(session->block_mode);
3063 }
3064#endif /* COAP_Q_BLOCK_SUPPORT */
3065 if (!block_option ||
3066 (block_option == COAP_OPTION_BLOCK1 && block.num == 0 && block.m == 0)) {
3067 /* Not blocked, or a single block */
3068 goto call_app_handler;
3069 }
3070
3071 size_opt = coap_check_option(pdu,
3073 &opt_iter);
3074 fmt_opt = coap_check_option(pdu,
3076 &opt_iter);
3077 fmt = fmt_opt ? coap_decode_var_bytes(coap_opt_value(fmt_opt),
3078 coap_opt_length(fmt_opt)) :
3080 rtag_opt = coap_check_option(pdu,
3082 &opt_iter);
3083 rtag_length = rtag_opt ? coap_opt_length(rtag_opt) : 0;
3084 rtag = rtag_opt ? coap_opt_value(rtag_opt) : NULL;
3085
3086 if (length > block.chunk_size) {
3087 coap_log_debug("block: Oversized packet - reduced to %"PRIu32" from %zu\n",
3088 block.chunk_size, length);
3089 length = block.chunk_size;
3090 } else if (!block.bert && block.m && length != block.chunk_size) {
3091 coap_log_info("block: Undersized packet chunk %"PRIu32" got %zu\n",
3092 block.chunk_size, length);
3093 response->code = COAP_RESPONSE_CODE(400);
3094 goto skip_app_handler;
3095 }
3096 total = size_opt ? coap_decode_var_bytes(coap_opt_value(size_opt),
3097 coap_opt_length(size_opt)) : 0;
3098 offset = block.num << (block.szx + 4);
3099
3100 if (!(session->block_mode &
3101#if COAP_Q_BLOCK_SUPPORT
3103#else /* COAP_Q_BLOCK_SUPPORT */
3105#endif /* COAP_Q_BLOCK_SUPPORT */
3106 && !block.bert) {
3107 uint8_t buf[4];
3108
3109 /* Ask for the next block */
3110 coap_insert_option(response, block_option,
3111 coap_encode_var_safe(buf, sizeof(buf),
3112 (block.num << 4) |
3113 (block.m << 3) |
3114 block.aszx),
3115 buf);
3116 /* Not re-assembling or checking for receipt order */
3117 pdu->body_data = data;
3118 pdu->body_length = length;
3119 pdu->body_offset = offset;
3120 if (total < (length + offset + (block.m ? 1 : 0)))
3121 total = length + offset + (block.m ? 1 : 0);
3122 pdu->body_total = total;
3123 *added_block = block.m;
3124 /* The application is responsible for returning the correct 2.01/2.04/2.31 etc. */
3125 goto call_app_handler;
3126 }
3127
3128 /*
3129 * locate the lg_srcv
3130 */
3131 LL_FOREACH(session->lg_srcv, lg_srcv) {
3132 if (rtag_opt || lg_srcv->rtag_set == 1) {
3133 if (!(rtag_opt && lg_srcv->rtag_set == 1))
3134 continue;
3135 if (lg_srcv->rtag_length != rtag_length ||
3136 memcmp(lg_srcv->rtag, rtag, rtag_length) != 0)
3137 continue;
3138 }
3139 if (resource == lg_srcv->resource) {
3140 break;
3141 }
3142 if ((lg_srcv->resource == context->unknown_resource ||
3143 resource == context->proxy_uri_resource) &&
3144 coap_string_equal(uri_path, lg_srcv->uri_path))
3145 break;
3146 }
3147
3148 if (!lg_srcv && block.num != 0 && session->block_mode & COAP_BLOCK_NOT_RANDOM_BLOCK1) {
3149 coap_add_data(response, sizeof("Missing block 0")-1,
3150 (const uint8_t *)"Missing block 0");
3151 response->code = COAP_RESPONSE_CODE(408);
3152 goto skip_app_handler;
3153 }
3154
3155 if (!lg_srcv) {
3156 /* Allocate lg_srcv to use for tracking */
3157 lg_srcv = coap_malloc_type(COAP_LG_SRCV, sizeof(coap_lg_srcv_t));
3158 if (lg_srcv == NULL) {
3159 coap_add_data(response, sizeof("Memory issue")-1,
3160 (const uint8_t *)"Memory issue");
3161 response->code = COAP_RESPONSE_CODE(500);
3162 goto skip_app_handler;
3163 }
3164 coap_log_debug("** %s: lg_srcv %p initialized\n",
3165 coap_session_str(session), (void *)lg_srcv);
3166 memset(lg_srcv, 0, sizeof(coap_lg_srcv_t));
3167 lg_srcv->resource = resource;
3168 if (resource == context->unknown_resource ||
3169 resource == context->proxy_uri_resource)
3170 lg_srcv->uri_path = coap_new_str_const(uri_path->s, uri_path->length);
3171 lg_srcv->content_format = fmt;
3172 lg_srcv->total_len = total;
3173 max_block_szx = COAP_BLOCK_MAX_SIZE_GET(session->block_mode);
3174 if (!block.bert && block.num == 0 && max_block_szx != 0 &&
3175 max_block_szx < block.szx) {
3176 lg_srcv->szx = max_block_szx;
3177 } else {
3178 lg_srcv->szx = block.szx;
3179 }
3180 lg_srcv->block_option = block_option;
3181 if (observe) {
3182 lg_srcv->observe_length = min(coap_opt_length(observe), 3);
3183 memcpy(lg_srcv->observe, coap_opt_value(observe), lg_srcv->observe_length);
3184 lg_srcv->observe_set = 1;
3185 }
3186 if (rtag_opt) {
3187 lg_srcv->rtag_length = coap_opt_length(rtag_opt);
3188 memcpy(lg_srcv->rtag, coap_opt_value(rtag_opt), lg_srcv->rtag_length);
3189 lg_srcv->rtag_set = 1;
3190 }
3191 lg_srcv->body_data = NULL;
3192 LL_PREPEND(session->lg_srcv, lg_srcv);
3193 }
3194 coap_ticks(&lg_srcv->last_used);
3195
3196 if (block_option == COAP_OPTION_BLOCK1 &&
3198 !check_if_next_block(&lg_srcv->rec_blocks, block.num)) {
3199 coap_add_data(response, sizeof("Missing interim block")-1,
3200 (const uint8_t *)"Missing interim block");
3201 response->code = COAP_RESPONSE_CODE(408);
3202 goto skip_app_handler;
3203 }
3204
3205 if (fmt != lg_srcv->content_format) {
3206 coap_add_data(response, sizeof("Content-Format mismatch")-1,
3207 (const uint8_t *)"Content-Format mismatch");
3208 response->code = COAP_RESPONSE_CODE(408);
3209 goto free_lg_srcv;
3210 }
3211
3212#if COAP_Q_BLOCK_SUPPORT
3213 if (block_option == COAP_OPTION_Q_BLOCK1) {
3214 if (total != lg_srcv->total_len) {
3215 coap_add_data(response, sizeof("Size1 mismatch")-1,
3216 (const uint8_t *)"Size1 mismatch");
3217 response->code = COAP_RESPONSE_CODE(408);
3218 goto free_lg_srcv;
3219 }
3222 pdu->actual_token.length);
3223 }
3224#endif /* COAP_Q_BLOCK_SUPPORT */
3225
3226 lg_srcv->last_mid = pdu->mid;
3227 lg_srcv->last_type = pdu->type;
3228
3229 update_data = 0;
3230 saved_num = block.num;
3231 saved_offset = offset;
3232
3233 while (offset < saved_offset + length) {
3234 if (!check_if_received_block(&lg_srcv->rec_blocks, block.num)) {
3235 /* Update list of blocks received */
3236 if (!update_received_blocks(&lg_srcv->rec_blocks, block.num, block.m)) {
3238 coap_add_data(response, sizeof("Too many missing blocks")-1,
3239 (const uint8_t *)"Too many missing blocks");
3240 response->code = COAP_RESPONSE_CODE(408);
3241 goto free_lg_srcv;
3242 }
3243 update_data = 1;
3244 }
3245 block.num++;
3246 offset = block.num << (block.szx + 4);
3247 }
3248 block.num--;
3249
3250 if (update_data) {
3251 /* Update saved data */
3252#if COAP_Q_BLOCK_SUPPORT
3253 lg_srcv->rec_blocks.processing_payload_set =
3254 block.num / COAP_MAX_PAYLOADS(session);
3255#endif /* COAP_Q_BLOCK_SUPPORT */
3256 if (lg_srcv->total_len < saved_offset + length) {
3257 lg_srcv->total_len = saved_offset + length;
3258 }
3259 if (context && context->block_data_handler && !resource->is_proxy_uri &&
3260 !resource->is_reverse_proxy &&
3261 ((session->block_mode & COAP_SINGLE_BLOCK_OR_Q) || block.bert) &&
3263 coap_response_t resp;
3264 resp = context->block_data_handler(session, pdu, resource, &lg_srcv->body_data,
3265 length, data, saved_offset,
3266 lg_srcv->total_len);
3267 if (resp != COAP_RESPONSE_OK) {
3268 response->code = COAP_RESPONSE_CODE(500);
3269 goto skip_app_handler;
3270 }
3271 } else {
3272 lg_srcv->body_data = coap_block_build_body(lg_srcv->body_data, length, data,
3273 saved_offset, lg_srcv->total_len);
3274 if (!lg_srcv->body_data) {
3275 coap_add_data(response, sizeof("Memory issue")-1,
3276 (const uint8_t *)"Memory issue");
3277 response->code = COAP_RESPONSE_CODE(500);
3278 goto skip_app_handler;
3279 }
3280 }
3281 }
3282
3283 if (block.m ||
3284 !check_all_blocks_in(&lg_srcv->rec_blocks)) {
3285 /* Not all the payloads of the body have arrived */
3286 if (block.m) {
3287 uint8_t buf[4];
3288
3289#if COAP_Q_BLOCK_SUPPORT
3290 if (block_option == COAP_OPTION_Q_BLOCK1) {
3291 if (check_all_blocks_in(&lg_srcv->rec_blocks)) {
3292 goto give_app_data;
3293 }
3294 if (lg_srcv->rec_blocks.used == 1 &&
3295 (lg_srcv->rec_blocks.range[0].end % COAP_MAX_PAYLOADS(session)) + 1
3296 == COAP_MAX_PAYLOADS(session)) {
3297 /* Blocks could arrive in wrong order */
3298 block.num = lg_srcv->rec_blocks.range[0].end;
3299 } else {
3300 /* The remote end will be sending the next one unless this
3301 is a MAX_PAYLOADS and all previous have been received */
3302 goto skip_app_handler;
3303 }
3304 if (COAP_PROTO_RELIABLE(session->proto) ||
3305 pdu->type != COAP_MESSAGE_NON)
3306 goto skip_app_handler;
3307 }
3308#endif /* COAP_Q_BLOCK_SUPPORT */
3309
3310 /* Check to see if block size is getting forced down */
3311 max_block_szx = COAP_BLOCK_MAX_SIZE_GET(session->block_mode);
3312 if (!block.bert && saved_num == 0 && max_block_szx != 0 &&
3313 max_block_szx < block.aszx) {
3314 block.aszx = max_block_szx;
3315 }
3316
3317 /*
3318 * If the last block has been seen, packets are coming in in
3319 * random order. If all blocks are now in, then need to send
3320 * complete payload to application and acknowledge this current
3321 * block.
3322 */
3323 if ((total == 0 && block.m) || !check_all_blocks_in(&lg_srcv->rec_blocks)) {
3324 /* Ask for the next block */
3325 coap_insert_option(response, block_option,
3326 coap_encode_var_safe(buf, sizeof(buf),
3327 (saved_num << 4) |
3328 (block.m << 3) |
3329 block.aszx),
3330 buf);
3331 response->code = COAP_RESPONSE_CODE(231);
3332 } else {
3333 /* Need to separately respond to this request */
3334 coap_pdu_t *tmp_pdu = coap_pdu_duplicate_lkd(response,
3335 session,
3336 response->actual_token.length,
3337 response->actual_token.s,
3338 NULL);
3339 if (tmp_pdu) {
3340 tmp_pdu->code = COAP_RESPONSE_CODE(231);
3341 coap_send_internal(session, tmp_pdu, NULL);
3342 }
3343 if (lg_srcv->last_token) {
3344 coap_update_token(response, lg_srcv->last_token->length, lg_srcv->last_token->s);
3345 coap_update_token(pdu, lg_srcv->last_token->length, lg_srcv->last_token->s);
3346 }
3347 /* Pass the assembled pdu and body to the application */
3348 goto give_app_data;
3349 }
3350 } else {
3351 /* block.m Block More option not set. Some outstanding blocks */
3352#if COAP_Q_BLOCK_SUPPORT
3353 if (block_option != COAP_OPTION_Q_BLOCK1) {
3354#endif /* COAP_Q_BLOCK_SUPPORT */
3355 /* Last chunk - but not all in */
3356 coap_ticks(&lg_srcv->last_used);
3357 lg_srcv->no_more_seen = 1;
3360 pdu->actual_token.length);
3361
3362 /*
3363 * Need to just ACK (no response code) to handle client's NSTART.
3364 * When final missing block comes in, we will pass all the data
3365 * for processing so a 2.01, 2.04 etc. code can be generated
3366 * and responded to as a separate response "RFC7252 5.2.2. Separate"
3367 * If missing block(s) do not come in, then will generate a 4.08
3368 * when lg_srcv times out.
3369 * Fall through to skip_app_handler.
3370 */
3371#if COAP_Q_BLOCK_SUPPORT
3372 }
3373#endif /* COAP_Q_BLOCK_SUPPORT */
3374 }
3375 goto skip_app_handler;
3376 }
3377
3378 /*
3379 * Entire payload received.
3380 * Remove the Block1 option as passing all of the data to
3381 * application layer. Add back in observe option if appropriate.
3382 * Adjust all other information.
3383 */
3384give_app_data:
3385 if (lg_srcv->observe_set) {
3387 lg_srcv->observe_length, lg_srcv->observe);
3388 }
3389 coap_remove_option(pdu, block_option);
3390 if (lg_srcv->body_data) {
3391 pdu->body_data = lg_srcv->body_data->s;
3392 pdu->body_length = lg_srcv->total_len;
3393 } else {
3394 pdu->body_data = NULL;
3395 pdu->body_length = 0;
3396 }
3397 pdu->body_offset = 0;
3398 pdu->body_total = lg_srcv->total_len;
3399 if (context && context->block_data_handler && !resource->is_proxy_uri &&
3400 !resource->is_reverse_proxy &&
3401 ((session->block_mode & COAP_SINGLE_BLOCK_OR_Q) || block.bert) &&
3403 /* Data has already been provided - do not duplicate */
3404 if (pdu->data) {
3405 pdu->used_size = pdu->data - pdu->token - 1;
3406 pdu->data = NULL;
3407 }
3408 }
3409 coap_log_debug("Server app version of updated PDU\n");
3411 lg_srcv->dont_timeout = 1;
3412 *pfree_lg_srcv = lg_srcv;
3413
3414call_app_handler:
3415 return 0;
3416
3417free_lg_srcv:
3418 LL_DELETE(session->lg_srcv, lg_srcv);
3419 coap_block_delete_lg_srcv(session, lg_srcv);
3420
3421skip_app_handler:
3422 return 1;
3423}
3424#endif /* COAP_SERVER_SUPPORT */
3425
3426#if COAP_CLIENT_SUPPORT
3427#if COAP_Q_BLOCK_SUPPORT
3428static uint32_t
3429derive_cbor_value(const uint8_t **bp, size_t rem_len) {
3430 uint32_t value = **bp & 0x1f;
3431 (*bp)++;
3432 if (value < 24) {
3433 return value;
3434 } else if (value == 24) {
3435 if (rem_len < 2)
3436 return (uint32_t)-1;
3437 value = **bp;
3438 (*bp)++;
3439 return value;
3440 } else if (value == 25) {
3441 if (rem_len < 3)
3442 return (uint32_t)-1;
3443 value = **bp << 8;
3444 (*bp)++;
3445 value |= **bp;
3446 (*bp)++;
3447 return value;
3448 }
3449 if (rem_len < 4)
3450 return (uint32_t)-1;
3451 value = **bp << 24;
3452 (*bp)++;
3453 value |= **bp << 16;
3454 (*bp)++;
3455 value |= **bp << 8;
3456 (*bp)++;
3457 value |= **bp;
3458 (*bp)++;
3459 return value;
3460}
3461#endif /* COAP_Q_BLOCK_SUPPORT */
3462
3463static int
3464check_freshness(coap_session_t *session, coap_pdu_t *rcvd, coap_pdu_t *sent,
3465 coap_lg_xmit_t *lg_xmit, coap_lg_crcv_t *lg_crcv) {
3466 /* Check for Echo option for freshness */
3467 coap_opt_iterator_t opt_iter;
3468 coap_opt_t *opt = coap_check_option(rcvd, COAP_OPTION_ECHO, &opt_iter);
3469
3470 if (opt) {
3471 if (sent || lg_xmit || lg_crcv) {
3472 /* Need to retransmit original request with Echo option added */
3473 coap_pdu_t *echo_pdu;
3474 coap_mid_t mid;
3475 const uint8_t *data;
3476 size_t data_len;
3477 int have_data = 0;
3478 uint8_t ltoken[8];
3479 size_t ltoken_len;
3480 uint64_t token;
3481
3482 if (sent) {
3483 if (coap_get_data(sent, &data_len, &data))
3484 have_data = 1;
3485 } else if (lg_xmit) {
3486 sent = lg_xmit->sent_pdu;
3487 if (lg_xmit->data_info->length) {
3488 size_t blk_size = (size_t)1 << (lg_xmit->blk_size + 4);
3489 size_t offset = (lg_xmit->last_block + 1) * blk_size;
3490 have_data = 1;
3491 data = &lg_xmit->data_info->data[offset];
3492 data_len = (lg_xmit->data_info->length - offset) > blk_size ? blk_size :
3493 lg_xmit->data_info->length - offset;
3494 }
3495 } else { /* lg_crcv */
3496 sent = lg_crcv->sent_pdu;
3497 if (coap_get_data(sent, &data_len, &data))
3498 have_data = 1;
3499 }
3500 if (lg_xmit) {
3501 token = STATE_TOKEN_FULL(lg_xmit->b.b1.state_token,
3502 ++lg_xmit->b.b1.count);
3503 } else {
3504 token = STATE_TOKEN_FULL(lg_crcv->state_token,
3505 ++lg_crcv->retry_counter);
3506 }
3507 ltoken_len = coap_encode_var_safe8(ltoken, sizeof(token), token);
3508 echo_pdu = coap_pdu_duplicate_lkd(sent, session, ltoken_len, ltoken, NULL);
3509 if (!echo_pdu)
3510 return 0;
3511 if (!coap_insert_option(echo_pdu, COAP_OPTION_ECHO,
3512 coap_opt_length(opt), coap_opt_value(opt)))
3513 goto not_sent;
3514 if (have_data) {
3515 coap_add_data(echo_pdu, data_len, data);
3516 }
3517 /* Need to track Observe token change if Observe */
3518 track_fetch_observe(echo_pdu, lg_crcv, 0, &echo_pdu->actual_token);
3519#if COAP_OSCORE_SUPPORT
3520 if (session->oscore_encryption &&
3521 (opt = coap_check_option(echo_pdu, COAP_OPTION_OBSERVE, &opt_iter)) &&
3523 /* Need to update the base PDU's Token for closing down Observe */
3524 if (lg_xmit) {
3525 lg_xmit->b.b1.state_token = token;
3526 } else {
3527 lg_crcv->state_token = token;
3528 }
3529 }
3530#endif /* COAP_OSCORE_SUPPORT */
3531 mid = coap_send_internal(session, echo_pdu, NULL);
3532 if (mid == COAP_INVALID_MID)
3533 goto not_sent;
3534 return 1;
3535 } else {
3536 /* Need to save Echo option value to add to next reansmission */
3537not_sent:
3538 coap_delete_bin_const(session->echo);
3539 session->echo = coap_new_bin_const(coap_opt_value(opt),
3540 coap_opt_length(opt));
3541 }
3542 }
3543 return 0;
3544}
3545
3546static void
3547track_echo(coap_session_t *session, coap_pdu_t *rcvd) {
3548 coap_opt_iterator_t opt_iter;
3549 coap_opt_t *opt = coap_check_option(rcvd, COAP_OPTION_ECHO, &opt_iter);
3550
3551 if (opt) {
3552 coap_delete_bin_const(session->echo);
3553 session->echo = coap_new_bin_const(coap_opt_value(opt),
3554 coap_opt_length(opt));
3555 }
3556}
3557
3558/*
3559 * Need to see if this is a response to a large body request transfer. If so,
3560 * need to initiate the request containing the next block and not trouble the
3561 * application. Note that Token must unique per request/response.
3562 *
3563 * Client receives large data acknowledgement from server (Block1)
3564 *
3565 * This is set up using coap_add_data_large_request_lkd()
3566 *
3567 * Client is using GET etc.
3568 *
3569 * Return: 0 Call application handler
3570 * 1 Do not call application handler - just send the built response
3571 */
3572int
3574 coap_pdu_t *rcvd) {
3575 coap_lg_xmit_t *lg_xmit;
3576 coap_lg_crcv_t *lg_crcv = NULL;
3577
3578 lg_xmit = coap_find_lg_xmit(session, rcvd);
3579 if (lg_xmit) {
3580 /* lg_xmit found */
3581 size_t chunk = (size_t)1 << (lg_xmit->blk_size + 4);
3582 coap_block_b_t block;
3583
3584 lg_crcv = coap_find_lg_crcv(session, rcvd);
3585 if (lg_crcv)
3586 coap_ticks(&lg_crcv->last_used);
3587
3588 if (COAP_RESPONSE_CLASS(rcvd->code) == 2 &&
3589 coap_get_block_b(session, rcvd, lg_xmit->option, &block)) {
3590
3591 if (block.bert) {
3592 coap_log_debug("found Block option, block is BERT, block nr. %u (%zu)\n",
3593 block.num, lg_xmit->b.b1.bert_size);
3594 } else {
3595 coap_log_debug("found Block option, block size is %u, block nr. %u\n",
3596 1 << (block.szx + 4), block.num);
3597 }
3598 if (block.szx != lg_xmit->blk_size) {
3599 if (block.szx > lg_xmit->blk_size) {
3600 coap_log_info("ignoring request to increase Block size, "
3601 "(%u > %u)\n",
3602 1 << (block.szx + 4), 1 << (lg_xmit->blk_size + 4));
3603 } else if ((lg_xmit->offset + chunk) % ((size_t)1 << (block.szx + 4)) == 0) {
3604 /*
3605 * Recompute the block number of the previous packet given the
3606 * new block size
3607 */
3608 block.num = (uint32_t)(((lg_xmit->offset + chunk) >> (block.szx + 4)) - 1);
3609 lg_xmit->blk_size = block.szx;
3610 chunk = (size_t)1 << (lg_xmit->blk_size + 4);
3611 lg_xmit->offset = block.num * chunk;
3612 coap_log_debug("new Block size is %u, block number %u completed\n",
3613 1 << (block.szx + 4), block.num);
3614 block.bert = 0;
3615 block.aszx = block.szx;
3616 } else {
3617 coap_log_debug("ignoring request to increase Block size, "
3618 "next block is not aligned on requested block size boundary. "
3619 "(%zu x %u mod %u = %zu != 0)\n",
3620 lg_xmit->offset/chunk + 1, (1 << (lg_xmit->blk_size + 4)),
3621 (1 << (block.szx + 4)),
3622 (lg_xmit->offset + chunk) % ((size_t)1 << (block.szx + 4)));
3623 }
3624 }
3625 track_echo(session, rcvd);
3626 if (lg_xmit->last_block == (int)block.num &&
3627 lg_xmit->option != COAP_OPTION_Q_BLOCK1) {
3628 /*
3629 * Duplicate Block1 ACK
3630 *
3631 * RFCs not clear here, but on a lossy connection, there could
3632 * be multiple Block1 ACKs, causing the client to retransmit the
3633 * same block multiple times, or the server retransmitting the
3634 * same ACK.
3635 *
3636 * Once a block has been ACKd, there is no need to retransmit it.
3637 */
3638 return 1;
3639 }
3640 if (block.bert)
3641 block.num += (unsigned int)(lg_xmit->b.b1.bert_size / 1024 - 1);
3642 lg_xmit->last_block = block.num;
3643 lg_xmit->offset = (block.num + 1) * chunk;
3644 if (lg_xmit->offset < lg_xmit->data_info->length) {
3645 /* Build the next PDU request based off the skeletal PDU */
3646 uint8_t buf[8];
3647 coap_pdu_t *pdu;
3648 uint64_t token = STATE_TOKEN_FULL(lg_xmit->b.b1.state_token, ++lg_xmit->b.b1.count);
3649 size_t len = coap_encode_var_safe8(buf, sizeof(token), token);
3650
3651 if (lg_xmit->sent_pdu->code == COAP_REQUEST_CODE_FETCH) {
3652 /* Need to handle Observe for large FETCH */
3653 if (lg_crcv) {
3654 if (coap_binary_equal(lg_xmit->b.b1.app_token, lg_crcv->app_token)) {
3655 coap_bin_const_t *new_token;
3656 coap_bin_const_t ctoken = { len, buf };
3657
3658 /* Need to save/restore Observe Token for large FETCH */
3659 new_token = track_fetch_observe(lg_xmit->sent_pdu, lg_crcv, block.num + 1,
3660 &ctoken);
3661 if (new_token) {
3662 assert(len <= sizeof(buf));
3663 len = new_token->length;
3664 memcpy(buf, new_token->s, len);
3665 }
3666 }
3667 }
3668 }
3669 pdu = coap_pdu_duplicate_lkd(lg_xmit->sent_pdu, session, len, buf, NULL);
3670 if (!pdu)
3671 goto fail_body;
3672
3673 /*
3674 * If initial transmit was multicast, that would have been NON.
3675 * Make subsequent traffic CON for reliability.
3676 */
3677 if (session->sock.flags & COAP_SOCKET_MULTICAST) {
3678 pdu->type = COAP_MESSAGE_CON;
3679 }
3680
3681 block.num++;
3682 if (block.bert) {
3683 size_t token_options = pdu->data ? (size_t)(pdu->data - pdu->token) :
3684 pdu->used_size;
3685 block.m = (lg_xmit->data_info->length - lg_xmit->offset) >
3686 ((pdu->max_size - token_options) /1024) * 1024;
3687 } else {
3688 block.m = (lg_xmit->offset + chunk) < lg_xmit->data_info->length;
3689 }
3690 coap_update_option(pdu, lg_xmit->option,
3691 coap_encode_var_safe(buf, sizeof(buf),
3692 (block.num << 4) |
3693 (block.m << 3) |
3694 block.aszx),
3695 buf);
3696
3697 if (!coap_add_block_b_data(pdu,
3698 lg_xmit->data_info->length,
3699 lg_xmit->data_info->data,
3700 &block))
3701 goto fail_body;
3702 lg_xmit->b.b1.bert_size = block.chunk_size;
3703 coap_ticks(&lg_xmit->last_sent);
3704#if COAP_Q_BLOCK_SUPPORT
3705 if (lg_xmit->option == COAP_OPTION_Q_BLOCK1 &&
3706 pdu->type == COAP_MESSAGE_NON) {
3707 if (coap_send_q_block1(session, block, pdu,
3708 COAP_SEND_INC_PDU) == COAP_INVALID_MID)
3709 goto fail_body;
3710 return 1;
3711 } else if (coap_send_internal(session, pdu, NULL) == COAP_INVALID_MID)
3712 goto fail_body;
3713#else /* ! COAP_Q_BLOCK_SUPPORT */
3714 if (coap_send_internal(session, pdu, NULL) == COAP_INVALID_MID)
3715 goto fail_body;
3716#endif /* ! COAP_Q_BLOCK_SUPPORT */
3717 return 1;
3718 }
3719 } else if (COAP_RESPONSE_CLASS(rcvd->code) == 2) {
3720 /*
3721 * Not a block response asking for the next block.
3722 * Could be an Observe response overlapping with block FETCH doing
3723 * Observe cancellation.
3724 */
3725 coap_opt_iterator_t opt_iter;
3726 coap_opt_t *obs_opt;
3727 int observe_action = -1;
3728
3729 if (lg_xmit->sent_pdu->code != COAP_REQUEST_CODE_FETCH) {
3730 goto lg_xmit_finished;
3731 }
3732 obs_opt = coap_check_option(lg_xmit->sent_pdu,
3734 &opt_iter);
3735 if (obs_opt) {
3736 observe_action = coap_decode_var_bytes(coap_opt_value(obs_opt),
3737 coap_opt_length(obs_opt));
3738 }
3739 if (observe_action != COAP_OBSERVE_CANCEL) {
3740 goto lg_xmit_finished;
3741 }
3742 obs_opt = coap_check_option(rcvd,
3744 &opt_iter);
3745 if (obs_opt) {
3746 return 0;
3747 }
3748 goto lg_xmit_finished;
3749 } else if (rcvd->code == COAP_RESPONSE_CODE(401)) {
3750 if (check_freshness(session, rcvd, sent, lg_xmit, NULL))
3751 return 1;
3752#if COAP_Q_BLOCK_SUPPORT
3753 } else if (rcvd->code == COAP_RESPONSE_CODE(402)) {
3754 /* Q-Block1 or Q-Block2 not present in p - duplicate error ? */
3755 if (coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK2, &block) ||
3756 coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK1, &block))
3757 return 1;
3758 } else if (rcvd->code == COAP_RESPONSE_CODE(408) &&
3759 lg_xmit->option == COAP_OPTION_Q_BLOCK1) {
3760 size_t length;
3761 const uint8_t *data;
3762 coap_opt_iterator_t opt_iter;
3763 coap_opt_t *fmt_opt = coap_check_option(rcvd,
3765 &opt_iter);
3766 uint16_t fmt = fmt_opt ?
3768 coap_opt_length(fmt_opt)) :
3770
3772 goto fail_body;
3773
3774 if (COAP_PROTO_RELIABLE(session->proto) ||
3775 rcvd->type != COAP_MESSAGE_NON) {
3776 coap_log_debug("Unexpected 4.08 - protocol violation - ignore\n");
3777 return 1;
3778 }
3779
3780 if (coap_get_data(rcvd, &length, &data)) {
3781 /* Need to decode CBOR to work out what blocks to re-send */
3782 const uint8_t *bp = data;
3783 uint32_t i;
3784 uint8_t buf[8];
3785 coap_pdu_t *pdu;
3786 uint64_t token;
3787 uint8_t ltoken[8];
3788 size_t ltoken_length;
3789
3790 for (i = 0; (bp < data + length) &&
3791 i < COAP_MAX_PAYLOADS(session); i++) {
3792 if ((*bp & 0xc0) != 0x00) /* uint(value) */
3793 goto fail_cbor;
3794 block.num = derive_cbor_value(&bp, data + length - bp);
3795 coap_log_debug("Q-Block1: Missing block %d\n", block.num);
3796 if (block.num > (1 << 20) -1)
3797 goto fail_cbor;
3798 block.m = (block.num + 1) * chunk < lg_xmit->data_info->length;
3799 block.szx = lg_xmit->blk_size;
3800
3801 /* Build the next PDU request based off the skeletal PDU */
3802 token = STATE_TOKEN_FULL(lg_xmit->b.b1.state_token,++lg_xmit->b.b1.count);
3803 ltoken_length = coap_encode_var_safe8(ltoken, sizeof(token), token);
3804 pdu = coap_pdu_duplicate_lkd(lg_xmit->sent_pdu, session, ltoken_length,
3805 ltoken, NULL);
3806 if (!pdu)
3807 goto fail_body;
3808
3809 coap_update_option(pdu, lg_xmit->option,
3810 coap_encode_var_safe(buf, sizeof(buf),
3811 (block.num << 4) |
3812 (block.m << 3) |
3813 block.szx),
3814 buf);
3815
3816 if (!coap_add_block(pdu,
3817 lg_xmit->data_info->length,
3818 lg_xmit->data_info->data,
3819 block.num,
3820 block.szx))
3821 goto fail_body;
3822 if (coap_send_internal(session, pdu, NULL) == COAP_INVALID_MID)
3823 goto fail_body;
3824 }
3825 return 1;
3826 }
3827fail_cbor:
3828 coap_log_info("Invalid application/missing-blocks+cbor-seq\n");
3829#endif /* COAP_Q_BLOCK_SUPPORT */
3830 }
3831 goto lg_xmit_finished;
3832 }
3833 return 0;
3834
3835fail_body:
3837 /* There has been an internal error of some sort */
3838 rcvd->code = COAP_RESPONSE_CODE(500);
3839lg_xmit_finished:
3840 if (lg_crcv) {
3841 if (STATE_TOKEN_BASE(lg_xmit->b.b1.state_token) ==
3842 STATE_TOKEN_BASE(lg_crcv->state_token)) {
3843 /* In case of observe */
3844 lg_crcv->state_token = lg_xmit->b.b1.state_token;
3845 lg_crcv->retry_counter = lg_xmit->b.b1.count;
3846 }
3847 }
3848 if (!lg_crcv) {
3849 /* need to put back original token into rcvd */
3850 if (lg_xmit->b.b1.app_token)
3851 coap_update_token(rcvd, lg_xmit->b.b1.app_token->length,
3852 lg_xmit->b.b1.app_token->s);
3853 coap_log_debug("Client app version of updated PDU (1)\n");
3855 } else {
3856 lg_crcv->sent_pdu->lg_xmit = 0;
3857 }
3858
3859 if (sent) {
3860 /* need to put back original token into sent */
3861 if (lg_xmit->b.b1.app_token)
3862 coap_update_token(sent, lg_xmit->b.b1.app_token->length,
3863 lg_xmit->b.b1.app_token->s);
3864 if (sent->lg_xmit)
3865 coap_remove_option(sent, sent->lg_xmit->option);
3866 sent->lg_xmit = NULL;
3867 }
3868 LL_DELETE(session->lg_xmit, lg_xmit);
3869 coap_block_delete_lg_xmit(session, lg_xmit);
3870 return 0;
3871}
3872#endif /* COAP_CLIENT_SUPPORT */
3873
3874void
3876 coap_block_data_handler_t block_data_handler) {
3877 context->block_data_handler = block_data_handler;
3878}
3879
3880/*
3881 * Re-assemble payloads into a body
3882 */
3884coap_block_build_body(coap_binary_t *body_data, size_t length,
3885 const uint8_t *data, size_t offset, size_t total) {
3886 if (data == NULL)
3887 return NULL;
3888 if (body_data == NULL && total) {
3889 body_data = coap_new_binary(total);
3890 }
3891 if (body_data == NULL)
3892 return NULL;
3893
3894 /* Update saved data */
3895 if (offset + length <= total && body_data->length >= total) {
3896 memcpy(&body_data->s[offset], data, length);
3897 } else {
3898 /*
3899 * total may be inaccurate as per
3900 * https://rfc-editor.org/rfc/rfc7959#section-4
3901 * o In a request carrying a Block1 Option, to indicate the current
3902 * estimate the client has of the total size of the resource
3903 * representation, measured in bytes ("size indication").
3904 * o In a response carrying a Block2 Option, to indicate the current
3905 * estimate the server has of the total size of the resource
3906 * representation, measured in bytes ("size indication").
3907 */
3908 coap_binary_t *new = coap_resize_binary(body_data, offset + length);
3909
3910 if (new) {
3911 body_data = new;
3912 memcpy(&body_data->s[offset], data, length);
3913 } else {
3914 coap_delete_binary(body_data);
3915 return NULL;
3916 }
3917 }
3918 return body_data;
3919}
3920
3921#if COAP_CLIENT_SUPPORT
3922/*
3923 * Need to see if this is a large body response to a request. If so,
3924 * need to initiate the request for the next block and not trouble the
3925 * application. Note that Token must be unique per request/response.
3926 *
3927 * This is set up using coap_send()
3928 * Client receives large data from server ((Q-)Block2)
3929 *
3930 * Return: 0 Call application handler
3931 * 1 Do not call application handler - just sent the next request
3932 */
3933int
3935 coap_session_t *session,
3936 coap_pdu_t *sent,
3937 coap_pdu_t *rcvd,
3938 coap_recurse_t recursive) {
3939 coap_lg_crcv_t *lg_crcv;
3940 coap_block_b_t block;
3941#if COAP_Q_BLOCK_SUPPORT
3942 coap_block_b_t qblock;
3943#endif /* COAP_Q_BLOCK_SUPPORT */
3944 int have_block = 0;
3945 uint16_t block_opt = 0;
3946 size_t offset;
3947 int ack_rst_sent = 0;
3948
3950 memset(&block, 0, sizeof(block));
3951#if COAP_Q_BLOCK_SUPPORT
3952 memset(&qblock, 0, sizeof(qblock));
3953#endif /* COAP_Q_BLOCK_SUPPORT */
3954 lg_crcv = coap_find_lg_crcv(session, rcvd);
3955 if (lg_crcv) {
3956 size_t chunk = 0;
3957 uint8_t buf[8];
3958 coap_opt_iterator_t opt_iter;
3959
3960 if (COAP_RESPONSE_CLASS(rcvd->code) == 2) {
3961 size_t length;
3962 const uint8_t *data;
3964 &opt_iter);
3965 size_t size2 = size_opt ?
3967 coap_opt_length(size_opt)) : 0;
3968
3969 /* length and data are cleared on error */
3970 (void)coap_get_data(rcvd, &length, &data);
3971 rcvd->body_offset = 0;
3972 rcvd->body_total = length;
3973 if (coap_get_block_b(session, rcvd, COAP_OPTION_BLOCK2, &block)) {
3974 have_block = 1;
3975 block_opt = COAP_OPTION_BLOCK2;
3976 }
3977#if COAP_Q_BLOCK_SUPPORT
3978 if (coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK2, &qblock)) {
3979 if (have_block) {
3980 coap_log_warn("Both Block1 and Q-Block1 not supported in a response\n");
3981 }
3982 have_block = 1;
3983 block_opt = COAP_OPTION_Q_BLOCK2;
3984 block = qblock;
3985 /* server indicating that it supports Q_BLOCK */
3986 if (!(session->block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
3987 set_block_mode_has_q(session->block_mode);
3988 }
3989 }
3990#endif /* COAP_Q_BLOCK_SUPPORT */
3991 track_echo(session, rcvd);
3992 if (have_block && (block.m || length)) {
3993 coap_opt_t *fmt_opt = coap_check_option(rcvd,
3995 &opt_iter);
3996 uint16_t fmt = fmt_opt ?
3998 coap_opt_length(fmt_opt)) :
4000 coap_opt_t *etag_opt = coap_check_option(rcvd,
4002 &opt_iter);
4003 size_t saved_offset;
4004 int updated_block;
4005
4006 if (length > block.chunk_size) {
4007 coap_log_debug("block: Oversized packet - reduced to %"PRIu32" from %zu\n",
4008 block.chunk_size, length);
4009 length = block.chunk_size;
4010 }
4011 if (block.m && length != block.chunk_size) {
4012 coap_log_warn("block: Undersized packet - expected %"PRIu32", got %zu\n",
4013 block.chunk_size, length);
4014 /* Unclear how to properly handle this */
4015 rcvd->code = COAP_RESPONSE_CODE(402);
4016 goto expire_lg_crcv;
4017 }
4018 /* Possibility that Size2 not sent, or is too small */
4019 chunk = (size_t)1 << (block.szx + 4);
4020 offset = block.num * chunk;
4021 if (size2 < (offset + length)) {
4022 if (block.m)
4023 size2 = offset + length + 1;
4024 else
4025 size2 = offset + length;
4026 }
4027 saved_offset = offset;
4028
4029 if (lg_crcv->initial) {
4030#if COAP_Q_BLOCK_SUPPORT
4031reinit:
4032#endif /* COAP_Q_BLOCK_SUPPORT */
4033 lg_crcv->initial = 0;
4034 if (lg_crcv->body_data) {
4036 lg_crcv->body_data = NULL;
4037 }
4038 if (etag_opt) {
4039 lg_crcv->etag_length = coap_opt_length(etag_opt);
4040 memcpy(lg_crcv->etag, coap_opt_value(etag_opt), lg_crcv->etag_length);
4041 lg_crcv->etag_set = 1;
4042 } else {
4043 lg_crcv->etag_set = 0;
4044 }
4045 lg_crcv->total_len = size2;
4046 lg_crcv->content_format = fmt;
4047 lg_crcv->szx = block.szx;
4048 lg_crcv->block_option = block_opt;
4049 lg_crcv->last_type = rcvd->type;
4050 lg_crcv->rec_blocks.used = 0;
4051 lg_crcv->rec_blocks.total_blocks = 0;
4052#if COAP_Q_BLOCK_SUPPORT
4053 lg_crcv->rec_blocks.processing_payload_set = 0;
4054#endif /* COAP_Q_BLOCK_SUPPORT */
4055 }
4056 if (lg_crcv->total_len < size2)
4057 lg_crcv->total_len = size2;
4058
4059 if (etag_opt) {
4060 if (!full_match(coap_opt_value(etag_opt),
4061 coap_opt_length(etag_opt),
4062 lg_crcv->etag, lg_crcv->etag_length)) {
4063 /* body of data has changed - need to restart request */
4064 coap_pdu_t *pdu;
4065 uint64_t token = STATE_TOKEN_FULL(lg_crcv->state_token,
4066 ++lg_crcv->retry_counter);
4067 size_t len = coap_encode_var_safe8(buf, sizeof(token), token);
4068 coap_opt_filter_t drop_options;
4069
4070#if COAP_Q_BLOCK_SUPPORT
4071 if (block_opt == COAP_OPTION_Q_BLOCK2)
4072 goto reinit;
4073#endif /* COAP_Q_BLOCK_SUPPORT */
4074
4075 coap_log_warn("Data body updated during receipt - new request started\n");
4076 if (!(session->block_mode & COAP_BLOCK_SINGLE_BODY))
4078
4079 lg_crcv->initial = 1;
4081 lg_crcv->body_data = NULL;
4082
4083 coap_session_new_token(session, &len, buf);
4084 memset(&drop_options, 0, sizeof(coap_opt_filter_t));
4087 pdu = coap_pdu_duplicate_lkd(lg_crcv->sent_pdu, session, len, buf, &drop_options);
4088 if (!pdu)
4089 goto fail_resp;
4090
4091 coap_update_option(pdu, block_opt,
4092 coap_encode_var_safe(buf, sizeof(buf),
4093 (0 << 4) | (0 << 3) | block.aszx),
4094 buf);
4095
4096 if (coap_send_internal(session, pdu, NULL) == COAP_INVALID_MID)
4097 goto fail_resp;
4098
4099 goto skip_app_handler;
4100 }
4101 } else if (lg_crcv->etag_set) {
4102 /* Cannot handle this change in ETag to not being there */
4103 coap_log_warn("Not all blocks have ETag option\n");
4104 goto fail_resp;
4105 }
4106
4107 if (fmt != lg_crcv->content_format) {
4108 coap_log_warn("Content-Format option mismatch\n");
4109 goto fail_resp;
4110 }
4111#if COAP_Q_BLOCK_SUPPORT
4112 if (block_opt == COAP_OPTION_Q_BLOCK2 && size2 != lg_crcv->total_len) {
4113 coap_log_warn("Size2 option mismatch\n");
4114 goto fail_resp;
4115 }
4116#endif /* COAP_Q_BLOCK_SUPPORT */
4117 if (block.num == 0) {
4118 coap_opt_t *obs_opt = coap_check_option(rcvd,
4120 &opt_iter);
4121 if (obs_opt) {
4122 lg_crcv->observe_length = min(coap_opt_length(obs_opt), 3);
4123 memcpy(lg_crcv->observe, coap_opt_value(obs_opt), lg_crcv->observe_length);
4124 lg_crcv->observe_set = 1;
4125 } else {
4126 lg_crcv->observe_set = 0;
4127 }
4128 }
4129 updated_block = 0;
4130 while (offset < saved_offset + length) {
4131 if (!check_if_received_block(&lg_crcv->rec_blocks, block.num)) {
4132#if COAP_Q_BLOCK_SUPPORT
4133 uint32_t this_payload_set = block.num / COAP_MAX_PAYLOADS(session);
4134#endif /* COAP_Q_BLOCK_SUPPORT */
4135
4136 coap_log_debug("found Block option, block size is %u, block nr. %u\n",
4137 1 << (block.szx + 4), block.num);
4138#if COAP_Q_BLOCK_SUPPORT
4139 if (block_opt == COAP_OPTION_Q_BLOCK2 && lg_crcv->rec_blocks.used &&
4140 this_payload_set > lg_crcv->rec_blocks.processing_payload_set &&
4141 this_payload_set != lg_crcv->rec_blocks.latest_payload_set) {
4142 coap_request_missing_q_block2(session, lg_crcv);
4143 }
4144 lg_crcv->rec_blocks.latest_payload_set = this_payload_set;
4145#endif /* COAP_Q_BLOCK_SUPPORT */
4146 /* Update list of blocks received */
4147 if (!update_received_blocks(&lg_crcv->rec_blocks, block.num, block.m)) {
4149 goto fail_resp;
4150 }
4151 updated_block = 1;
4152 }
4153 block.num++;
4154 offset = block.num << (block.szx + 4);
4155 if (!block.bert && block_opt != COAP_OPTION_Q_BLOCK2)
4156 break;
4157 }
4158 block.num--;
4159 /* Only process if not duplicate block */
4160 if (updated_block) {
4161 void *body_free;
4162
4163 if ((session->block_mode & COAP_SINGLE_BLOCK_OR_Q) || block.bert) {
4164 if (size2 < saved_offset + length) {
4165 size2 = saved_offset + length;
4166 }
4167 if (context && context->block_data_handler) {
4168 coap_response_t resp;
4169 resp = context->block_data_handler(session, rcvd, 0,
4170 &lg_crcv->body_data, length,
4171 data, saved_offset, size2);
4172 if (resp != COAP_RESPONSE_OK) {
4173 goto fail_resp;
4174 }
4175 } else {
4176 lg_crcv->body_data = coap_block_build_body(lg_crcv->body_data, length, data,
4177 saved_offset, size2);
4178 if (lg_crcv->body_data == NULL) {
4179 goto fail_resp;
4180 }
4181 }
4182 }
4183 if (block.m || !check_all_blocks_in(&lg_crcv->rec_blocks)) {
4184 /* Not all the payloads of the body have arrived */
4185 size_t len;
4186 coap_pdu_t *pdu;
4187 uint64_t token;
4188 coap_opt_filter_t drop_options;
4189
4190 if (block.m) {
4191#if COAP_Q_BLOCK_SUPPORT
4192 if (block_opt == COAP_OPTION_Q_BLOCK2) {
4193 /* Blocks could arrive in wrong order */
4194 if (check_all_blocks_in(&lg_crcv->rec_blocks)) {
4195 goto give_to_app;
4196 }
4197 if (check_all_blocks_in_for_payload_set(session,
4198 &lg_crcv->rec_blocks)) {
4199 block.num = lg_crcv->rec_blocks.range[0].end;
4200 /* Now requesting next payload */
4201 lg_crcv->rec_blocks.processing_payload_set =
4202 block.num / COAP_MAX_PAYLOADS(session) + 1;
4203 if (check_any_blocks_next_payload_set(session,
4204 &lg_crcv->rec_blocks)) {
4205 /* Need to ask for them individually */
4206 coap_request_missing_q_block2(session, lg_crcv);
4207 goto skip_app_handler;
4208 }
4209 } else {
4210 /* The remote end will be sending the next one unless this
4211 is a MAX_PAYLOADS and all previous have been received */
4212 goto skip_app_handler;
4213 }
4214 if (COAP_PROTO_RELIABLE(session->proto) ||
4215 rcvd->type != COAP_MESSAGE_NON)
4216 goto skip_app_handler;
4217
4218 } else
4219#endif /* COAP_Q_BLOCK_SUPPORT */
4220 block.m = 0;
4221
4222 /* Ask for the next block */
4223 token = STATE_TOKEN_FULL(lg_crcv->state_token, ++lg_crcv->retry_counter);
4224 len = coap_encode_var_safe8(buf, sizeof(token), token);
4225 memset(&drop_options, 0, sizeof(coap_opt_filter_t));
4227 pdu = coap_pdu_duplicate_lkd(lg_crcv->sent_pdu, session, len, buf, &drop_options);
4228 if (!pdu)
4229 goto fail_resp;
4230
4231 if (rcvd->type == COAP_MESSAGE_NON)
4232 pdu->type = COAP_MESSAGE_NON; /* Server is using NON */
4233
4234 /* Only sent with the first block */
4236
4237 coap_update_option(pdu, block_opt,
4238 coap_encode_var_safe(buf, sizeof(buf),
4239 ((block.num + 1) << 4) |
4240 (block.m << 3) | block.aszx),
4241 buf);
4242
4244 (void)coap_get_data(lg_crcv->sent_pdu, &length, &data);
4245 coap_add_data_large_internal(session, NULL, pdu, NULL, NULL, -1, 0, length, data, NULL, NULL, 0, 0);
4246 }
4247 if (coap_send_internal(session, pdu, NULL) == COAP_INVALID_MID)
4248 /* Session could now be disconnected, so no lg_crcv */
4249 goto skip_app_handler;
4250 }
4251 if ((session->block_mode & COAP_SINGLE_BLOCK_OR_Q) || block.bert)
4252 goto skip_app_handler;
4253
4254 /* need to put back original token into rcvd */
4255 coap_update_token(rcvd, lg_crcv->app_token->length, lg_crcv->app_token->s);
4256 rcvd->body_offset = saved_offset;
4257#if COAP_Q_BLOCK_SUPPORT
4258 rcvd->body_total = block_opt == COAP_OPTION_Q_BLOCK2 ?
4259 lg_crcv->total_len : size2;
4260#else /* ! COAP_Q_BLOCK_SUPPORT */
4261 rcvd->body_total = size2;
4262#endif /* ! COAP_Q_BLOCK_SUPPORT */
4263 coap_log_debug("Client app version of updated PDU (2)\n");
4265
4266 if (sent) {
4267 /* need to put back original token into sent */
4268 if (lg_crcv->app_token)
4269 coap_update_token(sent, lg_crcv->app_token->length,
4270 lg_crcv->app_token->s);
4271 coap_remove_option(sent, lg_crcv->block_option);
4272 }
4273 goto call_app_handler;
4274 }
4275#if COAP_Q_BLOCK_SUPPORT
4276give_to_app:
4277#endif /* COAP_Q_BLOCK_SUPPORT */
4278 if ((session->block_mode & COAP_SINGLE_BLOCK_OR_Q) || block.bert) {
4279 /* Pretend that there is no block */
4280 coap_remove_option(rcvd, block_opt);
4281 if (lg_crcv->observe_set) {
4283 lg_crcv->observe_length, lg_crcv->observe);
4284 }
4285 rcvd->body_data = lg_crcv->body_data ? lg_crcv->body_data->s : NULL;
4286#if COAP_Q_BLOCK_SUPPORT
4287 if (context && context->block_data_handler) {
4288 /* Data has already been provided - do not duplicate */
4289 if (rcvd->data) {
4290 rcvd->used_size = rcvd->data - rcvd->token - 1;
4291 rcvd->data = NULL;
4292 }
4293 }
4294 rcvd->body_length = block_opt == COAP_OPTION_Q_BLOCK2 ?
4295 lg_crcv->total_len : saved_offset + length;
4296#else /* ! COAP_Q_BLOCK_SUPPORT */
4297 rcvd->body_length = saved_offset + length;
4298#endif /* ! COAP_Q_BLOCK_SUPPORT */
4299 rcvd->body_offset = 0;
4300 rcvd->body_total = rcvd->body_length;
4301 } else {
4302 rcvd->body_offset = saved_offset;
4303#if COAP_Q_BLOCK_SUPPORT
4304 rcvd->body_total = block_opt == COAP_OPTION_Q_BLOCK2 ?
4305 lg_crcv->total_len : size2;
4306#else /* ! COAP_Q_BLOCK_SUPPORT */
4307 rcvd->body_total = size2;
4308#endif /* ! COAP_Q_BLOCK_SUPPORT */
4309 }
4310 /* need to put back original token into rcvd */
4311 if (!coap_binary_equal(&rcvd->actual_token, lg_crcv->app_token)) {
4312 coap_update_token(rcvd, lg_crcv->app_token->length, lg_crcv->app_token->s);
4313 coap_log_debug("Client app version of updated PDU (3)\n");
4315 }
4316 if (sent) {
4317 /* need to put back original token into sent */
4318 if (lg_crcv->app_token)
4319 coap_update_token(sent, lg_crcv->app_token->length,
4320 lg_crcv->app_token->s);
4321 coap_remove_option(sent, lg_crcv->block_option);
4322 }
4323 body_free = lg_crcv->body_data;
4324 lg_crcv->body_data = NULL;
4325 coap_call_response_handler(session, sent, rcvd, body_free);
4326
4327 ack_rst_sent = 1;
4328 if (lg_crcv->observe_set == 0) {
4329 /* Expire this entry */
4330 LL_DELETE(session->lg_crcv, lg_crcv);
4331 coap_block_delete_lg_crcv(session, lg_crcv);
4332 goto skip_app_handler;
4333 }
4334 /* Set up for the next data body as observing */
4335 lg_crcv->initial = 1;
4336 }
4337 coap_ticks(&lg_crcv->last_used);
4338 goto skip_app_handler;
4339 } else {
4340 coap_opt_t *obs_opt = coap_check_option(rcvd,
4342 &opt_iter);
4343 if (obs_opt) {
4344 lg_crcv->observe_length = min(coap_opt_length(obs_opt), 3);
4345 memcpy(lg_crcv->observe, coap_opt_value(obs_opt), lg_crcv->observe_length);
4346 lg_crcv->observe_set = 1;
4347 } else {
4348 lg_crcv->observe_set = 0;
4349 if (!coap_binary_equal(&rcvd->actual_token, lg_crcv->app_token)) {
4350 /* need to put back original token into rcvd */
4351 coap_update_token(rcvd, lg_crcv->app_token->length, lg_crcv->app_token->s);
4353 coap_log_debug("PDU presented to app.\n");
4355 }
4356 /* Expire this entry */
4357 goto expire_lg_crcv;
4358 }
4359 }
4360 coap_ticks(&lg_crcv->last_used);
4361 } else if (rcvd->code == COAP_RESPONSE_CODE(401)) {
4362#if COAP_OSCORE_SUPPORT
4363 if (check_freshness(session, rcvd,
4364 (session->oscore_encryption == 0) ? sent : NULL,
4365 NULL, lg_crcv))
4366#else /* !COAP_OSCORE_SUPPORT */
4367 if (check_freshness(session, rcvd, sent, NULL, lg_crcv))
4368#endif /* !COAP_OSCORE_SUPPORT */
4369 goto skip_app_handler;
4370 goto expire_lg_crcv;
4371 } else {
4372 /* Not 2.xx or 4.01 - assume it is a failure of some sort */
4373 goto expire_lg_crcv;
4374 }
4375 if (!block.m && !lg_crcv->observe_set) {
4376fail_resp:
4377 /* lg_crcv no longer required - cache it for 1 sec */
4378 coap_ticks(&lg_crcv->last_used);
4379 lg_crcv->last_used = lg_crcv->last_used - COAP_MAX_TRANSMIT_WAIT_TICKS(session) +
4381 }
4382 /* need to put back original token into rcvd */
4383 if (!coap_binary_equal(&rcvd->actual_token, lg_crcv->app_token)) {
4384 coap_update_token(rcvd, lg_crcv->app_token->length, lg_crcv->app_token->s);
4385 coap_log_debug("Client app version of updated PDU (4)\n");
4387 }
4388 }
4389
4390 /* Check if receiving a block response and if blocks can be set up */
4391 if (recursive == COAP_RECURSE_OK && !lg_crcv) {
4392 if (!sent) {
4393 if (coap_get_block_b(session, rcvd, COAP_OPTION_BLOCK2, &block)
4394#if COAP_Q_BLOCK_SUPPORT
4395 ||
4396 coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK2, &block)
4397#endif /* COAP_Q_BLOCK_SUPPORT */
4398 ) {
4399 coap_log_debug("** %s: large body receive internal issue\n",
4400 coap_session_str(session));
4401 goto skip_app_handler;
4402 }
4403 } else if (COAP_RESPONSE_CLASS(rcvd->code) == 2) {
4404 if (coap_get_block_b(session, rcvd, COAP_OPTION_BLOCK2, &block)) {
4405#if COAP_Q_BLOCK_SUPPORT
4406 if (session->block_mode & COAP_BLOCK_PROBE_Q_BLOCK) {
4407 set_block_mode_drop_q(session->block_mode);
4408 coap_log_debug("Q-Block support disabled\n");
4409 }
4410#endif /* COAP_Q_BLOCK_SUPPORT */
4411 have_block = 1;
4412 if (block.num != 0) {
4413 /* Assume random access and just give the single response to app */
4414 size_t length;
4415 const uint8_t *data;
4416 size_t chunk = (size_t)1 << (block.szx + 4);
4417
4418 coap_get_data(rcvd, &length, &data);
4419 rcvd->body_offset = block.num*chunk;
4420 rcvd->body_total = block.num*chunk + length + (block.m ? 1 : 0);
4421 goto call_app_handler;
4422 }
4423 }
4424#if COAP_Q_BLOCK_SUPPORT
4425 else if (coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK2, &block)) {
4426 have_block = 1;
4427 /* server indicating that it supports Q_BLOCK2 */
4428 if (!(session->block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
4429 set_block_mode_has_q(session->block_mode);
4430 }
4431 }
4432#endif /* COAP_Q_BLOCK_SUPPORT */
4433 if (have_block) {
4434 lg_crcv = coap_block_new_lg_crcv(session, sent, NULL);
4435
4436 if (lg_crcv) {
4437 LL_PREPEND(session->lg_crcv, lg_crcv);
4438 return coap_handle_response_get_block(context, session, sent, rcvd,
4440 }
4441 }
4442 track_echo(session, rcvd);
4443 } else if (rcvd->code == COAP_RESPONSE_CODE(401)) {
4444 lg_crcv = coap_block_new_lg_crcv(session, sent, NULL);
4445
4446 if (lg_crcv) {
4447 LL_PREPEND(session->lg_crcv, lg_crcv);
4448 return coap_handle_response_get_block(context, session, sent, rcvd,
4450 }
4451 }
4452 }
4453 return 0;
4454
4455expire_lg_crcv:
4456 /* need to put back original token into rcvd */
4457 if (!coap_binary_equal(&rcvd->actual_token, lg_crcv->app_token)) {
4458 coap_update_token(rcvd, lg_crcv->app_token->length, lg_crcv->app_token->s);
4459 coap_log_debug("Client app version of updated PDU (5)\n");
4461 }
4462
4463 if (sent) {
4464 /* need to put back original token into sent */
4465 if (lg_crcv->app_token)
4466 coap_update_token(sent, lg_crcv->app_token->length,
4467 lg_crcv->app_token->s);
4468 coap_remove_option(sent, lg_crcv->block_option);
4469 }
4470 /* Expire this entry */
4471 LL_DELETE(session->lg_crcv, lg_crcv);
4472 coap_block_delete_lg_crcv(session, lg_crcv);
4473
4474call_app_handler:
4475 return 0;
4476
4477skip_app_handler:
4478 if (!ack_rst_sent)
4479 coap_send_ack_lkd(session, rcvd);
4480 return 1;
4481}
4482#endif /* COAP_CLIENT_SUPPORT */
4483
4484#if COAP_SERVER_SUPPORT
4485/* Check if lg_xmit generated and update PDU code if so */
4486void
4488 const coap_pdu_t *request,
4489 coap_pdu_t *response, const coap_resource_t *resource,
4490 const coap_string_t *query) {
4491 coap_lg_xmit_t *lg_xmit;
4492
4493 if (response->code == 0)
4494 return;
4495 lg_xmit = coap_find_lg_xmit_response(session, request, resource, query);
4496 if (lg_xmit && lg_xmit->sent_pdu && lg_xmit->sent_pdu->code == 0) {
4497 lg_xmit->sent_pdu->code = response->code;
4498 return;
4499 }
4500}
4501#endif /* COAP_SERVER_SUPPORT */
4502
4503#if COAP_CLIENT_SUPPORT
4504void
4506 uint64_t token_match =
4508 pdu->actual_token.length));
4509 coap_lg_xmit_t *lg_xmit;
4510 coap_lg_crcv_t *lg_crcv;
4511
4512 if (session->lg_crcv) {
4513 LL_FOREACH(session->lg_crcv, lg_crcv) {
4514 if (coap_binary_equal(&pdu->actual_token, lg_crcv->app_token))
4515 return;
4516 if (token_match == STATE_TOKEN_BASE(lg_crcv->state_token)) {
4517 coap_update_token(pdu, lg_crcv->app_token->length,
4518 lg_crcv->app_token->s);
4519 coap_log_debug("Client app version of updated PDU (6)\n");
4521 return;
4522 }
4523 }
4524 }
4525 if (COAP_PDU_IS_REQUEST(pdu) && session->lg_xmit) {
4526 LL_FOREACH(session->lg_xmit, lg_xmit) {
4527 if (coap_binary_equal(&pdu->actual_token, lg_xmit->b.b1.app_token))
4528 return;
4529 if (token_match == STATE_TOKEN_BASE(lg_xmit->b.b1.state_token)) {
4530 coap_update_token(pdu, lg_xmit->b.b1.app_token->length,
4531 lg_xmit->b.b1.app_token->s);
4532 coap_log_debug("Client app version of updated PDU (7)\n");
4534 return;
4535 }
4536 }
4537 }
4538}
4539#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 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 coap_add_data_large_internal(coap_session_t *session, const coap_pdu_t *request, coap_pdu_t *pdu, coap_resource_t *resource, const coap_string_t *query, int maxage, uint64_t etag, size_t length, const uint8_t *data, coap_release_large_data_t release_func, void *app_ptr, int single_request, coap_pdu_code_t request_method)
Definition coap_block.c:771
static int setup_block_b(coap_session_t *session, coap_pdu_t *pdu, coap_block_b_t *block, unsigned int num, unsigned int blk_size, size_t total)
Definition coap_block.c:132
#define min(a, b)
Definition coap_block.c:19
static int check_if_received_block(coap_rblock_t *rec_blocks, uint32_t block_num)
int coap_flsll(long long j)
Definition coap_encode.c:28
int coap_fls(unsigned int i)
Definition coap_encode.c:21
#define PRIu32
@ COAP_NACK_TOO_MANY_RETRIES
Definition coap_io.h: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:5068
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:1082
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
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.
#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
#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:230
#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:4915
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:1335
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:1836
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:67
unsigned int coap_encode_var_safe8(uint8_t *buf, size_t length, uint64_t val)
Encodes multiple-length byte sequences.
Definition coap_encode.c:77
@ COAP_EVENT_PARTIAL_BLOCK
Triggered when not all of a large body has been received.
Definition coap_event.h: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:786
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:1655
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:633
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:493
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:417
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:727
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:343
#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:783
#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:954
#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:360
#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:773
#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:879
#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:887
#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:848
@ 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
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