Skip to content

Commit

Permalink
Greatly rework network API
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Feb 2, 2020
1 parent 0277a0a commit 2333ca7
Show file tree
Hide file tree
Showing 12 changed files with 253 additions and 177 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ rayon = "^1.0"
doc-comment = "0.3"

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["fileapi", "handleapi", "ioapiset", "minwindef", "pdh", "psapi", "synchapi", "sysinfoapi", "winbase", "winerror", "winioctl", "winnt", "oleauto", "wbemcli", "rpcdce", "combaseapi", "objidl", "objbase", "powerbase"] }
winapi = { version = "0.3", features = ["fileapi", "handleapi", "ifdef", "ioapiset", "minwindef", "pdh", "psapi", "synchapi", "sysinfoapi", "winbase", "winerror", "winioctl", "winnt", "oleauto", "wbemcli", "rpcdce", "combaseapi", "objidl", "objbase", "powerbase", "netioapi"] }
ntapi = "0.3"

[target.'cfg(not(any(target_os = "unknown", target_arch = "wasm32")))'.dependencies]
Expand Down
33 changes: 18 additions & 15 deletions src/linux/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ use NetworksExt;
use NetworksIter;

/// Network interfaces.
///
/// ```no_run
/// use sysinfo::{NetworksExt, System, SystemExt};
///
/// let s = System::new();
/// let networks = s.get_networks();
/// ```
#[derive(Debug)]
pub struct Networks {
interfaces: HashMap<String, NetworkData>,
Expand Down Expand Up @@ -54,20 +61,22 @@ impl Networks {
interfaces: HashMap::new(),
}
}
}

pub(crate) fn update(&mut self) {
if self.interfaces.is_empty() {
self.refresh_interfaces_list();
} else {
let mut v = vec![0; 30];
impl NetworksExt for Networks {
fn iter<'a>(&'a self) -> NetworksIter<'a> {
NetworksIter::new(self.interfaces.iter())
}

for (interface_name, data) in self.interfaces.iter_mut() {
data.update(interface_name, &mut v);
}
fn refresh(&mut self) {
let mut v = vec![0; 30];

for (interface_name, data) in self.interfaces.iter_mut() {
data.update(interface_name, &mut v);
}
}

pub(crate) fn refresh_interfaces_list(&mut self) {
fn refresh_interfaces_list(&mut self) {
if let Ok(dir) = std::fs::read_dir("/sys/class/net/") {
let mut data = vec![0; 30];
for entry in dir {
Expand Down Expand Up @@ -117,12 +126,6 @@ impl Networks {
}
}

impl NetworksExt for Networks {
fn iter<'a>(&'a self) -> NetworksIter<'a> {
NetworksIter::new(self.interfaces.iter())
}
}

/// Contains network information.
#[derive(Debug)]
pub struct NetworkData {
Expand Down
18 changes: 11 additions & 7 deletions src/linux/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ use sys::component::{self, Component};
use sys::disk;
use sys::process::*;
use sys::processor::*;
use sys::Disk;
use sys::Networks;

use Disk;
use LoadAvg;
use Networks;
use Pid;
use {DiskExt, ProcessExt, RefreshKind, SystemExt};
use {DiskExt, NetworksExt, ProcessExt, RefreshKind, SystemExt};

use libc::{self, gid_t, sysconf, uid_t, _SC_CLK_TCK, _SC_PAGESIZE};
use std::cell::UnsafeCell;
Expand Down Expand Up @@ -224,6 +225,9 @@ impl SystemExt for System {
networks: Networks::new(),
uptime: get_uptime(),
};
if refreshes.networks() {
s.networks.refresh_interfaces_list();
}
s.refresh_specifics(refreshes);
s
}
Expand Down Expand Up @@ -313,10 +317,6 @@ impl SystemExt for System {
self.disks = get_all_disks();
}

fn refresh_networks(&mut self) {
self.networks.update();
}

// COMMON PART
//
// Need to be moved into a "common" file to avoid duplication.
Expand All @@ -333,6 +333,10 @@ impl SystemExt for System {
&self.networks
}

fn get_networks_mut(&mut self) -> &mut Networks {
&mut self.networks
}

fn get_processor_list(&self) -> &[Processor] {
&self.processors[..]
}
Expand Down
21 changes: 16 additions & 5 deletions src/mac/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ use NetworksExt;
use NetworksIter;

/// Network interfaces.
///
/// ```no_run
/// use sysinfo::{NetworksExt, System, SystemExt};
///
/// let s = System::new();
/// let networks = s.get_networks();
/// ```
pub struct Networks {
interfaces: HashMap<String, NetworkData>,
}
Expand All @@ -25,9 +32,15 @@ impl Networks {
interfaces: HashMap::new(),
}
}
}

impl NetworksExt for Networks {
fn iter<'a>(&'a self) -> NetworksIter<'a> {
NetworksIter::new(self.interfaces.iter())
}

#[allow(clippy::cast_ptr_alignment)]
pub(crate) fn update(&mut self) {
fn refresh_interfaces_list(&mut self) {
let mib = &mut [CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST2, 0];
let mut len = 0;
if unsafe { libc::sysctl(mib.as_mut_ptr(), 6, null_mut(), &mut len, null_mut(), 0) } < 0 {
Expand Down Expand Up @@ -88,11 +101,9 @@ impl Networks {
}
}
}
}

impl NetworksExt for Networks {
fn iter<'a>(&'a self) -> NetworksIter<'a> {
NetworksIter::new(self.interfaces.iter())
fn refresh(&mut self) {
self.refresh_interfaces_list();
}
}

Expand Down
13 changes: 8 additions & 5 deletions src/mac/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use sys::network::Networks;
use sys::process::*;
use sys::processor::*;

use {DiskExt, LoadAvg, Pid, ProcessExt, ProcessorExt, RefreshKind, SystemExt};
use {DiskExt, LoadAvg, NetworksExt, Pid, ProcessExt, ProcessorExt, RefreshKind, SystemExt};

use std::cell::UnsafeCell;
use std::collections::HashMap;
Expand Down Expand Up @@ -86,6 +86,9 @@ impl SystemExt for System {
uptime: get_uptime(),
port: unsafe { ffi::mach_host_self() },
};
if refreshes.networks() {
s.networks.refresh_interfaces_list();
}
s.refresh_specifics(refreshes);
s
}
Expand Down Expand Up @@ -279,10 +282,6 @@ impl SystemExt for System {
}
}

fn refresh_networks(&mut self) {
self.networks.update();
}

fn refresh_processes(&mut self) {
let count = unsafe { ffi::proc_listallpids(::std::ptr::null_mut(), 0) };
if count < 1 {
Expand Down Expand Up @@ -351,6 +350,10 @@ impl SystemExt for System {
&self.networks
}

fn get_networks_mut(&mut self) -> &mut Networks {
&mut self.networks
}

fn get_total_memory(&self) -> u64 {
self.mem_total
}
Expand Down
82 changes: 81 additions & 1 deletion src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,10 +532,47 @@ pub trait SystemExt: Sized {
/// let mut s = System::new();
/// s.refresh_networks();
/// ```
fn refresh_networks(&mut self);
///
/// It is a shortcut for:
///
/// ```no_run
/// use sysinfo::{NetworksExt, System, SystemExt};
///
/// let mut s = System::new();
/// let networks = s.get_networks_mut();
/// networks.refresh();
/// ```
fn refresh_networks(&mut self) {
self.get_networks_mut().refresh();
}

/// The network list will be emptied then completely recomputed.
///
/// ```no_run
/// use sysinfo::{System, SystemExt};
///
/// let mut s = System::new();
/// s.refresh_network_interfaces();
/// ```
///
/// This is a shortcut for:
///
/// ```no_run
/// use sysinfo::{NetworksExt, System, SystemExt};
///
/// let mut s = System::new();
/// let networks = s.get_networks_mut();
/// networks.refresh_interfaces_list();
/// ```
fn refresh_network_interfaces(&mut self) {
self.get_networks_mut().refresh_interfaces_list();
}

/// Refreshes all system, processes and disks information.
///
/// Please note that it doesn't recompute disks list, components list nor network interfaces
/// list.
///
/// ```no_run
/// use sysinfo::{System, SystemExt};
///
Expand Down Expand Up @@ -702,6 +739,17 @@ pub trait SystemExt: Sized {
/// ```
fn get_networks(&self) -> &Networks;

/// Returns a mutable access to network interfaces.
///
/// ```no_run
/// use sysinfo::{NetworkExt, NetworksExt, System, SystemExt};
///
/// let mut s = System::new();
/// let networks = s.get_networks_mut();
/// networks.refresh_interfaces_list();
/// ```
fn get_networks_mut(&mut self) -> &mut Networks;

/// Returns system uptime.
///
/// ```no_run
Expand Down Expand Up @@ -787,7 +835,39 @@ pub trait NetworkExt {
/// Interacting with network interfaces.
pub trait NetworksExt {
/// Returns an iterator over the network interfaces.
///
/// ```no_run
/// use sysinfo::{NetworkExt, NetworksExt, System, SystemExt};
///
/// let s = System::new();
/// let networks = s.get_networks();
/// for (interface_name, network) in networks.iter() {
/// println!("in: {} B", network.get_income());
/// }
/// ```
fn iter(&self) -> NetworksIter;

/// Refreshes the network interfaces list.
///
/// ```no_run
/// use sysinfo::{NetworksExt, System, SystemExt};
///
/// let mut s = System::new();
/// let networks = s.get_networks_mut();
/// networks.refresh_interfaces_list();
/// ```
fn refresh_interfaces_list(&mut self);

/// Refreshes the network interfaces' content.
///
/// ```no_run
/// use sysinfo::{NetworksExt, System, SystemExt};
///
/// let mut s = System::new();
/// let networks = s.get_networks_mut();
/// networks.refresh();
/// ```
fn refresh(&mut self);
}

/// Getting a component temperature information.
Expand Down
27 changes: 22 additions & 5 deletions src/windows/ffi.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
//
// Sysinfo
//
// Copyright (c) 2020 Guillaume Gomez
//

// TO BE REMOVED ONCE https://github.com/retep998/winapi-rs/pull/802 IS MERGED!!!
use shared::ifdef::{NET_LUID, NET_IFINDEX};
use shared::ntdef::{UCHAR, ULONG, ULONG64, PVOID, WCHAR};
use shared::guiddef::GUID;
use shared::minwindef::BYTE;
use shared::netioapi::NETIOAPI_API;

#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]

use winapi::{ENUM, STRUCT};
use winapi::shared::basetsd::ULONG64;
use winapi::shared::ifdef::{NET_LUID, NET_IFINDEX};
use winapi::shared::ntdef::{UCHAR, ULONG, PVOID, WCHAR};
use winapi::shared::guiddef::GUID;
use winapi::shared::minwindef::BYTE;
use winapi::shared::netioapi::NETIOAPI_API;

const ANY_SIZE: usize = 1;

Expand All @@ -12,6 +25,7 @@ pub const IF_MAX_PHYS_ADDRESS_LENGTH: usize = 32;

pub type NET_IF_NETWORK_GUID = GUID;
pub type PMIB_IF_TABLE2 = *mut MIB_IF_TABLE2;
pub type PMIB_IF_ROW2 = *mut MIB_IF_ROW2;

STRUCT!{struct MIB_IF_TABLE2 {
NumEntries: ULONG,
Expand Down Expand Up @@ -172,6 +186,9 @@ extern "system" {
pub fn GetIfTable2(
Table: *mut PMIB_IF_TABLE2,
) -> NETIOAPI_API;
pub fn GetIfEntry2(
Row: PMIB_IF_ROW2,
) -> NETIOAPI_API;
pub fn FreeMibTable(
Memory: PVOID,
);
Expand Down
2 changes: 1 addition & 1 deletion src/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mod ffi;

pub use self::component::Component;
pub use self::disk::{Disk, DiskType};
pub use self::network::NetworkData;
pub use self::network::{Networks, NetworkData};
pub use self::process::{Process, ProcessStatus};
pub use self::processor::Processor;
pub use self::system::System;
Loading

0 comments on commit 2333ca7

Please sign in to comment.