tun_sync/
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 std::io::{Read, Write};
16use std::net::Ipv4Addr;
17use std::os::fd::AsRawFd;
18
19use crate::configuration::Configuration;
20use crate::error::*;
21
22/// A TUN device.
23pub trait Device: Read + Write + AsRawFd {
24    type Queue: Read + Write;
25
26    /// Reconfigure the device.
27    fn configure(&mut self, config: &Configuration) -> Result<()> {
28        if let Some(ip) = config.address {
29            self.set_address(ip)?;
30        }
31
32        if let Some(ip) = config.destination {
33            self.set_destination(ip)?;
34        }
35
36        if let Some(ip) = config.broadcast {
37            self.set_broadcast(ip)?;
38        }
39
40        if let Some(ip) = config.netmask {
41            self.set_netmask(ip)?;
42        }
43
44        if let Some(mtu) = config.mtu {
45            self.set_mtu(mtu)?;
46        }
47
48        if let Some(enabled) = config.enabled {
49            self.enabled(enabled)?;
50        }
51
52        Ok(())
53    }
54
55    /// Get the device name.
56    fn name(&self) -> &str;
57
58    /// Set the device name.
59    fn set_name(&mut self, name: &str) -> Result<()>;
60
61    /// Turn on or off the interface.
62    fn enabled(&mut self, value: bool) -> Result<()>;
63
64    /// Get the address.
65    fn address(&self) -> Result<Ipv4Addr>;
66
67    /// Set the address.
68    fn set_address(&mut self, value: Ipv4Addr) -> Result<()>;
69
70    /// Get the destination address.
71    fn destination(&self) -> Result<Ipv4Addr>;
72
73    /// Set the destination address.
74    fn set_destination(&mut self, value: Ipv4Addr) -> Result<()>;
75
76    /// Get the broadcast address.
77    fn broadcast(&self) -> Result<Ipv4Addr>;
78
79    /// Set the broadcast address.
80    fn set_broadcast(&mut self, value: Ipv4Addr) -> Result<()>;
81
82    /// Get the netmask.
83    fn netmask(&self) -> Result<Ipv4Addr>;
84
85    /// Set the netmask.
86    fn set_netmask(&mut self, value: Ipv4Addr) -> Result<()>;
87
88    /// Get the MTU.
89    fn mtu(&self) -> Result<i32>;
90
91    /// Set the MTU.
92    fn set_mtu(&mut self, value: i32) -> Result<()>;
93
94    /// Get a device queue.
95    fn queue(&mut self, index: usize) -> Option<&mut Self::Queue>;
96}