Skip to main content

rtci_shared/
transport.rs

1use bytes::BytesMut;
2use std::net::{IpAddr, Ipv4Addr, SocketAddr};
3use std::time::Instant;
4
5/// Explicit congestion notification codepoint (RFC 3168 section 5): the low two bits of the
6/// IPv4 TOS byte or the IPv6 Traffic Class. Not-ECT (`00`) is the absence of one, `None`.
7#[repr(u8)]
8#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
9pub enum EcnCodepoint {
10    /// `10`: ECN-Capable Transport, ECT(0).
11    Ect0 = 0b10,
12    /// `01`: ECN-Capable Transport, ECT(1), the L4S identifier (RFC 9331).
13    Ect1 = 0b01,
14    /// `11`: Congestion Experienced.
15    Ce = 0b11,
16}
17
18impl EcnCodepoint {
19    /// Create new object from the given bits
20    pub fn from_bits(x: u8) -> Option<Self> {
21        use self::EcnCodepoint::*;
22        Some(match x & 0b11 {
23            0b10 => Ect0,
24            0b01 => Ect1,
25            0b11 => Ce,
26            _ => {
27                return None;
28            }
29        })
30    }
31}
32
33/// Type of transport protocol, either UDP or TCP
34#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
35#[non_exhaustive]
36pub enum TransportProtocol {
37    /// UDP
38    #[default]
39    UDP,
40    /// TCP
41    TCP,
42}
43
44/// Transport Context with local address, peer address, ECN, protocol, etc.
45#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
46pub struct TransportContext {
47    /// Local address, either IPv4 or IPv6.
48    ///
49    /// For a datagram handed to a connection's `handle_read`, the transport address of the
50    /// local ICE candidate it arrived on, which the ICE agent matches exactly: a host
51    /// candidate's address, or, for a datagram a TURN allocation relayed, the relayed address
52    /// (the relay candidate's). A socket bound to the unspecified address (`0.0.0.0:port`,
53    /// `[::]:port`) reports the concrete address the datagram was received on (its destination
54    /// address, e.g. from `IP_PKTINFO` / `IPV6_RECVPKTINFO`), not its own unspecified one. For a
55    /// datagram from `poll_write`, the address to send from: a host candidate's socket, or a
56    /// relayed address, whose datagrams go through that TURN allocation.
57    pub local_addr: SocketAddr,
58    /// Peer socket address, either IPv4 or IPv6
59    pub peer_addr: SocketAddr,
60    /// Type of transport protocol, either UDP or TCP
61    pub transport_protocol: TransportProtocol,
62    /// Explicit congestion notification codepoint (`None` is Not-ECT).
63    ///
64    /// On a received datagram, the codepoint it arrived with, as the socket layer read it
65    /// (`IP_RECVTOS` / `IPV6_RECVTCLASS`); on a datagram to send, the codepoint to set on it
66    /// (`IP_TOS` / `IPV6_TCLASS`).
67    pub ecn: Option<EcnCodepoint>,
68}
69
70impl Default for TransportContext {
71    fn default() -> Self {
72        Self {
73            local_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 0),
74            peer_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 65535),
75            transport_protocol: TransportProtocol::UDP,
76            ecn: None,
77        }
78    }
79}
80
81/// A generic transmit with [TransportContext]
82pub struct TransportMessage<T> {
83    /// Received/Sent time
84    pub now: Instant,
85    /// A transport context with [local_addr](TransportContext::local_addr) and [peer_addr](TransportContext::peer_addr)
86    pub transport: TransportContext,
87    /// Message body with generic type
88    pub message: T,
89}
90
91/// BytesMut type transmit with [TransportContext]
92pub type TaggedBytesMut = TransportMessage<BytesMut>;
93
94/// String type transmit with [TransportContext]
95pub type TaggedString = TransportMessage<String>;
96
97/// Four Tuple consists of local address and peer address
98#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
99pub struct FourTuple {
100    /// Local socket address, either IPv4 or IPv6
101    pub local_addr: SocketAddr,
102    /// Peer socket address, either IPv4 or IPv6
103    pub peer_addr: SocketAddr,
104}
105
106impl From<&TransportContext> for FourTuple {
107    fn from(value: &TransportContext) -> Self {
108        Self {
109            local_addr: value.local_addr,
110            peer_addr: value.peer_addr,
111        }
112    }
113}
114
115impl From<TransportContext> for FourTuple {
116    fn from(value: TransportContext) -> Self {
117        Self {
118            local_addr: value.local_addr,
119            peer_addr: value.peer_addr,
120        }
121    }
122}
123
124/// Five Tuple consists of local address, peer address and protocol
125#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
126pub struct FiveTuple {
127    /// Local socket address, either IPv4 or IPv6
128    pub local_addr: SocketAddr,
129    /// Peer socket address, either IPv4 or IPv6
130    pub peer_addr: SocketAddr,
131    /// Type of protocol, either UDP or TCP
132    pub transport_protocol: TransportProtocol,
133}
134
135impl From<&TransportContext> for FiveTuple {
136    fn from(value: &TransportContext) -> Self {
137        Self {
138            local_addr: value.local_addr,
139            peer_addr: value.peer_addr,
140            transport_protocol: value.transport_protocol,
141        }
142    }
143}
144
145impl From<TransportContext> for FiveTuple {
146    fn from(value: TransportContext) -> Self {
147        Self {
148            local_addr: value.local_addr,
149            peer_addr: value.peer_addr,
150            transport_protocol: value.transport_protocol,
151        }
152    }
153}