Skip to main content

parse_binding_response

Function parse_binding_response 

Source
pub fn parse_binding_response(
    msg: &[u8],
    expected_txid: Option<&TransactionId>,
) -> Result<SocketAddr, StunError>
Expand description

Parse a STUN Binding success response, returning the reflexive SocketAddr from its XOR-MAPPED-ADDRESS (preferred) or legacy MAPPED-ADDRESS attribute (SPEC.md §2.4).

A PURE parser: it does NOT apply the address-usability guard (SPEC.md §5) — that is crate::query_reflexive_address’s job, layered on top of this parse.

Validates, in order: length ≥ 20 (StunError::Truncated); the magic cookie (checked BEFORE the message type, so a non-STUN datagram is never misreported as an unexpected STUN type); the message type is BINDING_SUCCESS; when expected_txid is Some, the transaction id matches; the declared message length fits the datagram; then walks the TLV attributes, returning the FIRST XOR-MAPPED-ADDRESS as soon as one is seen, else the first MAPPED-ADDRESS, else StunError::NoMappedAddress.