Loading...
Searching...
No Matches
coap_pdu_internal.h
Go to the documentation of this file.
/*
* coap_pdu_internal.h -- CoAP PDU structure
*
* Copyright (C) 2010-2024 Olaf Bergmann <bergmann@tzi.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
#ifndef COAP_COAP_PDU_INTERNAL_H_
#define COAP_COAP_PDU_INTERNAL_H_
#include "coap_internal.h"
#ifdef WITH_LWIP
#include <lwip/pbuf.h>
#endif
#ifdef RIOT_VERSION
#include <limits.h>
#endif /* RIOT_VERSION */
#ifdef HAVE_LIMITS_H
#include <limits.h>
#endif /* HAVE_LIMITS_H */
#include <stdint.h>
#define COAP_DEFAULT_VERSION 1 /* version of CoAP supported */
/* TCP Message format constants, do not modify */
#define COAP_MESSAGE_SIZE_OFFSET_TCP8 13
#define COAP_MESSAGE_SIZE_OFFSET_TCP16 269 /* 13 + 256 */
#define COAP_MESSAGE_SIZE_OFFSET_TCP32 65805 /* 269 + 65536 */
/* Derived message size limits */
#define COAP_MAX_MESSAGE_SIZE_TCP0 (COAP_MESSAGE_SIZE_OFFSET_TCP8-1) /* 12 */
#define COAP_MAX_MESSAGE_SIZE_TCP8 (COAP_MESSAGE_SIZE_OFFSET_TCP16-1) /* 268 */
#define COAP_MAX_MESSAGE_SIZE_TCP16 (COAP_MESSAGE_SIZE_OFFSET_TCP32-1) /* 65804 */
#define COAP_MAX_MESSAGE_SIZE_TCP32 (COAP_MESSAGE_SIZE_OFFSET_TCP32+0xFFFFFFFF)
#if COAP_OSCORE_SUPPORT
/* for oscore encryption */
#define COAP_MAX_CHUNK_SIZE COAP_DEFAULT_MAX_PDU_RX_SIZE
#define OSCORE_CRYPTO_BUFFER_SIZE (COAP_MAX_CHUNK_SIZE+16)
#endif /* COAP_OSCORE_SUPPORT */
/* Extended Token constants */
#define COAP_TOKEN_EXT_1B_TKL 13
#define COAP_TOKEN_EXT_2B_TKL 14
#define COAP_TOKEN_EXT_1B_BIAS 13
#define COAP_TOKEN_EXT_2B_BIAS 269 /* 13 + 256 */
#ifndef COAP_DEBUG_BUF_SIZE
#if defined(WITH_CONTIKI) || defined(WITH_LWIP)
#define COAP_DEBUG_BUF_SIZE 128
#else /* defined(WITH_CONTIKI) || defined(WITH_LWIP) */
/* 1024 derived from RFC7252 4.6. Message Size max payload */
#define COAP_DEBUG_BUF_SIZE (8 + 1024 * 2)
#endif /* defined(WITH_CONTIKI) || defined(WITH_LWIP) */
#endif /* COAP_DEBUG_BUF_SIZE */
#ifndef COAP_DEFAULT_MAX_PDU_RX_SIZE
#if defined(WITH_LWIP)
#define COAP_DEFAULT_MAX_PDU_RX_SIZE (COAP_MAX_MESSAGE_SIZE_TCP16+4UL)
#elif defined(WITH_CONTIKI)
#define COAP_DEFAULT_MAX_PDU_RX_SIZE (sizeof(coap_packet_t) + UIP_APPDATA_SIZE)
#elif (UINT_MAX < (8UL*1024*1024+256))
#define COAP_DEFAULT_MAX_PDU_RX_SIZE (1500UL)
#elif defined(RIOT_VERSION) && defined(COAP_DISABLE_TCP)
#define COAP_DEFAULT_MAX_PDU_RX_SIZE (1500UL)
#else
/* 8 MiB max-message-size plus some space for options */
#define COAP_DEFAULT_MAX_PDU_RX_SIZE (8UL*1024*1024+256)
#endif
#endif /* COAP_DEFAULT_MAX_PDU_RX_SIZE */
#define COAP_DROPPED_RESPONSE -2
#define COAP_PDU_DELAYED -3
#define COAP_PAYLOAD_START 0xFF /* payload marker */
#define COAP_PDU_IS_EMPTY(pdu) ((pdu)->code == 0)
#define COAP_PDU_IS_REQUEST(pdu) (!COAP_PDU_IS_EMPTY(pdu) && (pdu)->code < 32)
/* Code 1.xx (32-63) and 6.xx (192-224) currently invalid */
#define COAP_PDU_IS_RESPONSE(pdu) ((pdu)->code >= 64 && (pdu)->code < 192)
#define COAP_PDU_IS_SIGNALING(pdu) ((pdu)->code >= 224)
#define COAP_PDU_IS_PING(pdu) ((COAP_PDU_IS_EMPTY(pdu) && \
((pdu)->type == COAP_MESSAGE_CON)) || \
((pdu)->code == COAP_SIGNALING_CODE_PING))
#define COAP_PDU_MAX_UDP_HEADER_SIZE 4
#define COAP_PDU_MAX_TCP_HEADER_SIZE 6
struct coap_pdu_t {
coap_pdu_type_t type;
coap_pdu_code_t code;
coap_mid_t mid;
uint8_t max_hdr_size;
uint8_t hdr_size;
uint8_t crit_opt;
uint16_t max_opt;
uint32_t e_token_length;
coap_bin_const_t actual_token;
size_t alloc_size;
size_t used_size;
size_t max_size;
uint8_t *token;
uint8_t *data;
#ifdef WITH_LWIP
struct pbuf *pbuf;
#endif
const uint8_t *body_data;
size_t body_length;
size_t body_offset;
size_t body_total;
coap_lg_xmit_t *lg_xmit;
coap_session_t *session;
};
int coap_pdu_resize(coap_pdu_t *pdu, size_t new_size);
int coap_pdu_check_resize(coap_pdu_t *pdu, size_t new_size);
size_t coap_pdu_parse_header_size(coap_proto_t proto,
const uint8_t *data);
size_t coap_pdu_parse_size(coap_proto_t proto,
const uint8_t *data,
size_t length);
int coap_pdu_parse_header(coap_pdu_t *pdu, coap_proto_t proto);
int coap_pdu_parse_opt(coap_pdu_t *pdu);
void coap_pdu_clear(coap_pdu_t *pdu, size_t size);
size_t coap_add_option_internal(coap_pdu_t *pdu,
coap_option_num_t number,
size_t len,
const uint8_t *data);
int coap_remove_option(coap_pdu_t *pdu, coap_option_num_t number);
size_t coap_insert_option(coap_pdu_t *pdu, coap_option_num_t number,
size_t len, const uint8_t *data);
size_t coap_update_option(coap_pdu_t *pdu,
coap_option_num_t number,
size_t len,
const uint8_t *data);
size_t coap_pdu_encode_header(coap_pdu_t *pdu, coap_proto_t proto);
int coap_update_token(coap_pdu_t *pdu,
size_t len,
const uint8_t *data);
int coap_option_check_repeatable(coap_option_num_t number);
coap_pdu_t *coap_new_pdu_lkd(coap_pdu_type_t type, coap_pdu_code_t code,
coap_session_t *session);
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);
#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:610
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:473
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:397
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:1057
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:973
coap_new_pdu_lkd
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:166
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:567
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:1319
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:704
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:1469
coap_pdu_duplicate_lkd
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:214
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:323
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:1004
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:281
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:760
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:263
coap_proto_t
coap_proto_t
CoAP protocol types.
Definition coap_pdu.h:312
coap_pdu_code_t
coap_pdu_code_t
Set of codes available for a PDU.
Definition coap_pdu.h:326
coap_pdu_type_t
coap_pdu_type_t
CoAP PDU message type definitions.
Definition coap_pdu.h:68
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:144
coap_opt_filter_t
Definition coap_option.h:101
coap_pdu_t
structure for CoAP PDUs
Definition coap_pdu_internal.h:135
coap_pdu_t::max_hdr_size
uint8_t max_hdr_size
space reserved for protocol-specific header
Definition coap_pdu_internal.h:141
coap_pdu_t::max_opt
uint16_t max_opt
highest option number in PDU
Definition coap_pdu_internal.h:145
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:155
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:172
coap_pdu_t::body_length
size_t body_length
Holds body data length.
Definition coap_pdu_internal.h:169
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:153
coap_pdu_t::body_data
const uint8_t * body_data
Holds ptr to re-assembled data or NULL.
Definition coap_pdu_internal.h:168
coap_pdu_t::body_offset
size_t body_offset
Holds body data offset.
Definition coap_pdu_internal.h:170
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:137
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:142
coap_pdu_t::actual_token
coap_bin_const_t actual_token
Actual token in pdu.
Definition coap_pdu_internal.h:148
coap_pdu_t::data
uint8_t * data
first byte of payload, if any
Definition coap_pdu_internal.h:157
coap_pdu_t::mid
coap_mid_t mid
message id, if any, in regular host byte order
Definition coap_pdu_internal.h:139
coap_pdu_t::e_token_length
uint32_t e_token_length
length of Token space (includes leading extended bytes
Definition coap_pdu_internal.h:146
coap_pdu_t::used_size
size_t used_size
used bytes of storage for token, options and payload
Definition coap_pdu_internal.h:151
coap_pdu_t::crit_opt
uint8_t crit_opt
Set if unknown critical option for proxy.
Definition coap_pdu_internal.h:144
coap_pdu_t::alloc_size
size_t alloc_size
allocated storage for token, options and payload
Definition coap_pdu_internal.h:149
coap_pdu_t::session
coap_session_t * session
Session responsible for PDU or NULL.
Definition coap_pdu_internal.h:174
coap_pdu_t::body_total
size_t body_total
Holds body data total size.
Definition coap_pdu_internal.h:171
coap_pdu_t::type
coap_pdu_type_t type
message type
Definition coap_pdu_internal.h:136
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