Skip to main content

Crate rtc_stun

Crate rtc_stun 

Source
Expand description

STUN for the Sans-I/O WebRTC stack.

Session Traversal Utilities for NAT (RFC 5389, superseding RFC 3489), 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

  • messageMessage, the STUN message itself: build one from attributes, marshal it, unmarshal one off the wire.
  • attributes, textattrs, uattrs, xoraddr, error_code — the attribute types, including XOR-MAPPED-ADDRESS, USERNAME, REALM and ERROR-CODE.
  • integrity, fingerprintMESSAGE-INTEGRITY (HMAC-SHA1) and FINGERPRINT (CRC-32), the two attributes whose values depend on the encoded message.
  • agent, client — transaction tracking and a Sans-I/O client for talking to a STUN server.
  • uri — parsing stun:/stuns: URLs.
  • checks — validation helpers for received messages.

§Example

use rtc_stun::attributes::ATTR_SOFTWARE;
use rtc_stun::message::{BINDING_REQUEST, Message, TransactionId};
use rtc_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 — rtc-ice and rtc-turn build on it, and the rtc crate drives those.

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.
attributes
The STUN attribute types and the raw attribute representation. 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-CODE attribute and the codes defined by STUN, TURN and ICE. The ERROR-CODE attribute.
fingerprint
The FINGERPRINT attribute, a CRC-32 over the message.
integrity
The MESSAGE-INTEGRITY attribute, an HMAC-SHA1 over the message.
message
The STUN message itself: header, attributes, and encoding. The STUN message: header, attributes, and encoding.
textattrs
Text-valued attributes such as USERNAME, REALM and SOFTWARE.
uattrs
The UNKNOWN-ATTRIBUTES attribute, listing attributes a server could not process.
uri
Parsing stun: and stuns: URIs.
xoraddr
The XOR-MAPPED-ADDRESS attribute, 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).