libcoap  4.3.0rc3
block.c
Go to the documentation of this file.
1 /* block.c -- block transfer
2  *
3  * Copyright (C) 2010--2012,2015-2019 Olaf Bergmann <bergmann@tzi.org> and others
4  *
5  * This file is part of the CoAP library libcoap. Please see
6  * README for terms of use.
7  */
8 
9 #include "coap3/coap_internal.h"
10 
11 #ifndef min
12 #define min(a,b) ((a) < (b) ? (a) : (b))
13 #endif
14 
15 unsigned int
16 coap_opt_block_num(const coap_opt_t *block_opt) {
17  unsigned int num = 0;
18  uint16_t len;
19 
20  len = coap_opt_length(block_opt);
21 
22  if (len == 0) {
23  return 0;
24  }
25 
26  if (len > 1) {
27  num = coap_decode_var_bytes(coap_opt_value(block_opt),
28  coap_opt_length(block_opt) - 1);
29  }
30 
31  return (num << 4) | ((*COAP_OPT_BLOCK_LAST(block_opt) & 0xF0) >> 4);
32 }
33 
34 int
36  coap_block_t *block) {
37  coap_opt_iterator_t opt_iter;
38  coap_opt_t *option;
39 
40  assert(block);
41  memset(block, 0, sizeof(coap_block_t));
42 
43  if (pdu && (option = coap_check_option(pdu, number, &opt_iter)) != NULL) {
44  unsigned int num;
45 
46  block->szx = COAP_OPT_BLOCK_SZX(option);
47  if (COAP_OPT_BLOCK_MORE(option))
48  block->m = 1;
49 
50  /* The block number is at most 20 bits, so values above 2^20 - 1
51  * are illegal. */
52  num = coap_opt_block_num(option);
53  if (num > 0xFFFFF) {
54  return 0;
55  }
56  block->num = num;
57  return 1;
58  }
59 
60  return 0;
61 }
62 
63 int
65  coap_pdu_t *pdu, size_t data_length) {
66  size_t start, want, avail;
67  unsigned char buf[4];
68 
69  assert(pdu);
70 
71  start = block->num << (block->szx + 4);
72  if (block->num != 0 && data_length <= start) {
73  coap_log(LOG_DEBUG, "illegal block requested\n");
74  return -2;
75  }
76 
77  assert(pdu->max_size > 0);
78  avail = pdu->max_size - pdu->used_size - pdu->hdr_size;
79  want = (size_t)1 << (block->szx + 4);
80 
81  /* check if entire block fits in message */
82  if (want <= avail) {
83  block->m = want < data_length - start;
84  } else {
85  /* Sender has requested a block that is larger than the remaining
86  * space in pdu. This is ok if the remaining data fits into the pdu
87  * anyway. The block size needs to be adjusted only if there is more
88  * data left that cannot be delivered in this message. */
89 
90  if (data_length - start <= avail) {
91 
92  /* it's the final block and everything fits in the message */
93  block->m = 0;
94  } else {
95  unsigned int szx;
96  int newBlockSize;
97 
98  /* we need to decrease the block size */
99  if (avail < 16) { /* bad luck, this is the smallest block size */
101  "not enough space, even the smallest block does not fit\n");
102  return -3;
103  }
104  newBlockSize = coap_flsll((long long)avail) - 5;
106  "decrease block size for %zu to %d\n", avail, newBlockSize);
107  szx = block->szx;
108  block->szx = newBlockSize;
109  block->m = 1;
110  block->num <<= szx - block->szx;
111  }
112  }
113 
114  /* to re-encode the block option */
115  coap_insert_option(pdu, number, coap_encode_var_safe(buf, sizeof(buf),
116  ((block->num << 4) |
117  (block->m << 3) |
118  block->szx)),
119  buf);
120 
121  return 1;
122 }
123 
124 int
125 coap_add_block(coap_pdu_t *pdu, size_t len, const uint8_t *data,
126  unsigned int block_num, unsigned char block_szx) {
127  unsigned int start;
128  start = block_num << (block_szx + 4);
129 
130  if (len <= start)
131  return 0;
132 
133  return coap_add_data(pdu,
134  min(len - start, ((size_t)1 << (block_szx + 4))),
135  data + start);
136 }
137 
138 /*
139  * Note that the COAP_OPTION_ have to be added in the correct order
140  */
141 void
143  coap_pdu_t *response,
144  uint16_t media_type,
145  int maxage,
146  size_t length,
147  const uint8_t* data
148 ) {
149  coap_key_t etag;
150  unsigned char buf[4];
151  coap_block_t block2 = { 0, 0, 0 };
152  int block2_requested = 0;
153 
154  /*
155  * Need to check that a valid block is getting asked for so that the
156  * correct options are put into the PDU.
157  */
158  if (request) {
159  if (coap_get_block(request, COAP_OPTION_BLOCK2, &block2)) {
160  block2_requested = 1;
161  if (block2.num != 0 && length <= (block2.num << (block2.szx + 4))) {
162  coap_log(LOG_DEBUG, "Illegal block requested (%d > last = %zu)\n",
163  block2.num,
164  length >> (block2.szx + 4));
165  response->code = COAP_RESPONSE_CODE(400);
166  goto error;
167  }
168  }
169  }
170  response->code = COAP_RESPONSE_CODE(205);
171 
172  /* add etag for the resource */
173  memset(etag, 0, sizeof(etag));
174  coap_hash(data, length, etag);
175  coap_add_option(response, COAP_OPTION_ETAG, sizeof(etag), etag);
176 
178  coap_encode_var_safe(buf, sizeof(buf),
179  media_type),
180  buf);
181 
182  if (maxage >= 0) {
183  coap_insert_option(response,
185  coap_encode_var_safe(buf, sizeof(buf), maxage), buf);
186  }
187 
188  if (block2_requested) {
189  int res;
190 
191  res = coap_write_block_opt(&block2, COAP_OPTION_BLOCK2, response, length);
192 
193  switch (res) {
194  case -2: /* illegal block (caught above) */
195  response->code = COAP_RESPONSE_CODE(400);
196  goto error;
197  case -1: /* should really not happen */
198  assert(0);
199  /* fall through if assert is a no-op */
200  case -3: /* cannot handle request */
201  response->code = COAP_RESPONSE_CODE(500);
202  goto error;
203  default: /* everything is good */
204  ;
205  }
206 
207  coap_add_option(response,
209  coap_encode_var_safe8(buf, sizeof(buf), length),
210  buf);
211 
212  coap_add_block(response, length, data,
213  block2.num, block2.szx);
214  return;
215  }
216 
217  /*
218  * BLOCK2 not requested
219  */
220  if (!coap_add_data(response, length, data)) {
221  /*
222  * Insufficient space to add in data - use block mode
223  * set initial block size, will be lowered by
224  * coap_write_block_opt() automatically
225  */
226  block2.num = 0;
227  block2.szx = 6;
228  coap_write_block_opt(&block2, COAP_OPTION_BLOCK2, response, length);
229 
230  coap_add_option(response,
232  coap_encode_var_safe8(buf, sizeof(buf), length),
233  buf);
234 
235  coap_add_block(response, length, data,
236  block2.num, block2.szx);
237  }
238  return;
239 
240 error:
241  coap_add_data(response,
242  strlen(coap_response_phrase(response->code)),
243  (const unsigned char *)coap_response_phrase(response->code));
244 }
245 
246 void
248  uint8_t block_mode) {
249  context->block_mode = block_mode &= (COAP_BLOCK_USE_LIBCOAP |
251  if (!(block_mode & COAP_BLOCK_USE_LIBCOAP))
252  context->block_mode = 0;
253 }
254 
255 /*
256  * The block token match only matches on the bottom 32 bits
257  * [The upper 32 bits are incremented as different payloads are sent]
258  *
259  */
261 block_token_match(const uint8_t *a, size_t alen,
262  const uint8_t *b, size_t blen) {
263  size_t bias;
264  if (blen < 4)
265  return alen == blen && memcmp(a, b, blen) == 0;
266  bias = blen - 4;
267  return alen == blen && memcmp(a+bias, b+bias, 4) == 0;
268 }
269 
271 full_match(const uint8_t *a, size_t alen,
272  const uint8_t *b, size_t blen) {
273  return alen == blen && (alen == 0 || memcmp(a, b, alen) == 0);
274 }
275 
276 int
278  coap_pdu_type_t type) {
279  coap_lg_crcv_t *cq;
280 
281  assert(session);
282  if (!session)
283  return 0;
284 
285  LL_FOREACH(session->lg_crcv, cq) {
286  if (cq->observe_set) {
287  if ((!token && !cq->app_token->length) || (token &&
288  full_match(token->s, token->length, cq->app_token->s,
289  cq->app_token->length))) {
290  uint8_t buf[4];
291  coap_mid_t mid;
292  coap_pdu_t * pdu = coap_pdu_duplicate(&cq->pdu,
293  session,
294  cq->base_token_length,
295  cq->base_token,
296  NULL);
297 
298  cq->observe_set = 0;
299  if (pdu == NULL)
300  return 0;
301  /* Need to make sure that this is the correct type */
302  pdu->type = type;
303 
305  coap_encode_var_safe(buf, sizeof(buf),
307  buf);
308  mid = coap_send(session, pdu);
309  if (mid != COAP_INVALID_MID)
310  return 1;
311  break;
312  }
313  }
314  }
315  return 0;
316 }
317 
318 int
320  coap_pdu_t *pdu,
321  coap_resource_t *resource,
322  const coap_string_t *query,
323  int maxage,
324  uint64_t etag,
325  size_t length,
326  const uint8_t *data,
327  coap_release_large_data_t release_func,
328  void *app_ptr) {
329 
330  ssize_t avail;
331  coap_block_t block;
332  size_t chunk;
333  coap_lg_xmit_t *lg_xmit = NULL;
334  uint8_t buf[8];
335  int have_block_defined = 0;
336  uint8_t blk_size;
337  uint16_t option;
338 
339  assert(pdu);
340 
341  if (!(session->block_mode & COAP_BLOCK_USE_LIBCOAP)) {
343  "** %s: coap_send_large: COAP_BLOCK_USE_LIBCOAP not enabled\n",
344  coap_session_str(session));
345  goto add_data;
346  }
347 
348 /* Block NUM max 20 bits and block size is "2**(SZX + 4)" and SZX max of 6 */
349 #define MAX_BLK_LEN (((1 << 20) - 1) * (1 << (6 + 4)))
350 
351  if (length > MAX_BLK_LEN) {
353  "Size of large buffer restricted to 0x%x bytes\n", MAX_BLK_LEN);
354  length = MAX_BLK_LEN;
355  }
356  /* Determine the block size to use, adding in sensible options if needed */
357  if (COAP_PDU_IS_REQUEST(pdu)) {
358  coap_lg_xmit_t *q;
359 
360  option = COAP_OPTION_BLOCK1;
361 
362  /* See if this token is already in use for large bodies (unlikely) */
363  LL_FOREACH_SAFE(session->lg_xmit, lg_xmit, q) {
364  if (full_match(pdu->token, pdu->token_length,
365  lg_xmit->b.b1.app_token->s,
366  lg_xmit->b.b1.app_token->length)) {
367  /* Unfortunately need to free this off as potential size change */
368  LL_DELETE(session->lg_xmit, lg_xmit);
369  coap_block_delete_lg_xmit(session, lg_xmit);
370  lg_xmit = NULL;
371  break;
372  }
373  }
374  }
375  else {
376  /* Have to assume that it is a response even if code is 0.00 */
377  coap_lg_xmit_t *q;
378  coap_string_t empty = { 0, NULL};
379 
380  assert(resource);
381  option = COAP_OPTION_BLOCK2;
382 
383  /* Check if resource+query is already in use for large bodies (unlikely) */
384  LL_FOREACH_SAFE(session->lg_xmit, lg_xmit, q) {
385  if (resource == lg_xmit->b.b2.resource &&
386  coap_string_equal(query ? query : &empty,
387  lg_xmit->b.b2.query ? lg_xmit->b.b2.query : &empty)) {
388  /* Unfortunately need to free this off as potential size change */
389  LL_DELETE(session->lg_xmit, lg_xmit);
390  coap_block_delete_lg_xmit(session, lg_xmit);
391  lg_xmit = NULL;
392  break;
393  }
394  }
395  }
396 
397  avail = pdu->max_size - pdu->used_size - pdu->hdr_size;
398  /* May need token of length 8, so account for this */
399  avail -= (pdu->token_length <= 8) ? pdu->token_length <= 8 : 0;
400  blk_size = coap_flsll((long long)avail) - 4 - 1;
401 
402  /* see if BLOCKx defined - if so update blk_size as given by app */
403  if (coap_get_block(pdu, option, &block)) {
404  if (block.szx < blk_size)
405  blk_size = block.szx;
406  have_block_defined = 1;
407  }
408 
409  if (avail < 16 && ((ssize_t)length > avail || have_block_defined)) {
410  /* bad luck, this is the smallest block size */
412  "not enough space, even the smallest block does not fit\n");
413  goto fail;
414  }
415 
416  chunk = (size_t)1 << (blk_size + 4);
417  if (have_block_defined && block.num != 0) {
418  /* App is defining a single block to send */
419  size_t rem;
420 
421  pdu->body_data = data;
422  pdu->body_length = length;
423  coap_log(LOG_DEBUG, "PDU presented by app\n");
424  coap_show_pdu(LOG_DEBUG, pdu);
425  pdu->body_data = NULL;
426  pdu->body_length = 0;
427  if (length >= block.num * chunk) {
428  rem = chunk;
429  if (chunk > length - block.num * chunk)
430  rem = length - block.num * chunk;
431  if (!coap_add_data(pdu, rem, &data[block.num * chunk]))
432  goto fail;
433  }
434  if (release_func)
435  release_func(session, app_ptr);
436  }
437  else if ((have_block_defined && length > chunk) || (ssize_t)length > avail) {
438  /* Only add in lg_xmit if more than one block needs to be handled */
439  uint64_t token;
440  size_t rem;
441 
442  lg_xmit = coap_malloc_type(COAP_LG_XMIT, sizeof(coap_lg_xmit_t));
443  if (!lg_xmit)
444  goto fail;
445 
446  coap_log(LOG_DEBUG, "** %s: lg_xmit %p initialized\n",
447  coap_session_str(session), (void*)lg_xmit);
448  /* Set up for displaying all the data in the pdu */
449  pdu->body_data = data;
450  pdu->body_length = length;
451  coap_log(LOG_DEBUG, "PDU presented by app\n");
452  coap_show_pdu(LOG_DEBUG, pdu);
453  pdu->body_data = NULL;
454  pdu->body_length = 0;
455  /* Update lg_xmit with large data information */
456  lg_xmit->blk_size = blk_size;
457  lg_xmit->option = option;
458  lg_xmit->data = data;
459  lg_xmit->length = length;
460  lg_xmit->offset = 0;
461  lg_xmit->release_func = release_func;
462  lg_xmit->last_payload = 0;
463  lg_xmit->last_used = 0;
464  lg_xmit->app_ptr = app_ptr;
465  if (COAP_PDU_IS_REQUEST(pdu)) {
466  /* Need to keep original token for updating response PDUs */
467  lg_xmit->b.b1.app_token = coap_new_binary(pdu->token_length);
468  if (!lg_xmit->b.b1.app_token)
469  goto fail;
470  memcpy(lg_xmit->b.b1.app_token->s, pdu->token, pdu->token_length);
471  /*
472  * Need to set up new token for use during transmits
473  */
474  lg_xmit->b.b1.count = 1;
475  token = ((++session->tx_token) & 0xffffffff) +
476  ((uint64_t)lg_xmit->b.b1.count << 32);
477  memset(lg_xmit->b.b1.token, 0, sizeof(lg_xmit->b.b1.token));
478  lg_xmit->b.b1.token_length = coap_encode_var_safe8(lg_xmit->b.b1.token,
479  sizeof(token), token);
480  /*
481  * Token will be updated in pdu later as original pdu may be needed in
482  * coap_send_large()
483  */
484  coap_update_option(pdu,
486  coap_encode_var_safe(buf, sizeof(buf),
487  (unsigned int)length),
488  buf);
489  }
490  else {
491  /*
492  * resource+query match is used for BLOCK2 large body transmissions
493  * token match is used for BLOCK1 large body transmissions
494  */
495  lg_xmit->b.b2.resource = resource;
496  if (query) {
497  lg_xmit->b.b2.query = coap_new_string(query->length);
498  if (lg_xmit->b.b2.query) {
499  memcpy(lg_xmit->b.b2.query->s, query->s, query->length);
500  }
501  }
502  else {
503  lg_xmit->b.b2.query = NULL;
504  }
505  lg_xmit->b.b2.etag = etag;
506  if (maxage >= 0) {
507  coap_tick_t now;
508 
509  coap_ticks(&now);
510  lg_xmit->b.b2.maxage_expire = coap_ticks_to_rt(now) + maxage;
511  }
512  else {
513  lg_xmit->b.b2.maxage_expire = 0;
514  }
515  coap_update_option(pdu,
517  coap_encode_var_safe(buf, sizeof(buf),
518  (unsigned int)length),
519  buf);
520  if (etag == 0) {
521  if (++session->context->etag == 0)
522  ++session->context->etag;
523  etag = session->context->etag;
524  }
525  coap_update_option(pdu,
527  coap_encode_var_safe8(buf, sizeof(buf), etag),
528  buf);
529  }
530 
531  /* Add in with requested block num, more bit and block size */
532  block.m = ((block.num + 1) * chunk) < lg_xmit->length;
533  coap_update_option(pdu,
534  lg_xmit->option,
535  coap_encode_var_safe(buf, sizeof(buf),
536  (block.num << 4) | (block.m << 3) | lg_xmit->blk_size),
537  buf);
538 
539  /* Set up skeletal PDU to use as a basis for all the subsequent blocks */
540  memcpy(&lg_xmit->pdu, pdu, sizeof(lg_xmit->pdu));
541  lg_xmit->pdu.token = coap_malloc_type(COAP_PDU_BUF,
542  8 + lg_xmit->pdu.used_size + lg_xmit->pdu.hdr_size);
543  if (!lg_xmit->pdu.token)
544  goto fail;
545 
546  lg_xmit->pdu.alloc_size = 8 + lg_xmit->pdu.used_size +
547  lg_xmit->pdu.hdr_size;
548  lg_xmit->pdu.token += lg_xmit->pdu.hdr_size;
549  memcpy(lg_xmit->pdu.token, pdu->token, lg_xmit->pdu.used_size);
550  if (pdu->data)
551  lg_xmit->pdu.data = lg_xmit->pdu.token + (pdu->data - pdu->token);
552 
553  /* Check we still have space after adding in some options */
554  avail = pdu->max_size - pdu->used_size - pdu->hdr_size;
555  /* May need token of length 8, so account for this */
556  avail -= (pdu->token_length <= 8) ? pdu->token_length <= 8 : 0;
557  if (avail < (ssize_t)chunk) {
558  /* chunk size change down */
559  if (avail < 16) {
561  "not enough space, even the smallest block does not fit\n");
562  goto fail;
563  }
564  blk_size = coap_flsll((long long)avail) - 4 - 1;
565  block.num = block.num << (lg_xmit->blk_size - blk_size);
566  lg_xmit->blk_size = blk_size;
567  chunk = (size_t)1 << (lg_xmit->blk_size + 4);
568  coap_update_option(pdu,
569  lg_xmit->option,
570  coap_encode_var_safe(buf, sizeof(buf),
571  (block.num << 4) | (block.m << 3) | lg_xmit->blk_size),
572  buf);
573  }
574 
575  rem = chunk;
576  if (chunk > lg_xmit->length - block.num * chunk)
577  rem = lg_xmit->length - block.num * chunk;
578  if (!coap_add_data(pdu, rem, &data[block.num * chunk]))
579  goto fail;
580 
581  lg_xmit->last_block = -1;
582 
583  /* Link the new lg_xmit in */
584  LL_PREPEND(session->lg_xmit,lg_xmit);
585  }
586  else {
587  /* No need to use blocks */
588  if (have_block_defined) {
589  coap_update_option(pdu,
590  option,
591  coap_encode_var_safe(buf, sizeof(buf),
592  (0 << 4) | (0 << 3) | blk_size), buf);
593  }
594 add_data:
595  if (!coap_add_data(pdu, length, data))
596  goto fail;
597 
598  if (release_func)
599  release_func(session, app_ptr);
600  }
601  return 1;
602 
603 fail:
604  if (lg_xmit) {
605  coap_block_delete_lg_xmit(session, lg_xmit);
606  }
607  if (release_func)
608  release_func(session, app_ptr);
609  return 0;
610 }
611 
612 int
614  coap_pdu_t *pdu,
615  size_t length,
616  const uint8_t *data,
617  coap_release_large_data_t release_func,
618  void *app_ptr) {
619  return coap_add_data_large_internal(session, pdu, NULL, NULL, -1,
620  0, length, data, release_func, app_ptr);
621 }
622 
623 int
625  coap_session_t *session,
626  const coap_pdu_t *request,
627  coap_pdu_t *response,
628  const coap_string_t *query,
629  uint16_t media_type,
630  int maxage,
631  uint64_t etag,
632  size_t length,
633  const uint8_t *data,
634  coap_release_large_data_t release_func,
635  void *app_ptr
636 ) {
637  unsigned char buf[4];
638  coap_block_t block = { 0, 0, 0 };
639  int block_requested = 0;
640  uint16_t block_opt = COAP_OPTION_BLOCK2;
641 
642  /*
643  * Need to check that a valid block is getting asked for so that the
644  * correct options are put into the PDU.
645  */
646  if (request) {
647  if (coap_get_block(request, COAP_OPTION_BLOCK2, &block)) {
648  block_requested = 1;
649  if (block.num != 0 && length <= (block.num << (block.szx + 4))) {
650  coap_log(LOG_DEBUG, "Illegal block requested (%d > last = %zu)\n",
651  block.num,
652  length >> (block.szx + 4));
653  response->code = COAP_RESPONSE_CODE(400);
654  goto error;
655  }
656  }
657  }
658 
660  coap_encode_var_safe(buf, sizeof(buf),
661  media_type),
662  buf);
663 
664  if (maxage >= 0) {
665  coap_insert_option(response,
667  coap_encode_var_safe(buf, sizeof(buf), maxage), buf);
668  }
669 
670  if (block_requested) {
671  int res;
672 
673  res = coap_write_block_opt(&block, block_opt, response,
674  length);
675 
676  switch (res) {
677  case -2: /* illegal block (caught above) */
678  response->code = COAP_RESPONSE_CODE(400);
679  goto error;
680  case -1: /* should really not happen */
681  assert(0);
682  /* fall through if assert is a no-op */
683  case -3: /* cannot handle request */
684  response->code = COAP_RESPONSE_CODE(500);
685  goto error;
686  default: /* everything is good */
687  ;
688  }
689 
690  if (!coap_add_data_large_internal(session, response, resource, query,
691  maxage, etag, length, data,
692  release_func, app_ptr)) {
693  response->code = COAP_RESPONSE_CODE(500);
694  goto error;
695  }
696 
697  return 1;
698  }
699 
700  /*
701  * BLOCK2 not requested
702  */
703  if (!coap_add_data_large_internal(session, response, resource, query, maxage,
704  etag, length, data, release_func,
705  app_ptr)) {
706  response->code = COAP_RESPONSE_CODE(400);
707  goto error;
708  }
709 
710  return 1;
711 
712 error:
713  coap_add_data(response,
714  strlen(coap_response_phrase(response->code)),
715  (const unsigned char *)coap_response_phrase(response->code));
716  return 0;
717 }
718 
721  coap_lg_crcv_t *p;
722  coap_lg_crcv_t *q;
723  coap_tick_t partial_timeout = COAP_EXCHANGE_LIFETIME(session);
724  coap_tick_t tim_rem = -1;
725 
726  LL_FOREACH_SAFE(session->lg_crcv, p, q) {
727  if (!p->observe_set && p->last_used &&
728  p->last_used + partial_timeout <= now) {
729  /* Expire this entry */
730  LL_DELETE(session->lg_crcv, p);
731  coap_block_delete_lg_crcv(session, p);
732  }
733  else if (!p->observe_set && p->last_used) {
734  /* Delay until the lg_crcv needs to expire */
735  if (tim_rem > p->last_used + partial_timeout - now)
736  tim_rem = p->last_used + partial_timeout - now;
737  }
738  }
739  return tim_rem;
740 }
741 
742 static int
743 check_if_received_block(coap_rblock_t *rec_blocks, uint32_t block_num) {
744  uint32_t i;
745 
746  for (i = 0; i < rec_blocks->used; i++) {
747  if (block_num < rec_blocks->range[i].begin)
748  return 0;
749  if (block_num <= rec_blocks->range[i].end)
750  return 1;
751  }
752  return 0;
753 }
754 
755 static int
756 check_all_blocks_in(coap_rblock_t *rec_blocks, size_t total_blocks) {
757  uint32_t i;
758  uint32_t block = 0;
759 
760  for (i = 0; i < rec_blocks->used; i++) {
761  if (block < rec_blocks->range[i].begin)
762  return 0;
763  if (block < rec_blocks->range[i].end)
764  block = rec_blocks->range[i].end;
765  }
766  /* total_blocks counts from 1 */
767  if (block + 1 < total_blocks)
768  return 0;
769 
770  return 1;
771 }
772 
775  coap_lg_srcv_t *p;
776  coap_lg_srcv_t *q;
777  coap_tick_t partial_timeout = COAP_EXCHANGE_LIFETIME(session);
778  coap_tick_t tim_rem = -1;
779 
780  LL_FOREACH_SAFE(session->lg_srcv, p, q) {
781  if (p->last_used && p->last_used + partial_timeout <= now) {
782  /* Expire this entry */
783  LL_DELETE(session->lg_srcv, p);
784  coap_block_delete_lg_srcv(session, p);
785  }
786  else if (p->last_used) {
787  /* Delay until the lg_srcv needs to expire */
788  if (tim_rem > p->last_used + partial_timeout - now)
789  tim_rem = p->last_used + partial_timeout - now;
790  }
791  }
792  return tim_rem;
793 }
794 
797  coap_lg_crcv_t *lg_crcv;
798 
799  lg_crcv = coap_malloc_type(COAP_LG_CRCV, sizeof(coap_lg_crcv_t));
800 
801  if (lg_crcv == NULL)
802  return NULL;
803 
804  coap_log(LOG_DEBUG, "** %s: lg_crcv %p initialized\n",
805  coap_session_str(session), (void*)lg_crcv);
806  memset(lg_crcv, 0, sizeof(coap_lg_crcv_t));
807  lg_crcv->initial = 1;
808  /* Set up skeletal PDU to use as a basis for all the subsequent blocks */
809  memcpy(&lg_crcv->pdu, pdu, sizeof(lg_crcv->pdu));
811  lg_crcv->pdu.alloc_size + lg_crcv->pdu.hdr_size);
812  if (!lg_crcv->pdu.token) {
813  coap_block_delete_lg_crcv(session, lg_crcv);
814  return NULL;
815  }
816  lg_crcv->pdu.token += lg_crcv->pdu.hdr_size;
817  memcpy(lg_crcv->pdu.token, pdu->token, lg_crcv->pdu.used_size);
818  if (lg_crcv->pdu.data)
819  lg_crcv->pdu.data = lg_crcv->pdu.token + (pdu->data - pdu->token);
820  /* Check that there is space for increased token + option change */
821  if (lg_crcv->pdu.max_size < lg_crcv->pdu.used_size + 9)
822  lg_crcv->pdu.max_size = lg_crcv->pdu.used_size + 9;
823 
824  assert(pdu->token_length <= 8);
825  lg_crcv->token_length = min(pdu->token_length, 8);
826  memset(lg_crcv->token, 0, sizeof(lg_crcv->token));
827  memcpy(lg_crcv->token, pdu->token, lg_crcv->token_length);
828 
829  /* Need to keep original token for handling observe responses */
830  memset(lg_crcv->base_token, 0, sizeof(lg_crcv->base_token));
831  memcpy(lg_crcv->base_token, pdu->token, lg_crcv->token_length);
832  lg_crcv->base_token_length = lg_crcv->token_length;
833 
834  /* Need to keep original token for updating response PDUs */
835  lg_crcv->app_token = coap_new_binary(lg_crcv->token_length);
836  if (!lg_crcv->app_token) {
837  coap_block_delete_lg_crcv(session, lg_crcv);
838  return NULL;
839  }
840  memcpy(lg_crcv->app_token->s, pdu->token, lg_crcv->token_length);
841  /* In case it is there - must not be in continuing request PDUs */
843 
844  return lg_crcv;
845 }
846 
847 void
849  coap_lg_crcv_t *lg_crcv) {
850  if (lg_crcv == NULL)
851  return;
852 
853  if (lg_crcv->pdu.token)
854  coap_free_type(COAP_PDU_BUF, lg_crcv->pdu.token - lg_crcv->pdu.hdr_size);
856  coap_log(LOG_DEBUG, "** %s: lg_crcv %p released\n",
857  coap_session_str(session), (void*)lg_crcv);
858  coap_delete_binary(lg_crcv->app_token);
859  coap_free_type(COAP_LG_CRCV, lg_crcv);
860 }
861 
862 void
864  coap_lg_srcv_t *lg_srcv) {
865  if (lg_srcv == NULL)
866  return;
867 
870  coap_log(LOG_DEBUG, "** %s: lg_srcv %p released\n",
871  coap_session_str(session), (void*)lg_srcv);
872  coap_free_type(COAP_LG_SRCV, lg_srcv);
873 }
874 
875 void
877  coap_lg_xmit_t *lg_xmit) {
878  if (lg_xmit == NULL)
879  return;
880 
881  if (lg_xmit->release_func) {
882  lg_xmit->release_func(session, lg_xmit->app_ptr);
883  }
884  if (lg_xmit->pdu.token) {
885  coap_free_type(COAP_PDU_BUF, lg_xmit->pdu.token - lg_xmit->pdu.hdr_size);
886  }
887  if (COAP_PDU_IS_REQUEST(&lg_xmit->pdu))
888  coap_delete_binary(lg_xmit->b.b1.app_token);
889  else
890  coap_delete_string(lg_xmit->b.b2.query);
891 
892  coap_log(LOG_DEBUG, "** %s: lg_xmit %p released\n",
893  coap_session_str(session), (void*)lg_xmit);
894  coap_free_type(COAP_LG_XMIT, lg_xmit);
895 }
896 
897 static int
898 add_block_send(uint32_t num, uint32_t *out_blocks,
899  uint32_t *count, uint32_t max_count) {
900  uint32_t i;
901 
902  for (i = 0; i < *count && *count < max_count; i++) {
903  if (num == out_blocks[i])
904  return 0;
905  else if (num < out_blocks[i]) {
906  if (*count - i > 1)
907  memmove(&out_blocks[i], &out_blocks[i+1], *count - i -1);
908  out_blocks[i] = num;
909  (*count)++;
910  return 1;
911  }
912  }
913  if (*count < max_count) {
914  out_blocks[i] = num;
915  (*count)++;
916  return 1;
917  }
918  return 0;
919 }
920 
921 /*
922  * Need to see if this is a request for the next block of a large body
923  * transfer. If so, need to initiate the response with the next blocks
924  * and not trouble the application.
925  *
926  * If additional responses needed, then these are expicitly sent out and
927  * 'response' is updated to be the last response to be sent.
928  *
929  * This is set up using coap_add_data_response_large()
930  *
931  * Server is sending a large data response to GET / observe (BLOCK2)
932  *
933  * Return: 0 Call application handler
934  * 1 Do not call application handler - just send the built response
935  */
936 int
938  coap_pdu_t *pdu,
939  coap_pdu_t *response,
940  coap_resource_t *resource,
941  coap_string_t *query) {
942  coap_lg_xmit_t *p;
943  coap_block_t block;
944  uint16_t block_opt = 0;
945  uint32_t out_blocks[1];
946  const char *error_phrase;
947 
948  if (coap_get_block(pdu, COAP_OPTION_BLOCK2, &block)) {
949  block_opt = COAP_OPTION_BLOCK2;
950  }
951  LL_FOREACH(session->lg_xmit, p) {
952  size_t chunk;
953  coap_opt_iterator_t opt_iter;
954  coap_opt_iterator_t opt_b_iter;
955  coap_opt_t *option;
956  uint32_t request_cnt, i;
957  coap_opt_t *etag_opt = NULL;
958  coap_pdu_t *out_pdu = response;
959  static coap_string_t empty = { 0, NULL};
960 
961  if (COAP_PDU_IS_REQUEST(&p->pdu) || resource != p->b.b2.resource ||
962  !coap_string_equal(query ? query : &empty,
963  p->b.b2.query ? p->b.b2.query : &empty)) {
964  /* try out the next one */
965  continue;
966  }
967  etag_opt = coap_check_option(pdu, COAP_OPTION_ETAG, &opt_iter);
968  if (etag_opt) {
969  uint64_t etag = coap_decode_var_bytes8(coap_opt_value(etag_opt),
970  coap_opt_length(etag_opt));
971  if (etag != p->b.b2.etag) {
972  /* try out the next one */
973  continue;
974  }
975  out_pdu->code = COAP_RESPONSE_CODE(203);
976  return 1;
977  }
978  else {
979  out_pdu->code = p->pdu.code;
980  }
981 
982  /* lg_xmit (response) found */
983 
984  chunk = (size_t)1 << (p->blk_size + 4);
985  if (block_opt) {
987  "found Block option, block size is %zu, block nr. %u, M %d\n",
988  (size_t)1 << (block.szx + 4), block.num, block.m);
989  if (block.szx != p->blk_size) {
990  if ((p->offset + chunk) % ((size_t)1 << (block.szx + 4)) == 0) {
991  /*
992  * Recompute the block number of the previous packet given
993  * the new block size
994  */
995  block.num = (uint32_t)(((p->offset + chunk) >> (block.szx + 4)) - 1);
996  p->blk_size = block.szx;
997  chunk = (size_t)1 << (p->blk_size + 4);
998  p->offset = block.num * chunk;
1000  "new Block size is %u, block number %u completed\n",
1001  1 << (block.szx + 4), block.num);
1002  } else {
1004  "ignoring request to increase Block size, "
1005  "next block is not aligned on requested block size "
1006  "boundary. (%zu x %u mod %u = %zu (which is not 0)\n",
1007  p->offset/chunk + 1, (1 << (p->blk_size + 4)),
1008  (1 << (block.szx + 4)),
1009  (p->offset + chunk) % ((size_t)1 << (block.szx + 4)));
1010  }
1011  }
1012  }
1013 
1014  request_cnt = 0;
1015  coap_option_iterator_init(pdu, &opt_b_iter, COAP_OPT_ALL);
1016  while ((option = coap_option_next(&opt_b_iter))) {
1017  unsigned int num;
1018  if (opt_b_iter.number != p->option)
1019  continue;
1020  num = coap_opt_block_num(option);
1021  if (num > 0xFFFFF) /* 20 bits max for num */
1022  continue;
1023  if (block.szx != COAP_OPT_BLOCK_SZX(option)) {
1024  coap_add_data(response,
1025  sizeof("Changing blocksize during request invalid")-1,
1026  (const uint8_t *)"Changing blocksize during request invalid");
1027  response->code = COAP_RESPONSE_CODE(400);
1028  return 1;
1029  }
1030  add_block_send(num, out_blocks, &request_cnt, 1);
1031  break;
1032  }
1033  if (request_cnt == 0) {
1034  /* Block2 not found - give them the first block */
1035  block.szx = p->blk_size;
1036  p->offset = 0;
1037  out_blocks[0] = 0;
1038  request_cnt = 1;
1039  }
1040 
1041  for (i = 0; i < request_cnt; i++) {
1042  uint8_t buf[8];
1043 
1044  block.num = out_blocks[i];
1045  p->offset = block.num * chunk;
1046 
1047  if (i + 1 < request_cnt) {
1048  /* Need to set up a copy of the pdu to send */
1049  coap_opt_filter_t drop_options;
1050 
1051  memset(&drop_options, 0, sizeof(coap_opt_filter_t));
1052  if (block.num != 0)
1054  out_pdu = coap_pdu_duplicate(&p->pdu, session, pdu->token_length,
1055  pdu->token, &drop_options);
1056  if (!out_pdu) {
1057  response->code = COAP_RESPONSE_CODE(500);
1058  goto fail;
1059  }
1060  }
1061  else {
1062  /*
1063  * Copy the options across and then fix the block option
1064  *
1065  * Need to drop Observe option if BLOCK2 and block.num != 0
1066  */
1067  coap_option_iterator_init(&p->pdu, &opt_iter, COAP_OPT_ALL);
1068  while ((option = coap_option_next(&opt_iter))) {
1069  if (opt_iter.number == COAP_OPTION_OBSERVE && block.num != 0)
1070  continue;
1071  if (!coap_add_option(response, opt_iter.number,
1072  coap_opt_length(option),
1073  coap_opt_value(option))) {
1074  goto internal_issue;
1075  }
1076  }
1077  out_pdu = response;
1078  }
1079  if (pdu->type == COAP_MESSAGE_NON)
1080  out_pdu->type = COAP_MESSAGE_NON;
1081  if (!coap_update_option(out_pdu, p->option,
1083  sizeof(buf),
1084  (block.num << 4) |
1085  ((p->offset + chunk < p->length) << 3) |
1086  block.szx),
1087  buf)) {
1088  goto internal_issue;
1089  }
1090  if (p->b.b2.maxage_expire) {
1091  coap_tick_t now;
1092  coap_time_t rem;
1093 
1094  coap_ticks(&now);
1095  rem = coap_ticks_to_rt(now);
1096  if (p->b.b2.maxage_expire > rem) {
1097  rem = p->b.b2.maxage_expire - rem;
1098  }
1099  else {
1100  rem = 0;
1101  /* Entry needs to be expired */
1102  coap_ticks(&p->last_used);
1103  }
1104  if (!coap_update_option(out_pdu, COAP_OPTION_MAXAGE,
1106  sizeof(buf),
1107  rem),
1108  buf)) {
1109  goto internal_issue;
1110  }
1111  }
1112 
1113  if (!etag_opt && !coap_add_block(out_pdu,
1114  p->length,
1115  p->data,
1116  block.num,
1117  block.szx)) {
1118  goto internal_issue;
1119  }
1120  if (i + 1 < request_cnt) {
1121  coap_send(session, out_pdu);
1122  }
1123  }
1124  goto skip_app_handler;
1125 
1126 fail:
1127  /* Keep in cache for 4 * ACK_TIMOUT */
1128  coap_ticks(&p->last_used);
1129  goto skip_app_handler;
1130  } /* end of LL_FOREACH() */
1131  return 0;
1132 
1133 skip_app_handler:
1134  return 1;
1135 
1136 internal_issue:
1137  response->code = COAP_RESPONSE_CODE(500);
1138  error_phrase = coap_response_phrase(response->code);
1139  coap_add_data(response, strlen(error_phrase),
1140  (const uint8_t *)error_phrase);
1141  goto fail;
1142 }
1143 
1144 static int
1145 update_received_blocks(coap_rblock_t *rec_blocks, uint32_t block_num) {
1146  uint32_t i;
1147 
1148  /* Reset as there is activity */
1149  rec_blocks->retry = 0;
1150 
1151  for (i = 0; i < rec_blocks->used; i++) {
1152  if (block_num >= rec_blocks->range[i].begin &&
1153  block_num <= rec_blocks->range[i].end)
1154  break;
1155 
1156  if (block_num < rec_blocks->range[i].begin) {
1157  if (block_num + 1 == rec_blocks->range[i].begin) {
1158  rec_blocks->range[i].begin = block_num;
1159  }
1160  else {
1161  /* Need to insert a new range */
1162  if (rec_blocks->used == COAP_RBLOCK_CNT -1)
1163  /* Too many losses */
1164  return 0;
1165  memmove(&rec_blocks->range[i+1], &rec_blocks->range[i],
1166  (rec_blocks->used - i) * sizeof (rec_blocks->range[0]));
1167  rec_blocks->range[i].begin = rec_blocks->range[i].end = block_num;
1168  rec_blocks->used++;
1169  }
1170  break;
1171  }
1172  if (block_num == rec_blocks->range[i].end + 1) {
1173  rec_blocks->range[i].end = block_num;
1174  if (i + 1 < rec_blocks->used) {
1175  if (rec_blocks->range[i+1].begin == block_num + 1) {
1176  /* Merge the 2 ranges */
1177  rec_blocks->range[i].end = rec_blocks->range[i+1].end;
1178  if (i+2 < rec_blocks->used) {
1179  memmove (&rec_blocks->range[i+1], &rec_blocks->range[i+2],
1180  (rec_blocks->used - (i+2)) * sizeof (rec_blocks->range[0]));
1181  }
1182  rec_blocks->used--;
1183  }
1184  }
1185  break;
1186  }
1187  }
1188  if (i == rec_blocks->used) {
1189  if (rec_blocks->used == COAP_RBLOCK_CNT -1)
1190  /* Too many losses */
1191  return 0;
1192  rec_blocks->range[i].begin = rec_blocks->range[i].end = block_num;
1193  rec_blocks->used++;
1194  }
1195  coap_ticks(&rec_blocks->last_seen);
1196  return 1;
1197 }
1198 
1199 /*
1200  * Need to check if this is a large PUT / POST using multiple blocks
1201  *
1202  * Server receiving PUT/POST etc. of a large amount of data (BLOCK1)
1203  *
1204  * Return: 0 Call application handler
1205  * 1 Do not call application handler - just send the built response
1206  */
1207 int
1209  coap_session_t *session,
1210  coap_pdu_t *pdu,
1211  coap_pdu_t *response,
1212  coap_resource_t *resource,
1213  coap_string_t *uri_path,
1214  coap_opt_t *observe,
1215  coap_string_t *query,
1217  int *added_block) {
1218  size_t length = 0;
1219  const uint8_t *data = NULL;
1220  size_t offset = 0;
1221  size_t total = 0;
1222  coap_block_t block;
1223  coap_opt_iterator_t opt_iter;
1224  uint16_t block_option = 0;
1225 
1226  coap_get_data_large(pdu, &length, &data, &offset, &total);
1227  pdu->body_offset = 0;
1228  pdu->body_total = length;
1229 
1230  if (coap_get_block(pdu, COAP_OPTION_BLOCK1, &block)) {
1231  block_option = COAP_OPTION_BLOCK1;
1232  }
1233  if (block_option) {
1234  coap_lg_srcv_t *p;
1235  coap_opt_t *size_opt = coap_check_option(pdu,
1237  &opt_iter);
1238  coap_opt_t *fmt_opt = coap_check_option(pdu,
1240  &opt_iter);
1241  uint16_t fmt = fmt_opt ? coap_decode_var_bytes(coap_opt_value(fmt_opt),
1242  coap_opt_length(fmt_opt)) :
1244 
1245  total = size_opt ? coap_decode_var_bytes(coap_opt_value(size_opt),
1246  coap_opt_length(size_opt)) : 0;
1247  offset = block.num << (block.szx + 4);
1248 
1249  LL_FOREACH(session->lg_srcv, p) {
1250  if (resource == p->resource) {
1251  break;
1252  }
1253  if ((p->resource == context->unknown_resource ||
1254  resource == context->proxy_uri_resource) &&
1255  coap_string_equal(uri_path, p->uri_path))
1256  break;
1257  }
1258  if (!p && block.num != 0) {
1259  /* random access - no need to track */
1260  pdu->body_data = data;
1261  pdu->body_length = length;
1262  pdu->body_offset = offset;
1263  pdu->body_total = length + offset + (block.m ? 1 : 0);
1264  }
1265  /* Do not do this if this is a single block */
1266  else if (!p && !(offset == 0 && block.m == 0)) {
1268  if (p == NULL) {
1269  coap_add_data(response, sizeof("Memory issue")-1,
1270  (const uint8_t *)"Memory issue");
1271  response->code = COAP_RESPONSE_CODE(500);
1272  goto skip_app_handler;
1273  }
1274  coap_log(LOG_DEBUG, "** %s: lg_srcv %p initialized\n",
1275  coap_session_str(session), (void*)p);
1276  memset(p, 0, sizeof(coap_lg_srcv_t));
1277  p->resource = resource;
1278  if (resource == context->unknown_resource ||
1279  resource == context->proxy_uri_resource)
1280  p->uri_path = coap_new_str_const(uri_path->s, uri_path->length);
1281  p->content_format = fmt;
1282  p->total_len = total;
1283  p->amount_so_far = length;
1284  p->szx = block.szx;
1285  p->block_option = block_option;
1286  if (observe) {
1287  p->observe_length = min(coap_opt_length(observe), 3);
1288  memcpy(p->observe, coap_opt_value(observe), p->observe_length);
1289  p->observe_set = 1;
1290  }
1291  p->body_data = NULL;
1292  LL_PREPEND(session->lg_srcv, p);
1293  }
1294  if (p) {
1295  if (fmt != p->content_format) {
1296  coap_add_data(response, sizeof("Content-Format mismatch")-1,
1297  (const uint8_t *)"Content-Format mismatch");
1298  response->code = COAP_RESPONSE_CODE(408);
1299  goto free_lg_recv;
1300  }
1301  p->last_mid = pdu->mid;
1302  p->last_type = pdu->type;
1303  memcpy(p->last_token, pdu->token, pdu->token_length);
1304  p->last_token_length = pdu->token_length;
1305  if (session->block_mode & (COAP_BLOCK_SINGLE_BODY)) {
1306  size_t chunk = (size_t)1 << (block.szx + 4);
1307  if (!check_if_received_block(&p->rec_blocks, block.num)) {
1308  /* Update list of blocks received */
1309  if (!update_received_blocks(&p->rec_blocks, block.num)) {
1310  coap_handle_event(context, COAP_EVENT_PARTIAL_BLOCK, session);
1311  coap_add_data(response, sizeof("Too many missing blocks")-1,
1312  (const uint8_t *)"Too many missing blocks");
1313  response->code = COAP_RESPONSE_CODE(408);
1314  goto free_lg_recv;
1315  }
1316  /* Update saved data */
1317  p->body_data = coap_block_build_body(p->body_data, length, data,
1318  offset, p->total_len);
1319  if (!p->body_data)
1320  goto call_app_handler;
1321 
1322  }
1324  (uint32_t)(p->total_len + chunk -1)/chunk)) {
1325  /* Not all the payloads of the body have arrived */
1326  if (block.m) {
1327  uint8_t buf[4];
1328 
1329  /* Ask for the next block */
1330  coap_insert_option(response, block_option,
1331  coap_encode_var_safe(buf, sizeof(buf),
1332  (block.num << 4) |
1333  (block.m << 3) |
1334  block.szx),
1335  buf);
1336  response->code = COAP_RESPONSE_CODE(231);
1337  goto skip_app_handler;
1338  }
1339  goto skip_app_handler;
1340  }
1341 
1342  /*
1343  * Remove the BLOCK1 option as passing all of the data to
1344  * application layer. Add back in observe option if appropriate.
1345  * Adjust all other information.
1346  */
1347  if (p->observe_set) {
1349  p->observe_length, p->observe);
1350  }
1351  coap_remove_option(pdu, block_option);
1352  pdu->body_data = p->body_data->s;
1353  pdu->body_length = p->total_len;
1354  pdu->body_offset = 0;
1355  pdu->body_total = p->total_len;
1356  coap_log(LOG_DEBUG, "Server app version of updated PDU\n");
1357  coap_show_pdu(LOG_DEBUG, pdu);
1358  /* Need to do this here as we need to free off p */
1359  h(resource, session, pdu, query, response);
1360  /* Check if lg_xmit generated and update PDU code if so */
1361  coap_check_code_lg_xmit(session, response, resource, query);
1362  /* Last chunk - free off shortly */
1363  coap_ticks(&p->last_used);
1364  goto skip_app_handler;
1365  }
1366  else {
1367  /* No need to update body_data and body_length as a single PDU */
1368  pdu->body_offset = offset;
1369  /* Exact match if last block */
1370  if (block.m) {
1371  uint8_t buf[4];
1372 
1373  if (total > offset + length + block.m)
1374  pdu->body_total = total;
1375  else
1376  pdu->body_total = offset + length + block.m;
1377 
1378  coap_insert_option(response, block_option,
1379  coap_encode_var_safe(buf, sizeof(buf),
1380  (block.num << 4) |
1381  (block.m << 3) |
1382  block.szx),
1383  buf);
1384  h(resource, session, pdu, query, response);
1385  /* Check if lg_xmit generated and update PDU code if so */
1386  coap_check_code_lg_xmit(session, response, resource, query);
1387  if (COAP_RESPONSE_CLASS(response->code) == 2) {
1388  /* Just in case, as there are more to go */
1389  response->code = COAP_RESPONSE_CODE(231);
1390  }
1391  *added_block = 1;
1392  goto skip_app_handler;
1393  }
1394  else {
1395  pdu->body_total = offset + length + block.m;
1396  }
1397  }
1398 
1399  if (block.m == 0) {
1400  /* Last chunk - free off all */
1401  coap_ticks(&p->last_used);
1402  }
1403  goto call_app_handler;
1404 
1405 free_lg_recv:
1406  LL_DELETE(session->lg_srcv, p);
1407  coap_block_delete_lg_srcv(session, p);
1408  goto skip_app_handler;
1409  }
1410  }
1411 call_app_handler:
1412  return 0;
1413 
1414 skip_app_handler:
1415  return 1;
1416 }
1417 
1418 /*
1419  * Need to see if this is a response to a large body request transfer. If so,
1420  * need to initiate the request containing the next block and not trouble the
1421  * application. Note that Token must unique per request/response.
1422  *
1423  * Client receives large data acknowledgement from server (BLOCK1)
1424  *
1425  * This is set up using coap_add_data_request_large()
1426  *
1427  * Client is sending a large data request using GET etc.
1428  *
1429  * Return: 0 Call application handler
1430  * 1 Do not call application handler - just send the built response
1431  */
1432 int
1434  coap_lg_xmit_t *p;
1435  coap_lg_xmit_t *q;
1436 
1437  LL_FOREACH_SAFE(session->lg_xmit, p, q) {
1438  if (COAP_PDU_IS_REQUEST(&p->pdu) &&
1439  !block_token_match(rcvd->token, rcvd->token_length,
1440  p->b.b1.token, p->b.b1.token_length)) {
1441  }
1442  /* lg_xmit found */
1443  size_t chunk = (size_t)1 << (p->blk_size + 4);
1444  coap_block_t block;
1445 
1446  if (COAP_RESPONSE_CLASS(rcvd->code) == 2 &&
1447  coap_get_block(rcvd, p->option, &block)) {
1449  "found Block option, block size is %u, block nr. %u\n",
1450  1 << (block.szx + 4), block.num);
1451  if (block.szx != p->blk_size) {
1452  if ((p->offset + chunk) % ((size_t)1 << (block.szx + 4)) == 0) {
1453  /*
1454  * Recompute the block number of the previous packet given the
1455  * new block size
1456  */
1457  block.num = (uint32_t)(((p->offset + chunk) >> (block.szx + 4)) - 1);
1458  p->blk_size = block.szx;
1459  chunk = (size_t)1 << (p->blk_size + 4);
1460  p->offset = block.num * chunk;
1462  "new Block size is %u, block number %u completed\n",
1463  1 << (block.szx + 4), block.num);
1464  } else {
1465  coap_log(LOG_DEBUG, "ignoring request to increase Block size, "
1466  "next block is not aligned on requested block size boundary. "
1467  "(%zu x %u mod %u = %zu != 0)\n",
1468  p->offset/chunk + 1, (1 << (p->blk_size + 4)),
1469  (1 << (block.szx + 4)),
1470  (p->offset + chunk) % ((size_t)1 << (block.szx + 4)));
1471  }
1472  }
1473  if (p->last_block == (int)block.num) {
1474  /*
1475  * Duplicate BLOCK ACK
1476  *
1477  * RFCs not clear here, but on a lossy connection, there could
1478  * be multiple BLOCK ACKs, causing the client to retransmit the
1479  * same block multiple times, or the server retransmitting the
1480  * same ACK.
1481  *
1482  * Once a block has been ACKd, there is no need to retransmit it.
1483  */
1484  return 1;
1485  }
1486  p->last_block = block.num;
1487  p->offset = (block.num + 1) * chunk;
1488  if (p->offset < p->length) {
1489  /* Build the next PDU request based off the skeletal PDU */
1490  uint8_t buf[8];
1491  coap_pdu_t *pdu;
1492  uint64_t token = coap_decode_var_bytes8(p->pdu.token,
1493  p->pdu.token_length);
1494  uint8_t ltoken[8];
1495  size_t ltoken_length;
1496 
1497  token = (token & 0xffffffff) + ((uint64_t)(++p->b.b1.count) << 32);
1498  ltoken_length = coap_encode_var_safe8(ltoken, sizeof(token), token);
1499  pdu = coap_pdu_duplicate(&p->pdu, session, ltoken_length, ltoken, NULL);
1500  if (!pdu)
1501  goto fail_body;
1502 
1503  block.num++;
1504  coap_update_option(pdu, p->option,
1505  coap_encode_var_safe(buf, sizeof(buf),
1506  (block.num << 4) |
1507  ((p->offset + chunk < p->length) << 3) |
1508  block.szx),
1509  buf);
1510 
1511  if (!coap_add_block(pdu,
1512  p->length,
1513  p->data,
1514  block.num,
1515  block.szx))
1516  goto fail_body;
1517  if (coap_send(session, pdu) == COAP_INVALID_MID)
1518  goto fail_body;
1519  return 1;
1520  }
1521  }
1522 fail_body:
1523  /* need to put back original token into rcvd */
1524  if (p->b.b1.app_token)
1526  p->b.b1.app_token->s);
1527  coap_log(LOG_DEBUG, "PDU given to app\n");
1528  coap_show_pdu(LOG_DEBUG, rcvd);
1529 
1530  LL_DELETE(session->lg_xmit, p);
1531  coap_block_delete_lg_xmit(session, p);
1532  /*
1533  * There may be a block response after doing the large request
1534  * https://tools.ietf.org/html/rfc7959#section-3.3
1535  */
1536  break;
1537  } /* end of LL_FOREACH_SAFE */
1538  return 0;
1539 }
1540 
1541 /*
1542  * Re-assemble payloads into a body
1543  */
1544 coap_binary_t *
1545 coap_block_build_body(coap_binary_t *body_data, size_t length,
1546  const uint8_t *data, size_t offset, size_t total)
1547 {
1548  if (data == NULL)
1549  return NULL;
1550  if (body_data == NULL && total) {
1551  body_data = coap_new_binary(total);
1552  }
1553  if (body_data == NULL)
1554  return NULL;
1555 
1556  /* Update saved data */
1557  if (offset + length <= total && body_data->length >= total) {
1558  memcpy(&body_data->s[offset], data, length);
1559  }
1560  else {
1561  /*
1562  * total may be inaccurate as per
1563  * https://tools.ietf.org/html/rfc7959#section-4
1564  * o In a request carrying a Block1 Option, to indicate the current
1565  * estimate the client has of the total size of the resource
1566  * representation, measured in bytes ("size indication").
1567  * o In a response carrying a Block2 Option, to indicate the current
1568  * estimate the server has of the total size of the resource
1569  * representation, measured in bytes ("size indication").
1570  */
1571  coap_binary_t *new = coap_resize_binary(body_data, offset + length);
1572 
1573  if (new) {
1574  body_data = new;
1575  memcpy(&body_data->s[offset], data, length);
1576  }
1577  else {
1578  coap_delete_binary(body_data);
1579  return NULL;
1580  }
1581  }
1582  return body_data;
1583 }
1584 
1585 /*
1586  * Need to see if this is a large body response to a request. If so,
1587  * need to initiate the request for the next block and not trouble the
1588  * application. Note that Token must unique per request/response.
1589  *
1590  * This is set up using coap_send_large()
1591  * Client receives large data from server (BLOCK2)
1592  *
1593  * Return: 0 Call application handler
1594  * 1 Do not call application handler - just sent the next request
1595  */
1596 int
1598  coap_session_t *session,
1599  coap_pdu_t *sent,
1600  coap_pdu_t *rcvd,
1601  coap_recurse_t recursive) {
1602  coap_lg_crcv_t *p;
1603  int app_has_response = 0;
1604  coap_block_t block = {0, 0, 0};
1605  int have_block = 0;
1606  uint16_t block_opt = 0;
1607  size_t offset;
1608 
1609  LL_FOREACH(session->lg_crcv, p) {
1610  size_t chunk = 0;
1611  uint8_t buf[8];
1612  coap_opt_iterator_t opt_iter;
1613 
1614  if (!full_match(rcvd->token, rcvd->token_length,
1615  p->token, p->token_length)) {
1616  /* try out the next one */
1617  continue;
1618  }
1619 
1620  /* lg_crcv found */
1621 
1622  if (COAP_RESPONSE_CLASS(rcvd->code) == 2) {
1623  size_t length;
1624  const uint8_t *data;
1626  &opt_iter);
1627  size_t size2 = size_opt ?
1629  coap_opt_length(size_opt)) : 0;
1630 
1631  coap_get_data(rcvd, &length, &data);
1632  rcvd->body_offset = 0;
1633  rcvd->body_total = length;
1634  if (coap_get_block(rcvd, COAP_OPTION_BLOCK2, &block)) {
1635  have_block = 1;
1636  block_opt = COAP_OPTION_BLOCK2;
1637  }
1638  if (have_block) {
1639  coap_opt_t *fmt_opt = coap_check_option(rcvd,
1641  &opt_iter);
1642  uint16_t fmt = fmt_opt ?
1644  coap_opt_length(fmt_opt)) :
1646  coap_opt_t *etag_opt = coap_check_option(rcvd,
1648  &opt_iter);
1649  /* Possibility that Size2 not sent, or is too small */
1650  chunk = (size_t)1 << (block.szx + 4);
1651  offset = block.num * chunk;
1652  if (size2 < (offset + length)) {
1653  if (block.m)
1654  size2 = offset + length + 1;
1655  else
1656  size2 = offset + length;
1657  }
1658 
1659  if (p->initial) {
1660  p->initial = 0;
1661  if (etag_opt) {
1662  p->etag_length = coap_opt_length(etag_opt);
1663  memcpy(p->etag, coap_opt_value(etag_opt), p->etag_length);
1664  p->etag_set = 1;
1665  }
1666  else {
1667  p->etag_set = 0;
1668  }
1669  p->total_len = size2;
1670  p->content_format = fmt;
1671  p->szx = block.szx;
1672  p->block_option = block_opt;
1673  p->last_type = rcvd->type;
1674  p->rec_blocks.used = 0;
1675  }
1676  if (p->total_len < size2)
1677  p->total_len = size2;
1678 
1679  if (etag_opt) {
1680  if (!full_match(coap_opt_value(etag_opt),
1681  coap_opt_length(etag_opt),
1682  p->etag, p->etag_length)) {
1683  /* body of data has changed - need to restart request */
1684  size_t len;
1685  coap_pdu_t *pdu;
1686 
1688  "Data body updated during receipt - new request started\n");
1689  if (!(session->block_mode & COAP_BLOCK_SINGLE_BODY))
1690  coap_handle_event(context, COAP_EVENT_PARTIAL_BLOCK, session);
1691 
1692  p->initial = 1;
1694  p->body_data = NULL;
1695 
1696  coap_session_new_token(session, &len, buf);
1697  pdu = coap_pdu_duplicate(&p->pdu, session, len, buf, NULL);
1698  if (!pdu)
1699  goto fail_resp;
1700 
1701  memcpy(p->token, pdu->token, pdu->token_length);
1702  p->token_length = pdu->token_length;
1703 
1704  coap_update_option(pdu, block_opt,
1705  coap_encode_var_safe(buf, sizeof(buf),
1706  (0 << 4) | (0 << 3) | block.szx),
1707  buf);
1708 
1709  if (coap_send(session, pdu) == COAP_INVALID_MID)
1710  goto fail_resp;
1711 
1712  goto skip_app_handler;
1713  }
1714  }
1715  else if (p->etag_set) {
1716  /* Cannot handle this change in ETag to not being there */
1717  coap_log(LOG_WARNING, "Not all blocks have ETag option\n");
1718  session->block_mode &= ~(COAP_BLOCK_SINGLE_BODY);
1719  goto block_mode;
1720  }
1721 
1722  if (fmt != p->content_format) {
1723  coap_log(LOG_WARNING, "Content-Format option mismatch\n");
1724  session->block_mode &= ~(COAP_BLOCK_SINGLE_BODY);
1725  goto block_mode;
1726  }
1728  "found Block option, block size is %u, block nr. %u\n",
1729  1 << (block.szx + 4), block.num);
1730  if (block.num == 0) {
1731  coap_opt_t *obs_opt = coap_check_option(rcvd,
1733  &opt_iter);
1734  if (obs_opt) {
1735  p->observe_length = min(coap_opt_length(obs_opt), 3);
1736  memcpy(p->observe, coap_opt_value(obs_opt), p->observe_length);
1737  p->observe_set = 1;
1738  }
1739  else {
1740  p->observe_set = 0;
1741  }
1742  }
1743  if (!check_if_received_block(&p->rec_blocks, block.num)) {
1744  /* Update list of blocks received */
1745  if (!update_received_blocks(&p->rec_blocks, block.num)) {
1746  coap_handle_event(context, COAP_EVENT_PARTIAL_BLOCK, session);
1747  goto fail_resp;
1748  }
1749 
1750  if (session->block_mode & (COAP_BLOCK_SINGLE_BODY)) {
1751  p->body_data = coap_block_build_body(p->body_data, length, data,
1752  offset, size2);
1753  if (p->body_data == NULL) {
1754  /* Need to do it block by block */
1755  session->block_mode &= ~(COAP_BLOCK_SINGLE_BODY);
1756  goto block_mode;
1757  }
1758  }
1760  (size2 + chunk -1) / chunk)) {
1761  /* Not all the payloads of the body have arrived */
1762  size_t len;
1763  coap_pdu_t *pdu;
1764 
1765  if (block.m) {
1766  block.m = 0;
1767 
1768  /* Ask for the next block */
1769  coap_session_new_token(session, &len, buf);
1770  pdu = coap_pdu_duplicate(&p->pdu, session, len, buf, NULL);
1771  if (!pdu)
1772  goto fail_resp;
1773 
1774  memcpy(p->token, pdu->token, pdu->token_length);
1775  p->token_length = pdu->token_length;
1776 
1777  /* Only sent with the first block */
1779 
1780  coap_update_option(pdu, block_opt,
1781  coap_encode_var_safe(buf, sizeof(buf),
1782  ((block.num + 1) << 4) |
1783  (block.m << 3) | block.szx),
1784  buf);
1785 
1786  if (coap_send(session, pdu) == COAP_INVALID_MID)
1787  goto fail_resp;
1788  }
1789  if (session->block_mode & (COAP_BLOCK_SINGLE_BODY))
1790  goto skip_app_handler;
1791 
1792  /* need to put back original token into rcvd */
1793  coap_update_token(rcvd, p->app_token->length, p->app_token->s);
1794  rcvd->body_offset = block.num*chunk;
1795  rcvd->body_total = size2;
1796  goto call_app_handler;
1797  }
1798  /* need to put back original token into rcvd */
1799  coap_update_token(rcvd, p->app_token->length, p->app_token->s);
1800  if (session->block_mode & (COAP_BLOCK_SINGLE_BODY)) {
1801  /* Pretend that there is no block */
1802  coap_remove_option(rcvd, block_opt);
1803  if (p->observe_set) {
1805  p->observe_length, p->observe);
1806  }
1807  rcvd->body_data = p->body_data->s;
1808  rcvd->body_length = block.num*chunk + length;
1809  rcvd->body_offset = 0;
1810  rcvd->body_total = rcvd->body_length;
1811  }
1812  else {
1813  rcvd->body_offset = block.num*chunk;
1814  rcvd->body_total = size2;
1815  }
1816  if (context->response_handler) {
1817  if (session->block_mode &
1819  coap_log(LOG_DEBUG, "Client app version of updated PDU\n");
1820  coap_show_pdu(LOG_DEBUG, rcvd);
1821  }
1822  context->response_handler(session, sent, rcvd, rcvd->mid);
1823  }
1824  app_has_response = 1;
1825  /* Set up for the next data body if observing */
1826  p->initial = 1;
1827  memcpy(p->token, p->base_token, p->base_token_length);
1829  if (p->body_data) {
1831  p->body_data = NULL;
1832  }
1833  else {
1834  goto skip_app_handler;
1835  }
1836  }
1837  else {
1838 block_mode:
1839  /* need to put back original token into rcvd */
1840  coap_update_token(rcvd, p->app_token->length, p->app_token->s);
1841  rcvd->body_offset = block.num*chunk;
1842  /* slightly oversize if there is more data */
1843  if (block.m) {
1844  if(size2 > block.num*chunk + length + block.m)
1845  rcvd->body_total = size2;
1846  else
1847  rcvd->body_total = block.num*chunk + length + block.m;
1848  }
1849  else {
1850  rcvd->body_total = block.num*chunk + length;
1851  /* Set up for the next data body if observing */
1852  p->initial = 1;
1853  memcpy(p->token, p->base_token, p->base_token_length);
1855  }
1856  if (context->response_handler) {
1857  coap_log(LOG_DEBUG, "Client app version of updated PDU\n");
1858  coap_show_pdu(LOG_DEBUG, rcvd);
1859  context->response_handler(session, sent, rcvd, rcvd->mid);
1860  }
1861  app_has_response = 1;
1862  }
1863  }
1864  else {
1865  coap_opt_t *obs_opt = coap_check_option(rcvd,
1867  &opt_iter);
1868  if (obs_opt) {
1869  p->observe_length = min(coap_opt_length(obs_opt), 3);
1870  memcpy(p->observe, coap_opt_value(obs_opt), p->observe_length);
1871  p->observe_set = 1;
1872  }
1873  else {
1874  p->observe_set = 0;
1875  }
1876  }
1877  }
1878  if (!block.m && !p->observe_set) {
1879 fail_resp:
1880  /* lg_crcv no longer required - cache it */
1881  coap_ticks(&p->last_used);
1882  }
1883  /* need to put back original token into rcvd */
1884  coap_update_token(rcvd, p->app_token->length, p->app_token->s);
1885  break;
1886  } /* LL_FOREACH() */
1887 
1888  /* Check if receiving a block response and if blocks can be set up */
1889  if (recursive == COAP_RECURSE_OK && !p) {
1890  if (!sent) {
1891  if (coap_get_block(rcvd, COAP_OPTION_BLOCK2, &block)) {
1892  coap_log(LOG_DEBUG, "** %s: large body receive not supported by "
1893  "libcoap unless coap_send_large() is used to transmit "
1894  "request\n", coap_session_str(session));
1895  }
1896  }
1897  else if (COAP_RESPONSE_CLASS(rcvd->code) == 2) {
1898  if (coap_get_block(rcvd, COAP_OPTION_BLOCK2, &block)) {
1899  have_block = 1;
1900  block_opt = COAP_OPTION_BLOCK2;
1901  if (block.num != 0) {
1902  /* Assume random access and just give the single response to app */
1903  size_t length;
1904  const uint8_t *data;
1905  size_t chunk = (size_t)1 << (block.szx + 4);
1906 
1907  coap_get_data(rcvd, &length, &data);
1908  rcvd->body_offset = block.num*chunk;
1909  rcvd->body_total = block.num*chunk + length + (block.m ? 1 : 0);
1910  return 0;
1911  }
1912  }
1913  if (have_block) {
1914  coap_lg_crcv_t *lg_crcv = coap_block_new_lg_crcv(session, sent);
1915 
1916  if (lg_crcv) {
1917  LL_PREPEND(session->lg_crcv, lg_crcv);
1918  return coap_handle_response_get_block(context, session, sent, rcvd,
1919  COAP_RECURSE_NO);
1920  }
1921  }
1922  }
1923  }
1924  return app_has_response;
1925 
1926 call_app_handler:
1927  return 0;
1928 
1929 skip_app_handler:
1930  return 1;
1931 }
1932 
1933 /* Check if lg_xmit generated and update PDU code if so */
1934 void
1936  coap_resource_t *resource, coap_string_t *query) {
1937  coap_lg_xmit_t *lg_xmit;
1938  coap_string_t empty = { 0, NULL};
1939 
1940  if (response->code == 0)
1941  return;
1942  LL_FOREACH(session->lg_xmit, lg_xmit) {
1943  if (!COAP_PDU_IS_REQUEST(&lg_xmit->pdu) &&
1944  lg_xmit->b.b2.resource == resource &&
1945  coap_string_equal(query ? query : &empty,
1946  lg_xmit->b.b2.query ? lg_xmit->b.b2.query : &empty)) {
1947  /* lg_xmit found */
1948  if (lg_xmit->pdu.code == 0) {
1949  lg_xmit->pdu.code = response->code;
1950  return;
1951  }
1952  }
1953  }
1954 }
static int add_block_send(uint32_t num, uint32_t *out_blocks, uint32_t *count, uint32_t max_count)
Definition: block.c:898
COAP_STATIC_INLINE int full_match(const uint8_t *a, size_t alen, const uint8_t *b, size_t blen)
Definition: block.c:271
#define MAX_BLK_LEN
static int check_all_blocks_in(coap_rblock_t *rec_blocks, size_t total_blocks)
Definition: block.c:756
static int update_received_blocks(coap_rblock_t *rec_blocks, uint32_t block_num)
Definition: block.c:1145
#define min(a, b)
Definition: block.c:12
static int check_if_received_block(coap_rblock_t *rec_blocks, uint32_t block_num)
Definition: block.c:743
COAP_STATIC_INLINE int block_token_match(const uint8_t *a, size_t alen, const uint8_t *b, size_t blen)
Definition: block.c:261
unsigned char coap_key_t[4]
Definition: coap_hashkey.h:22
#define coap_hash(String, Length, Result)
Definition: coap_hashkey.h:36
Pulls together all the internal only header files.
int coap_flsll(long long i)
Definition: encode.c:19
coap_tick_t coap_block_check_lg_srcv_timeouts(coap_session_t *session, coap_tick_t now)
Definition: block.c:774
void coap_block_delete_lg_srcv(coap_session_t *session, coap_lg_srcv_t *lg_srcv)
Definition: block.c:863
#define COAP_RBLOCK_CNT
void coap_block_delete_lg_crcv(coap_session_t *session, coap_lg_crcv_t *lg_crcv)
Definition: block.c:848
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)
Definition: block.c:1597
coap_recurse_t
void coap_block_delete_lg_xmit(coap_session_t *session, coap_lg_xmit_t *lg_xmit)
Definition: block.c:876
void coap_check_code_lg_xmit(coap_session_t *session, coap_pdu_t *response, coap_resource_t *resource, coap_string_t *query)
The function checks that the code in a newly formed lg_xmit created by coap_add_data_large_response()...
Definition: block.c:1935
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)
The function that does all the work for the coap_add_data_large*() functions.
Definition: block.c:319
coap_tick_t coap_block_check_lg_crcv_timeouts(coap_session_t *session, coap_tick_t now)
Definition: block.c:720
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)
Definition: block.c:937
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)
Definition: block.c:1208
coap_lg_crcv_t * coap_block_new_lg_crcv(coap_session_t *session, coap_pdu_t *pdu)
Definition: block.c:796
int coap_handle_response_send_block(coap_session_t *session, coap_pdu_t *rcvd)
Definition: block.c:1433
@ 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:247
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.
Definition: block.c:613
#define COAP_OPT_BLOCK_SZX(opt)
Returns the value of the SZX-field of a Block option opt.
Definition: block.h:55
#define COAP_BLOCK_SINGLE_BODY
Definition: block.h:40
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:125
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:1545
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:195
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:142
#define COAP_OPT_BLOCK_MORE(opt)
Returns the value of the More-bit of a Block option opt.
Definition: block.h:51
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:16
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:35
#define COAP_OPT_BLOCK_LAST(opt)
Returns the value of the least significant byte of a Block option opt.
Definition: block.h:47
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:64
int coap_cancel_observe(coap_session_t *session, coap_binary_t *token, coap_pdu_type_t type)
Cancel an observe that is being tracked by the client large receive logic when using coap_send_large(...
Definition: block.c:277
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.
Definition: block.c:624
#define COAP_BLOCK_USE_LIBCOAP
Definition: block.h:39
#define COAP_EXCHANGE_LIFETIME(s)
The EXCHANGE_LIFETIME definition for the session (s).
Definition: coap_session.h:493
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:125
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition: coap_time.h:120
coap_time_t coap_ticks_to_rt(coap_tick_t t)
Helper function that converts coap ticks to wallclock time.
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:40
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:3171
coap_mid_t coap_send(coap_session_t *session, coap_pdu_t *pdu)
Sends a CoAP message to given peer.
Definition: net.c:1155
unsigned int coap_encode_var_safe(uint8_t *buf, size_t length, unsigned int val)
Encodes multiple-length byte sequences.
Definition: encode.c:38
unsigned int coap_decode_var_bytes(const uint8_t *buf, size_t len)
Decodes multiple-length byte sequences.
Definition: encode.c:29
uint64_t coap_decode_var_bytes8(const uint8_t *buf, size_t len)
Decodes multiple-length byte sequences.
Definition: encode.c:58
unsigned int coap_encode_var_safe8(uint8_t *buf, size_t length, uint64_t val)
Encodes multiple-length byte sequences.
Definition: encode.c:68
#define COAP_EVENT_PARTIAL_BLOCK
BLOCK2 receive errors.
Definition: coap_event.h:52
void coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu)
Display the contents of the specified pdu.
Definition: coap_debug.c:505
const char * coap_session_str(const coap_session_t *session)
Get session description.
#define coap_log(level,...)
Logging function.
Definition: coap_debug.h:150
@ LOG_WARNING
Warning.
Definition: coap_debug.h:54
@ LOG_DEBUG
Debug.
Definition: coap_debug.h:57
#define COAP_OBSERVE_CANCEL
The value COAP_OBSERVE_CANCEL in a GET/FETCH request option COAP_OPTION_OBSERVE indicates that the ob...
Definition: subscribe.h:37
coap_opt_t * coap_option_next(coap_opt_iterator_t *oi)
Updates the iterator oi to point to the next option.
Definition: option.c:146
uint32_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
Definition: option.c:209
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: option.c:196
#define COAP_OPT_ALL
Pre-defined filter that includes all options.
Definition: option.h:107
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
Definition: option.c:246
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: option.c:110
int coap_option_filter_set(coap_opt_filter_t *filter, coap_option_num_t option)
Sets the corresponding entry for number in filter.
Definition: option.c:491
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:412
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:312
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:280
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:501
#define COAP_PDU_IS_REQUEST(pdu)
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:153
#define COAP_OPTION_BLOCK2
Definition: pdu.h:122
#define COAP_OPTION_CONTENT_FORMAT
Definition: pdu.h:114
#define COAP_OPTION_SIZE2
Definition: pdu.h:124
#define COAP_OPTION_BLOCK1
Definition: pdu.h:123
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition: pdu.h:229
#define COAP_RESPONSE_CODE(N)
Definition: pdu.h:138
#define COAP_RESPONSE_CLASS(C)
Definition: pdu.h:141
#define COAP_OPTION_SIZE1
Definition: pdu.h:127
coap_pdu_type_t
CoAP PDU message type definitions.
Definition: pdu.h:54
#define COAP_MEDIATYPE_TEXT_PLAIN
Definition: pdu.h:187
#define COAP_OPTION_CONTENT_TYPE
Definition: pdu.h:115
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: pdu.c:541
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:652
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:660
#define COAP_INVALID_MID
Indicates an invalid message id.
Definition: pdu.h:232
const char * coap_response_phrase(unsigned char code)
Returns a human-readable response phrase for the specified CoAP response code.
Definition: pdu.c:727
#define COAP_OPTION_MAXAGE
Definition: pdu.h:117
#define COAP_OPTION_ETAG
Definition: pdu.h:107
#define COAP_OPTION_OBSERVE
Definition: pdu.h:109
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:622
@ COAP_MESSAGE_NON
Definition: pdu.h:56
void coap_session_new_token(coap_session_t *session, size_t *len, uint8_t *data)
Creates a new token for use.
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:13
void coap_delete_str_const(coap_str_const_t *s)
Deletes the given const string and releases any memory allocated.
Definition: str.c:51
void coap_delete_binary(coap_binary_t *s)
Deletes the given coap_binary_t object and releases any memory allocated.
Definition: str.c:89
#define coap_string_equal(string1, string2)
Compares the two strings for equality.
Definition: str.h:181
coap_binary_t * coap_new_binary(size_t size)
Returns a new binary object with at least size bytes storage allocated.
Definition: str.c:65
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:42
void coap_delete_string(coap_string_t *s)
Deletes the given string and releases any memory allocated.
Definition: str.c:38
coap_binary_t * coap_resize_binary(coap_binary_t *s, size_t size)
Resizes the given coap_binary_t object.
Definition: str.c:69
#define COAP_STATIC_INLINE
Definition: libcoap.h:38
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.
@ COAP_LG_XMIT
Definition: mem.h:48
@ COAP_LG_CRCV
Definition: mem.h:49
@ COAP_LG_SRCV
Definition: mem.h:50
@ COAP_STRING
Definition: mem.h:30
@ COAP_PDU_BUF
Definition: mem.h:38
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
uint16_t coap_option_num_t
Definition: 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: option.h:26
CoAP binary data definition.
Definition: str.h:48
size_t length
length of binary data
Definition: str.h:49
uint8_t * s
binary data
Definition: str.h:50
Structure of Block options.
Definition: block.h:33
unsigned int num
block number
Definition: block.h:34
unsigned int szx
block size
Definition: block.h:36
unsigned int m
1 if more blocks follow, 0 otherwise
Definition: block.h:35
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
size_t token_length
length of token
uint32_t count
the number of packets sent for payload
coap_binary_t * app_token
original PDU token
uint8_t token[8]
last used token
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 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
coap_binary_t * app_token
app requesting PDU token
uint8_t token[8]
last used token
uint8_t base_token[8]
established base PDU token
size_t token_length
length of token
size_t base_token_length
length of 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.
coap_mid_t last_mid
Last received mid for this set of packets.
uint8_t last_token[8]
< list of received blocks
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 last_type
Last request type (CON/NON)
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_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
size_t offset
large data next offset to transmit
coap_pdu_t pdu
skeletal PDU
size_t length
large data length
coap_tick_t last_used
Last time all data sent or 0.
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
Iterator to run through PDU options.
Definition: option.h:170
coap_option_num_t number
decoded option number
Definition: option.h:172
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 alloc_size
allocated 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
uint64_t tx_token
Next token number to use.
uint8_t block_mode
Zero or more COAP_BLOCK_ or'd options.
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 string data definition.
Definition: str.h:30
uint8_t * s
string data
Definition: str.h:32
size_t length
length of string
Definition: str.h:31