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