Skip to main content

rust_p2p_core/tunnel/
config.rs

1use std::io;
2use std::time::Duration;
3
4use crate::socket::LocalInterface;
5use crate::tunnel::tcp::{BytesInitCodec, InitCodec};
6use crate::tunnel::udp::Model;
7
8pub(crate) const MAX_SYMMETRIC_SOCKET_COUNT: usize = 200;
9pub(crate) const MAX_MAIN_SOCKET_COUNT: usize = 10;
10pub(crate) const ROUTE_IDLE_TIME: Duration = Duration::from_secs(10);
11
12#[derive(Clone, Copy, Eq, PartialEq, Debug, Default)]
13pub enum LoadBalance {
14    /// Use the route with the lowest latency among those with the fewest hops.
15    #[default]
16    MinHopLowestLatency,
17    /// Round-robin the route list.
18    RoundRobin,
19    /// Use the most recently added route.
20    MostRecent,
21    /// Use the route with the lowest latency.
22    LowestLatency,
23}
24#[derive(Clone)]
25pub struct TunnelConfig {
26    pub major_socket_count: usize,
27    pub udp_tunnel_config: Option<UdpTunnelConfig>,
28    pub tcp_tunnel_config: Option<TcpTunnelConfig>,
29}
30
31impl Default for TunnelConfig {
32    fn default() -> Self {
33        Self {
34            major_socket_count: MAX_MAJOR_SOCKET_COUNT,
35            udp_tunnel_config: Some(Default::default()),
36            tcp_tunnel_config: Some(Default::default()),
37        }
38    }
39}
40
41pub(crate) const MAX_MAJOR_SOCKET_COUNT: usize = 2;
42pub(crate) const MAX_UDP_SUB_SOCKET_COUNT: usize = 82;
43
44impl TunnelConfig {
45    pub fn new(tcp_init_codec: Box<dyn InitCodec>) -> TunnelConfig {
46        let udp_tunnel_config = Some(UdpTunnelConfig::default());
47        let tcp_tunnel_config = Some(TcpTunnelConfig::new(tcp_init_codec));
48        Self {
49            major_socket_count: MAX_MAJOR_SOCKET_COUNT,
50            udp_tunnel_config,
51            tcp_tunnel_config,
52        }
53    }
54}
55impl TunnelConfig {
56    pub fn none_tcp(self) -> Self {
57        self
58    }
59}
60impl TunnelConfig {
61    pub fn empty() -> Self {
62        Self {
63            major_socket_count: MAX_MAJOR_SOCKET_COUNT,
64            udp_tunnel_config: None,
65            tcp_tunnel_config: None,
66        }
67    }
68
69    pub fn major_socket_count(mut self, count: usize) -> Self {
70        self.major_socket_count = count;
71        self
72    }
73
74    pub fn set_udp_tunnel_config(mut self, config: UdpTunnelConfig) -> Self {
75        self.udp_tunnel_config.replace(config);
76        self
77    }
78    pub fn set_tcp_tunnel_config(mut self, config: TcpTunnelConfig) -> Self {
79        self.tcp_tunnel_config.replace(config);
80        self
81    }
82    pub fn check(&self) -> io::Result<()> {
83        if let Some(udp_tunnel_config) = self.udp_tunnel_config.as_ref() {
84            udp_tunnel_config.check()?;
85        }
86        if let Some(tcp_tunnel_config) = self.tcp_tunnel_config.as_ref() {
87            tcp_tunnel_config.check()?;
88        }
89        Ok(())
90    }
91}
92
93#[derive(Clone)]
94pub struct TcpTunnelConfig {
95    pub route_idle_time: Duration,
96    pub tcp_multiplexing_limit: usize,
97    pub default_interface: Option<LocalInterface>,
98    pub tcp_port: u16,
99    pub use_v6: bool,
100    pub init_codec: Box<dyn InitCodec>,
101}
102
103impl Default for TcpTunnelConfig {
104    fn default() -> Self {
105        Self {
106            route_idle_time: ROUTE_IDLE_TIME,
107            tcp_multiplexing_limit: MAX_MAJOR_SOCKET_COUNT,
108            default_interface: None,
109            tcp_port: 0,
110            use_v6: true,
111            init_codec: Box::new(BytesInitCodec),
112        }
113    }
114}
115
116impl TcpTunnelConfig {
117    pub fn new(init_codec: Box<dyn InitCodec>) -> TcpTunnelConfig {
118        Self {
119            route_idle_time: ROUTE_IDLE_TIME,
120            tcp_multiplexing_limit: MAX_MAJOR_SOCKET_COUNT,
121            default_interface: None,
122            tcp_port: 0,
123            use_v6: true,
124            init_codec,
125        }
126    }
127    pub fn check(&self) -> io::Result<()> {
128        if self.tcp_multiplexing_limit == 0 {
129            return Err(io::Error::other("tcp_multiplexing_limit cannot be 0"));
130        }
131        if self.tcp_multiplexing_limit > MAX_MAIN_SOCKET_COUNT {
132            return Err(io::Error::other("tcp_multiplexing_limit cannot too large"));
133        }
134        if self.use_v6 {
135            socket2::Socket::new(socket2::Domain::IPV6, socket2::Type::STREAM, None)?;
136        }
137        Ok(())
138    }
139    pub fn set_tcp_multiplexing_limit(mut self, tcp_multiplexing_limit: usize) -> Self {
140        self.tcp_multiplexing_limit = tcp_multiplexing_limit;
141        self
142    }
143    pub fn set_route_idle_time(mut self, route_idle_time: Duration) -> Self {
144        self.route_idle_time = route_idle_time;
145        self
146    }
147    pub fn set_default_interface(mut self, default_interface: LocalInterface) -> Self {
148        self.default_interface = Some(default_interface.clone());
149        self
150    }
151    pub fn set_tcp_port(mut self, tcp_port: u16) -> Self {
152        self.tcp_port = tcp_port;
153        self
154    }
155    pub fn set_use_v6(mut self, use_v6: bool) -> Self {
156        self.use_v6 = use_v6;
157        self
158    }
159}
160
161#[derive(Clone)]
162pub struct UdpTunnelConfig {
163    pub main_udp_count: usize,
164    pub sub_udp_count: usize,
165    pub model: Model,
166    pub default_interface: Option<LocalInterface>,
167    pub udp_ports: Vec<u16>,
168    pub use_v6: bool,
169}
170
171impl Default for UdpTunnelConfig {
172    fn default() -> Self {
173        Self {
174            main_udp_count: MAX_MAJOR_SOCKET_COUNT,
175            sub_udp_count: MAX_UDP_SUB_SOCKET_COUNT,
176            model: Model::Low,
177            default_interface: None,
178            udp_ports: vec![0, 0],
179            use_v6: true,
180        }
181    }
182}
183
184impl UdpTunnelConfig {
185    pub fn check(&self) -> io::Result<()> {
186        if self.main_udp_count == 0 {
187            return Err(io::Error::other("main socket count cannot be 0"));
188        }
189        if self.main_udp_count > MAX_MAIN_SOCKET_COUNT {
190            return Err(io::Error::other("main socket count is too large"));
191        }
192        if self.sub_udp_count > MAX_SYMMETRIC_SOCKET_COUNT {
193            return Err(io::Error::other(
194                "socket count for symmetric nat is too large",
195            ));
196        }
197        if self.use_v6 {
198            socket2::Socket::new(socket2::Domain::IPV6, socket2::Type::DGRAM, None)?;
199        }
200        Ok(())
201    }
202    pub fn set_main_udp_count(mut self, count: usize) -> Self {
203        self.main_udp_count = count;
204        self
205    }
206    pub fn set_sub_udp_count(mut self, count: usize) -> Self {
207        self.sub_udp_count = count;
208        self
209    }
210    pub fn set_model(mut self, model: Model) -> Self {
211        self.model = model;
212        self
213    }
214    pub fn set_default_interface(mut self, default_interface: LocalInterface) -> Self {
215        self.default_interface = Some(default_interface.clone());
216        self
217    }
218    pub fn set_udp_ports(mut self, udp_ports: Vec<u16>) -> Self {
219        self.udp_ports = udp_ports;
220        self
221    }
222    pub fn set_simple_udp_port(mut self, udp_port: u16) -> Self {
223        self.udp_ports = vec![udp_port];
224        self
225    }
226    pub fn set_use_v6(mut self, use_v6: bool) -> Self {
227        self.use_v6 = use_v6;
228        self
229    }
230}