libcoap 4.3.2rc2

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-2023 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#include "coap_internal.h"
21
22#ifdef WITH_LWIP
23#include <lwip/pbuf.h>
24#endif
25
26#include <stdint.h>
27
35#define COAP_DEFAULT_VERSION 1 /* version of CoAP supported */
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#if COAP_OSCORE_SUPPORT
48/* for oscore encryption */
49#define COAP_MAX_CHUNK_SIZE COAP_DEFAULT_MAX_PDU_RX_SIZE
50#define OSCORE_CRYPTO_BUFFER_SIZE (COAP_MAX_CHUNK_SIZE+16)
51#endif /* COAP_OSCORE_SUPPORT */
52
53/* Extended Token constants */
54#define COAP_TOKEN_EXT_1B_TKL 13
55#define COAP_TOKEN_EXT_2B_TKL 14
56#define COAP_TOKEN_EXT_1B_BIAS 13
57#define COAP_TOKEN_EXT_2B_BIAS 269 /* 13 + 256 */
58
59#ifndef COAP_DEBUG_BUF_SIZE
60#if defined(WITH_CONTIKI) || defined(WITH_LWIP)
61#define COAP_DEBUG_BUF_SIZE 128
62#else /* defined(WITH_CONTIKI) || defined(WITH_LWIP) */
63/* 1024 derived from RFC7252 4.6. Message Size max payload */
64#define COAP_DEBUG_BUF_SIZE (8 + 1024 * 2)
65#endif /* defined(WITH_CONTIKI) || defined(WITH_LWIP) */
66#endif /* COAP_DEBUG_BUF_SIZE */
67
68#ifndef COAP_DEFAULT_MAX_PDU_RX_SIZE
69#if defined(WITH_LWIP)
70#define COAP_DEFAULT_MAX_PDU_RX_SIZE (COAP_MAX_MESSAGE_SIZE_TCP16+4UL)
71#elif defined(WITH_CONTIKI)
72#define COAP_DEFAULT_MAX_PDU_RX_SIZE (sizeof(coap_packet_t) + UIP_APPDATA_SIZE)
73#else
74/* 8 MiB max-message-size plus some space for options */
75#define COAP_DEFAULT_MAX_PDU_RX_SIZE (8UL*1024*1024+256)
76#endif
77#endif /* COAP_DEFAULT_MAX_PDU_RX_SIZE */
78
83#define COAP_DROPPED_RESPONSE -2
84
85#define COAP_PDU_DELAYED -3
86
87#define COAP_PAYLOAD_START 0xFF /* payload marker */
88
89#define COAP_PDU_IS_EMPTY(pdu) ((pdu)->code == 0)
90#define COAP_PDU_IS_REQUEST(pdu) (!COAP_PDU_IS_EMPTY(pdu) && (pdu)->code < 32)
91#define COAP_PDU_IS_RESPONSE(pdu) ((pdu)->code >= 64 && (pdu)->code < 224)
92#define COAP_PDU_IS_SIGNALING(pdu) ((pdu)->code >= 224)
93
94#define COAP_PDU_MAX_UDP_HEADER_SIZE 4
95#define COAP_PDU_MAX_TCP_HEADER_SIZE 6
96
119 struct coap_pdu_t {
120 coap_pdu_type_t type;
121 coap_pdu_code_t code;
123 coap_mid_t mid;
125 uint8_t max_hdr_size;
126 uint8_t hdr_size;
128 uint8_t crit_opt;
129 uint16_t max_opt;
130 uint32_t e_token_length;
132 coap_bin_const_t actual_token;
133 size_t alloc_size;
135 size_t used_size;
137 size_t max_size;
139 uint8_t *token;
141 uint8_t *data;
142#ifdef WITH_LWIP
143 struct pbuf *pbuf;
151#endif
152 const uint8_t *body_data;
153 size_t body_length;
154 size_t body_offset;
155 size_t body_total;
156 coap_lg_xmit_t *lg_xmit;
158 coap_session_t *session;
159};
160
170 int coap_pdu_resize(coap_pdu_t *pdu, size_t new_size);
171
181 int coap_pdu_check_resize(coap_pdu_t *pdu, size_t new_size);
182
192 size_t coap_pdu_parse_header_size(coap_proto_t proto,
193 const uint8_t *data);
194
206 size_t coap_pdu_parse_size(coap_proto_t proto,
207 const uint8_t *data,
208 size_t length);
209
217 int coap_pdu_parse_header(coap_pdu_t *pdu, coap_proto_t proto);
218
230 int coap_pdu_parse_opt(coap_pdu_t *pdu);
231
241 void coap_pdu_clear(coap_pdu_t *pdu, size_t size);
242
263 size_t coap_add_option_internal(coap_pdu_t *pdu,
264 coap_option_num_t number,
265 size_t len,
266 const uint8_t *data);
275 int coap_remove_option(coap_pdu_t *pdu, coap_option_num_t number);
276
289 size_t coap_insert_option(coap_pdu_t *pdu, coap_option_num_t number,
290 size_t len, const uint8_t *data);
291
303 size_t coap_update_option(coap_pdu_t *pdu,
304 coap_option_num_t number,
305 size_t len,
306 const uint8_t *data);
307
317 size_t coap_pdu_encode_header(coap_pdu_t *pdu, coap_proto_t proto);
318
329 int coap_update_token(coap_pdu_t *pdu,
330 size_t len,
331 const uint8_t *data);
332
341 int coap_option_check_repeatable(coap_option_num_t number);
342
345#endif /* COAP_COAP_PDU_INTERNAL_H_ */
coap_internal.h
Pulls together all the internal only header files.
coap_option_num_t
uint16_t coap_option_num_t
Definition: coap_option.h:20
coap_insert_option
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: coap_pdu.c:563
coap_remove_option
int coap_remove_option(coap_pdu_t *pdu, coap_option_num_t number)
Removes (first) option of given number from the pdu.
Definition: coap_pdu.c:426
coap_update_token
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: coap_pdu.c:361
coap_pdu_parse_header
int coap_pdu_parse_header(coap_pdu_t *pdu, coap_proto_t proto)
Decode the protocol specific header for the specified PDU.
Definition: coap_pdu.c:996
coap_pdu_parse_header_size
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: coap_pdu.c:914
coap_option_check_repeatable
int coap_option_check_repeatable(coap_option_num_t number)
Check whether the option is allowed to be repeated or not.
Definition: coap_pdu.c:520
coap_pdu_clear
void coap_pdu_clear(coap_pdu_t *pdu, size_t size)
Clears any contents from pdu and resets used_size, and data pointers.
Definition: coap_pdu.c:42
coap_pdu_parse_opt
int coap_pdu_parse_opt(coap_pdu_t *pdu)
Verify consistency in the given CoAP PDU structure and locate the data.
Definition: coap_pdu.c:1258
coap_update_option
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: coap_pdu.c:651
coap_pdu_encode_header
size_t coap_pdu_encode_header(coap_pdu_t *pdu, coap_proto_t proto)
Compose the protocol specific header for the specified PDU.
Definition: coap_pdu.c:1405
coap_pdu_check_resize
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: coap_pdu.c:287
coap_pdu_parse_size
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: coap_pdu.c:944
coap_pdu_resize
int coap_pdu_resize(coap_pdu_t *pdu, size_t new_size)
Dynamically grows the size of pdu to new_size.
Definition: coap_pdu.c:245
coap_add_option_internal
size_t coap_add_option_internal(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: coap_pdu.c:701
coap_mid_t
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition: coap_pdu.h:255
coap_proto_t
coap_proto_t
CoAP protocol types.
Definition: coap_pdu.h:304
coap_pdu_code_t
coap_pdu_code_t
Set of codes available for a PDU.
Definition: coap_pdu.h:318
coap_pdu_type_t
coap_pdu_type_t
CoAP PDU message type definitions.
Definition: coap_pdu.h:64
coap_bin_const_t
CoAP binary data definition with const data.
Definition: coap_str.h:64
coap_lg_xmit_t
Structure to hold large body (many blocks) transmission information.
Definition: coap_block_internal.h:117
coap_pdu_t
structure for CoAP PDUs
Definition: coap_pdu_internal.h:119
coap_pdu_t::max_hdr_size
uint8_t max_hdr_size
space reserved for protocol-specific header
Definition: coap_pdu_internal.h:125
coap_pdu_t::max_opt
uint16_t max_opt
highest option number in PDU
Definition: coap_pdu_internal.h:129
coap_pdu_t::token
uint8_t * token
first byte of token (or extended length bytes prefix), if any, or options
Definition: coap_pdu_internal.h:139
coap_pdu_t::lg_xmit
coap_lg_xmit_t * lg_xmit
Holds ptr to lg_xmit if sending a set of blocks.
Definition: coap_pdu_internal.h:156
coap_pdu_t::body_length
size_t body_length
Holds body data length.
Definition: coap_pdu_internal.h:153
coap_pdu_t::max_size
size_t max_size
maximum size for token, options and payload, or zero for variable size pdu
Definition: coap_pdu_internal.h:137
coap_pdu_t::body_data
const uint8_t * body_data
Holds ptr to re-assembled data or NULL.
Definition: coap_pdu_internal.h:152
coap_pdu_t::body_offset
size_t body_offset
Holds body data offset.
Definition: coap_pdu_internal.h:154
coap_pdu_t::code
coap_pdu_code_t code
request method (value 1–31) or response code (value 64-255)
Definition: coap_pdu_internal.h:121
coap_pdu_t::hdr_size
uint8_t hdr_size
actual size used for protocol-specific header (0 until header is encoded)
Definition: coap_pdu_internal.h:126
coap_pdu_t::actual_token
coap_bin_const_t actual_token
Actual token in pdu.
Definition: coap_pdu_internal.h:132
coap_pdu_t::data
uint8_t * data
first byte of payload, if any
Definition: coap_pdu_internal.h:141
coap_pdu_t::mid
coap_mid_t mid
message id, if any, in regular host byte order
Definition: coap_pdu_internal.h:123
coap_pdu_t::e_token_length
uint32_t e_token_length
length of Token space (includes leading extended bytes
Definition: coap_pdu_internal.h:130
coap_pdu_t::used_size
size_t used_size
used bytes of storage for token, options and payload
Definition: coap_pdu_internal.h:135
coap_pdu_t::crit_opt
uint8_t crit_opt
Set if unknown critical option for proxy.
Definition: coap_pdu_internal.h:128
coap_pdu_t::alloc_size
size_t alloc_size
allocated storage for token, options and payload
Definition: coap_pdu_internal.h:133
coap_pdu_t::session
coap_session_t * session
Session responsible for PDU or NULL.
Definition: coap_pdu_internal.h:158
coap_pdu_t::body_total
size_t body_total
Holds body data total size.
Definition: coap_pdu_internal.h:155
coap_pdu_t::type
coap_pdu_type_t type
message type
Definition: coap_pdu_internal.h:120
coap_session_t
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
Definition: coap_session_internal.h:68