From 135de73e3f4d4d0b35b0827a35afda7c59fb773c Mon Sep 17 00:00:00 2001 From: shellrow <81893184+shellrow@users.noreply.github.com> Date: Thu, 5 May 2022 16:05:26 +0900 Subject: [PATCH 1/3] Add Interface TX/RX speed --- examples/default_interface.rs | 4 +++- examples/list_interfaces.rs | 4 +++- src/interface/mod.rs | 4 ++++ src/interface/windows.rs | 11 ++++++++--- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/examples/default_interface.rs b/examples/default_interface.rs index 1739e22..0ee637a 100644 --- a/examples/default_interface.rs +++ b/examples/default_interface.rs @@ -6,7 +6,7 @@ fn main(){ println!("Default Interface"); println!("\tIndex: {}", default_interface.index); println!("\tName: {}", default_interface.name); - println!("\tFriendlyName: {:?}", default_interface.friendly_name); + println!("\tFriendly Name: {:?}", default_interface.friendly_name); println!("\tDescription: {:?}", default_interface.description); println!("\tType: {}", default_interface.if_type.name()); if let Some(mac_addr) = default_interface.mac_addr { @@ -17,6 +17,8 @@ fn main(){ println!("\tIPv4: {:?}", default_interface.ipv4); println!("\tIPv6: {:?}", default_interface.ipv6); println!("\tFlags: {:?}", default_interface.flags); + println!("\tTransmit Speed: {:?}", default_interface.transmit_speed); + println!("\tReceive Speed: {:?}", default_interface.receive_speed); if let Some(gateway) = default_interface.gateway { println!("Default Gateway"); println!("\tMAC: {}", gateway.mac_addr); diff --git a/examples/list_interfaces.rs b/examples/list_interfaces.rs index af5db7a..52edb22 100644 --- a/examples/list_interfaces.rs +++ b/examples/list_interfaces.rs @@ -6,7 +6,7 @@ fn main(){ println!("Interface"); println!("\tIndex: {}", interface.index); println!("\tName: {}", interface.name); - println!("\tFriendlyName: {:?}", interface.friendly_name); + println!("\tFriendly Name: {:?}", interface.friendly_name); println!("\tDescription: {:?}", interface.description); println!("\tType: {}", interface.if_type.name()); if let Some(mac_addr) = interface.mac_addr { @@ -17,6 +17,8 @@ fn main(){ println!("\tIPv4: {:?}", interface.ipv4); println!("\tIPv6: {:?}", interface.ipv6); println!("\tFlags: {:?}", interface.flags); + println!("\tTransmit Speed: {:?}", interface.transmit_speed); + println!("\tReceive Speed: {:?}", interface.receive_speed); if let Some(gateway) = interface.gateway { println!("Gateway"); println!("\tMAC: {}", gateway.mac_addr); diff --git a/src/interface/mod.rs b/src/interface/mod.rs index be620d6..e9e89df 100644 --- a/src/interface/mod.rs +++ b/src/interface/mod.rs @@ -89,6 +89,10 @@ pub struct Interface { pub ipv6: Vec, /// Flags for the network interface (OS Specific) pub flags: u32, + /// Speed in bits per second of the transmit for the network interface + pub transmit_speed: Option, + /// Speed in bits per second of the receive for the network interface + pub receive_speed: Option, /// Default gateway for the network interface pub gateway: Option, } diff --git a/src/interface/windows.rs b/src/interface/windows.rs index 199ee90..0d34a00 100644 --- a/src/interface/windows.rs +++ b/src/interface/windows.rs @@ -91,12 +91,15 @@ pub fn interfaces() -> Vec { // Description let p_desc = unsafe { (*cur).Description.0}; let desc_len = unsafe { wcslen(p_desc as *const wchar_t) }; - let aname_slice = unsafe { std::slice::from_raw_parts(p_desc, desc_len) }; - let description = String::from_utf16(aname_slice).unwrap(); + let desc_slice = unsafe { std::slice::from_raw_parts(p_desc, desc_len) }; + let description = String::from_utf16(desc_slice).unwrap(); // MAC address let mac_addr_arr: [u8; 6] = unsafe { (*cur).PhysicalAddress }[..6].try_into().unwrap_or([0, 0, 0, 0, 0, 0]); let mac_addr: MacAddr = MacAddr::new(mac_addr_arr); - + // TransmitLinkSpeed (bits per second) + let transmit_speed = unsafe { (*cur).TransmitLinkSpeed}; + // ReceiveLinkSpeed (bits per second) + let receive_speed = unsafe { (*cur).ReceiveLinkSpeed}; let mut ipv4_vec: Vec = vec![]; let mut ipv6_vec: Vec = vec![]; // Enumerate all IPs @@ -168,6 +171,8 @@ pub fn interfaces() -> Vec { ipv4: ipv4_vec, ipv6: ipv6_vec, flags: flags, + transmit_speed: Some(transmit_speed), + receive_speed: Some(receive_speed), gateway: default_gateway, }; interfaces.push(interface); From 32a12ef8234ad19edbfbdf0610d7a98e24b46b45 Mon Sep 17 00:00:00 2001 From: shellrow <81893184+shellrow@users.noreply.github.com> Date: Thu, 5 May 2022 16:30:41 +0900 Subject: [PATCH 2/3] Linux support --- src/interface/linux.rs | 22 ++++++++++++++++++++++ src/interface/unix.rs | 5 +++++ 2 files changed, 27 insertions(+) diff --git a/src/interface/linux.rs b/src/interface/linux.rs index bd9fae9..b00b8c4 100644 --- a/src/interface/linux.rs +++ b/src/interface/linux.rs @@ -22,3 +22,25 @@ pub fn get_interface_type(if_name: String) -> InterfaceType { } }; } + +pub fn get_interface_speed(if_name: String) -> Option { + let if_speed_path: String = format!("/sys/class/net/{}/speed", if_name); + let r = read_to_string(if_speed_path); + match r { + Ok(content) => { + let if_speed_string = content.trim().to_string(); + match if_speed_string.parse::() { + Ok(if_speed) => { + // Convert Mbps to bps + return Some(if_speed * 1000000); + }, + Err(_) => { + return None; + } + } + }, + Err(_) => { + return None; + } + }; +} diff --git a/src/interface/unix.rs b/src/interface/unix.rs index 2285ebe..ec5fb4c 100644 --- a/src/interface/unix.rs +++ b/src/interface/unix.rs @@ -95,6 +95,9 @@ pub fn interfaces() -> Vec { }; for iface in &mut interfaces { iface.if_type = linux::get_interface_type(iface.name.clone()); + let if_speed: Option = linux::get_interface_speed(iface.name.clone()); + iface.transmit_speed = if_speed; + iface.receive_speed = if_speed; match local_ip { IpAddr::V4(local_ipv4) => { if iface.ipv4.iter().any(|x| x.addr == local_ipv4) { @@ -248,6 +251,8 @@ pub fn unix_interfaces() -> Vec { ipv4: ini_ipv4, ipv6: ini_ipv6, flags: addr_ref.ifa_flags, + transmit_speed: None, + receive_speed: None, gateway: None, }; let mut found: bool = false; From 3acb8dc98eb04e986a262442e6c093670963041f Mon Sep 17 00:00:00 2001 From: shellrow <81893184+shellrow@users.noreply.github.com> Date: Thu, 5 May 2022 16:47:17 +0900 Subject: [PATCH 3/3] Bump version to 0.11.0 --- Cargo.toml | 2 +- README.md | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 836fb17..01ed7f1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "default-net" -version = "0.10.0" +version = "0.11.0" authors = ["shellrow "] edition = "2021" description = "Cross-platform library for network interface and gateway" diff --git a/README.md b/README.md index 909c2fd..cd0e18c 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ Add `default-net` to your dependencies ```toml:Cargo.toml [dependencies] -default-net = "0.10.0" +default-net = "0.11.0" ``` ## Example @@ -32,7 +32,7 @@ fn main(){ println!("Default Interface"); println!("\tIndex: {}", default_interface.index); println!("\tName: {}", default_interface.name); - println!("\tFriendlyName: {:?}", default_interface.friendly_name); + println!("\tFriendly Name: {:?}", default_interface.friendly_name); println!("\tDescription: {:?}", default_interface.description); println!("\tType: {}", default_interface.if_type.name()); if let Some(mac_addr) = default_interface.mac_addr { @@ -43,6 +43,8 @@ fn main(){ println!("\tIPv4: {:?}", default_interface.ipv4); println!("\tIPv6: {:?}", default_interface.ipv6); println!("\tFlags: {:?}", default_interface.flags); + println!("\tTransmit Speed: {:?}", default_interface.transmit_speed); + println!("\tReceive Speed: {:?}", default_interface.receive_speed); if let Some(gateway) = default_interface.gateway { println!("Default Gateway"); println!("\tMAC: {}", gateway.mac_addr);