libcoap  4.3.0rc2
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 #include "option.h"
20 
21 #ifdef WITH_LWIP
22 #include <lwip/pbuf.h>
23 #endif
24 
25 #include <stdint.h>
26 
33 #define COAP_DEFAULT_PORT 5683 /* CoAP default UDP/TCP port */
34 #define COAPS_DEFAULT_PORT 5684 /* CoAP default UDP/TCP port for secure transmission */
35 #define COAP_DEFAULT_MAX_AGE 60 /* default maximum object lifetime in seconds */
36 #ifndef COAP_DEFAULT_MTU
37 #define COAP_DEFAULT_MTU 1152
38 #endif /* COAP_DEFAULT_MTU */
39 
40 #ifndef COAP_DEFAULT_HOP_LIMIT
41 #define COAP_DEFAULT_HOP_LIMIT 16
42 #endif /* COAP_DEFAULT_HOP_LIMIT */
43 
44 #define COAP_DEFAULT_SCHEME "coap" /* the default scheme for CoAP URIs */
45 
47 #define COAP_DEFAULT_URI_WELLKNOWN ".well-known/core"
48 
49 /* CoAP message types */
50 
54 typedef enum coap_pdu_type_t {
55  COAP_MESSAGE_CON, /* 0 confirmable message (requires ACK/RST) */
56  COAP_MESSAGE_NON, /* 1 non-confirmable message (one-shot message) */
57  COAP_MESSAGE_ACK, /* 2 used to acknowledge confirmable messages */
58  COAP_MESSAGE_RST /* 3 indicates error in received messages */
60 
64 typedef enum coap_request_t {
69  COAP_REQUEST_FETCH, /* 5 RFC 8132 */
70  COAP_REQUEST_PATCH, /* 6 RFC 8132 */
71  COAP_REQUEST_IPATCH, /* 7 RFC 8132 */
73 
74 /*
75  * CoAP option numbers (be sure to update coap_option_check_critical() and
76  * coap_add_option() when adding options
77  */
78 
79 /*
80  * The C, U, and N flags indicate the properties
81  * Critical, Unsafe, and NoCacheKey, respectively.
82  * If U is set, then N has no meaning as per
83  * https://tools.ietf.org/html/rfc7252#section-5.10
84  * and is set to a -.
85  *
86  * Separately, R is for the options that can be repeated
87  *
88  * The least significant byte of the option is set as followed
89  * as per https://tools.ietf.org/html/rfc7252#section-5.4.6
90  *
91  * 0 1 2 3 4 5 6 7
92  * --+---+---+---+---+---+---+---+
93  * | NoCacheKey| U | C |
94  * --+---+---+---+---+---+---+---+
95  *
96  * https://tools.ietf.org/html/rfc8613#section-4 goes on to define E, I and U
97  * properties Encrypted and Integrity Protected, Integrity Protected Only, and
98  * Unprotected respectively. Integrity Protected Only is not currently used.
99  *
100  * An Option is tagged with CUNREIU with any of the letters replaced with _ if
101  * not set, or - for N if U is set (see above) for aiding understanding of the
102  * Option.
103  */
104 
105 #define COAP_OPTION_IF_MATCH 1 /* C__RE__, opaque, 0-8 B, RFC7252 */
106 #define COAP_OPTION_URI_HOST 3 /* CU-___U, String, 1-255 B, RFC7252 */
107 #define COAP_OPTION_ETAG 4 /* ___RE__, opaque, 1-8 B, RFC7252 */
108 #define COAP_OPTION_IF_NONE_MATCH 5 /* C___E__, empty, 0 B, RFC7252 */
109 #define COAP_OPTION_OBSERVE 6 /* _U-_E_U, empty/uint,0/0-3 B, RFC7641 */
110 #define COAP_OPTION_URI_PORT 7 /* CU-___U, uint, 0-2 B, RFC7252 */
111 #define COAP_OPTION_LOCATION_PATH 8 /* ___RE__, String, 0-255 B, RFC7252 */
112 #define COAP_OPTION_OSCORE 9 /* C_____U, *, 0-255 B, RFC8613 */
113 #define COAP_OPTION_URI_PATH 11 /* CU-RE__, String, 0-255 B, RFC7252 */
114 #define COAP_OPTION_CONTENT_FORMAT 12 /* ____E__, uint, 0-2 B, RFC7252 */
115 #define COAP_OPTION_CONTENT_TYPE COAP_OPTION_CONTENT_FORMAT
116 /* COAP_OPTION_MAXAGE default 60 seconds if not set */
117 #define COAP_OPTION_MAXAGE 14 /* _U-_E_U, uint, 0-4 B, RFC7252 */
118 #define COAP_OPTION_URI_QUERY 15 /* CU-RE__, String, 1-255 B, RFC7252 */
119 #define COAP_OPTION_HOP_LIMIT 16 /* ______U, uint, 1 B, RFC8768 */
120 #define COAP_OPTION_ACCEPT 17 /* C___E__, uint, 0-2 B, RFC7252 */
121 #define COAP_OPTION_LOCATION_QUERY 20 /* ___RE__, String, 0-255 B, RFC7252 */
122 #define COAP_OPTION_BLOCK2 23 /* CU-_E_U, uint, 0-3 B, RFC7959 */
123 #define COAP_OPTION_BLOCK1 27 /* CU-_E_U, uint, 0-3 B, RFC7959 */
124 #define COAP_OPTION_SIZE2 28 /* __N_E_U, uint, 0-4 B, RFC7959 */
125 #define COAP_OPTION_PROXY_URI 35 /* CU-___U, String, 1-1034 B, RFC7252 */
126 #define COAP_OPTION_PROXY_SCHEME 39 /* CU-___U, String, 1-255 B, RFC7252 */
127 #define COAP_OPTION_SIZE1 60 /* __N_E_U, uint, 0-4 B, RFC7252 */
128 #define COAP_OPTION_NORESPONSE 258 /* _U-_E_U, uint, 0-1 B, RFC7967 */
129 
130 #define COAP_MAX_OPT 65535
132 /* CoAP result codes (HTTP-Code / 100 * 40 + HTTP-Code % 100) */
133 
134 /* As of draft-ietf-core-coap-04, response codes are encoded to base
135  * 32, i.e. the three upper bits determine the response class while
136  * the remaining five fine-grained information specific to that class.
137  */
138 #define COAP_RESPONSE_CODE(N) (((N)/100 << 5) | (N)%100)
139 
140 /* Determines the class of response code C */
141 #define COAP_RESPONSE_CLASS(C) (((C) >> 5) & 0xFF)
142 
143 #ifndef SHORT_ERROR_RESPONSE
154 const char *coap_response_phrase(unsigned char code);
155 
156 #define COAP_ERROR_PHRASE_LENGTH 32
158 #else
159 #define coap_response_phrase(x) ((char *)NULL)
160 
161 #define COAP_ERROR_PHRASE_LENGTH 0
162 #endif /* SHORT_ERROR_RESPONSE */
163 
164 #define COAP_SIGNALING_CODE(N) (((N)/100 << 5) | (N)%100)
165 
173 
174 /* Applies to COAP_SIGNALING_CSM */
175 #define COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE 2
176 #define COAP_SIGNALING_OPTION_BLOCK_WISE_TRANSFER 4
177 /* Applies to COAP_SIGNALING_PING / COAP_SIGNALING_PONG */
178 #define COAP_SIGNALING_OPTION_CUSTODY 2
179 /* Applies to COAP_SIGNALING_RELEASE */
180 #define COAP_SIGNALING_OPTION_ALTERNATIVE_ADDRESS 2
181 #define COAP_SIGNALING_OPTION_HOLD_OFF 4
182 /* Applies to COAP_SIGNALING_ABORT */
183 #define COAP_SIGNALING_OPTION_BAD_CSM_OPTION 2
184 
185 /* CoAP media type encoding */
186 
187 #define COAP_MEDIATYPE_TEXT_PLAIN 0 /* text/plain (UTF-8) */
188 #define COAP_MEDIATYPE_APPLICATION_LINK_FORMAT 40 /* application/link-format */
189 #define COAP_MEDIATYPE_APPLICATION_XML 41 /* application/xml */
190 #define COAP_MEDIATYPE_APPLICATION_OCTET_STREAM 42 /* application/octet-stream */
191 #define COAP_MEDIATYPE_APPLICATION_RDF_XML 43 /* application/rdf+xml */
192 #define COAP_MEDIATYPE_APPLICATION_EXI 47 /* application/exi */
193 #define COAP_MEDIATYPE_APPLICATION_JSON 50 /* application/json */
194 #define COAP_MEDIATYPE_APPLICATION_CBOR 60 /* application/cbor */
195 #define COAP_MEDIATYPE_APPLICATION_CWT 61 /* application/cwt, RFC 8392 */
196 
197 /* Content formats from RFC 8152 */
198 #define COAP_MEDIATYPE_APPLICATION_COSE_SIGN 98 /* application/cose; cose-type="cose-sign" */
199 #define COAP_MEDIATYPE_APPLICATION_COSE_SIGN1 18 /* application/cose; cose-type="cose-sign1" */
200 #define COAP_MEDIATYPE_APPLICATION_COSE_ENCRYPT 96 /* application/cose; cose-type="cose-encrypt" */
201 #define COAP_MEDIATYPE_APPLICATION_COSE_ENCRYPT0 16 /* application/cose; cose-type="cose-encrypt0" */
202 #define COAP_MEDIATYPE_APPLICATION_COSE_MAC 97 /* application/cose; cose-type="cose-mac" */
203 #define COAP_MEDIATYPE_APPLICATION_COSE_MAC0 17 /* application/cose; cose-type="cose-mac0" */
204 
205 #define COAP_MEDIATYPE_APPLICATION_COSE_KEY 101 /* application/cose-key */
206 #define COAP_MEDIATYPE_APPLICATION_COSE_KEY_SET 102 /* application/cose-key-set */
207 
208 /* Content formats from RFC 8428 */
209 #define COAP_MEDIATYPE_APPLICATION_SENML_JSON 110 /* application/senml+json */
210 #define COAP_MEDIATYPE_APPLICATION_SENSML_JSON 111 /* application/sensml+json */
211 #define COAP_MEDIATYPE_APPLICATION_SENML_CBOR 112 /* application/senml+cbor */
212 #define COAP_MEDIATYPE_APPLICATION_SENSML_CBOR 113 /* application/sensml+cbor */
213 #define COAP_MEDIATYPE_APPLICATION_SENML_EXI 114 /* application/senml-exi */
214 #define COAP_MEDIATYPE_APPLICATION_SENSML_EXI 115 /* application/sensml-exi */
215 #define COAP_MEDIATYPE_APPLICATION_SENML_XML 310 /* application/senml+xml */
216 #define COAP_MEDIATYPE_APPLICATION_SENSML_XML 311 /* application/sensml+xml */
217 
218 /* Content formats from RFC 8782 */
219 #define COAP_MEDIATYPE_APPLICATION_DOTS_CBOR 271 /* application/dots+cbor */
220 
221 /* Note that identifiers for registered media types are in the range 0-65535. We
222  * use an unallocated type here and hope for the best. */
223 #define COAP_MEDIATYPE_ANY 0xff /* any media type */
224 
229 typedef int coap_mid_t;
230 
232 #define COAP_INVALID_MID -1
233 
238 #define COAP_INVALID_TID COAP_INVALID_MID
239 
247 COAP_DEPRECATED typedef struct {
248  uint16_t key; /* the option key (no delta coding) */
249  unsigned int length;
250 } coap_option;
251 
252 #define COAP_OPTION_KEY(option) (option).key
253 #define COAP_OPTION_LENGTH(option) (option).length
254 #define COAP_OPTION_DATA(option) ((unsigned char *)&(option) + sizeof(coap_option))
255 
256 #ifdef WITH_LWIP
272 coap_pdu_t * coap_pdu_from_pbuf(struct pbuf *pbuf);
273 #endif
274 
278 typedef enum coap_proto_t {
285 
289 typedef enum coap_pdu_code_t {
291 
299 
328 
335 
351  coap_mid_t mid, size_t size);
352 
364  coap_session_t *session);
365 
374 void coap_delete_pdu(coap_pdu_t *pdu);
375 
389 coap_pdu_t *
390 coap_pdu_duplicate(const coap_pdu_t *old_pdu,
391  coap_session_t *session,
392  size_t token_length,
393  const uint8_t *token,
394  coap_opt_filter_t *drop_options);
395 
409 int coap_add_token(coap_pdu_t *pdu,
410  size_t len,
411  const uint8_t *data);
412 
434 size_t coap_add_option(coap_pdu_t *pdu,
435  coap_option_num_t number,
436  size_t len,
437  const uint8_t *data);
438 
450 int coap_add_data(coap_pdu_t *pdu,
451  size_t len,
452  const uint8_t *data);
453 
466 uint8_t *coap_add_data_after(coap_pdu_t *pdu, size_t len);
467 
480 int coap_get_data(const coap_pdu_t *pdu,
481  size_t *len,
482  const uint8_t **data);
483 
501 int coap_get_data_large(const coap_pdu_t *pdu,
502  size_t *len,
503  const uint8_t **data,
504  size_t *offset,
505  size_t *total);
506 
515 
523 
532 
540 
549 
558 
566 void coap_pdu_set_mid(coap_pdu_t *pdu, coap_mid_t mid);
567 
570 #endif /* COAP_PDU_H_ */
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
coap_pdu_code_t coap_pdu_get_code(const coap_pdu_t *pdu)
Gets the PDU code associated with pdu.
Definition: pdu.c:1204
void coap_delete_pdu(coap_pdu_t *pdu)
Dispose of an CoAP PDU and frees associated storage.
Definition: pdu.c:140
void coap_pdu_set_code(coap_pdu_t *pdu, coap_pdu_code_t code)
Sets the PDU code in the pdu.
Definition: pdu.c:1209
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition: pdu.h:229
coap_request_t
CoAP PDU Request methods.
Definition: pdu.h:64
#define COAP_RESPONSE_CODE(N)
Definition: pdu.h:138
#define COAP_SIGNALING_CODE(N)
Definition: pdu.h:164
coap_proto_t
CoAP protocol types.
Definition: pdu.h:278
coap_pdu_code_t
Set of codes available for a PDU.
Definition: pdu.h:289
coap_pdu_t * coap_new_pdu(coap_pdu_type_t type, coap_pdu_code_t code, coap_session_t *session)
Creates a new CoAP PDU.
Definition: pdu.c:130
coap_pdu_type_t
CoAP PDU message type definitions.
Definition: pdu.h:54
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:256
void coap_pdu_set_type(coap_pdu_t *pdu, coap_pdu_type_t type)
Sets the PDU type in the pdu.
Definition: pdu.c:1218
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
coap_pdu_signaling_proto_t
Definition: pdu.h:166
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:634
coap_pdu_type_t coap_pdu_get_type(const coap_pdu_t *pdu)
Gets the PDU type associated with pdu.
Definition: pdu.c:1214
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
void coap_pdu_set_mid(coap_pdu_t *pdu, coap_mid_t mid)
Sets the message id in the pdu.
Definition: pdu.c:1235
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
const char * coap_response_phrase(unsigned char code)
Returns a human-readable response phrase for the specified CoAP response code.
Definition: pdu.c:727
coap_mid_t coap_pdu_get_mid(const coap_pdu_t *pdu)
Gets the message id associated with pdu.
Definition: pdu.c:1231
coap_pdu_t * coap_pdu_init(coap_pdu_type_t type, coap_pdu_code_t code, coap_mid_t mid, size_t size)
Creates a new CoAP PDU with at least enough storage space for the given size maximum message size.
Definition: pdu.c:85
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_bin_const_t coap_pdu_get_token(const coap_pdu_t *pdu)
Gets the token associated with pdu.
Definition: pdu.c:1223
@ COAP_REQUEST_PUT
Definition: pdu.h:67
@ COAP_REQUEST_DELETE
Definition: pdu.h:68
@ COAP_REQUEST_GET
Definition: pdu.h:65
@ COAP_REQUEST_FETCH
Definition: pdu.h:69
@ COAP_REQUEST_PATCH
Definition: pdu.h:70
@ COAP_REQUEST_IPATCH
Definition: pdu.h:71
@ COAP_REQUEST_POST
Definition: pdu.h:66
@ COAP_PROTO_DTLS
Definition: pdu.h:281
@ COAP_PROTO_UDP
Definition: pdu.h:280
@ COAP_PROTO_NONE
Definition: pdu.h:279
@ COAP_PROTO_TLS
Definition: pdu.h:283
@ COAP_PROTO_TCP
Definition: pdu.h:282
@ COAP_RESPONSE_CODE_OK
Definition: pdu.h:300
@ COAP_RESPONSE_CODE_INTERNAL_ERROR
Definition: pdu.h:321
@ COAP_RESPONSE_CODE_HOP_LIMIT_REACHED
Definition: pdu.h:327
@ COAP_SIGNALING_CODE_ABORT
Definition: pdu.h:333
@ COAP_RESPONSE_CODE_PRECONDITION_FAILED
Definition: pdu.h:316
@ COAP_RESPONSE_CODE_NOT_ALLOWED
Definition: pdu.h:312
@ COAP_RESPONSE_CODE_CHANGED
Definition: pdu.h:304
@ COAP_REQUEST_CODE_PUT
Definition: pdu.h:294
@ COAP_REQUEST_CODE_POST
Definition: pdu.h:293
@ COAP_RESPONSE_CODE_NOT_ACCEPTABLE
Definition: pdu.h:313
@ COAP_SIGNALING_CODE_CSM
Definition: pdu.h:329
@ COAP_REQUEST_CODE_PATCH
Definition: pdu.h:297
@ COAP_RESPONSE_CODE_PROXYING_NOT_SUPPORTED
Definition: pdu.h:326
@ COAP_RESPONSE_CODE_BAD_REQUEST
Definition: pdu.h:307
@ COAP_SIGNALING_CODE_PING
Definition: pdu.h:330
@ COAP_RESPONSE_CODE_INCOMPLETE
Definition: pdu.h:314
@ COAP_REQUEST_CODE_DELETE
Definition: pdu.h:295
@ COAP_SIGNALING_CODE_PONG
Definition: pdu.h:331
@ COAP_RESPONSE_CODE_NOT_IMPLEMENTED
Definition: pdu.h:322
@ COAP_RESPONSE_CODE_NOT_FOUND
Definition: pdu.h:311
@ COAP_RESPONSE_CODE_BAD_GATEWAY
Definition: pdu.h:323
@ COAP_RESPONSE_CODE_BAD_OPTION
Definition: pdu.h:309
@ COAP_RESPONSE_CODE_REQUEST_TOO_LARGE
Definition: pdu.h:317
@ COAP_RESPONSE_CODE_TOO_MANY_REQUESTS
Definition: pdu.h:320
@ COAP_RESPONSE_CODE_CONFLICT
Definition: pdu.h:315
@ COAP_RESPONSE_CODE_DELETED
Definition: pdu.h:302
@ COAP_RESPONSE_CODE_UNAUTHORIZED
Definition: pdu.h:308
@ COAP_RESPONSE_CODE_CREATED
Definition: pdu.h:301
@ COAP_EMPTY_CODE
Definition: pdu.h:290
@ COAP_RESPONSE_CODE_CONTENT
Definition: pdu.h:305
@ COAP_RESPONSE_CODE_UNPROCESSABLE
Definition: pdu.h:319
@ COAP_RESPONSE_CODE_CONTINUE
Definition: pdu.h:306
@ COAP_RESPONSE_CODE_VALID
Definition: pdu.h:303
@ COAP_RESPONSE_CODE_UNSUPPORTED_CONTENT_FORMAT
Definition: pdu.h:318
@ COAP_REQUEST_CODE_GET
Definition: pdu.h:292
@ COAP_SIGNALING_CODE_RELEASE
Definition: pdu.h:332
@ COAP_REQUEST_CODE_FETCH
Definition: pdu.h:296
@ COAP_RESPONSE_CODE_GATEWAY_TIMEOUT
Definition: pdu.h:325
@ COAP_REQUEST_CODE_IPATCH
Definition: pdu.h:298
@ COAP_RESPONSE_CODE_SERVICE_UNAVAILABLE
Definition: pdu.h:324
@ COAP_RESPONSE_CODE_FORBIDDEN
Definition: pdu.h:310
@ COAP_MESSAGE_NON
Definition: pdu.h:56
@ COAP_MESSAGE_ACK
Definition: pdu.h:57
@ COAP_MESSAGE_CON
Definition: pdu.h:55
@ COAP_MESSAGE_RST
Definition: pdu.h:58
@ COAP_SIGNALING_RELEASE
Definition: pdu.h:170
@ COAP_SIGNALING_CSM
Definition: pdu.h:167
@ COAP_SIGNALING_PONG
Definition: pdu.h:169
@ COAP_SIGNALING_PING
Definition: pdu.h:168
@ COAP_SIGNALING_ABORT
Definition: pdu.h:171
#define COAP_DEPRECATED
Definition: libcoap.h:46
Helpers for handling options in CoAP PDUs.
uint16_t coap_option_num_t
Definition: option.h:20
CoAP binary data definition with const data.
Definition: str.h:56
unsigned int length
Definition: pdu.h:249
uint16_t key
Definition: pdu.h:248
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...