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 socket address, either IPv4 or IPv6
48    pub local_addr: SocketAddr,
49    /// Peer socket address, either IPv4 or IPv6
50    pub peer_addr: SocketAddr,
51    /// Type of transport protocol, either UDP or TCP
52    pub transport_protocol: TransportProtocol,
53    /// Explicit congestion notification codepoint (`None` is Not-ECT).
54    ///
55    /// On a received datagram, the codepoint it arrived with, as the socket layer read it
56    /// (`IP_RECVTOS` / `IPV6_RECVTCLASS`); on a datagram to send, the codepoint to set on it
57    /// (`IP_TOS` / `IPV6_TCLASS`).
58    pub ecn: Option<EcnCodepoint>,
59}
60
61impl Default for TransportContext {
62    fn default() -> Self {
63        Self {
64            local_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 0),
65            peer_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 65535),
66            transport_protocol: TransportProtocol::UDP,
67            ecn: None,
68        }
69    }
70}
71
72/// A generic transmit with [TransportContext]
73pub struct TransportMessage<T> {
74    /// Received/Sent time
75    pub now: Instant,
76    /// A transport context with [local_addr](TransportContext::local_addr) and [peer_addr](TransportContext::peer_addr)
77    pub transport: TransportContext,
78    /// Message body with generic type
79    pub message: T,
80}
81
82/// BytesMut type transmit with [TransportContext]
83pub type TaggedBytesMut = TransportMessage<BytesMut>;
84
85/// String type transmit with [TransportContext]
86pub type TaggedString = TransportMessage<String>;
87
88/// Four Tuple consists of local address and peer address
89#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
90pub struct FourTuple {
91    /// Local socket address, either IPv4 or IPv6
92    pub local_addr: SocketAddr,
93    /// Peer socket address, either IPv4 or IPv6
94    pub peer_addr: SocketAddr,
95}
96
97impl From<&TransportContext> for FourTuple {
98    fn from(value: &TransportContext) -> Self {
99        Self {
100            local_addr: value.local_addr,
101            peer_addr: value.peer_addr,
102        }
103    }
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
115/// Five Tuple consists of local address, peer address and protocol
116#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
117pub struct FiveTuple {
118    /// Local socket address, either IPv4 or IPv6
119    pub local_addr: SocketAddr,
120    /// Peer socket address, either IPv4 or IPv6
121    pub peer_addr: SocketAddr,
122    /// Type of protocol, either UDP or TCP
123    pub transport_protocol: TransportProtocol,
124}
125
126impl From<&TransportContext> for FiveTuple {
127    fn from(value: &TransportContext) -> Self {
128        Self {
129            local_addr: value.local_addr,
130            peer_addr: value.peer_addr,
131            transport_protocol: value.transport_protocol,
132        }
133    }
134}
135
136impl From<TransportContext> for FiveTuple {
137    fn from(value: TransportContext) -> Self {
138        Self {
139            local_addr: value.local_addr,
140            peer_addr: value.peer_addr,
141            transport_protocol: value.transport_protocol,
142        }
143    }
144}