1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//! 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`](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, including `XOR-MAPPED-ADDRESS`, `USERNAME`, `REALM` and `ERROR-CODE`.
//! * [`integrity`], [`fingerprint`] — `MESSAGE-INTEGRITY` (HMAC-SHA1),
//! `MESSAGE-INTEGRITY-SHA256` and `FINGERPRINT` (CRC-32), the attributes whose values depend
//! on the encoded message.
//! * [`validated`] — **how a received message is checked and read**:
//! [`Validator`](validated::Validator) runs the FINGERPRINT, integrity and unknown-attribute
//! checks and returns a [`ValidatedMessage`](validated::ValidatedMessage) that 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-DOMAIN` and 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`] — parsing `stun:`/`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`](message::Message)
//! that has only been through [`MessageIntegrity::check`](integrity::MessageIntegrity::check)
//! does not do that. Receivers go through [`validated`]:
//!
//! ```
//! # use rtci_stun::message::Message;
//! # use rtci_stun::attributes::{ATTR_USERNAME, STUN_ATTRIBUTES};
//! use rtci_stun::validated::{Rejection, Validator};
//! # fn receive(received: &Message, key: &[u8], crypto: &dyn rtci_stun::crypto::RTCCrypto) {
//! 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;
//!
//! # fn example() -> Result<(), Box<dyn std::error::Error>> {
//! 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);
//! # Ok(())
//! # }
//! ```
//!
//! Most applications do not depend on this crate directly — [`rtci-ice`] and
//! [`rtci-turn`] build on it, and the [`rtci`](https://docs.rs/rtci) crate drives those.
//!
//! [RFC 8489]: https://datatracker.ietf.org/doc/html/rfc8489
//! [RFC 5389]: https://datatracker.ietf.org/doc/html/rfc5389
//! [RFC 3489]: https://datatracker.ietf.org/doc/html/rfc3489
//! [RFC 8445]: https://datatracker.ietf.org/doc/html/rfc8445
//! [RFC 5766]: https://datatracker.ietf.org/doc/html/rfc5766
//! [`rtci-ice`]: https://docs.rs/rtci-ice
//! [`rtci-turn`]: https://docs.rs/rtci-turn
extern crate lazy_static;
/// The crypto provider API.
///
/// Re-exported because this crate's public constructors take an
/// [`Arc<dyn RTCCryptoProvider>`](crypto::RTCCryptoProvider), which a caller must be able to name
/// without adding — and version-matching — a direct `rtci-crypto` dependency.
pub use crypto;
/// Socket-address helpers shared by the address attributes.
/// Transaction tracking: which requests are outstanding and when they time out.
/// The client side of 300 (Try Alternate): `ALTERNATE-SERVER` and the redirection-loop rule.
/// Validation helpers for received messages and attributes.
/// A Sans-I/O STUN client for talking to a STUN server.
/// The `ERROR-CODE` attribute and the codes defined by STUN, TURN and ICE.
/// The `FINGERPRINT` attribute, a CRC-32 over the message.
/// The checks the fuzz targets in `fuzz/` run, shared with the replay test of
/// `fuzz/regressions/`. Built with `cfg(fuzzing)` (cargo-fuzz) or for this crate's tests; not
/// part of the supported API.
/// The `MESSAGE-INTEGRITY` and `MESSAGE-INTEGRITY-SHA256` attributes, HMACs over the message.
/// The client side of the long-term credential mechanism.
/// The STUN message itself: header, attributes, and encoding.
/// The nonce cookie and its STUN Security Feature bits.
/// The OpaqueString profile (RFC 8265) for user names, realms and passwords.
/// The `PASSWORD-ALGORITHM` and `PASSWORD-ALGORITHMS` attributes.
/// Text-valued attributes such as `USERNAME`, `REALM` and `SOFTWARE`.
/// The retransmission timer of RFC 8489 section 6.2, shared by every transaction layer.
/// The `UNKNOWN-ATTRIBUTES` attribute, listing attributes a server could not process.
/// Parsing `stun:` and `stuns:` URIs.
/// The `USERHASH` attribute.
/// Checking a received message and reading only what its integrity covers.
/// The `XOR-MAPPED-ADDRESS` attribute, whose value is masked with the magic cookie.
/// IANA assigned ports for "stun" protocol.
pub const DEFAULT_PORT: u16 = 3478;
/// The default port for `stuns:` (STUN over TLS/DTLS).
pub const DEFAULT_TLS_PORT: u16 = 5349;