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.
This commit is contained in:
Kyle Evans committed 2020-12-07 20:15:47 -06:00
1 parent 32400971a5
commit 2e8126cd73
3 files changed
+32 -18

No files matched your search

+29 -14
View File
@@ -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<T: ToSocketAddrs>(hostname: T) -> Result<Self> {
let address = hostname
.to_socket_addrs()?
.next()
.ok_or(Error::Other("Address resolution failed"))?;
pub fn connect<T: ToSocketAddrs>(host: T) -> Result<BcCamera> {
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) {
+1 -2
View File
@@ -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<String>,
+2 -2
View File
@@ -198,7 +198,6 @@ fn camera_main(
) -> Result<Never, CameraErr> {
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())?;