diff options
Diffstat (limited to 'src/configuration/uapi')
-rw-r--r-- | src/configuration/uapi/get.rs | 41 | ||||
-rw-r--r-- | src/configuration/uapi/mod.rs | 45 | ||||
-rw-r--r-- | src/configuration/uapi/set.rs | 21 |
3 files changed, 91 insertions, 16 deletions
diff --git a/src/configuration/uapi/get.rs b/src/configuration/uapi/get.rs index c6f3c42..99ebbde 100644 --- a/src/configuration/uapi/get.rs +++ b/src/configuration/uapi/get.rs @@ -10,6 +10,45 @@ struct Serializer<C: Configuration> { impl<C: Configuration> Serializer<C> { fn get(&self) -> Vec<String> { - vec![] + let mut peers = self.config.get_peers(); + let mut lines = Vec::with_capacity(peers.len() * 6 + 5); + let mut write = |key, value: String| { + lines.push(String::new() + key + "=" + &value); + }; + + // serialize interface + self.config + .get_private_key() + .map(|sk| write("private_key", hex::encode(sk.to_bytes()))); + + self.config + .get_listen_port() + .map(|port| write("listen_port", port.to_string())); + + self.config + .get_fwmark() + .map(|fwmark| write("fwmark", fwmark.to_string())); + + // serialize all peers + while let Some(p) = peers.pop() { + write("rx_bytes", p.rx_bytes.to_string()); + write("tx_bytes", p.tx_bytes.to_string()); + write( + "last_handshake_time_sec", + p.last_handshake_time_nsec.to_string(), + ); + write( + "last_handshake_time_nsec", + p.last_handshake_time_nsec.to_string(), + ); + write("public_key", hex::encode(p.public_key.as_bytes())); + p.preshared_key + .map(|psk| write("preshared_key", hex::encode(psk))); + for (ip, cidr) in p.allowed_ips { + write("allowed_ip", ip.to_string() + "/" + &cidr.to_string()) + } + } + + lines } } diff --git a/src/configuration/uapi/mod.rs b/src/configuration/uapi/mod.rs index 5d89b94..cba156d 100644 --- a/src/configuration/uapi/mod.rs +++ b/src/configuration/uapi/mod.rs @@ -1,4 +1,49 @@ mod get; mod set; +use std::io::{Read, Write}; + use super::{ConfigError, Configuration}; + +const MAX_LINE_LENGTH: usize = 128; + +struct Parser<C: Configuration, R: Read, W: Write> { + config: C, + reader: R, + writer: W, +} + +impl<C: Configuration, R: Read, W: Write> Parser<C, R, W> { + fn new(&self, reader: R, writer: W, config: C) -> Parser<C, R, W> { + Parser { + config, + reader, + writer, + } + } + + fn parse(&mut self) -> Option<()> { + // read string up to maximum length (why is this not in std?) + let mut line = || { + let mut m: [u8; 1] = [0u8]; + let mut l: String = String::with_capacity(MAX_LINE_LENGTH); + while let Ok(_) = self.reader.read_exact(&mut m) { + let c = m[0] as char; + if c == '\n' { + return Some(l); + }; + l.push(c); + if l.len() > MAX_LINE_LENGTH { + break; + } + } + None + }; + + match line()?.as_str() { + "get=1" => Some(()), + "set=1" => Some(()), + _ => None, + } + } +} diff --git a/src/configuration/uapi/set.rs b/src/configuration/uapi/set.rs index 575c7ad..c609d83 100644 --- a/src/configuration/uapi/set.rs +++ b/src/configuration/uapi/set.rs @@ -7,8 +7,8 @@ use super::{ConfigError, Configuration}; #[derive(Copy, Clone)] enum ParserState { Peer { - public_key: PublicKey, // peer identity - update_only: bool, // is the update_only flag set + public_key: PublicKey, + update_only: bool, }, Interface, } @@ -18,10 +18,6 @@ struct LineParser<C: Configuration> { state: ParserState, } -struct Serializer<C: Configuration> { - config: C, -} - impl<C: Configuration> LineParser<C> { fn new_peer(value: &str) -> Result<ParserState, ConfigError> { match <[u8; 32]>::from_hex(value) { @@ -34,6 +30,7 @@ impl<C: Configuration> LineParser<C> { } fn parse_line(&mut self, key: &str, value: &str) -> Option<ConfigError> { + // add the peer if not update_only let flush_peer = |st: ParserState| -> ParserState { match st { ParserState::Peer { @@ -51,7 +48,7 @@ impl<C: Configuration> LineParser<C> { }; // parse line and update parser state - let new_state = match self.state { + match self.state { // configure the interface ParserState::Interface => match key { // opt: set private key @@ -202,14 +199,8 @@ impl<C: Configuration> LineParser<C> { // unknown key _ => Err(ConfigError::InvalidKey), }, - }; - - match new_state { - Err(e) => Some(e), - Ok(st) => { - self.state = st; - None - } } + .map(|st| self.state = st) + .err() } } |