libcoap 4.3.5-develop-19cef11
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-2024 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#ifdef RIOT_VERSION
27#include <limits.h>
28#endif /* RIOT_VERSION */
29
30#ifdef HAVE_LIMITS_H
31#include <limits.h>
32#endif /* HAVE_LIMITS_H */
33
34#include <stdint.h>
35
43#define COAP_DEFAULT_VERSION 1 /* version of CoAP supported */
44
45/* TCP Message format constants, do not modify */
46#define COAP_MESSAGE_SIZE_OFFSET_TCP8 13
47#define COAP_MESSAGE_SIZE_OFFSET_TCP16 269 /* 13 + 256 */
48#define COAP_MESSAGE_SIZE_OFFSET_TCP32 65805 /* 269 + 65536 */
49
50/* Derived message size limits */
51#define COAP_MAX_MESSAGE_SIZE_TCP0 (COAP_MESSAGE_SIZE_OFFSET_TCP8-1) /* 12 */
52#define COAP_MAX_MESSAGE_SIZE_TCP8 (COAP_MESSAGE_SIZE_OFFSET_TCP16-1) /* 268 */
53#define COAP_MAX_MESSAGE_SIZE_TCP16 (COAP_MESSAGE_SIZE_OFFSET_TCP32-1) /* 65804 */
54#define COAP_MAX_MESSAGE_SIZE_TCP32 (COAP_MESSAGE_SIZE_OFFSET_TCP32+0xFFFFFFFF)
55#if COAP_OSCORE_SUPPORT
56/* for oscore encryption */
57#define COAP_MAX_CHUNK_SIZE COAP_DEFAULT_MAX_PDU_RX_SIZE
58#define OSCORE_CRYPTO_BUFFER_SIZE (COAP_MAX_CHUNK_SIZE+16)
59#endif /* COAP_OSCORE_SUPPORT */
60
61/* Extended Token constants */
62#define COAP_TOKEN_EXT_1B_TKL 13
63#define COAP_TOKEN_EXT_2B_TKL 14
64#define COAP_TOKEN_EXT_1B_BIAS 13
65#define COAP_TOKEN_EXT_2B_BIAS 269 /* 13 + 256 */
66
67#ifndef COAP_DEBUG_BUF_SIZE
68#if defined(WITH_CONTIKI) || defined(WITH_LWIP)
69#define COAP_DEBUG_BUF_SIZE 128
70#else /* defined(WITH_CONTIKI) || defined(WITH_LWIP) */
71/* 1024 derived from RFC7252 4.6. Message Size max payload */
72#define COAP_DEBUG_BUF_SIZE (8 + 1024 * 2)
73#endif /* defined(WITH_CONTIKI) || defined(WITH_LWIP) */
74#endif /* COAP_DEBUG_BUF_SIZE */
75
76#ifndef COAP_DEFAULT_MAX_PDU_RX_SIZE
77#if defined(WITH_LWIP)
78#define COAP_DEFAULT_MAX_PDU_RX_SIZE (COAP_MAX_MESSAGE_SIZE_TCP16+4UL)
79#elif defined(WITH_CONTIKI)
80#define COAP_DEFAULT_MAX_PDU_RX_SIZE (UIP_APPDATA_SIZE)
81#elif (UINT_MAX < (8UL*1024*1024+256))
82#define COAP_DEFAULT_MAX_PDU_RX_SIZE (1500UL)
83#elif defined(RIOT_VERSION) && defined(COAP_DISABLE_TCP)
84#define COAP_DEFAULT_MAX_PDU_RX_SIZE (1500UL)
85#else
86/* 8 MiB max-message-size plus some space for options */
87#define COAP_DEFAULT_MAX_PDU_RX_SIZE (8UL*1024*1024+256)
88#endif
89#endif /* COAP_DEFAULT_MAX_PDU_RX_SIZE */
90
95#define COAP_DROPPED_RESPONSE -2
96
97#define COAP_PDU_DELAYED -3
98
99#define COAP_PAYLOAD_START 0xFF /* payload marker */
100
101#define COAP_PDU_IS_EMPTY(pdu) ((pdu)->code == 0)
102#define COAP_PDU_IS_REQUEST(pdu) (!COAP_PDU_IS_EMPTY(pdu) && (pdu)->code < 32)
103/* Code 1.xx (32-63) and 6.xx (192-224) currently invalid */
104#define COAP_PDU_IS_RESPONSE(pdu) ((pdu)->code >= 64 && (pdu)->code < 192)
105#define COAP_PDU_IS_SIGNALING(pdu) ((pdu)->code >= 224)
106#define COAP_PDU_IS_PING(pdu) ((COAP_PDU_IS_EMPTY(pdu) && \
107 ((pdu)->type == COAP_MESSAGE_CON)) || \
108 ((pdu)->code == COAP_SIGNALING_CODE_PING))
109
110#define COAP_PDU_MAX_UDP_HEADER_SIZE 4
111#define COAP_PDU_MAX_TCP_HEADER_SIZE 6
112
141 uint8_t max_hdr_size;
142 uint8_t hdr_size;
144 uint8_t crit_opt;
145 uint16_t max_opt;
146 uint32_t e_token_length;
148 unsigned ref;
150 size_t alloc_size;
152 size_t used_size;
154 size_t max_size;
156 uint8_t *token;
158 uint8_t *data;
159#ifdef WITH_LWIP
160 struct pbuf *pbuf;
168#endif
169 const uint8_t *body_data;
171 size_t body_length;
172 size_t body_offset;
173 size_t body_total;
178};
179
189int coap_pdu_resize(coap_pdu_t *pdu, size_t new_size);
190
200int coap_pdu_check_resize(coap_pdu_t *pdu, size_t new_size);
201
212 const uint8_t *data);
213
226 const uint8_t *data,
227 size_t length);
228
237
250
260void coap_pdu_clear(coap_pdu_t *pdu, size_t size);
261
283 coap_option_num_t number,
284 size_t len,
285 const uint8_t *data);
295
309 size_t len, const uint8_t *data);
310
323 coap_option_num_t number,
324 size_t len,
325 const uint8_t *data);
326
337
349 size_t len,
350 const uint8_t *data);
351
361
375 coap_session_t *session);
376
393 coap_session_t *session,
394 size_t token_length,
395 const uint8_t *token,
396 coap_opt_filter_t *drop_options);
397
400#endif /* COAP_COAP_PDU_INTERNAL_H_ */
Pulls together all the internal only header files.
uint16_t coap_option_num_t
Definition: coap_option.h:20
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:619
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:482
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:406
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:1066
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:982
coap_pdu_t * coap_new_pdu_lkd(coap_pdu_type_t type, coap_pdu_code_t code, coap_session_t *session)
Creates a new CoAP PDU.
Definition: coap_pdu.c:170
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:576
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
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:1328
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:713
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:1478
coap_pdu_t * coap_pdu_duplicate_lkd(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: coap_pdu.c:223
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:332
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:1013
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:290
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:769
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition: coap_pdu.h:263
coap_proto_t
CoAP protocol types.
Definition: coap_pdu.h:312
coap_pdu_code_t
Set of codes available for a PDU.
Definition: coap_pdu.h:326
coap_pdu_type_t
CoAP PDU message type definitions.
Definition: coap_pdu.h:68
CoAP binary data definition with const data.
Definition: coap_str.h:64
CoAP binary data definition.
Definition: coap_str.h:56
Structure to hold large body (many blocks) transmission information.
structure for CoAP PDUs
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 (or extended length bytes prefix), 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.
unsigned ref
reference count
coap_pdu_code_t code
request method (value 1–31) or response code (value 64-255)
uint8_t hdr_size
actual size used for protocol-specific header (0 until header is encoded)
coap_bin_const_t actual_token
Actual token in pdu.
uint8_t * data
first byte of payload, if any
coap_mid_t mid
message id, if any, in regular host byte order
uint32_t e_token_length
length of Token space (includes leading extended bytes
size_t used_size
used bytes of storage for token, options and payload
uint8_t crit_opt
Set if unknown critical option for proxy.
coap_binary_t * data_free
Data to be freed off by coap_delete_pdu()
size_t alloc_size
allocated storage for token, options and payload
coap_session_t * session
Session responsible for PDU or NULL.
size_t body_total
Holds body data total size.
coap_pdu_type_t type
message type
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...