sinf 0.1.0

Library for obtaining in-depth system information at both the hardware and software levels
Documentation
mod counters;
mod snapshot;

pub(crate) use self::snapshot::NetworkSnapshot;

use self::counters::NetworkCounters;
use crate::{InterfaceOperationalState, IpNetwork, MacAddr};

/// Shared network interface state used by every backend.
pub(crate) struct NetworkDataInner {
    counters: NetworkCounters,
    pub(crate) mac_addr: MacAddr,
    pub(crate) ip_networks: Vec<IpNetwork>,
    pub(crate) mtu: u64,
    pub(crate) transmit_link_speed: Option<u64>,
    pub(crate) receive_link_speed: Option<u64>,
    pub(crate) operational_state: InterfaceOperationalState,
    pub(crate) updated: bool,
}

impl NetworkDataInner {
    #[cfg(not(any(target_os = "ios", target_os = "freebsd", target_os = "netbsd")))]
    pub(crate) fn new(
        snapshot: NetworkSnapshot,
        mtu: u64,
        transmit_link_speed: Option<u64>,
        receive_link_speed: Option<u64>,
        operational_state: InterfaceOperationalState,
    ) -> Self {
        Self {
            counters: NetworkCounters::new(snapshot, snapshot),
            mac_addr: MacAddr::UNSPECIFIED,
            ip_networks: Vec::new(),
            mtu,
            transmit_link_speed,
            receive_link_speed,
            operational_state,
            updated: true,
        }
    }

    #[cfg(any(target_os = "freebsd", target_os = "netbsd"))]
    pub(crate) fn new_with_zero_history(
        snapshot: NetworkSnapshot,
        mtu: u64,
        transmit_link_speed: Option<u64>,
        receive_link_speed: Option<u64>,
        operational_state: InterfaceOperationalState,
    ) -> Self {
        Self {
            counters: NetworkCounters::new(snapshot, NetworkSnapshot::default()),
            mac_addr: MacAddr::UNSPECIFIED,
            ip_networks: Vec::new(),
            mtu,
            transmit_link_speed,
            receive_link_speed,
            operational_state,
            updated: true,
        }
    }

    #[cfg(target_os = "macos")]
    pub(crate) fn update_metadata(
        &mut self,
        mtu: u64,
        transmit_link_speed: Option<u64>,
        receive_link_speed: Option<u64>,
        operational_state: InterfaceOperationalState,
    ) {
        self.mtu = mtu;
        self.transmit_link_speed = transmit_link_speed;
        self.receive_link_speed = receive_link_speed;
        self.operational_state = operational_state;
        self.updated = true;
    }

    #[cfg(not(target_os = "ios"))]
    pub(crate) fn update(
        &mut self,
        snapshot: NetworkSnapshot,
        mtu: u64,
        transmit_link_speed: Option<u64>,
        receive_link_speed: Option<u64>,
        operational_state: InterfaceOperationalState,
    ) {
        self.counters.update(snapshot);
        self.mtu = mtu;
        self.transmit_link_speed = transmit_link_speed;
        self.receive_link_speed = receive_link_speed;
        self.operational_state = operational_state;
        self.updated = true;
    }

    pub(crate) fn received(&self) -> u64 {
        self.counters.received()
    }

    pub(crate) fn total_received(&self) -> u64 {
        self.counters.current.received
    }

    pub(crate) fn transmitted(&self) -> u64 {
        self.counters.transmitted()
    }

    pub(crate) fn total_transmitted(&self) -> u64 {
        self.counters.current.transmitted
    }

    pub(crate) fn packets_received(&self) -> u64 {
        self.counters.packets_received()
    }

    pub(crate) fn total_packets_received(&self) -> u64 {
        self.counters.current.packets_received
    }

    pub(crate) fn packets_transmitted(&self) -> u64 {
        self.counters.packets_transmitted()
    }

    pub(crate) fn total_packets_transmitted(&self) -> u64 {
        self.counters.current.packets_transmitted
    }

    pub(crate) fn errors_on_received(&self) -> u64 {
        self.counters.errors_received()
    }

    pub(crate) fn total_errors_on_received(&self) -> u64 {
        self.counters.current.errors_received
    }

    pub(crate) fn errors_on_transmitted(&self) -> u64 {
        self.counters.errors_transmitted()
    }

    pub(crate) fn total_errors_on_transmitted(&self) -> u64 {
        self.counters.current.errors_transmitted
    }

    pub(crate) fn mac_address(&self) -> MacAddr {
        self.mac_addr
    }

    pub(crate) fn ip_networks(&self) -> &[IpNetwork] {
        &self.ip_networks
    }

    pub(crate) fn mtu(&self) -> u64 {
        self.mtu
    }

    pub(crate) fn transmit_link_speed(&self) -> Option<u64> {
        self.transmit_link_speed
    }

    pub(crate) fn receive_link_speed(&self) -> Option<u64> {
        self.receive_link_speed
    }

    pub(crate) fn operational_state(&self) -> InterfaceOperationalState {
        self.operational_state
    }
}