libcoap  4.3.0beta
option.c
Go to the documentation of this file.
1 /*
2  * option.c -- helpers for handling options in CoAP PDUs
3  *
4  * Copyright (C) 2010-2013 Olaf Bergmann <bergmann@tzi.org>
5  *
6  * This file is part of the CoAP library libcoap. Please see
7  * README for terms of use.
8  */
9 
10 
11 #include "coap_internal.h"
12 
13 #include <stdio.h>
14 #include <string.h>
15 
16 #define ADVANCE_OPT(o,e,step) if ((e) < step) { \
17  coap_log(LOG_DEBUG, "cannot advance opt past end\n"); \
18  return 0; \
19  } else { \
20  (e) -= step; \
21  (o) = ((o)) + step; \
22  }
23 
24 /*
25  * Used to prevent access to *opt when pointing to after end of buffer
26  * after doing a ADVANCE_OPT()
27  */
28 #define ADVANCE_OPT_CHECK(o,e,step) do { \
29  ADVANCE_OPT(o,e,step); \
30  if ((e) < 1) \
31  return 0; \
32  } while (0)
33 
34 size_t
35 coap_opt_parse(const coap_opt_t *opt, size_t length, coap_option_t *result) {
36 
37  const coap_opt_t *opt_start = opt; /* store where parsing starts */
38 
39  assert(opt); assert(result);
40 
41  if (length < 1)
42  return 0;
43 
44  result->delta = (*opt & 0xf0) >> 4;
45  result->length = *opt & 0x0f;
46 
47  switch(result->delta) {
48  case 15:
49  if (*opt != COAP_PAYLOAD_START) {
50  coap_log(LOG_DEBUG, "ignored reserved option delta 15\n");
51  }
52  return 0;
53  case 14:
54  /* Handle two-byte value: First, the MSB + 269 is stored as delta value.
55  * After that, the option pointer is advanced to the LSB which is handled
56  * just like case delta == 13. */
57  ADVANCE_OPT_CHECK(opt,length,1);
58  result->delta = ((*opt & 0xff) << 8) + 269;
59  if (result->delta < 269) {
60  coap_log(LOG_DEBUG, "delta too large\n");
61  return 0;
62  }
63  /* fall through */
64  case 13:
65  ADVANCE_OPT_CHECK(opt,length,1);
66  result->delta += *opt & 0xff;
67  break;
68 
69  default:
70  ;
71  }
72 
73  switch(result->length) {
74  case 15:
75  coap_log(LOG_DEBUG, "found reserved option length 15\n");
76  return 0;
77  case 14:
78  /* Handle two-byte value: First, the MSB + 269 is stored as delta value.
79  * After that, the option pointer is advanced to the LSB which is handled
80  * just like case delta == 13. */
81  ADVANCE_OPT_CHECK(opt,length,1);
82  result->length = ((*opt & 0xff) << 8) + 269;
83  /* fall through */
84  case 13:
85  ADVANCE_OPT_CHECK(opt,length,1);
86  result->length += *opt & 0xff;
87  break;
88 
89  default:
90  ;
91  }
92 
93  /* ADVANCE_OPT() is correct here */
94  ADVANCE_OPT(opt,length,1);
95  /* opt now points to value, if present */
96 
97  result->value = opt;
98  if (length < result->length) {
99  coap_log(LOG_DEBUG, "invalid option length\n");
100  return 0;
101  }
102 
103 #undef ADVANCE_OPT
104 #undef ADVANCE_OPT_CHECK
105 
106  return (opt + result->length) - opt_start;
107 }
108 
111  const coap_opt_filter_t filter) {
112  assert(pdu);
113  assert(pdu->token);
114  assert(oi);
115 
116  memset(oi, 0, sizeof(coap_opt_iterator_t));
117 
118  oi->next_option = pdu->token + pdu->token_length;
119  if (pdu->token + pdu->used_size <= oi->next_option) {
120  oi->bad = 1;
121  return NULL;
122  }
123 
124  oi->length = pdu->used_size - pdu->token_length;
125 
126  if (filter) {
127  memcpy(oi->filter, filter, sizeof(coap_opt_filter_t));
128  oi->filtered = 1;
129  }
130  return oi;
131 }
132 
135  assert(oi);
136 
137  if (oi->bad || oi->length == 0 ||
138  !oi->next_option || *oi->next_option == COAP_PAYLOAD_START) {
139  oi->bad = 1;
140  }
141 
142  return oi->bad;
143 }
144 
145 coap_opt_t *
147  coap_option_t option;
148  coap_opt_t *current_opt = NULL;
149  size_t optsize;
150  int b; /* to store result of coap_option_getb() */
151 
152  assert(oi);
153 
154  if (opt_finished(oi))
155  return NULL;
156 
157  while (1) {
158  /* oi->option always points to the next option to deliver; as
159  * opt_finished() filters out any bad conditions, we can assume that
160  * oi->option is valid. */
161  current_opt = oi->next_option;
162 
163  /* Advance internal pointer to next option, skipping any option that
164  * is not included in oi->filter. */
165  optsize = coap_opt_parse(oi->next_option, oi->length, &option);
166  if (optsize) {
167  assert(optsize <= oi->length);
168 
169  oi->next_option += optsize;
170  oi->length -= optsize;
171 
172  oi->type += option.delta;
173  } else { /* current option is malformed */
174  oi->bad = 1;
175  return NULL;
176  }
177 
178  /* Exit the while loop when:
179  * - no filtering is done at all
180  * - the filter matches for the current option
181  * - the filter is too small for the current option number
182  */
183  if (!oi->filtered ||
184  (b = coap_option_filter_get(oi->filter, oi->type)) > 0)
185  break;
186  else if (b < 0) { /* filter too small, cannot proceed */
187  oi->bad = 1;
188  return NULL;
189  }
190  }
191 
192  return current_opt;
193 }
194 
195 coap_opt_t *
196 coap_check_option(coap_pdu_t *pdu, uint16_t type,
197  coap_opt_iterator_t *oi) {
199 
201  coap_option_filter_set(f, type);
202 
203  coap_option_iterator_init(pdu, oi, f);
204 
205  return coap_option_next(oi);
206 }
207 
208 uint32_t
210  uint32_t length;
211 
212  length = *opt & 0x0f;
213  switch (*opt & 0xf0) {
214  case 0xf0:
215  coap_log(LOG_DEBUG, "illegal option delta\n");
216  return 0;
217  case 0xe0:
218  ++opt;
219  /* fall through */
220  /* to skip another byte */
221  case 0xd0:
222  ++opt;
223  /* fall through */
224  /* to skip another byte */
225  default:
226  ++opt;
227  }
228 
229  switch (length) {
230  case 0x0f:
231  coap_log(LOG_DEBUG, "illegal option length\n");
232  return 0;
233  case 0x0e:
234  length = (*opt++ << 8) + 269;
235  /* fall through */
236  case 0x0d:
237  length += *opt++;
238  break;
239  default:
240  ;
241  }
242  return length;
243 }
244 
245 const uint8_t *
247  size_t ofs = 1;
248 
249  switch (*opt & 0xf0) {
250  case 0xf0:
251  coap_log(LOG_DEBUG, "illegal option delta\n");
252  return 0;
253  case 0xe0:
254  ++ofs;
255  /* fall through */
256  case 0xd0:
257  ++ofs;
258  break;
259  default:
260  ;
261  }
262 
263  switch (*opt & 0x0f) {
264  case 0x0f:
265  coap_log(LOG_DEBUG, "illegal option length\n");
266  return 0;
267  case 0x0e:
268  ++ofs;
269  /* fall through */
270  case 0x0d:
271  ++ofs;
272  break;
273  default:
274  ;
275  }
276 
277  return (const uint8_t *)opt + ofs;
278 }
279 
280 size_t
282  coap_option_t option;
283 
284  /* we must assume that opt is encoded correctly */
285  return coap_opt_parse(opt, (size_t)-1, &option);
286 }
287 
288 size_t
289 coap_opt_setheader(coap_opt_t *opt, size_t maxlen,
290  uint16_t delta, size_t length) {
291  size_t skip = 0;
292 
293  assert(opt);
294 
295  if (maxlen == 0) /* need at least one byte */
296  return 0;
297 
298  if (delta < 13) {
299  opt[0] = (coap_opt_t)(delta << 4);
300  } else if (delta < 269) {
301  if (maxlen < 2) {
302  coap_log(LOG_DEBUG, "insufficient space to encode option delta %d\n",
303  delta);
304  return 0;
305  }
306 
307  opt[0] = 0xd0;
308  opt[++skip] = (coap_opt_t)(delta - 13);
309  } else {
310  if (maxlen < 3) {
311  coap_log(LOG_DEBUG, "insufficient space to encode option delta %d\n",
312  delta);
313  return 0;
314  }
315 
316  opt[0] = 0xe0;
317  opt[++skip] = ((delta - 269) >> 8) & 0xff;
318  opt[++skip] = (delta - 269) & 0xff;
319  }
320 
321  if (length < 13) {
322  opt[0] |= length & 0x0f;
323  } else if (length < 269) {
324  if (maxlen < skip + 2) {
325  coap_log(LOG_DEBUG, "insufficient space to encode option length %zu\n",
326  length);
327  return 0;
328  }
329 
330  opt[0] |= 0x0d;
331  opt[++skip] = (coap_opt_t)(length - 13);
332  } else {
333  if (maxlen < skip + 3) {
334  coap_log(LOG_DEBUG, "insufficient space to encode option delta %d\n",
335  delta);
336  return 0;
337  }
338 
339  opt[0] |= 0x0e;
340  opt[++skip] = ((length - 269) >> 8) & 0xff;
341  opt[++skip] = (length - 269) & 0xff;
342  }
343 
344  return skip + 1;
345 }
346 
347 size_t
348 coap_opt_encode_size(uint16_t delta, size_t length) {
349  size_t n = 1;
350 
351  if (delta >= 13) {
352  if (delta < 269)
353  n += 1;
354  else
355  n += 2;
356  }
357 
358  if (length >= 13) {
359  if (length < 269)
360  n += 1;
361  else
362  n += 2;
363  }
364 
365  return n + length;
366 }
367 
368 size_t
369 coap_opt_encode(coap_opt_t *opt, size_t maxlen, uint16_t delta,
370  const uint8_t *val, size_t length) {
371  size_t l = 1;
372 
373  l = coap_opt_setheader(opt, maxlen, delta, length);
374  assert(l <= maxlen);
375 
376  if (!l) {
377  coap_log(LOG_DEBUG, "coap_opt_encode: cannot set option header\n");
378  return 0;
379  }
380 
381  maxlen -= l;
382  opt += l;
383 
384  if (maxlen < length) {
385  coap_log(LOG_DEBUG, "coap_opt_encode: option too large for buffer\n");
386  return 0;
387  }
388 
389  if (val) /* better be safe here */
390  memcpy(opt, val, length);
391 
392  return l + length;
393 }
394 
395 /* coap_opt_filter_t has the following internal structure: */
396 typedef struct {
397  uint16_t mask;
398 
399 #define LONG_MASK ((1 << COAP_OPT_FILTER_LONG) - 1)
400 #define SHORT_MASK \
401  (~LONG_MASK & ((1 << (COAP_OPT_FILTER_LONG + COAP_OPT_FILTER_SHORT)) - 1))
402 
403  uint16_t long_opts[COAP_OPT_FILTER_LONG];
405 } opt_filter;
406 
409 is_long_option(uint16_t type) { return type > 255; }
410 
413 
433 static int
435  uint16_t type,
436  enum filter_op_t op) {
437  size_t lindex = 0;
438  opt_filter *of = (opt_filter *)filter;
439  uint16_t nr, mask = 0;
440 
441  if (is_long_option(type)) {
442  mask = LONG_MASK;
443 
444  for (nr = 1; lindex < COAP_OPT_FILTER_LONG; nr <<= 1, lindex++) {
445 
446  if (((of->mask & nr) > 0) && (of->long_opts[lindex] == type)) {
447  if (op == FILTER_CLEAR) {
448  of->mask &= ~nr;
449  }
450 
451  return 1;
452  }
453  }
454  } else {
455  mask = SHORT_MASK;
456 
457  for (nr = 1 << COAP_OPT_FILTER_LONG; lindex < COAP_OPT_FILTER_SHORT;
458  nr <<= 1, lindex++) {
459 
460  if (((of->mask & nr) > 0) && (of->short_opts[lindex] == (type & 0xff))) {
461  if (op == FILTER_CLEAR) {
462  of->mask &= ~nr;
463  }
464 
465  return 1;
466  }
467  }
468  }
469 
470  /* type was not found, so there is nothing to do if op is CLEAR or GET */
471  if ((op == FILTER_CLEAR) || (op == FILTER_GET)) {
472  return 0;
473  }
474 
475  /* handle FILTER_SET: */
476 
477  lindex = coap_fls(~of->mask & mask);
478  if (!lindex) {
479  return 0;
480  }
481 
482  if (is_long_option(type)) {
483  of->long_opts[lindex - 1] = type;
484  } else {
485  of->short_opts[lindex - COAP_OPT_FILTER_LONG - 1] = (uint8_t)type;
486  }
487 
488  of->mask |= 1 << (lindex - 1);
489 
490  return 1;
491 }
492 
493 int
495  return coap_option_filter_op(filter, type, FILTER_SET);
496 }
497 
498 int
500  return coap_option_filter_op(filter, type, FILTER_CLEAR);
501 }
502 
503 int
505  return coap_option_filter_op(filter, type, FILTER_GET);
506 }
507 
509 coap_new_optlist(uint16_t number,
510  size_t length,
511  const uint8_t *data
512 ) {
513  coap_optlist_t *node;
514 
515 #ifdef WITH_LWIP
516  if (length > MEMP_LEN_COAPOPTLIST) {
518  "coap_new_optlist: size too large (%zu > MEMP_LEN_COAPOPTLIST)\n",
519  length);
520  return NULL;
521  }
522 #endif /* WITH_LWIP */
523  node = coap_malloc_type(COAP_OPTLIST, sizeof(coap_optlist_t) + length);
524 
525  if (node) {
526  memset(node, 0, (sizeof(coap_optlist_t) + length));
527  node->number = number;
528  node->length = length;
529  node->data = (uint8_t *)&node[1];
530  memcpy(node->data, data, length);
531  } else {
532  coap_log(LOG_WARNING, "coap_new_optlist: malloc failure\n");
533  }
534 
535  return node;
536 }
537 
538 static int
539 order_opts(void *a, void *b) {
540  coap_optlist_t *o1 = (coap_optlist_t *)a;
541  coap_optlist_t *o2 = (coap_optlist_t *)b;
542 
543  if (!a || !b)
544  return a < b ? -1 : 1;
545 
546  return (int)(o1->number - o2->number);
547 }
548 
549 int
551  coap_optlist_t *opt;
552 
553  if (options && *options) {
554  /* sort options for delta encoding */
555  LL_SORT((*options), order_opts);
556 
557  LL_FOREACH((*options), opt) {
558  coap_add_option(pdu, opt->number, opt->length, opt->data);
559  }
560  return 1;
561  }
562  return 0;
563 }
564 
565 int
567  if (!node) {
568  coap_log(LOG_DEBUG, "optlist not provided\n");
569  } else {
570  /* must append at the list end to avoid re-ordering of
571  * options during sort */
572  LL_APPEND((*head), node);
573  }
574 
575  return node != NULL;
576 }
577 
578 static int
580  if (node) {
582  }
583  return 1;
584 }
585 
586 void
588  coap_optlist_t *elt, *tmp;
589 
590  if (!queue)
591  return;
592 
593  LL_FOREACH_SAFE(queue, elt, tmp) {
595  }
596 }
597 
Pulls together all the internal only header files.
int coap_fls(unsigned int i)
Definition: encode.c:13
#define coap_log(level,...)
Logging function.
Definition: coap_debug.h:150
@ LOG_CRIT
Critical.
Definition: coap_debug.h:52
@ LOG_WARNING
Warning.
Definition: coap_debug.h:54
@ LOG_DEBUG
Debug.
Definition: coap_debug.h:57
uint16_t coap_opt_filter_t[COAP_OPT_FILTER_SIZE]
Fixed-size vector we use for option filtering.
Definition: option.h:119
int coap_option_filter_set(coap_opt_filter_t filter, uint16_t type)
Sets the corresponding entry for type in filter.
Definition: option.c:494
coap_optlist_t * coap_new_optlist(uint16_t number, size_t length, const uint8_t *data)
Create a new optlist entry.
Definition: option.c:509
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
size_t coap_opt_encode(coap_opt_t *opt, size_t maxlen, uint16_t delta, const uint8_t *val, size_t length)
Encodes option with given delta into opt.
Definition: option.c:369
int coap_option_filter_get(coap_opt_filter_t filter, uint16_t type)
Checks if type is contained in filter.
Definition: option.c:504
uint32_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
Definition: option.c:209
void coap_delete_optlist(coap_optlist_t *queue)
Removes all entries from the optlist_chain, freeing off their memory usage.
Definition: option.c:587
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: option.c:348
int coap_option_filter_unset(coap_opt_filter_t filter, uint16_t type)
Clears the corresponding entry for type in filter.
Definition: option.c:499
#define COAP_OPT_FILTER_SHORT
The number of option types below 256 that can be stored in an option filter.
Definition: option.h:76
COAP_STATIC_INLINE void coap_option_filter_clear(coap_opt_filter_t f)
Clears filter f.
Definition: option.h:130
int coap_add_optlist_pdu(coap_pdu_t *pdu, coap_optlist_t **options)
The current optlist of optlist_chain is first sorted (as per RFC7272 ordering requirements) and then ...
Definition: option.c:550
#define COAP_OPT_FILTER_LONG
The number of option types above 255 that can be stored in an option filter.
Definition: option.h:84
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
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_t * coap_check_option(coap_pdu_t *pdu, uint16_t type, coap_opt_iterator_t *oi)
Retrieves the first option of type type from pdu.
Definition: option.c:196
int coap_insert_optlist(coap_optlist_t **head, coap_optlist_t *node)
Adds optlist to the given optlist_chain.
Definition: option.c:566
size_t coap_opt_setheader(coap_opt_t *opt, size_t maxlen, uint16_t delta, size_t length)
Encodes the given delta and length values into opt.
Definition: option.c:289
#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_OPTLIST
Definition: mem.h:45
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
const uint32_t n
Definition: murmur3.c:56
size_t coap_opt_parse(const coap_opt_t *opt, size_t length, coap_option_t *result)
Parses the option pointed to by opt into result.
Definition: option.c:35
COAP_STATIC_INLINE int is_long_option(uint16_t type)
Returns true iff type denotes an option type larger than 255.
Definition: option.c:409
filter_op_t
Operation specifiers for coap_filter_op().
Definition: option.c:412
@ FILTER_CLEAR
Definition: option.c:412
@ FILTER_GET
Definition: option.c:412
@ FILTER_SET
Definition: option.c:412
static int coap_option_filter_op(coap_opt_filter_t filter, uint16_t type, enum filter_op_t op)
Applies op on filter with respect to type.
Definition: option.c:434
#define ADVANCE_OPT_CHECK(o, e, step)
Definition: option.c:28
#define SHORT_MASK
Definition: option.c:400
size_t coap_opt_size(const coap_opt_t *opt)
Returns the size of the given option, taking into account a possible option jump.
Definition: option.c:281
COAP_STATIC_INLINE int opt_finished(coap_opt_iterator_t *oi)
Definition: option.c:134
static int coap_internal_delete(coap_optlist_t *node)
Definition: option.c:579
#define ADVANCE_OPT(o, e, step)
Definition: option.c:16
static int order_opts(void *a, void *b)
Definition: option.c:539
#define LONG_MASK
Definition: option.c:399
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:25
size_t coap_add_option(coap_pdu_t *pdu, uint16_t type, size_t len, const uint8_t *data)
Adds option of given type to pdu that is passed as first parameter.
Definition: pdu.c:344
#define COAP_PAYLOAD_START
Definition: pdu.h:257
Iterator to run through PDU options.
Definition: option.h:186
coap_opt_t * next_option
pointer to the unparsed next option
Definition: option.h:191
uint16_t type
decoded option type
Definition: option.h:188
coap_opt_filter_t filter
option filter
Definition: option.h:192
unsigned int bad
iterator object is ok if not set
Definition: option.h:189
size_t length
remaining length of PDU
Definition: option.h:187
unsigned int filtered
denotes whether or not filter is used
Definition: option.h:190
Representation of CoAP options.
Definition: option.h:31
const uint8_t * value
Definition: option.h:34
uint16_t delta
Definition: option.h:32
size_t length
Definition: option.h:33
Representation of chained list of CoAP options to install.
Definition: option.h:343
uint16_t number
the option number (no delta coding)
Definition: option.h:345
size_t length
the option value length
Definition: option.h:346
uint8_t * data
the option data
Definition: option.h:347
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
Definition: pdu.h:287
uint8_t * token
first byte of token, if any, or options
Definition: pdu.h:298
uint8_t token_length
length of Token
Definition: pdu.h:292
size_t used_size
used bytes of storage for token, options and payload
Definition: pdu.h:296
uint16_t mask
Definition: option.c:397
uint8_t short_opts[COAP_OPT_FILTER_SHORT]
Definition: option.c:404
uint16_t long_opts[COAP_OPT_FILTER_LONG]
Definition: option.c:403
unsigned int uint32_t
Definition: uthash.h:78
unsigned char uint8_t
Definition: uthash.h:79
#define LL_SORT(list, cmp)
Definition: utlist.h:108
#define LL_FOREACH(head, el)
Definition: utlist.h:413
#define LL_APPEND(head, add)
Definition: utlist.h:338
#define LL_FOREACH_SAFE(head, el, tmp)
Definition: utlist.h:419