libcoap 4.3.1rc1
block.c
Go to the documentation of this file.
1/* block.c -- block transfer
2 *
3 * Copyright (C) 2010--2012,2015-2022 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
16#include "coap3/coap_internal.h"
17
18#ifndef min
19#define min(a,b) ((a) < (b) ? (a) : (b))
20#endif
21
22#define STATE_TOKEN_BASE(t) ((t) & 0xffffffffffffULL)
23#define STATE_TOKEN_RETRY(t) ((uint64_t)(t) >> 48)
24#define STATE_TOKEN_FULL(t,r) (STATE_TOKEN_BASE(t) + ((uint64_t)(r) << 48))
25
26unsigned int
27coap_opt_block_num(const coap_opt_t *block_opt) {
28 unsigned int num = 0;
29 uint16_t len;
30
31 len = coap_opt_length(block_opt);
32
33 if (len == 0) {
34 return 0;
35 }
36
37 if (len > 1) {
39 coap_opt_length(block_opt) - 1);
40 }
41
42 return (num << 4) | ((*COAP_OPT_BLOCK_LAST(block_opt) & 0xF0) >> 4);
43}
44
45int
46coap_get_block_b(const coap_session_t *session, const coap_pdu_t *pdu,
47 coap_option_num_t number, coap_block_b_t *block) {
48 coap_opt_iterator_t opt_iter;
49 coap_opt_t *option;
50
51 assert(block);
52 memset(block, 0, sizeof(coap_block_b_t));
53
54 if (pdu && (option = coap_check_option(pdu, number, &opt_iter)) != NULL) {
55 unsigned int num;
56
57 block->aszx = block->szx = COAP_OPT_BLOCK_SZX(option);
58 if (block->szx == 7) {
59 size_t length;
60 const uint8_t *data;
61
62 if (session == NULL || COAP_PROTO_NOT_RELIABLE(session->proto) ||
63 !(session->csm_bert_rem_support && session->csm_bert_loc_support))
64 /* No BERT support */
65 return 0;
66
67 block->szx = 6; /* BERT is 1024 block chunks */
68 block->bert = 1;
69 if (coap_get_data(pdu, &length, &data))
70 block->chunk_size = (uint32_t)length;
71 else
72 block->chunk_size = 0;
73 } else {
74 block->chunk_size = (size_t)1 << (block->szx + 4);
75 }
76 block->defined = 1;
77 if (COAP_OPT_BLOCK_MORE(option))
78 block->m = 1;
79
80 /* The block number is at most 20 bits, so values above 2^20 - 1
81 * are illegal. */
82 num = coap_opt_block_num(option);
83 if (num > 0xFFFFF) {
84 return 0;
85 }
86 block->num = num;
87 return 1;
88 }
89
90 return 0;
91}
92
93int
95 coap_block_t *block) {
96 coap_block_b_t block_b;
97
98 assert(block);
99 memset(block, 0, sizeof(coap_block_t));
100
101 if (coap_get_block_b(NULL, pdu, number, &block_b)) {
102 block->num = block_b.num;
103 block->m = block_b.m;
104 block->szx = block_b.szx;
105 return 1;
106 }
107 return 0;
108}
109
110static int
112 unsigned int num,
113 unsigned int blk_size, size_t total) {
114 size_t avail = pdu->max_size - pdu->used_size - pdu->hdr_size;
115 unsigned int start = num << (blk_size + 4);
116 unsigned int can_use_bert = block->defined == 0 || block->bert;
117
118 assert(start <= total);
119 memset(block, 0, sizeof(*block));
120 block->num = num;
121 block->szx = block->aszx = blk_size;
122 if (can_use_bert && blk_size == 6 && avail >= 1024 && session != NULL &&
123 COAP_PROTO_RELIABLE(session->proto) &&
124 session->csm_bert_rem_support && session->csm_bert_loc_support) {
125 block->bert = 1;
126 block->aszx = 7;
127 block->chunk_size = (uint32_t)((avail / 1024) * 1024);
128 } else {
129 block->chunk_size = (size_t)1 << (blk_size + 4);
130 if (avail < block->chunk_size && (total - start) >= avail) {
131 /* Need to reduce block size */
132 unsigned int szx;
133 int new_blk_size;
134
135 if (avail < 16) { /* bad luck, this is the smallest block size */
137 "not enough space, even the smallest block does not fit\n");
138 return 0;
139 }
140 new_blk_size = coap_flsll((long long)avail) - 5;
142 "decrease block size for %zu to %d\n", avail, new_blk_size);
143 szx = block->szx;
144 block->szx = new_blk_size;
145 block->num <<= szx - block->szx;
146 block->chunk_size = (size_t)1 << (new_blk_size + 4);
147 }
148 }
149 block->m = block->chunk_size < total - start;
150 return 1;
151}
152
153int
155 coap_pdu_t *pdu, size_t data_length) {
156 size_t start;
157 unsigned char buf[4];
158 coap_block_b_t block_b;
159
160 assert(pdu);
161
162 start = block->num << (block->szx + 4);
163 if (block->num != 0 && data_length <= start) {
164 coap_log(LOG_DEBUG, "illegal block requested\n");
165 return -2;
166 }
167
168 assert(pdu->max_size > 0);
169
170 block_b.defined = 1;
171 block_b.bert = 0;
172 if (!setup_block_b(NULL, pdu, &block_b, block->num,
173 block->szx, data_length))
174 return -3;
175
176 /* to re-encode the block option */
177 coap_update_option(pdu, number, coap_encode_var_safe(buf, sizeof(buf),
178 ((block_b.num << 4) |
179 (block_b.m << 3) |
180 block_b.szx)),
181 buf);
182
183 return 1;
184}
185
186int
188 coap_option_num_t number,
189 coap_pdu_t *pdu, size_t data_length) {
190 size_t start;
191 unsigned char buf[4];
192
193 assert(pdu);
194
195 start = block->num << (block->szx + 4);
196 if (block->num != 0 && data_length <= start) {
197 coap_log(LOG_DEBUG, "illegal block requested\n");
198 return -2;
199 }
200
201 assert(pdu->max_size > 0);
202
203 if (!setup_block_b(session, pdu, block, block->num,
204 block->szx, data_length))
205 return -3;
206
207 /* to re-encode the block option */
208 coap_update_option(pdu, number, coap_encode_var_safe(buf, sizeof(buf),
209 ((block->num << 4) |
210 (block->m << 3) |
211 block->aszx)),
212 buf);
213
214 return 1;
215}
216
217int
218coap_add_block(coap_pdu_t *pdu, size_t len, const uint8_t *data,
219 unsigned int block_num, unsigned char block_szx) {
220 unsigned int start;
221 start = block_num << (block_szx + 4);
222
223 if (len <= start)
224 return 0;
225
226 return coap_add_data(pdu,
227 min(len - start, ((size_t)1 << (block_szx + 4))),
228 data + start);
229}
230
231int
232coap_add_block_b_data(coap_pdu_t *pdu, size_t len, const uint8_t *data,
233 coap_block_b_t *block) {
234 unsigned int start = block->num << (block->szx + 4);
235 size_t max_size;
236
237 if (len <= start)
238 return 0;
239
240 if (block->bert)
241 max_size = ((pdu->max_size - pdu->used_size - pdu->hdr_size) /1024) * 1024;
242 else
243 max_size = (size_t)1 << (block->szx + 4);
244 block->chunk_size = (uint32_t)max_size;
245
246 return coap_add_data(pdu,
247 min(len - start, max_size),
248 data + start);
249}
250
251/*
252 * Note that the COAP_OPTION_ have to be added in the correct order
253 */
254void
256 coap_pdu_t *response,
257 uint16_t media_type,
258 int maxage,
259 size_t length,
260 const uint8_t* data
261) {
262 coap_key_t etag;
263 unsigned char buf[4];
264 coap_block_t block2;
265 int block2_requested = 0;
266
267 memset(&block2, 0, sizeof(block2));
268 /*
269 * Need to check that a valid block is getting asked for so that the
270 * correct options are put into the PDU.
271 */
272 if (request) {
273 if (coap_get_block(request, COAP_OPTION_BLOCK2, &block2)) {
274 block2_requested = 1;
275 if (block2.num != 0 && length <= (block2.num << (block2.szx + 4))) {
276 coap_log(LOG_DEBUG, "Illegal block requested (%d > last = %zu)\n",
277 block2.num,
278 length >> (block2.szx + 4));
279 response->code = COAP_RESPONSE_CODE(400);
280 goto error;
281 }
282 }
283 }
284 response->code = COAP_RESPONSE_CODE(205);
285
286 /* add etag for the resource */
287 memset(etag, 0, sizeof(etag));
288 coap_hash(data, length, etag);
289 coap_add_option_internal(response, COAP_OPTION_ETAG, sizeof(etag), etag);
290
292 coap_encode_var_safe(buf, sizeof(buf),
293 media_type),
294 buf);
295
296 if (maxage >= 0) {
297 coap_insert_option(response,
299 coap_encode_var_safe(buf, sizeof(buf), maxage), buf);
300 }
301
302 if (block2_requested) {
303 int res;
304
305 res = coap_write_block_opt(&block2, COAP_OPTION_BLOCK2, response, length);
306
307 switch (res) {
308 case -2: /* illegal block (caught above) */
309 response->code = COAP_RESPONSE_CODE(400);
310 goto error;
311 case -1: /* should really not happen */
312 assert(0);
313 /* fall through if assert is a no-op */
314 case -3: /* cannot handle request */
315 response->code = COAP_RESPONSE_CODE(500);
316 goto error;
317 default: /* everything is good */
318 ;
319 }
320
323 coap_encode_var_safe8(buf, sizeof(buf), length),
324 buf);
325
326 coap_add_block(response, length, data,
327 block2.num, block2.szx);
328 return;
329 }
330
331 /*
332 * Block2 not requested
333 */
334 if (!coap_add_data(response, length, data)) {
335 /*
336 * Insufficient space to add in data - use block mode
337 * set initial block size, will be lowered by
338 * coap_write_block_opt() automatically
339 */
340 block2.num = 0;
341 block2.szx = 6;
342 coap_write_block_opt(&block2, COAP_OPTION_BLOCK2, response, length);
343
346 coap_encode_var_safe8(buf, sizeof(buf), length),
347 buf);
348
349 coap_add_block(response, length, data,
350 block2.num, block2.szx);
351 }
352 return;
353
354error:
355 coap_add_data(response,
356 strlen(coap_response_phrase(response->code)),
357 (const unsigned char *)coap_response_phrase(response->code));
358}
359
360void
362 uint8_t block_mode) {
363 context->block_mode = block_mode &= (COAP_BLOCK_USE_LIBCOAP |
365 if (!(block_mode & COAP_BLOCK_USE_LIBCOAP))
366 context->block_mode = 0;
367}
368
370full_match(const uint8_t *a, size_t alen,
371 const uint8_t *b, size_t blen) {
372 return alen == blen && (alen == 0 || memcmp(a, b, alen) == 0);
373}
374
375#if COAP_CLIENT_SUPPORT
376int
378 coap_pdu_type_t type) {
379 coap_lg_crcv_t *cq;
380
381 assert(session);
382 if (!session)
383 return 0;
384
385 if (!(session->block_mode & COAP_BLOCK_USE_LIBCOAP)) {
387 "** %s: coap_cancel_observe: COAP_BLOCK_USE_LIBCOAP not enabled\n",
388 coap_session_str(session));
389 return 0;
390 }
391
392 LL_FOREACH(session->lg_crcv, cq) {
393 if (cq->observe_set) {
394 if ((!token && !cq->app_token->length) || (token &&
395 full_match(token->s, token->length, cq->app_token->s,
396 cq->app_token->length))) {
397 uint8_t buf[8];
398 coap_mid_t mid;
399 size_t size = coap_encode_var_safe8(buf, sizeof(cq->state_token),
400 cq->state_token);
401 const uint8_t *data;
402 coap_pdu_t * pdu = coap_pdu_duplicate(&cq->pdu,
403 session,
404 size,
405 buf,
406 NULL);
407
408 cq->observe_set = 0;
409 if (pdu == NULL)
410 return 0;
411 /* Need to make sure that this is the correct type */
412 pdu->type = type;
413
414 if (coap_get_data(&cq->pdu, &size, &data)) {
415 coap_add_data(pdu, size, data);
416 }
418 coap_encode_var_safe(buf, sizeof(buf),
420 buf);
421 mid = coap_send_internal(session, pdu);
422 if (mid != COAP_INVALID_MID)
423 return 1;
424 break;
425 }
426 }
427 }
428 return 0;
429}
430#endif /* COAP_CLIENT_SUPPORT */
431
432static int
434 coap_pdu_t *pdu,
435 coap_resource_t *resource,
436 const coap_string_t *query,
437 int maxage,
438 uint64_t etag,
439 size_t length,
440 const uint8_t *data,
441 coap_release_large_data_t release_func,
442 void *app_ptr,
443 coap_pdu_code_t request_method) {
444
445 ssize_t avail;
446 coap_block_b_t block;
447 size_t chunk;
448 coap_lg_xmit_t *lg_xmit = NULL;
449 uint8_t buf[8];
450 int have_block_defined = 0;
451 uint8_t blk_size;
452 uint16_t option;
453
454 assert(pdu);
455 if (pdu->data) {
456 coap_log(LOG_WARNING, "coap_add_data_large: PDU already contains data\n");
457 if (release_func)
458 release_func(session, app_ptr);
459 return 0;
460 }
461
462 if (!(session->block_mode & COAP_BLOCK_USE_LIBCOAP)) {
464 "** %s: coap_add_data_large: COAP_BLOCK_USE_LIBCOAP not enabled\n",
465 coap_session_str(session));
466 goto add_data;
467 }
468
469 /* Block NUM max 20 bits and block size is "2**(SZX + 4)"
470 and using SZX max of 6 gives maximum size = 1,073,740,800
471 CSM Max-Message-Size theoretical maximum = 4,294,967,295
472 So, if using blocks, we are limited to 1,073,740,800.
473 */
474#define MAX_BLK_LEN (((1 << 20) - 1) * (1 << (6 + 4)))
475
476 if (length > MAX_BLK_LEN) {
478 "Size of large buffer restricted to 0x%x bytes\n", MAX_BLK_LEN);
479 length = MAX_BLK_LEN;
480 }
481 /* Determine the block size to use, adding in sensible options if needed */
482 if (COAP_PDU_IS_REQUEST(pdu)) {
484
485 option = COAP_OPTION_BLOCK1;
486
487 /* See if this token is already in use for large bodies (unlikely) */
488 LL_FOREACH_SAFE(session->lg_xmit, lg_xmit, q) {
489 if (full_match(pdu->token, pdu->token_length,
490 lg_xmit->b.b1.app_token->s,
491 lg_xmit->b.b1.app_token->length)) {
492 /* Unfortunately need to free this off as potential size change */
493 LL_DELETE(session->lg_xmit, lg_xmit);
494 coap_block_delete_lg_xmit(session, lg_xmit);
495 lg_xmit = NULL;
496 break;
497 }
498 }
499 }
500 else {
501 /* Have to assume that it is a response even if code is 0.00 */
503 coap_string_t empty = { 0, NULL};
504
505 assert(resource);
506 option = COAP_OPTION_BLOCK2;
507 /* Check if resource+query is already in use for large bodies (unlikely) */
508 LL_FOREACH_SAFE(session->lg_xmit, lg_xmit, q) {
509 if (resource == lg_xmit->b.b2.resource &&
510 request_method == lg_xmit->b.b2.request_method &&
511 coap_string_equal(query ? query : &empty,
512 lg_xmit->b.b2.query ? lg_xmit->b.b2.query : &empty)) {
513 /* Unfortunately need to free this off as potential size change */
514 LL_DELETE(session->lg_xmit, lg_xmit);
515 coap_block_delete_lg_xmit(session, lg_xmit);
516 lg_xmit = NULL;
517 break;
518 }
519 }
520 }
521
522 avail = pdu->max_size - pdu->used_size - pdu->hdr_size;
523 /* There may be a response with Echo option */
525 /* May need token of length 8, so account for this */
526 avail -= (pdu->token_length < 8) ? 8 - pdu->token_length : 0;
527 blk_size = coap_flsll((long long)avail) - 4 - 1;
528 if (blk_size > 6)
529 blk_size = 6;
530
531 /* see if BlockX defined - if so update blk_size as given by app */
532 if (coap_get_block_b(session, pdu, option, &block)) {
533 if (block.szx < blk_size)
534 blk_size = block.szx;
535 have_block_defined = 1;
536 }
537
538 if (avail < 16 && ((ssize_t)length > avail || have_block_defined)) {
539 /* bad luck, this is the smallest block size */
541 "not enough space, even the smallest block does not fit\n");
542 goto fail;
543 }
544
545 chunk = (size_t)1 << (blk_size + 4);
546 if (have_block_defined && block.num != 0) {
547 /* App is defining a single block to send */
548 size_t rem;
549
550 if (length >= block.num * chunk) {
551 rem = chunk;
552 if (chunk > length - block.num * chunk)
553 rem = length - block.num * chunk;
554 if (!coap_add_data(pdu, rem, &data[block.num * chunk]))
555 goto fail;
556 }
557 if (release_func)
558 release_func(session, app_ptr);
559 }
560 else if ((have_block_defined && length > chunk) || (ssize_t)length > avail) {
561 /* Only add in lg_xmit if more than one block needs to be handled */
562 size_t rem;
563
564 lg_xmit = coap_malloc_type(COAP_LG_XMIT, sizeof(coap_lg_xmit_t));
565 if (!lg_xmit)
566 goto fail;
567
568 /* Set up for displaying all the data in the pdu */
569 pdu->body_data = data;
570 pdu->body_length = length;
571 coap_log(LOG_DEBUG, "PDU presented by app.\n");
573 pdu->body_data = NULL;
574 pdu->body_length = 0;
575
576 coap_log(LOG_DEBUG, "** %s: lg_xmit %p initialized\n",
577 coap_session_str(session), (void*)lg_xmit);
578 /* Update lg_xmit with large data information */
579 memset(lg_xmit, 0, sizeof(coap_lg_xmit_t));
580 lg_xmit->blk_size = blk_size;
581 lg_xmit->option = option;
582 lg_xmit->last_block = 0;
583 lg_xmit->data = data;
584 lg_xmit->length = length;
585 lg_xmit->release_func = release_func;
586 lg_xmit->app_ptr = app_ptr;
587 coap_ticks(&lg_xmit->last_obs);
588 if (COAP_PDU_IS_REQUEST(pdu)) {
589 /* Need to keep original token for updating response PDUs */
590 lg_xmit->b.b1.app_token = coap_new_binary(pdu->token_length);
591 if (!lg_xmit->b.b1.app_token)
592 goto fail;
593 memcpy(lg_xmit->b.b1.app_token->s, pdu->token, pdu->token_length);
594 /*
595 * Need to set up new token for use during transmits
596 */
597 lg_xmit->b.b1.count = 1;
598 lg_xmit->b.b1.state_token = STATE_TOKEN_FULL(++session->tx_token,
599 lg_xmit->b.b1.count);
600 /*
601 * Token will be updated in pdu later as original pdu may be needed in
602 * coap_send()
603 */
606 coap_encode_var_safe(buf, sizeof(buf),
607 (unsigned int)length),
608 buf);
611 coap_encode_var_safe8(buf, sizeof(buf),
612 ++session->tx_rtag),
613 buf);
614 } else {
615 /*
616 * resource+query match is used for Block2 large body transmissions
617 * token match is used for Block1 large body transmissions
618 */
619 lg_xmit->b.b2.resource = resource;
620 if (query) {
621 lg_xmit->b.b2.query = coap_new_string(query->length);
622 if (lg_xmit->b.b2.query) {
623 memcpy(lg_xmit->b.b2.query->s, query->s, query->length);
624 }
625 }
626 else {
627 lg_xmit->b.b2.query = NULL;
628 }
629 lg_xmit->b.b2.etag = etag;
630 lg_xmit->b.b2.request_method = request_method;
631 if (maxage >= 0) {
632 coap_tick_t now;
633
634 coap_ticks(&now);
635 lg_xmit->b.b2.maxage_expire = coap_ticks_to_rt(now) + maxage;
636 }
637 else {
638 lg_xmit->b.b2.maxage_expire = 0;
639 }
642 coap_encode_var_safe(buf, sizeof(buf),
643 (unsigned int)length),
644 buf);
645 if (etag == 0) {
646 if (++session->context->etag == 0)
647 ++session->context->etag;
648 etag = session->context->etag;
649 }
652 coap_encode_var_safe8(buf, sizeof(buf), etag),
653 buf);
654 }
655
656 if (!setup_block_b(session, pdu, &block, block.num,
657 blk_size, lg_xmit->length))
658 goto fail;
659
660 /* Add in with requested block num, more bit and block size */
662 lg_xmit->option,
663 coap_encode_var_safe(buf, sizeof(buf),
664 (block.num << 4) | (block.m << 3) | block.aszx),
665 buf);
666
667 /* Set up skeletal PDU to use as a basis for all the subsequent blocks */
668 memcpy(&lg_xmit->pdu, pdu, sizeof(lg_xmit->pdu));
669 lg_xmit->pdu.token = coap_malloc_type(COAP_PDU_BUF,
670 8 + lg_xmit->pdu.used_size + lg_xmit->pdu.hdr_size);
671 if (!lg_xmit->pdu.token)
672 goto fail;
673
674 lg_xmit->pdu.alloc_size = 8 + lg_xmit->pdu.used_size +
675 lg_xmit->pdu.hdr_size;
676 lg_xmit->pdu.token += lg_xmit->pdu.hdr_size;
677 memcpy(lg_xmit->pdu.token, pdu->token, lg_xmit->pdu.used_size);
678 if (pdu->data)
679 lg_xmit->pdu.data = lg_xmit->pdu.token + (pdu->data - pdu->token);
680
681 /* Check we still have space after adding in some options */
682 avail = pdu->max_size - pdu->used_size - pdu->hdr_size;
683 /* There may be a response with Echo option */
685 /* May need token of length 8, so account for this */
686 avail -= (pdu->token_length < 8) ? 8 - pdu->token_length : 0;
687 if (avail < (ssize_t)chunk) {
688 /* chunk size change down */
689 if (avail < 16) {
691 "not enough space, even the smallest block does not fit\n");
692 goto fail;
693 }
694 blk_size = coap_flsll((long long)avail) - 4 - 1;
695 block.num = block.num << (lg_xmit->blk_size - blk_size);
696 lg_xmit->blk_size = blk_size;
697 chunk = (size_t)1 << (lg_xmit->blk_size + 4);
698 block.chunk_size = (uint32_t)chunk;
699 block.bert = 0;
701 lg_xmit->option,
702 coap_encode_var_safe(buf, sizeof(buf),
703 (block.num << 4) | (block.m << 3) | lg_xmit->blk_size),
704 buf);
705 }
706
707 rem = block.chunk_size;
708 if (rem > lg_xmit->length - block.num * chunk)
709 rem = lg_xmit->length - block.num * chunk;
710 if (!coap_add_data(pdu, rem, &data[block.num * chunk]))
711 goto fail;
712
713 if (COAP_PDU_IS_REQUEST(pdu))
714 lg_xmit->b.b1.bert_size = rem;
715
716 lg_xmit->last_block = -1;
717
718 /* Link the new lg_xmit in */
719 LL_PREPEND(session->lg_xmit,lg_xmit);
720 }
721 else {
722 /* No need to use blocks */
723 if (have_block_defined) {
725 option,
726 coap_encode_var_safe(buf, sizeof(buf),
727 (0 << 4) | (0 << 3) | blk_size), buf);
728 }
729add_data:
730 if (!coap_add_data(pdu, length, data))
731 goto fail;
732
733 if (release_func)
734 release_func(session, app_ptr);
735 }
736 return 1;
737
738fail:
739 if (lg_xmit) {
740 coap_block_delete_lg_xmit(session, lg_xmit);
741 } else if (release_func) {
742 release_func(session, app_ptr);
743 }
744 return 0;
745}
746
747#if COAP_CLIENT_SUPPORT
748int
750 coap_pdu_t *pdu,
751 size_t length,
752 const uint8_t *data,
753 coap_release_large_data_t release_func,
754 void *app_ptr) {
755 /*
756 * Delay if session->doing_first is set.
757 * E.g. Reliable and CSM not in yet for checking block support
758 */
759 if (coap_client_delay_first(session) == 0) {
760 if (release_func)
761 release_func(session, app_ptr);
762 return 0;
763 }
764 return coap_add_data_large_internal(session, pdu, NULL, NULL, -1, 0, length,
765 data, release_func, app_ptr, 0);
766}
767#endif /* ! COAP_CLIENT_SUPPORT */
768
769#if COAP_SERVER_SUPPORT
770int
772 coap_session_t *session,
773 const coap_pdu_t *request,
774 coap_pdu_t *response,
775 const coap_string_t *query,
776 uint16_t media_type,
777 int maxage,
778 uint64_t etag,
779 size_t length,
780 const uint8_t *data,
781 coap_release_large_data_t release_func,
782 void *app_ptr
783) {
784 unsigned char buf[4];
785 coap_block_b_t block;
786 int block_requested = 0;
787 uint16_t block_opt = COAP_OPTION_BLOCK2;
788
789 memset(&block, 0, sizeof(block));
790 /*
791 * Need to check that a valid block is getting asked for so that the
792 * correct options are put into the PDU.
793 */
794 if (request) {
795 if (coap_get_block_b(session, request, COAP_OPTION_BLOCK2, &block)) {
796 block_requested = 1;
797 if (block.num != 0 && length <= (block.num << (block.szx + 4))) {
798 coap_log(LOG_DEBUG, "Illegal block requested (%d > last = %zu)\n",
799 block.num,
800 length >> (block.szx + 4));
801 response->code = COAP_RESPONSE_CODE(400);
802 goto error;
803 }
804 }
805 }
806
807 if (media_type != 0)
809 coap_encode_var_safe(buf, sizeof(buf),
810 media_type),
811 buf);
812
813 if (maxage >= 0) {
814 coap_insert_option(response,
816 coap_encode_var_safe(buf, sizeof(buf), maxage), buf);
817 }
818
819 if (block_requested) {
820 int res;
821
822 res = coap_write_block_b_opt(session, &block, block_opt, response,
823 length);
824
825 switch (res) {
826 case -2: /* illegal block (caught above) */
827 response->code = COAP_RESPONSE_CODE(400);
828 goto error;
829 case -1: /* should really not happen */
830 assert(0);
831 /* fall through if assert is a no-op */
832 case -3: /* cannot handle request */
833 response->code = COAP_RESPONSE_CODE(500);
834 goto error;
835 default: /* everything is good */
836 ;
837 }
838 }
839
840 /* add data body */
841 if (request &&
842 !coap_add_data_large_internal(session, response, resource, query,
843 maxage, etag, length, data,
844 release_func, app_ptr,
845 request->code)) {
846 response->code = COAP_RESPONSE_CODE(500);
847 goto error_released;
848 }
849
850 return 1;
851
852error:
853 if (release_func)
854 release_func(session, app_ptr);
855error_released:
856#if COAP_ERROR_PHRASE_LENGTH > 0
857 coap_add_data(response,
858 strlen(coap_response_phrase(response->code)),
859 (const unsigned char *)coap_response_phrase(response->code));
860#endif /* COAP_ERROR_PHRASE_LENGTH > 0 */
861 return 0;
862}
863#endif /* ! COAP_SERVER_SUPPORT */
864
869 coap_tick_t idle_timeout = 8 * COAP_TICKS_PER_SECOND;
870 coap_tick_t tim_rem = -1;
871
872 LL_FOREACH_SAFE(session->lg_xmit, p, q) {
873 if (p->last_all_sent == 0) {
874 continue;
875 }
876 if (p->last_all_sent + idle_timeout <= now) {
877 /* Expire this entry */
878 LL_DELETE(session->lg_xmit, p);
879 coap_block_delete_lg_xmit(session, p);
880 }
881 else {
882 /* Delay until the lg_xmit needs to expire */
883 if (tim_rem > p->last_all_sent + idle_timeout - now)
884 tim_rem = p->last_all_sent + idle_timeout - now;
885 }
886 }
887 return tim_rem;
888}
889
890#if COAP_CLIENT_SUPPORT
895 coap_tick_t partial_timeout = COAP_EXCHANGE_LIFETIME(session);
896 coap_tick_t tim_rem = -1;
897
898 LL_FOREACH_SAFE(session->lg_crcv, p, q) {
899 if (!p->observe_set && p->last_used &&
900 p->last_used + partial_timeout <= now) {
901 /* Expire this entry */
902 LL_DELETE(session->lg_crcv, p);
903 coap_block_delete_lg_crcv(session, p);
904 }
905 else if (!p->observe_set && p->last_used) {
906 /* Delay until the lg_crcv needs to expire */
907 if (tim_rem > p->last_used + partial_timeout - now)
908 tim_rem = p->last_used + partial_timeout - now;
909 }
910 }
911 return tim_rem;
912}
913#endif /* COAP_CLIENT_SUPPORT */
914
915static int
916check_if_received_block(coap_rblock_t *rec_blocks, uint32_t block_num) {
917 uint32_t i;
918
919 for (i = 0; i < rec_blocks->used; i++) {
920 if (block_num < rec_blocks->range[i].begin)
921 return 0;
922 if (block_num <= rec_blocks->range[i].end)
923 return 1;
924 }
925 return 0;
926}
927
928static int
929check_all_blocks_in(coap_rblock_t *rec_blocks, size_t total_blocks) {
930 uint32_t i;
931 uint32_t block = 0;
932
933 for (i = 0; i < rec_blocks->used; i++) {
934 if (block < rec_blocks->range[i].begin)
935 return 0;
936 if (block < rec_blocks->range[i].end)
937 block = rec_blocks->range[i].end;
938 }
939 /* total_blocks counts from 1 */
940 if (block + 1 < total_blocks)
941 return 0;
942
943 return 1;
944}
945
946#if COAP_SERVER_SUPPORT
951 coap_tick_t partial_timeout = COAP_EXCHANGE_LIFETIME(session);
952 coap_tick_t tim_rem = -1;
953
954 LL_FOREACH_SAFE(session->lg_srcv, p, q) {
955 if (p->last_used && p->last_used + partial_timeout <= now) {
956 /* Expire this entry */
957 LL_DELETE(session->lg_srcv, p);
958 coap_block_delete_lg_srcv(session, p);
959 }
960 else if (p->last_used) {
961 /* Delay until the lg_srcv needs to expire */
962 if (tim_rem > p->last_used + partial_timeout - now)
963 tim_rem = p->last_used + partial_timeout - now;
964 }
965 }
966 return tim_rem;
967}
968#endif /* COAP_SERVER_SUPPORT */
969
970#if COAP_CLIENT_SUPPORT
973 coap_lg_crcv_t *lg_crcv;
974 uint64_t state_token = STATE_TOKEN_FULL(++session->tx_token, 1);
975
976 lg_crcv = coap_malloc_type(COAP_LG_CRCV, sizeof(coap_lg_crcv_t));
977
978 if (lg_crcv == NULL)
979 return NULL;
980
982 "** %s: lg_crcv %p initialized - stateless token xxxx%012llx\n",
983 coap_session_str(session), (void*)lg_crcv,
984 STATE_TOKEN_BASE(state_token));
985 memset(lg_crcv, 0, sizeof(coap_lg_crcv_t));
986 lg_crcv->initial = 1;
987 /* Set up skeletal PDU to use as a basis for all the subsequent blocks */
988 memcpy(&lg_crcv->pdu, pdu, sizeof(lg_crcv->pdu));
989 lg_crcv->pdu.token = coap_malloc_type(COAP_PDU_BUF,
990 lg_crcv->pdu.alloc_size + lg_crcv->pdu.hdr_size);
991 if (!lg_crcv->pdu.token) {
992 coap_block_delete_lg_crcv(session, lg_crcv);
993 return NULL;
994 }
995 lg_crcv->pdu.token += lg_crcv->pdu.hdr_size;
996 memcpy(lg_crcv->pdu.token, pdu->token, lg_crcv->pdu.used_size);
997 if (lg_crcv->pdu.data)
998 lg_crcv->pdu.data = lg_crcv->pdu.token + (pdu->data - pdu->token);
999 /* Check that there is space for increased token + option change */
1000 if (lg_crcv->pdu.max_size < lg_crcv->pdu.used_size + 9)
1001 lg_crcv->pdu.max_size = lg_crcv->pdu.used_size + 9;
1002
1003 /* Need to keep original token for updating response PDUs */
1004 lg_crcv->app_token = coap_new_binary(pdu->token_length);
1005 if (!lg_crcv->app_token) {
1006 coap_block_delete_lg_crcv(session, lg_crcv);
1007 return NULL;
1008 }
1009 memcpy(lg_crcv->app_token->s, pdu->token, pdu->token_length);
1010
1011 /* Need to set up a base token for actual communications if retries needed */
1012 lg_crcv->retry_counter = 1;
1013 lg_crcv->state_token = state_token;
1014
1015 /* In case it is there - must not be in continuing request PDUs */
1016 coap_remove_option(&lg_crcv->pdu, COAP_OPTION_BLOCK1);
1017
1018 return lg_crcv;
1019}
1020
1021void
1023 coap_lg_crcv_t *lg_crcv) {
1024 if (lg_crcv == NULL)
1025 return;
1026
1027 if (lg_crcv->pdu.token)
1028 coap_free_type(COAP_PDU_BUF, lg_crcv->pdu.token - lg_crcv->pdu.hdr_size);
1030 coap_log(LOG_DEBUG, "** %s: lg_crcv %p released\n",
1031 coap_session_str(session), (void*)lg_crcv);
1032 coap_delete_binary(lg_crcv->app_token);
1033 coap_free_type(COAP_LG_CRCV, lg_crcv);
1034}
1035#endif /* COAP_CLIENT_SUPPORT */
1036
1037#if COAP_SERVER_SUPPORT
1038void
1040 coap_lg_srcv_t *lg_srcv) {
1041 if (lg_srcv == NULL)
1042 return;
1043
1046 coap_log(LOG_DEBUG, "** %s: lg_srcv %p released\n",
1047 coap_session_str(session), (void*)lg_srcv);
1048 coap_free_type(COAP_LG_SRCV, lg_srcv);
1049}
1050#endif /* COAP_SERVER_SUPPORT */
1051
1052void
1054 coap_lg_xmit_t *lg_xmit) {
1055 if (lg_xmit == NULL)
1056 return;
1057
1058 if (lg_xmit->release_func) {
1059 lg_xmit->release_func(session, lg_xmit->app_ptr);
1060 }
1061 if (lg_xmit->pdu.token) {
1062 coap_free_type(COAP_PDU_BUF, lg_xmit->pdu.token - lg_xmit->pdu.hdr_size);
1063 }
1064 if (COAP_PDU_IS_REQUEST(&lg_xmit->pdu))
1065 coap_delete_binary(lg_xmit->b.b1.app_token);
1066 else
1067 coap_delete_string(lg_xmit->b.b2.query);
1068
1069 coap_log(LOG_DEBUG, "** %s: lg_xmit %p released\n",
1070 coap_session_str(session), (void*)lg_xmit);
1071 coap_free_type(COAP_LG_XMIT, lg_xmit);
1072}
1073
1074#if COAP_SERVER_SUPPORT
1075static int
1076add_block_send(uint32_t num, uint32_t *out_blocks,
1077 uint32_t *count, uint32_t max_count) {
1078 uint32_t i;
1079
1080 for (i = 0; i < *count && *count < max_count; i++) {
1081 if (num == out_blocks[i])
1082 return 0;
1083 else if (num < out_blocks[i]) {
1084 if (*count - i > 1)
1085 memmove(&out_blocks[i], &out_blocks[i+1], *count - i -1);
1086 out_blocks[i] = num;
1087 (*count)++;
1088 return 1;
1089 }
1090 }
1091 if (*count < max_count) {
1092 out_blocks[i] = num;
1093 (*count)++;
1094 return 1;
1095 }
1096 return 0;
1097}
1098
1099/*
1100 * Need to see if this is a request for the next block of a large body
1101 * transfer. If so, need to initiate the response with the next blocks
1102 * and not trouble the application.
1103 *
1104 * If additional responses needed, then these are expicitly sent out and
1105 * 'response' is updated to be the last response to be sent.
1106 *
1107 * This is set up using coap_add_data_large_response()
1108 *
1109 * Server is sending a large data response to GET / observe (Block2)
1110 *
1111 * Return: 0 Call application handler
1112 * 1 Do not call application handler - just send the built response
1113 */
1114int
1116 coap_pdu_t *pdu,
1117 coap_pdu_t *response,
1118 coap_resource_t *resource,
1119 coap_string_t *query) {
1120 coap_lg_xmit_t *p = NULL;
1121 coap_block_b_t block;
1122 uint16_t block_opt = 0;
1123 uint32_t out_blocks[1];
1124 const char *error_phrase;
1125
1126 if (!coap_get_block_b(session, pdu, COAP_OPTION_BLOCK2, &block))
1127 return 0;
1128 if (block.num == 0) {
1129 /* Get a fresh copy of the data */
1130 return 0;
1131 }
1132 block_opt = COAP_OPTION_BLOCK2;
1133 LL_FOREACH(session->lg_xmit, p) {
1134 size_t chunk;
1135 coap_opt_iterator_t opt_iter;
1136 coap_opt_iterator_t opt_b_iter;
1137 coap_opt_t *option;
1138 uint32_t request_cnt, i;
1139 coap_opt_t *etag_opt = NULL;
1140 coap_pdu_t *out_pdu = response;
1141 static coap_string_t empty = { 0, NULL};
1142
1143 if (COAP_PDU_IS_REQUEST(&p->pdu) || resource != p->b.b2.resource ||
1144 pdu->code != p->b.b2.request_method ||
1145 !coap_string_equal(query ? query : &empty,
1146 p->b.b2.query ? p->b.b2.query : &empty)) {
1147 /* try out the next one */
1148 continue;
1149 }
1150 p->last_all_sent = 0;
1151 etag_opt = coap_check_option(pdu, COAP_OPTION_ETAG, &opt_iter);
1152 if (etag_opt) {
1153 uint64_t etag = coap_decode_var_bytes8(coap_opt_value(etag_opt),
1154 coap_opt_length(etag_opt));
1155 if (etag != p->b.b2.etag) {
1156 /* try out the next one */
1157 continue;
1158 }
1159 out_pdu->code = COAP_RESPONSE_CODE(203);
1160 goto skip_app_handler;
1161 }
1162 else {
1163 out_pdu->code = p->pdu.code;
1164 }
1165
1166 /* lg_xmit (response) found */
1167 coap_ticks(&p->last_obs);
1168
1169 chunk = (size_t)1 << (p->blk_size + 4);
1170 if (block_opt) {
1171 if (block.bert) {
1173 "found Block option, block is BERT, block nr. %u\n",
1174 block.num);
1175 } else {
1177 "found Block option, block size is %u, block nr. %u\n",
1178 1 << (block.szx + 4), block.num);
1179 }
1180 if (block.bert == 0 && block.szx != p->blk_size) {
1181 if ((p->offset + chunk) % ((size_t)1 << (block.szx + 4)) == 0) {
1182 /*
1183 * Recompute the block number of the previous packet given
1184 * the new block size
1185 */
1186 block.num = (uint32_t)(((p->offset + chunk) >> (block.szx + 4)) - 1);
1187 p->blk_size = block.szx;
1188 chunk = (size_t)1 << (p->blk_size + 4);
1189 p->offset = block.num * chunk;
1191 "new Block size is %u, block number %u completed\n",
1192 1 << (block.szx + 4), block.num);
1193 } else {
1195 "ignoring request to increase Block size, "
1196 "next block is not aligned on requested block size "
1197 "boundary. (%zu x %u mod %u = %zu (which is not 0)\n",
1198 p->offset/chunk + 1, (1 << (p->blk_size + 4)),
1199 (1 << (block.szx + 4)),
1200 (p->offset + chunk) % ((size_t)1 << (block.szx + 4)));
1201 }
1202 }
1203 }
1204
1205 request_cnt = 0;
1206 coap_option_iterator_init(pdu, &opt_b_iter, COAP_OPT_ALL);
1207 while ((option = coap_option_next(&opt_b_iter))) {
1208 unsigned int num;
1209 if (opt_b_iter.number != p->option)
1210 continue;
1211 num = coap_opt_block_num(option);
1212 if (num > 0xFFFFF) /* 20 bits max for num */
1213 continue;
1214 if (block.aszx != COAP_OPT_BLOCK_SZX(option)) {
1215 coap_add_data(response,
1216 sizeof("Changing blocksize during request invalid")-1,
1217 (const uint8_t *)"Changing blocksize during request invalid");
1218 response->code = COAP_RESPONSE_CODE(400);
1219 return 1;
1220 }
1221 add_block_send(num, out_blocks, &request_cnt, 1);
1222 break;
1223 }
1224 if (request_cnt == 0) {
1225 /* Block2 not found - give them the first block */
1226 block.szx = p->blk_size;
1227 p->offset = 0;
1228 out_blocks[0] = 0;
1229 request_cnt = 1;
1230 }
1231
1232 for (i = 0; i < request_cnt; i++) {
1233 uint8_t buf[8];
1234
1235 block.num = out_blocks[i];
1236 p->offset = block.num * chunk;
1237
1238 if (i + 1 < request_cnt) {
1239 /* Need to set up a copy of the pdu to send */
1240 coap_opt_filter_t drop_options;
1241
1242 memset(&drop_options, 0, sizeof(coap_opt_filter_t));
1244 out_pdu = coap_pdu_duplicate(&p->pdu, session, pdu->token_length,
1245 pdu->token, &drop_options);
1246 if (!out_pdu) {
1247 goto internal_issue;
1248 }
1249 }
1250 else {
1251 /*
1252 * Copy the options across and then fix the block option
1253 *
1254 * Need to drop Observe option if Block2 and block.num != 0
1255 */
1257 while ((option = coap_option_next(&opt_iter))) {
1258 if (opt_iter.number == COAP_OPTION_OBSERVE)
1259 continue;
1260 if (opt_iter.number == p->option)
1261 continue;
1262 if (!coap_insert_option(response, opt_iter.number,
1263 coap_opt_length(option),
1264 coap_opt_value(option))) {
1265 goto internal_issue;
1266 }
1267 }
1268 out_pdu = response;
1269 }
1270 if (pdu->type == COAP_MESSAGE_NON)
1271 out_pdu->type = COAP_MESSAGE_NON;
1272 if (block.bert) {
1273 block.m = (p->length - p->offset) >
1274 ((out_pdu->max_size - out_pdu->used_size - out_pdu->hdr_size) /1024) * 1024;
1275 } else {
1276 block.m = (p->offset + chunk) < p->length;
1277 }
1278 if (!coap_update_option(out_pdu, p->option,
1280 sizeof(buf),
1281 (block.num << 4) |
1282 (block.m << 3) |
1283 block.aszx),
1284 buf)) {
1285 goto internal_issue;
1286 }
1287 if (!(p->offset + chunk < p->length)) {
1288 /* Last block - keep in cache for 4 * ACK_TIMOUT */
1290 }
1291 if (p->b.b2.maxage_expire) {
1292 coap_tick_t now;
1293 coap_time_t rem;
1294
1295 coap_ticks(&now);
1296 rem = coap_ticks_to_rt(now);
1297 if (p->b.b2.maxage_expire > rem) {
1298 rem = p->b.b2.maxage_expire - rem;
1299 }
1300 else {
1301 rem = 0;
1302 /* Entry needs to be expired */
1304 }
1307 sizeof(buf),
1308 rem),
1309 buf)) {
1310 goto internal_issue;
1311 }
1312 }
1313
1314 if (!etag_opt && !coap_add_block_b_data(out_pdu,
1315 p->length,
1316 p->data,
1317 &block)) {
1318 goto internal_issue;
1319 }
1320 if (i + 1 < request_cnt) {
1321 coap_send_internal(session, out_pdu);
1322 }
1323 }
1325 goto skip_app_handler;
1326
1327internal_issue:
1328 response->code = COAP_RESPONSE_CODE(500);
1329 error_phrase = coap_response_phrase(response->code);
1330 coap_add_data(response, strlen(error_phrase),
1331 (const uint8_t *)error_phrase);
1332 /* Keep in cache for 4 * ACK_TIMOUT */
1333 if (p)
1335 goto skip_app_handler;
1336 } /* end of LL_FOREACH() */
1337 return 0;
1338
1339skip_app_handler:
1340 return 1;
1341}
1342#endif /* COAP_SERVER_SUPPORT */
1343
1344static int
1345update_received_blocks(coap_rblock_t *rec_blocks, uint32_t block_num) {
1346 uint32_t i;
1347
1348 /* Reset as there is activity */
1349 rec_blocks->retry = 0;
1350
1351 for (i = 0; i < rec_blocks->used; i++) {
1352 if (block_num >= rec_blocks->range[i].begin &&
1353 block_num <= rec_blocks->range[i].end)
1354 break;
1355
1356 if (block_num < rec_blocks->range[i].begin) {
1357 if (block_num + 1 == rec_blocks->range[i].begin) {
1358 rec_blocks->range[i].begin = block_num;
1359 }
1360 else {
1361 /* Need to insert a new range */
1362 if (rec_blocks->used == COAP_RBLOCK_CNT -1)
1363 /* Too many losses */
1364 return 0;
1365 memmove(&rec_blocks->range[i+1], &rec_blocks->range[i],
1366 (rec_blocks->used - i) * sizeof (rec_blocks->range[0]));
1367 rec_blocks->range[i].begin = rec_blocks->range[i].end = block_num;
1368 rec_blocks->used++;
1369 }
1370 break;
1371 }
1372 if (block_num == rec_blocks->range[i].end + 1) {
1373 rec_blocks->range[i].end = block_num;
1374 if (i + 1 < rec_blocks->used) {
1375 if (rec_blocks->range[i+1].begin == block_num + 1) {
1376 /* Merge the 2 ranges */
1377 rec_blocks->range[i].end = rec_blocks->range[i+1].end;
1378 if (i+2 < rec_blocks->used) {
1379 memmove (&rec_blocks->range[i+1], &rec_blocks->range[i+2],
1380 (rec_blocks->used - (i+2)) * sizeof (rec_blocks->range[0]));
1381 }
1382 rec_blocks->used--;
1383 }
1384 }
1385 break;
1386 }
1387 }
1388 if (i == rec_blocks->used) {
1389 if (rec_blocks->used == COAP_RBLOCK_CNT -1)
1390 /* Too many losses */
1391 return 0;
1392 rec_blocks->range[i].begin = rec_blocks->range[i].end = block_num;
1393 rec_blocks->used++;
1394 }
1395 coap_ticks(&rec_blocks->last_seen);
1396 return 1;
1397}
1398
1399#if COAP_SERVER_SUPPORT
1400/*
1401 * Need to check if this is a large PUT / POST using multiple blocks
1402 *
1403 * Server receiving PUT/POST etc. of a large amount of data (Block1)
1404 *
1405 * Return: 0 Call application handler
1406 * 1 Do not call application handler - just send the built response
1407 */
1408int
1410 coap_session_t *session,
1411 coap_pdu_t *pdu,
1412 coap_pdu_t *response,
1413 coap_resource_t *resource,
1414 coap_string_t *uri_path,
1415 coap_opt_t *observe,
1416 coap_string_t *query,
1418 int *added_block) {
1419 size_t length = 0;
1420 const uint8_t *data = NULL;
1421 size_t offset = 0;
1422 size_t total = 0;
1423 coap_block_b_t block;
1424 coap_opt_iterator_t opt_iter;
1425 uint16_t block_option = 0;
1426
1427 coap_get_data_large(pdu, &length, &data, &offset, &total);
1428 pdu->body_offset = 0;
1429 pdu->body_total = length;
1430
1431 if (coap_get_block_b(session, pdu, COAP_OPTION_BLOCK1, &block)) {
1432 block_option = COAP_OPTION_BLOCK1;
1433 }
1434 if (block_option) {
1435 coap_lg_srcv_t *p;
1436 coap_opt_t *size_opt = coap_check_option(pdu,
1438 &opt_iter);
1439 coap_opt_t *fmt_opt = coap_check_option(pdu,
1441 &opt_iter);
1442 uint16_t fmt = fmt_opt ? coap_decode_var_bytes(coap_opt_value(fmt_opt),
1443 coap_opt_length(fmt_opt)) :
1445 coap_opt_t *rtag_opt = coap_check_option(pdu,
1447 &opt_iter);
1448 size_t rtag_length = rtag_opt ? coap_opt_length(rtag_opt) : 0;
1449 const uint8_t *rtag = rtag_opt ? coap_opt_value(rtag_opt) : NULL;
1450
1451 total = size_opt ? coap_decode_var_bytes(coap_opt_value(size_opt),
1452 coap_opt_length(size_opt)) : 0;
1453 offset = block.num << (block.szx + 4);
1454
1455 LL_FOREACH(session->lg_srcv, p) {
1456 if (rtag_opt || p->rtag_set == 1) {
1457 if (!(rtag_opt && p->rtag_set == 1))
1458 continue;
1459 if (p->rtag_length != rtag_length ||
1460 memcmp(p->rtag, rtag, rtag_length) != 0)
1461 continue;
1462 }
1463 if (resource == p->resource) {
1464 break;
1465 }
1466 if ((p->resource == context->unknown_resource ||
1467 resource == context->proxy_uri_resource) &&
1468 coap_string_equal(uri_path, p->uri_path))
1469 break;
1470 }
1471 if (!p && block.num != 0) {
1472 /* random access - no need to track */
1473 pdu->body_data = data;
1474 pdu->body_length = length;
1475 pdu->body_offset = offset;
1476 pdu->body_total = length + offset + (block.m ? 1 : 0);
1477 }
1478 /* Do not do this if this is a single block */
1479 else if (!p && !(offset == 0 && block.m == 0)) {
1481 if (p == NULL) {
1482 coap_add_data(response, sizeof("Memory issue")-1,
1483 (const uint8_t *)"Memory issue");
1484 response->code = COAP_RESPONSE_CODE(500);
1485 goto skip_app_handler;
1486 }
1487 coap_log(LOG_DEBUG, "** %s: lg_srcv %p initialized\n",
1488 coap_session_str(session), (void*)p);
1489 memset(p, 0, sizeof(coap_lg_srcv_t));
1490 p->resource = resource;
1491 if (resource == context->unknown_resource ||
1492 resource == context->proxy_uri_resource)
1493 p->uri_path = coap_new_str_const(uri_path->s, uri_path->length);
1494 p->content_format = fmt;
1495 p->total_len = total;
1496 p->amount_so_far = length;
1497 p->szx = block.szx;
1498 p->block_option = block_option;
1499 if (observe) {
1500 p->observe_length = min(coap_opt_length(observe), 3);
1501 memcpy(p->observe, coap_opt_value(observe), p->observe_length);
1502 p->observe_set = 1;
1503 }
1504 if (rtag_opt) {
1505 p->rtag_length = (uint8_t)rtag_length;
1506 memcpy(p->rtag, rtag, rtag_length);
1507 p->rtag_set = 1;
1508 }
1509 p->body_data = NULL;
1510 LL_PREPEND(session->lg_srcv, p);
1511 }
1512 if (p) {
1513 if (fmt != p->content_format) {
1514 coap_add_data(response, sizeof("Content-Format mismatch")-1,
1515 (const uint8_t *)"Content-Format mismatch");
1516 response->code = COAP_RESPONSE_CODE(408);
1517 goto free_lg_srcv;
1518 }
1519 p->last_mid = pdu->mid;
1520 p->last_type = pdu->type;
1521 memcpy(p->last_token, pdu->token, pdu->token_length);
1523 if ((session->block_mode & COAP_BLOCK_SINGLE_BODY) || block.bert) {
1524 size_t chunk = (size_t)1 << (block.szx + 4);
1525 int update_data = 0;
1526 unsigned int saved_num = block.num;
1527 size_t saved_offset = offset;
1528
1529 while (offset < saved_offset + length) {
1530 if (!check_if_received_block(&p->rec_blocks, block.num)) {
1531 /* Update list of blocks received */
1532 if (!update_received_blocks(&p->rec_blocks, block.num)) {
1534 coap_add_data(response, sizeof("Too many missing blocks")-1,
1535 (const uint8_t *)"Too many missing blocks");
1536 response->code = COAP_RESPONSE_CODE(408);
1537 goto free_lg_srcv;
1538 }
1539 update_data = 1;
1540 }
1541 block.num++;
1542 offset = block.num << (block.szx + 4);
1543 }
1544 block.num--;
1545 if (update_data) {
1546 /* Update saved data */
1547 p->body_data = coap_block_build_body(p->body_data, length, data,
1548 saved_offset, p->total_len);
1549 if (!p->body_data)
1550 goto call_app_handler;
1551
1552 }
1553 if (block.m ||
1555 (uint32_t)(p->total_len + chunk -1)/chunk)) {
1556 /* Not all the payloads of the body have arrived */
1557 if (block.m) {
1558 uint8_t buf[4];
1559
1560 /* Ask for the next block */
1561 coap_insert_option(response, block_option,
1562 coap_encode_var_safe(buf, sizeof(buf),
1563 (saved_num << 4) |
1564 (block.m << 3) |
1565 block.aszx),
1566 buf);
1567 response->code = COAP_RESPONSE_CODE(231);
1568 goto skip_app_handler;
1569 }
1570 goto skip_app_handler;
1571 }
1572
1573 /*
1574 * Remove the Block1 option as passing all of the data to
1575 * application layer. Add back in observe option if appropriate.
1576 * Adjust all other information.
1577 */
1578 if (p->observe_set) {
1580 p->observe_length, p->observe);
1581 }
1582 coap_remove_option(pdu, block_option);
1583 pdu->body_data = p->body_data->s;
1584 pdu->body_length = p->total_len;
1585 pdu->body_offset = 0;
1586 pdu->body_total = p->total_len;
1587 coap_log(LOG_DEBUG, "Server app version of updated PDU\n");
1589 coap_log(LOG_DEBUG, "call custom handler for resource '%*.*s'\n",
1590 (int)resource->uri_path->length,
1591 (int)resource->uri_path->length, resource->uri_path->s);
1592 /* Need to do this here as we need to free off p */
1593 h(resource, session, pdu, query, response);
1594 /* Check if lg_xmit generated and update PDU code if so */
1595 coap_check_code_lg_xmit(session, response, resource, query, pdu->code);
1596 /* Check to see if the server is doing a 4.01 + Echo response */
1597 if (response->code == COAP_RESPONSE_CODE(401) &&
1598 coap_check_option(response, COAP_OPTION_ECHO, &opt_iter)) {
1599 /* Need to keep lg_srcv around for client's response */
1600 goto skip_app_handler;
1601 } else {
1602 /* Last chunk - lg_srcv no longer needed */
1603 goto free_lg_srcv;
1604 }
1605 }
1606 else {
1607 /* No need to update body_data and body_length as a single PDU */
1608 pdu->body_offset = offset;
1609 /* Exact match if last block */
1610 if (block.m) {
1611 uint8_t buf[4];
1612
1613 if (total > offset + length + block.m)
1614 pdu->body_total = total;
1615 else
1616 pdu->body_total = offset + length + block.m;
1617
1618 coap_insert_option(response, block_option,
1619 coap_encode_var_safe(buf, sizeof(buf),
1620 (block.num << 4) |
1621 (block.m << 3) |
1622 block.aszx),
1623 buf);
1624 coap_log(LOG_DEBUG, "call custom handler for resource '%*.*s'\n",
1625 (int)resource->uri_path->length,
1626 (int)resource->uri_path->length, resource->uri_path->s);
1627 h(resource, session, pdu, query, response);
1628 /* Check if lg_xmit generated and update PDU code if so */
1629 coap_check_code_lg_xmit(session, response, resource, query,
1630 pdu->code);
1631 if (COAP_RESPONSE_CLASS(response->code) == 2) {
1632 /* Just in case, as there are more to go */
1633 response->code = COAP_RESPONSE_CODE(231);
1634 }
1635 *added_block = 1;
1636 goto skip_app_handler;
1637 }
1638 else {
1639 pdu->body_total = offset + length + block.m;
1640 }
1641 }
1642
1643 if (block.m == 0) {
1644 /* Last chunk - free off all */
1645 coap_ticks(&p->last_used);
1646 }
1647 goto call_app_handler;
1648
1649free_lg_srcv:
1650 LL_DELETE(session->lg_srcv, p);
1651 coap_block_delete_lg_srcv(session, p);
1652 goto skip_app_handler;
1653 }
1654 }
1655call_app_handler:
1656 return 0;
1657
1658skip_app_handler:
1659 return 1;
1660}
1661#endif /* COAP_SERVER_SUPPORT */
1662
1663#if COAP_CLIENT_SUPPORT
1664static int
1665check_freshness(coap_session_t *session, coap_pdu_t *rcvd, coap_pdu_t *sent,
1666 coap_lg_xmit_t *lg_xmit, coap_lg_crcv_t *lg_crcv)
1667{
1668 /* Check for Echo option for freshness */
1669 coap_opt_iterator_t opt_iter;
1670 coap_opt_t *opt = coap_check_option(rcvd, COAP_OPTION_ECHO, &opt_iter);
1671
1672 if (opt) {
1673 if (sent || lg_xmit || lg_crcv) {
1674 /* Need to retransmit original request with Echo option added */
1675 coap_pdu_t *echo_pdu;
1676 coap_mid_t mid;
1677 const uint8_t *data;
1678 size_t data_len;
1679 int have_data = 0;
1680 uint8_t ltoken[8];
1681 size_t ltoken_len;
1682 uint64_t token;
1683
1684 if (sent) {
1685 if (coap_get_data(sent, &data_len, &data))
1686 have_data = 1;
1687 } else if (lg_xmit) {
1688 sent = &lg_xmit->pdu;
1689 if (lg_xmit->length) {
1690 size_t blk_size = (size_t)1 << (lg_xmit->blk_size + 4);
1691 size_t offset = (lg_xmit->last_block + 1) * blk_size;
1692 have_data = 1;
1693 data = &lg_xmit->data[offset];
1694 data_len = (lg_xmit->length - offset) > blk_size ? blk_size :
1695 lg_xmit->length - offset;
1696 }
1697 } else /* lg_crcv */ {
1698 sent = &lg_crcv->pdu;
1699 if (coap_get_data(sent, &data_len, &data))
1700 have_data = 1;
1701 }
1702 if (lg_xmit) {
1703 token = STATE_TOKEN_FULL(lg_xmit->b.b1.state_token,
1704 ++lg_xmit->b.b1.count);
1705 } else {
1706 token = STATE_TOKEN_FULL(lg_crcv->state_token,
1707 ++lg_crcv->retry_counter);
1708 }
1709 ltoken_len = coap_encode_var_safe8(ltoken, sizeof(token), token);
1710 echo_pdu = coap_pdu_duplicate(sent, session, ltoken_len, ltoken, NULL);
1711 if (!echo_pdu)
1712 return 0;
1713 if (!coap_insert_option(echo_pdu, COAP_OPTION_ECHO,
1714 coap_opt_length(opt), coap_opt_value(opt)))
1715 goto not_sent;
1716 if (have_data) {
1717 coap_add_data(echo_pdu, data_len, data);
1718 }
1719
1720 mid = coap_send_internal(session, echo_pdu);
1721 if (mid == COAP_INVALID_MID)
1722 goto not_sent;
1723 return 1;
1724 } else {
1725 /* Need to save Echo option value to add to next reansmission */
1726not_sent:
1727 coap_delete_bin_const(session->echo);
1728 session->echo = coap_new_bin_const(coap_opt_value(opt),
1729 coap_opt_length(opt));
1730 }
1731 }
1732 return 0;
1733}
1734
1735static void
1736track_echo(coap_session_t *session, coap_pdu_t *rcvd)
1737{
1738 coap_opt_iterator_t opt_iter;
1739 coap_opt_t *opt = coap_check_option(rcvd, COAP_OPTION_ECHO, &opt_iter);
1740
1741 if (opt) {
1742 coap_delete_bin_const(session->echo);
1743 session->echo = coap_new_bin_const(coap_opt_value(opt),
1744 coap_opt_length(opt));
1745 }
1746}
1747
1748/*
1749 * Need to see if this is a response to a large body request transfer. If so,
1750 * need to initiate the request containing the next block and not trouble the
1751 * application. Note that Token must unique per request/response.
1752 *
1753 * Client receives large data acknowledgement from server (Block1)
1754 *
1755 * This is set up using coap_add_data_large_request()
1756 *
1757 * Client is using GET etc.
1758 *
1759 * Return: 0 Call application handler
1760 * 1 Do not call application handler - just send the built response
1761 */
1762int
1764 coap_pdu_t *rcvd) {
1765 coap_lg_xmit_t *p;
1766 coap_lg_xmit_t *q;
1768 rcvd->token_length));
1769
1770 LL_FOREACH_SAFE(session->lg_xmit, p, q) {
1771 if (!COAP_PDU_IS_REQUEST(&p->pdu) ||
1773 token_match !=
1775 p->b.b1.app_token->length)))) {
1776 /* try out the next one */
1777 continue;
1778 }
1779 /* lg_xmit found */
1780 size_t chunk = (size_t)1 << (p->blk_size + 4);
1781 coap_block_b_t block;
1782 coap_lg_crcv_t *lg_crcv = NULL;
1783
1784 if (COAP_RESPONSE_CLASS(rcvd->code) == 2 &&
1785 coap_get_block_b(session, rcvd, p->option, &block)) {
1786
1787 if (block.bert) {
1789 "found Block option, block is BERT, block nr. %u (%zu)\n",
1790 block.num, p->b.b1.bert_size);
1791 } else {
1793 "found Block option, block size is %u, block nr. %u\n",
1794 1 << (block.szx + 4), block.num);
1795 }
1796 if (block.szx != p->blk_size) {
1797 if ((p->offset + chunk) % ((size_t)1 << (block.szx + 4)) == 0) {
1798 /*
1799 * Recompute the block number of the previous packet given the
1800 * new block size
1801 */
1802 block.num = (uint32_t)(((p->offset + chunk) >> (block.szx + 4)) - 1);
1803 p->blk_size = block.szx;
1804 chunk = (size_t)1 << (p->blk_size + 4);
1805 p->offset = block.num * chunk;
1807 "new Block size is %u, block number %u completed\n",
1808 1 << (block.szx + 4), block.num);
1809 block.bert = 0;
1810 block.aszx = block.szx;
1811 } else {
1812 coap_log(LOG_DEBUG, "ignoring request to increase Block size, "
1813 "next block is not aligned on requested block size boundary. "
1814 "(%zu x %u mod %u = %zu != 0)\n",
1815 p->offset/chunk + 1, (1 << (p->blk_size + 4)),
1816 (1 << (block.szx + 4)),
1817 (p->offset + chunk) % ((size_t)1 << (block.szx + 4)));
1818 }
1819 }
1820 track_echo(session, rcvd);
1821 if (p->last_block == (int)block.num) {
1822 /*
1823 * Duplicate Block1 ACK
1824 *
1825 * RFCs not clear here, but on a lossy connection, there could
1826 * be multiple Block1 ACKs, causing the client to retransmit the
1827 * same block multiple times, or the server retransmitting the
1828 * same ACK.
1829 *
1830 * Once a block has been ACKd, there is no need to retransmit it.
1831 */
1832 return 1;
1833 }
1834 if (block.bert)
1835 block.num += (unsigned int)(p->b.b1.bert_size / 1024 - 1);
1836 p->last_block = block.num;
1837 p->offset = (block.num + 1) * chunk;
1838 if (p->offset < p->length) {
1839 /* Build the next PDU request based off the skeletal PDU */
1840 uint8_t buf[8];
1841 coap_pdu_t *pdu;
1842 uint64_t token = STATE_TOKEN_FULL(p->b.b1.state_token, ++p->b.b1.count);
1843 size_t len = coap_encode_var_safe8(buf, sizeof(token), token);
1844
1845 pdu = coap_pdu_duplicate(&p->pdu, session, len, buf, NULL);
1846 if (!pdu)
1847 goto fail_body;
1848
1849 block.num++;
1850 if (block.bert) {
1851 block.m = (p->length - p->offset) >
1852 ((pdu->max_size - pdu->used_size - pdu->hdr_size) /1024) * 1024;
1853 } else {
1854 block.m = (p->offset + chunk) < p->length;
1855 }
1856 coap_update_option(pdu, p->option,
1857 coap_encode_var_safe(buf, sizeof(buf),
1858 (block.num << 4) |
1859 (block.m << 3) |
1860 block.aszx),
1861 buf);
1862
1863 if (!coap_add_block_b_data(pdu,
1864 p->length,
1865 p->data,
1866 &block))
1867 goto fail_body;
1868 p->b.b1.bert_size = block.chunk_size;
1869 if (coap_send_internal(session, pdu) == COAP_INVALID_MID)
1870 goto fail_body;
1871 return 1;
1872 }
1873 } else if (rcvd->code == COAP_RESPONSE_CODE(401)) {
1874 if (check_freshness(session, rcvd, sent, p, NULL))
1875 return 1;
1876 }
1877fail_body:
1878 if (session->lg_crcv) {
1879 LL_FOREACH(session->lg_crcv, lg_crcv) {
1880 if (STATE_TOKEN_BASE(p->b.b1.state_token) ==
1881 STATE_TOKEN_BASE(lg_crcv->state_token)) {
1882 /* In case of observe */
1883 lg_crcv->state_token = p->b.b1.state_token;
1884 break;
1885 }
1886 }
1887 }
1888 if (!lg_crcv) {
1889 /* need to put back original token into rcvd */
1890 if (p->b.b1.app_token)
1892 p->b.b1.app_token->s);
1893 coap_log(LOG_DEBUG, "PDU given to app\n");
1894 coap_show_pdu(LOG_DEBUG, rcvd);
1895 }
1896
1897 LL_DELETE(session->lg_xmit, p);
1898 coap_block_delete_lg_xmit(session, p);
1899 /*
1900 * There may be a block response after doing the large request
1901 * https://tools.ietf.org/html/rfc7959#section-3.3
1902 */
1903 break;
1904 } /* end of LL_FOREACH_SAFE */
1905 return 0;
1906}
1907#endif /* COAP_CLIENT_SUPPORT */
1908
1909/*
1910 * Re-assemble payloads into a body
1911 */
1913coap_block_build_body(coap_binary_t *body_data, size_t length,
1914 const uint8_t *data, size_t offset, size_t total)
1915{
1916 if (data == NULL)
1917 return NULL;
1918 if (body_data == NULL && total) {
1919 body_data = coap_new_binary(total);
1920 }
1921 if (body_data == NULL)
1922 return NULL;
1923
1924 /* Update saved data */
1925 if (offset + length <= total && body_data->length >= total) {
1926 memcpy(&body_data->s[offset], data, length);
1927 }
1928 else {
1929 /*
1930 * total may be inaccurate as per
1931 * https://tools.ietf.org/html/rfc7959#section-4
1932 * o In a request carrying a Block1 Option, to indicate the current
1933 * estimate the client has of the total size of the resource
1934 * representation, measured in bytes ("size indication").
1935 * o In a response carrying a Block2 Option, to indicate the current
1936 * estimate the server has of the total size of the resource
1937 * representation, measured in bytes ("size indication").
1938 */
1939 coap_binary_t *new = coap_resize_binary(body_data, offset + length);
1940
1941 if (new) {
1942 body_data = new;
1943 memcpy(&body_data->s[offset], data, length);
1944 }
1945 else {
1946 coap_delete_binary(body_data);
1947 return NULL;
1948 }
1949 }
1950 return body_data;
1951}
1952
1953#if COAP_CLIENT_SUPPORT
1954/*
1955 * Need to see if this is a large body response to a request. If so,
1956 * need to initiate the request for the next block and not trouble the
1957 * application. Note that Token must unique per request/response.
1958 *
1959 * This is set up using coap_send()
1960 * Client receives large data from server (Block2)
1961 *
1962 * Return: 0 Call application handler
1963 * 1 Do not call application handler - just sent the next request
1964 */
1965int
1967 coap_session_t *session,
1968 coap_pdu_t *sent,
1969 coap_pdu_t *rcvd,
1970 coap_recurse_t recursive) {
1971 coap_lg_crcv_t *p;
1972 int app_has_response = 0;
1973 coap_block_b_t block;
1974 int have_block = 0;
1975 uint16_t block_opt = 0;
1976 size_t offset;
1978 rcvd->token_length));
1979
1980 memset(&block, 0, sizeof(block));
1981 LL_FOREACH(session->lg_crcv, p) {
1982 size_t chunk = 0;
1983 uint8_t buf[8];
1984 coap_opt_iterator_t opt_iter;
1985
1987 !full_match(rcvd->token, rcvd->token_length,
1988 p->app_token->s, p->app_token->length)) {
1989 /* try out the next one */
1990 continue;
1991 }
1992
1993 /* lg_crcv found */
1994
1995 if (COAP_RESPONSE_CLASS(rcvd->code) == 2) {
1996 size_t length;
1997 const uint8_t *data;
1999 &opt_iter);
2000 size_t size2 = size_opt ?
2002 coap_opt_length(size_opt)) : 0;
2003
2004 coap_get_data(rcvd, &length, &data);
2005 rcvd->body_offset = 0;
2006 rcvd->body_total = length;
2007 if (coap_get_block_b(session, rcvd, COAP_OPTION_BLOCK2, &block)) {
2008 have_block = 1;
2009 block_opt = COAP_OPTION_BLOCK2;
2010 }
2011 track_echo(session, rcvd);
2012 if (have_block) {
2013 coap_opt_t *fmt_opt = coap_check_option(rcvd,
2015 &opt_iter);
2016 uint16_t fmt = fmt_opt ?
2018 coap_opt_length(fmt_opt)) :
2020 coap_opt_t *etag_opt = coap_check_option(rcvd,
2022 &opt_iter);
2023 size_t saved_offset;
2024 int updated_block;
2025
2026 /* Possibility that Size2 not sent, or is too small */
2027 chunk = (size_t)1 << (block.szx + 4);
2028 offset = block.num * chunk;
2029 if (size2 < (offset + length)) {
2030 if (block.m)
2031 size2 = offset + length + 1;
2032 else
2033 size2 = offset + length;
2034 }
2035 saved_offset = offset;
2036
2037 if (p->initial) {
2038 p->initial = 0;
2039 if (etag_opt) {
2040 p->etag_length = coap_opt_length(etag_opt);
2041 memcpy(p->etag, coap_opt_value(etag_opt), p->etag_length);
2042 p->etag_set = 1;
2043 }
2044 else {
2045 p->etag_set = 0;
2046 }
2047 p->total_len = size2;
2048 p->content_format = fmt;
2049 p->szx = block.szx;
2050 p->block_option = block_opt;
2051 p->last_type = rcvd->type;
2052 p->rec_blocks.used = 0;
2053 }
2054 if (p->total_len < size2)
2055 p->total_len = size2;
2056
2057 if (etag_opt) {
2058 if (!full_match(coap_opt_value(etag_opt),
2059 coap_opt_length(etag_opt),
2060 p->etag, p->etag_length)) {
2061 /* body of data has changed - need to restart request */
2062 coap_pdu_t *pdu;
2063 uint64_t token = STATE_TOKEN_FULL(p->state_token,
2064 ++p->retry_counter);
2065 size_t len = coap_encode_var_safe8(buf, sizeof(token), token);
2066 coap_opt_filter_t drop_options;
2067
2069 "Data body updated during receipt - new request started\n");
2070 if (!(session->block_mode & COAP_BLOCK_SINGLE_BODY))
2072
2073 p->initial = 1;
2075 p->body_data = NULL;
2076
2077 coap_session_new_token(session, &len, buf);
2078 memset(&drop_options, 0, sizeof(coap_opt_filter_t));
2080 pdu = coap_pdu_duplicate(&p->pdu, session, len, buf, &drop_options);
2081 if (!pdu)
2082 goto fail_resp;
2083
2084 coap_update_option(pdu, block_opt,
2085 coap_encode_var_safe(buf, sizeof(buf),
2086 (0 << 4) | (0 << 3) | block.aszx),
2087 buf);
2088
2089 if (coap_send_internal(session, pdu) == COAP_INVALID_MID)
2090 goto fail_resp;
2091
2092 goto skip_app_handler;
2093 }
2094 }
2095 else if (p->etag_set) {
2096 /* Cannot handle this change in ETag to not being there */
2097 coap_log(LOG_WARNING, "Not all blocks have ETag option\n");
2098 session->block_mode &= ~(COAP_BLOCK_SINGLE_BODY);
2099 goto block_mode;
2100 }
2101
2102 if (fmt != p->content_format) {
2103 coap_log(LOG_WARNING, "Content-Format option mismatch\n");
2104 session->block_mode &= ~(COAP_BLOCK_SINGLE_BODY);
2105 goto block_mode;
2106 }
2107 if (block.num == 0) {
2108 coap_opt_t *obs_opt = coap_check_option(rcvd,
2110 &opt_iter);
2111 if (obs_opt) {
2112 p->observe_length = min(coap_opt_length(obs_opt), 3);
2113 memcpy(p->observe, coap_opt_value(obs_opt), p->observe_length);
2114 p->observe_set = 1;
2115 }
2116 else {
2117 p->observe_set = 0;
2118 }
2119 }
2120 updated_block = 0;
2121 while (offset < saved_offset + length) {
2122 if (!check_if_received_block(&p->rec_blocks, block.num)) {
2123 /* Update list of blocks received */
2124 if (!update_received_blocks(&p->rec_blocks, block.num)) {
2126 goto fail_resp;
2127 }
2128 updated_block = 1;
2129 }
2130 block.num++;
2131 offset = block.num << (block.szx + 4);
2132 if (!block.bert)
2133 break;
2134 }
2135 block.num--;
2136 if (updated_block) {
2137 if ((session->block_mode & COAP_BLOCK_SINGLE_BODY) || block.bert) {
2138 p->body_data = coap_block_build_body(p->body_data, length, data,
2139 saved_offset, size2);
2140 if (p->body_data == NULL) {
2141 /* Need to do it block by block */
2142 session->block_mode &= ~(COAP_BLOCK_SINGLE_BODY);
2143 goto block_mode;
2144 }
2145 }
2146 if (block.m || !check_all_blocks_in(&p->rec_blocks,
2147 (size2 + chunk -1) / chunk)) {
2148 /* Not all the payloads of the body have arrived */
2149 size_t len;
2150 coap_pdu_t *pdu;
2151 uint64_t token;
2152
2153 if (block.m) {
2154 block.m = 0;
2155
2156 /* Ask for the next block */
2157 token = STATE_TOKEN_FULL(p->state_token, ++p->retry_counter);
2158 len = coap_encode_var_safe8(buf, sizeof(token), token);
2159 pdu = coap_pdu_duplicate(&p->pdu, session, len, buf, NULL);
2160 if (!pdu)
2161 goto fail_resp;
2162
2163 if (rcvd->type == COAP_MESSAGE_NON)
2164 pdu->type = COAP_MESSAGE_NON; /* Server is using NON */
2165
2166 /* Only sent with the first block */
2168
2169 coap_update_option(pdu, block_opt,
2170 coap_encode_var_safe(buf, sizeof(buf),
2171 ((block.num + 1) << 4) |
2172 (block.m << 3) | block.aszx),
2173 buf);
2174
2175 if (coap_send_internal(session, pdu) == COAP_INVALID_MID)
2176 goto fail_resp;
2177 }
2178 if (session->block_mode & (COAP_BLOCK_SINGLE_BODY) || block.bert)
2179 goto skip_app_handler;
2180
2181 /* need to put back original token into rcvd */
2183 rcvd->body_offset = saved_offset;
2184 rcvd->body_total = size2;
2185 goto call_app_handler;
2186 }
2187 /* need to put back original token into rcvd */
2189 if (session->block_mode & (COAP_BLOCK_SINGLE_BODY) || block.bert) {
2190 /* Pretend that there is no block */
2191 coap_remove_option(rcvd, block_opt);
2192 if (p->observe_set) {
2194 p->observe_length, p->observe);
2195 }
2196 rcvd->body_data = p->body_data->s;
2197 rcvd->body_length = saved_offset + length;
2198 rcvd->body_offset = 0;
2199 rcvd->body_total = rcvd->body_length;
2200 }
2201 else {
2202 rcvd->body_offset = saved_offset;
2203 rcvd->body_total = size2;
2204 }
2205 if (context->response_handler) {
2206 coap_log(LOG_DEBUG, "Client app version of updated PDU\n");
2207 coap_show_pdu(LOG_DEBUG, rcvd);
2208 context->response_handler(session, sent, rcvd, rcvd->mid);
2209 }
2210 app_has_response = 1;
2211 /* Set up for the next data body if observing */
2212 p->initial = 1;
2213 if (p->body_data) {
2215 p->body_data = NULL;
2216 }
2217 else {
2218 goto skip_app_handler;
2219 }
2220 }
2221 else {
2222block_mode:
2223 /* need to put back original token into rcvd */
2225 rcvd->body_offset = saved_offset;
2226 /* slightly oversize if there is more data */
2227 if (block.m) {
2228 if(size2 > saved_offset + length + block.m)
2229 rcvd->body_total = size2;
2230 else
2231 rcvd->body_total = saved_offset + length + block.m;
2232 }
2233 else {
2234 rcvd->body_total = saved_offset + length;
2235 /* Set up for the next data body if observing */
2236 p->initial = 1;
2237 }
2238 if (context->response_handler) {
2239 coap_log(LOG_DEBUG, "Client app version of updated PDU\n");
2240 coap_show_pdu(LOG_DEBUG, rcvd);
2241 context->response_handler(session, sent, rcvd, rcvd->mid);
2242 }
2243 app_has_response = 1;
2244 }
2245 }
2246 else {
2247 coap_opt_t *obs_opt = coap_check_option(rcvd,
2249 &opt_iter);
2250 if (obs_opt) {
2251 p->observe_length = min(coap_opt_length(obs_opt), 3);
2252 memcpy(p->observe, coap_opt_value(obs_opt), p->observe_length);
2253 p->observe_set = 1;
2254 }
2255 else {
2256 p->observe_set = 0;
2257 /* need to put back original token into rcvd */
2259 /* Expire this entry */
2260 LL_DELETE(session->lg_crcv, p);
2261 coap_block_delete_lg_crcv(session, p);
2262 goto call_app_handler;
2263 }
2264 }
2265 } else if (rcvd->code == COAP_RESPONSE_CODE(401)) {
2266 if (check_freshness(session, rcvd, sent, NULL, p))
2267 goto skip_app_handler;
2268 goto fail_resp;
2269 }
2270 if (!block.m && !p->observe_set) {
2271fail_resp:
2272 /* lg_crcv no longer required - cache it */
2273 coap_ticks(&p->last_used);
2274 }
2275 /* need to put back original token into rcvd */
2277 break;
2278 } /* LL_FOREACH() */
2279
2280 /* Check if receiving a block response and if blocks can be set up */
2281 if (recursive == COAP_RECURSE_OK && !p) {
2282 if (!sent) {
2283 if (coap_get_block_b(session, rcvd, COAP_OPTION_BLOCK2, &block)) {
2284 coap_log(LOG_DEBUG, "** %s: large body receive internal issue\n",
2285 coap_session_str(session));
2286 goto skip_app_handler;
2287 }
2288 }
2289 else if (COAP_RESPONSE_CLASS(rcvd->code) == 2) {
2290 if (coap_get_block_b(session, rcvd, COAP_OPTION_BLOCK2, &block)) {
2291 have_block = 1;
2292 block_opt = COAP_OPTION_BLOCK2;
2293 if (block.num != 0) {
2294 /* Assume random access and just give the single response to app */
2295 size_t length;
2296 const uint8_t *data;
2297 size_t chunk = (size_t)1 << (block.szx + 4);
2298
2299 coap_get_data(rcvd, &length, &data);
2300 rcvd->body_offset = block.num*chunk;
2301 rcvd->body_total = block.num*chunk + length + (block.m ? 1 : 0);
2302 goto call_app_handler;
2303 }
2304 }
2305 if (have_block) {
2306 coap_lg_crcv_t *lg_crcv = coap_block_new_lg_crcv(session, sent);
2307
2308 if (lg_crcv) {
2309 uint8_t buf[8];
2310 size_t length = coap_encode_var_safe8(buf,
2311 sizeof(lg_crcv->state_token),
2312 lg_crcv->state_token);
2313 if (coap_update_token(rcvd, length, buf)) {
2314 LL_PREPEND(session->lg_crcv, lg_crcv);
2315 return coap_handle_response_get_block(context, session, sent, rcvd,
2317 }
2318 coap_block_delete_lg_crcv(session, lg_crcv);
2319 }
2320 }
2321 track_echo(session, rcvd);
2322 } else if (rcvd->code == COAP_RESPONSE_CODE(401)) {
2323 coap_lg_crcv_t *lg_crcv = coap_block_new_lg_crcv(session, sent);
2324
2325 if (lg_crcv) {
2326 LL_PREPEND(session->lg_crcv, lg_crcv);
2327 return coap_handle_response_get_block(context, session, sent, rcvd,
2329 }
2330 }
2331 }
2332 return app_has_response;
2333
2334call_app_handler:
2335 return 0;
2336
2337skip_app_handler:
2338 return 1;
2339}
2340#endif /* COAP_CLIENT_SUPPORT */
2341
2342/* Check if lg_xmit generated and update PDU code if so */
2343void
2345 coap_resource_t *resource, coap_string_t *query,
2346 coap_pdu_code_t request_method) {
2347 coap_lg_xmit_t *lg_xmit;
2348 coap_string_t empty = { 0, NULL};
2349
2350 if (response->code == 0)
2351 return;
2352 LL_FOREACH(session->lg_xmit, lg_xmit) {
2353 if (!COAP_PDU_IS_REQUEST(&lg_xmit->pdu) &&
2354 lg_xmit->b.b2.resource == resource &&
2355 lg_xmit->b.b2.request_method == request_method &&
2356 coap_string_equal(query ? query : &empty,
2357 lg_xmit->b.b2.query ? lg_xmit->b.b2.query : &empty)) {
2358 /* lg_xmit found */
2359 if (lg_xmit->pdu.code == 0) {
2360 lg_xmit->pdu.code = response->code;
2361 return;
2362 }
2363 }
2364 }
2365}
COAP_STATIC_INLINE int full_match(const uint8_t *a, size_t alen, const uint8_t *b, size_t blen)
Definition: block.c:370
#define MAX_BLK_LEN
static int coap_add_data_large_internal(coap_session_t *session, coap_pdu_t *pdu, coap_resource_t *resource, const coap_string_t *query, int maxage, uint64_t etag, size_t length, const uint8_t *data, coap_release_large_data_t release_func, void *app_ptr, coap_pdu_code_t request_method)
Definition: block.c:433
#define STATE_TOKEN_FULL(t, r)
Definition: block.c:24
static int check_all_blocks_in(coap_rblock_t *rec_blocks, size_t total_blocks)
Definition: block.c:929
#define STATE_TOKEN_BASE(t)
Definition: block.c:22
static int update_received_blocks(coap_rblock_t *rec_blocks, uint32_t block_num)
Definition: block.c:1345
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: block.c:111
#define min(a, b)
Definition: block.c:19
static int check_if_received_block(coap_rblock_t *rec_blocks, uint32_t block_num)
Definition: block.c:916
unsigned char coap_key_t[4]
Definition: coap_hashkey.h:24
#define coap_hash(String, Length, Result)
Definition: coap_hashkey.h:38
Pulls together all the internal only header files.
uint16_t coap_option_num_t
Definition: coap_option.h:20
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:26
int coap_flsll(long long i)
Definition: encode.c:26
coap_tick_t coap_block_check_lg_srcv_timeouts(coap_session_t *session, coap_tick_t now)
void coap_block_delete_lg_srcv(coap_session_t *session, coap_lg_srcv_t *lg_srcv)
coap_tick_t coap_block_check_lg_xmit_timeouts(coap_session_t *session, coap_tick_t now)
Definition: block.c:866
#define COAP_RBLOCK_CNT
void coap_block_delete_lg_crcv(coap_session_t *session, coap_lg_crcv_t *lg_crcv)
int coap_handle_response_get_block(coap_context_t *context, coap_session_t *session, coap_pdu_t *sent, coap_pdu_t *rcvd, coap_recurse_t recursive)
coap_lg_crcv_t * coap_block_new_lg_crcv(coap_session_t *session, coap_pdu_t *pdu)
int coap_handle_response_send_block(coap_session_t *session, coap_pdu_t *sent, coap_pdu_t *rcvd)
coap_recurse_t
void coap_block_delete_lg_xmit(coap_session_t *session, coap_lg_xmit_t *lg_xmit)
Definition: block.c:1053
void coap_check_code_lg_xmit(coap_session_t *session, coap_pdu_t *response, coap_resource_t *resource, coap_string_t *query, coap_pdu_code_t request_method)
The function checks that the code in a newly formed lg_xmit created by coap_add_data_large_response()...
Definition: block.c:2344
coap_tick_t coap_block_check_lg_crcv_timeouts(coap_session_t *session, coap_tick_t now)
int coap_handle_request_send_block(coap_session_t *session, coap_pdu_t *pdu, coap_pdu_t *response, coap_resource_t *resource, coap_string_t *query)
int coap_handle_request_put_block(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu, coap_pdu_t *response, coap_resource_t *resource, coap_string_t *uri_path, coap_opt_t *observe, coap_string_t *query, coap_method_handler_t h, int *added_block)
@ COAP_RECURSE_OK
@ COAP_RECURSE_NO
void coap_context_set_block_mode(coap_context_t *context, uint8_t block_mode)
Set the context level CoAP block handling bits for handling RFC7959.
Definition: block.c:361
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_OPT_BLOCK_SZX(opt)
Returns the value of the SZX-field of a Block option opt.
Definition: block.h:77
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: block.c:232
#define COAP_BLOCK_SINGLE_BODY
Definition: block.h:62
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: block.c:187
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: block.c:218
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: block.h:276
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: block.c:255
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: block.c:46
#define COAP_OPT_BLOCK_MORE(opt)
Returns the value of the More-bit of a Block option opt.
Definition: block.h:73
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: block.c:27
int coap_get_block(const coap_pdu_t *pdu, coap_option_num_t number, coap_block_t *block)
Initializes block from pdu.
Definition: block.c:94
#define COAP_OPT_BLOCK_LAST(opt)
Returns the value of the least significant byte of a Block option opt.
Definition: block.h:69
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: block.c:154
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.
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.
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.
Definition: block.c:1913
#define COAP_BLOCK_USE_LIBCOAP
Definition: block.h:61
void coap_ticks(coap_tick_t *t)
Sets t to the internal time with COAP_TICKS_PER_SECOND resolution.
time_t coap_time_t
CoAP time in seconds since epoch.
Definition: coap_time.h:132
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition: coap_time.h:127
coap_time_t coap_ticks_to_rt(coap_tick_t t)
Helper function that converts coap ticks to wallclock time.
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition: coap_time.h:142
void(* coap_method_handler_t)(coap_resource_t *, coap_session_t *, const coap_pdu_t *, const coap_string_t *, coap_pdu_t *)
Definition of message handler function.
Definition: resource.h:43
int coap_client_delay_first(coap_session_t *session)
Delay the sending of the first client request until some other negotiation has completed.
Definition: net.c:1045
coap_mid_t coap_send_internal(coap_session_t *session, coap_pdu_t *pdu)
Sends a CoAP message to given peer.
Definition: net.c:1242
int coap_handle_event(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: net.c:3356
unsigned int coap_encode_var_safe(uint8_t *buf, size_t length, unsigned int val)
Encodes multiple-length byte sequences.
Definition: encode.c:45
unsigned int coap_decode_var_bytes(const uint8_t *buf, size_t len)
Decodes multiple-length byte sequences.
Definition: encode.c:36
uint64_t coap_decode_var_bytes8(const uint8_t *buf, size_t len)
Decodes multiple-length byte sequences.
Definition: encode.c:65
unsigned int coap_encode_var_safe8(uint8_t *buf, size_t length, uint64_t val)
Encodes multiple-length byte sequences.
Definition: encode.c:75
@ COAP_EVENT_PARTIAL_BLOCK
(Q-)Block receive errors
Definition: coap_event.h:60
void coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu)
Display the contents of the specified pdu.
Definition: coap_debug.c:523
#define LOG_DEBUG
Definition: coap_debug.h:81
const char * coap_session_str(const coap_session_t *session)
Get session description.
#define LOG_WARNING
Definition: coap_debug.h:72
#define coap_log(level,...)
Logging function.
Definition: coap_debug.h:165
#define COAP_OBSERVE_CANCEL
The value COAP_OBSERVE_CANCEL in a GET/FETCH request option COAP_OPTION_OBSERVE indicates that the ob...
coap_opt_t * coap_option_next(coap_opt_iterator_t *oi)
Updates the iterator oi to point to the next option.
Definition: coap_option.c:152
uint32_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
Definition: coap_option.c:215
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.
Definition: coap_option.c:116
size_t coap_opt_encode_size(uint16_t delta, size_t length)
Compute storage bytes needed for an option with given delta and length.
Definition: coap_option.c:354
#define COAP_OPT_ALL
Pre-defined filter that includes all options.
Definition: coap_option.h:108
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.
Definition: coap_option.c:202
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
Definition: coap_option.c:252
int coap_option_filter_set(coap_opt_filter_t *filter, coap_option_num_t option)
Sets the corresponding entry for number in filter.
Definition: coap_option.c:497
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: pdu.c:466
int coap_remove_option(coap_pdu_t *pdu, coap_option_num_t number)
Removes (first) option of given number from the pdu.
Definition: pdu.c:322
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: pdu.c:288
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: pdu.c:554
#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: pdu.c:604
#define COAP_OPTION_BLOCK2
Definition: pdu.h:128
const char * coap_response_phrase(unsigned char code)
Returns a human-readable response phrase for the specified CoAP response code.
Definition: pdu.c:777
#define COAP_OPTION_CONTENT_FORMAT
Definition: pdu.h:120
#define COAP_OPTION_SIZE2
Definition: pdu.h:130
#define COAP_OPTION_BLOCK1
Definition: pdu.h:129
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition: pdu.h:243
#define COAP_RESPONSE_CODE(N)
Definition: pdu.h:146
#define COAP_RESPONSE_CLASS(C)
Definition: pdu.h:149
coap_pdu_code_t
Set of codes available for a PDU.
Definition: pdu.h:303
#define COAP_OPTION_SIZE1
Definition: pdu.h:133
coap_pdu_type_t
CoAP PDU message type definitions.
Definition: pdu.h:60
#define COAP_MEDIATYPE_TEXT_PLAIN
Definition: pdu.h:195
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: pdu.c:702
#define COAP_OPTION_RTAG
Definition: pdu.h:136
coap_pdu_t * coap_pdu_duplicate(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: pdu.c:161
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: pdu.c:710
#define COAP_INVALID_MID
Indicates an invalid message id.
Definition: pdu.h:246
#define COAP_OPTION_MAXAGE
Definition: pdu.h:123
#define COAP_OPTION_ETAG
Definition: pdu.h:113
#define COAP_OPTION_OBSERVE
Definition: pdu.h:115
#define COAP_OPTION_ECHO
Definition: pdu.h:134
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: pdu.c:671
@ COAP_MESSAGE_NON
Definition: pdu.h:62
#define COAP_EXCHANGE_LIFETIME(s)
The EXCHANGE_LIFETIME definition for the session (s).
#define COAP_PROTO_NOT_RELIABLE(p)
Definition: coap_session.h:36
#define COAP_PROTO_RELIABLE(p)
Definition: coap_session.h:37
void coap_session_new_token(coap_session_t *session, size_t *len, uint8_t *data)
Creates a new token for use.
void coap_delete_bin_const(coap_bin_const_t *s)
Deletes the given const binary data and releases any memory allocated.
Definition: str.c:109
void coap_delete_str_const(coap_str_const_t *s)
Deletes the given const string and releases any memory allocated.
Definition: str.c:58
coap_binary_t * coap_new_binary(size_t size)
Returns a new binary object with at least size bytes storage allocated.
Definition: str.c:72
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: str.c:100
coap_binary_t * coap_resize_binary(coap_binary_t *s, size_t size)
Resizes the given coap_binary_t object.
Definition: str.c:76
void coap_delete_binary(coap_binary_t *s)
Deletes the given coap_binary_t object and releases any memory allocated.
Definition: str.c:96
#define coap_string_equal(string1, string2)
Compares the two strings for equality.
Definition: str.h:189
coap_string_t * coap_new_string(size_t size)
Returns a new string object with at least size+1 bytes storage allocated.
Definition: str.c:20
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: str.c:49
void coap_delete_string(coap_string_t *s)
Deletes the given string and releases any memory allocated.
Definition: str.c:45
#define COAP_STATIC_INLINE
Definition: libcoap.h:45
@ COAP_LG_XMIT
Definition: mem.h:55
@ COAP_LG_CRCV
Definition: mem.h:56
@ COAP_LG_SRCV
Definition: mem.h:57
@ COAP_STRING
Definition: mem.h:37
@ COAP_PDU_BUF
Definition: mem.h:45
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().
COAP_STATIC_INLINE int token_match(const uint8_t *a, size_t alen, const uint8_t *b, size_t blen)
Definition: net.c:1039
CoAP binary data definition.
Definition: str.h:56
size_t length
length of binary data
Definition: str.h:57
uint8_t * s
binary data
Definition: str.h:58
Structure of Block options with BERT support.
Definition: block.h:51
unsigned int num
block number
Definition: block.h:52
uint32_t chunk_size
‍1024 if BERT
Definition: block.h:58
unsigned int bert
Operating as BERT.
Definition: block.h:57
unsigned int aszx
block size (0-7 including BERT
Definition: block.h:55
unsigned int defined
Set if block found.
Definition: block.h:56
unsigned int m
1 if more blocks follow, 0 otherwise
Definition: block.h:53
unsigned int szx
block size (0-6)
Definition: block.h:54
Structure of Block options.
Definition: block.h:42
unsigned int num
block number
Definition: block.h:43
unsigned int szx
block size
Definition: block.h:45
unsigned int m
1 if more blocks follow, 0 otherwise
Definition: block.h:44
The CoAP stack's global state is stored in a coap_context_t object.
uint64_t etag
Next ETag to use.
coap_response_handler_t response_handler
coap_resource_t * proxy_uri_resource
can be used for handling proxy URI resources
uint8_t block_mode
Zero or more COAP_BLOCK_ or'd options.
coap_resource_t * unknown_resource
can be used for handling unknown resources
uint64_t state_token
state token
size_t bert_size
size of last BERT block
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.
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.
Structure to hold large body (many blocks) client receive information.
uint16_t block_option
Block option in use.
uint8_t etag[8]
ETag for block checking.
uint8_t etag_length
ETag length.
uint8_t last_type
Last request type (CON/NON)
uint8_t observe_length
Length of observe data.
uint8_t observe[3]
Observe data (if observe_set) (only 24 bits)
uint8_t etag_set
Set if ETag is in receive PDU.
coap_rblock_t rec_blocks
uint8_t initial
If set, has not been used yet.
uint8_t szx
size of individual blocks
uint16_t content_format
Content format for the set of blocks.
coap_pdu_t pdu
skeletal PDU
coap_tick_t last_used
< list of received blocks
uint64_t state_token
state token (and observe token)
coap_binary_t * app_token
app requesting PDU token
uint16_t retry_counter
Retry counter (part of state token)
coap_binary_t * body_data
Used for re-assembling entire body.
uint8_t observe_set
Set if this is an observe receive PDU.
size_t total_len
Length as indicated by SIZE2 option.
Structure to hold large body (many blocks) server receive information.
uint8_t rtag[8]
RTag for block checking.
coap_mid_t last_mid
Last received mid for this set of packets.
uint8_t last_token[8]
< list of received blocks
uint8_t rtag_set
Set if RTag is in receive PDU.
uint16_t block_option
Block option in use.
size_t total_len
Length as indicated by SIZE1 option.
uint8_t observe_length
Length of observe data.
coap_rblock_t rec_blocks
set to uri_path if unknown resource
coap_binary_t * body_data
Used for re-assembling entire body.
coap_resource_t * resource
associated resource
size_t last_token_length
length of token
uint8_t observe_set
Set if this is an observe receive PDU.
uint8_t rtag_length
RTag length.
uint8_t last_type
Last request type (CON/NON)
uint8_t szx
size of individual blocks
size_t amount_so_far
Amount of data seen so far.
coap_tick_t last_used
Last time data sent or 0.
uint8_t observe[3]
Observe data (if set) (only 24 bits)
uint16_t content_format
Content format for the set of blocks.
coap_str_const_t * uri_path
Structure to hold large body (many blocks) transmission information.
coap_tick_t last_all_sent
Last time all data sent or 0.
coap_release_large_data_t release_func
large data de-alloc function
uint8_t blk_size
large block transmission size
union coap_lg_xmit_t::@1 b
const uint8_t * data
large data ptr
int last_block
last acknowledged block number
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 pdu
skeletal PDU
size_t length
large data length
coap_l_block1_t b1
coap_l_block2_t b2
uint16_t option
large block transmisson CoAP option
void * app_ptr
applicaton provided ptr for de-alloc function
coap_tick_t last_obs
Last time used (Observe tracking) or 0.
Iterator to run through PDU options.
Definition: coap_option.h:171
coap_option_num_t number
decoded option number
Definition: coap_option.h:173
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
uint8_t * token
first byte of token, if any, or options
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)
uint8_t token_length
length of Token
uint8_t hdr_size
actual size used for protocol-specific header
uint8_t * data
first byte of payload, if any
coap_mid_t mid
message id, if any, in regular host byte order
size_t used_size
used bytes of storage for token, options and payload
size_t body_total
Holds body data total size.
coap_pdu_type_t type
message type
Structure to keep track of received blocks.
coap_tick_t last_seen
struct coap_lg_range range[COAP_RBLOCK_CNT]
Abstraction of resource that can be attached to coap_context_t.
coap_str_const_t * uri_path
Request URI Path for this resource.
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
uint8_t csm_bert_rem_support
CSM TCP BERT blocks supported (remote)
uint64_t tx_token
Next token number to use.
uint8_t block_mode
Zero or more COAP_BLOCK_ or'd options.
uint8_t csm_bert_loc_support
CSM TCP BERT blocks supported (local)
coap_proto_t proto
protocol used
uint32_t tx_rtag
Next Request-Tag number to use.
coap_lg_srcv_t * lg_srcv
Server list of expected large receives.
coap_lg_crcv_t * lg_crcv
Client list of expected large receives.
coap_context_t * context
session's context
coap_bin_const_t * echo
last token used to make a request
const uint8_t * s
read-only string data
Definition: str.h:48
size_t length
length of string
Definition: str.h:47
CoAP string data definition.
Definition: str.h:38
uint8_t * s
string data
Definition: str.h:40
size_t length
length of string
Definition: str.h:39