STUN module (rfc5389)

Message

STUN/TURN protocol message with method, class, transaction ID, and attributes.

Message

#include <icy/stun/message.h>

Inherits: IPacket Subclassed by: Request

STUN/TURN protocol message with method, class, transaction ID, and attributes.

Public Methods

ReturnNameDescription
MessageConstructs a default message (Request class, Undefined method) with a randomly generated 12-byte transaction ID.
MessageConstructs a message with explicit class and method.
MessageDeep-copy constructor; clones all attributes.
MessageMove constructor.
Message &operator=Deep-copy assignment; clones all attributes from that.
Message &operator=Move assignment.
std::unique_ptr< IPacket >clone virtual const#### Returns
voidsetClassSets the message class field.
voidsetMethodSets the message method field.
voidsetTransactionIDSets the 12-byte transaction ID.
ClassTypeclassType const#### Returns
MethodTypemethodType const#### Returns
const TransactionID &transactionID const inline#### Returns
size_tsize virtual const inline#### Returns
std::stringmethodString const#### Returns
std::stringclassString const#### Returns
std::stringerrorString constMaps a numeric error code to its canonical string description.
T &add inlineConstructs an attribute of type T in-place and appends it to the message. Returns a reference to the new attribute for further configuration.
voidaddAppends an attribute to the message, taking ownership via unique_ptr.
Attribute *get constReturns the Nth attribute of the given type, or nullptr if not found.
T *get const inlineType-safe attribute accessor using the concrete attribute's TypeID.
ssize_tread virtualParses a STUN/TURN packet from the given buffer.
voidwrite virtual constSerialises this message into a STUN/TURN wire-format packet.
std::stringtoString const#### Returns
voidprint virtual constWrites the same representation as toString() to the given stream.
const char *className virtual const inlineReturns the class name of this packet type for logging and diagnostics.

Message

Message()

Constructs a default message (Request class, Undefined method) with a randomly generated 12-byte transaction ID.


Message

Message(ClassType clss, MethodType meth)

Constructs a message with explicit class and method.

Parameters

  • clss Message class (Request, Indication, SuccessResponse, or ErrorResponse).

  • meth Message method (Binding, Allocate, Refresh, etc.).


Message

Message(const Message & that)

Deep-copy constructor; clones all attributes.


Message

Message(Message && that) noexcept

Move constructor.


operator=

Message & operator=(const Message & that)

Deep-copy assignment; clones all attributes from that.


operator=

Message & operator=(Message && that) noexcept

Move assignment.


clone

virtual const

virtual std::unique_ptr< IPacket > clone() const

Returns

A heap-allocated deep copy of this message.


setClass

void setClass(ClassType type)

Sets the message class field.

Parameters

  • type One of Request, Indication, SuccessResponse, ErrorResponse.

setMethod

void setMethod(MethodType type)

Sets the message method field.

Parameters

  • type One of the MethodType enumerators.

setTransactionID

void setTransactionID(const std::string & id)

Sets the 12-byte transaction ID.

Parameters

  • id Must be exactly kTransactionIdLength (12) bytes.

classType

const

ClassType classType() const

Returns

The message class.


methodType

const

MethodType methodType() const

Returns

The message method.


transactionID

const inline

inline const TransactionID & transactionID() const

Returns

Reference to the 12-byte transaction ID string.


size

virtual const inline

virtual inline size_t size() const

Returns

Total body size in bytes (sum of padded attribute headers and bodies).


methodString

const

std::string methodString() const

Returns

Human-readable method name (e.g. "BINDING", "ALLOCATE").


classString

const

std::string classString() const

Returns

Human-readable class name (e.g. "Request", "SuccessResponse").


errorString

const

std::string errorString(uint16_t errorCode) const

Maps a numeric error code to its canonical string description.

Parameters

  • errorCode One of the ErrorCodes enumerators.

Returns

Human-readable error string, or "UnknownError" if not recognised.


add

inline

template<typename T> inline T & add()

Constructs an attribute of type T in-place and appends it to the message. Returns a reference to the new attribute for further configuration.

Parameters

  • T Concrete attribute type (e.g. stun::Lifetime, stun::XorMappedAddress).

Returns

Reference to the newly added attribute.


add

void add(std::unique_ptr< Attribute > attr)

Appends an attribute to the message, taking ownership via unique_ptr.

Parameters


get

const

Attribute * get(Attribute::Type type, int index) const

Returns the Nth attribute of the given type, or nullptr if not found.

Parameters

  • type Attribute type code to search for.

  • index Zero-based occurrence index (0 = first match).

Returns

Raw pointer to the attribute (owned by this message), or nullptr.


get

const inline

template<typename T> inline T * get(int index) const

Type-safe attribute accessor using the concrete attribute's TypeID.

Parameters

  • T Concrete attribute type (must define TypeID).

Parameters

  • index Zero-based occurrence index.

Returns

Pointer to T, or nullptr if the attribute is absent.


read

virtual

virtual ssize_t read(const ConstBuffer & buf)

Parses a STUN/TURN packet from the given buffer.

Parameters

  • buf Buffer containing at least one complete STUN message.

Returns

Number of bytes consumed, or 0 on parse failure.


write

virtual const

virtual void write(Buffer & buf) const

Serialises this message into a STUN/TURN wire-format packet.

Parameters

  • buf Destination buffer; data is appended.

toString

const

std::string toString() const

Returns

A concise string representation for logging (method, transaction ID, attribute types).


print

virtual const

virtual void print(std::ostream & os) const

Writes the same representation as toString() to the given stream.

Parameters

  • os Output stream.

className

virtual const inline

virtual inline const char * className() const

Returns the class name of this packet type for logging and diagnostics.

Protected Attributes

ReturnNameDescription
uint16_t_class
uint16_t_method
uint16_t_sizeSet by read(); write() uses computeBodySize() instead.
TransactionID_transactionID
std::vector< std::unique_ptr< Attribute > >_attrs

_class

uint16_t _class

_method

uint16_t _method

_size

uint16_t _size

Set by read(); write() uses computeBodySize() instead.


_transactionID

TransactionID _transactionID

_attrs

std::vector< std::unique_ptr< Attribute > > _attrs

Public Types


MethodType

enum MethodType
ValueDescription
Undefineddefault error type
BindingSTUN.
AllocateTURN.
Refresh
SendIndication(only indication semantics defined)
DataIndication(only indication semantics defined)
CreatePermission(only request/response semantics defined)
ChannelBind(only request/response semantics defined)
ConnectTURN TCP RFC 6062.
ConnectionBind
ConnectionAttempt

ClassType

enum ClassType
ValueDescription
Request
Indication
SuccessResponse
ErrorResponse

ErrorCodes

enum ErrorCodes
ValueDescription
TryAlternate
BadRequest
NotAuthorized
Forbidden
UnknownAttribute
StaleCredentials
IntegrityCheckFailure
MissingUsername
UseTLS
AllocationMismatch
StaleNonce
WrongCredentials
UnsupportedTransport
AllocationQuotaReached
RoleConflict
ServerError
InsufficientCapacity
GlobalFailure
ConnectionAlreadyExistsTURN TCP.
ConnectionTimeoutOrFailure

Private Methods

ReturnNameDescription
uint16_tcomputeBodySize constComputes the wire body size from the current attribute list.

computeBodySize

const

uint16_t computeBodySize() const

Computes the wire body size from the current attribute list.