13#ifndef ZEPHYR_INCLUDE_NET_STUN_H_
14#define ZEPHYR_INCLUDE_NET_STUN_H_
41#define STUN_MAGIC_COOKIE 0x2112A442UL
43#define STUN_HEADER_SIZE 20U
45#define STUN_TXID_SIZE 12U
48#define STUN_INTEGRITY_LEN 20U
50#define STUN_INTEGRITY_SHA256_MIN_LEN 16U
52#define STUN_INTEGRITY_SHA256_MAX_LEN 32U
58#define STUN_TEXT_MAX_LEN 763U
60#define STUN_USERNAME_MAX_LEN 508U
65#define STUN_TEXT_MAX_CHARS 127U
72#define STUN_BINDING_REQUEST 0x0001U
74#define STUN_BINDING_SUCCESS_RESPONSE 0x0101U
76#define STUN_BINDING_ERROR_RESPONSE 0x0111U
80#define STUN_METHOD_BINDING 0x001U
101#define STUN_ATTR_MAPPED_ADDRESS 0x0001U
103#define STUN_ATTR_USERNAME 0x0006U
105#define STUN_ATTR_MESSAGE_INTEGRITY 0x0008U
107#define STUN_ATTR_ERROR_CODE 0x0009U
109#define STUN_ATTR_UNKNOWN_ATTRIBUTES 0x000AU
111#define STUN_ATTR_REALM 0x0014U
113#define STUN_ATTR_NONCE 0x0015U
115#define STUN_ATTR_MESSAGE_INTEGRITY_SHA256 0x001CU
117#define STUN_ATTR_XOR_MAPPED_ADDRESS 0x0020U
119#define STUN_ATTR_PRIORITY 0x0024U
121#define STUN_ATTR_USE_CANDIDATE 0x0025U
123#define STUN_ATTR_SOFTWARE 0x8022U
125#define STUN_ATTR_FINGERPRINT 0x8028U
127#define STUN_ATTR_ICE_CONTROLLED 0x8029U
129#define STUN_ATTR_ICE_CONTROLLING 0x802AU
192 size_t integrity_off;
194 size_t integrity_sha256_off;
198 size_t fingerprint_off;
uint16_t stun_msg_type_compose(uint16_t method, enum stun_class msg_class)
Compose a message type.
int stun_add_error_code(struct stun_builder *builder, uint16_t code, const char *reason)
Add ERROR-CODE: an error code and its reason phrase.
int stun_add_integrity_sha256(struct stun_builder *builder, const uint8_t *key, size_t key_len)
Add MESSAGE-INTEGRITY-SHA256.
int stun_add_integrity(struct stun_builder *builder, const uint8_t *key, size_t key_len)
Add MESSAGE-INTEGRITY.
int stun_add_priority(struct stun_builder *builder, uint32_t priority)
Add PRIORITY.
int stun_finish(struct stun_builder *builder, const uint8_t *key, size_t key_len)
Close a message with MESSAGE-INTEGRITY and FINGERPRINT.
int stun_builder_init(struct stun_builder *builder, uint8_t *buf, size_t buf_size, uint16_t msg_type, const uint8_t transaction_id[12U])
Start a message.
int stun_parse(const uint8_t *buf, size_t len, struct stun_msg *out)
Parse one datagram.
int stun_add_ice_controlling(struct stun_builder *builder, uint64_t tiebreaker)
Add ICE-CONTROLLING.
uint16_t stun_msg_type_get_method(uint16_t type)
Get the method of a message type.
#define STUN_TXID_SIZE
Size of the transaction id.
Definition stun.h:45
bool stun_has_valid_header(const uint8_t *buf, size_t len)
Check that a datagram starts with a valid STUN header.
int stun_add_xor_mapped_address(struct stun_builder *builder, const struct net_sockaddr *addr)
Add XOR-MAPPED-ADDRESS.
enum stun_class stun_msg_type_get_class(uint16_t type)
Get the class of a message type.
int stun_builder_len(const struct stun_builder *builder)
Get the length of the message built so far.
int stun_verify_fingerprint(const struct stun_msg *msg)
Verify FINGERPRINT.
int stun_add_username(struct stun_builder *builder, const char *username)
Add USERNAME.
int stun_add_software(struct stun_builder *builder, const char *sw)
Add SOFTWARE.
int stun_add_ice_controlled(struct stun_builder *builder, uint64_t tiebreaker)
Add ICE-CONTROLLED.
stun_class
Message classes (RFC 8489, section-5).
Definition stun.h:83
int stun_add_attr(struct stun_builder *builder, uint16_t attr_type, const uint8_t *value, size_t value_len)
Add an attribute by type and value.
int stun_add_fingerprint(struct stun_builder *builder)
Add FINGERPRINT.
int stun_verify_integrity(const struct stun_msg *msg, const uint8_t *key, size_t key_len)
Verify MESSAGE-INTEGRITY.
int stun_add_xor_address(struct stun_builder *builder, uint16_t attr_type, const struct net_sockaddr *addr)
Add an attribute in the XOR-MAPPED-ADDRESS format.
int stun_find_attr(const struct stun_msg *msg, uint16_t attr_type, const uint8_t **value, uint16_t *value_len)
Find an attribute by type.
int stun_get_xor_mapped_address(const struct stun_msg *msg, struct net_sockaddr *addr)
Decode XOR-MAPPED-ADDRESS.
int stun_verify_integrity_sha256(const struct stun_msg *msg, const uint8_t *key, size_t key_len, size_t min_len)
Verify MESSAGE-INTEGRITY-SHA256.
int stun_add_unknown_attributes(struct stun_builder *builder, const uint16_t *types, size_t n_types)
Add UNKNOWN-ATTRIBUTES.
int stun_add_use_candidate(struct stun_builder *builder)
Add USE-CANDIDATE.
int stun_unknown_attrs(const struct stun_msg *msg, const uint16_t *known, size_t n_known, uint16_t *out, size_t cap)
List the comprehension-required attributes nobody understands.
int stun_finish_plain(struct stun_builder *builder)
Close a message with FINGERPRINT alone.
int stun_get_xor_address(const struct stun_msg *msg, uint16_t attr_type, struct net_sockaddr *addr)
Decode an attribute in the XOR-MAPPED-ADDRESS format.
@ STUN_CLASS_SUCCESS
Success response.
Definition stun.h:89
@ STUN_CLASS_INDICATION
Indication.
Definition stun.h:87
@ STUN_CLASS_ERROR
Error response.
Definition stun.h:91
@ STUN_CLASS_REQUEST
Request.
Definition stun.h:85
IPv6 and IPv4 definitions.
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT64_TYPE__ uint64_t
Definition stdint.h:91
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
Generic sockaddr struct.
Definition net_ip.h:462
A message under construction.
Definition stun.h:469
A parsed message.
Definition stun.h:141
uint32_t priority
PRIORITY value.
Definition stun.h:169
bool has_integrity_sha256
MESSAGE-INTEGRITY-SHA256 is present.
Definition stun.h:161
bool has_fingerprint
FINGERPRINT is present.
Definition stun.h:164
uint8_t txid[12U]
Transaction id.
Definition stun.h:147
bool has_use_candidate
USE-CANDIDATE is present.
Definition stun.h:172
uint16_t error_code
Error code: class * 100 + number, 300 to 699.
Definition stun.h:177
bool has_ice_controlled
ICE-CONTROLLED is present.
Definition stun.h:182
const uint8_t * username
USERNAME value as received: not terminated and not validated, the parser checks its length,...
Definition stun.h:153
bool has_priority
PRIORITY is present.
Definition stun.h:167
bool has_ice_controlling
ICE-CONTROLLING is present.
Definition stun.h:180
uint16_t length
Header Length field: the size of the attributes.
Definition stun.h:145
uint16_t type
Message type: method and class.
Definition stun.h:143
uint64_t ice_tiebreaker
Tie-breaker of whichever role attribute is present.
Definition stun.h:184
uint16_t username_len
Length of the USERNAME value.
Definition stun.h:155
bool has_integrity
MESSAGE-INTEGRITY is present.
Definition stun.h:158
bool has_error_code
ERROR-CODE is present.
Definition stun.h:175