STUN module (rfc5389)

stun

STUN messages, attributes, and transaction helpers defined by RFC 5389.

stun

STUN messages, attributes, and transaction helpers defined by RFC 5389.

Classes

NameDescription
AddressAttributeImplements a STUN/TURN attribute that contains a socket address. Handles XOR encoding/decoding for address and port as required by RFC 5389 section 15.2.
AttributeThe virtual base class for all STUN/TURN attributes.
ErrorCodeImplements the STUN ERROR-CODE attribute (RFC 5389 section 15.6). Encodes a 3-digit error code as a class (hundreds digit) and number (tens + units digits), plus an optional UTF-8 reason phrase.
FlagAttributeImplements a zero-length STUN/TURN flag attribute (presence implies the flag is set).
MessageSTUN/TURN protocol message with method, class, transaction ID, and attributes.
MessageIntegrityImplements the STUN MESSAGE-INTEGRITY attribute (RFC 5389 section 15.4). On write, computes an HMAC-SHA1 over the message bytes preceding this attribute when a key is set. On read, captures the raw HMAC bytes and the input bytes needed to verify them later via verifyHmac().
StringAttributeImplements a STUN/TURN attribute that holds an arbitrary byte string. Used for Username, Password, Realm, Nonce, Software, Data, and similar attributes.
TransactionSTUN request/response transaction with timeout and retry logic. Extends the generic net::Transaction with STUN-specific transaction ID matching and response class inference (Success, Error, or Indication).
UInt16ListAttributeImplements a STUN/TURN attribute that holds a list of attribute type codes. Used by the UNKNOWN-ATTRIBUTES attribute (RFC 5389 section 15.9).
UInt32AttributeImplements a STUN/TURN attribute that holds a 32-bit integer.
UInt64AttributeImplements a STUN/TURN attribute that holds a 64-bit integer.
UInt8AttributeImplements a STUN/TURN attribute that holds an 8-bit integer.

Enumerations

NameDescription
AddressFamilySTUN address types as defined in RFC 5389. NB: Undefined is not part of the STUN spec.

AddressFamily

enum AddressFamily

STUN address types as defined in RFC 5389. NB: Undefined is not part of the STUN spec.

ValueDescription
UndefinedNot a valid STUN address family; used as a sentinel.
IPv4IPv4 transport address.
IPv6IPv6 transport address.

Typedefs

ReturnNameDescription
std::stringTransactionIDFixed-width 12-byte STUN transaction identifier stored as raw bytes.

TransactionID

std::string TransactionID()

Fixed-width 12-byte STUN transaction identifier stored as raw bytes.

Functions

ReturnNameDescription
constexpr boolisValidMethodReturns true if methodType corresponds to a recognised STUN/TURN method. Used during parsing to reject malformed packets.

isValidMethod

constexpr bool isValidMethod(uint16_t methodType)

Returns true if methodType corresponds to a recognised STUN/TURN method. Used during parsing to reject malformed packets.

Parameters

  • methodType Raw method bits extracted from the message type field.

Returns

true if the method is one of the defined MethodType values.

Variables

ReturnNameDescription
constexpr intkAttributeHeaderSizeBytes in a STUN attribute header.
constexpr intkMessageHeaderSizeBytes in the fixed STUN message header.
constexpr intkTransactionIdOffsetByte offset of the transaction ID inside the message header.
constexpr intkTransactionIdLengthLength in bytes of a STUN transaction ID.
constexpr uint32_tkMagicCookieRFC 5389 magic cookie used by modern STUN/TURN messages.
constexpr intkMagicCookieLengthLength in bytes of the magic cookie field.

kAttributeHeaderSize

constexpr int kAttributeHeaderSize = 4

Bytes in a STUN attribute header.


kMessageHeaderSize

constexpr int kMessageHeaderSize = 20

Bytes in the fixed STUN message header.


kTransactionIdOffset

constexpr int kTransactionIdOffset = 8

Byte offset of the transaction ID inside the message header.


kTransactionIdLength

constexpr int kTransactionIdLength = 12

Length in bytes of a STUN transaction ID.


kMagicCookie

constexpr uint32_t kMagicCookie = 0x2112A442

RFC 5389 magic cookie used by modern STUN/TURN messages.


kMagicCookieLength

constexpr int kMagicCookieLength = sizeof()

Length in bytes of the magic cookie field.