use std::collections::HashMap;
use std::fmt;
use std::net::Ipv4Addr;
use serde::Serialize;
use serde_json::Value;
use crate::generic::Error;
use crate::lists::{self, Branch};
use crate::read::{Reader, Values};
use crate::session::{self, Session};
use crate::settings;
use crate::status::{self, Host};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Lookup {
Mac(String),
Ip(Ipv4Addr),
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum InvalidAddress {
#[error("{0:?} is not a MAC address like AA:BB:CC:DD:EE:FF")]
Mac(String),
#[error("{0:?} is not an IPv4 address like 192.168.178.23")]
Ip(String),
}
impl Lookup {
pub fn mac(text: &str) -> Result<Self, InvalidAddress> {
let parts: Vec<&str> = text.split([':', '-']).collect();
let valid = parts.len() == 6
&& parts
.iter()
.all(|p| p.len() == 2 && p.chars().all(|c| c.is_ascii_hexdigit()));
if valid {
Ok(Self::Mac(parts.join(":").to_ascii_uppercase()))
} else {
Err(InvalidAddress::Mac(text.to_owned()))
}
}
pub fn ip(text: &str) -> Result<Self, InvalidAddress> {
text.parse()
.map(Self::Ip)
.map_err(|_| InvalidAddress::Ip(text.to_owned()))
}
fn matches(&self, host: &Host) -> bool {
match self {
Self::Mac(mac) => host
.mac
.as_deref()
.is_some_and(|m| m.eq_ignore_ascii_case(mac)),
Self::Ip(ip) => host.ip.as_deref() == Some(ip.to_string().as_str()),
}
}
}
impl fmt::Display for Lookup {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Mac(mac) => write!(f, "the MAC address {mac}"),
Self::Ip(ip) => write!(f, "the IP address {ip}"),
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
pub struct Detail {
#[serde(flatten)]
pub host: Host,
#[serde(skip_serializing_if = "Option::is_none")]
pub lease_remaining: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub attached_to: Option<Attachment>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
pub struct Attachment {
pub node: Option<String>,
pub kind: Option<String>,
pub interface: Option<String>,
pub band: Option<String>,
pub rate_down: Option<u64>,
pub rate_up: Option<u64>,
}
pub async fn lookup(reader: &mut Reader, lookup: &Lookup) -> Result<Option<Detail>, Error> {
let by_ip = match lookup {
Lookup::Mac(_) => None,
Lookup::Ip(ip) => {
let ip = ip.to_string();
let input = [("NewIPAddress", ip.as_str())];
reader
.try_read("Hosts1", "X_AVM-DE_GetSpecificHostEntryByIP", &input)
.await?
}
};
let listed = host_list(reader)
.await?
.into_iter()
.find(|h| lookup.matches(h));
let found = listed.is_some() || by_ip.is_some();
let mut host = listed.unwrap_or_default();
match lookup {
Lookup::Mac(mac) => host.mac = host.mac.or_else(|| Some(mac.clone())),
Lookup::Ip(ip) => host.ip = host.ip.or_else(|| Some(ip.to_string())),
}
let host = match &by_ip {
Some(by_ip) => overlay(host, by_ip),
None => host,
};
let entry = entry(reader, host.mac.as_deref()).await?;
if !found && entry.is_none() {
return Ok(None);
}
Ok(Some(complete(reader, host, entry.as_ref()).await?))
}
pub async fn detail(reader: &mut Reader, host: Host) -> Result<Detail, Error> {
let entry = entry(reader, host.mac.as_deref()).await?;
complete(reader, host, entry.as_ref()).await
}
async fn entry(reader: &mut Reader, mac: Option<&str>) -> Result<Option<Values>, Error> {
let Some(mac) = mac else { return Ok(None) };
let input = [("NewMACAddress", mac)];
reader
.try_read("Hosts1", "GetSpecificHostEntry", &input)
.await
}
async fn complete(
reader: &mut Reader,
host: Host,
entry: Option<&Values>,
) -> Result<Detail, Error> {
let host = match entry {
Some(entry) => overlay(host, entry),
None => host,
};
let lease_remaining = entry
.and_then(|e| e.number("NewLeaseTimeRemaining"))
.filter(|seconds| *seconds > 0);
let attached_to = match &host.mac {
Some(mac) => status::mesh(reader)
.await?
.and_then(|mesh| attachment(&mesh.tree(), mac)),
None => None,
};
Ok(Detail {
host,
lease_remaining,
attached_to,
})
}
async fn host_list(reader: &mut Reader) -> Result<Vec<Host>, Error> {
let xml = reader
.document(
"Hosts1",
"X_AVM-DE_GetHostListPath",
"NewX_AVM-DE_HostListPath",
)
.await?;
xml.map_or_else(|| Ok(Vec::new()), |xml| lists::hosts(&xml))
}
fn overlay(host: Host, entry: &Values) -> Host {
let text = |name: &str| entry.text(name);
Host {
name: text("NewHostName").or(host.name),
ip: text("NewIPAddress").or(host.ip),
mac: text("NewMACAddress").or(host.mac),
active: entry.flag("NewActive").unwrap_or(host.active),
interface: text("NewInterfaceType").or(host.interface),
address_source: text("NewAddressSource").or(host.address_source),
permanent_address: host.permanent_address,
guest: entry.flag("NewX_AVM-DE_Guest").or(host.guest),
internet_access: text("NewX_AVM-DE_WANAccess").or(host.internet_access),
speed: entry
.number("NewX_AVM-DE_Speed")
.filter(|speed| *speed > 0)
.or(host.speed),
}
}
fn attachment(branches: &[Branch], mac: &str) -> Option<Attachment> {
branches.iter().find_map(|parent| {
parent
.children
.iter()
.find(|child| (child.node.mac.as_deref()).is_some_and(|m| m.eq_ignore_ascii_case(mac)))
.map(|child| {
let uplink = child.uplinks.first().cloned().unwrap_or_default();
Attachment {
node: parent.node.name.clone(),
band: uplink.interface.as_deref().and_then(band),
kind: uplink.kind,
interface: uplink.interface,
rate_down: uplink.rate_down,
rate_up: uplink.rate_up,
}
})
.or_else(|| attachment(&parent.children, mac))
})
}
fn band(interface: &str) -> Option<String> {
interface.split(':').find_map(|part| match part {
"2G" => Some("2.4 GHz".to_owned()),
"5G" => Some("5 GHz".to_owned()),
"6G" => Some("6 GHz".to_owned()),
_ => None,
})
}
#[must_use]
pub fn named(hosts: Vec<Host>, name: &str) -> Vec<Host> {
let name = name.to_lowercase();
let lower = |host: &Host| host.name.as_deref().map(str::to_lowercase);
let (exact, rest): (Vec<Host>, Vec<Host>) = hosts
.into_iter()
.partition(|h| lower(h).as_deref() == Some(name.as_str()));
if exact.is_empty() {
rest.into_iter()
.filter(|h| lower(h).is_some_and(|n| n.contains(&name)))
.collect()
} else {
exact
}
}
#[must_use]
pub fn contains(host: &Host, text: &str) -> bool {
let text = text.to_lowercase();
[&host.name, &host.ip, &host.mac]
.into_iter()
.flatten()
.any(|v| v.to_lowercase().contains(&text))
}
pub async fn add_addressing(session: &Session, hosts: &mut [Host]) -> Result<(), session::Error> {
let devices = settings::module(session, "landevice").await?;
addressing(&devices, hosts);
Ok(())
}
pub fn addressing(devices: &Value, hosts: &mut [Host]) {
let flag = |device: &Value, name: &str| match device.get(name).and_then(Value::as_str) {
Some("1") => Some(true),
Some("0") => Some(false),
_ => None,
};
let by_mac: HashMap<String, &Value> = devices
.get("landevice")
.and_then(Value::as_array)
.into_iter()
.flatten()
.filter_map(|d| Some((d.get("mac")?.as_str()?.to_ascii_uppercase(), d)))
.collect();
for host in hosts {
let Some(device) = host
.mac
.as_deref()
.and_then(|mac| by_mac.get(&mac.to_ascii_uppercase()))
else {
continue;
};
if host.address_source.is_none() {
host.address_source =
flag(device, "dhcp").map(|dhcp| if dhcp { "DHCP" } else { "Static" }.to_owned());
}
host.permanent_address = flag(device, "static_dhcp");
}
}
#[cfg(test)]
mod tests {
use super::*;
fn host(name: &str) -> Host {
Host {
name: Some(name.to_owned()),
..Host::default()
}
}
#[test]
fn checks_addresses() {
assert_eq!(
Lookup::mac("aa-bb-cc-00-00-0f"),
Ok(Lookup::Mac("AA:BB:CC:00:00:0F".to_owned()))
);
for invalid in [
"",
"AA:BB:CC:00:00",
"AA:BB:CC:00:00:0G",
"AAB:B:CC:00:00:01",
] {
assert_eq!(
Lookup::mac(invalid),
Err(InvalidAddress::Mac(invalid.to_owned()))
);
}
assert_eq!(
Lookup::ip("192.0.2.1"),
Ok(Lookup::Ip(Ipv4Addr::new(192, 0, 2, 1)))
);
let error = Lookup::ip("2001:db8::1").unwrap_err();
assert_eq!(
error.to_string(),
"\"2001:db8::1\" is not an IPv4 address like 192.168.178.23"
);
assert_eq!(
Lookup::mac("x").unwrap_err().to_string(),
"\"x\" is not a MAC address like AA:BB:CC:DD:EE:FF"
);
assert_eq!(
Lookup::Ip(Ipv4Addr::new(192, 0, 2, 1)).to_string(),
"the IP address 192.0.2.1"
);
assert_eq!(
Lookup::Mac("AA:BB:CC:00:00:01".to_owned()).to_string(),
"the MAC address AA:BB:CC:00:00:01"
);
}
#[test]
fn finds_hosts_by_name() {
let hosts = || {
vec![
host("device-1"),
host("Device-11"),
host("device-11"),
Host::default(),
]
};
assert_eq!(named(hosts(), "DEVICE-1"), vec![host("device-1")]);
assert_eq!(named(hosts(), "device-11").len(), 2);
assert_eq!(named(hosts(), "ice-1").len(), 3);
assert_eq!(named(hosts(), "printer"), Vec::<Host>::new());
}
#[test]
fn searches_names_and_addresses() {
let host = Host {
name: Some("Laptop".to_owned()),
ip: Some("192.0.2.10".to_owned()),
mac: Some("AA:BB:CC:00:00:0A".to_owned()),
..Host::default()
};
assert!(contains(&host, "lap"));
assert!(contains(&host, "2.10"));
assert!(contains(&host, "00:0a"));
assert!(!contains(&host, "phone"));
}
#[test]
fn names_bands() {
assert_eq!(band("AP:2G:0").as_deref(), Some("2.4 GHz"));
assert_eq!(band("UPLINK:6G:0").as_deref(), Some("6 GHz"));
assert_eq!(band("AP:5G:1").as_deref(), Some("5 GHz"));
assert_eq!(band("LAN:1"), None);
}
#[test]
fn adds_the_addressing_of_lan_devices() {
let with_mac = |mac: &str, source: Option<&str>| Host {
mac: Some(mac.to_owned()),
address_source: source.map(str::to_owned),
..Host::default()
};
let mut hosts = [
with_mac("AA:BB:CC:00:00:01", None),
with_mac("aa:bb:cc:00:00:02", None),
with_mac("AA:BB:CC:00:00:03", Some("DHCP")),
with_mac("AA:BB:CC:00:00:04", None),
with_mac("AA:BB:CC:00:00:05", None),
Host::default(),
];
let devices = serde_json::json!({"landevice": [
{"mac": "aa:bb:cc:00:00:01", "dhcp": "1", "static_dhcp": "1"},
{"mac": "AA:BB:CC:00:00:02", "dhcp": "0", "static_dhcp": "0"},
{"mac": "AA:BB:CC:00:00:03", "dhcp": "0", "static_dhcp": "0"},
{"mac": "AA:BB:CC:00:00:04", "dhcp": "", "static_dhcp": 1},
{"name": "no MAC", "dhcp": "1"},
]});
addressing(&devices, &mut hosts);
let found: Vec<_> = hosts
.iter()
.map(|h| (h.address_source.as_deref(), h.permanent_address))
.collect();
assert_eq!(
found,
[
(Some("DHCP"), Some(true)),
(Some("Static"), Some(false)),
(Some("DHCP"), Some(false)),
(None, None),
(None, None),
(None, None),
]
);
let before = hosts.clone();
addressing(&serde_json::json!({}), &mut hosts);
assert_eq!(hosts, before);
}
}