From 2e8126cd739f12d5f54a55f35ee6323ecd7637ac Mon Sep 17 00:00:00 2001 From: Kyle Evans Date: Tue, 1 Dec 2020 23:32:04 -0600 Subject: [PATCH] config: allow hostnames for camera addresses This offers more flexibility, as a camera system admin only needs a DNS assignment rather than DHCP. The syntax is still as expected: address = "cameras.example.com:9000" This is especially handy in more complex topologies, where one or more specified "camera" may actually be pointed at a system providing a tunnel through to multiple cameras; names make it easier to identify said gateway machine. All IPs that the given name resolves to are tried so that we correctly handle, e.g., a name with both AAAA and A records. Note that addresses are no longer validated up front, but instead simply get resolved when the connection attempt is made. Name resolution will be aborted after ~60s of failure. --- src/bc_protocol.rs | 43 +++++++++++++++++++++++++++++-------------- src/config.rs | 3 +-- src/main.rs | 4 ++-- 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/bc_protocol.rs b/src/bc_protocol.rs index 09b78aa..72fb7bd 100644 --- a/src/bc_protocol.rs +++ b/src/bc_protocol.rs @@ -58,6 +58,9 @@ pub enum Error { #[error(display = "Credential error")] AuthFailed, + #[error(display = "Failed to translate camera address")] + AddrResolutionError, + #[error(display = "ADPCM Decoding Error")] AdpcmDecodingError(&'static str), @@ -81,22 +84,34 @@ impl Drop for BcCamera { } impl BcCamera { - pub fn new_with_addr(hostname: T) -> Result { - let address = hostname - .to_socket_addrs()? - .next() - .ok_or(Error::Other("Address resolution failed"))?; + pub fn connect(host: T) -> Result { + let addr_iter = match host.to_socket_addrs() { + Ok(iter) => iter, + Err(_) => return Err(Error::AddrResolutionError), + }; - Ok(Self { - address, - connection: None, - logged_in: false, - }) - } + for addr in addr_iter { + debug!("Trying {}", addr); + let conn = match BcConnection::new(addr, RX_TIMEOUT) { + Ok(conn) => conn, + Err(err) => match err { + connection::Error::CommunicationError(ref err) => { + debug!("Assuming timeout from {}", err); + continue; + } + err => return Err(err.into()), + }, + }; - pub fn connect(&mut self) -> Result<()> { - self.connection = Some(BcConnection::new(self.address, RX_TIMEOUT)?); - Ok(()) + debug!("Success: {}", addr); + return Ok(Self { + address: addr, + connection: Some(conn), + logged_in: false, + }); + } + + return Err(Error::Timeout); } pub fn disconnect(&mut self) { diff --git a/src/config.rs b/src/config.rs index 79b4af5..d99ae55 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,7 +2,6 @@ use lazy_static::lazy_static; use regex::Regex; use serde::Deserialize; use std::clone::Clone; -use std::net::SocketAddr; use std::time::Duration; use validator::{Validate, ValidationError}; use validator_derive::Validate; @@ -45,7 +44,7 @@ pub struct CameraConfig { pub name: String, #[serde(rename = "address")] - pub camera_addr: SocketAddr, + pub camera_addr: String, pub username: String, pub password: Option, diff --git a/src/main.rs b/src/main.rs index ffba065..44a63f3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -198,7 +198,6 @@ fn camera_main( ) -> Result { let mut connected = false; (|| { - let mut camera = BcCamera::new_with_addr(camera_config.camera_addr)?; if camera_config.timeout.is_some() { warn!("The undocumented `timeout` config option has been removed and is no longer needed."); warn!("Please update your config file."); @@ -208,7 +207,8 @@ fn camera_main( "{}: Connecting to camera at {}", camera_config.name, camera_config.camera_addr ); - camera.connect()?; + + let mut camera = BcCamera::connect(&camera_config.camera_addr)?; camera.login(&camera_config.username, camera_config.password.as_deref())?;