libcoap  4.3.0
coap_pdu_internal.h
Go to the documentation of this file.
1 /*
2  * coap_pdu_internal.h -- CoAP PDU structure
3  *
4  * Copyright (C) 2010-2021 Olaf Bergmann <bergmann@tzi.org>
5  *
6  * SPDX-License-Identifier: BSD-2-Clause
7  *
8  * This file is part of the CoAP library libcoap. Please see README for terms
9  * of use.
10  */
11 
17 #ifndef COAP_COAP_PDU_INTERNAL_H_
18 #define COAP_COAP_PDU_INTERNAL_H_
19 
20 #ifdef WITH_LWIP
21 #include <lwip/pbuf.h>
22 #endif
23 
24 #include <stdint.h>
25 
33 #define COAP_DEFAULT_VERSION 1 /* version of CoAP supported */
34 
35 /* TCP Message format constants, do not modify */
36 #define COAP_MESSAGE_SIZE_OFFSET_TCP8 13
37 #define COAP_MESSAGE_SIZE_OFFSET_TCP16 269 /* 13 + 256 */
38 #define COAP_MESSAGE_SIZE_OFFSET_TCP32 65805 /* 269 + 65536 */
39 
40 /* Derived message size limits */
41 #define COAP_MAX_MESSAGE_SIZE_TCP0 (COAP_MESSAGE_SIZE_OFFSET_TCP8-1) /* 12 */
42 #define COAP_MAX_MESSAGE_SIZE_TCP8 (COAP_MESSAGE_SIZE_OFFSET_TCP16-1) /* 268 */
43 #define COAP_MAX_MESSAGE_SIZE_TCP16 (COAP_MESSAGE_SIZE_OFFSET_TCP32-1) /* 65804 */
44 #define COAP_MAX_MESSAGE_SIZE_TCP32 (COAP_MESSAGE_SIZE_OFFSET_TCP32+0xFFFFFFFF)
45 
46 #ifndef COAP_DEBUG_BUF_SIZE
47 #if defined(WITH_CONTIKI) || defined(WITH_LWIP)
48 #define COAP_DEBUG_BUF_SIZE 128
49 #else /* defined(WITH_CONTIKI) || defined(WITH_LWIP) */
50 /* 1024 derived from RFC7252 4.6. Message Size max payload */
51 #define COAP_DEBUG_BUF_SIZE (8 + 1024 * 2)
52 #endif /* defined(WITH_CONTIKI) || defined(WITH_LWIP) */
53 #endif /* COAP_DEBUG_BUF_SIZE */
54 
55 #ifndef COAP_DEFAULT_MAX_PDU_RX_SIZE
56 #if defined(WITH_CONTIKI) || defined(WITH_LWIP)
57 #define COAP_DEFAULT_MAX_PDU_RX_SIZE (COAP_MAX_MESSAGE_SIZE_TCP16+4UL)
58 #else
59 /* 8 MiB max-message-size plus some space for options */
60 #define COAP_DEFAULT_MAX_PDU_RX_SIZE (8UL*1024*1024+256)
61 #endif
62 #endif /* COAP_DEFAULT_MAX_PDU_RX_SIZE */
63 
68 #define COAP_DROPPED_RESPONSE -2
69 
70 #define COAP_PDU_DELAYED -3
71 
72 #define COAP_PAYLOAD_START 0xFF /* payload marker */
73 
74 #define COAP_PDU_IS_EMPTY(pdu) ((pdu)->code == 0)
75 #define COAP_PDU_IS_REQUEST(pdu) (!COAP_PDU_IS_EMPTY(pdu) && (pdu)->code < 32)
76 #define COAP_PDU_IS_RESPONSE(pdu) ((pdu)->code >= 64 && (pdu)->code < 224)
77 #define COAP_PDU_IS_SIGNALING(pdu) ((pdu)->code >= 224)
78 
79 #define COAP_PDU_MAX_UDP_HEADER_SIZE 4
80 #define COAP_PDU_MAX_TCP_HEADER_SIZE 6
81 
94 struct coap_pdu_t {
100  uint8_t max_hdr_size;
101  uint8_t hdr_size;
103  uint8_t token_length;
104  uint16_t max_opt;
105  size_t alloc_size;
107  size_t used_size;
109  size_t max_size;
111  uint8_t *token;
112  uint8_t *data;
113 #ifdef WITH_LWIP
114  struct pbuf *pbuf;
122 #endif
123  const uint8_t *body_data;
124  size_t body_length;
125  size_t body_offset;
126  size_t body_total;
129 };
130 
140 int coap_pdu_resize(coap_pdu_t *pdu, size_t new_size);
141 
151 int coap_pdu_check_resize(coap_pdu_t *pdu, size_t new_size);
152 
163  const uint8_t *data);
164 
176 size_t coap_pdu_parse_size(coap_proto_t proto,
177  const uint8_t *data,
178  size_t length);
179 
188 
201 
216 int coap_pdu_parse(coap_proto_t proto,
217  const uint8_t *data,
218  size_t length,
219  coap_pdu_t *pdu);
220 
230 void coap_pdu_clear(coap_pdu_t *pdu, size_t size);
231 
241 
255  size_t len, const uint8_t *data);
256 
268 size_t coap_update_option(coap_pdu_t *pdu,
269  coap_option_num_t number,
270  size_t len,
271  const uint8_t *data);
272 
283 
295  size_t len,
296  const uint8_t *data);
297 
300 #endif /* COAP_COAP_PDU_INTERNAL_H_ */
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:414
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:314
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:282
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:825
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:770
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:37
int coap_pdu_parse_opt(coap_pdu_t *pdu)
Verify consistency in the given CoAP PDU structure and locate the data.
Definition: pdu.c:977
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:503
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:1127
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:1102
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:241
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:793
int coap_pdu_resize(coap_pdu_t *pdu, size_t new_size)
Dynamically grows the size of pdu to new_size.
Definition: pdu.c:207
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition: pdu.h:231
coap_proto_t
CoAP protocol types.
Definition: pdu.h:280
coap_pdu_code_t
Set of codes available for a PDU.
Definition: pdu.h:291
coap_pdu_type_t
CoAP PDU message type definitions.
Definition: pdu.h:56
uint16_t coap_option_num_t
Definition: option.h:20
Structure to hold large body (many blocks) transmission information.
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
uint8_t max_hdr_size
space reserved for protocol-specific header
uint16_t max_opt
highest option number in PDU
uint8_t * token
first byte of token, if any, or options
coap_lg_xmit_t * lg_xmit
Holds ptr to lg_xmit if sending a set of blocks.
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