From 92dbb4c46a5651afb8f92375e0ed154673929eeb Mon Sep 17 00:00:00 2001 From: Mathias Hall-Andersen Date: Thu, 21 Nov 2019 11:43:16 +0100 Subject: Clean dependencies and imports --- src/configuration/config.rs | 4 ++++ src/configuration/uapi/get.rs | 12 ++++++++++-- src/configuration/uapi/mod.rs | 13 ++++++------- src/configuration/uapi/set.rs | 12 ++++++++++-- 4 files changed, 30 insertions(+), 11 deletions(-) (limited to 'src/configuration') diff --git a/src/configuration/config.rs b/src/configuration/config.rs index e50aeb6..f11d90d 100644 --- a/src/configuration/config.rs +++ b/src/configuration/config.rs @@ -22,6 +22,8 @@ pub struct PeerState { pub last_handshake_time_nsec: u64, pub public_key: PublicKey, pub allowed_ips: Vec<(IpAddr, u32)>, + pub endpoint: Option, + pub persistent_keepalive_interval: u64, pub preshared_key: [u8; 32], // 0^32 is the "default value" } @@ -295,8 +297,10 @@ impl Configuration for WireguardConfig // extract state into PeerState state.push(PeerState { preshared_key: psk, + endpoint: p.router.get_endpoint(), rx_bytes: p.rx_bytes.load(Ordering::Relaxed), tx_bytes: p.tx_bytes.load(Ordering::Relaxed), + persistent_keepalive_interval: p.get_keepalive_interval(), allowed_ips: p.router.list_allowed_ips(), last_handshake_time_nsec: last_handshake.subsec_nanos() as u64, last_handshake_time_sec: last_handshake.as_secs(), diff --git a/src/configuration/uapi/get.rs b/src/configuration/uapi/get.rs index 43d4735..f0746b3 100644 --- a/src/configuration/uapi/get.rs +++ b/src/configuration/uapi/get.rs @@ -2,12 +2,13 @@ use log; use std::io; use super::Configuration; +use super::Endpoint; pub fn serialize(writer: &mut W, config: &C) -> io::Result<()> { let mut write = |key: &'static str, value: String| { debug_assert!(value.is_ascii()); debug_assert!(key.is_ascii()); - log::trace!("UAPI: return : {} = {}", key, value); + log::trace!("UAPI: return : {}={}", key, value); writer.write(key.as_ref())?; writer.write(b"=")?; writer.write(value.as_ref())?; @@ -30,6 +31,7 @@ pub fn serialize(writer: &mut W, config: &C) -> // serialize all peers let mut peers = config.get_peers(); while let Some(p) = peers.pop() { + write("public_key", hex::encode(p.public_key.as_bytes()))?; write("rx_bytes", p.rx_bytes.to_string())?; write("tx_bytes", p.tx_bytes.to_string())?; write( @@ -40,7 +42,13 @@ pub fn serialize(writer: &mut W, config: &C) -> "last_handshake_time_nsec", p.last_handshake_time_nsec.to_string(), )?; - write("public_key", hex::encode(p.public_key.as_bytes()))?; + write( + "persistent_keepalive_interval", + p.persistent_keepalive_interval.to_string(), + )?; + if let Some(endpoint) = p.endpoint { + write("endpoint", endpoint.into_address().to_string())?; + } write("preshared_key", hex::encode(p.preshared_key))?; for (ip, cidr) in p.allowed_ips { write("allowed_ip", ip.to_string() + "/" + &cidr.to_string())?; diff --git a/src/configuration/uapi/mod.rs b/src/configuration/uapi/mod.rs index 3cb88c0..4f0b741 100644 --- a/src/configuration/uapi/mod.rs +++ b/src/configuration/uapi/mod.rs @@ -4,6 +4,7 @@ mod set; use log; use std::io::{Read, Write}; +use super::Endpoint; use super::{ConfigError, Configuration}; use get::serialize; @@ -55,14 +56,12 @@ pub fn handle(stream: &mut S, config: &C) { loop { let ln = readline(stream)?; if ln == "" { - // end of transcript - parser.parse_line("", "")?; // flush final peer - break Ok(()); - } else { - let (k, v) = keypair(ln.as_str())?; - parser.parse_line(k, v)?; - }; + break; + } + let (k, v) = keypair(ln.as_str())?; + parser.parse_line(k, v)?; } + parser.parse_line("", "") } _ => Err(ConfigError::InvalidOperation), } diff --git a/src/configuration/uapi/set.rs b/src/configuration/uapi/set.rs index 882e4a7..b44ee1c 100644 --- a/src/configuration/uapi/set.rs +++ b/src/configuration/uapi/set.rs @@ -56,33 +56,40 @@ impl<'a, C: Configuration> LineParser<'a, C> { // flush peer updates to configuration fn flush_peer(config: &C, peer: &ParsedPeer) -> Option { if peer.remove { + log::trace!("flush peer, remove peer"); config.remove_peer(&peer.public_key); return None; } if !peer.update_only { + log::trace!("flush peer, add peer"); config.add_peer(&peer.public_key); } - for (ip, masklen) in &peer.allowed_ips { - config.add_allowed_ip(&peer.public_key, *ip, *masklen); + for (ip, cidr) in &peer.allowed_ips { + log::trace!("flush peer, add allowed_ips : {}/{}", ip.to_string(), cidr); + config.add_allowed_ip(&peer.public_key, *ip, *cidr); } if let Some(psk) = peer.preshared_key { + log::trace!("flush peer, set preshared_key {}", hex::encode(psk)); config.set_preshared_key(&peer.public_key, psk); } if let Some(secs) = peer.persistent_keepalive_interval { + log::trace!("flush peer, set persistent_keepalive_interval {}", secs); config.set_persistent_keepalive_interval(&peer.public_key, secs); } if let Some(version) = peer.protocol_version { + log::trace!("flush peer, set protocol_version {}", version); if version == 0 || version > config.get_protocol_version() { return Some(ConfigError::UnsupportedProtocolVersion); } } if let Some(endpoint) = peer.endpoint { + log::trace!("flush peer, set endpoint {}", endpoint.to_string()); config.set_endpoint(&peer.public_key, endpoint); }; @@ -232,6 +239,7 @@ impl<'a, C: Configuration> LineParser<'a, C> { // flush (used at end of transcipt) "" => { + log::trace!("UAPI, Set, processes end of transaction"); flush_peer(self.config, &peer); Ok(()) } -- cgit v1.2.3-59-g8ed1b