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