tun_easytier/
configuration.rs

1//            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
2//                    Version 2, December 2004
3//
4// Copyleft (ↄ) meh. <meh@schizofreni.co> | http://meh.schizofreni.co
5//
6// Everyone is permitted to copy and distribute verbatim or modified
7// copies of this license document, and changing it is allowed as long
8// as the name is changed.
9//
10//            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
11//   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
12//
13//  0. You just DO WHAT THE FUCK YOU WANT TO.
14
15use 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/// TUN interface OSI layer of operation.
33#[derive(Clone, Copy, Default, Debug, Eq, PartialEq)]
34pub enum Layer {
35    L2,
36    #[default]
37    L3,
38}
39
40/// Configuration builder for a TUN interface.
41#[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    /// Access the platform-dependent configuration.
65    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    /// Functionally equivalent to `tun_name`
74    #[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    /// Set the tun name.
84    ///
85    /// [Note: on macOS, the tun name must be the form `utunx` where `x` is a number, such as `utun3`. -- end note]
86    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    /// Set the address.
92    pub fn address<A: IntoAddress>(&mut self, value: A) -> &mut Self {
93        self.address = Some(value.into_address().unwrap());
94        self
95    }
96
97    /// Set the destination address.
98    pub fn destination<A: IntoAddress>(&mut self, value: A) -> &mut Self {
99        self.destination = Some(value.into_address().unwrap());
100        self
101    }
102
103    /// Set the broadcast address.
104    pub fn broadcast<A: IntoAddress>(&mut self, value: A) -> &mut Self {
105        self.broadcast = Some(value.into_address().unwrap());
106        self
107    }
108
109    /// Set the netmask.
110    pub fn netmask<A: IntoAddress>(&mut self, value: A) -> &mut Self {
111        self.netmask = Some(value.into_address().unwrap());
112        self
113    }
114
115    /// Set the MTU.
116    ///
117    /// [Note: mtu on the Windows platform is always 65535 due to wintun -- end note]
118    pub fn mtu(&mut self, value: u16) -> &mut Self {
119        // mtu on windows platform is always 65535 due to wintun
120        if cfg!(target_family = "unix") {
121            self.mtu = Some(value);
122        }
123        self
124    }
125
126    /// Set the interface to be enabled once created.
127    pub fn up(&mut self) -> &mut Self {
128        self.enabled = Some(true);
129        self
130    }
131
132    /// Set the interface to be disabled once created.
133    pub fn down(&mut self) -> &mut Self {
134        self.enabled = Some(false);
135        self
136    }
137
138    /// Set the OSI layer of operation.
139    pub fn layer(&mut self, value: Layer) -> &mut Self {
140        self.layer = Some(value);
141        self
142    }
143
144    /// Set the number of queues.
145    /// Note: The queues must be 1, otherwise will failed.
146    #[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    /// Set the raw fd.
153    #[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    /// Set whether to close the received raw file descriptor on drop or not.
170    /// The default behaviour is to close the received or tun2 generated file descriptor.
171    /// Note: If this is set to false, it is up to the caller to ensure the
172    /// file descriptor that they pass via [Configuration::raw_fd] is properly closed.
173    #[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}