tun_sync/
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::Ipv4Addr;
16use std::os::unix::io::RawFd;
17
18use crate::address::IntoAddress;
19use crate::platform;
20
21/// TUN interface OSI layer of operation.
22#[derive(Clone, Copy, Debug, Eq, PartialEq)]
23pub enum Layer {
24    L2,
25    L3,
26}
27
28impl Default for Layer {
29    fn default() -> Self {
30        Layer::L3
31    }
32}
33
34/// Configuration builder for a TUN interface.
35#[derive(Clone, Default, Debug)]
36pub struct Configuration {
37    pub(crate) name: Option<String>,
38    pub(crate) platform: platform::Configuration,
39
40    pub(crate) address: Option<Ipv4Addr>,
41    pub(crate) destination: Option<Ipv4Addr>,
42    pub(crate) broadcast: Option<Ipv4Addr>,
43    pub(crate) netmask: Option<Ipv4Addr>,
44    pub(crate) mtu: Option<i32>,
45    pub(crate) enabled: Option<bool>,
46    pub(crate) layer: Option<Layer>,
47    pub(crate) queues: Option<usize>,
48    pub(crate) raw_fd: Option<RawFd>,
49}
50
51impl Configuration {
52    /// Access the platform dependant configuration.
53    pub fn platform<F>(&mut self, f: F) -> &mut Self
54    where
55        F: FnOnce(&mut platform::Configuration),
56    {
57        f(&mut self.platform);
58        self
59    }
60
61    /// Set the name.
62    pub fn name<S: AsRef<str>>(&mut self, name: S) -> &mut Self {
63        self.name = Some(name.as_ref().into());
64        self
65    }
66
67    /// Set the address.
68    pub fn address<A: IntoAddress>(&mut self, value: A) -> &mut Self {
69        self.address = Some(value.into_address().unwrap());
70        self
71    }
72
73    /// Set the destination address.
74    pub fn destination<A: IntoAddress>(&mut self, value: A) -> &mut Self {
75        self.destination = Some(value.into_address().unwrap());
76        self
77    }
78
79    /// Set the broadcast address.
80    pub fn broadcast<A: IntoAddress>(&mut self, value: A) -> &mut Self {
81        self.broadcast = Some(value.into_address().unwrap());
82        self
83    }
84
85    /// Set the netmask.
86    pub fn netmask<A: IntoAddress>(&mut self, value: A) -> &mut Self {
87        self.netmask = Some(value.into_address().unwrap());
88        self
89    }
90
91    /// Set the MTU.
92    pub fn mtu(&mut self, value: i32) -> &mut Self {
93        self.mtu = Some(value);
94        self
95    }
96
97    /// Set the interface to be enabled once created.
98    pub fn up(&mut self) -> &mut Self {
99        self.enabled = Some(true);
100        self
101    }
102
103    /// Set the interface to be disabled once created.
104    pub fn down(&mut self) -> &mut Self {
105        self.enabled = Some(false);
106        self
107    }
108
109    /// Set the OSI layer of operation.
110    pub fn layer(&mut self, value: Layer) -> &mut Self {
111        self.layer = Some(value);
112        self
113    }
114
115    /// Set the number of queues.
116    pub fn queues(&mut self, value: usize) -> &mut Self {
117        self.queues = Some(value);
118        self
119    }
120
121    /// Set the raw fd.
122    pub fn raw_fd(&mut self, fd: RawFd) -> &mut Self {
123        self.raw_fd = Some(fd);
124        self
125    }
126}