From 3fa928b3158ce33a57e9ba2c1913485eb409ff4b Mon Sep 17 00:00:00 2001 From: Mathias Hall-Andersen Date: Wed, 23 Oct 2019 10:32:18 +0200 Subject: Work on platform specific code (Linux) --- src/platform/dummy.rs | 22 ++++++++++++++++++++++ src/platform/linux/mod.rs | 3 ++- src/platform/linux/tun.rs | 39 ++++++++++++++++++++------------------- src/platform/linux/udp.rs | 27 +++++++++++++++++++++++++++ src/platform/mod.rs | 23 +++++++++++++++++------ 5 files changed, 88 insertions(+), 26 deletions(-) create mode 100644 src/platform/dummy.rs (limited to 'src/platform') diff --git a/src/platform/dummy.rs b/src/platform/dummy.rs new file mode 100644 index 0000000..208febe --- /dev/null +++ b/src/platform/dummy.rs @@ -0,0 +1,22 @@ +#[cfg(test)] +use super::super::wireguard::dummy; +use super::BindOwner; +use super::PlatformBind; + +pub struct VoidOwner {} + +impl BindOwner for VoidOwner { + type Error = dummy::BindError; + + fn set_fwmark(&self, value: Option) -> Option { + None + } +} + +impl PlatformBind for dummy::PairBind { + type Owner = VoidOwner; + + fn bind(_port: u16) -> Result<(Vec, Self::Writer, Self::Owner), Self::Error> { + Err(dummy::BindError::Disconnected) + } +} diff --git a/src/platform/linux/mod.rs b/src/platform/linux/mod.rs index 7a456ad..7d6a61c 100644 --- a/src/platform/linux/mod.rs +++ b/src/platform/linux/mod.rs @@ -1,4 +1,5 @@ mod tun; mod udp; -pub use tun::PlatformTun; +pub use tun::LinuxTun; +pub use udp::LinuxBind; diff --git a/src/platform/linux/tun.rs b/src/platform/linux/tun.rs index 17390a1..5b7b105 100644 --- a/src/platform/linux/tun.rs +++ b/src/platform/linux/tun.rs @@ -1,6 +1,6 @@ use super::super::super::wireguard::tun::*; +use super::super::PlatformTun; use super::super::Tun; -use super::super::TunBind; use libc::*; @@ -32,13 +32,13 @@ struct Ifreq { _pad: [u8; 64], } -pub struct PlatformTun {} +pub struct LinuxTun {} -pub struct PlatformTunReader { +pub struct LinuxTunReader { fd: RawFd, } -pub struct PlatformTunWriter { +pub struct LinuxTunWriter { fd: RawFd, } @@ -46,7 +46,7 @@ pub struct PlatformTunWriter { * announcing an MTU update for the interface */ #[derive(Clone)] -pub struct PlatformTunMTU { +pub struct LinuxTunMTU { value: Arc, } @@ -83,14 +83,14 @@ impl Error for LinuxTunError { } } -impl MTU for PlatformTunMTU { +impl MTU for LinuxTunMTU { #[inline(always)] fn mtu(&self) -> usize { self.value.load(Ordering::Relaxed) } } -impl Reader for PlatformTunReader { +impl Reader for LinuxTunReader { type Error = LinuxTunError; fn read(&self, buf: &mut [u8], offset: usize) -> Result { @@ -109,7 +109,7 @@ impl Reader for PlatformTunReader { } } -impl Writer for PlatformTunWriter { +impl Writer for LinuxTunWriter { type Error = LinuxTunError; fn write(&self, src: &[u8]) -> Result<(), Self::Error> { @@ -120,14 +120,14 @@ impl Writer for PlatformTunWriter { } } -impl Tun for PlatformTun { +impl Tun for LinuxTun { type Error = LinuxTunError; - type Reader = PlatformTunReader; - type Writer = PlatformTunWriter; - type MTU = PlatformTunMTU; + type Reader = LinuxTunReader; + type Writer = LinuxTunWriter; + type MTU = LinuxTunMTU; } -impl TunBind for PlatformTun { +impl PlatformTun for LinuxTun { fn create(name: &str) -> Result<(Vec, Self::Writer, Self::MTU), Self::Error> { // construct request struct let mut req = Ifreq { @@ -157,10 +157,10 @@ impl TunBind for PlatformTun { // create PlatformTunMTU instance Ok(( - vec![PlatformTunReader { fd }], // TODO: enable multi-queue for Linux - PlatformTunWriter { fd }, - PlatformTunMTU { - value: Arc::new(AtomicUsize::new(1500)), + vec![LinuxTunReader { fd }], // TODO: enable multi-queue for Linux + LinuxTunWriter { fd }, + LinuxTunMTU { + value: Arc::new(AtomicUsize::new(1500)), // TODO: fetch and update }, )) } @@ -174,7 +174,7 @@ mod tests { fn is_root() -> bool { match env::var("USER") { Ok(val) => val == "root", - Err(e) => false, + Err(_) => false, } } @@ -183,6 +183,7 @@ mod tests { if !is_root() { return; } - let (readers, writers, mtu) = PlatformTun::create("test").unwrap(); + let (readers, writers, mtu) = LinuxTun::create("test").unwrap(); + // TODO: test (any good idea how?) } } diff --git a/src/platform/linux/udp.rs b/src/platform/linux/udp.rs index e69de29..0a1a186 100644 --- a/src/platform/linux/udp.rs +++ b/src/platform/linux/udp.rs @@ -0,0 +1,27 @@ +use super::super::Bind; +use super::super::Endpoint; +use super::super::PlatformBind; + +use std::net::SocketAddr; + +pub struct LinuxEndpoint {} + +pub struct LinuxBind {} + +impl Endpoint for LinuxEndpoint { + fn clear_src(&mut self) {} + + fn from_address(addr: SocketAddr) -> Self { + LinuxEndpoint {} + } + + fn into_address(&self) -> SocketAddr { + "127.0.0.1:6060".parse().unwrap() + } +} + +/* +impl Bind for PlatformBind { + type Endpoint = PlatformEndpoint; +} +*/ diff --git a/src/platform/mod.rs b/src/platform/mod.rs index de33714..a0bbc13 100644 --- a/src/platform/mod.rs +++ b/src/platform/mod.rs @@ -2,21 +2,32 @@ use std::error::Error; use super::wireguard::bind::Bind; use super::wireguard::tun::Tun; +use super::wireguard::Endpoint; + +#[cfg(test)] +mod dummy; #[cfg(target_os = "linux")] mod linux; #[cfg(target_os = "linux")] -pub use linux::PlatformTun; +pub use linux::LinuxTun as TunInstance; + +pub trait BindOwner: Send { + type Error: Error; + + fn set_fwmark(&self, value: Option) -> Option; +} -pub trait UDPBind: Bind { - type Closer; +pub trait PlatformBind: Bind { + type Owner: BindOwner; /// Bind to a new port, returning the reader/writer and - /// an associated instance of the Closer type, which closes the UDP socket upon "drop". - fn bind(port: u16) -> Result<(Self::Reader, Self::Writer, Self::Closer), Self::Error>; + /// an associated instance of the owner type, which closes the UDP socket upon "drop" + /// and enables configuration of the fwmark value. + fn bind(port: u16) -> Result<(Vec, Self::Writer, Self::Owner), Self::Error>; } -pub trait TunBind: Tun { +pub trait PlatformTun: Tun { fn create(name: &str) -> Result<(Vec, Self::Writer, Self::MTU), Self::Error>; } -- cgit v1.2.3-59-g8ed1b