sinf 0.1.0

Library for obtaining in-depth system information at both the hardware and software levels
Documentation
use std::collections::HashMap;
use std::ffi::OsString;
use std::mem::MaybeUninit;

use super::utils;
use crate::network::refresh_networks_addresses;
use crate::platform::bsd::network::remove_stale;
use crate::platform::network_data::{NetworkDataInner, NetworkSnapshot};
use crate::{Error, InterfaceOperationalState, NetworkData};

pub(crate) struct NetworksInner {
    pub(crate) interfaces: HashMap<OsString, NetworkData>,
}

impl NetworksInner {
    pub(crate) fn new() -> Result<Self, Error> {
        Ok(Self {
            interfaces: HashMap::new(),
        })
    }

    pub(crate) fn list(&self) -> &HashMap<OsString, NetworkData> {
        &self.interfaces
    }

    pub(crate) fn refresh(&mut self, remove_not_listed_interfaces: bool) {
        unsafe {
            refresh_interfaces(&mut self.interfaces);
        }
        remove_stale(&mut self.interfaces, remove_not_listed_interfaces);
        refresh_networks_addresses(&mut self.interfaces);
    }
}

unsafe fn refresh_interfaces(interfaces: &mut HashMap<OsString, NetworkData>) {
    let mut nb_interfaces: libc::c_int = 0;
    if unsafe {
        !utils::get_sys_value(
            &[
                libc::CTL_NET,
                libc::PF_LINK,
                libc::NETLINK_GENERIC,
                libc::IFMIB_SYSTEM,
                libc::IFMIB_IFCOUNT,
            ],
            &mut nb_interfaces,
        )
    } {
        return;
    }

    for interface in interfaces.values_mut() {
        interface.inner.updated = false;
    }

    let mut data: libc::ifmibdata = unsafe { MaybeUninit::zeroed().assume_init() };
    for row in 1..=nb_interfaces {
        let mib = [
            libc::CTL_NET,
            libc::PF_LINK,
            libc::NETLINK_GENERIC,
            libc::IFMIB_IFDATA,
            row,
            libc::IFDATA_GENERAL,
        ];

        if unsafe { !utils::get_sys_value(&mib, &mut data) } {
            continue;
        }
        let name = utils::c_buf_to_os_string(&data.ifmd_name);
        if name.is_empty() {
            continue;
        }
        let flags = data.ifmd_flags;
        let data = &data.ifmd_data;
        let mtu = data.ifi_mtu as u64;
        let baudrate = data.ifi_baudrate;
        let link_speed = (baudrate != 0).then_some(baudrate);
        let operational_state =
            InterfaceOperationalState::from_flag(flags, data.ifi_link_state.into());
        let snapshot = NetworkSnapshot::new(
            data.ifi_ibytes,
            data.ifi_obytes,
            data.ifi_ipackets,
            data.ifi_opackets,
            data.ifi_ierrors,
            data.ifi_oerrors,
        );

        match interfaces.entry(name) {
            std::collections::hash_map::Entry::Occupied(mut entry) => {
                entry.get_mut().inner.update(
                    snapshot,
                    mtu,
                    link_speed,
                    link_speed,
                    operational_state,
                );
            }
            std::collections::hash_map::Entry::Vacant(entry) => {
                entry.insert(NetworkData {
                    inner: NetworkDataInner::new_with_zero_history(
                        snapshot,
                        mtu,
                        link_speed,
                        link_speed,
                        operational_state,
                    ),
                });
            }
        }
    }
}