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] 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);