From a85725eede89f8d7fecd10dc0628a01e48cccd7d Mon Sep 17 00:00:00 2001 From: Mathias Hall-Andersen Date: Wed, 13 Nov 2019 15:30:16 +0100 Subject: Initial version of full UAPI parser --- src/configuration/uapi/get.rs | 94 +++++++++++++++++++++---------------------- 1 file changed, 46 insertions(+), 48 deletions(-) (limited to 'src/configuration/uapi/get.rs') diff --git a/src/configuration/uapi/get.rs b/src/configuration/uapi/get.rs index 99ebbde..9b05421 100644 --- a/src/configuration/uapi/get.rs +++ b/src/configuration/uapi/get.rs @@ -1,54 +1,52 @@ use hex::FromHex; use subtle::ConstantTimeEq; -use x25519_dalek::{PublicKey, StaticSecret}; -use super::{ConfigError, Configuration}; - -struct Serializer { - config: C, -} - -impl Serializer { - fn get(&self) -> 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()) - } +use super::Configuration; +use std::io; + +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()); + writer.write(key.as_ref())?; + writer.write(b"=")?; + writer.write(value.as_ref()) + }; + + // serialize interface + config + .get_private_key() + .map(|sk| write("private_key", hex::encode(sk.to_bytes()))); + + config + .get_listen_port() + .map(|port| write("listen_port", port.to_string())); + + config + .get_fwmark() + .map(|fwmark| write("fwmark", fwmark.to_string())); + + // serialize all peers + let mut peers = config.get_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()))?; + if let Some(psk) = p.preshared_key { + write("preshared_key", hex::encode(psk))?; + } + for (ip, cidr) in p.allowed_ips { + write("allowed_ip", ip.to_string() + "/" + &cidr.to_string())?; } - - lines } + + Ok(()) } -- cgit v1.2.3-59-g8ed1b