rtc_turn/proto/mod.rs
1//! TURN's STUN attributes and ChannelData framing.
2//!
3//! TURN is defined as a set of STUN methods and attributes, so these build on
4//! [`rtc-stun`](https://docs.rs/rtc-stun). The attributes name the relay's parts:
5//! [`relayaddr`](crate::proto::relayaddr) the allocated public address, [`peeraddr`](crate::proto::peeraddr) the far end, [`lifetime`](crate::proto::lifetime) the
6//! allocation's expiry, [`data`](crate::proto::data) the relayed payload.
7//!
8//! [`chandata`](crate::proto::chandata) is the exception — a ChannelData message is not STUN at all, but a compact
9//! four-byte framing that replaces the 36-byte Send/Data indication header once a channel is
10//! bound. Its [`channum`](crate::proto::channum) range is chosen so the two can be told apart on a shared port.
11#[cfg(test)]
12mod proto_test;
13
14/// Address helpers and the five-tuple that identifies an allocation.
15pub mod addr;
16/// ChannelData messages — the compact 4-byte framing for relayed data.
17pub mod chandata;
18/// The `CHANNEL-NUMBER` attribute.
19pub mod channum;
20/// The `DATA` attribute, which carries relayed payloads in Send/Data indications.
21pub mod data;
22/// The `DONT-FRAGMENT` attribute, asking the server to set DF on relayed packets.
23pub mod dontfrag;
24/// The `EVEN-PORT` attribute, requesting an even relayed port (for RTP/RTCP pairs).
25pub mod evenport;
26/// The `LIFETIME` attribute, which sets and reports allocation expiry.
27pub mod lifetime;
28/// The `XOR-PEER-ADDRESS` attribute, naming the peer in permission and data messages.
29pub mod peeraddr;
30/// The `XOR-RELAYED-ADDRESS` attribute, which reports the allocated public address.
31pub mod relayaddr;
32/// The `REQUESTED-ADDRESS-FAMILY` attribute, for asking for an IPv4 or IPv6 allocation.
33pub mod reqfamily;
34/// The `REQUESTED-TRANSPORT` attribute, which selects the relay's transport to peers.
35pub mod reqtrans;
36/// The `RESERVATION-TOKEN` attribute, used to claim a previously reserved port.
37pub mod rsrvtoken;
38
39use std::fmt;
40
41use stun::message::*;
42
43// proto implements RFC 5766 Traversal Using Relays around NAT.
44
45/// `Protocol` is IANA assigned protocol number.
46#[derive(PartialEq, Eq, Default, Debug, Clone, Copy, Hash)]
47pub struct Protocol(pub u8);
48
49/// `PROTO_TCP` is IANA assigned protocol number for TCP.
50pub const PROTO_TCP: Protocol = Protocol(6);
51/// `PROTO_UDP` is IANA assigned protocol number for UDP.
52pub const PROTO_UDP: Protocol = Protocol(17);
53
54impl fmt::Display for Protocol {
55 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56 let others = format!("{}", self.0);
57 let s = match *self {
58 PROTO_UDP => "UDP",
59 PROTO_TCP => "TCP",
60 _ => others.as_str(),
61 };
62
63 write!(f, "{s}")
64 }
65}
66
67// Default ports for TURN from RFC 5766 Section 4.
68
69/// `DEFAULT_PORT` for TURN is same as STUN.
70pub const DEFAULT_PORT: u16 = stun::DEFAULT_PORT;
71/// `DEFAULT_TLSPORT` is for TURN over TLS and is same as STUN.
72pub const DEFAULT_TLS_PORT: u16 = stun::DEFAULT_TLS_PORT;
73
74/// Shorthand for create permission request type.
75pub fn create_permission_request() -> MessageType {
76 MessageType::new(METHOD_CREATE_PERMISSION, CLASS_REQUEST)
77}
78
79/// Shorthand for allocation request message type.
80pub fn allocate_request() -> MessageType {
81 MessageType::new(METHOD_ALLOCATE, CLASS_REQUEST)
82}
83
84/// Shorthand for send indication message type.
85pub fn send_indication() -> MessageType {
86 MessageType::new(METHOD_SEND, CLASS_INDICATION)
87}
88
89/// Shorthand for refresh request message type.
90pub fn refresh_request() -> MessageType {
91 MessageType::new(METHOD_REFRESH, CLASS_REQUEST)
92}