Skip to main content

rtci_stun/
lib.rs

1#![warn(rust_2018_idioms)]
2#![warn(missing_docs)]
3#![allow(dead_code)]
4
5//! STUN for the Sans-I/O WebRTC stack.
6//!
7//! Session Traversal Utilities for NAT ([RFC 8489], which obsoletes [RFC 5389]), plus the
8//! attributes ICE ([RFC 8445]) and TURN ([RFC 5766]) layer on top. In WebRTC, STUN does
9//! double duty: it discovers a peer's server-reflexive address, and its binding
10//! request/response exchange *is* the ICE connectivity check.
11//!
12//! # Structure
13//!
14//! * [`message`] — [`Message`](message::Message), the STUN message itself: build one from
15//!   attributes, marshal it, unmarshal one off the wire.
16//! * [`attributes`], [`textattrs`], [`uattrs`], [`xoraddr`], [`error_code`] — the attribute
17//!   types, including `XOR-MAPPED-ADDRESS`, `USERNAME`, `REALM` and `ERROR-CODE`.
18//! * [`integrity`], [`fingerprint`] — `MESSAGE-INTEGRITY` (HMAC-SHA1),
19//!   `MESSAGE-INTEGRITY-SHA256` and `FINGERPRINT` (CRC-32), the attributes whose values depend
20//!   on the encoded message.
21//! * [`validated`] — **how a received message is checked and read**:
22//!   [`Validator`](validated::Validator) runs the FINGERPRINT, integrity and unknown-attribute
23//!   checks and returns a [`ValidatedMessage`](validated::ValidatedMessage) that shows only
24//!   the attributes the integrity covers.
25//! * [`long_term`], [`nonce_cookie`], [`password_algorithm`], [`userhash`], [`opaque`] — the
26//!   long-term credential mechanism of RFC 8489: challenge processing with bid-down
27//!   protection, `PASSWORD-ALGORITHM(S)`, `USERHASH`, and the OpaqueString profile.
28//! * [`alternate`] — the client side of 300 (Try Alternate): `ALTERNATE-SERVER`,
29//!   `ALTERNATE-DOMAIN` and the redirection-loop rule.
30//! * [`agent`], [`client`] — transaction tracking and a Sans-I/O client for talking to a
31//!   STUN server, with the retransmission timer of RFC 8489 section 6.2.1.
32//! * [`uri`] — parsing `stun:`/`stuns:` URLs.
33//! * [`checks`] — validation helpers for received messages.
34//!
35//! # Receiving a message
36//!
37//! An integrity attribute covers the message up to itself, so RFC 8489 section 9 has agents
38//! ignore every attribute after it. Reading attributes off a [`Message`](message::Message)
39//! that has only been through [`MessageIntegrity::check`](integrity::MessageIntegrity::check)
40//! does not do that. Receivers go through [`validated`]:
41//!
42//! ```
43//! # use rtci_stun::message::Message;
44//! # use rtci_stun::attributes::{ATTR_USERNAME, STUN_ATTRIBUTES};
45//! use rtci_stun::validated::{Rejection, Validator};
46//! # fn receive(received: &Message, key: &[u8], crypto: &dyn rtci_stun::crypto::RTCCrypto) {
47//! let validator = Validator::new().require_fingerprint().known_attributes(&STUN_ATTRIBUTES);
48//! match validator.authenticate(received, key, crypto) {
49//!     // Only attributes ahead of the integrity attribute can be read from `valid`.
50//!     Ok(valid) => { let _ = valid.get(ATTR_USERNAME); }
51//!     // A request: answer 420 with `validated::unknown_attributes_response`.
52//!     Err(Rejection::UnknownAttributes(_unknown)) => {}
53//!     // Anything else is dropped.
54//!     Err(_) => {}
55//! }
56//! # }
57//! ```
58//!
59//! # Example
60//!
61//! ```
62//! use rtci_stun::attributes::ATTR_SOFTWARE;
63//! use rtci_stun::message::{BINDING_REQUEST, Message, TransactionId};
64//! use rtci_stun::textattrs::TextAttribute;
65//!
66//! # fn example() -> Result<(), Box<dyn std::error::Error>> {
67//! let mut msg = Message::new();
68//! msg.build(&[
69//!     Box::new(TransactionId::new()),
70//!     Box::new(BINDING_REQUEST),
71//!     Box::new(TextAttribute::new(ATTR_SOFTWARE, "webrtc-rs".to_owned())),
72//! ])?;
73//!
74//! // `build` encodes as it goes, so `raw` is ready to send.
75//! assert!(!msg.raw.is_empty());
76//!
77//! let mut decoded = Message::new();
78//! decoded.raw = msg.raw.clone();
79//! decoded.decode()?;
80//! assert_eq!(decoded.typ, BINDING_REQUEST);
81//! # Ok(())
82//! # }
83//! ```
84//!
85//! Most applications do not depend on this crate directly — [`rtci-ice`] and
86//! [`rtci-turn`] build on it, and the [`rtci`](https://docs.rs/rtci) crate drives those.
87//!
88//! [RFC 8489]: https://datatracker.ietf.org/doc/html/rfc8489
89//! [RFC 5389]: https://datatracker.ietf.org/doc/html/rfc5389
90//! [RFC 3489]: https://datatracker.ietf.org/doc/html/rfc3489
91//! [RFC 8445]: https://datatracker.ietf.org/doc/html/rfc8445
92//! [RFC 5766]: https://datatracker.ietf.org/doc/html/rfc5766
93//! [`rtci-ice`]: https://docs.rs/rtci-ice
94//! [`rtci-turn`]: https://docs.rs/rtci-turn
95
96#[macro_use]
97extern crate lazy_static;
98
99/// The crypto provider API.
100///
101/// Re-exported because this crate's public constructors take an
102/// [`Arc<dyn RTCCryptoProvider>`](crypto::RTCCryptoProvider), which a caller must be able to name
103/// without adding — and version-matching — a direct `rtci-crypto` dependency.
104pub use crypto;
105
106/// Socket-address helpers shared by the address attributes.
107pub mod addr;
108/// Transaction tracking: which requests are outstanding and when they time out.
109pub mod agent;
110/// The client side of 300 (Try Alternate): `ALTERNATE-SERVER` and the redirection-loop rule.
111pub mod alternate;
112pub mod attributes;
113/// Validation helpers for received messages and attributes.
114pub mod checks;
115/// A Sans-I/O STUN client for talking to a STUN server.
116pub mod client;
117/// The `ERROR-CODE` attribute and the codes defined by STUN, TURN and ICE.
118pub mod error_code;
119/// The `FINGERPRINT` attribute, a CRC-32 over the message.
120pub mod fingerprint;
121/// The checks the fuzz targets in `fuzz/` run, shared with the replay test of
122/// `fuzz/regressions/`. Built with `cfg(fuzzing)` (cargo-fuzz) or for this crate's tests; not
123/// part of the supported API.
124#[cfg(any(fuzzing, test))]
125#[doc(hidden)]
126pub mod fuzz_oracles;
127#[cfg(test)]
128mod fuzz_regressions_test;
129/// The `MESSAGE-INTEGRITY` and `MESSAGE-INTEGRITY-SHA256` attributes, HMACs over the message.
130pub mod integrity;
131/// The client side of the long-term credential mechanism.
132pub mod long_term;
133/// The STUN message itself: header, attributes, and encoding.
134pub mod message;
135/// The nonce cookie and its STUN Security Feature bits.
136pub mod nonce_cookie;
137/// The OpaqueString profile (RFC 8265) for user names, realms and passwords.
138pub mod opaque;
139/// The `PASSWORD-ALGORITHM` and `PASSWORD-ALGORITHMS` attributes.
140pub mod password_algorithm;
141/// Text-valued attributes such as `USERNAME`, `REALM` and `SOFTWARE`.
142pub mod textattrs;
143/// The retransmission timer of RFC 8489 section 6.2, shared by every transaction layer.
144pub mod timer;
145/// The `UNKNOWN-ATTRIBUTES` attribute, listing attributes a server could not process.
146pub mod uattrs;
147/// Parsing `stun:` and `stuns:` URIs.
148pub mod uri;
149/// The `USERHASH` attribute.
150pub mod userhash;
151/// Checking a received message and reading only what its integrity covers.
152pub mod validated;
153/// The `XOR-MAPPED-ADDRESS` attribute, whose value is masked with the magic cookie.
154pub mod xoraddr;
155
156/// IANA assigned ports for "stun" protocol.
157pub const DEFAULT_PORT: u16 = 3478;
158/// The default port for `stuns:` (STUN over TLS/DTLS).
159pub const DEFAULT_TLS_PORT: u16 = 5349;