From ea049e4a4ca2e4efc85e4376d31b21d82b9549f1 Mon Sep 17 00:00:00 2001 From: Dennis Schwerdel Date: Sat, 28 Nov 2020 23:47:43 +0100 Subject: [PATCH] Remove dummy device type --- CHANGELOG.md | 4 ++++ src/cloud.rs | 2 +- src/config.rs | 2 +- src/device.rs | 44 ++++++-------------------------------------- src/main.rs | 3 +-- src/poll/epoll.rs | 13 ++++--------- 6 files changed, 17 insertions(+), 51 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 43a3167..6917f0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ This project follows [semantic versioning](http://semver.org). +### UNRELEASED + +- [removed] Removed dummy device type + ### v2.0.1 (2020-11-07) - [changed] Changed documentation diff --git a/src/cloud.rs b/src/cloud.rs index 4106e89..6cab3a3 100644 --- a/src/cloud.rs +++ b/src/cloud.rs @@ -106,7 +106,7 @@ impl GenericCloud { match config.device_type { Type::Tap => (true, true), - Type::Tun | Type::Dummy => (false, false) + Type::Tun => (false, false) } } Mode::Router => (false, false), diff --git a/src/config.rs b/src/config.rs index 843bad5..cec0309 100644 --- a/src/config.rs +++ b/src/config.rs @@ -589,7 +589,7 @@ statsd: #[test] fn default_config_as_default() { let mut default_config = Config { - device_type: Type::Dummy, + device_type: Type::Tun, device_name: "".to_string(), device_path: None, fix_rp_filter: false, diff --git a/src/device.rs b/src/device.rs index 169c191..34c7b18 100644 --- a/src/device.rs +++ b/src/device.rs @@ -51,18 +51,14 @@ pub enum Type { Tun, /// Tap interface: This interface transports Ethernet frames. #[serde(rename = "tap")] - Tap, - /// Dummy interface: This interface does nothing. - #[serde(rename = "dummy")] - Dummy + Tap } impl fmt::Display for Type { fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> { match *self { Type::Tun => write!(formatter, "tun"), - Type::Tap => write!(formatter, "tap"), - Type::Dummy => write!(formatter, "dummy") + Type::Tap => write!(formatter, "tap") } } } @@ -74,7 +70,6 @@ impl FromStr for Type { Ok(match &text.to_lowercase() as &str { "tun" => Self::Tun, "tap" => Self::Tap, - "dummy" => Self::Dummy, _ => return Err("Unknown device type") }) } @@ -144,14 +139,10 @@ impl TunTapDevice { #[allow(clippy::useless_conversion)] pub fn new(ifname: &str, type_: Type, path: Option<&str>) -> io::Result { let path = path.unwrap_or_else(|| Self::default_path(type_)); - if type_ == Type::Dummy { - return Self::dummy(ifname, path, type_) - } let fd = fs::OpenOptions::new().read(true).write(true).open(path)?; let flags = match type_ { Type::Tun => libc::IFF_TUN | libc::IFF_NO_PI, - Type::Tap => libc::IFF_TAP | libc::IFF_NO_PI, - Type::Dummy => unreachable!() + Type::Tap => libc::IFF_TAP | libc::IFF_NO_PI }; let mut ifreq = IfReq::new(ifname); ifreq.data.flags = flags as libc::c_short; @@ -172,33 +163,10 @@ impl TunTapDevice { #[inline] pub fn default_path(type_: Type) -> &'static str { match type_ { - Type::Tun | Type::Tap => "/dev/net/tun", - Type::Dummy => "/dev/null" + Type::Tun | Type::Tap => "/dev/net/tun" } } - /// Creates a dummy device based on an existing file - /// - /// This method opens a regular or special file and reads from it to receive packets and - /// writes to it to send packets. This method does not use a networking device and therefore - /// can be used for testing. - /// - /// The parameter `path` is the file that should be used. Special files like `/dev/null`, - /// named pipes and unix sockets can be used with this method. - /// - /// Both `ifname` and `type_` parameters have no effect. - /// - /// # Errors - /// This method will return an error if the file can not be opened for reading and writing. - #[allow(dead_code)] - pub fn dummy(ifname: &str, path: &str, type_: Type) -> io::Result { - Ok(TunTapDevice { - fd: fs::OpenOptions::new().create(true).read(true).write(true).open(path)?, - ifname: ifname.to_string(), - type_ - }) - } - #[cfg(any(target_os = "linux", target_os = "android"))] #[inline] fn correct_data_after_read(&mut self, _buffer: &mut MsgBuffer) {} @@ -255,7 +223,7 @@ impl TunTapDevice { + 1 /* message type header */ + match self.type_ { Type::Tap => 14, /* inner ethernet header */ - Type::Tun | Type::Dummy => 0 + Type::Tun => 0 } } @@ -357,7 +325,7 @@ impl MockDevice { impl Device for MockDevice { fn get_type(&self) -> Type { - Type::Dummy + Type::Tun } fn ifname(&self) -> &str { diff --git a/src/main.rs b/src/main.rs index 868c8d1..156f17d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -286,7 +286,6 @@ fn main() { debug!("Config: {:?}", config); match config.device_type { Type::Tap => run::(config), - Type::Tun => run::(config), - Type::Dummy => run::(config) + Type::Tun => run::(config) } } diff --git a/src/poll/epoll.rs b/src/poll/epoll.rs index 07b4c13..5d69d43 100644 --- a/src/poll/epoll.rs +++ b/src/poll/epoll.rs @@ -6,7 +6,7 @@ use crate::device::Device; use std::{io, os::unix::io::RawFd}; use super::WaitResult; -use crate::{device::Type, net::Socket}; +use crate::net::Socket; pub struct EpollWait { poll_fd: RawFd, @@ -31,15 +31,10 @@ impl EpollWait { if poll_fd == -1 { return Err(io::Error::last_os_error()) } - let raw_fds = if device.get_type() != Type::Dummy { - vec![socket.as_raw_fd(), device.as_raw_fd()] - } else { - vec![socket.as_raw_fd()] - }; - for fd in raw_fds { - event.u64 = fd as u64; + for fd in &[socket.as_raw_fd(), device.as_raw_fd()] { + event.u64 = *fd as u64; event.events = flags; - let res = unsafe { libc::epoll_ctl(poll_fd, libc::EPOLL_CTL_ADD, fd, &mut event) }; + let res = unsafe { libc::epoll_ctl(poll_fd, libc::EPOLL_CTL_ADD, *fd, &mut event) }; if res == -1 { return Err(io::Error::last_os_error()) }