From c57b24a26db85dbb3fb6689bd1c77fbae5e484db Mon Sep 17 00:00:00 2001 From: shellrow <81893184+shellrow@users.noreply.github.com> Date: Sun, 27 Mar 2022 16:54:36 +0900 Subject: [PATCH] Add if_type and flags for linux --- src/interface/types.rs | 13 +++++++++++-- src/interface/unix.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/interface/types.rs b/src/interface/types.rs index 368448c..2dd81f1 100644 --- a/src/interface/types.rs +++ b/src/interface/types.rs @@ -69,9 +69,18 @@ impl InterfaceType { #[cfg(any(target_os = "linux", target_os = "android"))] pub fn value(&self) -> u32 { - // TODO match *self { - _ => 0, + 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, + _ => 999, } } diff --git a/src/interface/unix.rs b/src/interface/unix.rs index a646664..085ab4a 100644 --- a/src/interface/unix.rs +++ b/src/interface/unix.rs @@ -10,6 +10,9 @@ use std::mem::{self, MaybeUninit}; use std::os::raw::c_char; use std::str::from_utf8_unchecked; use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; +use std::convert::TryFrom; +use std::fs::read_to_string; +use crate::interface::InterfaceType; #[cfg(any(target_os = "macos", target_os = "openbsd", target_os = "freebsd", target_os = "netbsd", target_os = "ios"))] pub fn interfaces() -> Vec { @@ -79,6 +82,30 @@ pub fn interfaces() -> Vec { interfaces } +#[cfg(any(target_os = "macos", target_os = "openbsd", target_os = "freebsd", target_os = "netbsd", target_os = "ios"))] +pub fn get_interface_type(if_name: String) -> InterfaceType { + // TODO + InterfaceType::Unknown +} + +#[cfg(any(target_os = "linux", target_os = "android"))] +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); + let if_type_string = match r { + Ok(content) => content.trim().to_string(), + Err(_) => String::from("999"), + }; + match if_type_string.parse::() { + Ok(if_type) => { + InterfaceType::try_from(if_type).unwrap_or(InterfaceType::Unknown) + }, + Err(_) => { + InterfaceType::Unknown + } + } +} + pub fn unix_interfaces() -> Vec { let mut ifaces: Vec = vec![]; let mut addrs: MaybeUninit<*mut libc::ifaddrs> = MaybeUninit::uninit(); @@ -130,9 +157,11 @@ pub fn unix_interfaces() -> Vec { index: 0, name: name.clone(), description: None, + if_type: get_interface_type(name.clone()), mac_addr: mac.clone(), ipv4: ini_ipv4, ipv6: ini_ipv6, + flags: addr_ref.ifa_flags, gateway: None, }; let mut found: bool = false;