Skip to content

Commit 1428497

Browse files
committed
Add BSD and macOS link speed implementation
This commit adds code to get the freebsd, openbsd, netbsd and macOS transmit and receive speed using `ioctl`. Furthermore, it also add the Wi-Fi transmit rate on macOS. Additionally, the auto_negotiate field of `Interface` shows if the interface link speed has been auto-negotiated. If `false`, the interface link speed is either configured manually or not available. Unfortunately, this is a rather large commit because every BSD variant uses different constants for their if_media sub types. I didn't find a better way to include all different values while still reusing the ones that are equal across the various operating systems. Implements #158
1 parent 53511c5 commit 1428497

10 files changed

Lines changed: 854 additions & 11 deletions

File tree

‎Cargo.toml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ objc2-core-foundation = "0.3"
4141
objc2-system-configuration = { version = "0.3", features = ["SCNetworkConfiguration"] }
4242
plist = "1.8"
4343

44+
[target.'cfg(target_os = "macos")'.dependencies]
45+
objc2-core-wlan = { version = "0.3" }
46+
objc2-foundation = { version = "0.3" }
47+
4448
[target.'cfg(target_os = "ios")'.dependencies]
4549
dispatch2 = "0.3"
4650
block2 = "0.6"

‎examples/default_interface.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ fn main() {
2828
println!("\tIPv6: {:?}", interface.ipv6);
2929
println!("\tTransmit Speed: {:?}", interface.transmit_speed);
3030
println!("\tReceive Speed: {:?}", interface.receive_speed);
31+
println!("\tAuto-negotiate: {:?}", interface.auto_negotiate);
3132
println!("\tStats: {:?}", interface.stats);
3233
if let Some(gateway) = interface.gateway {
3334
println!("Default Gateway");

‎examples/list_interfaces.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ fn main() {
6060

6161
println!("\tTransmit Speed: {:?}", interface.transmit_speed);
6262
println!("\tReceive Speed: {:?}", interface.receive_speed);
63+
println!("\tAuto-negotiate: {:?}", interface.auto_negotiate);
6364
println!("\tStats: {:?}", interface.stats);
6465
#[cfg(feature = "gateway")]
6566
if let Some(gateway) = interface.gateway {

‎src/interface/interface.rs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,18 @@ pub struct Interface {
7171
pub oper_state: OperState,
7272
/// Transmit link speed in bits per second.
7373
///
74-
/// This field is usually available on Linux, Android, and Windows.
74+
/// This field is usually available on Linux, Android, BSD, macOS and Windows.
7575
/// It may be `None` for virtual adapters, unsupported drivers, or platforms that do not
7676
/// expose link speed.
7777
pub transmit_speed: Option<u64>,
7878
/// Reported receive link speed in bits per second.
7979
///
8080
/// This field follows the same availability rules as `Interface::transmit_speed`.
8181
pub receive_speed: Option<u64>,
82+
/// Whether this interface was configured to auto-negotiate the link speed.
83+
///
84+
/// It may `None` if reading this information has not been implemented for a specific OS.
85+
pub auto_negotiate: Option<bool>,
8286
/// Traffic counters captured when the interface snapshot was collected.
8387
///
8488
/// The counters are cumulative totals reported by the OS, typically since boot.
@@ -133,6 +137,7 @@ impl Interface {
133137
oper_state: OperState::Unknown,
134138
transmit_speed: None,
135139
receive_speed: None,
140+
auto_negotiate: None,
136141
stats: None,
137142
#[cfg(feature = "gateway")]
138143
gateway: None,

‎src/os/macos/interface.rs‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,19 @@ use crate::os::darwin::types::{get_functional_type, interface_type_by_name};
22
use crate::os::macos::sc::{get_sc_interface_map, read_sc_interfaces_plist_map};
33
use crate::{
44
interface::interface::Interface,
5-
os::{macos::sc::SCInterface, unix::interface::unix_interfaces},
5+
os::{macos::sc::SCInterface, macos::wifi::get_wifi_transmit_rate, unix::interface::unix_interfaces},
66
prelude::InterfaceType,
77
};
8+
89
use std::collections::HashMap;
910

1011
pub fn interfaces() -> Vec<Interface> {
1112
let mut ifaces: Vec<Interface> = unix_interfaces();
1213

13-
let if_extra_map: HashMap<String, SCInterface> = match read_sc_interfaces_plist_map() {
14-
Ok(m) => m,
15-
Err(_) => {
16-
// Fallback to SCNetworkInterfaceCopyAll ...
17-
get_sc_interface_map()
18-
}
19-
};
14+
let if_extra_map: HashMap<String, SCInterface> = read_sc_interfaces_plist_map().unwrap_or_else(|_| {
15+
// Fallback to SCNetworkInterfaceCopyAll ...
16+
get_sc_interface_map()
17+
});
2018

2119
#[cfg(feature = "gateway")]
2220
let gateway_map = crate::os::darwin::route::get_gateway_map();
@@ -40,6 +38,10 @@ pub fn interfaces() -> Vec<Interface> {
4038
iface.friendly_name = sc_inface.friendly_name.clone();
4139
}
4240

41+
if iface.if_type == InterfaceType::Wireless80211 {
42+
iface.transmit_speed = get_wifi_transmit_rate(&iface.name);
43+
}
44+
4345
#[cfg(feature = "gateway")]
4446
{
4547
if let Some(gateway) = gateway_map.get(&iface.index) {

‎src/os/macos/mod.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
pub mod interface;
22
pub mod sc;
3+
4+
mod wifi;

‎src/os/macos/wifi.rs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use objc2_core_wlan::CWWiFiClient;
2+
use objc2_foundation::NSString;
3+
4+
/// Returns the macOS Wi-Fi transmit rate in bps for the given interface name.
5+
pub(crate) fn get_wifi_transmit_rate(iface_name: &str) -> Option<u64> {
6+
let client = unsafe { CWWiFiClient::sharedWiFiClient() };
7+
let name = NSString::from_str(iface_name);
8+
9+
let wifi_iface = unsafe { client.interfaceWithName(Some(&name)) };
10+
wifi_iface.map(|i| {
11+
let transmit_rate = unsafe { i.transmitRate() };
12+
return (transmit_rate * 1e6) as u64;
13+
})
14+
}

‎src/os/unix/interface.rs‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ use std::mem::MaybeUninit;
33
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
44
use std::os::raw::c_char;
55

6-
use super::sockaddr::{SockaddrRef, compute_sockaddr_len, netmask_ip_autolen, try_mac_from_raw};
6+
use super::sockaddr::{compute_sockaddr_len, netmask_ip_autolen, try_mac_from_raw, SockaddrRef};
77
use crate::interface::interface::Interface;
88
use crate::interface::ipv6_addr_flags::get_ipv6_addr_flags;
99
use crate::interface::mtu::get_mtu;
1010
use crate::interface::state::OperState;
1111
use crate::ipnet::{Ipv4Net, Ipv6Net};
12+
use crate::os::unix::link_speed::get_link_speed;
1213
use crate::os::unix::types::{get_interface_type, interface_name_from_ptr};
13-
use crate::stats::counters::{InterfaceStats, get_stats};
14+
use crate::stats::counters::{get_stats, InterfaceStats};
1415

1516
#[cfg(target_os = "android")]
1617
pub fn unix_interfaces() -> Vec<Interface> {
@@ -139,6 +140,7 @@ fn unix_interfaces_inner(
139140
Some(ipv6_addr) => vec![get_ipv6_addr_flags(&name, &ipv6_addr.addr())],
140141
None => Vec::new(),
141142
};
143+
142144
let interface: Interface = Interface {
143145
index: if_index,
144146
name,
@@ -163,6 +165,7 @@ fn unix_interfaces_inner(
163165
oper_state: OperState::from_if_flags(addr_ref.ifa_flags),
164166
transmit_speed: None,
165167
receive_speed: None,
168+
auto_negotiate: None,
166169
stats,
167170
#[cfg(feature = "gateway")]
168171
gateway: None,
@@ -183,6 +186,19 @@ fn unix_interfaces_inner(
183186
if iface.index == 0 {
184187
iface.index = if_nametoindex_or_zero(&iface.name);
185188
}
189+
190+
#[cfg(any(
191+
target_os = "macos",
192+
target_os = "freebsd",
193+
target_os = "openbsd",
194+
target_os = "netbsd"
195+
))]
196+
if let Some(ls) = get_link_speed(&iface.name).ok() {
197+
iface.transmit_speed = ls.bps;
198+
iface.receive_speed = ls.bps;
199+
200+
iface.auto_negotiate = Some(ls.auto_negotiate);
201+
}
186202
}
187203
ifaces
188204
}

0 commit comments

Comments
 (0)