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