libcoap  4.3.0beta
pdu.h
Go to the documentation of this file.
1 /*
2  * pdu.h -- CoAP message structure
3  *
4  * Copyright (C) 2010-2014 Olaf Bergmann <bergmann@tzi.org>
5  *
6  * This file is part of the CoAP library libcoap. Please see README for terms
7  * of use.
8  */
9 
15 #ifndef COAP_PDU_H_
16 #define COAP_PDU_H_
17 
18 #include "uri.h"
19 
20 #ifdef WITH_LWIP
21 #include <lwip/pbuf.h>
22 #endif
23 
24 #include <stdint.h>
25 
26 #define COAP_DEFAULT_PORT 5683 /* CoAP default UDP/TCP port */
27 #define COAPS_DEFAULT_PORT 5684 /* CoAP default UDP/TCP port for secure transmission */
28 #define COAP_DEFAULT_MAX_AGE 60 /* default maximum object lifetime in seconds */
29 #ifndef COAP_DEFAULT_MTU
30 #define COAP_DEFAULT_MTU 1152
31 #endif /* COAP_DEFAULT_MTU */
32 
33 #ifndef COAP_DEFAULT_HOP_LIMIT
34 #define COAP_DEFAULT_HOP_LIMIT 16
35 #endif /* COAP_DEFAULT_HOP_LIMIT */
36 
37 /* TCP Message format constants, do not modify */
38 #define COAP_MESSAGE_SIZE_OFFSET_TCP8 13
39 #define COAP_MESSAGE_SIZE_OFFSET_TCP16 269 /* 13 + 256 */
40 #define COAP_MESSAGE_SIZE_OFFSET_TCP32 65805 /* 269 + 65536 */
41 
42 /* Derived message size limits */
43 #define COAP_MAX_MESSAGE_SIZE_TCP0 (COAP_MESSAGE_SIZE_OFFSET_TCP8-1) /* 12 */
44 #define COAP_MAX_MESSAGE_SIZE_TCP8 (COAP_MESSAGE_SIZE_OFFSET_TCP16-1) /* 268 */
45 #define COAP_MAX_MESSAGE_SIZE_TCP16 (COAP_MESSAGE_SIZE_OFFSET_TCP32-1) /* 65804 */
46 #define COAP_MAX_MESSAGE_SIZE_TCP32 (COAP_MESSAGE_SIZE_OFFSET_TCP32+0xFFFFFFFF)
47 
48 #ifndef COAP_DEFAULT_MAX_PDU_RX_SIZE
49 #if defined(WITH_CONTIKI) || defined(WITH_LWIP)
50 #define COAP_DEFAULT_MAX_PDU_RX_SIZE (COAP_MAX_MESSAGE_SIZE_TCP16+4UL)
51 #else
52 /* 8 MiB max-message-size plus some space for options */
53 #define COAP_DEFAULT_MAX_PDU_RX_SIZE (8UL*1024*1024+256)
54 #endif
55 #endif /* COAP_DEFAULT_MAX_PDU_RX_SIZE */
56 
57 #ifndef COAP_DEBUG_BUF_SIZE
58 #if defined(WITH_CONTIKI) || defined(WITH_LWIP)
59 #define COAP_DEBUG_BUF_SIZE 128
60 #else /* defined(WITH_CONTIKI) || defined(WITH_LWIP) */
61 /* 1024 derived from RFC7252 4.6. Message Size max payload */
62 #define COAP_DEBUG_BUF_SIZE (8 + 1024 * 2)
63 #endif /* defined(WITH_CONTIKI) || defined(WITH_LWIP) */
64 #endif /* COAP_DEBUG_BUF_SIZE */
65 
66 #define COAP_DEFAULT_VERSION 1 /* version of CoAP supported */
67 #define COAP_DEFAULT_SCHEME "coap" /* the default scheme for CoAP URIs */
68 
70 #define COAP_DEFAULT_URI_WELLKNOWN ".well-known/core"
71 
72 /* CoAP message types */
73 
74 #define COAP_MESSAGE_CON 0 /* confirmable message (requires ACK/RST) */
75 #define COAP_MESSAGE_NON 1 /* non-confirmable message (one-shot message) */
76 #define COAP_MESSAGE_ACK 2 /* used to acknowledge confirmable messages */
77 #define COAP_MESSAGE_RST 3 /* indicates error in received messages */
78 
79 /* CoAP request methods */
80 
81 typedef enum coap_request_t {
86  COAP_REQUEST_FETCH, /* 5 RFC 8132 */
87  COAP_REQUEST_PATCH, /* 6 RFC 8132 */
88  COAP_REQUEST_IPATCH, /* 7 RFC 8132 */
90 
91 /*
92  * CoAP option types (be sure to update coap_option_check_critical() and
93  * coap_add_option() when adding options
94  */
95 
96 /*
97  * The C, U, and N flags indicate the properties
98  * Critical, Unsafe, and NoCacheKey, respectively.
99  * If U is set, then N has no meaning as per
100  * https://tools.ietf.org/html/rfc7252#section-5.10
101  * and is set to a -.
102  *
103  * Separately, R is for the options that can be repeated
104  *
105  * The least significant byte of the option is set as followed
106  * as per https://tools.ietf.org/html/rfc7252#section-5.4.6
107  *
108  * 0 1 2 3 4 5 6 7
109  * --+---+---+---+---+---+---+---+
110  * | NoCacheKey| U | C |
111  * --+---+---+---+---+---+---+---+
112  *
113  * https://tools.ietf.org/html/rfc8613#section-4 goes on to define E, I and U
114  * properties Encrypted and Integrity Protected, Integrity Protected Only and
115  * Unprotected respectively. Integretity Protected Only is not currently used.
116  *
117  * An Option is tagged with CUNREIU with any of the letters replaced with _ if
118  * not set, or - for N if U is set (see above) for aiding understanding of the
119  * Option.
120  */
121 
122 #define COAP_OPTION_IF_MATCH 1 /* C__RE__, opaque, 0-8 B, RFC7252 */
123 #define COAP_OPTION_URI_HOST 3 /* CU-___U, String, 1-255 B, RFC7252 */
124 #define COAP_OPTION_ETAG 4 /* ___RE__, opaque, 1-8 B, RFC7252 */
125 #define COAP_OPTION_IF_NONE_MATCH 5 /* C___E__, empty, 0 B, RFC7252 */
126 #define COAP_OPTION_OBSERVE 6 /* _U-_E_U, empty/uint,0/0-3 B, RFC7641 */
127 #define COAP_OPTION_URI_PORT 7 /* CU-___U, uint, 0-2 B, RFC7252 */
128 #define COAP_OPTION_LOCATION_PATH 8 /* ___RE__, String, 0-255 B, RFC7252 */
129 #define COAP_OPTION_OSCORE 9 /* C_____U, *, 0-255 B, RFC8613 */
130 #define COAP_OPTION_URI_PATH 11 /* CU-RE__, String, 0-255 B, RFC7252 */
131 #define COAP_OPTION_CONTENT_FORMAT 12 /* ____E__, uint, 0-2 B, RFC7252 */
132 #define COAP_OPTION_CONTENT_TYPE COAP_OPTION_CONTENT_FORMAT
133 /* COAP_OPTION_MAXAGE default 60 seconds if not set */
134 #define COAP_OPTION_MAXAGE 14 /* _U-_E_U, uint, 0-4 B, RFC7252 */
135 #define COAP_OPTION_URI_QUERY 15 /* CU-RE__, String, 1-255 B, RFC7252 */
136 #define COAP_OPTION_HOP_LIMIT 16 /* ______U, uint, 1 B, RFC8768 */
137 #define COAP_OPTION_ACCEPT 17 /* C___E__, uint, 0-2 B, RFC7252 */
138 #define COAP_OPTION_LOCATION_QUERY 20 /* ___RE__, String, 0-255 B, RFC7252 */
139 #define COAP_OPTION_BLOCK2 23 /* CU-_E_U, uint, 0-3 B, RFC7959 */
140 #define COAP_OPTION_BLOCK1 27 /* CU-_E_U, uint, 0-3 B, RFC7959 */
141 #define COAP_OPTION_SIZE2 28 /* __N_E_U, uint, 0-4 B, RFC7959 */
142 #define COAP_OPTION_PROXY_URI 35 /* CU-___U, String, 1-1034 B, RFC7252 */
143 #define COAP_OPTION_PROXY_SCHEME 39 /* CU-___U, String, 1-255 B, RFC7252 */
144 #define COAP_OPTION_SIZE1 60 /* __N_E_U, uint, 0-4 B, RFC7252 */
145 #define COAP_OPTION_NORESPONSE 258 /* _U-_E_U, uint, 0-1 B, RFC7967 */
146 
147 #define COAP_MAX_OPT 65535
149 /* CoAP result codes (HTTP-Code / 100 * 40 + HTTP-Code % 100) */
150 
151 /* As of draft-ietf-core-coap-04, response codes are encoded to base
152  * 32, i.e. the three upper bits determine the response class while
153  * the remaining five fine-grained information specific to that class.
154  */
155 #define COAP_RESPONSE_CODE(N) (((N)/100 << 5) | (N)%100)
156 
157 /* Determines the class of response code C */
158 #define COAP_RESPONSE_CLASS(C) (((C) >> 5) & 0xFF)
159 
160 #ifndef SHORT_ERROR_RESPONSE
171 const char *coap_response_phrase(unsigned char code);
172 
173 #define COAP_ERROR_PHRASE_LENGTH 32
175 #else
176 #define coap_response_phrase(x) ((char *)NULL)
177 
178 #define COAP_ERROR_PHRASE_LENGTH 0
179 #endif /* SHORT_ERROR_RESPONSE */
180 
181 #define COAP_SIGNALING_CODE(N) (((N)/100 << 5) | (N)%100)
182 #define COAP_SIGNALING_CSM COAP_SIGNALING_CODE(701)
183 #define COAP_SIGNALING_PING COAP_SIGNALING_CODE(702)
184 #define COAP_SIGNALING_PONG COAP_SIGNALING_CODE(703)
185 #define COAP_SIGNALING_RELEASE COAP_SIGNALING_CODE(704)
186 #define COAP_SIGNALING_ABORT COAP_SIGNALING_CODE(705)
187 
188 /* Applies to COAP_SIGNALING_CSM */
189 #define COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE 2
190 #define COAP_SIGNALING_OPTION_BLOCK_WISE_TRANSFER 4
191 /* Applies to COAP_SIGNALING_PING / COAP_SIGNALING_PONG */
192 #define COAP_SIGNALING_OPTION_CUSTODY 2
193 /* Applies to COAP_SIGNALING_RELEASE */
194 #define COAP_SIGNALING_OPTION_ALTERNATIVE_ADDRESS 2
195 #define COAP_SIGNALING_OPTION_HOLD_OFF 4
196 /* Applies to COAP_SIGNALING_ABORT */
197 #define COAP_SIGNALING_OPTION_BAD_CSM_OPTION 2
198 
199 /* CoAP media type encoding */
200 
201 #define COAP_MEDIATYPE_TEXT_PLAIN 0 /* text/plain (UTF-8) */
202 #define COAP_MEDIATYPE_APPLICATION_LINK_FORMAT 40 /* application/link-format */
203 #define COAP_MEDIATYPE_APPLICATION_XML 41 /* application/xml */
204 #define COAP_MEDIATYPE_APPLICATION_OCTET_STREAM 42 /* application/octet-stream */
205 #define COAP_MEDIATYPE_APPLICATION_RDF_XML 43 /* application/rdf+xml */
206 #define COAP_MEDIATYPE_APPLICATION_EXI 47 /* application/exi */
207 #define COAP_MEDIATYPE_APPLICATION_JSON 50 /* application/json */
208 #define COAP_MEDIATYPE_APPLICATION_CBOR 60 /* application/cbor */
209 #define COAP_MEDIATYPE_APPLICATION_CWT 61 /* application/cwt, RFC 8392 */
210 
211 /* Content formats from RFC 8152 */
212 #define COAP_MEDIATYPE_APPLICATION_COSE_SIGN 98 /* application/cose; cose-type="cose-sign" */
213 #define COAP_MEDIATYPE_APPLICATION_COSE_SIGN1 18 /* application/cose; cose-type="cose-sign1" */
214 #define COAP_MEDIATYPE_APPLICATION_COSE_ENCRYPT 96 /* application/cose; cose-type="cose-encrypt" */
215 #define COAP_MEDIATYPE_APPLICATION_COSE_ENCRYPT0 16 /* application/cose; cose-type="cose-encrypt0" */
216 #define COAP_MEDIATYPE_APPLICATION_COSE_MAC 97 /* application/cose; cose-type="cose-mac" */
217 #define COAP_MEDIATYPE_APPLICATION_COSE_MAC0 17 /* application/cose; cose-type="cose-mac0" */
218 
219 #define COAP_MEDIATYPE_APPLICATION_COSE_KEY 101 /* application/cose-key */
220 #define COAP_MEDIATYPE_APPLICATION_COSE_KEY_SET 102 /* application/cose-key-set */
221 
222 /* Content formats from RFC 8428 */
223 #define COAP_MEDIATYPE_APPLICATION_SENML_JSON 110 /* application/senml+json */
224 #define COAP_MEDIATYPE_APPLICATION_SENSML_JSON 111 /* application/sensml+json */
225 #define COAP_MEDIATYPE_APPLICATION_SENML_CBOR 112 /* application/senml+cbor */
226 #define COAP_MEDIATYPE_APPLICATION_SENSML_CBOR 113 /* application/sensml+cbor */
227 #define COAP_MEDIATYPE_APPLICATION_SENML_EXI 114 /* application/senml-exi */
228 #define COAP_MEDIATYPE_APPLICATION_SENSML_EXI 115 /* application/sensml-exi */
229 #define COAP_MEDIATYPE_APPLICATION_SENML_XML 310 /* application/senml+xml */
230 #define COAP_MEDIATYPE_APPLICATION_SENSML_XML 311 /* application/sensml+xml */
231 
232 /* Content formats from RFC 8782 */
233 #define COAP_MEDIATYPE_APPLICATION_DOTS_CBOR 271 /* application/dots+cbor */
234 
235 /* Note that identifiers for registered media types are in the range 0-65535. We
236  * use an unallocated type here and hope for the best. */
237 #define COAP_MEDIATYPE_ANY 0xff /* any media type */
238 
244 typedef int coap_tid_t;
245 
247 #define COAP_INVALID_TID -1
248 
253 #define COAP_DROPPED_RESPONSE -2
254 
255 #define COAP_PDU_DELAYED -3
256 
257 #define COAP_PAYLOAD_START 0xFF /* payload marker */
258 
266 COAP_DEPRECATED typedef struct {
267  uint16_t key; /* the option key (no delta coding) */
268  unsigned int length;
269 } coap_option;
270 
271 #define COAP_OPTION_KEY(option) (option).key
272 #define COAP_OPTION_LENGTH(option) (option).length
273 #define COAP_OPTION_DATA(option) ((unsigned char *)&(option) + sizeof(coap_option))
274 
287 typedef struct coap_pdu_t {
293  uint16_t tid;
294  uint16_t max_opt;
295  size_t alloc_size;
296  size_t used_size;
297  size_t max_size;
300 #ifdef WITH_LWIP
301  struct pbuf *pbuf;
309 #endif
311 
312 #define COAP_PDU_IS_EMPTY(pdu) ((pdu)->code == 0)
313 #define COAP_PDU_IS_REQUEST(pdu) (!COAP_PDU_IS_EMPTY(pdu) && (pdu)->code < 32)
314 #define COAP_PDU_IS_RESPONSE(pdu) ((pdu)->code >= 64 && (pdu)->code < 224)
315 #define COAP_PDU_IS_SIGNALING(pdu) ((pdu)->code >= 224)
316 
317 #define COAP_PDU_MAX_UDP_HEADER_SIZE 4
318 #define COAP_PDU_MAX_TCP_HEADER_SIZE 6
319 
320 #ifdef WITH_LWIP
336 coap_pdu_t * coap_pdu_from_pbuf(struct pbuf *pbuf);
337 #endif
338 
343 #define COAP_PROTO_NONE 0
344 #define COAP_PROTO_UDP 1
345 #define COAP_PROTO_DTLS 2
346 #define COAP_PROTO_TCP 3
347 #define COAP_PROTO_TLS 4
348 
363 coap_pdu_t *
364 coap_pdu_init(uint8_t type, uint8_t code, uint16_t tid, size_t size);
365 
375 int coap_pdu_resize(coap_pdu_t *pdu, size_t new_size);
376 
388 int coap_pdu_check_resize(coap_pdu_t *pdu, size_t new_size);
389 
396 void coap_pdu_clear(coap_pdu_t *pdu, size_t size);
397 
401 coap_pdu_t *coap_new_pdu(const struct coap_session_t *session);
402 
411 
422  const uint8_t *data);
423 
435 size_t coap_pdu_parse_size(coap_proto_t proto,
436  const uint8_t *data,
437  size_t length);
438 
447 
460 
475 int coap_pdu_parse(coap_proto_t proto,
476  const uint8_t *data,
477  size_t length,
478  coap_pdu_t *pdu);
492 int coap_add_token(coap_pdu_t *pdu,
493  size_t len,
494  const uint8_t *data);
495 
496 size_t coap_insert_option(coap_pdu_t *, uint16_t, size_t, const uint8_t *);
497 
506 size_t coap_add_option(coap_pdu_t *pdu,
507  uint16_t type,
508  size_t len,
509  const uint8_t *data);
510 
511 
512 
519 size_t coap_insert_option(coap_pdu_t *pdu, uint16_t type,
520  size_t len, const uint8_t *data);
521 
528  uint16_t type,
529  size_t len,
530  const uint8_t *data);
536 int coap_add_data(coap_pdu_t *pdu,
537  size_t len,
538  const uint8_t *data);
539 
547 uint8_t *coap_add_data_after(coap_pdu_t *pdu, size_t len);
548 
554 int coap_get_data(const coap_pdu_t *pdu,
555  size_t *len,
556  uint8_t **data);
557 
566 
567 #endif /* COAP_PDU_H_ */
#define COAP_DEPRECATED
Definition: libcoap.h:46
int coap_update_option(coap_pdu_t *pdu, uint16_t type, size_t len, const uint8_t *data)
Updates existing first option of given type in the pdu with the new data.
Definition: pdu.c:303
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:593
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:538
coap_request_t
Definition: pdu.h:81
@ COAP_REQUEST_PUT
Definition: pdu.h:84
@ COAP_REQUEST_DELETE
Definition: pdu.h:85
@ COAP_REQUEST_GET
Definition: pdu.h:82
@ COAP_REQUEST_FETCH
Definition: pdu.h:86
@ COAP_REQUEST_PATCH
Definition: pdu.h:87
@ COAP_REQUEST_IPATCH
Definition: pdu.h:88
@ COAP_REQUEST_POST
Definition: pdu.h:83
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:444
struct coap_pdu_t coap_pdu_t
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
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:79
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:191
coap_pdu_t * coap_new_pdu(const struct coap_session_t *session)
Creates a new CoAP PDU.
Definition: pdu.c:119
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:34
uint8_t coap_proto_t
Definition: pdu.h:339
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
int coap_pdu_parse_opt(coap_pdu_t *pdu)
Verify consistency in the given CoAP PDU structure and locate the data.
Definition: pdu.c:679
void coap_delete_pdu(coap_pdu_t *)
Dispose of an CoAP PDU and frees associated storage.
Definition: pdu.c:127
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 copy it.
Definition: pdu.c:426
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:797
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:772
int coap_tid_t
coap_tid_t is used to store CoAP transaction id, i.e.
Definition: pdu.h:244
const char * coap_response_phrase(unsigned char code)
Returns a human-readable response phrase for the specified CoAP response code.
Definition: pdu.c:497
size_t coap_insert_option(coap_pdu_t *, uint16_t, size_t, const uint8_t *)
Inserts option of given type in the pdu with the appropriate data.
Definition: pdu.c:214
int coap_pdu_check_resize(coap_pdu_t *pdu, size_t new_size)
Dynamically grows the size of pdu to new_size if needed.
Definition: pdu.c:174
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:561
int coap_pdu_resize(coap_pdu_t *pdu, size_t new_size)
Dynamically grows the size of pdu to new_size.
Definition: pdu.c:140
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:414
unsigned int length
Definition: pdu.h:268
uint16_t key
Definition: pdu.h:267
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
Definition: pdu.h:287
uint8_t max_hdr_size
space reserved for protocol-specific header
Definition: pdu.h:290
uint8_t type
message type
Definition: pdu.h:288
uint16_t max_opt
highest option number in PDU
Definition: pdu.h:294
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
uint16_t tid
transaction id, if any, in regular host byte order
Definition: pdu.h:293
uint8_t code
request method (value 1–31) or response code (value 64-255)
Definition: pdu.h:289
uint8_t token_length
length of Token
Definition: pdu.h:292
uint8_t hdr_size
actual size used for protocol-specific header
Definition: pdu.h:291
uint8_t * data
first byte of payload, if any
Definition: pdu.h:299
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
unsigned char uint8_t
Definition: uthash.h:79