From: Ian Jackson Date: Sat, 24 Jul 2021 18:38:57 +0000 (+0100) Subject: config: wip defaulting X-Git-Tag: hippotat/1.0.0~460 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=hippotat.git;a=commitdiff_plain;h=61c21b67b0a15e6a16faf4ad8c3e35f5722aed06 config: wip defaulting Signed-off-by: Ian Jackson --- diff --git a/README.config b/README.config index a6db64e..4a9affa 100644 --- a/README.config +++ b/README.config @@ -130,6 +130,7 @@ Ordinary settings, used by both, not client-specific: vaddr Address of server's virtual interface. + [first host entry in , so 172.24.230.193] vrelay Virtual point-to-point address used for tunnel routing diff --git a/src/config.rs b/src/config.rs index 560233d..4607eb6 100644 --- a/src/config.rs +++ b/src/config.rs @@ -92,7 +92,7 @@ pub struct InstanceConfig { // Ordinary settings: pub addrs: Vec, pub vnetwork: Vec, - pub vaddr: Vec, + pub vaddr: IpAddr, pub vrelay: IpAddr, pub port: u16, pub mtu: u32, @@ -614,8 +614,48 @@ impl<'c> ResolveContext<'c> { impl InstanceConfig { #[throws(AE)] - fn complete(&self) { - + fn complete(&mut self, end: LinkEnd) { + let mut vhosts = self.vnetwork.iter() + .map(|n| n.hosts()).flatten() + .filter({ let vaddr = self.vaddr; move |v| v != &vaddr }); + + if self.vaddr.is_unspecified() { + self.vaddr = vhosts.next().ok_or_else( + || anyhow!("vnetwork too small to generate vaddrr") + )?; + } + if self.vrelay.is_unspecified() { + self.vrelay = vhosts.next().ok_or_else( + || anyhow!("vnetwork too small to generate vrelay") + )?; + } + + match end { + LinkEnd::Client => { + if &self.url == &default::() { + let addr = self.addrs.get(0).ok_or_else( + || anyhow!("client needs addrs or url set") + )?; + self.url = format!( + "http://{}{}/", + match addr { + IpAddr::V4(a) => format!("{}", a), + IpAddr::V6(a) => format!("[{}]", a), + }, + match self.port { + 80 => format!(""), + p => format!(":{}", p), + }) + .parse().unwrap() + } + }, + + LinkEnd::Server => { + if self.addrs.is_empty() { + throw!(anyhow!("missing 'addrs' setting")) + } + }, + } } } @@ -662,10 +702,10 @@ pub fn read(end: LinkEnd) -> Vec { ], }; - let ic = InstanceConfig::resolve_instance(&rctx) + let mut ic = InstanceConfig::resolve_instance(&rctx) .with_context(|| format!(r#"resolve config for "{:?}""#, &link))?; - ic.complete() + ic.complete(end) .with_context(|| format!(r#"complete config for "{:?}""#, &link))?; ics.push(ic);