libcoap 4.3.5-develop-490e4e0
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 (COAP_PROTO_RELIABLE(session->proto) || lg_srcv->last_type != COAP_MESSAGE_NON)
1833 goto check_expire;
1834
1835#if COAP_Q_BLOCK_SUPPORT
1836 if (lg_srcv->block_option == COAP_OPTION_Q_BLOCK1 && lg_srcv->rec_blocks.used) {
1837 size_t scaled_timeout = receive_timeout *
1838 ((size_t)1 << lg_srcv->rec_blocks.retry);
1839
1840 if (lg_srcv->rec_blocks.retry >= COAP_NON_MAX_RETRANSMIT(session)) {
1841 /* Done NON_MAX_RETRANSMIT retries */
1842 goto expire;
1843 }
1844 if (lg_srcv->rec_blocks.last_seen + scaled_timeout <= now) {
1845 uint32_t i;
1846 int block = -1; /* Last one seen */
1847 size_t block_size = (size_t)1 << (lg_srcv->szx + 4);
1848 size_t final_block = (lg_srcv->total_len + block_size - 1)/block_size - 1;
1849 size_t cur_payload;
1850 size_t last_payload_block;
1851 coap_pdu_t *pdu = NULL;
1852 size_t no_blocks = 0;
1853
1854 /* Need to count the number of missing blocks */
1855 for (i = 0; i < lg_srcv->rec_blocks.used; i++) {
1856 if (block < (int)lg_srcv->rec_blocks.range[i].begin &&
1857 lg_srcv->rec_blocks.range[i].begin != 0) {
1858 block++;
1859 no_blocks += lg_srcv->rec_blocks.range[i].begin - block;
1860 }
1861 if (block < (int)lg_srcv->rec_blocks.range[i].end) {
1862 block = lg_srcv->rec_blocks.range[i].end;
1863 }
1864 }
1865 if (no_blocks == 0 && block == (int)final_block)
1866 goto expire;
1867
1868 /* Include missing up to end of current payload or total amount */
1869 cur_payload = block / COAP_MAX_PAYLOADS(session);
1870 last_payload_block = (cur_payload + 1) * COAP_MAX_PAYLOADS(session) - 1;
1871 if (final_block > last_payload_block) {
1872 final_block = last_payload_block;
1873 }
1874 no_blocks += final_block - block;
1875 if (no_blocks == 0) {
1876 /* Add in the blocks out of the next payload */
1877 final_block = (lg_srcv->total_len + block_size - 1)/block_size - 1;
1878 last_payload_block += COAP_MAX_PAYLOADS(session);
1879 if (final_block > last_payload_block) {
1880 final_block = last_payload_block;
1881 }
1882 }
1883 /* Ask for the missing blocks */
1884 block = -1;
1885 for (i = 0; i < lg_srcv->rec_blocks.used; i++) {
1886 if (block < (int)lg_srcv->rec_blocks.range[i].begin &&
1887 lg_srcv->rec_blocks.range[i].begin != 0) {
1888 /* Report on missing blocks */
1889 if (pdu == NULL) {
1890 pdu = pdu_408_build(session, lg_srcv);
1891 if (!pdu)
1892 continue;
1893 }
1894 block++;
1895 for (; block < (int)lg_srcv->rec_blocks.range[i].begin; block++) {
1896 if (!add_408_block(pdu, block)) {
1897 break;
1898 }
1899 }
1900 }
1901 if (block < (int)lg_srcv->rec_blocks.range[i].end) {
1902 block = lg_srcv->rec_blocks.range[i].end;
1903 }
1904 }
1905 block++;
1906 for (; block <= (int)final_block; block++) {
1907 if (pdu == NULL) {
1908 pdu = pdu_408_build(session, lg_srcv);
1909 if (!pdu)
1910 continue;
1911 }
1912 if (!add_408_block(pdu, block)) {
1913 break;
1914 }
1915 }
1916 if (pdu)
1917 coap_send_internal(session, pdu, NULL);
1918 lg_srcv->rec_blocks.retry++;
1919 coap_ticks(&lg_srcv->rec_blocks.last_seen);
1920 }
1921 if (*tim_rem > lg_srcv->rec_blocks.last_seen + scaled_timeout - now) {
1922 *tim_rem = lg_srcv->rec_blocks.last_seen + scaled_timeout - now;
1923 ret = 1;
1924 }
1925 }
1926#endif /* COAP_Q_BLOCK_SUPPORT */
1927 /* Used for Block1 and Q-Block1 */
1928check_expire:
1929 if (lg_srcv->no_more_seen)
1930 partial_timeout = 10 * COAP_TICKS_PER_SECOND;
1931 if (lg_srcv->last_used && lg_srcv->last_used + partial_timeout <= now) {
1932#if COAP_Q_BLOCK_SUPPORT
1933expire:
1934#endif /* COAP_Q_BLOCK_SUPPORT */
1935 /* Expire this entry */
1936 if (lg_srcv->no_more_seen && lg_srcv->block_option == COAP_OPTION_BLOCK1) {
1937 /*
1938 * Need to send a separate 4.08 to indicate missing blocks
1939 * Using NON is permissable as per
1940 * https://datatracker.ietf.org/doc/html/rfc7252#section-5.2.3
1941 */
1942 coap_pdu_t *pdu;
1943
1945 COAP_RESPONSE_CODE(408),
1946 coap_new_message_id_lkd(session),
1948 if (pdu) {
1949 if (lg_srcv->last_token)
1950 coap_add_token(pdu, lg_srcv->last_token->length, lg_srcv->last_token->s);
1951 coap_add_data(pdu, sizeof("Missing interim block")-1,
1952 (const uint8_t *)"Missing interim block");
1953 coap_send_internal(session, pdu, NULL);
1954 }
1955 }
1956 LL_DELETE(session->lg_srcv, lg_srcv);
1957 coap_block_delete_lg_srcv(session, lg_srcv);
1958 } else if (lg_srcv->last_used) {
1959 /* Delay until the lg_srcv needs to expire */
1960 if (*tim_rem > lg_srcv->last_used + partial_timeout - now) {
1961 *tim_rem = lg_srcv->last_used + partial_timeout - now;
1962 ret = 1;
1963 }
1964 }
1965 }
1966 return ret;
1967}
1968#endif /* COAP_SERVER_SUPPORT */
1969
1970#if COAP_Q_BLOCK_SUPPORT
1971/*
1972 * pdu is always released before return IF COAP_SEND_INC_PDU
1973 */
1975coap_send_q_blocks(coap_session_t *session,
1976 coap_lg_xmit_t *lg_xmit,
1977 coap_block_b_t block,
1978 coap_pdu_t *pdu,
1979 coap_send_pdu_t send_pdu) {
1980 coap_pdu_t *block_pdu = NULL;
1981 coap_opt_filter_t drop_options;
1983 uint64_t token;
1984 const uint8_t *ptoken;
1985 uint8_t ltoken[8];
1986 size_t ltoken_length;
1987 uint32_t delayqueue_cnt = 0;
1988
1989 if (!lg_xmit) {
1990 if (send_pdu == COAP_SEND_INC_PDU)
1991 return coap_send_internal(session, pdu, NULL);
1992 return COAP_INVALID_MID;
1993 }
1994
1995 if (pdu->type == COAP_MESSAGE_CON) {
1996 coap_queue_t *delayqueue;
1997
1998 delayqueue_cnt = session->con_active +
1999 (send_pdu == COAP_SEND_INC_PDU ? 1 : 0);
2000 LL_FOREACH(session->delayqueue, delayqueue) {
2001 delayqueue_cnt++;
2002 }
2003 }
2004 pdu->lg_xmit = lg_xmit;
2005 if (block.m &&
2006 ((pdu->type == COAP_MESSAGE_NON &&
2007 ((block.num + 1) % COAP_MAX_PAYLOADS(session)) + 1 !=
2008 COAP_MAX_PAYLOADS(session)) ||
2009 (pdu->type == COAP_MESSAGE_ACK &&
2010 lg_xmit->option == COAP_OPTION_Q_BLOCK2) ||
2011 (pdu->type == COAP_MESSAGE_CON &&
2012 delayqueue_cnt < COAP_NSTART(session)) ||
2013 COAP_PROTO_RELIABLE(session->proto))) {
2014 /* Allocate next pdu if there is headroom */
2015 if (COAP_PDU_IS_RESPONSE(pdu)) {
2016 ptoken = pdu->actual_token.s;
2017 ltoken_length = pdu->actual_token.length;
2018 } else {
2019 token = STATE_TOKEN_FULL(lg_xmit->b.b1.state_token,++lg_xmit->b.b1.count);
2020 ltoken_length = coap_encode_var_safe8(ltoken, sizeof(token), token);
2021 ptoken = ltoken;
2022 }
2023
2024 memset(&drop_options, 0, sizeof(coap_opt_filter_t));
2025 coap_option_filter_set(&drop_options, lg_xmit->option);
2026 block_pdu = coap_pdu_duplicate_lkd(pdu, session,
2027 ltoken_length,
2028 ptoken, &drop_options);
2029 if (block_pdu->type == COAP_MESSAGE_ACK)
2030 block_pdu->type = COAP_MESSAGE_CON;
2031 }
2032
2033 /* Send initial pdu (which deletes 'pdu') */
2034 if (send_pdu == COAP_SEND_INC_PDU &&
2035 (mid = coap_send_internal(session, pdu, NULL)) == COAP_INVALID_MID) {
2036 /* Not expected, underlying issue somewhere */
2037 coap_delete_pdu_lkd(block_pdu);
2038 return COAP_INVALID_MID;
2039 }
2040
2041 while (block_pdu) {
2042 coap_pdu_t *t_pdu = NULL;
2043 uint8_t buf[8];
2044 size_t chunk = ((size_t)1 << (lg_xmit->blk_size + 4));
2045
2046 block.num++;
2047 lg_xmit->offset = block.num * chunk;
2048 block.m = lg_xmit->offset + chunk < lg_xmit->data_info->length;
2049 if (block.m && ((block_pdu->type == COAP_MESSAGE_NON &&
2050 (block.num % COAP_MAX_PAYLOADS(session)) + 1 !=
2051 COAP_MAX_PAYLOADS(session)) ||
2052 (block_pdu->type == COAP_MESSAGE_CON &&
2053 delayqueue_cnt + 1 < COAP_NSTART(session)) ||
2054 COAP_PROTO_RELIABLE(session->proto))) {
2055 /*
2056 * Send following block if
2057 * NON and more in MAX_PAYLOADS
2058 * CON and NSTART allows it (based on number in delayqueue)
2059 * Reliable transport
2060 */
2061 if (COAP_PDU_IS_RESPONSE(block_pdu)) {
2062 ptoken = block_pdu->actual_token.s;
2063 ltoken_length = block_pdu->actual_token.length;
2064 } else {
2065 token = STATE_TOKEN_FULL(lg_xmit->b.b1.state_token,++lg_xmit->b.b1.count);
2066 ltoken_length = coap_encode_var_safe8(ltoken, sizeof(token), token);
2067 ptoken = ltoken;
2068 }
2069 t_pdu = coap_pdu_duplicate_lkd(block_pdu, session,
2070 ltoken_length, ptoken, &drop_options);
2071 }
2072 if (!coap_update_option(block_pdu, lg_xmit->option,
2074 sizeof(buf),
2075 ((block.num) << 4) |
2076 (block.m << 3) |
2077 block.szx),
2078 buf)) {
2079 coap_log_warn("Internal update issue option\n");
2080 coap_delete_pdu_lkd(block_pdu);
2081 coap_delete_pdu_lkd(t_pdu);
2082 break;
2083 }
2084
2085 if (!coap_add_block(block_pdu,
2086 lg_xmit->data_info->length,
2087 lg_xmit->data_info->data,
2088 block.num,
2089 block.szx)) {
2090 coap_log_warn("Internal update issue data\n");
2091 coap_delete_pdu_lkd(block_pdu);
2092 coap_delete_pdu_lkd(t_pdu);
2093 break;
2094 }
2095 if (COAP_PDU_IS_RESPONSE(block_pdu)) {
2096 lg_xmit->last_block = block.num;
2097 }
2098 mid = coap_send_internal(session, block_pdu, NULL);
2099 if (mid == COAP_INVALID_MID) {
2100 /* Not expected, underlying issue somewhere */
2101 coap_delete_pdu_lkd(t_pdu);
2102 return COAP_INVALID_MID;
2103 }
2104 block_pdu = t_pdu;
2105 }
2106 if (!block.m) {
2107 lg_xmit->last_payload = 0;
2108 coap_ticks(&lg_xmit->last_all_sent);
2109 } else
2110 coap_ticks(&lg_xmit->last_payload);
2111 return mid;
2112}
2113
2114#if COAP_CLIENT_SUPPORT
2115/*
2116 * Return 1 if there is a future expire time, else 0.
2117 * Update tim_rem with remaining value if return is 1.
2118 */
2119int
2120coap_block_check_q_block1_xmit(coap_session_t *session, coap_tick_t now, coap_tick_t *tim_rem) {
2121 coap_lg_xmit_t *lg_xmit;
2122 coap_lg_xmit_t *q;
2123 coap_tick_t timed_out;
2124 int ret = 0;
2125
2126 *tim_rem = COAP_MAX_DELAY_TICKS;
2127 LL_FOREACH_SAFE(session->lg_xmit, lg_xmit, q) {
2128 coap_tick_t non_timeout = lg_xmit->non_timeout_random_ticks;
2129
2130 if (now <= non_timeout) {
2131 /* Too early in the startup cycle to have an accurate response */
2132 *tim_rem = non_timeout - now;
2133 return 1;
2134 }
2135 timed_out = now - non_timeout;
2136
2137 if (lg_xmit->last_payload) {
2138 if (lg_xmit->last_payload <= timed_out) {
2139 /* Send off the next MAX_PAYLOAD set */
2140 coap_block_b_t block;
2141 size_t chunk = (size_t)1 << (lg_xmit->blk_size + 4);
2142
2143 memset(&block, 0, sizeof(block));
2144 block.num = (uint32_t)(lg_xmit->offset / chunk);
2145 block.m = lg_xmit->offset + chunk < lg_xmit->data_info->length;
2146 block.szx = lg_xmit->blk_size;
2147 coap_send_q_blocks(session, lg_xmit, block, lg_xmit->sent_pdu, COAP_SEND_SKIP_PDU);
2148 if (*tim_rem > non_timeout) {
2149 *tim_rem = non_timeout;
2150 ret = 1;
2151 }
2152 } else {
2153 /* Delay until the next MAX_PAYLOAD needs to be sent off */
2154 if (*tim_rem > lg_xmit->last_payload - timed_out) {
2155 *tim_rem = lg_xmit->last_payload - timed_out;
2156 ret = 1;
2157 }
2158 }
2159 } else if (lg_xmit->last_all_sent) {
2160 non_timeout = COAP_NON_TIMEOUT_TICKS(session);
2161 if (lg_xmit->last_all_sent + 4 * non_timeout <= now) {
2162 /* Expire this entry */
2163 LL_DELETE(session->lg_xmit, lg_xmit);
2164 coap_block_delete_lg_xmit(session, lg_xmit);
2165 } else {
2166 /* Delay until the lg_xmit needs to expire */
2167 if (*tim_rem > lg_xmit->last_all_sent + 4 * non_timeout - now) {
2168 *tim_rem = lg_xmit->last_all_sent + 4 * non_timeout - now;
2169 ret = 1;
2170 }
2171 }
2172 }
2173 }
2174 return ret;
2175}
2176#endif /* COAP_CLIENT_SUPPORT */
2177
2178#if COAP_SERVER_SUPPORT
2179/*
2180 * Return 1 if there is a future expire time, else 0.
2181 * Update tim_rem with remaining value if return is 1.
2182 */
2183int
2184coap_block_check_q_block2_xmit(coap_session_t *session, coap_tick_t now, coap_tick_t *tim_rem) {
2185 coap_lg_xmit_t *lg_xmit;
2186 coap_lg_xmit_t *q;
2187 coap_tick_t timed_out;
2188 int ret = 0;
2189
2190 *tim_rem = (coap_tick_t)-1;
2191 LL_FOREACH_SAFE(session->lg_xmit, lg_xmit, q) {
2192 coap_tick_t non_timeout = lg_xmit->non_timeout_random_ticks;
2193
2194 if (now <= non_timeout) {
2195 /* Too early in the startup cycle to have an accurate response */
2196 *tim_rem = non_timeout - now;
2197 return 1;
2198 }
2199 timed_out = now - non_timeout;
2200
2201 if (lg_xmit->last_payload) {
2202 if (lg_xmit->last_payload <= timed_out) {
2203 /* Send off the next MAX_PAYLOAD set */
2204 coap_block_b_t block;
2205 size_t chunk = (size_t)1 << (lg_xmit->blk_size + 4);
2206
2207 memset(&block, 0, sizeof(block));
2208 block.num = (uint32_t)(lg_xmit->offset / chunk);
2209 block.m = lg_xmit->offset + chunk < lg_xmit->data_info->length;
2210 block.szx = lg_xmit->blk_size;
2211 if (block.num == (uint32_t)lg_xmit->last_block)
2212 coap_send_q_blocks(session, lg_xmit, block, lg_xmit->sent_pdu, COAP_SEND_SKIP_PDU);
2213 if (*tim_rem > non_timeout) {
2214 *tim_rem = non_timeout;
2215 ret = 1;
2216 }
2217 } else {
2218 /* Delay until the next MAX_PAYLOAD needs to be sent off */
2219 if (*tim_rem > lg_xmit->last_payload - timed_out) {
2220 *tim_rem = lg_xmit->last_payload - timed_out;
2221 ret = 1;
2222 }
2223 }
2224 } else if (lg_xmit->last_all_sent) {
2225 non_timeout = COAP_NON_TIMEOUT_TICKS(session);
2226 if (lg_xmit->last_all_sent + 4 * non_timeout <= now) {
2227 /* Expire this entry */
2228 LL_DELETE(session->lg_xmit, lg_xmit);
2229 coap_block_delete_lg_xmit(session, lg_xmit);
2230 } else {
2231 /* Delay until the lg_xmit needs to expire */
2232 if (*tim_rem > lg_xmit->last_all_sent + 4 * non_timeout - now) {
2233 *tim_rem = lg_xmit->last_all_sent + 4 * non_timeout - now;
2234 ret = 1;
2235 }
2236 }
2237 }
2238 }
2239 return ret;
2240}
2241#endif /* COAP_SERVER_SUPPORT */
2242#endif /* COAP_Q_BLOCK_SUPPORT */
2243
2244#if COAP_CLIENT_SUPPORT
2245/*
2246 * If Observe = 0, save the token away and return NULL
2247 * Else If Observe = 1, return the saved token for this block
2248 * Else, return NULL
2249 */
2250static coap_bin_const_t *
2251track_fetch_observe(coap_pdu_t *pdu, coap_lg_crcv_t *lg_crcv,
2252 uint32_t block_num, coap_bin_const_t *token) {
2253 /* Need to handle Observe for large FETCH */
2254 coap_opt_iterator_t opt_iter;
2256 &opt_iter);
2257
2258 if (opt && lg_crcv) {
2259 int observe_action = -1;
2260 coap_bin_const_t **tmp;
2261
2262 observe_action = coap_decode_var_bytes(coap_opt_value(opt),
2263 coap_opt_length(opt));
2264 if (observe_action == COAP_OBSERVE_ESTABLISH) {
2265 /* Save the token in lg_crcv */
2266 if (lg_crcv->obs_token_cnt <= block_num) {
2267 size_t i;
2268
2269 tmp = coap_realloc_type(COAP_STRING, lg_crcv->obs_token,
2270 (block_num + 1) * sizeof(lg_crcv->obs_token[0]));
2271 if (tmp == NULL)
2272 return NULL;
2273 lg_crcv->obs_token = tmp;
2274 for (i = lg_crcv->obs_token_cnt; i < block_num + 1; i++) {
2275 lg_crcv->obs_token[i] = NULL;
2276 }
2277 }
2278 coap_delete_bin_const(lg_crcv->obs_token[block_num]);
2279
2280 lg_crcv->obs_token_cnt = block_num + 1;
2281 lg_crcv->obs_token[block_num] = coap_new_bin_const(token->s,
2282 token->length);
2283 if (lg_crcv->obs_token[block_num] == NULL)
2284 return NULL;
2285 } else if (observe_action == COAP_OBSERVE_CANCEL) {
2286 /* Use the token in lg_crcv */
2287 if (block_num < lg_crcv->obs_token_cnt) {
2288 return lg_crcv->obs_token[block_num];
2289 }
2290 }
2291 }
2292 return NULL;
2293}
2294
2295#if COAP_Q_BLOCK_SUPPORT
2297coap_send_q_block1(coap_session_t *session,
2298 coap_block_b_t block,
2299 coap_pdu_t *request,
2300 coap_send_pdu_t send_request) {
2301 /* Need to send up to MAX_PAYLOAD blocks if this is a Q_BLOCK1 */
2302 coap_lg_xmit_t *lg_xmit;
2303 uint64_t token_match =
2305 request->actual_token.length));
2306
2307 LL_FOREACH(session->lg_xmit, lg_xmit) {
2308 if (lg_xmit->option == COAP_OPTION_Q_BLOCK1 &&
2309 (token_match == STATE_TOKEN_BASE(lg_xmit->b.b1.state_token) ||
2310 token_match ==
2312 lg_xmit->b.b1.app_token->length))))
2313 break;
2314 /* try out the next one */
2315 }
2316 return coap_send_q_blocks(session, lg_xmit, block, request, send_request);
2317}
2318#endif /* COAP_Q_BLOCK_SUPPORT */
2319#endif /* COAP_CLIENT_SUPPORT */
2320
2321#if COAP_SERVER_SUPPORT
2322#if COAP_Q_BLOCK_SUPPORT
2323/*
2324 * response is always released before return IF COAP_SEND_INC_PDU
2325 */
2327coap_send_q_block2(coap_session_t *session,
2328 coap_resource_t *resource,
2329 const coap_string_t *query,
2330 coap_pdu_code_t request_method,
2331 coap_block_b_t block,
2332 coap_pdu_t *response,
2333 coap_send_pdu_t send_response) {
2334 /* Need to send up to MAX_PAYLOAD blocks if this is a Q_BLOCK2 */
2335 coap_lg_xmit_t *lg_xmit;
2336 coap_string_t empty = { 0, NULL};
2337
2338 LL_FOREACH(session->lg_xmit, lg_xmit) {
2339 if (lg_xmit->option == COAP_OPTION_Q_BLOCK2 &&
2340 resource == lg_xmit->b.b2.resource &&
2341 request_method == lg_xmit->b.b2.request_method &&
2342 coap_string_equal(query ? query : &empty,
2343 lg_xmit->b.b2.query ? lg_xmit->b.b2.query : &empty))
2344 break;
2345 }
2346 return coap_send_q_blocks(session, lg_xmit, block, response, send_response);
2347}
2348#endif /* COAP_Q_BLOCK_SUPPORT */
2349#endif /* COAP_SERVER_SUPPORT */
2350
2351static void
2353 coap_lg_xmit_data_t *data_info) {
2354 if (!data_info)
2355 return;
2356 if (data_info->ref > 0) {
2357 data_info->ref--;
2358 return;
2359 }
2360 if (data_info->release_func) {
2361 coap_lock_callback(data_info->release_func(session,
2362 data_info->app_ptr));
2363 }
2364 coap_free_type(COAP_STRING, data_info);
2365}
2366
2367#if COAP_CLIENT_SUPPORT
2368#if COAP_Q_BLOCK_SUPPORT
2369/*
2370 * Send out a test PDU for Q-Block.
2371 */
2373coap_block_test_q_block(coap_session_t *session, coap_pdu_t *actual) {
2374 coap_pdu_t *pdu;
2375 uint8_t token[8];
2376 size_t token_len;
2377 uint8_t buf[4];
2378 coap_mid_t mid;
2379
2380#if NDEBUG
2381 (void)actual;
2382#endif /* NDEBUG */
2383 assert(session->block_mode & COAP_BLOCK_TRY_Q_BLOCK &&
2384 session->type == COAP_SESSION_TYPE_CLIENT &&
2385 COAP_PDU_IS_REQUEST(actual));
2386
2387 coap_log_debug("Testing for Q-Block support\n");
2388 /* RFC9177 Section 4.1 when checking if available */
2390 coap_new_message_id_lkd(session),
2392 if (!pdu) {
2393 return COAP_INVALID_MID;
2394 }
2395
2396 coap_session_new_token(session, &token_len, token);
2397 coap_add_token(pdu, token_len, token);
2398 /* Use a resource that the server MUST support (.well-known/core) */
2400 11, (const uint8_t *)".well-known");
2402 4, (const uint8_t *)"core");
2403 /*
2404 * M needs to be unset as 'asking' for only the first block using
2405 * Q-Block2 as a test for server support.
2406 * See RFC9177 Section 4.4 Using the Q-Block2 Option.
2407 *
2408 * As the client is asking for 16 byte chunks, it is unlikely that
2409 * the .well-known/core response will be 16 bytes or less, so
2410 * if the server supports Q-Block, it will be forced to respond with
2411 * a Q-Block2, so the client can detect the server Q-Block support.
2412 */
2414 coap_encode_var_safe(buf, sizeof(buf),
2415 (0 << 4) | (0 << 3) | 0),
2416 buf);
2417 set_block_mode_probe_q(session->block_mode);
2418 mid = coap_send_internal(session, pdu, NULL);
2419 if (mid == COAP_INVALID_MID)
2420 return COAP_INVALID_MID;
2421 session->remote_test_mid = mid;
2422 return mid;
2423}
2424#endif /* COAP_Q_BLOCK_SUPPORT */
2425
2428 coap_lg_xmit_t *lg_xmit) {
2429 coap_block_b_t block;
2430 coap_lg_crcv_t *lg_crcv;
2431 uint64_t state_token = STATE_TOKEN_FULL(++session->tx_token, 1);
2432
2433 lg_crcv = coap_malloc_type(COAP_LG_CRCV, sizeof(coap_lg_crcv_t));
2434
2435 if (lg_crcv == NULL)
2436 return NULL;
2437
2438 coap_log_debug("** %s: lg_crcv %p initialized - stateless token xxxxx%011llx\n",
2439 coap_session_str(session), (void *)lg_crcv,
2440 STATE_TOKEN_BASE(state_token));
2441 memset(lg_crcv, 0, sizeof(coap_lg_crcv_t));
2442 lg_crcv->initial = 1;
2443 coap_ticks(&lg_crcv->last_used);
2444 /* Keep a copy of the sent pdu */
2445 lg_crcv->sent_pdu = coap_pdu_reference_lkd(pdu);
2446 if (lg_xmit) {
2447 coap_opt_iterator_t opt_iter;
2448 coap_opt_t *opt;
2449
2450 opt = coap_check_option(pdu, COAP_OPTION_OBSERVE, &opt_iter);
2451
2452 if (opt) {
2453 int observe_action;
2454
2455 observe_action = coap_decode_var_bytes(coap_opt_value(opt),
2456 coap_opt_length(opt));
2457 if (observe_action == COAP_OBSERVE_ESTABLISH) {
2458 /* Need to keep information for Observe Cancel */
2459 size_t data_len;
2460 const uint8_t *data;
2461
2462 if (coap_get_data(pdu, &data_len, &data)) {
2463 if (data_len < lg_xmit->data_info->length) {
2464 lg_xmit->data_info->ref++;
2465 lg_crcv->obs_data = lg_xmit->data_info;
2466 }
2467 }
2468 }
2469 }
2470 }
2471
2472 /* Need to keep original token for updating response PDUs */
2473 lg_crcv->app_token = coap_new_binary(pdu->actual_token.length);
2474 if (!lg_crcv->app_token) {
2475 coap_block_delete_lg_crcv(session, lg_crcv);
2476 return NULL;
2477 }
2478 memcpy(lg_crcv->app_token->s, pdu->actual_token.s, pdu->actual_token.length);
2479
2480 /* Need to set up a base token for actual communications if retries needed */
2481 lg_crcv->retry_counter = 1;
2482 lg_crcv->state_token = state_token;
2483 coap_address_copy(&lg_crcv->upstream, &session->addr_info.remote);
2484
2485 if (pdu->code == COAP_REQUEST_CODE_FETCH) {
2486 coap_bin_const_t *new_token;
2487
2488 /* Need to save/restore Observe Token for large FETCH */
2489 new_token = track_fetch_observe(pdu, lg_crcv, 0, &pdu->actual_token);
2490 if (new_token)
2491 coap_update_token(pdu, new_token->length, new_token->s);
2492 }
2493
2494 if (coap_get_block_b(session, pdu, COAP_OPTION_BLOCK1, &block)) {
2495 /* In case it is there - must not be in continuing request PDUs */
2496 lg_crcv->o_block_option = COAP_OPTION_BLOCK1;
2497 lg_crcv->o_blk_size = block.aszx;
2498 }
2499
2500 return lg_crcv;
2501}
2502
2503void
2505 coap_lg_crcv_t *lg_crcv) {
2506 size_t i;
2507
2508#if (COAP_MAX_LOGGING_LEVEL < _COAP_LOG_DEBUG)
2509 (void)session;
2510#endif
2511 if (lg_crcv == NULL)
2512 return;
2513
2515 if (lg_crcv->obs_data) {
2516 coap_block_release_lg_xmit_data(session, lg_crcv->obs_data);
2517 lg_crcv->obs_data = NULL;
2518 }
2519 coap_address_copy(&session->addr_info.remote, &lg_crcv->upstream);
2520 coap_log_debug("** %s: lg_crcv %p released\n",
2521 coap_session_str(session), (void *)lg_crcv);
2522 coap_delete_binary(lg_crcv->app_token);
2523 for (i = 0; i < lg_crcv->obs_token_cnt; i++) {
2524 coap_delete_bin_const(lg_crcv->obs_token[i]);
2525 }
2527 coap_delete_pdu_lkd(lg_crcv->sent_pdu);
2528 coap_free_type(COAP_LG_CRCV, lg_crcv);
2529}
2530#endif /* COAP_CLIENT_SUPPORT */
2531
2532#if COAP_SERVER_SUPPORT
2533void
2535 coap_lg_srcv_t *lg_srcv) {
2536#if (COAP_MAX_LOGGING_LEVEL < _COAP_LOG_DEBUG)
2537 (void)session;
2538#endif
2539 if (lg_srcv == NULL)
2540 return;
2541
2545 coap_log_debug("** %s: lg_srcv %p released\n",
2546 coap_session_str(session), (void *)lg_srcv);
2547 coap_free_type(COAP_LG_SRCV, lg_srcv);
2548}
2549#endif /* COAP_SERVER_SUPPORT */
2550
2551void
2553 coap_lg_xmit_t *lg_xmit) {
2554 if (lg_xmit == NULL)
2555 return;
2556
2557 coap_block_release_lg_xmit_data(session, lg_xmit->data_info);
2558 if (COAP_PDU_IS_REQUEST(lg_xmit->sent_pdu))
2559 coap_delete_binary(lg_xmit->b.b1.app_token);
2560 else
2561 coap_delete_string(lg_xmit->b.b2.query);
2562 coap_delete_pdu_lkd(lg_xmit->sent_pdu);
2563
2564 coap_log_debug("** %s: lg_xmit %p released\n",
2565 coap_session_str(session), (void *)lg_xmit);
2566 coap_free_type(COAP_LG_XMIT, lg_xmit);
2567}
2568
2569#if COAP_SERVER_SUPPORT
2570typedef struct {
2571 uint32_t num;
2572 int is_continue;
2573} send_track;
2574
2575static int
2576add_block_send(uint32_t num, int is_continue, send_track *out_blocks,
2577 uint32_t *count, uint32_t max_count) {
2578 uint32_t i;
2579
2580 for (i = 0; i < *count && *count < max_count; i++) {
2581 if (num == out_blocks[i].num)
2582 return 0;
2583 else if (num < out_blocks[i].num) {
2584 if (*count - i > 1)
2585 memmove(&out_blocks[i], &out_blocks[i+1], *count - i -1);
2586 out_blocks[i].num = num;
2587 out_blocks[i].is_continue = is_continue;
2588 (*count)++;
2589 return 1;
2590 }
2591 }
2592 if (*count < max_count) {
2593 out_blocks[i].num = num;
2594 out_blocks[i].is_continue = is_continue;
2595 (*count)++;
2596 return 1;
2597 }
2598 return 0;
2599}
2600
2601/*
2602 * Need to see if this is a request for the next block of a large body
2603 * transfer. If so, need to initiate the response with the next blocks
2604 * and not trouble the application.
2605 *
2606 * If additional responses needed, then these are expicitly sent out and
2607 * 'response' is updated to be the last response to be sent. There can be
2608 * multiple Q-Block2 in the request, as well as the 'Continue' Q-Block2
2609 * request.
2610 *
2611 * This is set up using coap_add_data_large_response_lkd()
2612 *
2613 * Server is sending a large data response to GET / observe (Block2)
2614 *
2615 * Return: 0 Call application handler
2616 * 1 Do not call application handler - just send the built response
2617 */
2618int
2620 coap_pdu_t *pdu,
2621 coap_pdu_t *response,
2622 coap_resource_t *resource,
2623 coap_string_t *query) {
2624 coap_lg_xmit_t *lg_xmit = NULL;
2625 coap_block_b_t block;
2626 coap_block_b_t alt_block;
2627 uint16_t block_opt = 0;
2628 send_track *out_blocks = NULL;
2629 const char *error_phrase;
2630 coap_opt_iterator_t opt_iter;
2631 size_t chunk;
2632 coap_opt_iterator_t opt_b_iter;
2633 coap_opt_t *option;
2634 uint32_t request_cnt, i;
2635 coap_opt_t *etag_opt = NULL;
2636 coap_pdu_t *out_pdu = response;
2637#if COAP_Q_BLOCK_SUPPORT
2638 size_t max_block;
2639
2640 /* Is client indicating that it supports Q_BLOCK2 ? */
2641 if (coap_get_block_b(session, pdu, COAP_OPTION_Q_BLOCK2, &block)) {
2642 if (!(session->block_mode & COAP_BLOCK_HAS_Q_BLOCK))
2643 set_block_mode_has_q(session->block_mode);
2644 block_opt = COAP_OPTION_Q_BLOCK2;
2645 }
2646#endif /* COAP_Q_BLOCK_SUPPORT */
2647 if (coap_get_block_b(session, pdu, COAP_OPTION_BLOCK2, &alt_block)) {
2648 if (block_opt) {
2649 coap_log_warn("Block2 and Q-Block2 cannot be in the same request\n");
2650 coap_add_data(response, sizeof("Both Block2 and Q-Block2 invalid")-1,
2651 (const uint8_t *)"Both Block2 and Q-Block2 invalid");
2652 response->code = COAP_RESPONSE_CODE(400);
2653 goto skip_app_handler;
2654 }
2655 block = alt_block;
2656 block_opt = COAP_OPTION_BLOCK2;
2657 }
2658 if (block_opt == 0)
2659 return 0;
2660 if (block.num == 0) {
2661 /* Get a fresh copy of the data */
2662 return 0;
2663 }
2664 lg_xmit = coap_find_lg_xmit_response(session, pdu, resource, query);
2665 if (lg_xmit == NULL)
2666 return 0;
2667
2668#if COAP_Q_BLOCK_SUPPORT
2669 out_blocks = coap_malloc_type(COAP_STRING, sizeof(send_track) * COAP_MAX_PAYLOADS(session));
2670#else /* ! COAP_Q_BLOCK_SUPPORT */
2671 out_blocks = coap_malloc_type(COAP_STRING, sizeof(send_track));
2672#endif /* ! COAP_Q_BLOCK_SUPPORT */
2673 if (!out_blocks) {
2674 goto internal_issue;
2675 }
2676
2677 /* lg_xmit (response) found */
2678
2679 etag_opt = coap_check_option(pdu, COAP_OPTION_ETAG, &opt_iter);
2680 if (etag_opt) {
2681 /* There may be multiple ETag - need to check each one */
2682 coap_option_iterator_init(pdu, &opt_iter, COAP_OPT_ALL);
2683 while ((etag_opt = coap_option_next(&opt_iter))) {
2684 if (opt_iter.number == COAP_OPTION_ETAG) {
2685 uint64_t etag = coap_decode_var_bytes8(coap_opt_value(etag_opt),
2686 coap_opt_length(etag_opt));
2687 if (etag == lg_xmit->b.b2.etag) {
2688 break;
2689 }
2690 }
2691 }
2692 if (!etag_opt) {
2693 /* Not a match - pass up to a higher level */
2694 return 0;
2695 }
2696 }
2697 out_pdu->code = lg_xmit->sent_pdu->code;
2698 coap_ticks(&lg_xmit->last_obs);
2699 lg_xmit->last_all_sent = 0;
2700
2701 chunk = (size_t)1 << (lg_xmit->blk_size + 4);
2702 if (block_opt) {
2703 if (block.bert) {
2704 coap_log_debug("found Block option, block is BERT, block nr. %u, M %d\n",
2705 block.num, block.m);
2706 } else {
2707 coap_log_debug("found Block option, block size is %u, block nr. %u, M %d\n",
2708 1 << (block.szx + 4), block.num, block.m);
2709 }
2710 if (block.bert == 0 && block.szx != lg_xmit->blk_size) {
2711 if (block.num == 0) {
2712 if ((lg_xmit->offset + chunk) % ((size_t)1 << (block.szx + 4)) == 0) {
2713 /*
2714 * Recompute the block number of the previous packet given
2715 * the new block size
2716 */
2717 block.num = (uint32_t)(((lg_xmit->offset + chunk) >> (block.szx + 4)) - 1);
2718 lg_xmit->blk_size = block.szx;
2719 chunk = (size_t)1 << (lg_xmit->blk_size + 4);
2720 lg_xmit->offset = block.num * chunk;
2721 coap_log_debug("new Block size is %u, block number %u completed\n",
2722 1 << (block.szx + 4), block.num);
2723 } else {
2724 coap_log_debug("ignoring request to increase Block size, "
2725 "next block is not aligned on requested block size "
2726 "boundary. (%zu x %u mod %u = %zu (which is not 0)\n",
2727 lg_xmit->offset/chunk + 1, (1 << (lg_xmit->blk_size + 4)),
2728 (1 << (block.szx + 4)),
2729 (lg_xmit->offset + chunk) % ((size_t)1 << (block.szx + 4)));
2730 }
2731 } else {
2732 coap_log_debug("ignoring request to change Block size from %u to %u\n",
2733 (1 << (lg_xmit->blk_size + 4)), (1 << (block.szx + 4)));
2734 block.szx = block.aszx = lg_xmit->blk_size;
2735 }
2736 }
2737 }
2738
2739 /*
2740 * Need to check if there are multiple Q-Block2 requests. If so, they
2741 * need to be sent out in order of requests with the final request being
2742 * handled as per singular Block 2 request.
2743 */
2744 request_cnt = 0;
2745#if COAP_Q_BLOCK_SUPPORT
2746 max_block = (lg_xmit->data_info->length + chunk - 1)/chunk;
2747#endif /* COAP_Q_BLOCK_SUPPORT */
2748 coap_option_iterator_init(pdu, &opt_b_iter, COAP_OPT_ALL);
2749 while ((option = coap_option_next(&opt_b_iter))) {
2750 uint32_t num;
2751 if (opt_b_iter.number != lg_xmit->option)
2752 continue;
2753 num = coap_opt_block_num(option);
2754 if (num > 0xFFFFF) /* 20 bits max for num */
2755 continue;
2756 if (block.aszx != COAP_OPT_BLOCK_SZX(option)) {
2757 coap_add_data(response,
2758 sizeof("Changing blocksize during request invalid")-1,
2759 (const uint8_t *)"Changing blocksize during request invalid");
2760 response->code = COAP_RESPONSE_CODE(400);
2761 goto skip_app_handler;
2762 }
2763#if COAP_Q_BLOCK_SUPPORT
2764 if (COAP_OPT_BLOCK_MORE(option) && lg_xmit->option == COAP_OPTION_Q_BLOCK2) {
2765 if ((num % COAP_MAX_PAYLOADS(session)) == 0) {
2766 if (num == 0) {
2767 /* This is a repeat request for everything - hmm */
2768 goto call_app_handler;
2769 }
2770 /* 'Continue' request */
2771 for (i = 0; i < COAP_MAX_PAYLOADS(session) &&
2772 num + i < max_block; i++) {
2773 add_block_send(num + i, 1, out_blocks, &request_cnt,
2774 COAP_MAX_PAYLOADS(session));
2775 lg_xmit->last_block = num + i;
2776 }
2777 } else {
2778 /* Requesting remaining payloads in this MAX_PAYLOADS */
2779 for (i = 0; i < COAP_MAX_PAYLOADS(session) -
2780 num % COAP_MAX_PAYLOADS(session) &&
2781 num + i < max_block; i++) {
2782 add_block_send(num + i, 0, out_blocks, &request_cnt,
2783 COAP_MAX_PAYLOADS(session));
2784 }
2785 }
2786 } else
2787 add_block_send(num, 0, out_blocks, &request_cnt,
2788 COAP_MAX_PAYLOADS(session));
2789#else /* ! COAP_Q_BLOCK_SUPPORT */
2790 add_block_send(num, 0, out_blocks, &request_cnt, 1);
2791 break;
2792#endif /* ! COAP_Q_BLOCK_SUPPORT */
2793 }
2794 if (request_cnt == 0) {
2795 /* Block2 or Q-Block2 not found - give them the first block */
2796 block.szx = lg_xmit->blk_size;
2797 lg_xmit->offset = 0;
2798 out_blocks[0].num = 0;
2799 out_blocks[0].is_continue = 0;
2800 request_cnt = 1;
2801 }
2802
2803 for (i = 0; i < request_cnt; i++) {
2804 uint8_t buf[8];
2805
2806 block.num = out_blocks[i].num;
2807 lg_xmit->offset = block.num * chunk;
2808
2809 if (i + 1 < request_cnt) {
2810 /* Need to set up a copy of the pdu to send */
2811 coap_opt_filter_t drop_options;
2812
2813 memset(&drop_options, 0, sizeof(coap_opt_filter_t));
2814 if (block.num != 0)
2816 if (out_blocks[i].is_continue) {
2817 out_pdu = coap_pdu_duplicate_lkd(lg_xmit->sent_pdu, session,
2818 lg_xmit->sent_pdu->actual_token.length,
2819 lg_xmit->sent_pdu->actual_token.s, &drop_options);
2820 } else {
2821 out_pdu = coap_pdu_duplicate_lkd(lg_xmit->sent_pdu, session, pdu->actual_token.length,
2822 pdu->actual_token.s, &drop_options);
2823 }
2824 if (!out_pdu) {
2825 goto internal_issue;
2826 }
2827 } else {
2828 if (out_blocks[i].is_continue)
2829 coap_update_token(response, lg_xmit->sent_pdu->actual_token.length,
2830 lg_xmit->sent_pdu->actual_token.s);
2831 /*
2832 * Copy the options across and then fix the block option
2833 *
2834 * Need to drop Observe option if Block2 and block.num != 0
2835 */
2836 coap_option_iterator_init(lg_xmit->sent_pdu, &opt_iter, COAP_OPT_ALL);
2837 while ((option = coap_option_next(&opt_iter))) {
2838 if (opt_iter.number == COAP_OPTION_OBSERVE && block.num != 0)
2839 continue;
2840 if (!coap_insert_option(response, opt_iter.number,
2841 coap_opt_length(option),
2842 coap_opt_value(option))) {
2843 goto internal_issue;
2844 }
2845 }
2846 out_pdu = response;
2847 }
2848 if (pdu->type == COAP_MESSAGE_NON)
2849 out_pdu->type = COAP_MESSAGE_NON;
2850 if (block.bert) {
2851 size_t token_options = pdu->data ? (size_t)(pdu->data - pdu->token) : pdu->used_size;
2852 block.m = (lg_xmit->data_info->length - lg_xmit->offset) >
2853 ((out_pdu->max_size - token_options) /1024) * 1024;
2854 } else {
2855 block.m = (lg_xmit->offset + chunk) < lg_xmit->data_info->length;
2856 }
2857 if (!coap_update_option(out_pdu, lg_xmit->option,
2859 sizeof(buf),
2860 (block.num << 4) |
2861 (block.m << 3) |
2862 block.aszx),
2863 buf)) {
2864 goto internal_issue;
2865 }
2866 if (!(lg_xmit->offset + chunk < lg_xmit->data_info->length)) {
2867 /* Last block - keep in cache for 4 * ACK_TIMOUT */
2868 coap_ticks(&lg_xmit->last_all_sent);
2869 }
2870 if (lg_xmit->b.b2.maxage_expire) {
2871 coap_tick_t now;
2872 coap_time_t rem;
2873
2874 if (!(lg_xmit->offset + chunk < lg_xmit->data_info->length)) {
2875 /* Last block - keep in cache for 4 * ACK_TIMOUT */
2876 coap_ticks(&lg_xmit->last_all_sent);
2877 }
2878 coap_ticks(&now);
2879 rem = coap_ticks_to_rt(now);
2880 if (lg_xmit->b.b2.maxage_expire > rem) {
2881 rem = lg_xmit->b.b2.maxage_expire - rem;
2882 } else {
2883 rem = 0;
2884 /* Entry needs to be expired */
2885 coap_ticks(&lg_xmit->last_all_sent);
2886 }
2889 sizeof(buf),
2890 rem),
2891 buf)) {
2892 goto internal_issue;
2893 }
2894 }
2895
2896 if (!coap_add_block_b_data(out_pdu,
2897 lg_xmit->data_info->length,
2898 lg_xmit->data_info->data,
2899 &block)) {
2900 goto internal_issue;
2901 }
2902 if (i + 1 < request_cnt) {
2903 coap_ticks(&lg_xmit->last_sent);
2904 coap_send_internal(session, out_pdu, NULL);
2905 }
2906 }
2907 coap_ticks(&lg_xmit->last_payload);
2908 goto skip_app_handler;
2909#if COAP_Q_BLOCK_SUPPORT
2910call_app_handler:
2911 coap_free_type(COAP_STRING, out_blocks);
2912 return 0;
2913#endif /* COAP_Q_BLOCK_SUPPORT */
2914
2915internal_issue:
2916 response->code = COAP_RESPONSE_CODE(500);
2917 error_phrase = coap_response_phrase(response->code);
2918 coap_add_data(response, strlen(error_phrase),
2919 (const uint8_t *)error_phrase);
2920 /* Keep in cache for 4 * ACK_TIMOUT incase of retry */
2921 if (lg_xmit)
2922 coap_ticks(&lg_xmit->last_all_sent);
2923
2924skip_app_handler:
2925 coap_free_type(COAP_STRING, out_blocks);
2926 return 1;
2927}
2928#endif /* COAP_SERVER_SUPPORT */
2929
2930static int
2931update_received_blocks(coap_rblock_t *rec_blocks, uint32_t block_num, uint32_t block_m) {
2932 uint32_t i;
2933
2934 if (rec_blocks->total_blocks && block_num + 1 > rec_blocks->total_blocks) {
2935 /* received block number greater than Block No defined when More bit unset */
2936 return 0;
2937 }
2938
2939 /* Reset as there is activity */
2940 rec_blocks->retry = 0;
2941
2942 for (i = 0; i < rec_blocks->used; i++) {
2943 if (block_num >= rec_blocks->range[i].begin &&
2944 block_num <= rec_blocks->range[i].end)
2945 break;
2946
2947 if (block_num < rec_blocks->range[i].begin) {
2948 if (block_num + 1 == rec_blocks->range[i].begin) {
2949 rec_blocks->range[i].begin = block_num;
2950 } else {
2951 /* Need to insert a new range */
2952 if (rec_blocks->used == COAP_RBLOCK_CNT -1)
2953 /* Too many losses */
2954 return 0;
2955 memmove(&rec_blocks->range[i+1], &rec_blocks->range[i],
2956 (rec_blocks->used - i) * sizeof(rec_blocks->range[0]));
2957 rec_blocks->range[i].begin = rec_blocks->range[i].end = block_num;
2958 rec_blocks->used++;
2959 }
2960 break;
2961 }
2962 if (block_num == rec_blocks->range[i].end + 1) {
2963 rec_blocks->range[i].end = block_num;
2964 if (i + 1 < rec_blocks->used) {
2965 if (rec_blocks->range[i+1].begin == block_num + 1) {
2966 /* Merge the 2 ranges */
2967 rec_blocks->range[i].end = rec_blocks->range[i+1].end;
2968 if (i+2 < rec_blocks->used) {
2969 memmove(&rec_blocks->range[i+1], &rec_blocks->range[i+2],
2970 (rec_blocks->used - (i+2)) * sizeof(rec_blocks->range[0]));
2971 }
2972 rec_blocks->used--;
2973 }
2974 }
2975 break;
2976 }
2977 }
2978 if (i == rec_blocks->used) {
2979 if (rec_blocks->used == COAP_RBLOCK_CNT -1)
2980 /* Too many losses */
2981 return 0;
2982 rec_blocks->range[i].begin = rec_blocks->range[i].end = block_num;
2983 rec_blocks->used++;
2984 }
2985 if (!block_m)
2986 rec_blocks->total_blocks = block_num + 1;
2987
2988 coap_ticks(&rec_blocks->last_seen);
2989 return 1;
2990}
2991
2992#if COAP_SERVER_SUPPORT
2993/*
2994 * Need to check if this is a large PUT / POST etc. using multiple blocks
2995 *
2996 * Server receiving PUT/POST etc. of a large amount of data (Block1)
2997 *
2998 * Return: 0 Call application handler
2999 * 1 Do not call application handler - just send the built response
3000 */
3001int
3003 coap_session_t *session,
3004 coap_pdu_t *pdu,
3005 coap_pdu_t *response,
3006 coap_resource_t *resource,
3007 coap_string_t *uri_path,
3008 coap_opt_t *observe,
3009 int *added_block,
3010 coap_lg_srcv_t **pfree_lg_srcv) {
3011 size_t length = 0;
3012 const uint8_t *data = NULL;
3013 size_t offset = 0;
3014 size_t total = 0;
3015 coap_block_b_t block;
3016 coap_opt_iterator_t opt_iter;
3017 uint16_t block_option = 0;
3018 coap_lg_srcv_t *lg_srcv;
3019 coap_opt_t *size_opt;
3020 coap_opt_t *fmt_opt;
3021 uint16_t fmt;
3022 coap_opt_t *rtag_opt;
3023 size_t rtag_length;
3024 const uint8_t *rtag;
3025 uint32_t max_block_szx;
3026 int update_data;
3027 unsigned int saved_num;
3028 size_t saved_offset;
3029
3030 *added_block = 0;
3031 *pfree_lg_srcv = NULL;
3032 coap_get_data_large(pdu, &length, &data, &offset, &total);
3033 pdu->body_offset = 0;
3034 pdu->body_total = length;
3035
3036 if (coap_get_block_b(session, pdu, COAP_OPTION_BLOCK1, &block)) {
3037 block_option = COAP_OPTION_BLOCK1;
3038#if COAP_Q_BLOCK_SUPPORT
3039 if (coap_check_option(pdu, COAP_OPTION_Q_BLOCK1, &opt_iter)) {
3040 /* Cannot handle Q-Block1 as well */
3041 coap_add_data(response, sizeof("Block1 + Q-Block1 together")-1,
3042 (const uint8_t *)"Block1 + Q-Block1 together");
3043 response->code = COAP_RESPONSE_CODE(402);
3044 goto skip_app_handler;
3045 }
3046#endif /* COAP_Q_BLOCK_SUPPORT */
3047 }
3048#if COAP_Q_BLOCK_SUPPORT
3049 else if (coap_get_block_b(session, pdu, COAP_OPTION_Q_BLOCK1, &block)) {
3050 block_option = COAP_OPTION_Q_BLOCK1;
3051 set_block_mode_has_q(session->block_mode);
3052 }
3053#endif /* COAP_Q_BLOCK_SUPPORT */
3054 if (!block_option ||
3055 (block_option == COAP_OPTION_BLOCK1 && block.num == 0 && block.m == 0)) {
3056 /* Not blocked, or a single block */
3057 goto call_app_handler;
3058 }
3059
3060 size_opt = coap_check_option(pdu,
3062 &opt_iter);
3063 fmt_opt = coap_check_option(pdu,
3065 &opt_iter);
3066 fmt = fmt_opt ? coap_decode_var_bytes(coap_opt_value(fmt_opt),
3067 coap_opt_length(fmt_opt)) :
3069 rtag_opt = coap_check_option(pdu,
3071 &opt_iter);
3072 rtag_length = rtag_opt ? coap_opt_length(rtag_opt) : 0;
3073 rtag = rtag_opt ? coap_opt_value(rtag_opt) : NULL;
3074
3075 if (length > block.chunk_size) {
3076 coap_log_debug("block: Oversized packet - reduced to %"PRIu32" from %zu\n",
3077 block.chunk_size, length);
3078 length = block.chunk_size;
3079 } else if (!block.bert && block.m && length != block.chunk_size) {
3080 coap_log_info("block: Undersized packet chunk %"PRIu32" got %zu\n",
3081 block.chunk_size, length);
3082 response->code = COAP_RESPONSE_CODE(400);
3083 goto skip_app_handler;
3084 }
3085 total = size_opt ? coap_decode_var_bytes(coap_opt_value(size_opt),
3086 coap_opt_length(size_opt)) : 0;
3087 offset = block.num << (block.szx + 4);
3088
3089 if (!(session->block_mode &
3090#if COAP_Q_BLOCK_SUPPORT
3092#else /* COAP_Q_BLOCK_SUPPORT */
3094#endif /* COAP_Q_BLOCK_SUPPORT */
3095 && !block.bert) {
3096 uint8_t buf[4];
3097
3098 /* Ask for the next block */
3099 coap_insert_option(response, block_option,
3100 coap_encode_var_safe(buf, sizeof(buf),
3101 (block.num << 4) |
3102 (block.m << 3) |
3103 block.aszx),
3104 buf);
3105 /* Not re-assembling or checking for receipt order */
3106 pdu->body_data = data;
3107 pdu->body_length = length;
3108 pdu->body_offset = offset;
3109 if (total < (length + offset + (block.m ? 1 : 0)))
3110 total = length + offset + (block.m ? 1 : 0);
3111 pdu->body_total = total;
3112 *added_block = block.m;
3113 /* The application is responsible for returning the correct 2.01/2.04/2.31 etc. */
3114 goto call_app_handler;
3115 }
3116
3117 /*
3118 * locate the lg_srcv
3119 */
3120 LL_FOREACH(session->lg_srcv, lg_srcv) {
3121 if (rtag_opt || lg_srcv->rtag_set == 1) {
3122 if (!(rtag_opt && lg_srcv->rtag_set == 1))
3123 continue;
3124 if (lg_srcv->rtag_length != rtag_length ||
3125 memcmp(lg_srcv->rtag, rtag, rtag_length) != 0)
3126 continue;
3127 }
3128 if (resource == lg_srcv->resource) {
3129 break;
3130 }
3131 if ((lg_srcv->resource == context->unknown_resource ||
3132 resource == context->proxy_uri_resource) &&
3133 coap_string_equal(uri_path, lg_srcv->uri_path))
3134 break;
3135 }
3136
3137 if (!lg_srcv && block.num != 0 && session->block_mode & COAP_BLOCK_NOT_RANDOM_BLOCK1) {
3138 coap_add_data(response, sizeof("Missing block 0")-1,
3139 (const uint8_t *)"Missing block 0");
3140 response->code = COAP_RESPONSE_CODE(408);
3141 goto skip_app_handler;
3142 }
3143
3144 if (!lg_srcv) {
3145 /* Allocate lg_srcv to use for tracking */
3146 lg_srcv = coap_malloc_type(COAP_LG_SRCV, sizeof(coap_lg_srcv_t));
3147 if (lg_srcv == NULL) {
3148 coap_add_data(response, sizeof("Memory issue")-1,
3149 (const uint8_t *)"Memory issue");
3150 response->code = COAP_RESPONSE_CODE(500);
3151 goto skip_app_handler;
3152 }
3153 coap_log_debug("** %s: lg_srcv %p initialized\n",
3154 coap_session_str(session), (void *)lg_srcv);
3155 memset(lg_srcv, 0, sizeof(coap_lg_srcv_t));
3156 lg_srcv->resource = resource;
3157 if (resource == context->unknown_resource ||
3158 resource == context->proxy_uri_resource)
3159 lg_srcv->uri_path = coap_new_str_const(uri_path->s, uri_path->length);
3160 lg_srcv->content_format = fmt;
3161 lg_srcv->total_len = total;
3162 max_block_szx = COAP_BLOCK_MAX_SIZE_GET(session->block_mode);
3163 if (!block.bert && block.num == 0 && max_block_szx != 0 &&
3164 max_block_szx < block.szx) {
3165 lg_srcv->szx = max_block_szx;
3166 } else {
3167 lg_srcv->szx = block.szx;
3168 }
3169 lg_srcv->block_option = block_option;
3170 if (observe) {
3171 lg_srcv->observe_length = min(coap_opt_length(observe), 3);
3172 memcpy(lg_srcv->observe, coap_opt_value(observe), lg_srcv->observe_length);
3173 lg_srcv->observe_set = 1;
3174 }
3175 if (rtag_opt) {
3176 lg_srcv->rtag_length = coap_opt_length(rtag_opt);
3177 memcpy(lg_srcv->rtag, coap_opt_value(rtag_opt), lg_srcv->rtag_length);
3178 lg_srcv->rtag_set = 1;
3179 }
3180 lg_srcv->body_data = NULL;
3181 LL_PREPEND(session->lg_srcv, lg_srcv);
3182 }
3183 coap_ticks(&lg_srcv->last_used);
3184
3185 if (block_option == COAP_OPTION_BLOCK1 &&
3187 !check_if_next_block(&lg_srcv->rec_blocks, block.num)) {
3188 coap_add_data(response, sizeof("Missing interim block")-1,
3189 (const uint8_t *)"Missing interim block");
3190 response->code = COAP_RESPONSE_CODE(408);
3191 goto skip_app_handler;
3192 }
3193
3194 if (fmt != lg_srcv->content_format) {
3195 coap_add_data(response, sizeof("Content-Format mismatch")-1,
3196 (const uint8_t *)"Content-Format mismatch");
3197 response->code = COAP_RESPONSE_CODE(408);
3198 goto free_lg_srcv;
3199 }
3200
3201#if COAP_Q_BLOCK_SUPPORT
3202 if (block_option == COAP_OPTION_Q_BLOCK1) {
3203 if (total != lg_srcv->total_len) {
3204 coap_add_data(response, sizeof("Size1 mismatch")-1,
3205 (const uint8_t *)"Size1 mismatch");
3206 response->code = COAP_RESPONSE_CODE(408);
3207 goto free_lg_srcv;
3208 }
3211 pdu->actual_token.length);
3212 }
3213#endif /* COAP_Q_BLOCK_SUPPORT */
3214
3215 lg_srcv->last_mid = pdu->mid;
3216 lg_srcv->last_type = pdu->type;
3217
3218 update_data = 0;
3219 saved_num = block.num;
3220 saved_offset = offset;
3221
3222 while (offset < saved_offset + length) {
3223 if (!check_if_received_block(&lg_srcv->rec_blocks, block.num)) {
3224 /* Update list of blocks received */
3225 if (!update_received_blocks(&lg_srcv->rec_blocks, block.num, block.m)) {
3227 coap_add_data(response, sizeof("Too many missing blocks")-1,
3228 (const uint8_t *)"Too many missing blocks");
3229 response->code = COAP_RESPONSE_CODE(408);
3230 goto free_lg_srcv;
3231 }
3232 update_data = 1;
3233 }
3234 block.num++;
3235 offset = block.num << (block.szx + 4);
3236 }
3237 block.num--;
3238
3239 if (update_data) {
3240 /* Update saved data */
3241#if COAP_Q_BLOCK_SUPPORT
3242 lg_srcv->rec_blocks.processing_payload_set =
3243 block.num / COAP_MAX_PAYLOADS(session);
3244#endif /* COAP_Q_BLOCK_SUPPORT */
3245 if (lg_srcv->total_len < saved_offset + length) {
3246 lg_srcv->total_len = saved_offset + length;
3247 }
3248 lg_srcv->body_data = coap_block_build_body(lg_srcv->body_data, length, data,
3249 saved_offset, lg_srcv->total_len);
3250 if (!lg_srcv->body_data) {
3251 coap_add_data(response, sizeof("Memory issue")-1,
3252 (const uint8_t *)"Memory issue");
3253 response->code = COAP_RESPONSE_CODE(500);
3254 goto skip_app_handler;
3255 }
3256 }
3257
3258 if (block.m ||
3259 !check_all_blocks_in(&lg_srcv->rec_blocks)) {
3260 /* Not all the payloads of the body have arrived */
3261 if (block.m) {
3262 uint8_t buf[4];
3263
3264#if COAP_Q_BLOCK_SUPPORT
3265 if (block_option == COAP_OPTION_Q_BLOCK1) {
3266 if (check_all_blocks_in(&lg_srcv->rec_blocks)) {
3267 goto give_app_data;
3268 }
3269 if (lg_srcv->rec_blocks.used == 1 &&
3270 (lg_srcv->rec_blocks.range[0].end % COAP_MAX_PAYLOADS(session)) + 1
3271 == COAP_MAX_PAYLOADS(session)) {
3272 /* Blocks could arrive in wrong order */
3273 block.num = lg_srcv->rec_blocks.range[0].end;
3274 } else {
3275 /* The remote end will be sending the next one unless this
3276 is a MAX_PAYLOADS and all previous have been received */
3277 goto skip_app_handler;
3278 }
3279 if (COAP_PROTO_RELIABLE(session->proto) ||
3280 pdu->type != COAP_MESSAGE_NON)
3281 goto skip_app_handler;
3282 }
3283#endif /* COAP_Q_BLOCK_SUPPORT */
3284
3285 /* Check to see if block size is getting forced down */
3286 max_block_szx = COAP_BLOCK_MAX_SIZE_GET(session->block_mode);
3287 if (!block.bert && saved_num == 0 && max_block_szx != 0 &&
3288 max_block_szx < block.aszx) {
3289 block.aszx = max_block_szx;
3290 }
3291
3292 /*
3293 * If the last block has been seen, packets are coming in in
3294 * random order. If all blocks are now in, then need to send
3295 * complete payload to application and acknowledge this current
3296 * block.
3297 */
3298 if ((total == 0 && block.m) || !check_all_blocks_in(&lg_srcv->rec_blocks)) {
3299 /* Ask for the next block */
3300 coap_insert_option(response, block_option,
3301 coap_encode_var_safe(buf, sizeof(buf),
3302 (saved_num << 4) |
3303 (block.m << 3) |
3304 block.aszx),
3305 buf);
3306 response->code = COAP_RESPONSE_CODE(231);
3307 } else {
3308 /* Need to separately respond to this request */
3309 coap_pdu_t *tmp_pdu = coap_pdu_duplicate_lkd(response,
3310 session,
3311 response->actual_token.length,
3312 response->actual_token.s,
3313 NULL);
3314 if (tmp_pdu) {
3315 tmp_pdu->code = COAP_RESPONSE_CODE(231);
3316 coap_send_internal(session, tmp_pdu, NULL);
3317 }
3318 if (lg_srcv->last_token) {
3319 coap_update_token(response, lg_srcv->last_token->length, lg_srcv->last_token->s);
3320 coap_update_token(pdu, lg_srcv->last_token->length, lg_srcv->last_token->s);
3321 }
3322 /* Pass the assembled pdu and body to the application */
3323 goto give_app_data;
3324 }
3325 } else {
3326 /* block.m Block More option not set. Some outstanding blocks */
3327#if COAP_Q_BLOCK_SUPPORT
3328 if (block_option != COAP_OPTION_Q_BLOCK1) {
3329#endif /* COAP_Q_BLOCK_SUPPORT */
3330 /* Last chunk - but not all in */
3331 coap_ticks(&lg_srcv->last_used);
3332 lg_srcv->no_more_seen = 1;
3335 pdu->actual_token.length);
3336
3337 /*
3338 * Need to just ACK (no response code) to handle client's NSTART.
3339 * When final missing block comes in, we will pass all the data
3340 * for processing so a 2.01, 2.04 etc. code can be generated
3341 * and responded to as a separate response "RFC7252 5.2.2. Separate"
3342 * If missing block(s) do not come in, then will generate a 4.08
3343 * when lg_srcv times out.
3344 * Fall through to skip_app_handler.
3345 */
3346#if COAP_Q_BLOCK_SUPPORT
3347 }
3348#endif /* COAP_Q_BLOCK_SUPPORT */
3349 }
3350 goto skip_app_handler;
3351 }
3352
3353 /*
3354 * Entire payload received.
3355 * Remove the Block1 option as passing all of the data to
3356 * application layer. Add back in observe option if appropriate.
3357 * Adjust all other information.
3358 */
3359give_app_data:
3360 if (lg_srcv->observe_set) {
3362 lg_srcv->observe_length, lg_srcv->observe);
3363 }
3364 coap_remove_option(pdu, block_option);
3365 if (lg_srcv->body_data) {
3366 pdu->body_data = lg_srcv->body_data->s;
3367 pdu->body_length = lg_srcv->total_len;
3368 } else {
3369 pdu->body_data = NULL;
3370 pdu->body_length = 0;
3371 }
3372 pdu->body_offset = 0;
3373 pdu->body_total = lg_srcv->total_len;
3374 coap_log_debug("Server app version of updated PDU\n");
3376 *pfree_lg_srcv = lg_srcv;
3377
3378call_app_handler:
3379 return 0;
3380
3381free_lg_srcv:
3382 LL_DELETE(session->lg_srcv, lg_srcv);
3383 coap_block_delete_lg_srcv(session, lg_srcv);
3384
3385skip_app_handler:
3386 return 1;
3387}
3388#endif /* COAP_SERVER_SUPPORT */
3389
3390#if COAP_CLIENT_SUPPORT
3391#if COAP_Q_BLOCK_SUPPORT
3392static uint32_t
3393derive_cbor_value(const uint8_t **bp, size_t rem_len) {
3394 uint32_t value = **bp & 0x1f;
3395 (*bp)++;
3396 if (value < 24) {
3397 return value;
3398 } else if (value == 24) {
3399 if (rem_len < 2)
3400 return (uint32_t)-1;
3401 value = **bp;
3402 (*bp)++;
3403 return value;
3404 } else if (value == 25) {
3405 if (rem_len < 3)
3406 return (uint32_t)-1;
3407 value = **bp << 8;
3408 (*bp)++;
3409 value |= **bp;
3410 (*bp)++;
3411 return value;
3412 }
3413 if (rem_len < 4)
3414 return (uint32_t)-1;
3415 value = **bp << 24;
3416 (*bp)++;
3417 value |= **bp << 16;
3418 (*bp)++;
3419 value |= **bp << 8;
3420 (*bp)++;
3421 value |= **bp;
3422 (*bp)++;
3423 return value;
3424}
3425#endif /* COAP_Q_BLOCK_SUPPORT */
3426
3427static int
3428check_freshness(coap_session_t *session, coap_pdu_t *rcvd, coap_pdu_t *sent,
3429 coap_lg_xmit_t *lg_xmit, coap_lg_crcv_t *lg_crcv) {
3430 /* Check for Echo option for freshness */
3431 coap_opt_iterator_t opt_iter;
3432 coap_opt_t *opt = coap_check_option(rcvd, COAP_OPTION_ECHO, &opt_iter);
3433
3434 if (opt) {
3435 if (sent || lg_xmit || lg_crcv) {
3436 /* Need to retransmit original request with Echo option added */
3437 coap_pdu_t *echo_pdu;
3438 coap_mid_t mid;
3439 const uint8_t *data;
3440 size_t data_len;
3441 int have_data = 0;
3442 uint8_t ltoken[8];
3443 size_t ltoken_len;
3444 uint64_t token;
3445
3446 if (sent) {
3447 if (coap_get_data(sent, &data_len, &data))
3448 have_data = 1;
3449 } else if (lg_xmit) {
3450 sent = lg_xmit->sent_pdu;
3451 if (lg_xmit->data_info->length) {
3452 size_t blk_size = (size_t)1 << (lg_xmit->blk_size + 4);
3453 size_t offset = (lg_xmit->last_block + 1) * blk_size;
3454 have_data = 1;
3455 data = &lg_xmit->data_info->data[offset];
3456 data_len = (lg_xmit->data_info->length - offset) > blk_size ? blk_size :
3457 lg_xmit->data_info->length - offset;
3458 }
3459 } else { /* lg_crcv */
3460 sent = lg_crcv->sent_pdu;
3461 if (coap_get_data(sent, &data_len, &data))
3462 have_data = 1;
3463 }
3464 if (lg_xmit) {
3465 token = STATE_TOKEN_FULL(lg_xmit->b.b1.state_token,
3466 ++lg_xmit->b.b1.count);
3467 } else {
3468 token = STATE_TOKEN_FULL(lg_crcv->state_token,
3469 ++lg_crcv->retry_counter);
3470 }
3471 ltoken_len = coap_encode_var_safe8(ltoken, sizeof(token), token);
3472 echo_pdu = coap_pdu_duplicate_lkd(sent, session, ltoken_len, ltoken, NULL);
3473 if (!echo_pdu)
3474 return 0;
3475 if (!coap_insert_option(echo_pdu, COAP_OPTION_ECHO,
3476 coap_opt_length(opt), coap_opt_value(opt)))
3477 goto not_sent;
3478 if (have_data) {
3479 coap_add_data(echo_pdu, data_len, data);
3480 }
3481 /* Need to track Observe token change if Observe */
3482 track_fetch_observe(echo_pdu, lg_crcv, 0, &echo_pdu->actual_token);
3483#if COAP_OSCORE_SUPPORT
3484 if (session->oscore_encryption &&
3485 (opt = coap_check_option(echo_pdu, COAP_OPTION_OBSERVE, &opt_iter)) &&
3487 /* Need to update the base PDU's Token for closing down Observe */
3488 if (lg_xmit) {
3489 lg_xmit->b.b1.state_token = token;
3490 } else {
3491 lg_crcv->state_token = token;
3492 }
3493 }
3494#endif /* COAP_OSCORE_SUPPORT */
3495 mid = coap_send_internal(session, echo_pdu, NULL);
3496 if (mid == COAP_INVALID_MID)
3497 goto not_sent;
3498 return 1;
3499 } else {
3500 /* Need to save Echo option value to add to next reansmission */
3501not_sent:
3502 coap_delete_bin_const(session->echo);
3503 session->echo = coap_new_bin_const(coap_opt_value(opt),
3504 coap_opt_length(opt));
3505 }
3506 }
3507 return 0;
3508}
3509
3510static void
3511track_echo(coap_session_t *session, coap_pdu_t *rcvd) {
3512 coap_opt_iterator_t opt_iter;
3513 coap_opt_t *opt = coap_check_option(rcvd, COAP_OPTION_ECHO, &opt_iter);
3514
3515 if (opt) {
3516 coap_delete_bin_const(session->echo);
3517 session->echo = coap_new_bin_const(coap_opt_value(opt),
3518 coap_opt_length(opt));
3519 }
3520}
3521
3522/*
3523 * Need to see if this is a response to a large body request transfer. If so,
3524 * need to initiate the request containing the next block and not trouble the
3525 * application. Note that Token must unique per request/response.
3526 *
3527 * Client receives large data acknowledgement from server (Block1)
3528 *
3529 * This is set up using coap_add_data_large_request_lkd()
3530 *
3531 * Client is using GET etc.
3532 *
3533 * Return: 0 Call application handler
3534 * 1 Do not call application handler - just send the built response
3535 */
3536int
3538 coap_pdu_t *rcvd) {
3539 coap_lg_xmit_t *lg_xmit;
3540 coap_lg_crcv_t *lg_crcv = NULL;
3541
3542 lg_xmit = coap_find_lg_xmit(session, rcvd);
3543 if (lg_xmit) {
3544 /* lg_xmit found */
3545 size_t chunk = (size_t)1 << (lg_xmit->blk_size + 4);
3546 coap_block_b_t block;
3547
3548 lg_crcv = coap_find_lg_crcv(session, rcvd);
3549 if (lg_crcv)
3550 coap_ticks(&lg_crcv->last_used);
3551
3552 if (COAP_RESPONSE_CLASS(rcvd->code) == 2 &&
3553 coap_get_block_b(session, rcvd, lg_xmit->option, &block)) {
3554
3555 if (block.bert) {
3556 coap_log_debug("found Block option, block is BERT, block nr. %u (%zu)\n",
3557 block.num, lg_xmit->b.b1.bert_size);
3558 } else {
3559 coap_log_debug("found Block option, block size is %u, block nr. %u\n",
3560 1 << (block.szx + 4), block.num);
3561 }
3562 if (block.szx != lg_xmit->blk_size) {
3563 if (block.szx > lg_xmit->blk_size) {
3564 coap_log_info("ignoring request to increase Block size, "
3565 "(%u > %u)\n",
3566 1 << (block.szx + 4), 1 << (lg_xmit->blk_size + 4));
3567 } else if ((lg_xmit->offset + chunk) % ((size_t)1 << (block.szx + 4)) == 0) {
3568 /*
3569 * Recompute the block number of the previous packet given the
3570 * new block size
3571 */
3572 block.num = (uint32_t)(((lg_xmit->offset + chunk) >> (block.szx + 4)) - 1);
3573 lg_xmit->blk_size = block.szx;
3574 chunk = (size_t)1 << (lg_xmit->blk_size + 4);
3575 lg_xmit->offset = block.num * chunk;
3576 coap_log_debug("new Block size is %u, block number %u completed\n",
3577 1 << (block.szx + 4), block.num);
3578 block.bert = 0;
3579 block.aszx = block.szx;
3580 } else {
3581 coap_log_debug("ignoring request to increase Block size, "
3582 "next block is not aligned on requested block size boundary. "
3583 "(%zu x %u mod %u = %zu != 0)\n",
3584 lg_xmit->offset/chunk + 1, (1 << (lg_xmit->blk_size + 4)),
3585 (1 << (block.szx + 4)),
3586 (lg_xmit->offset + chunk) % ((size_t)1 << (block.szx + 4)));
3587 }
3588 }
3589 track_echo(session, rcvd);
3590 if (lg_xmit->last_block == (int)block.num &&
3591 lg_xmit->option != COAP_OPTION_Q_BLOCK1) {
3592 /*
3593 * Duplicate Block1 ACK
3594 *
3595 * RFCs not clear here, but on a lossy connection, there could
3596 * be multiple Block1 ACKs, causing the client to retransmit the
3597 * same block multiple times, or the server retransmitting the
3598 * same ACK.
3599 *
3600 * Once a block has been ACKd, there is no need to retransmit it.
3601 */
3602 return 1;
3603 }
3604 if (block.bert)
3605 block.num += (unsigned int)(lg_xmit->b.b1.bert_size / 1024 - 1);
3606 lg_xmit->last_block = block.num;
3607 lg_xmit->offset = (block.num + 1) * chunk;
3608 if (lg_xmit->offset < lg_xmit->data_info->length) {
3609 /* Build the next PDU request based off the skeletal PDU */
3610 uint8_t buf[8];
3611 coap_pdu_t *pdu;
3612 uint64_t token = STATE_TOKEN_FULL(lg_xmit->b.b1.state_token, ++lg_xmit->b.b1.count);
3613 size_t len = coap_encode_var_safe8(buf, sizeof(token), token);
3614
3615 if (lg_xmit->sent_pdu->code == COAP_REQUEST_CODE_FETCH) {
3616 /* Need to handle Observe for large FETCH */
3617 if (lg_crcv) {
3618 if (coap_binary_equal(lg_xmit->b.b1.app_token, lg_crcv->app_token)) {
3619 coap_bin_const_t *new_token;
3620 coap_bin_const_t ctoken = { len, buf };
3621
3622 /* Need to save/restore Observe Token for large FETCH */
3623 new_token = track_fetch_observe(lg_xmit->sent_pdu, lg_crcv, block.num + 1,
3624 &ctoken);
3625 if (new_token) {
3626 assert(len <= sizeof(buf));
3627 len = new_token->length;
3628 memcpy(buf, new_token->s, len);
3629 }
3630 }
3631 }
3632 }
3633 pdu = coap_pdu_duplicate_lkd(lg_xmit->sent_pdu, session, len, buf, NULL);
3634 if (!pdu)
3635 goto fail_body;
3636
3637 /*
3638 * If initial transmit was multicast, that would have been NON.
3639 * Make subsequent traffic CON for reliability.
3640 */
3641 if (session->sock.flags & COAP_SOCKET_MULTICAST) {
3642 pdu->type = COAP_MESSAGE_CON;
3643 }
3644
3645 block.num++;
3646 if (block.bert) {
3647 size_t token_options = pdu->data ? (size_t)(pdu->data - pdu->token) :
3648 pdu->used_size;
3649 block.m = (lg_xmit->data_info->length - lg_xmit->offset) >
3650 ((pdu->max_size - token_options) /1024) * 1024;
3651 } else {
3652 block.m = (lg_xmit->offset + chunk) < lg_xmit->data_info->length;
3653 }
3654 coap_update_option(pdu, lg_xmit->option,
3655 coap_encode_var_safe(buf, sizeof(buf),
3656 (block.num << 4) |
3657 (block.m << 3) |
3658 block.aszx),
3659 buf);
3660
3661 if (!coap_add_block_b_data(pdu,
3662 lg_xmit->data_info->length,
3663 lg_xmit->data_info->data,
3664 &block))
3665 goto fail_body;
3666 lg_xmit->b.b1.bert_size = block.chunk_size;
3667 coap_ticks(&lg_xmit->last_sent);
3668#if COAP_Q_BLOCK_SUPPORT
3669 if (lg_xmit->option == COAP_OPTION_Q_BLOCK1 &&
3670 pdu->type == COAP_MESSAGE_NON) {
3671 if (coap_send_q_block1(session, block, pdu,
3672 COAP_SEND_INC_PDU) == COAP_INVALID_MID)
3673 goto fail_body;
3674 return 1;
3675 } else if (coap_send_internal(session, pdu, NULL) == COAP_INVALID_MID)
3676 goto fail_body;
3677#else /* ! COAP_Q_BLOCK_SUPPORT */
3678 if (coap_send_internal(session, pdu, NULL) == COAP_INVALID_MID)
3679 goto fail_body;
3680#endif /* ! COAP_Q_BLOCK_SUPPORT */
3681 return 1;
3682 }
3683 } else if (COAP_RESPONSE_CLASS(rcvd->code) == 2) {
3684 /*
3685 * Not a block response asking for the next block.
3686 * Could be an Observe response overlapping with block FETCH doing
3687 * Observe cancellation.
3688 */
3689 coap_opt_iterator_t opt_iter;
3690 coap_opt_t *obs_opt;
3691 int observe_action = -1;
3692
3693 if (lg_xmit->sent_pdu->code != COAP_REQUEST_CODE_FETCH) {
3694 goto lg_xmit_finished;
3695 }
3696 obs_opt = coap_check_option(lg_xmit->sent_pdu,
3698 &opt_iter);
3699 if (obs_opt) {
3700 observe_action = coap_decode_var_bytes(coap_opt_value(obs_opt),
3701 coap_opt_length(obs_opt));
3702 }
3703 if (observe_action != COAP_OBSERVE_CANCEL) {
3704 goto lg_xmit_finished;
3705 }
3706 obs_opt = coap_check_option(rcvd,
3708 &opt_iter);
3709 if (obs_opt) {
3710 return 0;
3711 }
3712 goto lg_xmit_finished;
3713 } else if (rcvd->code == COAP_RESPONSE_CODE(401)) {
3714 if (check_freshness(session, rcvd, sent, lg_xmit, NULL))
3715 return 1;
3716#if COAP_Q_BLOCK_SUPPORT
3717 } else if (rcvd->code == COAP_RESPONSE_CODE(402)) {
3718 /* Q-Block1 or Q-Block2 not present in p - duplicate error ? */
3719 if (coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK2, &block) ||
3720 coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK1, &block))
3721 return 1;
3722 } else if (rcvd->code == COAP_RESPONSE_CODE(408) &&
3723 lg_xmit->option == COAP_OPTION_Q_BLOCK1) {
3724 size_t length;
3725 const uint8_t *data;
3726 coap_opt_iterator_t opt_iter;
3727 coap_opt_t *fmt_opt = coap_check_option(rcvd,
3729 &opt_iter);
3730 uint16_t fmt = fmt_opt ?
3732 coap_opt_length(fmt_opt)) :
3734
3736 goto fail_body;
3737
3738 if (COAP_PROTO_RELIABLE(session->proto) ||
3739 rcvd->type != COAP_MESSAGE_NON) {
3740 coap_log_debug("Unexpected 4.08 - protocol violation - ignore\n");
3741 return 1;
3742 }
3743
3744 if (coap_get_data(rcvd, &length, &data)) {
3745 /* Need to decode CBOR to work out what blocks to re-send */
3746 const uint8_t *bp = data;
3747 uint32_t i;
3748 uint8_t buf[8];
3749 coap_pdu_t *pdu;
3750 uint64_t token;
3751 uint8_t ltoken[8];
3752 size_t ltoken_length;
3753
3754 for (i = 0; (bp < data + length) &&
3755 i < COAP_MAX_PAYLOADS(session); i++) {
3756 if ((*bp & 0xc0) != 0x00) /* uint(value) */
3757 goto fail_cbor;
3758 block.num = derive_cbor_value(&bp, data + length - bp);
3759 coap_log_debug("Q-Block1: Missing block %d\n", block.num);
3760 if (block.num > (1 << 20) -1)
3761 goto fail_cbor;
3762 block.m = (block.num + 1) * chunk < lg_xmit->data_info->length;
3763 block.szx = lg_xmit->blk_size;
3764
3765 /* Build the next PDU request based off the skeletal PDU */
3766 token = STATE_TOKEN_FULL(lg_xmit->b.b1.state_token,++lg_xmit->b.b1.count);
3767 ltoken_length = coap_encode_var_safe8(ltoken, sizeof(token), token);
3768 pdu = coap_pdu_duplicate_lkd(lg_xmit->sent_pdu, session, ltoken_length,
3769 ltoken, NULL);
3770 if (!pdu)
3771 goto fail_body;
3772
3773 coap_update_option(pdu, lg_xmit->option,
3774 coap_encode_var_safe(buf, sizeof(buf),
3775 (block.num << 4) |
3776 (block.m << 3) |
3777 block.szx),
3778 buf);
3779
3780 if (!coap_add_block(pdu,
3781 lg_xmit->data_info->length,
3782 lg_xmit->data_info->data,
3783 block.num,
3784 block.szx))
3785 goto fail_body;
3786 if (coap_send_internal(session, pdu, NULL) == COAP_INVALID_MID)
3787 goto fail_body;
3788 }
3789 return 1;
3790 }
3791fail_cbor:
3792 coap_log_info("Invalid application/missing-blocks+cbor-seq\n");
3793#endif /* COAP_Q_BLOCK_SUPPORT */
3794 }
3795 goto lg_xmit_finished;
3796 }
3797 return 0;
3798
3799fail_body:
3801 /* There has been an internal error of some sort */
3802 rcvd->code = COAP_RESPONSE_CODE(500);
3803lg_xmit_finished:
3804 if (lg_crcv) {
3805 if (STATE_TOKEN_BASE(lg_xmit->b.b1.state_token) ==
3806 STATE_TOKEN_BASE(lg_crcv->state_token)) {
3807 /* In case of observe */
3808 lg_crcv->state_token = lg_xmit->b.b1.state_token;
3809 lg_crcv->retry_counter = lg_xmit->b.b1.count;
3810 }
3811 }
3812 if (!lg_crcv) {
3813 /* need to put back original token into rcvd */
3814 if (lg_xmit->b.b1.app_token)
3815 coap_update_token(rcvd, lg_xmit->b.b1.app_token->length,
3816 lg_xmit->b.b1.app_token->s);
3817 coap_log_debug("Client app version of updated PDU (1)\n");
3819 } else {
3820 lg_crcv->sent_pdu->lg_xmit = 0;
3821 }
3822
3823 if (sent) {
3824 /* need to put back original token into sent */
3825 if (lg_xmit->b.b1.app_token)
3826 coap_update_token(sent, lg_xmit->b.b1.app_token->length,
3827 lg_xmit->b.b1.app_token->s);
3828 if (sent->lg_xmit)
3829 coap_remove_option(sent, sent->lg_xmit->option);
3830 sent->lg_xmit = NULL;
3831 }
3832 LL_DELETE(session->lg_xmit, lg_xmit);
3833 coap_block_delete_lg_xmit(session, lg_xmit);
3834 return 0;
3835}
3836#endif /* COAP_CLIENT_SUPPORT */
3837
3838/*
3839 * Re-assemble payloads into a body
3840 */
3842coap_block_build_body(coap_binary_t *body_data, size_t length,
3843 const uint8_t *data, size_t offset, size_t total) {
3844 if (data == NULL)
3845 return NULL;
3846 if (body_data == NULL && total) {
3847 body_data = coap_new_binary(total);
3848 }
3849 if (body_data == NULL)
3850 return NULL;
3851
3852 /* Update saved data */
3853 if (offset + length <= total && body_data->length >= total) {
3854 memcpy(&body_data->s[offset], data, length);
3855 } else {
3856 /*
3857 * total may be inaccurate as per
3858 * https://rfc-editor.org/rfc/rfc7959#section-4
3859 * o In a request carrying a Block1 Option, to indicate the current
3860 * estimate the client has of the total size of the resource
3861 * representation, measured in bytes ("size indication").
3862 * o In a response carrying a Block2 Option, to indicate the current
3863 * estimate the server has of the total size of the resource
3864 * representation, measured in bytes ("size indication").
3865 */
3866 coap_binary_t *new = coap_resize_binary(body_data, offset + length);
3867
3868 if (new) {
3869 body_data = new;
3870 memcpy(&body_data->s[offset], data, length);
3871 } else {
3872 coap_delete_binary(body_data);
3873 return NULL;
3874 }
3875 }
3876 return body_data;
3877}
3878
3879#if COAP_CLIENT_SUPPORT
3880/*
3881 * Need to see if this is a large body response to a request. If so,
3882 * need to initiate the request for the next block and not trouble the
3883 * application. Note that Token must be unique per request/response.
3884 *
3885 * This is set up using coap_send()
3886 * Client receives large data from server ((Q-)Block2)
3887 *
3888 * Return: 0 Call application handler
3889 * 1 Do not call application handler - just sent the next request
3890 */
3891int
3893 coap_session_t *session,
3894 coap_pdu_t *sent,
3895 coap_pdu_t *rcvd,
3896 coap_recurse_t recursive) {
3897 coap_lg_crcv_t *lg_crcv;
3898 coap_block_b_t block;
3899#if COAP_Q_BLOCK_SUPPORT
3900 coap_block_b_t qblock;
3901#endif /* COAP_Q_BLOCK_SUPPORT */
3902 int have_block = 0;
3903 uint16_t block_opt = 0;
3904 size_t offset;
3905 int ack_rst_sent = 0;
3906
3908 memset(&block, 0, sizeof(block));
3909#if COAP_Q_BLOCK_SUPPORT
3910 memset(&qblock, 0, sizeof(qblock));
3911#endif /* COAP_Q_BLOCK_SUPPORT */
3912 lg_crcv = coap_find_lg_crcv(session, rcvd);
3913 if (lg_crcv) {
3914 size_t chunk = 0;
3915 uint8_t buf[8];
3916 coap_opt_iterator_t opt_iter;
3917
3918 if (COAP_RESPONSE_CLASS(rcvd->code) == 2) {
3919 size_t length;
3920 const uint8_t *data;
3922 &opt_iter);
3923 size_t size2 = size_opt ?
3925 coap_opt_length(size_opt)) : 0;
3926
3927 /* length and data are cleared on error */
3928 (void)coap_get_data(rcvd, &length, &data);
3929 rcvd->body_offset = 0;
3930 rcvd->body_total = length;
3931 if (coap_get_block_b(session, rcvd, COAP_OPTION_BLOCK2, &block)) {
3932 have_block = 1;
3933 block_opt = COAP_OPTION_BLOCK2;
3934 }
3935#if COAP_Q_BLOCK_SUPPORT
3936 if (coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK2, &qblock)) {
3937 if (have_block) {
3938 coap_log_warn("Both Block1 and Q-Block1 not supported in a response\n");
3939 }
3940 have_block = 1;
3941 block_opt = COAP_OPTION_Q_BLOCK2;
3942 block = qblock;
3943 /* server indicating that it supports Q_BLOCK */
3944 if (!(session->block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
3945 set_block_mode_has_q(session->block_mode);
3946 }
3947 }
3948#endif /* COAP_Q_BLOCK_SUPPORT */
3949 track_echo(session, rcvd);
3950 if (have_block && (block.m || length)) {
3951 coap_opt_t *fmt_opt = coap_check_option(rcvd,
3953 &opt_iter);
3954 uint16_t fmt = fmt_opt ?
3956 coap_opt_length(fmt_opt)) :
3958 coap_opt_t *etag_opt = coap_check_option(rcvd,
3960 &opt_iter);
3961 size_t saved_offset;
3962 int updated_block;
3963
3964 if (length > block.chunk_size) {
3965 coap_log_debug("block: Oversized packet - reduced to %"PRIu32" from %zu\n",
3966 block.chunk_size, length);
3967 length = block.chunk_size;
3968 }
3969 if (block.m && length != block.chunk_size) {
3970 coap_log_warn("block: Undersized packet - expected %"PRIu32", got %zu\n",
3971 block.chunk_size, length);
3972 /* Unclear how to properly handle this */
3973 rcvd->code = COAP_RESPONSE_CODE(402);
3974 goto expire_lg_crcv;
3975 }
3976 /* Possibility that Size2 not sent, or is too small */
3977 chunk = (size_t)1 << (block.szx + 4);
3978 offset = block.num * chunk;
3979 if (size2 < (offset + length)) {
3980 if (block.m)
3981 size2 = offset + length + 1;
3982 else
3983 size2 = offset + length;
3984 }
3985 saved_offset = offset;
3986
3987 if (lg_crcv->initial) {
3988#if COAP_Q_BLOCK_SUPPORT
3989reinit:
3990#endif /* COAP_Q_BLOCK_SUPPORT */
3991 lg_crcv->initial = 0;
3992 if (lg_crcv->body_data) {
3994 lg_crcv->body_data = NULL;
3995 }
3996 if (etag_opt) {
3997 lg_crcv->etag_length = coap_opt_length(etag_opt);
3998 memcpy(lg_crcv->etag, coap_opt_value(etag_opt), lg_crcv->etag_length);
3999 lg_crcv->etag_set = 1;
4000 } else {
4001 lg_crcv->etag_set = 0;
4002 }
4003 lg_crcv->total_len = size2;
4004 lg_crcv->content_format = fmt;
4005 lg_crcv->szx = block.szx;
4006 lg_crcv->block_option = block_opt;
4007 lg_crcv->last_type = rcvd->type;
4008 lg_crcv->rec_blocks.used = 0;
4009 lg_crcv->rec_blocks.total_blocks = 0;
4010#if COAP_Q_BLOCK_SUPPORT
4011 lg_crcv->rec_blocks.processing_payload_set = 0;
4012#endif /* COAP_Q_BLOCK_SUPPORT */
4013 }
4014 if (lg_crcv->total_len < size2)
4015 lg_crcv->total_len = size2;
4016
4017 if (etag_opt) {
4018 if (!full_match(coap_opt_value(etag_opt),
4019 coap_opt_length(etag_opt),
4020 lg_crcv->etag, lg_crcv->etag_length)) {
4021 /* body of data has changed - need to restart request */
4022 coap_pdu_t *pdu;
4023 uint64_t token = STATE_TOKEN_FULL(lg_crcv->state_token,
4024 ++lg_crcv->retry_counter);
4025 size_t len = coap_encode_var_safe8(buf, sizeof(token), token);
4026 coap_opt_filter_t drop_options;
4027
4028#if COAP_Q_BLOCK_SUPPORT
4029 if (block_opt == COAP_OPTION_Q_BLOCK2)
4030 goto reinit;
4031#endif /* COAP_Q_BLOCK_SUPPORT */
4032
4033 coap_log_warn("Data body updated during receipt - new request started\n");
4034 if (!(session->block_mode & COAP_BLOCK_SINGLE_BODY))
4036
4037 lg_crcv->initial = 1;
4039 lg_crcv->body_data = NULL;
4040
4041 coap_session_new_token(session, &len, buf);
4042 memset(&drop_options, 0, sizeof(coap_opt_filter_t));
4045 pdu = coap_pdu_duplicate_lkd(lg_crcv->sent_pdu, session, len, buf, &drop_options);
4046 if (!pdu)
4047 goto fail_resp;
4048
4049 coap_update_option(pdu, block_opt,
4050 coap_encode_var_safe(buf, sizeof(buf),
4051 (0 << 4) | (0 << 3) | block.aszx),
4052 buf);
4053
4054 if (coap_send_internal(session, pdu, NULL) == COAP_INVALID_MID)
4055 goto fail_resp;
4056
4057 goto skip_app_handler;
4058 }
4059 } else if (lg_crcv->etag_set) {
4060 /* Cannot handle this change in ETag to not being there */
4061 coap_log_warn("Not all blocks have ETag option\n");
4062 goto fail_resp;
4063 }
4064
4065 if (fmt != lg_crcv->content_format) {
4066 coap_log_warn("Content-Format option mismatch\n");
4067 goto fail_resp;
4068 }
4069#if COAP_Q_BLOCK_SUPPORT
4070 if (block_opt == COAP_OPTION_Q_BLOCK2 && size2 != lg_crcv->total_len) {
4071 coap_log_warn("Size2 option mismatch\n");
4072 goto fail_resp;
4073 }
4074#endif /* COAP_Q_BLOCK_SUPPORT */
4075 if (block.num == 0) {
4076 coap_opt_t *obs_opt = coap_check_option(rcvd,
4078 &opt_iter);
4079 if (obs_opt) {
4080 lg_crcv->observe_length = min(coap_opt_length(obs_opt), 3);
4081 memcpy(lg_crcv->observe, coap_opt_value(obs_opt), lg_crcv->observe_length);
4082 lg_crcv->observe_set = 1;
4083 } else {
4084 lg_crcv->observe_set = 0;
4085 }
4086 }
4087 updated_block = 0;
4088 while (offset < saved_offset + length) {
4089 if (!check_if_received_block(&lg_crcv->rec_blocks, block.num)) {
4090#if COAP_Q_BLOCK_SUPPORT
4091 uint32_t this_payload_set = block.num / COAP_MAX_PAYLOADS(session);
4092#endif /* COAP_Q_BLOCK_SUPPORT */
4093
4094 coap_log_debug("found Block option, block size is %u, block nr. %u\n",
4095 1 << (block.szx + 4), block.num);
4096#if COAP_Q_BLOCK_SUPPORT
4097 if (block_opt == COAP_OPTION_Q_BLOCK2 && lg_crcv->rec_blocks.used &&
4098 this_payload_set > lg_crcv->rec_blocks.processing_payload_set &&
4099 this_payload_set != lg_crcv->rec_blocks.latest_payload_set) {
4100 coap_request_missing_q_block2(session, lg_crcv);
4101 }
4102 lg_crcv->rec_blocks.latest_payload_set = this_payload_set;
4103#endif /* COAP_Q_BLOCK_SUPPORT */
4104 /* Update list of blocks received */
4105 if (!update_received_blocks(&lg_crcv->rec_blocks, block.num, block.m)) {
4107 goto fail_resp;
4108 }
4109 updated_block = 1;
4110 }
4111 block.num++;
4112 offset = block.num << (block.szx + 4);
4113 if (!block.bert && block_opt != COAP_OPTION_Q_BLOCK2)
4114 break;
4115 }
4116 block.num--;
4117 /* Only process if not duplicate block */
4118 if (updated_block) {
4119 void *body_free;
4120
4121 if ((session->block_mode & COAP_SINGLE_BLOCK_OR_Q) || block.bert) {
4122 if (size2 < saved_offset + length) {
4123 size2 = saved_offset + length;
4124 }
4125 lg_crcv->body_data = coap_block_build_body(lg_crcv->body_data, length, data,
4126 saved_offset, size2);
4127 if (lg_crcv->body_data == NULL) {
4128 goto fail_resp;
4129 }
4130 }
4131 if (block.m || !check_all_blocks_in(&lg_crcv->rec_blocks)) {
4132 /* Not all the payloads of the body have arrived */
4133 size_t len;
4134 coap_pdu_t *pdu;
4135 uint64_t token;
4136 coap_opt_filter_t drop_options;
4137
4138 if (block.m) {
4139#if COAP_Q_BLOCK_SUPPORT
4140 if (block_opt == COAP_OPTION_Q_BLOCK2) {
4141 /* Blocks could arrive in wrong order */
4142 if (check_all_blocks_in(&lg_crcv->rec_blocks)) {
4143 goto give_to_app;
4144 }
4145 if (check_all_blocks_in_for_payload_set(session,
4146 &lg_crcv->rec_blocks)) {
4147 block.num = lg_crcv->rec_blocks.range[0].end;
4148 /* Now requesting next payload */
4149 lg_crcv->rec_blocks.processing_payload_set =
4150 block.num / COAP_MAX_PAYLOADS(session) + 1;
4151 if (check_any_blocks_next_payload_set(session,
4152 &lg_crcv->rec_blocks)) {
4153 /* Need to ask for them individually */
4154 coap_request_missing_q_block2(session, lg_crcv);
4155 goto skip_app_handler;
4156 }
4157 } else {
4158 /* The remote end will be sending the next one unless this
4159 is a MAX_PAYLOADS and all previous have been received */
4160 goto skip_app_handler;
4161 }
4162 if (COAP_PROTO_RELIABLE(session->proto) ||
4163 rcvd->type != COAP_MESSAGE_NON)
4164 goto skip_app_handler;
4165
4166 } else
4167#endif /* COAP_Q_BLOCK_SUPPORT */
4168 block.m = 0;
4169
4170 /* Ask for the next block */
4171 token = STATE_TOKEN_FULL(lg_crcv->state_token, ++lg_crcv->retry_counter);
4172 len = coap_encode_var_safe8(buf, sizeof(token), token);
4173 memset(&drop_options, 0, sizeof(coap_opt_filter_t));
4175 pdu = coap_pdu_duplicate_lkd(lg_crcv->sent_pdu, session, len, buf, &drop_options);
4176 if (!pdu)
4177 goto fail_resp;
4178
4179 if (rcvd->type == COAP_MESSAGE_NON)
4180 pdu->type = COAP_MESSAGE_NON; /* Server is using NON */
4181
4182 /* Only sent with the first block */
4184
4185 coap_update_option(pdu, block_opt,
4186 coap_encode_var_safe(buf, sizeof(buf),
4187 ((block.num + 1) << 4) |
4188 (block.m << 3) | block.aszx),
4189 buf);
4190
4192 (void)coap_get_data(lg_crcv->sent_pdu, &length, &data);
4193 coap_add_data_large_internal(session, NULL, pdu, NULL, NULL, -1, 0, length, data, NULL, NULL, 0, 0);
4194 }
4195 if (coap_send_internal(session, pdu, NULL) == COAP_INVALID_MID)
4196 /* Session could now be disconnected, so no lg_crcv */
4197 goto skip_app_handler;
4198 }
4199 if ((session->block_mode & COAP_SINGLE_BLOCK_OR_Q) || block.bert)
4200 goto skip_app_handler;
4201
4202 /* need to put back original token into rcvd */
4203 coap_update_token(rcvd, lg_crcv->app_token->length, lg_crcv->app_token->s);
4204 rcvd->body_offset = saved_offset;
4205#if COAP_Q_BLOCK_SUPPORT
4206 rcvd->body_total = block_opt == COAP_OPTION_Q_BLOCK2 ?
4207 lg_crcv->total_len : size2;
4208#else /* ! COAP_Q_BLOCK_SUPPORT */
4209 rcvd->body_total = size2;
4210#endif /* ! COAP_Q_BLOCK_SUPPORT */
4211 coap_log_debug("Client app version of updated PDU (2)\n");
4213
4214 if (sent) {
4215 /* need to put back original token into sent */
4216 if (lg_crcv->app_token)
4217 coap_update_token(sent, lg_crcv->app_token->length,
4218 lg_crcv->app_token->s);
4219 coap_remove_option(sent, lg_crcv->block_option);
4220 }
4221 goto call_app_handler;
4222 }
4223#if COAP_Q_BLOCK_SUPPORT
4224give_to_app:
4225#endif /* COAP_Q_BLOCK_SUPPORT */
4226 if ((session->block_mode & COAP_SINGLE_BLOCK_OR_Q) || block.bert) {
4227 /* Pretend that there is no block */
4228 coap_remove_option(rcvd, block_opt);
4229 if (lg_crcv->observe_set) {
4231 lg_crcv->observe_length, lg_crcv->observe);
4232 }
4233 rcvd->body_data = lg_crcv->body_data->s;
4234#if COAP_Q_BLOCK_SUPPORT
4235 rcvd->body_length = block_opt == COAP_OPTION_Q_BLOCK2 ?
4236 lg_crcv->total_len : saved_offset + length;
4237#else /* ! COAP_Q_BLOCK_SUPPORT */
4238 rcvd->body_length = saved_offset + length;
4239#endif /* ! COAP_Q_BLOCK_SUPPORT */
4240 rcvd->body_offset = 0;
4241 rcvd->body_total = rcvd->body_length;
4242 } else {
4243 rcvd->body_offset = saved_offset;
4244#if COAP_Q_BLOCK_SUPPORT
4245 rcvd->body_total = block_opt == COAP_OPTION_Q_BLOCK2 ?
4246 lg_crcv->total_len : size2;
4247#else /* ! COAP_Q_BLOCK_SUPPORT */
4248 rcvd->body_total = size2;
4249#endif /* ! COAP_Q_BLOCK_SUPPORT */
4250 }
4251 /* need to put back original token into rcvd */
4252 if (!coap_binary_equal(&rcvd->actual_token, lg_crcv->app_token)) {
4253 coap_update_token(rcvd, lg_crcv->app_token->length, lg_crcv->app_token->s);
4254 coap_log_debug("Client app version of updated PDU (3)\n");
4256 }
4257 if (sent) {
4258 /* need to put back original token into sent */
4259 if (lg_crcv->app_token)
4260 coap_update_token(sent, lg_crcv->app_token->length,
4261 lg_crcv->app_token->s);
4262 coap_remove_option(sent, lg_crcv->block_option);
4263 }
4264 body_free = lg_crcv->body_data;
4265 lg_crcv->body_data = NULL;
4266 coap_call_response_handler(session, sent, rcvd, body_free);
4267
4268 ack_rst_sent = 1;
4269 if (lg_crcv->observe_set == 0) {
4270 /* Expire this entry */
4271 LL_DELETE(session->lg_crcv, lg_crcv);
4272 coap_block_delete_lg_crcv(session, lg_crcv);
4273 goto skip_app_handler;
4274 }
4275 /* Set up for the next data body as observing */
4276 lg_crcv->initial = 1;
4277 }
4278 coap_ticks(&lg_crcv->last_used);
4279 goto skip_app_handler;
4280 } else {
4281 coap_opt_t *obs_opt = coap_check_option(rcvd,
4283 &opt_iter);
4284 if (obs_opt) {
4285 lg_crcv->observe_length = min(coap_opt_length(obs_opt), 3);
4286 memcpy(lg_crcv->observe, coap_opt_value(obs_opt), lg_crcv->observe_length);
4287 lg_crcv->observe_set = 1;
4288 } else {
4289 lg_crcv->observe_set = 0;
4290 if (!coap_binary_equal(&rcvd->actual_token, lg_crcv->app_token)) {
4291 /* need to put back original token into rcvd */
4292 coap_update_token(rcvd, lg_crcv->app_token->length, lg_crcv->app_token->s);
4294 coap_log_debug("PDU presented to app.\n");
4296 }
4297 /* Expire this entry */
4298 goto expire_lg_crcv;
4299 }
4300 }
4301 coap_ticks(&lg_crcv->last_used);
4302 } else if (rcvd->code == COAP_RESPONSE_CODE(401)) {
4303#if COAP_OSCORE_SUPPORT
4304 if (check_freshness(session, rcvd,
4305 (session->oscore_encryption == 0) ? sent : NULL,
4306 NULL, lg_crcv))
4307#else /* !COAP_OSCORE_SUPPORT */
4308 if (check_freshness(session, rcvd, sent, NULL, lg_crcv))
4309#endif /* !COAP_OSCORE_SUPPORT */
4310 goto skip_app_handler;
4311 goto expire_lg_crcv;
4312 } else {
4313 /* Not 2.xx or 4.01 - assume it is a failure of some sort */
4314 goto expire_lg_crcv;
4315 }
4316 if (!block.m && !lg_crcv->observe_set) {
4317fail_resp:
4318 /* lg_crcv no longer required - cache it for 1 sec */
4319 coap_ticks(&lg_crcv->last_used);
4320 lg_crcv->last_used = lg_crcv->last_used - COAP_MAX_TRANSMIT_WAIT_TICKS(session) +
4322 }
4323 /* need to put back original token into rcvd */
4324 if (!coap_binary_equal(&rcvd->actual_token, lg_crcv->app_token)) {
4325 coap_update_token(rcvd, lg_crcv->app_token->length, lg_crcv->app_token->s);
4326 coap_log_debug("Client app version of updated PDU (4)\n");
4328 }
4329 }
4330
4331 /* Check if receiving a block response and if blocks can be set up */
4332 if (recursive == COAP_RECURSE_OK && !lg_crcv) {
4333 if (!sent) {
4334 if (coap_get_block_b(session, rcvd, COAP_OPTION_BLOCK2, &block)
4335#if COAP_Q_BLOCK_SUPPORT
4336 ||
4337 coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK2, &block)
4338#endif /* COAP_Q_BLOCK_SUPPORT */
4339 ) {
4340 coap_log_debug("** %s: large body receive internal issue\n",
4341 coap_session_str(session));
4342 goto skip_app_handler;
4343 }
4344 } else if (COAP_RESPONSE_CLASS(rcvd->code) == 2) {
4345 if (coap_get_block_b(session, rcvd, COAP_OPTION_BLOCK2, &block)) {
4346#if COAP_Q_BLOCK_SUPPORT
4347 if (session->block_mode & COAP_BLOCK_PROBE_Q_BLOCK) {
4348 set_block_mode_drop_q(session->block_mode);
4349 coap_log_debug("Q-Block support disabled\n");
4350 }
4351#endif /* COAP_Q_BLOCK_SUPPORT */
4352 have_block = 1;
4353 if (block.num != 0) {
4354 /* Assume random access and just give the single response to app */
4355 size_t length;
4356 const uint8_t *data;
4357 size_t chunk = (size_t)1 << (block.szx + 4);
4358
4359 coap_get_data(rcvd, &length, &data);
4360 rcvd->body_offset = block.num*chunk;
4361 rcvd->body_total = block.num*chunk + length + (block.m ? 1 : 0);
4362 goto call_app_handler;
4363 }
4364 }
4365#if COAP_Q_BLOCK_SUPPORT
4366 else if (coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK2, &block)) {
4367 have_block = 1;
4368 /* server indicating that it supports Q_BLOCK2 */
4369 if (!(session->block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
4370 set_block_mode_has_q(session->block_mode);
4371 }
4372 }
4373#endif /* COAP_Q_BLOCK_SUPPORT */
4374 if (have_block) {
4375 lg_crcv = coap_block_new_lg_crcv(session, sent, NULL);
4376
4377 if (lg_crcv) {
4378 LL_PREPEND(session->lg_crcv, lg_crcv);
4379 return coap_handle_response_get_block(context, session, sent, rcvd,
4381 }
4382 }
4383 track_echo(session, rcvd);
4384 } else if (rcvd->code == COAP_RESPONSE_CODE(401)) {
4385 lg_crcv = coap_block_new_lg_crcv(session, sent, NULL);
4386
4387 if (lg_crcv) {
4388 LL_PREPEND(session->lg_crcv, lg_crcv);
4389 return coap_handle_response_get_block(context, session, sent, rcvd,
4391 }
4392 }
4393 }
4394 return 0;
4395
4396expire_lg_crcv:
4397 /* need to put back original token into rcvd */
4398 if (!coap_binary_equal(&rcvd->actual_token, lg_crcv->app_token)) {
4399 coap_update_token(rcvd, lg_crcv->app_token->length, lg_crcv->app_token->s);
4400 coap_log_debug("Client app version of updated PDU (5)\n");
4402 }
4403
4404 if (sent) {
4405 /* need to put back original token into sent */
4406 if (lg_crcv->app_token)
4407 coap_update_token(sent, lg_crcv->app_token->length,
4408 lg_crcv->app_token->s);
4409 coap_remove_option(sent, lg_crcv->block_option);
4410 }
4411 /* Expire this entry */
4412 LL_DELETE(session->lg_crcv, lg_crcv);
4413 coap_block_delete_lg_crcv(session, lg_crcv);
4414
4415call_app_handler:
4416 return 0;
4417
4418skip_app_handler:
4419 if (!ack_rst_sent)
4420 coap_send_ack_lkd(session, rcvd);
4421 return 1;
4422}
4423#endif /* COAP_CLIENT_SUPPORT */
4424
4425#if COAP_SERVER_SUPPORT
4426/* Check if lg_xmit generated and update PDU code if so */
4427void
4429 const coap_pdu_t *request,
4430 coap_pdu_t *response, const coap_resource_t *resource,
4431 const coap_string_t *query) {
4432 coap_lg_xmit_t *lg_xmit;
4433
4434 if (response->code == 0)
4435 return;
4436 lg_xmit = coap_find_lg_xmit_response(session, request, resource, query);
4437 if (lg_xmit && lg_xmit->sent_pdu && lg_xmit->sent_pdu->code == 0) {
4438 lg_xmit->sent_pdu->code = response->code;
4439 return;
4440 }
4441}
4442#endif /* COAP_SERVER_SUPPORT */
4443
4444#if COAP_CLIENT_SUPPORT
4445void
4447 uint64_t token_match =
4449 pdu->actual_token.length));
4450 coap_lg_xmit_t *lg_xmit;
4451 coap_lg_crcv_t *lg_crcv;
4452
4453 if (session->lg_crcv) {
4454 LL_FOREACH(session->lg_crcv, lg_crcv) {
4455 if (coap_binary_equal(&pdu->actual_token, lg_crcv->app_token))
4456 return;
4457 if (token_match == STATE_TOKEN_BASE(lg_crcv->state_token)) {
4458 coap_update_token(pdu, lg_crcv->app_token->length,
4459 lg_crcv->app_token->s);
4460 coap_log_debug("Client app version of updated PDU (6)\n");
4462 return;
4463 }
4464 }
4465 }
4466 if (COAP_PDU_IS_REQUEST(pdu) && session->lg_xmit) {
4467 LL_FOREACH(session->lg_xmit, lg_xmit) {
4468 if (coap_binary_equal(&pdu->actual_token, lg_xmit->b.b1.app_token))
4469 return;
4470 if (token_match == STATE_TOKEN_BASE(lg_xmit->b.b1.state_token)) {
4471 coap_update_token(pdu, lg_xmit->b.b1.app_token->length,
4472 lg_xmit->b.b1.app_token->s);
4473 coap_log_debug("Client app version of updated PDU (7)\n");
4475 return;
4476 }
4477 }
4478 }
4479}
4480#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
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:4904
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_ticks(coap_tick_t *t)
Returns the current value of an internal tick counter.
Definition coap_time.c:90
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:331
#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:334
@ COAP_REQUEST_CODE_FETCH
Definition coap_pdu.h:338
@ 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.
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 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.
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