libcoap 4.3.5-develop-f0533d3
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
2135 while (block_pdu) {
2136 coap_pdu_t *t_pdu = NULL;
2137 uint8_t buf[8];
2138 size_t chunk = ((size_t)1 << (lg_xmit->blk_size + 4));
2139
2140 block.num++;
2141 lg_xmit->offset = block.num * chunk;
2142 block.m = lg_xmit->offset + chunk < lg_xmit->data_info->length;
2143 if (block.m && ((block_pdu->type == COAP_MESSAGE_NON &&
2144 (block.num % COAP_MAX_PAYLOADS(session)) + 1 !=
2145 COAP_MAX_PAYLOADS(session)) ||
2146 (block_pdu->type == COAP_MESSAGE_CON &&
2147 delayqueue_cnt + 1 < COAP_NSTART(session)) ||
2148 COAP_PROTO_RELIABLE(session->proto))) {
2149 /*
2150 * Send following block if
2151 * NON and more in MAX_PAYLOADS
2152 * CON and NSTART allows it (based on number in delayqueue)
2153 * Reliable transport
2154 */
2155 if (COAP_PDU_IS_RESPONSE(block_pdu)) {
2156 ptoken = block_pdu->actual_token.s;
2157 ltoken_length = block_pdu->actual_token.length;
2158 } else {
2159 token = STATE_TOKEN_FULL(lg_xmit->b.b1.state_token,++lg_xmit->b.b1.count);
2160 ltoken_length = coap_encode_var_safe8(ltoken, sizeof(token), token);
2161 ptoken = ltoken;
2162 }
2163 t_pdu = coap_pdu_duplicate_lkd(block_pdu, session,
2164 ltoken_length, ptoken, &drop_options);
2165 }
2166 if (!coap_update_option(block_pdu, lg_xmit->option,
2168 sizeof(buf),
2169 ((block.num) << 4) |
2170 (block.m << 3) |
2171 block.szx),
2172 buf)) {
2173 coap_log_warn("Internal update issue option\n");
2174 coap_delete_pdu_lkd(block_pdu);
2175 coap_delete_pdu_lkd(t_pdu);
2176 break;
2177 }
2178
2179 if (lg_xmit->data_info->get_func) {
2180#if COAP_CONSTRAINED_STACK
2181 /* Protected by global_lock if needed */
2182 static uint8_t l_data[1024];
2183#else /* ! COAP_CONSTRAINED_STACK */
2184 uint8_t l_data[1024];
2185#endif /* ! COAP_CONSTRAINED_STACK */
2186 size_t l_length;
2187
2188 assert(chunk <= 1024);
2189 if (lg_xmit->data_info->get_func(session, chunk,
2190 block.num * chunk, l_data, &l_length,
2191 lg_xmit->data_info->app_ptr)) {
2192 if (!coap_add_data(block_pdu, l_length, l_data)) {
2193 coap_log_warn("Internal update issue data (1)\n");
2194 coap_delete_pdu_lkd(block_pdu);
2195 coap_delete_pdu_lkd(t_pdu);
2196 break;
2197 }
2198 }
2199 } else {
2200 if (!coap_add_block(block_pdu,
2201 lg_xmit->data_info->length,
2202 lg_xmit->data_info->data,
2203 block.num,
2204 block.szx)) {
2205 coap_log_warn("Internal update issue data (2)\n");
2206 coap_delete_pdu_lkd(block_pdu);
2207 coap_delete_pdu_lkd(t_pdu);
2208 break;
2209 }
2210 }
2211 if (COAP_PDU_IS_RESPONSE(block_pdu)) {
2212 lg_xmit->last_block = block.num;
2213 }
2214 mid = coap_send_internal(session, block_pdu, NULL);
2215 if (mid == COAP_INVALID_MID) {
2216 /* Not expected, underlying issue somewhere */
2217 coap_delete_pdu_lkd(t_pdu);
2218 return COAP_INVALID_MID;
2219 }
2220 block_pdu = t_pdu;
2221 }
2222 if (!block.m) {
2223 lg_xmit->last_payload = 0;
2224 coap_ticks(&lg_xmit->last_all_sent);
2225 } else
2226 coap_ticks(&lg_xmit->last_payload);
2227 return mid;
2228}
2229
2230#if COAP_CLIENT_SUPPORT
2231/*
2232 * Return 1 if there is a future expire time, else 0.
2233 * Update tim_rem with remaining value if return is 1.
2234 */
2235int
2236coap_block_check_q_block1_xmit(coap_session_t *session, coap_tick_t now, coap_tick_t *tim_rem) {
2237 coap_lg_xmit_t *lg_xmit;
2238 coap_lg_xmit_t *q;
2239 coap_tick_t timed_out;
2240 int ret = 0;
2241
2242 *tim_rem = COAP_MAX_DELAY_TICKS;
2243 LL_FOREACH_SAFE(session->lg_xmit, lg_xmit, q) {
2244 coap_tick_t non_timeout = lg_xmit->non_timeout_random_ticks;
2245
2246 if (now <= non_timeout) {
2247 /* Too early in the startup cycle to have an accurate response */
2248 *tim_rem = non_timeout - now;
2249 return 1;
2250 }
2251 timed_out = now - non_timeout;
2252
2253 if (lg_xmit->last_payload) {
2254 if (lg_xmit->last_payload <= timed_out) {
2255 /* Send off the next MAX_PAYLOAD set */
2256 coap_block_b_t block;
2257 size_t chunk = (size_t)1 << (lg_xmit->blk_size + 4);
2258
2259 memset(&block, 0, sizeof(block));
2260 block.num = (uint32_t)(lg_xmit->offset / chunk);
2261 block.m = lg_xmit->offset + chunk < lg_xmit->data_info->length;
2262 block.szx = lg_xmit->blk_size;
2263 coap_send_q_blocks(session, lg_xmit, block, lg_xmit->sent_pdu, COAP_SEND_SKIP_PDU);
2264 if (*tim_rem > non_timeout) {
2265 *tim_rem = non_timeout;
2266 ret = 1;
2267 }
2268 } else {
2269 /* Delay until the next MAX_PAYLOAD needs to be sent off */
2270 if (*tim_rem > lg_xmit->last_payload - timed_out) {
2271 *tim_rem = lg_xmit->last_payload - timed_out;
2272 ret = 1;
2273 }
2274 }
2275 } else if (lg_xmit->last_all_sent) {
2276 non_timeout = COAP_NON_TIMEOUT_TICKS(session);
2277 if (lg_xmit->last_all_sent + 4 * non_timeout <= now) {
2278 /* Expire this entry */
2279 LL_DELETE(session->lg_xmit, lg_xmit);
2280 coap_block_delete_lg_xmit(session, lg_xmit);
2281 } else {
2282 /* Delay until the lg_xmit needs to expire */
2283 if (*tim_rem > lg_xmit->last_all_sent + 4 * non_timeout - now) {
2284 *tim_rem = lg_xmit->last_all_sent + 4 * non_timeout - now;
2285 ret = 1;
2286 }
2287 }
2288 }
2289 }
2290 return ret;
2291}
2292#endif /* COAP_CLIENT_SUPPORT */
2293
2294#if COAP_SERVER_SUPPORT
2295/*
2296 * Return 1 if there is a future expire time, else 0.
2297 * Update tim_rem with remaining value if return is 1.
2298 */
2299int
2300coap_block_check_q_block2_xmit(coap_session_t *session, coap_tick_t now, coap_tick_t *tim_rem) {
2301 coap_lg_xmit_t *lg_xmit;
2302 coap_lg_xmit_t *q;
2303 coap_tick_t timed_out;
2304 int ret = 0;
2305
2306 *tim_rem = (coap_tick_t)-1;
2307 LL_FOREACH_SAFE(session->lg_xmit, lg_xmit, q) {
2308 coap_tick_t non_timeout = lg_xmit->non_timeout_random_ticks;
2309
2310 if (now <= non_timeout) {
2311 /* Too early in the startup cycle to have an accurate response */
2312 *tim_rem = non_timeout - now;
2313 return 1;
2314 }
2315 timed_out = now - non_timeout;
2316
2317 if (lg_xmit->last_payload) {
2318 if (lg_xmit->last_payload <= timed_out) {
2319 /* Send off the next MAX_PAYLOAD set */
2320 coap_block_b_t block;
2321 size_t chunk = (size_t)1 << (lg_xmit->blk_size + 4);
2322
2323 memset(&block, 0, sizeof(block));
2324 block.num = (uint32_t)(lg_xmit->offset / chunk);
2325 block.m = lg_xmit->offset + chunk < lg_xmit->data_info->length;
2326 block.szx = lg_xmit->blk_size;
2327 if (block.num == (uint32_t)lg_xmit->last_block)
2328 coap_send_q_blocks(session, lg_xmit, block, lg_xmit->sent_pdu, COAP_SEND_SKIP_PDU);
2329 if (*tim_rem > non_timeout) {
2330 *tim_rem = non_timeout;
2331 ret = 1;
2332 }
2333 } else {
2334 /* Delay until the next MAX_PAYLOAD needs to be sent off */
2335 if (*tim_rem > lg_xmit->last_payload - timed_out) {
2336 *tim_rem = lg_xmit->last_payload - timed_out;
2337 ret = 1;
2338 }
2339 }
2340 } else if (lg_xmit->last_all_sent) {
2341 non_timeout = COAP_NON_TIMEOUT_TICKS(session);
2342 if (lg_xmit->last_all_sent + 4 * non_timeout <= now) {
2343 /* Expire this entry */
2344 LL_DELETE(session->lg_xmit, lg_xmit);
2345 coap_block_delete_lg_xmit(session, lg_xmit);
2346 } else {
2347 /* Delay until the lg_xmit needs to expire */
2348 if (*tim_rem > lg_xmit->last_all_sent + 4 * non_timeout - now) {
2349 *tim_rem = lg_xmit->last_all_sent + 4 * non_timeout - now;
2350 ret = 1;
2351 }
2352 }
2353 }
2354 }
2355 return ret;
2356}
2357#endif /* COAP_SERVER_SUPPORT */
2358#endif /* COAP_Q_BLOCK_SUPPORT */
2359
2360#if COAP_CLIENT_SUPPORT
2361/*
2362 * If Observe = 0, save the token away and return NULL
2363 * Else If Observe = 1, return the saved token for this block
2364 * Else, return NULL
2365 */
2366static coap_bin_const_t *
2367track_fetch_observe(coap_pdu_t *pdu, coap_lg_crcv_t *lg_crcv,
2368 uint32_t block_num, coap_bin_const_t *token) {
2369 /* Need to handle Observe for large FETCH */
2370 coap_opt_iterator_t opt_iter;
2372 &opt_iter);
2373
2374 if (opt && lg_crcv) {
2375 int observe_action = -1;
2376 coap_bin_const_t **tmp;
2377
2378 observe_action = coap_decode_var_bytes(coap_opt_value(opt),
2379 coap_opt_length(opt));
2380 if (observe_action == COAP_OBSERVE_ESTABLISH) {
2381 /* Save the token in lg_crcv */
2382 if (lg_crcv->obs_token_cnt <= block_num) {
2383 size_t i;
2384
2385 tmp = coap_realloc_type(COAP_STRING, lg_crcv->obs_token,
2386 (block_num + 1) * sizeof(lg_crcv->obs_token[0]));
2387 if (tmp == NULL)
2388 return NULL;
2389 lg_crcv->obs_token = tmp;
2390 for (i = lg_crcv->obs_token_cnt; i < block_num + 1; i++) {
2391 lg_crcv->obs_token[i] = NULL;
2392 }
2393 }
2394 coap_delete_bin_const(lg_crcv->obs_token[block_num]);
2395
2396 if (lg_crcv->obs_token_cnt <= block_num)
2397 lg_crcv->obs_token_cnt = block_num + 1;
2398 lg_crcv->obs_token[block_num] = coap_new_bin_const(token->s,
2399 token->length);
2400 if (lg_crcv->obs_token[block_num] == NULL)
2401 return NULL;
2402 } else if (observe_action == COAP_OBSERVE_CANCEL) {
2403 /* Use the token in lg_crcv */
2404 if (block_num < lg_crcv->obs_token_cnt) {
2405 return lg_crcv->obs_token[block_num];
2406 }
2407 }
2408 }
2409 return NULL;
2410}
2411
2412#if COAP_Q_BLOCK_SUPPORT
2414coap_send_q_block1(coap_session_t *session,
2415 coap_block_b_t block,
2416 coap_pdu_t *request,
2417 coap_send_pdu_t send_request) {
2418 /* Need to send up to MAX_PAYLOAD blocks if this is a Q_BLOCK1 */
2419 coap_lg_xmit_t *lg_xmit;
2420 uint64_t token_match =
2422 request->actual_token.length));
2423
2424 LL_FOREACH(session->lg_xmit, lg_xmit) {
2425 if (lg_xmit->option == COAP_OPTION_Q_BLOCK1 &&
2426 (token_match == STATE_TOKEN_BASE(lg_xmit->b.b1.state_token) ||
2427 token_match ==
2429 lg_xmit->b.b1.app_token->length))))
2430 break;
2431 /* try out the next one */
2432 }
2433 return coap_send_q_blocks(session, lg_xmit, block, request, send_request);
2434}
2435#endif /* COAP_Q_BLOCK_SUPPORT */
2436#endif /* COAP_CLIENT_SUPPORT */
2437
2438#if COAP_SERVER_SUPPORT
2439#if COAP_Q_BLOCK_SUPPORT
2440/*
2441 * response is always released before return IF COAP_SEND_INC_PDU
2442 */
2444coap_send_q_block2(coap_session_t *session,
2445 coap_resource_t *resource,
2446 const coap_string_t *query,
2447 coap_pdu_code_t request_method,
2448 coap_block_b_t block,
2449 coap_pdu_t *response,
2450 coap_send_pdu_t send_response) {
2451 /* Need to send up to MAX_PAYLOAD blocks if this is a Q_BLOCK2 */
2452 coap_lg_xmit_t *lg_xmit;
2453 coap_string_t empty = { 0, NULL};
2454
2455 LL_FOREACH(session->lg_xmit, lg_xmit) {
2456 if (lg_xmit->option == COAP_OPTION_Q_BLOCK2 &&
2457 resource == lg_xmit->b.b2.resource &&
2458 request_method == lg_xmit->b.b2.request_method &&
2459 coap_string_equal(query ? query : &empty,
2460 lg_xmit->b.b2.query ? lg_xmit->b.b2.query : &empty))
2461 break;
2462 }
2463 return coap_send_q_blocks(session, lg_xmit, block, response, send_response);
2464}
2465#endif /* COAP_Q_BLOCK_SUPPORT */
2466#endif /* COAP_SERVER_SUPPORT */
2467
2468static void
2470 coap_lg_xmit_data_t *data_info) {
2471 if (!data_info)
2472 return;
2473 if (data_info->ref > 0) {
2474 data_info->ref--;
2475 return;
2476 }
2477 if (data_info->release_func) {
2478 coap_lock_callback(data_info->release_func(session,
2479 data_info->app_ptr));
2480 data_info->release_func = NULL;
2481 }
2482 coap_free_type(COAP_STRING, data_info);
2483}
2484
2485#if COAP_CLIENT_SUPPORT
2486#if COAP_Q_BLOCK_SUPPORT
2487/*
2488 * Send out a test PDU for Q-Block.
2489 */
2491coap_block_test_q_block(coap_session_t *session, coap_pdu_t *actual) {
2492 coap_pdu_t *pdu;
2493 uint8_t token[8];
2494 size_t token_len;
2495 uint8_t buf[4];
2496 coap_mid_t mid;
2497
2498#if NDEBUG
2499 (void)actual;
2500#endif /* NDEBUG */
2501 assert(session->block_mode & COAP_BLOCK_TRY_Q_BLOCK &&
2502 session->type == COAP_SESSION_TYPE_CLIENT &&
2503 COAP_PDU_IS_REQUEST(actual));
2504
2505 coap_log_debug("Testing for Q-Block support\n");
2506 /* RFC9177 Section 4.1 when checking if available */
2508 coap_new_message_id_lkd(session),
2510 if (!pdu) {
2511 return COAP_INVALID_MID;
2512 }
2513
2514 coap_session_new_token(session, &token_len, token);
2515 coap_add_token(pdu, token_len, token);
2516 /* Use a resource that the server MUST support (.well-known/core) */
2518 11, (const uint8_t *)".well-known");
2520 4, (const uint8_t *)"core");
2521 /*
2522 * M needs to be unset as 'asking' for only the first block using
2523 * Q-Block2 as a test for server support.
2524 * See RFC9177 Section 4.4 Using the Q-Block2 Option.
2525 *
2526 * As the client is asking for 16 byte chunks, it is unlikely that
2527 * the .well-known/core response will be 16 bytes or less, so
2528 * if the server supports Q-Block, it will be forced to respond with
2529 * a Q-Block2, so the client can detect the server Q-Block support.
2530 */
2532 coap_encode_var_safe(buf, sizeof(buf),
2533 (0 << 4) | (0 << 3) | 0),
2534 buf);
2535 set_block_mode_probe_q(session->block_mode);
2536 mid = coap_send_internal(session, pdu, NULL);
2537 if (mid == COAP_INVALID_MID)
2538 return COAP_INVALID_MID;
2539 session->remote_test_mid = mid;
2540 return mid;
2541}
2542#endif /* COAP_Q_BLOCK_SUPPORT */
2543
2545coap_block_new_lg_crcv(coap_session_t *session, coap_pdu_t *pdu,
2546 coap_lg_xmit_t *lg_xmit) {
2547 coap_block_b_t block;
2548 coap_lg_crcv_t *lg_crcv;
2549 uint64_t state_token = STATE_TOKEN_FULL(++session->tx_token, 1);
2550
2551 lg_crcv = coap_malloc_type(COAP_LG_CRCV, sizeof(coap_lg_crcv_t));
2552
2553 if (lg_crcv == NULL)
2554 return NULL;
2555
2556 coap_log_debug("** %s: lg_crcv %p initialized - stateless token xxxxx%011llx\n",
2557 coap_session_str(session), (void *)lg_crcv,
2558 STATE_TOKEN_BASE(state_token));
2559 memset(lg_crcv, 0, sizeof(coap_lg_crcv_t));
2560 lg_crcv->initial = 1;
2561 coap_ticks(&lg_crcv->last_used);
2562 /* Keep a copy of the sent pdu */
2563 lg_crcv->sent_pdu = coap_pdu_reference_lkd(pdu);
2564 if (lg_xmit) {
2565 coap_opt_iterator_t opt_iter;
2566 coap_opt_t *opt;
2567
2568 opt = coap_check_option(pdu, COAP_OPTION_OBSERVE, &opt_iter);
2569
2570 if (opt) {
2571 int observe_action;
2572
2573 observe_action = coap_decode_var_bytes(coap_opt_value(opt),
2574 coap_opt_length(opt));
2575 if (observe_action == COAP_OBSERVE_ESTABLISH) {
2576 /* Need to keep information for Observe Cancel */
2577 size_t data_len;
2578 const uint8_t *data;
2579
2580 if (coap_get_data(pdu, &data_len, &data)) {
2581 if (data_len < lg_xmit->data_info->length) {
2582 lg_xmit->data_info->ref++;
2583 lg_crcv->obs_data = lg_xmit->data_info;
2584 }
2585 }
2586 }
2587 }
2588 }
2589
2590 /* Need to keep original token for updating response PDUs */
2591 lg_crcv->app_token = coap_new_binary(pdu->actual_token.length);
2592 if (!lg_crcv->app_token) {
2593 coap_block_delete_lg_crcv(session, lg_crcv);
2594 return NULL;
2595 }
2596 memcpy(lg_crcv->app_token->s, pdu->actual_token.s, pdu->actual_token.length);
2597
2598 /* Need to set up a base token for actual communications if retries needed */
2599 lg_crcv->retry_counter = 1;
2600 lg_crcv->state_token = state_token;
2601 coap_address_copy(&lg_crcv->upstream, &session->addr_info.remote);
2602
2603 if (pdu->code == COAP_REQUEST_CODE_FETCH) {
2604 coap_bin_const_t *new_token;
2605
2606 /* Need to save/restore Observe Token for large FETCH */
2607 new_token = track_fetch_observe(pdu, lg_crcv, 0, &pdu->actual_token);
2608 if (new_token)
2609 coap_update_token(pdu, new_token->length, new_token->s);
2610 }
2611
2612 if (coap_get_block_b(session, pdu, COAP_OPTION_BLOCK1, &block)) {
2613 /* In case it is there - must not be in continuing request PDUs */
2614 lg_crcv->o_block_option = COAP_OPTION_BLOCK1;
2615 lg_crcv->o_blk_size = block.aszx;
2616 }
2617
2618 return lg_crcv;
2619}
2620
2621void
2622coap_block_delete_lg_crcv(coap_session_t *session,
2623 coap_lg_crcv_t *lg_crcv) {
2624 size_t i;
2625
2626#if (COAP_MAX_LOGGING_LEVEL < _COAP_LOG_DEBUG)
2627 (void)session;
2628#endif
2629 if (lg_crcv == NULL)
2630 return;
2631
2632 coap_free_type(COAP_STRING, lg_crcv->body_data);
2633 if (lg_crcv->obs_data) {
2634 coap_block_release_lg_xmit_data(session, lg_crcv->obs_data);
2635 lg_crcv->obs_data = NULL;
2636 }
2637 coap_address_copy(&session->addr_info.remote, &lg_crcv->upstream);
2638 coap_log_debug("** %s: lg_crcv %p released\n",
2639 coap_session_str(session), (void *)lg_crcv);
2640 coap_delete_binary(lg_crcv->app_token);
2641 for (i = 0; i < lg_crcv->obs_token_cnt; i++) {
2642 coap_delete_bin_const(lg_crcv->obs_token[i]);
2643 }
2644 coap_free_type(COAP_STRING, lg_crcv->obs_token);
2645 coap_delete_pdu_lkd(lg_crcv->sent_pdu);
2646 coap_free_type(COAP_LG_CRCV, lg_crcv);
2647}
2648#endif /* COAP_CLIENT_SUPPORT */
2649
2650#if COAP_SERVER_SUPPORT
2651void
2652coap_block_delete_lg_srcv(coap_session_t *session,
2653 coap_lg_srcv_t *lg_srcv) {
2654#if (COAP_MAX_LOGGING_LEVEL < _COAP_LOG_DEBUG)
2655 (void)session;
2656#endif
2657 if (lg_srcv == NULL)
2658 return;
2659
2660 coap_delete_str_const(lg_srcv->uri_path);
2661 coap_delete_bin_const(lg_srcv->last_token);
2662 coap_free_type(COAP_STRING, lg_srcv->body_data);
2663 coap_log_debug("** %s: lg_srcv %p released\n",
2664 coap_session_str(session), (void *)lg_srcv);
2665 coap_free_type(COAP_LG_SRCV, lg_srcv);
2666}
2667#endif /* COAP_SERVER_SUPPORT */
2668
2669void
2671 coap_lg_xmit_t *lg_xmit) {
2672 if (lg_xmit == NULL)
2673 return;
2674
2675 coap_block_release_lg_xmit_data(session, lg_xmit->data_info);
2676 if (COAP_PDU_IS_REQUEST(lg_xmit->sent_pdu))
2677 coap_delete_binary(lg_xmit->b.b1.app_token);
2678 else
2679 coap_delete_string(lg_xmit->b.b2.query);
2680 coap_delete_pdu_lkd(lg_xmit->sent_pdu);
2681
2682 coap_log_debug("** %s: lg_xmit %p released\n",
2683 coap_session_str(session), (void *)lg_xmit);
2684 coap_free_type(COAP_LG_XMIT, lg_xmit);
2685}
2686
2687#if COAP_SERVER_SUPPORT
2688typedef struct {
2689 uint32_t num;
2690 int is_continue;
2691} send_track;
2692
2693static int
2694add_block_send(uint32_t num, int is_continue, send_track *out_blocks,
2695 uint32_t *count, uint32_t max_count) {
2696 uint32_t i;
2697
2698 for (i = 0; i < *count && *count < max_count; i++) {
2699 if (num == out_blocks[i].num)
2700 return 0;
2701 else if (num < out_blocks[i].num) {
2702 if (*count - i > 1)
2703 memmove(&out_blocks[i], &out_blocks[i+1], *count - i -1);
2704 out_blocks[i].num = num;
2705 out_blocks[i].is_continue = is_continue;
2706 (*count)++;
2707 return 1;
2708 }
2709 }
2710 if (*count < max_count) {
2711 out_blocks[i].num = num;
2712 out_blocks[i].is_continue = is_continue;
2713 (*count)++;
2714 return 1;
2715 }
2716 return 0;
2717}
2718
2719/*
2720 * Need to see if this is a request for the next block of a large body
2721 * transfer. If so, need to initiate the response with the next blocks
2722 * and not trouble the application.
2723 *
2724 * If additional responses needed, then these are expicitly sent out and
2725 * 'response' is updated to be the last response to be sent. There can be
2726 * multiple Q-Block2 in the request, as well as the 'Continue' Q-Block2
2727 * request.
2728 *
2729 * This is set up using coap_add_data_large_response_lkd()
2730 *
2731 * Server is sending a large data response to GET / observe (Block2)
2732 *
2733 * Return: 0 Call application handler
2734 * 1 Do not call application handler - just send the built response
2735 */
2736int
2737coap_handle_request_send_block(coap_session_t *session,
2738 coap_pdu_t *pdu,
2739 coap_pdu_t *response,
2740 coap_resource_t *resource,
2741 coap_string_t *query) {
2742 coap_lg_xmit_t *lg_xmit = NULL;
2743 coap_block_b_t block;
2744 coap_block_b_t alt_block;
2745 uint16_t block_opt = 0;
2746 send_track *out_blocks = NULL;
2747 const char *error_phrase;
2748 coap_opt_iterator_t opt_iter;
2749 size_t chunk;
2750 coap_opt_iterator_t opt_b_iter;
2751 coap_opt_t *option;
2752 uint32_t request_cnt, i;
2753 coap_opt_t *etag_opt = NULL;
2754 coap_pdu_t *out_pdu = response;
2755#if COAP_Q_BLOCK_SUPPORT
2756 size_t max_block;
2757
2758 /* Is client indicating that it supports Q_BLOCK2 ? */
2759 if (coap_get_block_b(session, pdu, COAP_OPTION_Q_BLOCK2, &block)) {
2760 if (!(session->block_mode & COAP_BLOCK_HAS_Q_BLOCK))
2761 set_block_mode_has_q(session->block_mode);
2762 block_opt = COAP_OPTION_Q_BLOCK2;
2763 }
2764#endif /* COAP_Q_BLOCK_SUPPORT */
2765 if (coap_get_block_b(session, pdu, COAP_OPTION_BLOCK2, &alt_block)) {
2766 if (block_opt) {
2767 coap_log_warn("Block2 and Q-Block2 cannot be in the same request\n");
2768 coap_add_data(response, sizeof("Both Block2 and Q-Block2 invalid")-1,
2769 (const uint8_t *)"Both Block2 and Q-Block2 invalid");
2770 response->code = COAP_RESPONSE_CODE(400);
2771 goto skip_app_handler;
2772 }
2773 block = alt_block;
2774 block_opt = COAP_OPTION_BLOCK2;
2775 }
2776 if (block_opt == 0)
2777 return 0;
2778 if (block.num == 0) {
2779 /* Get a fresh copy of the data */
2780 return 0;
2781 }
2782 lg_xmit = coap_find_lg_xmit_response(session, pdu, resource, query);
2783 if (lg_xmit == NULL)
2784 return 0;
2785
2786#if COAP_Q_BLOCK_SUPPORT
2787 out_blocks = coap_malloc_type(COAP_STRING, sizeof(send_track) * COAP_MAX_PAYLOADS(session));
2788#else /* ! COAP_Q_BLOCK_SUPPORT */
2789 out_blocks = coap_malloc_type(COAP_STRING, sizeof(send_track));
2790#endif /* ! COAP_Q_BLOCK_SUPPORT */
2791 if (!out_blocks) {
2792 goto internal_issue;
2793 }
2794
2795 /* lg_xmit (response) found */
2796
2797 etag_opt = coap_check_option(pdu, COAP_OPTION_ETAG, &opt_iter);
2798 if (etag_opt) {
2799 /* There may be multiple ETag - need to check each one */
2800 coap_option_iterator_init(pdu, &opt_iter, COAP_OPT_ALL);
2801 while ((etag_opt = coap_option_next(&opt_iter))) {
2802 if (opt_iter.number == COAP_OPTION_ETAG) {
2803 uint64_t etag = coap_decode_var_bytes8(coap_opt_value(etag_opt),
2804 coap_opt_length(etag_opt));
2805 if (etag == lg_xmit->b.b2.etag) {
2806 break;
2807 }
2808 }
2809 }
2810 if (!etag_opt) {
2811 /* Not a match - pass up to a higher level */
2812 return 0;
2813 }
2814 }
2815 out_pdu->code = lg_xmit->sent_pdu->code;
2816 coap_ticks(&lg_xmit->last_obs);
2817 lg_xmit->last_all_sent = 0;
2818
2819 chunk = (size_t)1 << (lg_xmit->blk_size + 4);
2820 if (block_opt) {
2821 if (block.bert) {
2822 coap_log_debug("found Block option, block is BERT, block nr. %u, M %d\n",
2823 block.num, block.m);
2824 } else {
2825 coap_log_debug("found Block option, block size is %u, block nr. %u, M %d\n",
2826 1 << (block.szx + 4), block.num, block.m);
2827 }
2828 if (block.bert == 0 && block.szx != lg_xmit->blk_size) {
2829 if (block.num == 0) {
2830 if ((lg_xmit->offset + chunk) % ((size_t)1 << (block.szx + 4)) == 0) {
2831 /*
2832 * Recompute the block number of the previous packet given
2833 * the new block size
2834 */
2835 block.num = (uint32_t)(((lg_xmit->offset + chunk) >> (block.szx + 4)) - 1);
2836 lg_xmit->blk_size = block.szx;
2837 chunk = (size_t)1 << (lg_xmit->blk_size + 4);
2838 lg_xmit->offset = block.num * chunk;
2839 coap_log_debug("new Block size is %u, block number %u completed\n",
2840 1 << (block.szx + 4), block.num);
2841 } else {
2842 coap_log_debug("ignoring request to increase Block size, "
2843 "next block is not aligned on requested block size "
2844 "boundary. (%" PRIuS " x %u mod %u = %" PRIuS " (which is not 0)\n",
2845 lg_xmit->offset/chunk + 1, (1 << (lg_xmit->blk_size + 4)),
2846 (1 << (block.szx + 4)),
2847 (lg_xmit->offset + chunk) % ((size_t)1 << (block.szx + 4)));
2848 }
2849 } else {
2850 coap_log_debug("ignoring request to change Block size from %u to %u\n",
2851 (1 << (lg_xmit->blk_size + 4)), (1 << (block.szx + 4)));
2852 block.szx = block.aszx = lg_xmit->blk_size;
2853 }
2854 }
2855 }
2856
2857 /*
2858 * Need to check if there are multiple Q-Block2 requests. If so, they
2859 * need to be sent out in order of requests with the final request being
2860 * handled as per singular Block 2 request.
2861 */
2862 request_cnt = 0;
2863#if COAP_Q_BLOCK_SUPPORT
2864 max_block = (lg_xmit->data_info->length + chunk - 1)/chunk;
2865#endif /* COAP_Q_BLOCK_SUPPORT */
2866 coap_option_iterator_init(pdu, &opt_b_iter, COAP_OPT_ALL);
2867 while ((option = coap_option_next(&opt_b_iter))) {
2868 uint32_t num;
2869 if (opt_b_iter.number != lg_xmit->option)
2870 continue;
2871 num = coap_opt_block_num(option);
2872 if (num > 0xFFFFF) /* 20 bits max for num */
2873 continue;
2874 if (block.aszx != COAP_OPT_BLOCK_SZX(option)) {
2875 coap_add_data(response,
2876 sizeof("Changing blocksize during request invalid")-1,
2877 (const uint8_t *)"Changing blocksize during request invalid");
2878 response->code = COAP_RESPONSE_CODE(400);
2879 goto skip_app_handler;
2880 }
2881#if COAP_Q_BLOCK_SUPPORT
2882 if (COAP_OPT_BLOCK_MORE(option) && lg_xmit->option == COAP_OPTION_Q_BLOCK2) {
2883 if ((num % COAP_MAX_PAYLOADS(session)) == 0) {
2884 if (num == 0) {
2885 /* This is a repeat request for everything - hmm */
2886 goto call_app_handler;
2887 }
2888 /* 'Continue' request */
2889 for (i = 0; i < COAP_MAX_PAYLOADS(session) &&
2890 num + i < max_block; i++) {
2891 add_block_send(num + i, 1, out_blocks, &request_cnt,
2892 COAP_MAX_PAYLOADS(session));
2893 lg_xmit->last_block = num + i;
2894 }
2895 } else {
2896 /* Requesting remaining payloads in this MAX_PAYLOADS */
2897 for (i = 0; i < COAP_MAX_PAYLOADS(session) -
2898 num % COAP_MAX_PAYLOADS(session) &&
2899 num + i < max_block; i++) {
2900 add_block_send(num + i, 0, out_blocks, &request_cnt,
2901 COAP_MAX_PAYLOADS(session));
2902 }
2903 }
2904 } else
2905 add_block_send(num, 0, out_blocks, &request_cnt,
2906 COAP_MAX_PAYLOADS(session));
2907#else /* ! COAP_Q_BLOCK_SUPPORT */
2908 add_block_send(num, 0, out_blocks, &request_cnt, 1);
2909 break;
2910#endif /* ! COAP_Q_BLOCK_SUPPORT */
2911 }
2912 if (request_cnt == 0) {
2913 /* Block2 or Q-Block2 not found - give them the first block */
2914 block.szx = lg_xmit->blk_size;
2915 lg_xmit->offset = 0;
2916 out_blocks[0].num = 0;
2917 out_blocks[0].is_continue = 0;
2918 request_cnt = 1;
2919 }
2920
2921 for (i = 0; i < request_cnt; i++) {
2922 uint8_t buf[8];
2923
2924 block.num = out_blocks[i].num;
2925 lg_xmit->offset = block.num * chunk;
2926
2927 if (i + 1 < request_cnt) {
2928 /* Need to set up a copy of the pdu to send */
2929 coap_opt_filter_t drop_options;
2930
2931 memset(&drop_options, 0, sizeof(coap_opt_filter_t));
2932 if (block.num != 0)
2934 if (out_blocks[i].is_continue) {
2935 out_pdu = coap_pdu_duplicate_lkd(lg_xmit->sent_pdu, session,
2936 lg_xmit->sent_pdu->actual_token.length,
2937 lg_xmit->sent_pdu->actual_token.s, &drop_options);
2938 } else {
2939 out_pdu = coap_pdu_duplicate_lkd(lg_xmit->sent_pdu, session, pdu->actual_token.length,
2940 pdu->actual_token.s, &drop_options);
2941 }
2942 if (!out_pdu) {
2943 goto internal_issue;
2944 }
2945 } else {
2946 if (out_blocks[i].is_continue)
2947 coap_update_token(response, lg_xmit->sent_pdu->actual_token.length,
2948 lg_xmit->sent_pdu->actual_token.s);
2949 /*
2950 * Copy the options across and then fix the block option
2951 *
2952 * Need to drop Observe option if Block2 and block.num != 0
2953 */
2954 coap_option_iterator_init(lg_xmit->sent_pdu, &opt_iter, COAP_OPT_ALL);
2955 while ((option = coap_option_next(&opt_iter))) {
2956 if (opt_iter.number == COAP_OPTION_OBSERVE && block.num != 0)
2957 continue;
2958 if (!coap_insert_option(response, opt_iter.number,
2959 coap_opt_length(option),
2960 coap_opt_value(option))) {
2961 goto internal_issue;
2962 }
2963 }
2964 out_pdu = response;
2965 }
2966 if (pdu->type == COAP_MESSAGE_NON)
2967 out_pdu->type = COAP_MESSAGE_NON;
2968 if (block.bert) {
2969 size_t token_options = pdu->data ? (size_t)(pdu->data - pdu->token) : pdu->used_size;
2970 block.m = (lg_xmit->data_info->length - lg_xmit->offset) >
2971 ((out_pdu->max_size - token_options) /1024) * 1024;
2972 } else {
2973 block.m = (lg_xmit->offset + chunk) < lg_xmit->data_info->length;
2974 }
2975 if (!coap_update_option(out_pdu, lg_xmit->option,
2977 sizeof(buf),
2978 (block.num << 4) |
2979 (block.m << 3) |
2980 block.aszx),
2981 buf)) {
2982 goto internal_issue;
2983 }
2984 if (!(lg_xmit->offset + chunk < lg_xmit->data_info->length)) {
2985 /* Last block - keep in cache for 4 * ACK_TIMOUT */
2986 coap_ticks(&lg_xmit->last_all_sent);
2987 }
2988 if (lg_xmit->b.b2.maxage_expire) {
2989 coap_tick_t now;
2990 coap_time_t rem;
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 coap_ticks(&now);
2997 rem = coap_ticks_to_rt(now);
2998 if (lg_xmit->b.b2.maxage_expire > rem) {
2999 rem = lg_xmit->b.b2.maxage_expire - rem;
3000 } else {
3001 rem = 0;
3002 /* Entry needs to be expired */
3003 coap_ticks(&lg_xmit->last_all_sent);
3004 }
3007 sizeof(buf),
3008 rem),
3009 buf)) {
3010 goto internal_issue;
3011 }
3012 }
3013
3014 if (!coap_add_block_b_data(out_pdu,
3015 lg_xmit->data_info->length,
3016 lg_xmit->data_info->data,
3017 &block)) {
3018 goto internal_issue;
3019 }
3020 if (i + 1 < request_cnt) {
3021 coap_ticks(&lg_xmit->last_sent);
3022 coap_send_internal(session, out_pdu, NULL);
3023 }
3024 }
3025 coap_ticks(&lg_xmit->last_payload);
3026 goto skip_app_handler;
3027#if COAP_Q_BLOCK_SUPPORT
3028call_app_handler:
3029 coap_free_type(COAP_STRING, out_blocks);
3030 return 0;
3031#endif /* COAP_Q_BLOCK_SUPPORT */
3032
3033internal_issue:
3034 response->code = COAP_RESPONSE_CODE(500);
3035 error_phrase = coap_response_phrase(response->code);
3036 coap_add_data(response, strlen(error_phrase),
3037 (const uint8_t *)error_phrase);
3038 /* Keep in cache for 4 * ACK_TIMOUT incase of retry */
3039 if (lg_xmit)
3040 coap_ticks(&lg_xmit->last_all_sent);
3041
3042skip_app_handler:
3043 coap_free_type(COAP_STRING, out_blocks);
3044 return 1;
3045}
3046#endif /* COAP_SERVER_SUPPORT */
3047
3048static int
3049update_received_blocks(coap_rblock_t *rec_blocks, uint32_t block_num, uint32_t block_m) {
3050 uint32_t i;
3051
3052 if (rec_blocks->total_blocks && block_num + 1 > rec_blocks->total_blocks) {
3053 /* received block number greater than Block No defined when More bit unset */
3054 return 0;
3055 }
3056
3057 /* Reset as there is activity */
3058 rec_blocks->retry = 0;
3059
3060 for (i = 0; i < rec_blocks->used; i++) {
3061 if (block_num >= rec_blocks->range[i].begin &&
3062 block_num <= rec_blocks->range[i].end)
3063 break;
3064
3065 if (block_num < rec_blocks->range[i].begin) {
3066 if (block_num + 1 == rec_blocks->range[i].begin) {
3067 rec_blocks->range[i].begin = block_num;
3068 } else {
3069 /* Need to insert a new range */
3070 if (rec_blocks->used == COAP_RBLOCK_CNT -1)
3071 /* Too many losses */
3072 return 0;
3073 memmove(&rec_blocks->range[i+1], &rec_blocks->range[i],
3074 (rec_blocks->used - i) * sizeof(rec_blocks->range[0]));
3075 rec_blocks->range[i].begin = rec_blocks->range[i].end = block_num;
3076 rec_blocks->used++;
3077 }
3078 break;
3079 }
3080 if (block_num == rec_blocks->range[i].end + 1) {
3081 rec_blocks->range[i].end = block_num;
3082 if (i + 1 < rec_blocks->used) {
3083 if (rec_blocks->range[i+1].begin == block_num + 1) {
3084 /* Merge the 2 ranges */
3085 rec_blocks->range[i].end = rec_blocks->range[i+1].end;
3086 if (i+2 < rec_blocks->used) {
3087 memmove(&rec_blocks->range[i+1], &rec_blocks->range[i+2],
3088 (rec_blocks->used - (i+2)) * sizeof(rec_blocks->range[0]));
3089 }
3090 rec_blocks->used--;
3091 }
3092 }
3093 break;
3094 }
3095 }
3096 if (i == rec_blocks->used) {
3097 if (rec_blocks->used == COAP_RBLOCK_CNT -1)
3098 /* Too many losses */
3099 return 0;
3100 rec_blocks->range[i].begin = rec_blocks->range[i].end = block_num;
3101 rec_blocks->used++;
3102 }
3103 if (!block_m)
3104 rec_blocks->total_blocks = block_num + 1;
3105
3106 coap_ticks(&rec_blocks->last_seen);
3107 return 1;
3108}
3109
3110#if COAP_SERVER_SUPPORT
3111/*
3112 * Need to check if this is a large PUT / POST etc. using multiple blocks
3113 *
3114 * Server receiving PUT/POST etc. of a large amount of data (Block1)
3115 *
3116 * Return: 0 Call application handler
3117 * 1 Do not call application handler - just send the built response
3118 */
3119int
3120coap_handle_request_put_block(coap_context_t *context,
3121 coap_session_t *session,
3122 coap_pdu_t *pdu,
3123 coap_pdu_t *response,
3124 coap_resource_t *resource,
3125 coap_string_t *uri_path,
3126 coap_opt_t *observe,
3127 int *added_block,
3128 coap_lg_srcv_t **pfree_lg_srcv) {
3129 size_t length = 0;
3130 const uint8_t *data = NULL;
3131 size_t offset = 0;
3132 size_t total = 0;
3133 coap_block_b_t block;
3134 coap_opt_iterator_t opt_iter;
3135 uint16_t block_option = 0;
3136 coap_lg_srcv_t *lg_srcv;
3137 coap_opt_t *size_opt;
3138 coap_opt_t *fmt_opt;
3139 uint16_t fmt;
3140 coap_opt_t *rtag_opt;
3141 size_t rtag_length;
3142 const uint8_t *rtag;
3143 uint32_t max_block_szx;
3144 int update_data;
3145 unsigned int saved_num;
3146 size_t saved_offset;
3147
3148 *added_block = 0;
3149 *pfree_lg_srcv = NULL;
3150 coap_get_data_large(pdu, &length, &data, &offset, &total);
3151 pdu->body_offset = 0;
3152 pdu->body_total = length;
3153
3154 if (coap_get_block_b(session, pdu, COAP_OPTION_BLOCK1, &block)) {
3155 block_option = COAP_OPTION_BLOCK1;
3156#if COAP_Q_BLOCK_SUPPORT
3157 if (coap_check_option(pdu, COAP_OPTION_Q_BLOCK1, &opt_iter)) {
3158 /* Cannot handle Q-Block1 as well */
3159 coap_add_data(response, sizeof("Block1 + Q-Block1 together")-1,
3160 (const uint8_t *)"Block1 + Q-Block1 together");
3161 response->code = COAP_RESPONSE_CODE(402);
3162 goto skip_app_handler;
3163 }
3164#endif /* COAP_Q_BLOCK_SUPPORT */
3165 }
3166#if COAP_Q_BLOCK_SUPPORT
3167 else if (coap_get_block_b(session, pdu, COAP_OPTION_Q_BLOCK1, &block)) {
3168 block_option = COAP_OPTION_Q_BLOCK1;
3169 set_block_mode_has_q(session->block_mode);
3170 }
3171#endif /* COAP_Q_BLOCK_SUPPORT */
3172 if (!block_option ||
3173 (block_option == COAP_OPTION_BLOCK1 && block.num == 0 && block.m == 0)) {
3174 /* Not blocked, or a single block */
3175 if (context->max_body_size && total > context->max_body_size) {
3176 uint8_t buf[4];
3177
3178 coap_update_option(response,
3180 coap_encode_var_safe((uint8_t *)buf, sizeof(buf),
3181 context->max_body_size),
3182 (uint8_t *)buf);
3183 response->code = COAP_RESPONSE_CODE(413);
3184 coap_log_warn("Unable to handle data size %" PRIuS " (max %" PRIu32 ")\n", total,
3185 context->max_body_size);
3186 goto skip_app_handler;
3187 }
3188 goto call_app_handler;
3189 }
3190
3191 size_opt = coap_check_option(pdu,
3193 &opt_iter);
3194 fmt_opt = coap_check_option(pdu,
3196 &opt_iter);
3197 fmt = fmt_opt ? coap_decode_var_bytes(coap_opt_value(fmt_opt),
3198 coap_opt_length(fmt_opt)) :
3200 rtag_opt = coap_check_option(pdu,
3202 &opt_iter);
3203 rtag_length = rtag_opt ? coap_opt_length(rtag_opt) : 0;
3204 rtag = rtag_opt ? coap_opt_value(rtag_opt) : NULL;
3205
3206 if (length > block.chunk_size) {
3207 coap_log_debug("block: Oversized packet - reduced to %"PRIu32" from %" PRIuS "\n",
3208 block.chunk_size, length);
3209 length = block.chunk_size;
3210 } else if (!block.bert && block.m && length != block.chunk_size) {
3211 coap_log_info("block: Undersized packet chunk %"PRIu32" got %" PRIuS "\n",
3212 block.chunk_size, length);
3213 response->code = COAP_RESPONSE_CODE(400);
3214 goto skip_app_handler;
3215 }
3216 total = size_opt ? coap_decode_var_bytes(coap_opt_value(size_opt),
3217 coap_opt_length(size_opt)) : 0;
3218 if (total) {
3219 uint32_t max_body;
3220
3221 max_block_szx = COAP_BLOCK_MAX_SIZE_GET(session->block_mode);
3222 if (max_block_szx == 0 || max_block_szx > block.szx) {
3223 max_block_szx = block.szx;
3224 }
3225 max_body = ((1UL << 20) * (1 << (max_block_szx + 4)));
3226 if (max_body > MAX_BLK_LEN)
3227 max_body = MAX_BLK_LEN;
3228 if ((context->max_body_size && total > context->max_body_size) ||
3229 (total > max_body)) {
3230 /* Suggested body size larger than allowed */
3231 char buf[32];
3232 uint32_t max_body_size = context->max_body_size;
3233
3234 if (max_body_size == 0 || max_body < max_body_size) {
3235 max_body_size = max_body;
3236 }
3237 coap_update_option(response,
3239 coap_encode_var_safe((uint8_t *)buf, sizeof(buf),
3240 max_body_size),
3241 (uint8_t *)buf);
3242 snprintf(buf, sizeof(buf), "Max body size %" PRIu32, max_body_size);
3243 coap_add_data(response, strlen(buf), (uint8_t *)buf);
3244 response->code = COAP_RESPONSE_CODE(413);
3245 coap_log_warn("Unable to handle body size %" PRIuS " (max %" PRIu32 ")\n", total, max_body_size);
3246 goto skip_app_handler;
3247 }
3248 }
3249 offset = block.num << (block.szx + 4);
3250
3251 if (!(session->block_mode &
3252#if COAP_Q_BLOCK_SUPPORT
3254#else /* COAP_Q_BLOCK_SUPPORT */
3256#endif /* COAP_Q_BLOCK_SUPPORT */
3257 && !block.bert) {
3258 uint8_t buf[4];
3259
3260 /* Ask for the next block */
3261 coap_insert_option(response, block_option,
3262 coap_encode_var_safe(buf, sizeof(buf),
3263 (block.num << 4) |
3264 (block.m << 3) |
3265 block.aszx),
3266 buf);
3267 /* Not re-assembling or checking for receipt order */
3268 pdu->body_data = data;
3269 pdu->body_length = length;
3270 pdu->body_offset = offset;
3271 if (total < (length + offset + (block.m ? 1 : 0)))
3272 total = length + offset + (block.m ? 1 : 0);
3273 pdu->body_total = total;
3274 *added_block = block.m;
3275 /* The application is responsible for returning the correct 2.01/2.04/2.31 etc. */
3276 goto call_app_handler;
3277 }
3278
3279 /*
3280 * locate the lg_srcv
3281 */
3282 LL_FOREACH(session->lg_srcv, lg_srcv) {
3283 if (rtag_opt || lg_srcv->rtag_set == 1) {
3284 if (!(rtag_opt && lg_srcv->rtag_set == 1))
3285 continue;
3286 if (lg_srcv->rtag_length != rtag_length ||
3287 memcmp(lg_srcv->rtag, rtag, rtag_length) != 0)
3288 continue;
3289 }
3290 if (resource == lg_srcv->resource) {
3291 break;
3292 }
3293 if ((lg_srcv->resource == context->unknown_resource ||
3294 resource == context->proxy_uri_resource) &&
3295 coap_string_equal(uri_path, lg_srcv->uri_path))
3296 break;
3297 }
3298
3299 if (!lg_srcv && block.num != 0 && session->block_mode & COAP_BLOCK_NOT_RANDOM_BLOCK1) {
3300 coap_add_data(response, sizeof("Missing block 0")-1,
3301 (const uint8_t *)"Missing block 0");
3302 response->code = COAP_RESPONSE_CODE(408);
3303 goto skip_app_handler;
3304 }
3305
3306 if (!lg_srcv) {
3307 /* Allocate lg_srcv to use for tracking */
3308 lg_srcv = coap_malloc_type(COAP_LG_SRCV, sizeof(coap_lg_srcv_t));
3309 if (lg_srcv == NULL) {
3310 coap_add_data(response, sizeof("Memory issue")-1,
3311 (const uint8_t *)"Memory issue");
3312 response->code = COAP_RESPONSE_CODE(500);
3313 goto skip_app_handler;
3314 }
3315 coap_log_debug("** %s: lg_srcv %p initialized\n",
3316 coap_session_str(session), (void *)lg_srcv);
3317 memset(lg_srcv, 0, sizeof(coap_lg_srcv_t));
3318 lg_srcv->resource = resource;
3319 if (resource == context->unknown_resource ||
3320 resource == context->proxy_uri_resource)
3321 lg_srcv->uri_path = coap_new_str_const(uri_path->s, uri_path->length);
3322 lg_srcv->content_format = fmt;
3323 lg_srcv->total_len = total;
3324 max_block_szx = COAP_BLOCK_MAX_SIZE_GET(session->block_mode);
3325 if (!block.bert && block.num == 0 && max_block_szx != 0 &&
3326 max_block_szx < block.szx) {
3327 lg_srcv->szx = max_block_szx;
3328 } else {
3329 lg_srcv->szx = block.szx;
3330 }
3331 lg_srcv->block_option = block_option;
3332 if (observe) {
3333 lg_srcv->observe_length = min(coap_opt_length(observe), 3);
3334 memcpy(lg_srcv->observe, coap_opt_value(observe), lg_srcv->observe_length);
3335 lg_srcv->observe_set = 1;
3336 }
3337 if (rtag_opt) {
3338 lg_srcv->rtag_length = coap_opt_length(rtag_opt);
3339 memcpy(lg_srcv->rtag, coap_opt_value(rtag_opt), lg_srcv->rtag_length);
3340 lg_srcv->rtag_set = 1;
3341 }
3342 lg_srcv->body_data = NULL;
3343 LL_PREPEND(session->lg_srcv, lg_srcv);
3344 }
3345 coap_ticks(&lg_srcv->last_used);
3346
3347 if (block_option == COAP_OPTION_BLOCK1 &&
3349 !check_if_next_block(&lg_srcv->rec_blocks, block.num)) {
3350 coap_add_data(response, sizeof("Missing interim block")-1,
3351 (const uint8_t *)"Missing interim block");
3352 response->code = COAP_RESPONSE_CODE(408);
3353 goto skip_app_handler;
3354 }
3355
3356 if (fmt != lg_srcv->content_format) {
3357 coap_add_data(response, sizeof("Content-Format mismatch")-1,
3358 (const uint8_t *)"Content-Format mismatch");
3359 response->code = COAP_RESPONSE_CODE(408);
3360 goto free_lg_srcv;
3361 }
3362
3363#if COAP_Q_BLOCK_SUPPORT
3364 if (block_option == COAP_OPTION_Q_BLOCK1) {
3365 if (total != lg_srcv->total_len) {
3366 coap_add_data(response, sizeof("Size1 mismatch")-1,
3367 (const uint8_t *)"Size1 mismatch");
3368 response->code = COAP_RESPONSE_CODE(408);
3369 goto free_lg_srcv;
3370 }
3371 coap_delete_bin_const(lg_srcv->last_token);
3372 lg_srcv->last_token = coap_new_bin_const(pdu->actual_token.s,
3373 pdu->actual_token.length);
3374 }
3375#endif /* COAP_Q_BLOCK_SUPPORT */
3376
3377 lg_srcv->last_mid = pdu->mid;
3378 lg_srcv->last_type = pdu->type;
3379
3380 update_data = 0;
3381 saved_num = block.num;
3382 saved_offset = offset;
3383
3384 while (offset < saved_offset + length) {
3385 if (!check_if_received_block(&lg_srcv->rec_blocks, block.num)) {
3386 /* Update list of blocks received */
3387 if (!update_received_blocks(&lg_srcv->rec_blocks, block.num, block.m)) {
3389 coap_add_data(response, sizeof("Too many missing blocks")-1,
3390 (const uint8_t *)"Too many missing blocks");
3391 response->code = COAP_RESPONSE_CODE(408);
3392 goto free_lg_srcv;
3393 }
3394 update_data = 1;
3395 }
3396 block.num++;
3397 offset = block.num << (block.szx + 4);
3398 }
3399 block.num--;
3400
3401 if (update_data) {
3402 /* Update saved data */
3403#if COAP_Q_BLOCK_SUPPORT
3404 lg_srcv->rec_blocks.processing_payload_set =
3405 block.num / COAP_MAX_PAYLOADS(session);
3406#endif /* COAP_Q_BLOCK_SUPPORT */
3407 if (lg_srcv->total_len < saved_offset + length) {
3408 lg_srcv->total_len = saved_offset + length;
3409 }
3410
3411#define USE_BLOCK_DATA_HANDLER (context && context->block_data_cb && \
3412 !resource->is_proxy_uri && \
3413 !resource->is_reverse_proxy && \
3414 ((session->block_mode & COAP_SINGLE_BLOCK_OR_Q) || block.bert) && \
3415 (resource->flags & COAP_RESOURCE_USE_BLOCK_DATA_HANDLER))
3416
3417 if (USE_BLOCK_DATA_HANDLER) {
3418 coap_response_t resp;
3419
3421 context->block_data_cb(session, pdu, resource,
3422 &lg_srcv->body_data,
3423 length, data, saved_offset,
3424 lg_srcv->total_len));
3425 if (resp != COAP_RESPONSE_OK) {
3426 response->code = COAP_RESPONSE_CODE(500);
3427 goto skip_app_handler;
3428 }
3429 } else {
3430 lg_srcv->body_data = coap_block_build_body_lkd(lg_srcv->body_data, length, data,
3431 saved_offset, lg_srcv->total_len);
3432 if (!lg_srcv->body_data) {
3433 coap_add_data(response, sizeof("Memory issue")-1,
3434 (const uint8_t *)"Memory issue");
3435 response->code = COAP_RESPONSE_CODE(500);
3436 goto skip_app_handler;
3437 }
3438 }
3439 }
3440
3441 if (block.m ||
3442 !check_all_blocks_in(&lg_srcv->rec_blocks)) {
3443 /* Not all the payloads of the body have arrived */
3444 if (block.m) {
3445 uint8_t buf[4];
3446
3447#if COAP_Q_BLOCK_SUPPORT
3448 if (block_option == COAP_OPTION_Q_BLOCK1) {
3449 if (check_all_blocks_in(&lg_srcv->rec_blocks)) {
3450 goto give_app_data;
3451 }
3452 if (lg_srcv->rec_blocks.used == 1 &&
3453 (lg_srcv->rec_blocks.range[0].end % COAP_MAX_PAYLOADS(session)) + 1
3454 == COAP_MAX_PAYLOADS(session)) {
3455 if (block.num != lg_srcv->rec_blocks.range[0].end) {
3456 /* Blocks could arrive in wrong order */
3457 block.num = lg_srcv->rec_blocks.range[0].end;
3458 goto skip_app_handler;
3459 }
3460 } else {
3461 /* The remote end will be sending the next one unless this
3462 is a MAX_PAYLOADS and all previous have been received */
3463 goto skip_app_handler;
3464 }
3465 if (COAP_PROTO_RELIABLE(session->proto) ||
3466 pdu->type != COAP_MESSAGE_NON)
3467 goto skip_app_handler;
3468 }
3469#endif /* COAP_Q_BLOCK_SUPPORT */
3470
3471 /* Check to see if block size is getting forced down */
3472 max_block_szx = COAP_BLOCK_MAX_SIZE_GET(session->block_mode);
3473 if (!block.bert && saved_num == 0 && max_block_szx != 0 &&
3474 max_block_szx < block.aszx) {
3475 block.aszx = max_block_szx;
3476 }
3477
3478 /*
3479 * If the last block has been seen, packets are coming in in
3480 * random order. If all blocks are now in, then need to send
3481 * complete payload to application and acknowledge this current
3482 * block.
3483 */
3484 if ((total == 0 && block.m) || !check_all_blocks_in(&lg_srcv->rec_blocks)) {
3485 /* Ask for the next block */
3486 coap_insert_option(response, block_option,
3487 coap_encode_var_safe(buf, sizeof(buf),
3488 (saved_num << 4) |
3489 (block.m << 3) |
3490 block.aszx),
3491 buf);
3492 response->code = COAP_RESPONSE_CODE(231);
3493 } else {
3494 /* Need to separately respond to this request */
3495 coap_pdu_t *tmp_pdu = coap_pdu_duplicate_lkd(response,
3496 session,
3497 response->actual_token.length,
3498 response->actual_token.s,
3499 NULL);
3500 if (tmp_pdu) {
3501 tmp_pdu->code = COAP_RESPONSE_CODE(231);
3502 if (coap_send_internal(session, tmp_pdu, NULL) == COAP_INVALID_MID) {
3503 /* lg_srcv may have got deleted */
3504 coap_lg_srcv_t *sg;
3505
3506 LL_FOREACH(session->lg_srcv, sg) {
3507 if (lg_srcv == sg) {
3508 /* Still there */
3509 break;
3510 }
3511 }
3512 if (!sg)
3513 goto skip_app_handler;
3514 }
3515 }
3516 if (lg_srcv->last_token) {
3517 coap_update_token(response, lg_srcv->last_token->length, lg_srcv->last_token->s);
3518 coap_update_token(pdu, lg_srcv->last_token->length, lg_srcv->last_token->s);
3519 }
3520 /* Pass the assembled pdu and body to the application */
3521 goto give_app_data;
3522 }
3523 } else {
3524 /* block.m Block More option not set. Some outstanding blocks */
3525#if COAP_Q_BLOCK_SUPPORT
3526 if (block_option != COAP_OPTION_Q_BLOCK1) {
3527#endif /* COAP_Q_BLOCK_SUPPORT */
3528 /* Last chunk - but not all in */
3529 coap_ticks(&lg_srcv->last_used);
3530 lg_srcv->no_more_seen = 1;
3531 coap_delete_bin_const(lg_srcv->last_token);
3532 lg_srcv->last_token = coap_new_bin_const(pdu->actual_token.s,
3533 pdu->actual_token.length);
3534
3535 /*
3536 * Need to just ACK (no response code) to handle client's NSTART.
3537 * When final missing block comes in, we will pass all the data
3538 * for processing so a 2.01, 2.04 etc. code can be generated
3539 * and responded to as a separate response "RFC7252 5.2.2. Separate"
3540 * If missing block(s) do not come in, then will generate a 4.08
3541 * when lg_srcv times out.
3542 * Fall through to skip_app_handler.
3543 */
3544#if COAP_Q_BLOCK_SUPPORT
3545 }
3546#endif /* COAP_Q_BLOCK_SUPPORT */
3547 }
3548 goto skip_app_handler;
3549 }
3550
3551 /*
3552 * Entire payload received.
3553 * Remove the Block1 option as passing all of the data to
3554 * application layer. Add back in observe option if appropriate.
3555 * Adjust all other information.
3556 */
3557give_app_data:
3558 if (lg_srcv->observe_set) {
3560 lg_srcv->observe_length, lg_srcv->observe);
3561 }
3562 coap_remove_option(pdu, block_option);
3563 if (lg_srcv->body_data) {
3564 pdu->body_data = lg_srcv->body_data->s;
3565 pdu->body_length = lg_srcv->total_len;
3566 } else {
3567 pdu->body_data = NULL;
3568 pdu->body_length = 0;
3569 }
3570 pdu->body_offset = 0;
3571 pdu->body_total = lg_srcv->total_len;
3572 if (USE_BLOCK_DATA_HANDLER) {
3573 /* Data has already been provided - do not duplicate */
3574 if (pdu->data) {
3575 pdu->used_size = pdu->data - pdu->token - 1;
3576 pdu->data = NULL;
3577 }
3578 }
3579 coap_log_debug("Server app version of updated PDU\n");
3581 lg_srcv->dont_timeout = 1;
3582 *pfree_lg_srcv = lg_srcv;
3583
3584call_app_handler:
3585 return 0;
3586
3587free_lg_srcv:
3588 LL_DELETE(session->lg_srcv, lg_srcv);
3589 coap_block_delete_lg_srcv(session, lg_srcv);
3590
3591skip_app_handler:
3592 return 1;
3593}
3594#endif /* COAP_SERVER_SUPPORT */
3595
3596#if COAP_CLIENT_SUPPORT
3597#if COAP_Q_BLOCK_SUPPORT
3598static uint32_t
3599derive_cbor_value(const uint8_t **bp, size_t rem_len) {
3600 uint32_t value = **bp & 0x1f;
3601 (*bp)++;
3602 if (value < 24) {
3603 return value;
3604 } else if (value == 24) {
3605 if (rem_len < 2)
3606 return (uint32_t)-1;
3607 value = **bp;
3608 (*bp)++;
3609 return value;
3610 } else if (value == 25) {
3611 if (rem_len < 3)
3612 return (uint32_t)-1;
3613 value = **bp << 8;
3614 (*bp)++;
3615 value |= **bp;
3616 (*bp)++;
3617 return value;
3618 }
3619 if (rem_len < 4)
3620 return (uint32_t)-1;
3621 value = (uint32_t)(**bp) << 24;
3622 (*bp)++;
3623 value |= **bp << 16;
3624 (*bp)++;
3625 value |= **bp << 8;
3626 (*bp)++;
3627 value |= **bp;
3628 (*bp)++;
3629 return value;
3630}
3631#endif /* COAP_Q_BLOCK_SUPPORT */
3632
3633static int
3634check_freshness(coap_session_t *session, coap_pdu_t *rcvd, coap_pdu_t *sent,
3635 coap_lg_xmit_t *lg_xmit, coap_lg_crcv_t *lg_crcv) {
3636 /* Check for Echo option for freshness */
3637 coap_opt_iterator_t opt_iter;
3638 coap_opt_t *opt = coap_check_option(rcvd, COAP_OPTION_ECHO, &opt_iter);
3639
3640 if (opt) {
3641 if (sent || lg_xmit || lg_crcv) {
3642 /* Need to retransmit original request with Echo option added */
3643 coap_pdu_t *echo_pdu;
3644 coap_mid_t mid;
3645 const uint8_t *data;
3646 size_t data_len;
3647 int have_data = 0;
3648 uint8_t ltoken[8];
3649 size_t ltoken_len;
3650 uint64_t token;
3651
3652 if (sent) {
3653 if (coap_get_data(sent, &data_len, &data))
3654 have_data = 1;
3655 } else if (lg_xmit) {
3656 sent = lg_xmit->sent_pdu;
3657 if (lg_xmit->data_info->length) {
3658 size_t blk_size = (size_t)1 << (lg_xmit->blk_size + 4);
3659 size_t offset = (lg_xmit->last_block + 1) * blk_size;
3660 have_data = 1;
3661 data = &lg_xmit->data_info->data[offset];
3662 data_len = (lg_xmit->data_info->length - offset) > blk_size ? blk_size :
3663 lg_xmit->data_info->length - offset;
3664 }
3665 } else { /* lg_crcv */
3666 sent = lg_crcv->sent_pdu;
3667 if (coap_get_data(sent, &data_len, &data))
3668 have_data = 1;
3669 }
3670 if (lg_xmit) {
3671 token = STATE_TOKEN_FULL(lg_xmit->b.b1.state_token,
3672 ++lg_xmit->b.b1.count);
3673 } else {
3674 token = STATE_TOKEN_FULL(lg_crcv->state_token,
3675 ++lg_crcv->retry_counter);
3676 }
3677 ltoken_len = coap_encode_var_safe8(ltoken, sizeof(token), token);
3678 echo_pdu = coap_pdu_duplicate_lkd(sent, session, ltoken_len, ltoken, NULL);
3679 if (!echo_pdu)
3680 return 0;
3681 if (!coap_insert_option(echo_pdu, COAP_OPTION_ECHO,
3682 coap_opt_length(opt), coap_opt_value(opt)))
3683 goto not_sent;
3684 if (have_data) {
3685 coap_add_data(echo_pdu, data_len, data);
3686 }
3687 /* Need to track Observe token change if Observe */
3688 track_fetch_observe(echo_pdu, lg_crcv, 0, &echo_pdu->actual_token);
3689#if COAP_OSCORE_SUPPORT
3690 if (session->oscore_encryption &&
3691 (opt = coap_check_option(echo_pdu, COAP_OPTION_OBSERVE, &opt_iter)) &&
3693 /* Need to update the base PDU's Token for closing down Observe */
3694 if (lg_xmit) {
3695 lg_xmit->b.b1.state_token = token;
3696 } else {
3697 lg_crcv->state_token = token;
3698 }
3699 }
3700#endif /* COAP_OSCORE_SUPPORT */
3701 mid = coap_send_internal(session, echo_pdu, NULL);
3702 if (mid == COAP_INVALID_MID)
3703 goto not_sent;
3704 return 1;
3705 } else {
3706 /* Need to save Echo option value to add to next reansmission */
3707not_sent:
3708 coap_delete_bin_const(session->echo);
3709 session->echo = coap_new_bin_const(coap_opt_value(opt),
3710 coap_opt_length(opt));
3711 }
3712 }
3713 return 0;
3714}
3715
3716static void
3717track_echo(coap_session_t *session, coap_pdu_t *rcvd) {
3718 coap_opt_iterator_t opt_iter;
3719 coap_opt_t *opt = coap_check_option(rcvd, COAP_OPTION_ECHO, &opt_iter);
3720
3721 if (opt) {
3722 coap_delete_bin_const(session->echo);
3723 session->echo = coap_new_bin_const(coap_opt_value(opt),
3724 coap_opt_length(opt));
3725 }
3726}
3727
3728/*
3729 * Need to see if this is a response to a large body request transfer. If so,
3730 * need to initiate the request containing the next block and not trouble the
3731 * application. Note that Token must unique per request/response.
3732 *
3733 * Client receives large data acknowledgement from server (Block1)
3734 *
3735 * This is set up using coap_add_data_large_request_lkd()
3736 *
3737 * Client is using GET etc.
3738 *
3739 * Return: 0 Call application handler
3740 * 1 Do not call application handler - just send the built response
3741 */
3742int
3743coap_handle_response_send_block(coap_session_t *session, coap_pdu_t *sent,
3744 coap_pdu_t *rcvd) {
3745 coap_lg_xmit_t *lg_xmit;
3746 coap_lg_crcv_t *lg_crcv = NULL;
3747
3748 lg_xmit = coap_find_lg_xmit(session, rcvd);
3749 if (lg_xmit) {
3750 /* lg_xmit found */
3751 size_t chunk = (size_t)1 << (lg_xmit->blk_size + 4);
3752 coap_block_b_t block;
3753
3754 lg_crcv = coap_find_lg_crcv(session, rcvd);
3755 if (lg_crcv)
3756 coap_ticks(&lg_crcv->last_used);
3757
3758 if (COAP_RESPONSE_CLASS(rcvd->code) == 2 &&
3759 coap_get_block_b(session, rcvd, lg_xmit->option, &block)) {
3760
3761 if (block.bert) {
3762 coap_log_debug("found Block option, block is BERT, block nr. %u (%" PRIuS ")\n",
3763 block.num, lg_xmit->b.b1.bert_size);
3764 } else {
3765 coap_log_debug("found Block option, block size is %u, block nr. %u\n",
3766 1 << (block.szx + 4), block.num);
3767 }
3768 if (block.szx != lg_xmit->blk_size) {
3769 if (block.szx > lg_xmit->blk_size) {
3770 coap_log_info("ignoring request to increase Block size, "
3771 "(%u > %u)\n",
3772 1 << (block.szx + 4), 1 << (lg_xmit->blk_size + 4));
3773 } else if ((lg_xmit->offset + chunk) % ((size_t)1 << (block.szx + 4)) == 0) {
3774 /*
3775 * Recompute the block number of the previous packet given the
3776 * new block size
3777 */
3778 block.num = (uint32_t)(((lg_xmit->offset + chunk) >> (block.szx + 4)) - 1);
3779 lg_xmit->blk_size = block.szx;
3780 chunk = (size_t)1 << (lg_xmit->blk_size + 4);
3781 lg_xmit->offset = block.num * chunk;
3782 coap_log_debug("new Block size is %u, block number %u completed\n",
3783 1 << (block.szx + 4), block.num);
3784 block.bert = 0;
3785 block.aszx = block.szx;
3786 } else {
3787 coap_log_debug("ignoring request to increase Block size, "
3788 "next block is not aligned on requested block size boundary. "
3789 "(%" PRIuS " x %u mod %u = %" PRIuS " != 0)\n",
3790 lg_xmit->offset/chunk + 1, (1 << (lg_xmit->blk_size + 4)),
3791 (1 << (block.szx + 4)),
3792 (lg_xmit->offset + chunk) % ((size_t)1 << (block.szx + 4)));
3793 }
3794 }
3795 track_echo(session, rcvd);
3796 if (lg_xmit->last_block == (int)block.num &&
3797 lg_xmit->option != COAP_OPTION_Q_BLOCK1) {
3798 /*
3799 * Duplicate Block1 ACK
3800 *
3801 * RFCs not clear here, but on a lossy connection, there could
3802 * be multiple Block1 ACKs, causing the client to retransmit the
3803 * same block multiple times, or the server retransmitting the
3804 * same ACK.
3805 *
3806 * Once a block has been ACKd, there is no need to retransmit it.
3807 */
3808 return 1;
3809 }
3810 if (block.bert)
3811 block.num += (unsigned int)(lg_xmit->b.b1.bert_size / 1024 - 1);
3812 lg_xmit->last_block = block.num;
3813 lg_xmit->offset = (block.num + 1) * chunk;
3814 if (lg_xmit->offset < lg_xmit->data_info->length) {
3815 /* Build the next PDU request based off the skeletal PDU */
3816 uint8_t buf[8];
3817 coap_pdu_t *pdu;
3818 uint64_t token = STATE_TOKEN_FULL(lg_xmit->b.b1.state_token, ++lg_xmit->b.b1.count);
3819 size_t len = coap_encode_var_safe8(buf, sizeof(token), token);
3820
3821 if (lg_xmit->sent_pdu->code == COAP_REQUEST_CODE_FETCH) {
3822 /* Need to handle Observe for large FETCH */
3823 if (lg_crcv) {
3824 if (coap_binary_equal(lg_xmit->b.b1.app_token, lg_crcv->app_token)) {
3825 coap_bin_const_t *new_token;
3826 coap_bin_const_t ctoken = { len, buf };
3827
3828 /* Need to save/restore Observe Token for large FETCH */
3829 new_token = track_fetch_observe(lg_xmit->sent_pdu, lg_crcv, block.num + 1,
3830 &ctoken);
3831 if (new_token) {
3832 assert(len <= sizeof(buf));
3833 len = new_token->length;
3834 memcpy(buf, new_token->s, len);
3835 }
3836 }
3837 }
3838 }
3839 pdu = coap_pdu_duplicate_lkd(lg_xmit->sent_pdu, session, len, buf, NULL);
3840 if (!pdu)
3841 goto fail_body;
3842
3843 /*
3844 * If initial transmit was multicast, that would have been NON.
3845 * Make subsequent traffic CON for reliability.
3846 */
3847 if (session->sock.flags & COAP_SOCKET_MULTICAST) {
3848 pdu->type = COAP_MESSAGE_CON;
3849 }
3850
3851 block.num++;
3852 if (block.bert) {
3853 size_t token_options = pdu->data ? (size_t)(pdu->data - pdu->token) :
3854 pdu->used_size;
3855 block.m = (lg_xmit->data_info->length - lg_xmit->offset) >
3856 ((pdu->max_size - token_options) /1024) * 1024;
3857 } else {
3858 block.m = (lg_xmit->offset + chunk) < lg_xmit->data_info->length;
3859 }
3860 coap_update_option(pdu, lg_xmit->option,
3861 coap_encode_var_safe(buf, sizeof(buf),
3862 (block.num << 4) |
3863 (block.m << 3) |
3864 block.aszx),
3865 buf);
3866
3867 if (lg_xmit->data_info->get_func) {
3868#if COAP_CONSTRAINED_STACK
3869 /* Protected by global_lock if needed */
3870 static uint8_t l_data[1024];
3871#else /* ! COAP_CONSTRAINED_STACK */
3872 uint8_t l_data[1024];
3873#endif /* ! COAP_CONSTRAINED_STACK */
3874 size_t l_length;
3875
3876 assert(chunk <= 1024);
3877 if (lg_xmit->data_info->get_func(session, chunk,
3878 block.num * chunk, l_data, &l_length,
3879 lg_xmit->data_info->app_ptr)) {
3880 if (!coap_add_data(pdu, l_length, l_data)) {
3881 goto fail_body;
3882 }
3883 }
3884 } else {
3885 if (!coap_add_block_b_data(pdu,
3886 lg_xmit->data_info->length,
3887 lg_xmit->data_info->data,
3888 &block))
3889 goto fail_body;
3890 }
3891 lg_xmit->b.b1.bert_size = block.chunk_size;
3892 coap_ticks(&lg_xmit->last_sent);
3893#if COAP_Q_BLOCK_SUPPORT
3894 if (lg_xmit->option == COAP_OPTION_Q_BLOCK1 &&
3895 pdu->type == COAP_MESSAGE_NON) {
3896 if (coap_send_q_block1(session, block, pdu,
3897 COAP_SEND_INC_PDU) == COAP_INVALID_MID)
3898 goto fail_body;
3899 return 1;
3900 } else if (coap_send_internal(session, pdu, NULL) == COAP_INVALID_MID)
3901 goto fail_body;
3902#else /* ! COAP_Q_BLOCK_SUPPORT */
3903 if (coap_send_internal(session, pdu, NULL) == COAP_INVALID_MID)
3904 goto fail_body;
3905#endif /* ! COAP_Q_BLOCK_SUPPORT */
3906 return 1;
3907 }
3908 } else if (COAP_RESPONSE_CLASS(rcvd->code) == 2) {
3909 /*
3910 * Not a block response asking for the next block.
3911 * Could be an Observe response overlapping with block FETCH doing
3912 * Observe cancellation.
3913 */
3914 coap_opt_iterator_t opt_iter;
3915 coap_opt_t *obs_opt;
3916 int observe_action = -1;
3917
3918 if (lg_xmit->sent_pdu->code != COAP_REQUEST_CODE_FETCH) {
3919 goto lg_xmit_finished;
3920 }
3921 obs_opt = coap_check_option(lg_xmit->sent_pdu,
3923 &opt_iter);
3924 if (obs_opt) {
3925 observe_action = coap_decode_var_bytes(coap_opt_value(obs_opt),
3926 coap_opt_length(obs_opt));
3927 }
3928 if (observe_action != COAP_OBSERVE_CANCEL) {
3929 goto lg_xmit_finished;
3930 }
3931 obs_opt = coap_check_option(rcvd,
3933 &opt_iter);
3934 if (obs_opt) {
3935 return 0;
3936 }
3937 goto lg_xmit_finished;
3938 } else if (rcvd->code == COAP_RESPONSE_CODE(401)) {
3939 if (check_freshness(session, rcvd, sent, lg_xmit, NULL))
3940 return 1;
3941#if COAP_Q_BLOCK_SUPPORT
3942 } else if (rcvd->code == COAP_RESPONSE_CODE(402)) {
3943 /* Q-Block1 or Q-Block2 not present in p - duplicate error ? */
3944 if (coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK2, &block) ||
3945 coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK1, &block))
3946 return 1;
3947 } else if (rcvd->code == COAP_RESPONSE_CODE(408) &&
3948 lg_xmit->option == COAP_OPTION_Q_BLOCK1) {
3949 size_t length;
3950 const uint8_t *data;
3951 coap_opt_iterator_t opt_iter;
3952 coap_opt_t *fmt_opt = coap_check_option(rcvd,
3954 &opt_iter);
3955 uint16_t fmt = fmt_opt ?
3957 coap_opt_length(fmt_opt)) :
3959
3961 goto fail_body;
3962
3963 if (COAP_PROTO_RELIABLE(session->proto) ||
3964 rcvd->type != COAP_MESSAGE_NON) {
3965 coap_log_debug("Unexpected 4.08 - protocol violation - ignore\n");
3966 return 1;
3967 }
3968
3969 if (coap_get_data(rcvd, &length, &data)) {
3970 /* Need to decode CBOR to work out what blocks to re-send */
3971 const uint8_t *bp = data;
3972 uint32_t i;
3973 uint8_t buf[8];
3974 coap_pdu_t *pdu;
3975 uint64_t token;
3976 uint8_t ltoken[8];
3977 size_t ltoken_length;
3978
3979 for (i = 0; (bp < data + length) &&
3980 i < COAP_MAX_PAYLOADS(session); i++) {
3981 if ((*bp & 0xc0) != 0x00) /* uint(value) */
3982 goto fail_cbor;
3983 block.num = derive_cbor_value(&bp, data + length - bp);
3984 coap_log_debug("Q-Block1: Missing block %d\n", block.num);
3985 if (block.num > (1 << 20) -1)
3986 goto fail_cbor;
3987 block.m = (block.num + 1) * chunk < lg_xmit->data_info->length;
3988 block.szx = lg_xmit->blk_size;
3989
3990 /* Build the next PDU request based off the skeletal PDU */
3991 token = STATE_TOKEN_FULL(lg_xmit->b.b1.state_token,++lg_xmit->b.b1.count);
3992 ltoken_length = coap_encode_var_safe8(ltoken, sizeof(token), token);
3993 pdu = coap_pdu_duplicate_lkd(lg_xmit->sent_pdu, session, ltoken_length,
3994 ltoken, NULL);
3995 if (!pdu)
3996 goto fail_body;
3997
3998 coap_update_option(pdu, lg_xmit->option,
3999 coap_encode_var_safe(buf, sizeof(buf),
4000 (block.num << 4) |
4001 (block.m << 3) |
4002 block.szx),
4003 buf);
4004
4005 if (!coap_add_block(pdu,
4006 lg_xmit->data_info->length,
4007 lg_xmit->data_info->data,
4008 block.num,
4009 block.szx))
4010 goto fail_body;
4011 if (coap_send_internal(session, pdu, NULL) == COAP_INVALID_MID)
4012 goto fail_body;
4013 }
4014 return 1;
4015 }
4016fail_cbor:
4017 coap_log_info("Invalid application/missing-blocks+cbor-seq\n");
4018#endif /* COAP_Q_BLOCK_SUPPORT */
4019 }
4020 goto lg_xmit_finished;
4021 }
4022 return 0;
4023
4024fail_body:
4026 /* There has been an internal error of some sort */
4027 rcvd->code = COAP_RESPONSE_CODE(500);
4028lg_xmit_finished:
4029 if (lg_crcv) {
4030 if (STATE_TOKEN_BASE(lg_xmit->b.b1.state_token) ==
4031 STATE_TOKEN_BASE(lg_crcv->state_token)) {
4032 /* In case of observe */
4033 lg_crcv->state_token = lg_xmit->b.b1.state_token;
4034 lg_crcv->retry_counter = lg_xmit->b.b1.count;
4035 }
4036 }
4037 if (!lg_crcv) {
4038 /* need to put back original token into rcvd */
4039 if (lg_xmit->b.b1.app_token)
4040 coap_update_token(rcvd, lg_xmit->b.b1.app_token->length,
4041 lg_xmit->b.b1.app_token->s);
4042 coap_log_debug("Client app version of updated PDU (1)\n");
4044 } else {
4045 lg_crcv->sent_pdu->lg_xmit = 0;
4046 }
4047
4048 if (sent) {
4049 /* need to put back original token into sent */
4050 if (lg_xmit->b.b1.app_token)
4051 coap_update_token(sent, lg_xmit->b.b1.app_token->length,
4052 lg_xmit->b.b1.app_token->s);
4053 if (sent->lg_xmit)
4054 coap_remove_option(sent, sent->lg_xmit->option);
4055 sent->lg_xmit = NULL;
4056 }
4057 LL_DELETE(session->lg_xmit, lg_xmit);
4058 coap_block_delete_lg_xmit(session, lg_xmit);
4059 return 0;
4060}
4061#endif /* COAP_CLIENT_SUPPORT */
4062
4063void
4065 coap_block_data_handler_t block_data_handler) {
4066 context->block_data_cb = block_data_handler;
4067}
4068
4070coap_block_build_body(coap_binary_t *body_data, size_t length,
4071 const uint8_t *data, size_t offset, size_t total) {
4072 coap_binary_t *ret;
4073
4074 coap_lock_lock(return NULL);
4075 ret = coap_block_build_body_lkd(body_data, length, data, offset, total);
4077 return ret;
4078}
4079/*
4080 * Re-assemble payloads into a body
4081 */
4084 const uint8_t *data, size_t offset, size_t total) {
4085 if (data == NULL)
4086 return NULL;
4087 if (body_data == NULL && total) {
4088 body_data = coap_new_binary(total);
4089 }
4090 if (body_data == NULL)
4091 return NULL;
4092
4093 /* Check no overflow (including a 8 byte small headroom) */
4094 if (SIZE_MAX - length < 8 || offset > SIZE_MAX - length - 8) {
4095 coap_delete_binary(body_data);
4096 return NULL;
4097 }
4098
4099 /* Update saved data */
4100 if (offset + length <= total && body_data->length >= total) {
4101 memcpy(&body_data->s[offset], data, length);
4102 } else {
4103 /*
4104 * total may be inaccurate as per
4105 * https://rfc-editor.org/rfc/rfc7959#section-4
4106 * o In a request carrying a Block1 Option, to indicate the current
4107 * estimate the client has of the total size of the resource
4108 * representation, measured in bytes ("size indication").
4109 * o In a response carrying a Block2 Option, to indicate the current
4110 * estimate the server has of the total size of the resource
4111 * representation, measured in bytes ("size indication").
4112 */
4113 coap_binary_t *new = coap_resize_binary(body_data, offset + length);
4114
4115 if (new) {
4116 body_data = new;
4117 memcpy(&body_data->s[offset], data, length);
4118 } else {
4119 coap_delete_binary(body_data);
4120 return NULL;
4121 }
4122 }
4123 return body_data;
4124}
4125
4126#if COAP_CLIENT_SUPPORT
4127/*
4128 * Need to see if this is a large body response to a request. If so,
4129 * need to initiate the request for the next block and not trouble the
4130 * application. Note that Token must be unique per request/response.
4131 *
4132 * This is set up using coap_send()
4133 * Client receives large data from server ((Q-)Block2)
4134 *
4135 * Return: 0 Call application handler
4136 * 1 Do not call application handler - just sent the next request
4137 */
4138int
4139coap_handle_response_get_block(coap_context_t *context,
4140 coap_session_t *session,
4141 coap_pdu_t *sent,
4142 coap_pdu_t *rcvd,
4143 coap_recurse_t recursive) {
4144 coap_lg_crcv_t *lg_crcv;
4145 coap_block_b_t block;
4146#if COAP_Q_BLOCK_SUPPORT
4147 coap_block_b_t qblock;
4148#endif /* COAP_Q_BLOCK_SUPPORT */
4149 int have_block = 0;
4150 uint16_t block_opt = 0;
4151 size_t offset;
4152 int ack_rst_sent = 0;
4153
4155 memset(&block, 0, sizeof(block));
4156#if COAP_Q_BLOCK_SUPPORT
4157 memset(&qblock, 0, sizeof(qblock));
4158#endif /* COAP_Q_BLOCK_SUPPORT */
4159 lg_crcv = coap_find_lg_crcv(session, rcvd);
4160 if (lg_crcv) {
4161 size_t chunk = 0;
4162 uint8_t buf[8];
4163 coap_opt_iterator_t opt_iter;
4164
4165 if (COAP_RESPONSE_CLASS(rcvd->code) == 2) {
4166 size_t length;
4167 const uint8_t *data;
4169 &opt_iter);
4170 size_t size2 = size_opt ?
4172 coap_opt_length(size_opt)) : 0;
4173
4174 /* length and data are cleared on error */
4175 (void)coap_get_data(rcvd, &length, &data);
4176 rcvd->body_offset = 0;
4177 rcvd->body_total = length;
4178 if (coap_get_block_b(session, rcvd, COAP_OPTION_BLOCK2, &block)) {
4179 have_block = 1;
4180 block_opt = COAP_OPTION_BLOCK2;
4181 }
4182#if COAP_Q_BLOCK_SUPPORT
4183 if (coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK2, &qblock)) {
4184 if (have_block) {
4185 coap_log_warn("Both Block1 and Q-Block1 not supported in a response\n");
4186 }
4187 have_block = 1;
4188 block_opt = COAP_OPTION_Q_BLOCK2;
4189 block = qblock;
4190 /* server indicating that it supports Q_BLOCK */
4191 if (!(session->block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
4192 set_block_mode_has_q(session->block_mode);
4193 }
4194 }
4195#endif /* COAP_Q_BLOCK_SUPPORT */
4196 track_echo(session, rcvd);
4197 if (have_block && (block.m || length)) {
4198 coap_opt_t *fmt_opt = coap_check_option(rcvd,
4200 &opt_iter);
4201 uint16_t fmt = fmt_opt ?
4203 coap_opt_length(fmt_opt)) :
4205 coap_opt_t *etag_opt = coap_check_option(rcvd,
4207 &opt_iter);
4208 size_t saved_offset;
4209 int updated_block;
4210
4211 if (length > block.chunk_size) {
4212 coap_log_debug("block: Oversized packet - reduced to %"PRIu32" from %" PRIuS "\n",
4213 block.chunk_size, length);
4214 length = block.chunk_size;
4215 }
4216 if (block.m && length != block.chunk_size) {
4217 coap_log_warn("block: Undersized packet - expected %"PRIu32", got %" PRIuS "\n",
4218 block.chunk_size, length);
4219 /* Unclear how to properly handle this */
4220 rcvd->code = COAP_RESPONSE_CODE(402);
4221 goto expire_lg_crcv;
4222 }
4223 /* Possibility that Size2 not sent, or is too small */
4224 chunk = (size_t)1 << (block.szx + 4);
4225 offset = block.num * chunk;
4226 if (size2 < (offset + length)) {
4227 if (block.m)
4228 size2 = offset + length + 1;
4229 else
4230 size2 = offset + length;
4231 }
4232 saved_offset = offset;
4233
4234 if (lg_crcv->initial) {
4235#if COAP_Q_BLOCK_SUPPORT
4236reinit:
4237#endif /* COAP_Q_BLOCK_SUPPORT */
4238 lg_crcv->initial = 0;
4239 if (lg_crcv->body_data) {
4240 coap_free_type(COAP_STRING, lg_crcv->body_data);
4241 lg_crcv->body_data = NULL;
4242 }
4243 if (etag_opt) {
4244 lg_crcv->etag_length = coap_opt_length(etag_opt);
4245 memcpy(lg_crcv->etag, coap_opt_value(etag_opt), lg_crcv->etag_length);
4246 lg_crcv->etag_set = 1;
4247 } else {
4248 lg_crcv->etag_set = 0;
4249 }
4250 lg_crcv->total_len = size2;
4251 lg_crcv->content_format = fmt;
4252 lg_crcv->szx = block.szx;
4253 lg_crcv->block_option = block_opt;
4254 lg_crcv->last_type = rcvd->type;
4255 lg_crcv->rec_blocks.used = 0;
4256 lg_crcv->rec_blocks.total_blocks = 0;
4257#if COAP_Q_BLOCK_SUPPORT
4258 lg_crcv->rec_blocks.processing_payload_set = 0;
4259#endif /* COAP_Q_BLOCK_SUPPORT */
4260 }
4261 if (lg_crcv->total_len < size2)
4262 lg_crcv->total_len = size2;
4263
4264 /* Check whether we can handle this size */
4265 uint32_t max_body;
4266 uint8_t max_block_szx;
4267
4268 max_block_szx = COAP_BLOCK_MAX_SIZE_GET(session->block_mode);
4269 if (max_block_szx == 0 || max_block_szx > block.szx) {
4270 max_block_szx = block.szx;
4271 }
4272 max_body = ((1UL << 20) * (1 << (max_block_szx + 4)));
4273 if (max_body > MAX_BLK_LEN)
4274 max_body = MAX_BLK_LEN;
4275 if ((context->max_body_size && size2 > context->max_body_size) ||
4276 (size2 > max_body)) {
4277 uint32_t max_body_size = context->max_body_size;
4278
4279 if (max_body_size == 0 || max_body < max_body_size) {
4280 max_body_size = max_body;
4281 }
4282 coap_log_warn("Unable to handle body size %" PRIuS " (max %" PRIu32 ")\n", size2, max_body_size);
4283 /* Try to hint to the server thare is an issue */
4284 coap_send_rst_lkd(session, rcvd);
4286 return 1;
4287 }
4288
4289 if (etag_opt) {
4290 if (!full_match(coap_opt_value(etag_opt),
4291 coap_opt_length(etag_opt),
4292 lg_crcv->etag, lg_crcv->etag_length)) {
4293 /* body of data has changed - need to restart request */
4294 coap_pdu_t *pdu;
4295 uint64_t token = STATE_TOKEN_FULL(lg_crcv->state_token,
4296 ++lg_crcv->retry_counter);
4297 size_t len = coap_encode_var_safe8(buf, sizeof(token), token);
4298 coap_opt_filter_t drop_options;
4299
4300#if COAP_Q_BLOCK_SUPPORT
4301 if (block_opt == COAP_OPTION_Q_BLOCK2)
4302 goto reinit;
4303#endif /* COAP_Q_BLOCK_SUPPORT */
4304
4305 coap_log_warn("Data body updated during receipt - new request started\n");
4306 if (!(session->block_mode & COAP_BLOCK_SINGLE_BODY))
4308
4309 lg_crcv->initial = 1;
4310 coap_free_type(COAP_STRING, lg_crcv->body_data);
4311 lg_crcv->body_data = NULL;
4312
4313 coap_session_new_token(session, &len, buf);
4314 memset(&drop_options, 0, sizeof(coap_opt_filter_t));
4317 pdu = coap_pdu_duplicate_lkd(lg_crcv->sent_pdu, session, len, buf, &drop_options);
4318 if (!pdu)
4319 goto fail_resp;
4320
4321 coap_update_option(pdu, block_opt,
4322 coap_encode_var_safe(buf, sizeof(buf),
4323 (0 << 4) | (0 << 3) | block.aszx),
4324 buf);
4325
4326 if (coap_send_internal(session, pdu, NULL) == COAP_INVALID_MID)
4327 goto fail_resp;
4328
4329 goto skip_app_handler;
4330 }
4331 } else if (lg_crcv->etag_set) {
4332 /* Cannot handle this change in ETag to not being there */
4333 coap_log_warn("Not all blocks have ETag option\n");
4334 goto fail_resp;
4335 }
4336
4337 if (fmt != lg_crcv->content_format) {
4338 coap_log_warn("Content-Format option mismatch\n");
4339 goto fail_resp;
4340 }
4341#if COAP_Q_BLOCK_SUPPORT
4342 if (block_opt == COAP_OPTION_Q_BLOCK2 && size2 != lg_crcv->total_len) {
4343 coap_log_warn("Size2 option mismatch\n");
4344 goto fail_resp;
4345 }
4346#endif /* COAP_Q_BLOCK_SUPPORT */
4347 if (block.num == 0) {
4348 coap_opt_t *obs_opt = coap_check_option(rcvd,
4350 &opt_iter);
4351 if (obs_opt) {
4352 lg_crcv->observe_length = min(coap_opt_length(obs_opt), 3);
4353 memcpy(lg_crcv->observe, coap_opt_value(obs_opt), lg_crcv->observe_length);
4354 lg_crcv->observe_set = 1;
4355 } else {
4356 lg_crcv->observe_set = 0;
4357 }
4358 }
4359 updated_block = 0;
4360 while (offset < saved_offset + length) {
4361 if (!check_if_received_block(&lg_crcv->rec_blocks, block.num)) {
4362#if COAP_Q_BLOCK_SUPPORT
4363 uint32_t this_payload_set = block.num / COAP_MAX_PAYLOADS(session);
4364#endif /* COAP_Q_BLOCK_SUPPORT */
4365
4366 coap_log_debug("found Block option, block size is %u, block nr. %u\n",
4367 1 << (block.szx + 4), block.num);
4368#if COAP_Q_BLOCK_SUPPORT
4369 if (block_opt == COAP_OPTION_Q_BLOCK2 && lg_crcv->rec_blocks.used &&
4370 this_payload_set > lg_crcv->rec_blocks.processing_payload_set &&
4371 this_payload_set != lg_crcv->rec_blocks.latest_payload_set) {
4372 coap_request_missing_q_block2(session, lg_crcv);
4373 }
4374 lg_crcv->rec_blocks.latest_payload_set = this_payload_set;
4375#endif /* COAP_Q_BLOCK_SUPPORT */
4376 /* Update list of blocks received */
4377 if (!update_received_blocks(&lg_crcv->rec_blocks, block.num, block.m)) {
4379 goto fail_resp;
4380 }
4381 updated_block = 1;
4382 }
4383 block.num++;
4384 offset = block.num << (block.szx + 4);
4385 if (!block.bert && block_opt != COAP_OPTION_Q_BLOCK2)
4386 break;
4387 }
4388 block.num--;
4389 /* Only process if not duplicate block */
4390 if (updated_block) {
4391 void *body_free;
4392
4393 /* Update last_used to prevent premature timeout during long transfers */
4394 coap_ticks(&lg_crcv->last_used);
4395
4396 if ((session->block_mode & COAP_SINGLE_BLOCK_OR_Q) || block.bert) {
4397 if (size2 < saved_offset + length) {
4398 size2 = saved_offset + length;
4399 }
4400 if (context && context->block_data_cb) {
4401 coap_response_t resp;
4402
4404 context->block_data_cb(session, rcvd, 0,
4405 &lg_crcv->body_data,
4406 length, data,
4407 saved_offset, size2));
4408 if (resp != COAP_RESPONSE_OK) {
4409 goto fail_resp;
4410 }
4411 } else {
4412 lg_crcv->body_data = coap_block_build_body_lkd(lg_crcv->body_data, length, data,
4413 saved_offset, size2);
4414 if (lg_crcv->body_data == NULL) {
4415 goto fail_resp;
4416 }
4417 }
4418 }
4419 if (block.m || !check_all_blocks_in(&lg_crcv->rec_blocks)) {
4420 /* Not all the payloads of the body have arrived */
4421 size_t len;
4422 coap_pdu_t *pdu;
4423 uint64_t token;
4424 coap_opt_filter_t drop_options;
4425
4426 if (block.m) {
4427#if COAP_Q_BLOCK_SUPPORT
4428 if (block_opt == COAP_OPTION_Q_BLOCK2) {
4429 /* Blocks could arrive in wrong order */
4430 if (check_all_blocks_in(&lg_crcv->rec_blocks)) {
4431 goto give_to_app;
4432 }
4433 if (check_all_blocks_in_for_payload_set(session,
4434 &lg_crcv->rec_blocks)) {
4435 block.num = lg_crcv->rec_blocks.range[0].end;
4436 /* Now requesting next payload */
4437 lg_crcv->rec_blocks.processing_payload_set =
4438 block.num / COAP_MAX_PAYLOADS(session) + 1;
4439 if (check_any_blocks_next_payload_set(session,
4440 &lg_crcv->rec_blocks)) {
4441 /* Need to ask for them individually */
4442 coap_request_missing_q_block2(session, lg_crcv);
4443 goto skip_app_handler;
4444 }
4445 } else {
4446 /* The remote end will be sending the next one unless this
4447 is a MAX_PAYLOADS and all previous have been received */
4448 goto skip_app_handler;
4449 }
4450 if (COAP_PROTO_RELIABLE(session->proto) ||
4451 rcvd->type != COAP_MESSAGE_NON)
4452 goto skip_app_handler;
4453
4454 } else
4455#endif /* COAP_Q_BLOCK_SUPPORT */
4456 block.m = 0;
4457
4458 /* Ask for the next block */
4459 token = STATE_TOKEN_FULL(lg_crcv->state_token, ++lg_crcv->retry_counter);
4460 len = coap_encode_var_safe8(buf, sizeof(token), token);
4461 memset(&drop_options, 0, sizeof(coap_opt_filter_t));
4463 pdu = coap_pdu_duplicate_lkd(lg_crcv->sent_pdu, session, len, buf, &drop_options);
4464 if (!pdu)
4465 goto fail_resp;
4466
4467 if (rcvd->type == COAP_MESSAGE_NON)
4468 pdu->type = COAP_MESSAGE_NON; /* Server is using NON */
4469
4470 /* Only sent with the first block */
4472
4473 coap_update_option(pdu, block_opt,
4474 coap_encode_var_safe(buf, sizeof(buf),
4475 ((block.num + 1) << 4) |
4476 (block.m << 3) | block.aszx),
4477 buf);
4478
4480 (void)coap_get_data(lg_crcv->sent_pdu, &length, &data);
4481 coap_add_data_large_internal(session, NULL, pdu, NULL, NULL, -1, 0, length, data, NULL, NULL, NULL,
4482 0, 0);
4483 }
4484 if (coap_send_internal(session, pdu, NULL) == COAP_INVALID_MID)
4485 /* Session could now be disconnected, so no lg_crcv */
4486 goto skip_app_handler;
4487 }
4488 if ((session->block_mode & COAP_SINGLE_BLOCK_OR_Q) || block.bert)
4489 goto skip_app_handler;
4490
4491 /* need to put back original token into rcvd */
4492 coap_update_token(rcvd, lg_crcv->app_token->length, lg_crcv->app_token->s);
4493 rcvd->body_offset = saved_offset;
4494#if COAP_Q_BLOCK_SUPPORT
4495 rcvd->body_total = block_opt == COAP_OPTION_Q_BLOCK2 ?
4496 lg_crcv->total_len : size2;
4497#else /* ! COAP_Q_BLOCK_SUPPORT */
4498 rcvd->body_total = size2;
4499#endif /* ! COAP_Q_BLOCK_SUPPORT */
4500 coap_log_debug("Client app version of updated PDU (2)\n");
4502
4503 if (sent) {
4504 /* need to put back original token into sent */
4505 if (lg_crcv->app_token)
4506 coap_update_token(sent, lg_crcv->app_token->length,
4507 lg_crcv->app_token->s);
4508 coap_remove_option(sent, lg_crcv->block_option);
4509 }
4510 goto call_app_handler;
4511 }
4512#if COAP_Q_BLOCK_SUPPORT
4513give_to_app:
4514#endif /* COAP_Q_BLOCK_SUPPORT */
4515 if ((session->block_mode & COAP_SINGLE_BLOCK_OR_Q) || block.bert) {
4516 /* Pretend that there is no block */
4517 coap_remove_option(rcvd, block_opt);
4518 if (lg_crcv->observe_set) {
4520 lg_crcv->observe_length, lg_crcv->observe);
4521 }
4522 rcvd->body_data = lg_crcv->body_data ? lg_crcv->body_data->s : NULL;
4523#if COAP_Q_BLOCK_SUPPORT
4524 if (context && context->block_data_cb) {
4525 /* Data has already been provided - do not duplicate */
4526 if (rcvd->data) {
4527 rcvd->used_size = rcvd->data - rcvd->token - 1;
4528 rcvd->data = NULL;
4529 }
4530 }
4531 rcvd->body_length = block_opt == COAP_OPTION_Q_BLOCK2 ?
4532 lg_crcv->total_len : saved_offset + length;
4533#else /* ! COAP_Q_BLOCK_SUPPORT */
4534 rcvd->body_length = saved_offset + length;
4535#endif /* ! COAP_Q_BLOCK_SUPPORT */
4536 rcvd->body_offset = 0;
4537 rcvd->body_total = rcvd->body_length;
4538 } else {
4539 rcvd->body_offset = saved_offset;
4540#if COAP_Q_BLOCK_SUPPORT
4541 rcvd->body_total = block_opt == COAP_OPTION_Q_BLOCK2 ?
4542 lg_crcv->total_len : size2;
4543#else /* ! COAP_Q_BLOCK_SUPPORT */
4544 rcvd->body_total = size2;
4545#endif /* ! COAP_Q_BLOCK_SUPPORT */
4546 }
4547 /* need to put back original token into rcvd */
4548 if (!coap_binary_equal(&rcvd->actual_token, lg_crcv->app_token)) {
4549 coap_update_token(rcvd, lg_crcv->app_token->length, lg_crcv->app_token->s);
4550 coap_log_debug("Client app version of updated PDU (3)\n");
4552 }
4553 if (sent) {
4554 /* need to put back original token into sent */
4555 if (lg_crcv->app_token)
4556 coap_update_token(sent, lg_crcv->app_token->length,
4557 lg_crcv->app_token->s);
4558 coap_remove_option(sent, lg_crcv->block_option);
4559 }
4560 body_free = lg_crcv->body_data;
4561 lg_crcv->body_data = NULL;
4562 coap_call_response_handler(session, sent, rcvd, body_free);
4563
4564 ack_rst_sent = 1;
4565 if (lg_crcv->observe_set == 0) {
4566 /* Expire this entry */
4567 LL_DELETE(session->lg_crcv, lg_crcv);
4568 coap_block_delete_lg_crcv(session, lg_crcv);
4569 goto skip_app_handler;
4570 }
4571 /* Set up for the next data body as observing */
4572 lg_crcv->initial = 1;
4573 }
4574 coap_ticks(&lg_crcv->last_used);
4575 goto skip_app_handler;
4576 } else {
4577 coap_opt_t *obs_opt = coap_check_option(rcvd,
4579 &opt_iter);
4580 if (context->max_body_size && length > context->max_body_size) {
4581 coap_log_warn("Unable to handle body size %" PRIuS " (max %" PRIu32 ")\n", length,
4582 context->max_body_size);
4583 /* Try to hint to the server thare is an issue */
4584 coap_send_rst_lkd(session, rcvd);
4586 return 1;
4587 }
4588 if (obs_opt) {
4589 lg_crcv->observe_length = min(coap_opt_length(obs_opt), 3);
4590 memcpy(lg_crcv->observe, coap_opt_value(obs_opt), lg_crcv->observe_length);
4591 lg_crcv->observe_set = 1;
4592 } else {
4593 lg_crcv->observe_set = 0;
4594 if (!coap_binary_equal(&rcvd->actual_token, lg_crcv->app_token)) {
4595 /* need to put back original token into rcvd */
4596 coap_update_token(rcvd, lg_crcv->app_token->length, lg_crcv->app_token->s);
4598 coap_log_debug("PDU presented to app.\n");
4600 }
4601 /* Expire this entry */
4602 goto expire_lg_crcv;
4603 }
4604 }
4605 coap_ticks(&lg_crcv->last_used);
4606 } else if (rcvd->code == COAP_RESPONSE_CODE(401)) {
4607#if COAP_OSCORE_SUPPORT
4608 if (check_freshness(session, rcvd,
4609 (session->oscore_encryption == 0) ? sent : NULL,
4610 NULL, lg_crcv))
4611#else /* !COAP_OSCORE_SUPPORT */
4612 if (check_freshness(session, rcvd, sent, NULL, lg_crcv))
4613#endif /* !COAP_OSCORE_SUPPORT */
4614 goto skip_app_handler;
4615 goto expire_lg_crcv;
4616 } else {
4617 /* Not 2.xx or 4.01 - assume it is a failure of some sort */
4618 goto expire_lg_crcv;
4619 }
4620 if (!block.m && !lg_crcv->observe_set) {
4621fail_resp:
4622 /* lg_crcv no longer required - cache it for 1 sec */
4623 coap_ticks(&lg_crcv->last_used);
4624 lg_crcv->last_used = lg_crcv->last_used - COAP_MAX_TRANSMIT_WAIT_TICKS(session) +
4626 }
4627 /* need to put back original token into rcvd */
4628 if (!coap_binary_equal(&rcvd->actual_token, lg_crcv->app_token)) {
4629 coap_update_token(rcvd, lg_crcv->app_token->length, lg_crcv->app_token->s);
4630 coap_log_debug("Client app version of updated PDU (4)\n");
4632 }
4633 }
4634
4635 /* Check if receiving a block response and if blocks can be set up */
4636 if (recursive == COAP_RECURSE_OK && !lg_crcv) {
4637 if (!sent) {
4638 if (coap_get_block_b(session, rcvd, COAP_OPTION_BLOCK2, &block)
4639#if COAP_Q_BLOCK_SUPPORT
4640 ||
4641 coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK2, &block)
4642#endif /* COAP_Q_BLOCK_SUPPORT */
4643 ) {
4644 coap_log_debug("** %s: large body receive internal issue\n",
4645 coap_session_str(session));
4646 goto skip_app_handler;
4647 }
4648 } else if (COAP_RESPONSE_CLASS(rcvd->code) == 2) {
4649 const uint8_t *data;
4650 size_t length;
4651
4652 if (coap_get_block_b(session, rcvd, COAP_OPTION_BLOCK2, &block)) {
4653#if COAP_Q_BLOCK_SUPPORT
4654 if (session->block_mode & COAP_BLOCK_PROBE_Q_BLOCK) {
4655 set_block_mode_drop_q(session->block_mode);
4656 coap_log_debug("Q-Block support disabled\n");
4657 }
4658#endif /* COAP_Q_BLOCK_SUPPORT */
4659 have_block = 1;
4660 if (block.num != 0) {
4661 /* Assume random access and just give the single response to app */
4662 size_t chunk = (size_t)1 << (block.szx + 4);
4663
4664 coap_get_data(rcvd, &length, &data);
4665 rcvd->body_offset = block.num*chunk;
4666 rcvd->body_total = block.num*chunk + length + (block.m ? 1 : 0);
4667 goto call_app_handler;
4668 }
4669 }
4670#if COAP_Q_BLOCK_SUPPORT
4671 else if (coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK2, &block)) {
4672 have_block = 1;
4673 /* server indicating that it supports Q_BLOCK2 */
4674 if (!(session->block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
4675 set_block_mode_has_q(session->block_mode);
4676 }
4677 }
4678#endif /* COAP_Q_BLOCK_SUPPORT */
4679 if (have_block) {
4680 lg_crcv = coap_block_new_lg_crcv(session, sent, NULL);
4681
4682 if (lg_crcv) {
4683 LL_PREPEND(session->lg_crcv, lg_crcv);
4684 return coap_handle_response_get_block(context, session, sent, rcvd,
4686 }
4687 }
4688 coap_get_data(rcvd, &length, &data);
4689 if (context->max_body_size && length > context->max_body_size) {
4690 coap_log_warn("Unable to handle body size %" PRIuS " (max %" PRIu32 ")\n", length,
4691 context->max_body_size);
4692 /* Try to hint to the server thare is an issue */
4693 coap_send_rst_lkd(session, rcvd);
4695 return 1;
4696 }
4697 track_echo(session, rcvd);
4698 } else if (rcvd->code == COAP_RESPONSE_CODE(401)) {
4699 lg_crcv = coap_block_new_lg_crcv(session, sent, NULL);
4700
4701 if (lg_crcv) {
4702 LL_PREPEND(session->lg_crcv, lg_crcv);
4703 return coap_handle_response_get_block(context, session, sent, rcvd,
4705 }
4706 }
4707 }
4708 return 0;
4709
4710expire_lg_crcv:
4711 /* need to put back original token into rcvd */
4712 if (!coap_binary_equal(&rcvd->actual_token, lg_crcv->app_token)) {
4713 coap_update_token(rcvd, lg_crcv->app_token->length, lg_crcv->app_token->s);
4714 coap_log_debug("Client app version of updated PDU (5)\n");
4716 }
4717
4718 if (sent) {
4719 /* need to put back original token into sent */
4720 if (lg_crcv->app_token)
4721 coap_update_token(sent, lg_crcv->app_token->length,
4722 lg_crcv->app_token->s);
4723 coap_remove_option(sent, lg_crcv->block_option);
4724 }
4725 /* Expire this entry */
4726 LL_DELETE(session->lg_crcv, lg_crcv);
4727 coap_block_delete_lg_crcv(session, lg_crcv);
4728
4729call_app_handler:
4730 return 0;
4731
4732skip_app_handler:
4733 if (!ack_rst_sent)
4734 coap_send_ack_lkd(session, rcvd);
4735 return 1;
4736}
4737#endif /* COAP_CLIENT_SUPPORT */
4738
4739#if COAP_SERVER_SUPPORT
4740/* Check if lg_xmit generated and update PDU code if so */
4741void
4743 const coap_pdu_t *request,
4744 coap_pdu_t *response, const coap_resource_t *resource,
4745 const coap_string_t *query) {
4746 coap_lg_xmit_t *lg_xmit;
4747
4748 if (response->code == 0)
4749 return;
4750 lg_xmit = coap_find_lg_xmit_response(session, request, resource, query);
4751 if (lg_xmit && lg_xmit->sent_pdu && lg_xmit->sent_pdu->code == 0) {
4752 lg_xmit->sent_pdu->code = response->code;
4753 return;
4754 }
4755}
4756#endif /* COAP_SERVER_SUPPORT */
4757
4758#if COAP_CLIENT_SUPPORT
4759void
4761 uint64_t token_match =
4763 pdu->actual_token.length));
4764 coap_lg_xmit_t *lg_xmit;
4765 coap_lg_crcv_t *lg_crcv;
4766
4767 if (session->lg_crcv) {
4768 LL_FOREACH(session->lg_crcv, lg_crcv) {
4769 if (coap_binary_equal(&pdu->actual_token, lg_crcv->app_token))
4770 return;
4771 if (token_match == STATE_TOKEN_BASE(lg_crcv->state_token)) {
4772 coap_update_token(pdu, lg_crcv->app_token->length,
4773 lg_crcv->app_token->s);
4774 coap_log_debug("Client app version of updated PDU (6)\n");
4776 return;
4777 }
4778 }
4779 }
4780 if (COAP_PDU_IS_REQUEST(pdu) && session->lg_xmit) {
4781 LL_FOREACH(session->lg_xmit, lg_xmit) {
4782 if (coap_binary_equal(&pdu->actual_token, lg_xmit->b.b1.app_token))
4783 return;
4784 if (token_match == STATE_TOKEN_BASE(lg_xmit->b.b1.state_token)) {
4785 coap_update_token(pdu, lg_xmit->b.b1.app_token->length,
4786 lg_xmit->b.b1.app_token->s);
4787 coap_log_debug("Client app version of updated PDU (7)\n");
4789 return;
4790 }
4791 }
4792 }
4793}
4794#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:5189
#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:1091
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:1106
#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...
void coap_block_delete_lg_xmit(coap_session_t *session, coap_lg_xmit_t *lg_xmit)
#define STATE_TOKEN_FULL(t, r)
#define COAP_SINGLE_BLOCK_OR_Q
#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_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:5030
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:1363
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:1883
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:129
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
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