From a4ff443588ad83f668a5434257dfcbb2716d5ef8 Mon Sep 17 00:00:00 2001 From: Hristo Venev Date: Mon, 30 Sep 2019 14:49:13 +0300 Subject: Document [peer], implement in procd. --- src/main.rs | 82 +++++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 56 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 0a6e356..f084c40 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,43 +37,64 @@ fn file_config(path: OsString) -> io::Result { } fn cli_config(args: &mut impl Iterator) -> Option { + enum State<'a> { + Source(&'a mut config::Source), + Peer(&'a mut config::Peer), + None, + } + use std::str::FromStr; let mut cfg = config::Config::default(); - let mut cur_src: Option<&mut config::Source> = None; + let mut cur = State::None; while let Some(key) = args.next() { let arg; - if let Some(ref mut s) = cur_src { - if key == "psk" { - arg = args.next()?; - let arg = arg.to_str()?; - s.psk = Some(model::Key::from_str(arg).ok()?); - continue; - } - if key == "ipv4" { - arg = args.next()?; - let arg = arg.to_str()?; - for arg in arg.split(',') { - s.ipv4.insert(model::Ipv4Net::from_str(arg).ok()?); + match cur { + State::Source(ref mut s) => { + if key == "psk" { + arg = args.next()?; + let arg = arg.to_str()?; + s.psk = Some(model::Key::from_str(arg).ok()?); + continue; } - continue; - } - if key == "ipv6" { - arg = args.next()?; - let arg = arg.to_str()?; - for arg in arg.split(',') { - s.ipv6.insert(model::Ipv6Net::from_str(arg).ok()?); + if key == "ipv4" { + arg = args.next()?; + let arg = arg.to_str()?; + for arg in arg.split(',') { + s.ipv4.insert(model::Ipv4Net::from_str(arg).ok()?); + } + continue; + } + if key == "ipv6" { + arg = args.next()?; + let arg = arg.to_str()?; + for arg in arg.split(',') { + s.ipv6.insert(model::Ipv6Net::from_str(arg).ok()?); + } + continue; + } + if key == "required" { + s.required = true; + continue; } - continue; } - if key == "required" { - s.required = true; - continue; + State::Peer(ref mut p) => { + if key == "psk" { + arg = args.next()?; + let arg = arg.to_str()?; + p.psk = Some(model::Key::from_str(arg).ok()?); + continue; + } + if key == "source" { + p.source = Some(args.next()?.into_string().ok()?); + continue; + } } + State::None => {} } - cur_src = None; + cur = State::None; if key == "min_keepalive" { arg = args.next()?; @@ -96,7 +117,7 @@ fn cli_config(args: &mut impl Iterator) -> Option) -> Option