tun_easytier/
device.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 crate::configuration::Configuration;
16use crate::error::Result;
17use std::io::{Read, Write};
18use std::net::IpAddr;
19
20/// A TUN abstract device interface.
21pub trait AbstractDevice: Read + Write {
22    /// Reconfigure the device.
23    fn configure(&mut self, config: &Configuration) -> Result<()> {
24        if let Some(ip) = config.address {
25            self.set_address(ip)?;
26        }
27
28        if let Some(ip) = config.destination {
29            self.set_destination(ip)?;
30        }
31
32        if let Some(ip) = config.broadcast {
33            self.set_broadcast(ip)?;
34        }
35
36        if let Some(ip) = config.netmask {
37            self.set_netmask(ip)?;
38        }
39
40        if let Some(mtu) = config.mtu {
41            self.set_mtu(mtu)?;
42        }
43
44        if let Some(enabled) = config.enabled {
45            self.enabled(enabled)?;
46        }
47
48        Ok(())
49    }
50
51    /// Get the device tun name.
52    fn tun_name(&self) -> Result<String>;
53
54    /// Set the device tun name.
55    fn set_tun_name(&mut self, tun_name: &str) -> Result<()>;
56
57    /// Turn on or off the interface.
58    fn enabled(&mut self, value: bool) -> Result<()>;
59
60    /// Get the address.
61    fn address(&self) -> Result<IpAddr>;
62
63    /// Set the address.
64    fn set_address(&mut self, value: IpAddr) -> Result<()>;
65
66    /// Get the destination address.
67    fn destination(&self) -> Result<IpAddr>;
68
69    /// Set the destination address.
70    fn set_destination(&mut self, value: IpAddr) -> Result<()>;
71
72    /// Get the broadcast address.
73    fn broadcast(&self) -> Result<IpAddr>;
74
75    /// Set the broadcast address.
76    fn set_broadcast(&mut self, value: IpAddr) -> Result<()>;
77
78    /// Get the netmask.
79    fn netmask(&self) -> Result<IpAddr>;
80
81    /// Set the netmask.
82    fn set_netmask(&mut self, value: IpAddr) -> Result<()>;
83
84    /// Get the MTU.
85    fn mtu(&self) -> Result<u16>;
86
87    /// Set the MTU.
88    ///
89    /// [Note: This setting has no effect on the Windows platform due to the mtu of wintun is always 65535. --end note]
90    fn set_mtu(&mut self, value: u16) -> Result<()>;
91
92    /// Return whether the underlying tun device on the platform has packet information
93    ///
94    /// [Note: This value is not used to specify whether the packets delivered from/to tun2 have packet information. -- end note]
95    fn packet_information(&self) -> bool;
96}