Merge pull request #11 from shellrow/6-feature-request-interface-speed

6 feature request interface speed
main
shellrow 3 years ago committed by GitHub
commit cf7ca24e7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      Cargo.toml
  2. 4
      README.md
  3. 2
      examples/default_interface.rs
  4. 2
      examples/list_interfaces.rs
  5. 22
      src/interface/linux.rs
  6. 4
      src/interface/mod.rs
  7. 5
      src/interface/unix.rs
  8. 11
      src/interface/windows.rs

@ -1,6 +1,6 @@
[package] [package]
name = "default-net" name = "default-net"
version = "0.10.0" version = "0.11.0"
authors = ["shellrow <shellrow@protonmail.com>"] authors = ["shellrow <shellrow@protonmail.com>"]
edition = "2021" edition = "2021"
description = "Cross-platform library for network interface and gateway" description = "Cross-platform library for network interface and gateway"

@ -18,7 +18,7 @@
Add `default-net` to your dependencies Add `default-net` to your dependencies
```toml:Cargo.toml ```toml:Cargo.toml
[dependencies] [dependencies]
default-net = "0.10.0" default-net = "0.11.0"
``` ```
## Example ## Example
@ -43,6 +43,8 @@ fn main(){
println!("\tIPv4: {:?}", default_interface.ipv4); println!("\tIPv4: {:?}", default_interface.ipv4);
println!("\tIPv6: {:?}", default_interface.ipv6); println!("\tIPv6: {:?}", default_interface.ipv6);
println!("\tFlags: {:?}", default_interface.flags); 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 { if let Some(gateway) = default_interface.gateway {
println!("Default Gateway"); println!("Default Gateway");
println!("\tMAC: {}", gateway.mac_addr); println!("\tMAC: {}", gateway.mac_addr);

@ -17,6 +17,8 @@ fn main(){
println!("\tIPv4: {:?}", default_interface.ipv4); println!("\tIPv4: {:?}", default_interface.ipv4);
println!("\tIPv6: {:?}", default_interface.ipv6); println!("\tIPv6: {:?}", default_interface.ipv6);
println!("\tFlags: {:?}", default_interface.flags); 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 { if let Some(gateway) = default_interface.gateway {
println!("Default Gateway"); println!("Default Gateway");
println!("\tMAC: {}", gateway.mac_addr); println!("\tMAC: {}", gateway.mac_addr);

@ -17,6 +17,8 @@ fn main(){
println!("\tIPv4: {:?}", interface.ipv4); println!("\tIPv4: {:?}", interface.ipv4);
println!("\tIPv6: {:?}", interface.ipv6); println!("\tIPv6: {:?}", interface.ipv6);
println!("\tFlags: {:?}", interface.flags); println!("\tFlags: {:?}", interface.flags);
println!("\tTransmit Speed: {:?}", interface.transmit_speed);
println!("\tReceive Speed: {:?}", interface.receive_speed);
if let Some(gateway) = interface.gateway { if let Some(gateway) = interface.gateway {
println!("Gateway"); println!("Gateway");
println!("\tMAC: {}", gateway.mac_addr); println!("\tMAC: {}", gateway.mac_addr);

@ -22,3 +22,25 @@ pub fn get_interface_type(if_name: String) -> InterfaceType {
} }
}; };
} }
pub fn get_interface_speed(if_name: String) -> Option<u64> {
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::<u64>() {
Ok(if_speed) => {
// Convert Mbps to bps
return Some(if_speed * 1000000);
},
Err(_) => {
return None;
}
}
},
Err(_) => {
return None;
}
};
}

@ -89,6 +89,10 @@ pub struct Interface {
pub ipv6: Vec<Ipv6Net>, pub ipv6: Vec<Ipv6Net>,
/// Flags for the network interface (OS Specific) /// Flags for the network interface (OS Specific)
pub flags: u32, pub flags: u32,
/// Speed in bits per second of the transmit for the network interface
pub transmit_speed: Option<u64>,
/// Speed in bits per second of the receive for the network interface
pub receive_speed: Option<u64>,
/// Default gateway for the network interface /// Default gateway for the network interface
pub gateway: Option<Gateway>, pub gateway: Option<Gateway>,
} }

@ -95,6 +95,9 @@ pub fn interfaces() -> Vec<Interface> {
}; };
for iface in &mut interfaces { for iface in &mut interfaces {
iface.if_type = linux::get_interface_type(iface.name.clone()); iface.if_type = linux::get_interface_type(iface.name.clone());
let if_speed: Option<u64> = linux::get_interface_speed(iface.name.clone());
iface.transmit_speed = if_speed;
iface.receive_speed = if_speed;
match local_ip { match local_ip {
IpAddr::V4(local_ipv4) => { IpAddr::V4(local_ipv4) => {
if iface.ipv4.iter().any(|x| x.addr == local_ipv4) { if iface.ipv4.iter().any(|x| x.addr == local_ipv4) {
@ -248,6 +251,8 @@ pub fn unix_interfaces() -> Vec<Interface> {
ipv4: ini_ipv4, ipv4: ini_ipv4,
ipv6: ini_ipv6, ipv6: ini_ipv6,
flags: addr_ref.ifa_flags, flags: addr_ref.ifa_flags,
transmit_speed: None,
receive_speed: None,
gateway: None, gateway: None,
}; };
let mut found: bool = false; let mut found: bool = false;

@ -91,12 +91,15 @@ pub fn interfaces() -> Vec<Interface> {
// Description // Description
let p_desc = unsafe { (*cur).Description.0}; let p_desc = unsafe { (*cur).Description.0};
let desc_len = unsafe { wcslen(p_desc as *const wchar_t) }; 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 desc_slice = unsafe { std::slice::from_raw_parts(p_desc, desc_len) };
let description = String::from_utf16(aname_slice).unwrap(); let description = String::from_utf16(desc_slice).unwrap();
// MAC address // 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_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); 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<Ipv4Net> = vec![]; let mut ipv4_vec: Vec<Ipv4Net> = vec![];
let mut ipv6_vec: Vec<Ipv6Net> = vec![]; let mut ipv6_vec: Vec<Ipv6Net> = vec![];
// Enumerate all IPs // Enumerate all IPs
@ -168,6 +171,8 @@ pub fn interfaces() -> Vec<Interface> {
ipv4: ipv4_vec, ipv4: ipv4_vec,
ipv6: ipv6_vec, ipv6: ipv6_vec,
flags: flags, flags: flags,
transmit_speed: Some(transmit_speed),
receive_speed: Some(receive_speed),
gateway: default_gateway, gateway: default_gateway,
}; };
interfaces.push(interface); interfaces.push(interface);

Loading…
Cancel
Save