webrtc_ice/
udp_network.rs

1use std::sync::Arc;
2
3use super::udp_mux::UDPMux;
4use super::Error;
5
6#[derive(Default, Clone)]
7pub struct EphemeralUDP {
8    port_min: u16,
9    port_max: u16,
10}
11
12impl EphemeralUDP {
13    pub fn new(port_min: u16, port_max: u16) -> Result<Self, Error> {
14        let mut s = Self::default();
15        s.set_ports(port_min, port_max)?;
16
17        Ok(s)
18    }
19
20    pub fn port_min(&self) -> u16 {
21        self.port_min
22    }
23
24    pub fn port_max(&self) -> u16 {
25        self.port_max
26    }
27
28    pub fn set_ports(&mut self, port_min: u16, port_max: u16) -> Result<(), Error> {
29        if port_max < port_min {
30            return Err(Error::ErrPort);
31        }
32
33        self.port_min = port_min;
34        self.port_max = port_max;
35
36        Ok(())
37    }
38}
39
40/// Configuration for the underlying UDP network stack.
41/// There are two ways to configure this Ephemeral and Muxed.
42///
43/// **Ephemeral mode**
44///
45/// In Ephemeral mode sockets are created and bound to random ports during ICE
46/// gathering. The ports to use can be restricted by setting [`EphemeralUDP::port_min`] and
47/// [`EphemeralUDP::port_max`] in which case only ports in this range will be used.
48///
49/// **Muxed**
50///
51/// In muxed mode a single UDP socket is used and all connections are muxed over this single socket.
52///
53#[derive(Clone)]
54pub enum UDPNetwork {
55    Ephemeral(EphemeralUDP),
56    Muxed(Arc<dyn UDPMux + Send + Sync>),
57}
58
59impl Default for UDPNetwork {
60    fn default() -> Self {
61        Self::Ephemeral(Default::default())
62    }
63}
64
65impl UDPNetwork {
66    fn is_ephemeral(&self) -> bool {
67        matches!(self, Self::Ephemeral(_))
68    }
69
70    fn is_muxed(&self) -> bool {
71        matches!(self, Self::Muxed(_))
72    }
73}
74
75#[cfg(test)]
76mod test {
77    use super::EphemeralUDP;
78
79    #[test]
80    fn test_ephemeral_udp_constructor() {
81        assert!(
82            EphemeralUDP::new(3000, 2999).is_err(),
83            "EphemeralUDP should not allow invalid port range"
84        );
85
86        let e = EphemeralUDP::default();
87        assert_eq!(e.port_min(), 0, "EphemeralUDP should default port_min to 0");
88        assert_eq!(e.port_max(), 0, "EphemeralUDP should default port_max to 0");
89    }
90
91    #[test]
92    fn test_ephemeral_udp_set_ports() {
93        let mut e = EphemeralUDP::default();
94
95        assert!(
96            e.set_ports(3000, 2999).is_err(),
97            "EphemeralUDP should not allow invalid port range"
98        );
99
100        assert!(
101            e.set_ports(6000, 6001).is_ok(),
102            "EphemeralUDP::set_ports should allow valid port range"
103        );
104
105        assert_eq!(
106            e.port_min(),
107            6000,
108            "Ports set with `EphemeralUDP::set_ports` should be reflected"
109        );
110        assert_eq!(
111            e.port_max(),
112            6001,
113            "Ports set with `EphemeralUDP::set_ports` should be reflected"
114        );
115    }
116}