tun_easytier/
configuration.rs1use std::net::IpAddr;
16#[cfg(unix)]
17use std::os::unix::io::RawFd;
18
19use crate::address::IntoAddress;
20use crate::platform::PlatformConfig;
21
22cfg_if::cfg_if! {
23 if #[cfg(windows)] {
24 #[allow(dead_code)]
25 #[derive(Clone, Debug)]
26 pub(crate) struct WinHandle(std::os::windows::raw::HANDLE);
27 unsafe impl Send for WinHandle {}
28 unsafe impl Sync for WinHandle {}
29 }
30}
31
32#[derive(Clone, Copy, Default, Debug, Eq, PartialEq)]
34pub enum Layer {
35 L2,
36 #[default]
37 L3,
38}
39
40#[derive(Clone, Default, Debug)]
42pub struct Configuration {
43 pub(crate) tun_name: Option<String>,
44 pub(crate) platform_config: PlatformConfig,
45 pub(crate) address: Option<IpAddr>,
46 pub(crate) destination: Option<IpAddr>,
47 pub(crate) broadcast: Option<IpAddr>,
48 pub(crate) netmask: Option<IpAddr>,
49 pub(crate) mtu: Option<u16>,
50 pub(crate) enabled: Option<bool>,
51 pub(crate) layer: Option<Layer>,
52 pub(crate) queues: Option<usize>,
53 #[cfg(unix)]
54 pub(crate) raw_fd: Option<RawFd>,
55 #[cfg(not(unix))]
56 pub(crate) raw_fd: Option<i32>,
57 #[cfg(windows)]
58 pub(crate) raw_handle: Option<WinHandle>,
59 #[cfg(unix)]
60 pub(crate) close_fd_on_drop: Option<bool>,
61}
62
63impl Configuration {
64 pub fn platform_config<F>(&mut self, f: F) -> &mut Self
66 where
67 F: FnOnce(&mut PlatformConfig),
68 {
69 f(&mut self.platform_config);
70 self
71 }
72
73 #[deprecated(
75 since = "1.1.2",
76 note = "Since the API `name` may have an easy name conflict when IDE prompts, it is replaced by `tun_name` for better coding experience"
77 )]
78 pub fn name<S: AsRef<str>>(&mut self, tun_name: S) -> &mut Self {
79 self.tun_name = Some(tun_name.as_ref().into());
80 self
81 }
82
83 pub fn tun_name<S: AsRef<str>>(&mut self, tun_name: S) -> &mut Self {
87 self.tun_name = Some(tun_name.as_ref().into());
88 self
89 }
90
91 pub fn address<A: IntoAddress>(&mut self, value: A) -> &mut Self {
93 self.address = Some(value.into_address().unwrap());
94 self
95 }
96
97 pub fn destination<A: IntoAddress>(&mut self, value: A) -> &mut Self {
99 self.destination = Some(value.into_address().unwrap());
100 self
101 }
102
103 pub fn broadcast<A: IntoAddress>(&mut self, value: A) -> &mut Self {
105 self.broadcast = Some(value.into_address().unwrap());
106 self
107 }
108
109 pub fn netmask<A: IntoAddress>(&mut self, value: A) -> &mut Self {
111 self.netmask = Some(value.into_address().unwrap());
112 self
113 }
114
115 pub fn mtu(&mut self, value: u16) -> &mut Self {
119 if cfg!(target_family = "unix") {
121 self.mtu = Some(value);
122 }
123 self
124 }
125
126 pub fn up(&mut self) -> &mut Self {
128 self.enabled = Some(true);
129 self
130 }
131
132 pub fn down(&mut self) -> &mut Self {
134 self.enabled = Some(false);
135 self
136 }
137
138 pub fn layer(&mut self, value: Layer) -> &mut Self {
140 self.layer = Some(value);
141 self
142 }
143
144 #[deprecated(since = "1.0.0", note = "The queues will always be 1.")]
147 pub fn queues(&mut self, value: usize) -> &mut Self {
148 self.queues = Some(value);
149 self
150 }
151
152 #[cfg(unix)]
154 pub fn raw_fd(&mut self, fd: RawFd) -> &mut Self {
155 self.raw_fd = Some(fd);
156 self
157 }
158 #[cfg(not(unix))]
159 pub fn raw_fd(&mut self, fd: i32) -> &mut Self {
160 self.raw_fd = Some(fd);
161 self
162 }
163 #[cfg(windows)]
164 pub fn raw_handle(&mut self, handle: std::os::windows::raw::HANDLE) -> &mut Self {
165 self.raw_handle = Some(WinHandle(handle));
166 self
167 }
168
169 #[cfg(unix)]
174 pub fn close_fd_on_drop(&mut self, value: bool) -> &mut Self {
175 self.close_fd_on_drop = Some(value);
176 self
177 }
178}