diff --git a/src/interface/linux.rs b/src/interface/linux.rs index 1108853..9073977 100644 --- a/src/interface/linux.rs +++ b/src/interface/linux.rs @@ -5,6 +5,12 @@ use std::net::IpAddr; const PATH_RESOLV_CONF: &str = "/etc/resolv.conf"; +fn is_wifi_interface(interface_name: &str) -> bool { + let wireless_path = format!("/sys/class/net/{}/wireless", interface_name); + let phy80211_path = format!("/sys/class/net/{}/phy80211", interface_name); + std::path::Path::new(&wireless_path).exists() || std::path::Path::new(&phy80211_path).exists() +} + pub fn get_interface_type(if_name: String) -> InterfaceType { let if_type_path: String = format!("/sys/class/net/{}/type", if_name); let r = read_to_string(if_type_path); @@ -13,7 +19,17 @@ pub fn get_interface_type(if_name: String) -> InterfaceType { let if_type_string = content.trim().to_string(); match if_type_string.parse::() { Ok(if_type) => { - return InterfaceType::try_from(if_type).unwrap_or(InterfaceType::Unknown); + if if_type == crate::sys::if_arp::ARPHRD_ETHER { + // Since some Wi-Fi interfaces may also be reported as Ethernet, + // further check if the interface is actually Wi-Fi. + if is_wifi_interface(&if_name) { + return InterfaceType::Wireless80211; + } else { + return InterfaceType::Ethernet; + } + } else { + return InterfaceType::try_from(if_type).unwrap_or(InterfaceType::Unknown); + } } Err(_) => { return InterfaceType::Unknown; diff --git a/src/interface/types.rs b/src/interface/types.rs index 937daf2..15f885e 100644 --- a/src/interface/types.rs +++ b/src/interface/types.rs @@ -106,17 +106,20 @@ impl InterfaceType { /// Returns OS-specific value of InterfaceType #[cfg(any(target_os = "linux", target_os = "android"))] pub fn value(&self) -> u32 { + use crate::sys; match *self { - InterfaceType::Ethernet => 1, - InterfaceType::TokenRing => 4, - InterfaceType::Fddi => 774, - InterfaceType::Ppp => 512, - InterfaceType::Loopback => 772, - InterfaceType::Ethernet3Megabit => 2, - InterfaceType::Slip => 256, - InterfaceType::Atm => 19, - InterfaceType::Wireless80211 => 801, - InterfaceType::Tunnel => 768, + InterfaceType::Ethernet => sys::if_arp::ARPHRD_ETHER, + InterfaceType::TokenRing => sys::if_arp::ARPHRD_IEEE802, + InterfaceType::Fddi => sys::if_arp::ARPHRD_FDDI, + InterfaceType::Ppp => sys::if_arp::ARPHRD_PPP, + InterfaceType::Loopback => sys::if_arp::ARPHRD_LOOPBACK, + InterfaceType::Ethernet3Megabit => sys::if_arp::ARPHRD_EETHER, + InterfaceType::Slip => sys::if_arp::ARPHRD_SLIP, + InterfaceType::Atm => sys::if_arp::ARPHRD_ATM, + InterfaceType::Wireless80211 => sys::if_arp::ARPHRD_IEEE80211, + InterfaceType::Tunnel => sys::if_arp::ARPHRD_TUNNEL, + InterfaceType::Isdn => sys::if_arp::ARPHRD_X25, + InterfaceType::HighPerformanceSerialBus => sys::if_arp::ARPHRD_IEEE1394, _ => u32::MAX, } } diff --git a/src/sys/linux.rs b/src/sys/linux.rs new file mode 100644 index 0000000..3252bcd --- /dev/null +++ b/src/sys/linux.rs @@ -0,0 +1,15 @@ +// ARP protocol HARDWARE identifiers +pub mod if_arp { + pub const ARPHRD_ETHER: u32 = libc::ARPHRD_ETHER as u32; + pub const ARPHRD_IEEE802: u32 = libc::ARPHRD_IEEE802 as u32; + pub const ARPHRD_FDDI: u32 = libc::ARPHRD_FDDI as u32; + pub const ARPHRD_PPP: u32 = libc::ARPHRD_PPP as u32; + pub const ARPHRD_LOOPBACK: u32 = libc::ARPHRD_LOOPBACK as u32; + pub const ARPHRD_EETHER: u32 = libc::ARPHRD_EETHER as u32; + pub const ARPHRD_SLIP: u32 = libc::ARPHRD_SLIP as u32; + pub const ARPHRD_ATM: u32 = libc::ARPHRD_ATM as u32; + pub const ARPHRD_IEEE80211: u32 = libc::ARPHRD_IEEE80211 as u32; + pub const ARPHRD_TUNNEL: u32 = libc::ARPHRD_TUNNEL as u32; + pub const ARPHRD_X25: u32 = libc::ARPHRD_X25 as u32; + pub const ARPHRD_IEEE1394: u32 = libc::ARPHRD_IEEE1394 as u32; +} diff --git a/src/sys/mod.rs b/src/sys/mod.rs index 08a0df4..b5c87ee 100644 --- a/src/sys/mod.rs +++ b/src/sys/mod.rs @@ -7,3 +7,8 @@ pub use self::unix::*; mod windows; #[cfg(target_os = "windows")] pub use self::windows::*; + +#[cfg(any(target_os = "linux", target_os = "android"))] +mod linux; +#[cfg(any(target_os = "linux", target_os = "android"))] +pub use self::linux::*;