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