From 42f8fad9e2a27ef0df8c2985ba615d213c53cf42 Mon Sep 17 00:00:00 2001 From: shellrow <81893184+shellrow@users.noreply.github.com> Date: Sat, 12 Feb 2022 15:20:22 +0900 Subject: [PATCH] Add adapter type filter (#3) --- src/interface/windows.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/interface/windows.rs b/src/interface/windows.rs index ccf0dd0..0ff7ebc 100644 --- a/src/interface/windows.rs +++ b/src/interface/windows.rs @@ -1,5 +1,6 @@ use windows::Win32::Foundation::{ERROR_BUFFER_OVERFLOW, NO_ERROR}; use windows::Win32::NetworkManagement::IpHelper::{GetAdaptersInfo, IP_ADAPTER_INFO, IP_ADDR_STRING, SendARP}; +use windows::Win32::NetworkManagement::IpHelper::{MIB_IF_TYPE_ETHERNET, MIB_IF_TYPE_FDDI, MIB_IF_TYPE_LOOPBACK, MIB_IF_TYPE_OTHER, MIB_IF_TYPE_PPP, MIB_IF_TYPE_SLIP, MIB_IF_TYPE_TOKENRING}; use std::convert::TryInto; use std::mem; use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; @@ -10,9 +11,6 @@ use crate::ip::{Ipv4Net, Ipv6Net}; use crate::interface::{Interface, MacAddr}; use crate::gateway::Gateway; -const EXCEPTION_INTERFACE_INDEX: u32 = 0; -const EXCEPTION_INTERFACE_COMBOINDEX: u32 = 4928; - // Convert C string to Rust string without trailing null bytes fn bytes_to_string(bytes: &[u8]) -> String { let result: String = match CStr::from_bytes_with_nul(bytes) { @@ -79,9 +77,18 @@ pub fn interfaces() -> Vec { p_adaptor = unsafe { mem::transmute(&raw_adaptor_mem) }; while p_adaptor as u64 != 0 { let adapter: IP_ADAPTER_INFO = unsafe { *p_adaptor }; - if adapter.Index == EXCEPTION_INTERFACE_INDEX || adapter.ComboIndex == EXCEPTION_INTERFACE_COMBOINDEX{ - unsafe { p_adaptor = (*p_adaptor).Next; } - continue; + match adapter.Type { + MIB_IF_TYPE_ETHERNET => {}, + MIB_IF_TYPE_FDDI => {}, + MIB_IF_TYPE_LOOPBACK => {}, + MIB_IF_TYPE_OTHER => {}, + MIB_IF_TYPE_PPP => {}, + MIB_IF_TYPE_SLIP => {}, + MIB_IF_TYPE_TOKENRING => {}, + _ => { + unsafe { p_adaptor = (*p_adaptor).Next; } + continue; + }, } let adapter_name: String = bytes_to_string(&adapter.AdapterName); let adapter_desc: String = bytes_to_string(&adapter.Description);