libcoap  4.2.0rc1
pdu.c
Go to the documentation of this file.
1 /* pdu.c -- CoAP message structure
2  *
3  * Copyright (C) 2010--2016 Olaf Bergmann <bergmann@tzi.org>
4  *
5  * This file is part of the CoAP library libcoap. Please see
6  * README for terms of use.
7  */
8 
9 #include "coap_config.h"
10 
11 #if defined(HAVE_ASSERT_H) && !defined(assert)
12 # include <assert.h>
13 #endif
14 
15 #if defined(HAVE_LIMITS_H)
16 #include <limits.h>
17 #endif
18 
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #ifdef HAVE_ARPA_INET_H
23 #include <arpa/inet.h>
24 #endif
25 #ifdef HAVE_WINSOCK2_H
26 #include <winsock2.h>
27 #endif
28 
29 #include "libcoap.h"
30 #include "debug.h"
31 #include "pdu.h"
32 #include "option.h"
33 #include "encode.h"
34 #include "mem.h"
35 #include "coap_session.h"
36 
37 #ifndef min
38 #define min(a,b) ((a) < (b) ? (a) : (b))
39 #endif
40 
41 #ifndef max
42 #define max(a,b) ((a) > (b) ? (a) : (b))
43 #endif
44 
45 void
46 coap_pdu_clear(coap_pdu_t *pdu, size_t size) {
47  assert(pdu);
48  assert(pdu->token);
50  if (pdu->alloc_size > size)
51  pdu->alloc_size = size;
52  pdu->type = 0;
53  pdu->code = 0;
54  pdu->hdr_size = 0;
55  pdu->token_length = 0;
56  pdu->tid = 0;
57  pdu->max_delta = 0;
58  pdu->max_size = size;
59  pdu->used_size = 0;
60  pdu->data = NULL;
61 }
62 
63 #ifdef WITH_LWIP
64 coap_pdu_t *
65 coap_pdu_from_pbuf( struct pbuf *pbuf )
66 {
67  coap_pdu_t *pdu;
68 
69  if (pbuf == NULL) return NULL;
70 
71  LWIP_ASSERT("Can only deal with contiguous PBUFs", pbuf->tot_len == pbuf->len);
72  LWIP_ASSERT("coap_read needs to receive an exclusive copy of the incoming pbuf", pbuf->ref == 1);
73 
74  pdu = coap_malloc_type(COAP_PDU, sizeof(coap_pdu_t) );
75  if (!pdu) {
76  pbuf_free(pbuf);
77  return NULL;
78  }
79 
81  pdu->pbuf = pbuf;
82  pdu->token = (uint8_t *)pbuf->payload + pdu->max_hdr_size;
83  pdu->alloc_size = pbuf->tot_len - pdu->max_hdr_size;
84  coap_pdu_clear(pdu, pdu->alloc_size);
85 
86  return pdu;
87 }
88 #endif
89 
90 coap_pdu_t *
91 coap_pdu_init(uint8_t type, uint8_t code, uint16_t tid, size_t size) {
92  coap_pdu_t *pdu;
93 
94  pdu = coap_malloc_type(COAP_PDU, sizeof(coap_pdu_t));
95  if (!pdu) return NULL;
96 
97 #if defined(WITH_CONTIKI) || defined(WITH_LWIP)
99  if (size > COAP_MAX_MESSAGE_SIZE_TCP16 + 4)
100  return NULL;
102 #else
104 #endif
105 
106 #ifdef WITH_LWIP
107  pdu->pbuf = pbuf_alloc(PBUF_TRANSPORT, size + pdu->max_hdr_size, PBUF_RAM);
108  if (pdu->pbuf == NULL) {
109  coap_free_type(COAP_PDU, pdu);
110  return NULL;
111  }
112  pdu->token = (uint8_t *)pdu->pbuf->payload + pdu->max_hdr_size;
113 #else /* WITH_LWIP */
114  uint8_t *buf;
115  pdu->alloc_size = min(size, 256);
117  if (buf == NULL) {
118  coap_free_type(COAP_PDU, pdu);
119  return NULL;
120  }
121  pdu->token = buf + pdu->max_hdr_size;
122 #endif /* WITH_LWIP */
123  coap_pdu_clear(pdu, size);
124  pdu->tid = tid;
125  pdu->type = type;
126  pdu->code = code;
127  return pdu;
128 }
129 
130 coap_pdu_t *
131 coap_new_pdu(struct coap_session_t *session) {
132  coap_pdu_t *pdu = coap_pdu_init(0, 0, 0, coap_session_max_pdu_size(session));
133 #ifndef NDEBUG
134  if (!pdu)
135  coap_log(LOG_CRIT, "coap_new_pdu: cannot allocate memory for new PDU\n");
136 #endif
137  return pdu;
138 }
139 
140 void
142  if (pdu != NULL) {
143 #ifdef WITH_LWIP
144  pbuf_free(pdu->pbuf);
145 #else
146  if (pdu->token != NULL)
148 #endif
149  coap_free_type(COAP_PDU, pdu);
150  }
151 }
152 
153 int
154 coap_pdu_resize(coap_pdu_t *pdu, size_t new_size) {
155  if (new_size > pdu->alloc_size) {
156 #if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
157  uint8_t *new_hdr;
158  size_t offset;
159 #endif
160  if (pdu->max_size && new_size > pdu->max_size) {
161  warn( "coap_pdu_resize: pdu too big\n" );
162  return 0;
163  }
164 #if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
165  if (pdu->data != NULL) {
166  assert(pdu->data > pdu->token);
167  offset = pdu->data - pdu->token;
168  } else {
169  offset = 0;
170  }
171  new_hdr = (uint8_t*)realloc(pdu->token - pdu->max_hdr_size, new_size + pdu->max_hdr_size);
172  if (new_hdr == NULL) {
173  warn("coap_pdu_resize: realloc failed\n");
174  return 0;
175  }
176  pdu->token = new_hdr + pdu->max_hdr_size;
177  if (offset > 0)
178  pdu->data = pdu->token + offset;
179  else
180  pdu->data = NULL;
181 #endif
182  }
183  pdu->alloc_size = new_size;
184  return 1;
185 }
186 
187 static int
188 coap_pdu_check_resize(coap_pdu_t *pdu, size_t size) {
189  if (size > pdu->alloc_size) {
190  size_t new_size = max(256, pdu->alloc_size * 2);
191  while (size > new_size)
192  new_size *= 2;
193  if (pdu->max_size && new_size > pdu->max_size) {
194  new_size = pdu->max_size;
195  if (new_size < size)
196  return 0;
197  }
198  if (!coap_pdu_resize(pdu, new_size))
199  return 0;
200  }
201  return 1;
202 }
203 
204 int
205 coap_add_token(coap_pdu_t *pdu, size_t len, const uint8_t *data) {
206  /* must allow for pdu == NULL as callers may rely on this */
207  if (!pdu || len > 8)
208  return 0;
209 
210  if (pdu->used_size) {
212  "coap_add_token: The token must defined first. Token ignored\n");
213  return 0;
214  }
215  if (!coap_pdu_check_resize(pdu, len))
216  return 0;
217  pdu->token_length = (uint8_t)len;
218  if (len)
219  memcpy(pdu->token, data, len);
220  pdu->max_delta = 0;
221  pdu->used_size = len;
222  pdu->data = NULL;
223 
224  return 1;
225 }
226 
228 size_t
229 coap_add_option(coap_pdu_t *pdu, uint16_t type, size_t len, const uint8_t *data) {
230  size_t optsize;
231  coap_opt_t *opt;
232 
233  assert(pdu);
234  pdu->data = NULL;
235 
236  if (type < pdu->max_delta) {
237  warn("coap_add_option: options are not in correct order\n");
238  return 0;
239  }
240 
241  if (!coap_pdu_check_resize(pdu,
242  pdu->used_size + coap_opt_encode_size(type - pdu->max_delta, len)))
243  return 0;
244 
245  opt = pdu->token + pdu->used_size;
246 
247  /* encode option and check length */
248  optsize = coap_opt_encode(opt, pdu->alloc_size - pdu->used_size,
249  type - pdu->max_delta, data, len);
250 
251  if (!optsize) {
252  warn("coap_add_option: cannot add option\n");
253  /* error */
254  return 0;
255  } else {
256  pdu->max_delta = type;
257  pdu->used_size += optsize;
258  }
259 
260  return optsize;
261 }
262 
264 uint8_t*
265 coap_add_option_later(coap_pdu_t *pdu, uint16_t type, size_t len) {
266  size_t optsize;
267  coap_opt_t *opt;
268 
269  assert(pdu);
270  pdu->data = NULL;
271 
272  if (type < pdu->max_delta) {
273  warn("coap_add_option: options are not in correct order\n");
274  return NULL;
275  }
276 
277  if (!coap_pdu_check_resize(pdu,
278  pdu->used_size + coap_opt_encode_size(type - pdu->max_delta, len)))
279  return 0;
280 
281  opt = pdu->token + pdu->used_size;
282 
283  /* encode option and check length */
284  optsize = coap_opt_encode(opt, pdu->alloc_size - pdu->used_size,
285  type - pdu->max_delta, NULL, len);
286 
287  if (!optsize) {
288  warn("coap_add_option: cannot add option\n");
289  /* error */
290  return NULL;
291  } else {
292  pdu->max_delta = type;
293  pdu->used_size += (uint16_t)optsize;
294  }
295 
296  return opt + optsize - len;
297 }
298 
299 int
300 coap_add_data(coap_pdu_t *pdu, size_t len, const uint8_t *data) {
301  if (len == 0) {
302  return 1;
303  } else {
304  uint8_t *payload = coap_add_data_after(pdu, len);
305  if (payload != NULL)
306  memcpy(payload, data, len);
307  return payload != NULL;
308  }
309 }
310 
311 uint8_t *
312 coap_add_data_after(coap_pdu_t *pdu, size_t len) {
313  assert(pdu);
314  assert(pdu->data == NULL);
315 
316  pdu->data = NULL;
317 
318  if (len == 0)
319  return NULL;
320 
321  if (!coap_pdu_resize(pdu, pdu->used_size + len + 1))
322  return 0;
323  pdu->token[pdu->used_size++] = COAP_PAYLOAD_START;
324  pdu->data = pdu->token + pdu->used_size;
325  pdu->used_size += len;
326  return pdu->data;
327 }
328 
329 int
330 coap_get_data(const coap_pdu_t *pdu, size_t *len, uint8_t **data) {
331  assert(pdu);
332  assert(len);
333  assert(data);
334 
335  *data = pdu->data;
336  if(pdu->data == NULL) {
337  *len = 0;
338  return 0;
339  }
340 
341  *len = pdu->used_size - (pdu->data - pdu->token);
342 
343  return 1;
344 }
345 
346 #ifndef SHORT_ERROR_RESPONSE
347 typedef struct {
348  unsigned char code;
349  const char *phrase;
350 } error_desc_t;
351 
352 /* if you change anything here, make sure, that the longest string does not
353  * exceed COAP_ERROR_PHRASE_LENGTH. */
355  { COAP_RESPONSE_CODE(201), "Created" },
356  { COAP_RESPONSE_CODE(202), "Deleted" },
357  { COAP_RESPONSE_CODE(203), "Valid" },
358  { COAP_RESPONSE_CODE(204), "Changed" },
359  { COAP_RESPONSE_CODE(205), "Content" },
360  { COAP_RESPONSE_CODE(231), "Continue" },
361  { COAP_RESPONSE_CODE(400), "Bad Request" },
362  { COAP_RESPONSE_CODE(401), "Unauthorized" },
363  { COAP_RESPONSE_CODE(402), "Bad Option" },
364  { COAP_RESPONSE_CODE(403), "Forbidden" },
365  { COAP_RESPONSE_CODE(404), "Not Found" },
366  { COAP_RESPONSE_CODE(405), "Method Not Allowed" },
367  { COAP_RESPONSE_CODE(406), "Not Acceptable" },
368  { COAP_RESPONSE_CODE(408), "Request Entity Incomplete" },
369  { COAP_RESPONSE_CODE(412), "Precondition Failed" },
370  { COAP_RESPONSE_CODE(413), "Request Entity Too Large" },
371  { COAP_RESPONSE_CODE(415), "Unsupported Content-Format" },
372  { COAP_RESPONSE_CODE(500), "Internal Server Error" },
373  { COAP_RESPONSE_CODE(501), "Not Implemented" },
374  { COAP_RESPONSE_CODE(502), "Bad Gateway" },
375  { COAP_RESPONSE_CODE(503), "Service Unavailable" },
376  { COAP_RESPONSE_CODE(504), "Gateway Timeout" },
377  { COAP_RESPONSE_CODE(505), "Proxying Not Supported" },
378  { 0, NULL } /* end marker */
379 };
380 
381 const char *
382 coap_response_phrase(unsigned char code) {
383  int i;
384  for (i = 0; coap_error[i].code; ++i) {
385  if (coap_error[i].code == code)
386  return coap_error[i].phrase;
387  }
388  return NULL;
389 }
390 #endif
391 
397 static size_t
399  coap_option_t option;
400  size_t optsize;
401 
402  assert(optp); assert(*optp);
403  assert(length);
404 
405  optsize = coap_opt_parse(*optp, *length, &option);
406  if (optsize) {
407  assert(optsize <= *length);
408 
409  *optp += optsize;
410  *length -= optsize;
411  }
412 
413  return optsize;
414 }
415 
416 size_t
418  const uint8_t *data) {
419  assert(data);
420  size_t header_size = 0;
421 
422  if (proto == COAP_PROTO_TCP || proto==COAP_PROTO_TLS) {
423  uint8_t len = *data >> 4;
424  if (len < 13)
425  header_size = 2;
426  else if (len==13)
427  header_size = 3;
428  else if (len==14)
429  header_size = 4;
430  else
431  header_size = 6;
432  } else if (proto == COAP_PROTO_UDP || proto==COAP_PROTO_DTLS) {
433  header_size = 4;
434  }
435 
436  return header_size;
437 }
438 
439 size_t
441  const uint8_t *data,
442  size_t length) {
443  assert(data);
444  assert(proto == COAP_PROTO_TCP || proto == COAP_PROTO_TLS);
445  assert(coap_pdu_parse_header_size(proto, data) <= length );
446 
447  size_t size = 0;
448 
449  if ((proto == COAP_PROTO_TCP || proto==COAP_PROTO_TLS) && length >= 1) {
450  uint8_t len = *data >> 4;
451  if (len < 13) {
452  size = len;
453  } else if (length >= 2) {
454  if (len==13) {
455  size = (size_t)data[1] + COAP_MESSAGE_SIZE_OFFSET_TCP8;
456  } else if (length >= 3) {
457  if (len==14) {
458  size = ((size_t)data[1] << 8) + data[2] + COAP_MESSAGE_SIZE_OFFSET_TCP16;
459  } else if (length >= 5) {
460  size = ((size_t)data[1] << 24) + ((size_t)data[2] << 16)
461  + ((size_t)data[3] << 8) + data[4] + COAP_MESSAGE_SIZE_OFFSET_TCP32;
462  }
463  }
464  }
465  size += data[0] & 0x0f;
466  }
467 
468  return size;
469 }
470 
471 int
473  uint8_t *hdr = pdu->token - pdu->hdr_size;
474  if (proto == COAP_PROTO_UDP || proto == COAP_PROTO_DTLS) {
475  assert(pdu->hdr_size == 4);
476  if ((hdr[0] >> 6) != COAP_DEFAULT_VERSION) {
477  debug( "coap_pdu_parse: UDP version not supported\n" );
478  return 0;
479  }
480  pdu->type = (hdr[0] >> 4) & 0x03;
481  pdu->token_length = hdr[0] & 0x0f;
482  pdu->code = hdr[1];
483  pdu->tid = (uint16_t)hdr[2] << 8 | hdr[3];
484  } else if (proto == COAP_PROTO_TCP || proto == COAP_PROTO_TLS) {
485  assert(pdu->hdr_size >= 2 && pdu->hdr_size <= 6);
486  pdu->type = COAP_MESSAGE_CON;
487  pdu->token_length = hdr[0] & 0x0f;
488  pdu->code = hdr[pdu->hdr_size-1];
489  pdu->tid = 0;
490  } else {
491  debug( "coap_pdu_parse: unsupported protocol\n" );
492  return 0;
493  }
494  if (pdu->token_length > pdu->alloc_size) {
495  /* Invalid PDU provided - not wise to assert here though */
496  coap_log(LOG_DEBUG, "coap_pdu_parse: PDU header token size broken\n");
497  pdu->token_length = (uint8_t)pdu->alloc_size;
498  return 0;
499  }
500  return 1;
501 }
502 
503 int
505 
506  /* sanity checks */
507  if (pdu->code == 0) {
508  if (pdu->used_size != 0 || pdu->token_length) {
509  debug("coap_pdu_parse: empty message is not empty\n");
510  return 0;
511  }
512  }
513 
514  if (pdu->token_length > pdu->used_size || pdu->token_length > 8) {
515  debug("coap_pdu_parse: invalid Token\n");
516  return 0;
517  }
518 
519  if (pdu->code == 0) {
520  /* empty packet */
521  pdu->used_size = 0;
522  pdu->data = NULL;
523  } else {
524  /* skip header + token */
525  coap_opt_t *opt = pdu->token + pdu->token_length;
526  size_t length = pdu->used_size - pdu->token_length;
527 
528  while (length > 0 && *opt != COAP_PAYLOAD_START) {
529  if ( !next_option_safe( &opt, (size_t *)&length ) ) {
530  debug( "coap_pdu_parse: missing payload start code\n" );
531  return 0;
532  }
533  }
534 
535  if (length > 0) {
536  assert(*opt == COAP_PAYLOAD_START);
537  opt++; length--;
538 
539  if (length == 0) {
540  debug("coap_pdu_parse: message ending in payload start marker\n");
541  return 0;
542  }
543  }
544  if (length > 0)
545  pdu->data = (uint8_t*)opt;
546  else
547  pdu->data = NULL;
548  }
549 
550  return 1;
551 }
552 
553 int
555  const uint8_t *data,
556  size_t length,
557  coap_pdu_t *pdu)
558 {
559  size_t hdr_size;
560 
561  if (length == 0)
562  return 0;
563  hdr_size = coap_pdu_parse_header_size(proto, data);
564  if (!hdr_size || hdr_size > length)
565  return 0;
566  if (hdr_size > pdu->max_hdr_size)
567  return 0;
568  if (!coap_pdu_resize(pdu, length - hdr_size))
569  return 0;
570 #ifndef WITH_LWIP
571  memcpy(pdu->token - hdr_size, data, length);
572 #endif
573  pdu->hdr_size = (uint8_t)hdr_size;
574  pdu->used_size = length - hdr_size;
575  return coap_pdu_parse_header(pdu, proto) && coap_pdu_parse_opt(pdu);
576 }
577 
578 size_t
580  if (proto == COAP_PROTO_UDP || proto == COAP_PROTO_DTLS) {
581  assert(pdu->max_hdr_size >= 4);
582  if (pdu->max_hdr_size < 4) {
583  warn("coap_pdu_encode_header: not enough space for UDP-style header");
584  return 0;
585  }
586  pdu->token[-4] = COAP_DEFAULT_VERSION << 6
587  | pdu->type << 4
588  | pdu->token_length;
589  pdu->token[-3] = pdu->code;
590  pdu->token[-2] = (uint8_t)(pdu->tid >> 8);
591  pdu->token[-1] = (uint8_t)(pdu->tid);
592  pdu->hdr_size = 4;
593  } else if (proto == COAP_PROTO_TCP || proto == COAP_PROTO_TLS) {
594  size_t len;
595  assert(pdu->used_size >= pdu->token_length);
596  if (pdu->used_size < pdu->token_length) {
597  warn("coap_pdu_encode_header: corrupted PDU");
598  return 0;
599  }
600  len = pdu->used_size - pdu->token_length;
601  if (len <= COAP_MAX_MESSAGE_SIZE_TCP0) {
602  assert(pdu->max_hdr_size >= 2);
603  if (pdu->max_hdr_size < 2) {
604  warn("coap_pdu_encode_header: not enough space for TCP0 header");
605  return 0;
606  }
607  pdu->token[-2] = (uint8_t)len << 4
608  | pdu->token_length;
609  pdu->token[-1] = pdu->code;
610  pdu->hdr_size = 2;
611  } else if (len <= COAP_MAX_MESSAGE_SIZE_TCP8) {
612  assert(pdu->max_hdr_size >= 3);
613  if (pdu->max_hdr_size < 3) {
614  warn("coap_pdu_encode_header: not enough space for TCP8 header");
615  return 0;
616  }
617  pdu->token[-3] = 13 << 4 | pdu->token_length;
618  pdu->token[-2] = (uint8_t)(len - COAP_MESSAGE_SIZE_OFFSET_TCP8);
619  pdu->token[-1] = pdu->code;
620  pdu->hdr_size = 3;
621  } else if (len <= COAP_MAX_MESSAGE_SIZE_TCP16) {
622  assert(pdu->max_hdr_size >= 4);
623  if (pdu->max_hdr_size < 4) {
624  warn("coap_pdu_encode_header: not enough space for TCP16 header");
625  return 0;
626  }
627  pdu->token[-4] = 14 << 4 | pdu->token_length;
628  pdu->token[-3] = (uint8_t)((len - COAP_MESSAGE_SIZE_OFFSET_TCP16) >> 8);
629  pdu->token[-2] = (uint8_t)(len - COAP_MESSAGE_SIZE_OFFSET_TCP16);
630  pdu->token[-1] = pdu->code;
631  pdu->hdr_size = 4;
632  } else {
633  assert(pdu->max_hdr_size >= 6);
634  if (pdu->max_hdr_size < 6) {
635  warn("coap_pdu_encode_header: not enough space for TCP32 header");
636  return 0;
637  }
638  pdu->token[-6] = 15 << 4 | pdu->token_length;
639  pdu->token[-5] = (uint8_t)((len - COAP_MESSAGE_SIZE_OFFSET_TCP32) >> 24);
640  pdu->token[-4] = (uint8_t)((len - COAP_MESSAGE_SIZE_OFFSET_TCP32) >> 16);
641  pdu->token[-3] = (uint8_t)((len - COAP_MESSAGE_SIZE_OFFSET_TCP32) >> 8);
642  pdu->token[-2] = (uint8_t)(len - COAP_MESSAGE_SIZE_OFFSET_TCP32);
643  pdu->token[-1] = pdu->code;
644  pdu->hdr_size = 6;
645  }
646  } else {
647  warn("coap_pdu_encode_header: unsupported protocol");
648  }
649  return pdu->hdr_size;
650 }
uint8_t type
message type
Definition: pdu.h:288
uint8_t code
request method (value 1–10) or response code (value 40-255)
Definition: pdu.h:289
size_t length
the option value length
Definition: option.h:411
#define max(a, b)
Definition: pdu.c:42
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
uint8_t coap_proto_t
Definition: pdu.h:339
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:300
#define COAP_RESPONSE_CODE(N)
Definition: pdu.h:132
uint8_t * coap_add_data_after(coap_pdu_t *pdu, size_t len)
Adds given data to the pdu that is passed as first parameter but does not copyt it.
Definition: pdu.c:312
static size_t next_option_safe(coap_opt_t **optp, size_t *length)
Advances *optp to next option if still in PDU.
Definition: pdu.c:398
const char * phrase
Definition: pdu.c:349
#define COAP_MAX_MESSAGE_SIZE_TCP8
Definition: pdu.h:42
#define COAP_PROTO_DTLS
Definition: pdu.h:345
int coap_pdu_parse_header(coap_pdu_t *pdu, coap_proto_t proto)
Decode the protocol specific header for the specified PDU.
Definition: pdu.c:472
int coap_pdu_parse_opt(coap_pdu_t *pdu)
Verify consistency in the given CoAP PDU structure and locate the data.
Definition: pdu.c:504
Debug.
Definition: debug.h:49
#define COAP_MAX_MESSAGE_SIZE_TCP16
Definition: pdu.h:43
Helpers for handling options in CoAP PDUs.
#define COAP_MESSAGE_SIZE_OFFSET_TCP16
Definition: pdu.h:37
static int coap_pdu_check_resize(coap_pdu_t *pdu, size_t size)
Definition: pdu.c:188
void coap_pdu_clear(coap_pdu_t *pdu, size_t size)
Clears any contents from pdu and resets used_size, and data pointers.
Definition: pdu.c:46
int coap_add_token(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Adds token of length len to pdu.
Definition: pdu.c:205
int coap_pdu_resize(coap_pdu_t *pdu, size_t new_size)
Dynamically grows the size of pdu to new_size.
Definition: pdu.c:154
#define COAP_MESSAGE_SIZE_OFFSET_TCP32
Definition: pdu.h:38
#define COAP_PROTO_UDP
Definition: pdu.h:344
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:384
size_t coap_pdu_encode_header(coap_pdu_t *pdu, coap_proto_t proto)
Compose the protocol specific header for the specified PDU.
Definition: pdu.c:579
#define COAP_DEFAULT_VERSION
Definition: pdu.h:64
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
Definition: pdu.h:287
uint8_t * data
first byte of payload, if any
Definition: pdu.h:299
Warning.
Definition: debug.h:46
error_desc_t coap_error[]
Definition: pdu.c:354
Representation of CoAP options.
Definition: option.h:31
#define assert(...)
Definition: mem.c:18
uint8_t * data
the option data
Definition: option.h:412
#define min(a, b)
Definition: pdu.c:38
size_t used_size
used bytes of storage for token, options and payload
Definition: pdu.h:296
size_t alloc_size
allocated storage for token, options and payload
Definition: pdu.h:295
#define COAP_MESSAGE_SIZE_OFFSET_TCP8
Definition: pdu.h:36
uint8_t * coap_add_option_later(coap_pdu_t *pdu, uint16_t type, size_t len)
de-duplicate code with coap_add_option
Definition: pdu.c:265
uint8_t * token
first byte of token, if any, or options
Definition: pdu.h:298
size_t max_size
maximum size for token, options and payload, or zero for variable size pdu
Definition: pdu.h:297
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:46
#define COAP_MESSAGE_CON
Definition: pdu.h:72
#define COAP_PROTO_TLS
Definition: pdu.h:347
int coap_get_data(const coap_pdu_t *pdu, size_t *len, uint8_t **data)
Retrieves the length and data pointer of specified PDU.
Definition: pdu.c:330
size_t coap_add_option(coap_pdu_t *pdu, uint16_t type, size_t len, const uint8_t *data)
de-duplicate code with coap_add_option_later
Definition: pdu.c:229
Pre-defined constants that reflect defaults for CoAP.
void coap_delete_pdu(coap_pdu_t *pdu)
Dispose of an CoAP PDU and frees associated storage.
Definition: pdu.c:141
uint16_t max_delta
highest option number
Definition: pdu.h:294
const char * coap_response_phrase(unsigned char code)
Returns a human-readable response phrase for the specified CoAP response code.
Definition: pdu.c:382
#define warn(...)
Obsoleted.
Definition: debug.h:138
#define COAP_PAYLOAD_START
Definition: pdu.h:257
int coap_pdu_parse(coap_proto_t proto, const uint8_t *data, size_t length, coap_pdu_t *pdu)
Parses data into the CoAP PDU structure given in result.
Definition: pdu.c:554
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:405
unsigned char code
Definition: pdu.c:348
#define COAP_MAX_MESSAGE_SIZE_TCP0
Definition: pdu.h:41
#define debug(...)
Obsoleted.
Definition: debug.h:143
uint8_t max_hdr_size
space reserved for protocol-specific header
Definition: pdu.h:290
#define COAP_PDU_MAX_TCP_HEADER_SIZE
Definition: pdu.h:318
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.
uint8_t hdr_size
actaul size used for protocol-specific header
Definition: pdu.h:291
size_t coap_pdu_parse_header_size(coap_proto_t proto, const uint8_t *data)
Interprets data to determine the number of bytes in the header.
Definition: pdu.c:417
#define coap_log(level,...)
Logging function.
Definition: debug.h:122
size_t coap_session_max_pdu_size(coap_session_t *session)
Get maximum acceptable PDU size.
Definition: coap_session.c:186
coap_pdu_t * coap_pdu_init(uint8_t type, uint8_t code, uint16_t tid, size_t size)
Creates a new CoAP PDU with at least enough storage space for the given size maximum message size...
Definition: pdu.c:91
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
unsigned char uint8_t
Definition: uthash.h:79
uint8_t token_length
length of Token
Definition: pdu.h:292
Critical.
Definition: debug.h:44
Definition: mem.h:37
#define COAP_PROTO_TCP
Definition: pdu.h:346
#define COAP_PDU_MAX_UDP_HEADER_SIZE
Definition: pdu.h:317
uint16_t tid
transaction id, if any, in regular host byte order
Definition: pdu.h:293
coap_pdu_t * coap_new_pdu(struct coap_session_t *session)
Creates a new CoAP PDU.
Definition: pdu.c:131
size_t coap_pdu_parse_size(coap_proto_t proto, const uint8_t *data, size_t length)
Parses data to extract the message size.
Definition: pdu.c:440