Skip to main content

rtc_shared/
transport.rs

1use bytes::BytesMut;
2use std::net::{IpAddr, Ipv4Addr, SocketAddr};
3use std::time::Instant;
4
5/// Explicit congestion notification codepoint
6#[repr(u8)]
7#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
8pub enum EcnCodepoint {
9    #[doc(hidden)]
10    Ect0 = 0b10,
11    #[doc(hidden)]
12    Ect1 = 0b01,
13    #[doc(hidden)]
14    Ce = 0b11,
15}
16
17impl EcnCodepoint {
18    /// Create new object from the given bits
19    pub fn from_bits(x: u8) -> Option<Self> {
20        use self::EcnCodepoint::*;
21        Some(match x & 0b11 {
22            0b10 => Ect0,
23            0b01 => Ect1,
24            0b11 => Ce,
25            _ => {
26                return None;
27            }
28        })
29    }
30}
31
32/// Type of transport protocol, either UDP or TCP
33#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
34#[non_exhaustive]
35pub enum TransportProtocol {
36    /// UDP
37    #[default]
38    UDP,
39    /// TCP
40    TCP,
41}
42
43/// Transport Context with local address, peer address, ECN, protocol, etc.
44#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
45pub struct TransportContext {
46    /// Local socket address, either IPv4 or IPv6
47    pub local_addr: SocketAddr,
48    /// Peer socket address, either IPv4 or IPv6
49    pub peer_addr: SocketAddr,
50    /// Type of transport protocol, either UDP or TCP
51    pub transport_protocol: TransportProtocol,
52    /// Explicit congestion notification bits to set on the packet
53    pub ecn: Option<EcnCodepoint>,
54}
55
56impl Default for TransportContext {
57    fn default() -> Self {
58        Self {
59            local_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 0),
60            peer_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 65535),
61            transport_protocol: TransportProtocol::UDP,
62            ecn: None,
63        }
64    }
65}
66
67/// A generic transmit with [TransportContext]
68pub struct TransportMessage<T> {
69    /// Received/Sent time
70    pub now: Instant,
71    /// A transport context with [local_addr](TransportContext::local_addr) and [peer_addr](TransportContext::peer_addr)
72    pub transport: TransportContext,
73    /// Message body with generic type
74    pub message: T,
75}
76
77/// BytesMut type transmit with [TransportContext]
78pub type TaggedBytesMut = TransportMessage<BytesMut>;
79
80/// String type transmit with [TransportContext]
81pub type TaggedString = TransportMessage<String>;
82
83/// Four Tuple consists of local address and peer address
84#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
85pub struct FourTuple {
86    /// Local socket address, either IPv4 or IPv6
87    pub local_addr: SocketAddr,
88    /// Peer socket address, either IPv4 or IPv6
89    pub peer_addr: SocketAddr,
90}
91
92impl From<&TransportContext> for FourTuple {
93    fn from(value: &TransportContext) -> Self {
94        Self {
95            local_addr: value.local_addr,
96            peer_addr: value.peer_addr,
97        }
98    }
99}
100
101impl From<TransportContext> for FourTuple {
102    fn from(value: TransportContext) -> Self {
103        Self {
104            local_addr: value.local_addr,
105            peer_addr: value.peer_addr,
106        }
107    }
108}
109
110/// Five Tuple consists of local address, peer address and protocol
111#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
112pub struct FiveTuple {
113    /// Local socket address, either IPv4 or IPv6
114    pub local_addr: SocketAddr,
115    /// Peer socket address, either IPv4 or IPv6
116    pub peer_addr: SocketAddr,
117    /// Type of protocol, either UDP or TCP
118    pub transport_protocol: TransportProtocol,
119}
120
121impl From<&TransportContext> for FiveTuple {
122    fn from(value: &TransportContext) -> Self {
123        Self {
124            local_addr: value.local_addr,
125            peer_addr: value.peer_addr,
126            transport_protocol: value.transport_protocol,
127        }
128    }
129}
130
131impl From<TransportContext> for FiveTuple {
132    fn from(value: TransportContext) -> Self {
133        Self {
134            local_addr: value.local_addr,
135            peer_addr: value.peer_addr,
136            transport_protocol: value.transport_protocol,
137        }
138    }
139}