use super::{InterfaceStats, InterfaceStatsProvider};
use std::ffi::CStr;
use std::io;
use std::ptr;
#[cfg(target_os = "macos")]
use std::time::SystemTime;
fn collect_af_link_stats(
extract: impl Fn(&libc::if_data, String) -> InterfaceStats,
) -> Result<Vec<InterfaceStats>, io::Error> {
unsafe {
let mut ifap: *mut libc::ifaddrs = ptr::null_mut();
if libc::getifaddrs(&mut ifap) != 0 {
return Err(io::Error::last_os_error());
}
let mut stats = Vec::new();
let mut current = ifap;
while let Some(ifa) = current.as_ref() {
if let Some(addr) = ifa.ifa_addr.as_ref()
&& addr.sa_family as i32 == libc::AF_LINK
{
let name = CStr::from_ptr(ifa.ifa_name).to_string_lossy().to_string();
if let Some(if_data) = (ifa.ifa_data as *const libc::if_data).as_ref() {
stats.push(extract(if_data, name));
}
}
current = ifa.ifa_next;
}
libc::freeifaddrs(ifap);
Ok(stats)
}
}
#[cfg(target_os = "macos")]
pub struct MacOSStatsProvider;
#[cfg(target_os = "macos")]
fn sanitize_counter(value: u32, total_packets: u32) -> u64 {
const MAX_REASONABLE_U32: u32 = 0x7FFF_FFFF;
if value > MAX_REASONABLE_U32 {
return 0;
}
if total_packets > 0 && value > total_packets {
return 0;
}
value as u64
}
#[cfg(target_os = "macos")]
impl InterfaceStatsProvider for MacOSStatsProvider {
fn get_all_stats(&self) -> Result<Vec<InterfaceStats>, io::Error> {
collect_af_link_stats(|if_data, name| {
let total_rx_packets = if_data.ifi_ipackets;
let total_tx_packets = if_data.ifi_opackets;
InterfaceStats {
interface_name: name,
rx_bytes: if_data.ifi_ibytes as u64,
tx_bytes: if_data.ifi_obytes as u64,
rx_packets: total_rx_packets as u64,
tx_packets: total_tx_packets as u64,
rx_errors: sanitize_counter(if_data.ifi_ierrors, total_rx_packets),
tx_errors: sanitize_counter(if_data.ifi_oerrors, total_tx_packets),
rx_dropped: sanitize_counter(if_data.ifi_iqdrops, total_rx_packets),
tx_dropped: 0, collisions: sanitize_counter(
if_data.ifi_collisions,
total_rx_packets + total_tx_packets,
),
timestamp: SystemTime::now(),
}
})
}
}
#[cfg(target_os = "freebsd")]
pub struct FreeBSDStatsProvider;
#[cfg(target_os = "freebsd")]
impl InterfaceStatsProvider for FreeBSDStatsProvider {
fn get_all_stats(&self) -> Result<Vec<InterfaceStats>, io::Error> {
collect_af_link_stats(|if_data, name| InterfaceStats {
interface_name: name,
rx_bytes: if_data.ifi_ibytes,
tx_bytes: if_data.ifi_obytes,
rx_packets: if_data.ifi_ipackets,
tx_packets: if_data.ifi_opackets,
rx_errors: if_data.ifi_ierrors,
tx_errors: if_data.ifi_oerrors,
rx_dropped: if_data.ifi_iqdrops,
tx_dropped: 0, collisions: if_data.ifi_collisions,
timestamp: std::time::SystemTime::now(),
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(target_os = "macos")]
#[test]
fn test_macos_list_interfaces() {
let provider = MacOSStatsProvider;
let stats = provider.get_all_stats().expect("Failed to list interfaces");
assert!(!stats.is_empty(), "Expected at least one interface");
assert!(
stats.iter().any(|s| s.interface_name.starts_with("lo")),
"Expected loopback interface"
);
for stat in stats {
assert!(!stat.interface_name.is_empty());
}
}
#[cfg(target_os = "freebsd")]
#[test]
fn test_freebsd_list_interfaces() {
let provider = FreeBSDStatsProvider;
let stats = provider.get_all_stats().expect("Failed to list interfaces");
assert!(!stats.is_empty(), "Expected at least one interface");
for stat in stats {
assert!(!stat.interface_name.is_empty());
}
}
}