Expand description
STUN for the Sans-I/O WebRTC stack.
Session Traversal Utilities for NAT (RFC 8489, which obsoletes RFC 5389), plus the attributes ICE (RFC 8445) and TURN (RFC 5766) layer on top. In WebRTC, STUN does double duty: it discovers a peer’s server-reflexive address, and its binding request/response exchange is the ICE connectivity check.
§Structure
message—Message, the STUN message itself: build one from attributes, marshal it, unmarshal one off the wire.attributes,textattrs,uattrs,xoraddr,error_code— the attribute types, includingXOR-MAPPED-ADDRESS,USERNAME,REALMandERROR-CODE.integrity,fingerprint—MESSAGE-INTEGRITY(HMAC-SHA1),MESSAGE-INTEGRITY-SHA256andFINGERPRINT(CRC-32), the attributes whose values depend on the encoded message.validated— how a received message is checked and read:Validatorruns the FINGERPRINT, integrity and unknown-attribute checks and returns aValidatedMessagethat shows only the attributes the integrity covers.long_term,nonce_cookie,password_algorithm,userhash,opaque— the long-term credential mechanism of RFC 8489: challenge processing with bid-down protection,PASSWORD-ALGORITHM(S),USERHASH, and the OpaqueString profile.alternate— the client side of 300 (Try Alternate):ALTERNATE-SERVER,ALTERNATE-DOMAINand the redirection-loop rule.agent,client— transaction tracking and a Sans-I/O client for talking to a STUN server, with the retransmission timer of RFC 8489 section 6.2.1.uri— parsingstun:/stuns:URLs.checks— validation helpers for received messages.
§Receiving a message
An integrity attribute covers the message up to itself, so RFC 8489 section 9 has agents
ignore every attribute after it. Reading attributes off a Message
that has only been through MessageIntegrity::check
does not do that. Receivers go through validated:
use rtci_stun::validated::{Rejection, Validator};
let validator = Validator::new().require_fingerprint().known_attributes(&STUN_ATTRIBUTES);
match validator.authenticate(received, key, crypto) {
// Only attributes ahead of the integrity attribute can be read from `valid`.
Ok(valid) => { let _ = valid.get(ATTR_USERNAME); }
// A request: answer 420 with `validated::unknown_attributes_response`.
Err(Rejection::UnknownAttributes(_unknown)) => {}
// Anything else is dropped.
Err(_) => {}
}§Example
use rtci_stun::attributes::ATTR_SOFTWARE;
use rtci_stun::message::{BINDING_REQUEST, Message, TransactionId};
use rtci_stun::textattrs::TextAttribute;
let mut msg = Message::new();
msg.build(&[
Box::new(TransactionId::new()),
Box::new(BINDING_REQUEST),
Box::new(TextAttribute::new(ATTR_SOFTWARE, "webrtc-rs".to_owned())),
])?;
// `build` encodes as it goes, so `raw` is ready to send.
assert!(!msg.raw.is_empty());
let mut decoded = Message::new();
decoded.raw = msg.raw.clone();
decoded.decode()?;
assert_eq!(decoded.typ, BINDING_REQUEST);Most applications do not depend on this crate directly — rtci-ice and
rtci-turn build on it, and the rtci crate drives those.
Re-exports§
pub use crypto;
Modules§
- addr
- Socket-address helpers shared by the address attributes.
- agent
- Transaction tracking: which requests are outstanding and when they time out. STUN transaction tracking.
- alternate
- The client side of 300 (Try Alternate):
ALTERNATE-SERVERand the redirection-loop rule. The client side of the ALTERNATE-SERVER mechanism (RFC 8489 section 10): reading a 300 (Try Alternate) error response, and refusing a redirection loop. - attributes
- STUN attribute types.
- checks
- Validation helpers for received messages and attributes.
- client
- A Sans-I/O STUN client for talking to a STUN server. A Sans-I/O STUN client.
- error_
code - The
ERROR-CODEattribute and the codes defined by STUN, TURN and ICE. TheERROR-CODEattribute. - fingerprint
- The
FINGERPRINTattribute, a CRC-32 over the message. - integrity
- The
MESSAGE-INTEGRITYandMESSAGE-INTEGRITY-SHA256attributes, HMACs over the message.MESSAGE-INTEGRITYandMESSAGE-INTEGRITY-SHA256(RFC 8489 sections 14.5 and 14.6), and the keys of the two credential mechanisms (sections 9.1.1, 9.2.2 and 18.5.1). - long_
term - The client side of the long-term credential mechanism. The client side of the long-term credential mechanism (RFC 8489 section 9.2), as data and rules without I/O.
- message
- The STUN message itself: header, attributes, and encoding. The STUN message: header, attributes, and encoding.
- nonce_
cookie - The nonce cookie and its STUN Security Feature bits. The nonce cookie and the STUN Security Features it carries (RFC 8489 sections 9.2, 9.2.1 and 18.1).
- opaque
- The OpaqueString profile (RFC 8265) for user names, realms and passwords. The OpaqueString profile of the PRECIS framework (RFC 8265 section 4.2), which RFC 8489 applies to user names, realms and passwords before they are compared, hashed or used as a key (sections 9.1.1, 9.2.2, 14.3, 14.4 and 14.9).
- password_
algorithm - The
PASSWORD-ALGORITHMandPASSWORD-ALGORITHMSattributes.PASSWORD-ALGORITHMandPASSWORD-ALGORITHMS(RFC 8489 sections 14.11 and 14.12): the algorithm that turns a long-term password into the HMAC key, and the list a server offers. - textattrs
- Text-valued attributes such as
USERNAME,REALMandSOFTWARE. - timer
- The retransmission timer of RFC 8489 section 6.2, shared by every transaction layer. The retransmission timer of RFC 8489 section 6.2, as data and arithmetic without I/O.
- uattrs
- The
UNKNOWN-ATTRIBUTESattribute, listing attributes a server could not process. - uri
- Parsing
stun:andstuns:URIs. - userhash
- The
USERHASHattribute. TheUSERHASHattribute (RFC 8489 section 14.4), which replaces USERNAME when the server supports username anonymity. - validated
- Checking a received message and reading only what its integrity covers. Receiving a STUN message safely: the checks of RFC 8489 sections 6.3, 9 and 14.7 in one place, with a result that only shows what the checks cover.
- xoraddr
- The
XOR-MAPPED-ADDRESSattribute, whose value is masked with the magic cookie.
Constants§
- DEFAULT_
PORT - IANA assigned ports for “stun” protocol.
- DEFAULT_
TLS_ PORT - The default port for
stuns:(STUN over TLS/DTLS).