use std::net::SocketAddr;
use socket2::{Domain, Protocol, Socket, Type};
#[derive(Debug, Clone)]
pub struct NetworkHealthReport {
pub udp_bind_ok: bool,
pub broadcast_ok: bool,
pub multicast_ok: bool,
pub notes: Vec<String>,
}
impl NetworkHealthReport {
pub fn probe() -> Self {
let mut notes = Vec::new();
let mut udp_bind_ok = false;
let mut broadcast_ok = false;
let mut multicast_ok = false;
let udp = Socket::new(Domain::IPV4, Type::DGRAM, Some(Protocol::UDP));
match udp {
Ok(sock) => {
if sock
.bind(&SocketAddr::from(([0, 0, 0, 0], 0)).into())
.is_ok()
{
udp_bind_ok = true;
} else {
notes.push("udp bind failed".to_string());
}
if sock.set_broadcast(true).is_ok() {
broadcast_ok = true;
} else {
notes.push("udp broadcast disabled or blocked".to_string());
}
if sock.set_multicast_ttl_v4(4).is_ok() {
multicast_ok = true;
} else {
notes.push("udp multicast disabled or blocked".to_string());
}
}
Err(err) => {
notes.push(format!("udp socket creation failed: {}", err));
}
}
Self {
udp_bind_ok,
broadcast_ok,
multicast_ok,
notes,
}
}
pub fn ok(&self) -> bool {
self.udp_bind_ok && (self.broadcast_ok || self.multicast_ok)
}
}