rtci_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//! [`rtci-stun`](https://docs.rs/rtci-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/// The `ADDITIONAL-ADDRESS-FAMILY` attribute, for asking for an IPv6 relayed address beside
15/// the IPv4 one.
16pub mod addfamily;
17/// Address helpers and the five-tuple that identifies an allocation.
18pub mod addr;
19/// The `ADDRESS-ERROR-CODE` attribute, which says why one of two address families was not
20/// allocated.
21pub mod addrerror;
22/// ChannelData messages — the compact 4-byte framing for relayed data.
23pub mod chandata;
24/// The `CHANNEL-NUMBER` attribute.
25pub mod channum;
26/// The `CONNECTION-ID` attribute of TCP allocations (RFC 6062), which names a peer's TCP
27/// connection.
28pub mod connid;
29/// The `DATA` attribute, which carries relayed payloads in Send/Data indications.
30pub mod data;
31/// The `DONT-FRAGMENT` attribute, asking the server to set DF on relayed packets.
32pub mod dontfrag;
33/// The `EVEN-PORT` attribute, requesting an even relayed port (for RTP/RTCP pairs).
34pub mod evenport;
35/// The `ICMP` attribute, which reports an ICMP error a relayed datagram met.
36pub mod icmp;
37/// The `LIFETIME` attribute, which sets and reports allocation expiry.
38pub mod lifetime;
39/// The `XOR-PEER-ADDRESS` attribute, naming the peer in permission and data messages.
40pub mod peeraddr;
41/// The `XOR-RELAYED-ADDRESS` attribute, which reports the allocated public address.
42pub mod relayaddr;
43/// The `REQUESTED-ADDRESS-FAMILY` attribute, for asking for an IPv4 or IPv6 allocation.
44pub mod reqfamily;
45/// The `REQUESTED-TRANSPORT` attribute, which selects the relay's transport to peers.
46pub mod reqtrans;
47/// The `RESERVATION-TOKEN` attribute, used to claim a previously reserved port.
48pub mod rsrvtoken;
49
50use std::fmt;
51
52use stun::message::*;
53
54// proto implements RFC 8656 Traversal Using Relays around NAT.
55
56/// `Protocol` is IANA assigned protocol number.
57#[derive(PartialEq, Eq, Default, Debug, Clone, Copy, Hash)]
58pub struct Protocol(pub u8);
59
60/// `PROTO_TCP` is IANA assigned protocol number for TCP.
61pub const PROTO_TCP: Protocol = Protocol(6);
62/// `PROTO_UDP` is IANA assigned protocol number for UDP.
63pub const PROTO_UDP: Protocol = Protocol(17);
64
65impl fmt::Display for Protocol {
66 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67 let others = format!("{}", self.0);
68 let s = match *self {
69 PROTO_UDP => "UDP",
70 PROTO_TCP => "TCP",
71 _ => others.as_str(),
72 };
73
74 write!(f, "{s}")
75 }
76}
77
78// Default ports for TURN from RFC 8656 Section 4.1.
79
80/// `DEFAULT_PORT` for TURN is same as STUN.
81pub const DEFAULT_PORT: u16 = stun::DEFAULT_PORT;
82/// `DEFAULT_TLSPORT` is for TURN over TLS and is same as STUN.
83pub const DEFAULT_TLS_PORT: u16 = stun::DEFAULT_TLS_PORT;
84
85/// Shorthand for create permission request type.
86pub fn create_permission_request() -> MessageType {
87 MessageType::new(METHOD_CREATE_PERMISSION, CLASS_REQUEST)
88}
89
90/// Shorthand for allocation request message type.
91pub fn allocate_request() -> MessageType {
92 MessageType::new(METHOD_ALLOCATE, CLASS_REQUEST)
93}
94
95/// Shorthand for send indication message type.
96pub fn send_indication() -> MessageType {
97 MessageType::new(METHOD_SEND, CLASS_INDICATION)
98}
99
100/// Shorthand for refresh request message type.
101pub fn refresh_request() -> MessageType {
102 MessageType::new(METHOD_REFRESH, CLASS_REQUEST)
103}