From 027d3d24d97e80af8b46c82b9de6786aa126b5b9 Mon Sep 17 00:00:00 2001 From: Mathias Hall-Andersen Date: Sat, 2 Nov 2019 21:58:04 +0100 Subject: Work on configuration interface --- src/configuration/config.rs | 85 +++++++++++++++++++++++++++++++++------------ src/configuration/mod.rs | 1 + 2 files changed, 63 insertions(+), 23 deletions(-) (limited to 'src/configuration') diff --git a/src/configuration/config.rs b/src/configuration/config.rs index 6aa6a77..d68084c 100644 --- a/src/configuration/config.rs +++ b/src/configuration/config.rs @@ -19,15 +19,9 @@ pub struct PeerState { allowed_ips: Vec<(IpAddr, u32)>, } -struct UDPState { - fwmark: Option, - owner: O, - port: u16, -} - pub struct WireguardConfig { wireguard: Wireguard, - network: Mutex>>, + network: Mutex>, } impl WireguardConfig { @@ -42,6 +36,7 @@ impl WireguardConfig { pub enum ConfigError { NoSuchPeer, NotListening, + FailedToBind, } impl ConfigError { @@ -50,6 +45,7 @@ impl ConfigError { match self { ConfigError::NoSuchPeer => 1, ConfigError::NotListening => 2, + ConfigError::FailedToBind => 3, } } } @@ -77,7 +73,7 @@ pub trait Configuration { /// An integer indicating the protocol version fn get_protocol_version(&self) -> usize; - fn set_listen_port(&self, port: u16) -> Option; + fn set_listen_port(&self, port: Option) -> Option; /// Set the firewall mark (or similar, depending on platform) /// @@ -200,24 +196,39 @@ impl Configuration for WireguardConfig { 1 } - fn set_listen_port(&self, port: u16) -> Option { - let mut udp = self.network.lock(); + fn set_listen_port(&self, port: Option) -> Option { + let mut bind = self.network.lock(); // close the current listener - *udp = None; + *bind = None; + + // bind to new port + if let Some(port) = port { + // create new listener + let (mut readers, writer, owner) = match B::bind(port) { + Ok(r) => r, + Err(_) => { + return Some(ConfigError::FailedToBind); + } + }; + + // add readers/writer to wireguard + self.wireguard.set_writer(writer); + while let Some(reader) = readers.pop() { + self.wireguard.add_reader(reader); + } + + // create new UDP state + *bind = Some(owner); + } None } fn set_fwmark(&self, mark: Option) -> Option { match self.network.lock().as_mut() { - Some(mut bind) => { - // there is a active bind - // set the fwmark (the IO operation) - bind.owner.set_fwmark(mark).unwrap(); // TODO: handle - - // update stored value - bind.fwmark = mark; + Some(bind) => { + bind.set_fwmark(mark).unwrap(); // TODO: handle None } None => Some(ConfigError::NotListening), @@ -238,11 +249,21 @@ impl Configuration for WireguardConfig { } fn set_preshared_key(&self, peer: &PublicKey, psk: Option<[u8; 32]>) -> Option { - None + if self.wireguard.set_psk(*peer, psk) { + None + } else { + Some(ConfigError::NoSuchPeer) + } } fn set_endpoint(&self, peer: &PublicKey, addr: SocketAddr) -> Option { - None + match self.wireguard.lookup_peer(peer) { + Some(peer) => { + peer.router.set_endpoint(B::Endpoint::from_address(addr)); + None + } + None => Some(ConfigError::NoSuchPeer), + } } fn set_persistent_keepalive_interval( @@ -250,15 +271,33 @@ impl Configuration for WireguardConfig { peer: &PublicKey, interval: usize, ) -> Option { - None + match self.wireguard.lookup_peer(peer) { + Some(peer) => { + peer.set_persistent_keepalive_interval(interval); + None + } + None => Some(ConfigError::NoSuchPeer), + } } fn replace_allowed_ips(&self, peer: &PublicKey) -> Option { - None + match self.wireguard.lookup_peer(peer) { + Some(peer) => { + peer.router.remove_allowed_ips(); + None + } + None => Some(ConfigError::NoSuchPeer), + } } fn add_allowed_ip(&self, peer: &PublicKey, ip: IpAddr, masklen: u32) -> Option { - None + match self.wireguard.lookup_peer(peer) { + Some(peer) => { + peer.router.add_allowed_ip(ip, masklen); + None + } + None => Some(ConfigError::NoSuchPeer), + } } fn get_peers(&self) -> Vec { diff --git a/src/configuration/mod.rs b/src/configuration/mod.rs index 520b397..83419bc 100644 --- a/src/configuration/mod.rs +++ b/src/configuration/mod.rs @@ -1,5 +1,6 @@ mod config; +use super::platform::Endpoint; use super::platform::{bind, tun}; use super::wireguard::Wireguard; -- cgit v1.2.3-59-g8ed1b