Skip to content

Commit

Permalink
feat: add ip networks for interface
Browse files Browse the repository at this point in the history
  • Loading branch information
gongzhengyang committed Mar 5, 2024
1 parent 8da2f05 commit e6d2436
Show file tree
Hide file tree
Showing 13 changed files with 258 additions and 15 deletions.
180 changes: 167 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,17 @@ rustdoc-args = ["--generate-link-to-definition"]
cfg-if = "1.0"
rayon = { version = "^1.8", optional = true }
serde = { version = "^1.0.190", optional = true }
ipnetwork = "0.20"

[target.'cfg(any(windows, target_os = "linux", target_os = "android"))'.dependencies]
once_cell = "1.18"

[target.'cfg(unix)'.dependencies]
pnet_datalink = "0.34"

[target.'cfg(windows)'.dependencies]
ntapi = "0.4"
ipconfig = "0.3.2"
windows = { version = "0.52", features = [
"Wdk_System_SystemInformation",
"Wdk_System_SystemServices",
Expand Down
15 changes: 15 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
SystemInner, UserInner,
};

use ipnetwork::IpNetwork;
use std::cmp::Ordering;
use std::collections::{HashMap, HashSet};
use std::convert::TryFrom;
Expand Down Expand Up @@ -2328,6 +2329,20 @@ impl NetworkData {
pub fn mac_address(&self) -> MacAddr {
self.inner.mac_address()
}

/// Returns the Ip Networks associated to current interface.
///
/// ```no_run
/// use sysinfo::Networks;
///
/// let mut networks = Networks::new_with_refreshed_list();
/// for (interface_name, network) in &networks {
/// println!("Ip Networks: {}", network.ip_networks());
/// }
/// ```
pub fn ip_networks(&self) -> &[IpNetwork] {
self.inner.ip_networks()
}
}

/// Struct containing a disk information.
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub(crate) use crate::sys::{
NetworksInner, ProcessInner, SystemInner, UserInner,
};
pub use crate::sys::{IS_SUPPORTED_SYSTEM, MINIMUM_CPU_UPDATE_INTERVAL, SUPPORTED_SIGNALS};
pub use ipnetwork::IpNetwork;

#[cfg(feature = "c-interface")]
pub use crate::c_interface::*;
Expand Down
8 changes: 7 additions & 1 deletion src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

use std::collections::HashMap;

use crate::network_helper::get_interface_address;
use crate::network_helper::{get_interface_address, get_interface_ip_networks};
use crate::NetworkData;

/// Interface addresses are OS-independent
pub(crate) fn refresh_networks_addresses(interfaces: &mut HashMap<String, NetworkData>) {
let ip_network_map = get_interface_ip_networks();
for (interface_name, ip_networks) in ip_network_map {
if let Some(interface) = interfaces.get_mut(&interface_name) {
interface.inner.ip_networks = ip_networks;
}
}
match get_interface_address() {
Ok(ifa_iterator) => {
for (name, ifa) in ifa_iterator {
Expand Down
1 change: 1 addition & 0 deletions src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ impl Serialize for crate::NetworkData {
&self.total_errors_on_transmitted(),
)?;
state.serialize_field("mac_address", &self.mac_address())?;
state.serialize_field("ip_networks", &self.ip_networks())?;

state.end()
}
Expand Down
Loading

0 comments on commit e6d2436

Please sign in to comment.