2026-05-29 09:37:33 -04:00
|
|
|
use std::net::{IpAddr, Ipv4Addr};
|
|
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
|
|
use chrono::Local;
|
|
|
|
|
use log::{debug, error, info, warn};
|
|
|
|
|
|
|
|
|
|
use crate::db;
|
2026-05-29 09:58:45 -04:00
|
|
|
use crate::device_finders::mdns;
|
2026-05-29 09:37:33 -04:00
|
|
|
use crate::events;
|
|
|
|
|
use crate::mac_vendor_finder;
|
|
|
|
|
use crate::mdns_scanner_status;
|
|
|
|
|
use crate::model::devices::Device;
|
|
|
|
|
use crate::settings::get_settings;
|
|
|
|
|
use crate::vendor_device_type_finder;
|
|
|
|
|
|
|
|
|
|
/// Passively listen for mDNS/Bonjour announcements and feed discovered devices into the same
|
|
|
|
|
/// pipeline used by the ARP scanner (devices table + events + notifications).
|
|
|
|
|
pub async fn listen() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
|
let interface = get_settings().networking.interface.clone();
|
|
|
|
|
let socket = mdns::open_socket(interface.clone())?;
|
|
|
|
|
mdns_scanner_status::set_listening();
|
|
|
|
|
info!("mDNS scanner listening for announcements");
|
|
|
|
|
|
|
|
|
|
let probe_timeout = Duration::from(get_settings().mdns_scanner.probe_timeout);
|
|
|
|
|
|
|
|
|
|
let mut buf = [0u8; 4096];
|
|
|
|
|
loop {
|
|
|
|
|
let (len, src) = match socket.recv_from(&mut buf).await {
|
|
|
|
|
Ok(value) => value,
|
|
|
|
|
Err(err) => {
|
|
|
|
|
warn!("mDNS socket receive error: {err}");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let src_ip = match src.ip() {
|
|
|
|
|
IpAddr::V4(ip) => ip,
|
|
|
|
|
IpAddr::V6(_) => continue, // the device model is IPv4-only
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let hostname = match mdns::parse_announcement(&buf[..len]).into_iter().next() {
|
|
|
|
|
Some(host) => host,
|
|
|
|
|
None => continue,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
process_announcement(src_ip, hostname, interface.clone(), probe_timeout).await;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn process_announcement(
|
|
|
|
|
src_ip: Ipv4Addr,
|
|
|
|
|
hostname: String,
|
|
|
|
|
interface: Option<String>,
|
|
|
|
|
probe_timeout: Duration,
|
|
|
|
|
) {
|
2026-05-29 09:58:45 -04:00
|
|
|
let mac = match crate::utils::network::resolve_mac_address(src_ip, interface, probe_timeout).await {
|
2026-05-29 09:37:33 -04:00
|
|
|
Some(mac) => mac.to_string(),
|
|
|
|
|
None => {
|
|
|
|
|
debug!("Could not resolve MAC for mDNS device {src_ip} ({hostname}); skipping");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let vendor = mac_vendor_finder::find(mac.get(0..8).unwrap_or("").to_string());
|
|
|
|
|
let mut device = Device::new(
|
|
|
|
|
mac.clone(),
|
|
|
|
|
src_ip.to_string(),
|
|
|
|
|
vendor,
|
|
|
|
|
Local::now().to_utc(),
|
|
|
|
|
);
|
|
|
|
|
device.device_type = vendor_device_type_finder::find(&device.vendor);
|
|
|
|
|
device.name = Some(hostname);
|
|
|
|
|
|
|
|
|
|
match db::devices::read(mac.clone()) {
|
|
|
|
|
Some(recorded) => {
|
|
|
|
|
debug!("mDNS sighting of known device {mac}; updating");
|
|
|
|
|
if let Err(err) = db::devices::update(device.clone()) {
|
|
|
|
|
error!("Failed to update mDNS device {mac}: {err}");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// Ignoring errors: do not stop the listener if notification delivery fails
|
|
|
|
|
events::trigger_existing_device(recorded, device).ok();
|
|
|
|
|
}
|
|
|
|
|
None => {
|
|
|
|
|
debug!("New device {mac} discovered via mDNS; inserting");
|
|
|
|
|
if let Err(err) = db::devices::insert(device.clone()) {
|
|
|
|
|
error!("Failed to insert mDNS device {mac}: {err}");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
events::trigger_new_device(device).ok();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mdns_scanner_status::record_discovery();
|
|
|
|
|
}
|