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