From 7fe034af2da818328cec3512b762ed25d9dc820d Mon Sep 17 00:00:00 2001 From: shellrow <81893184+shellrow@users.noreply.github.com> Date: Sun, 9 Jan 2022 23:30:40 +0900 Subject: [PATCH] Windows support --- src/os/unix.rs | 5 ++-- src/os/windows.rs | 58 +++++++++++++++++++++++++++++++++++------------ 2 files changed, 46 insertions(+), 17 deletions(-) diff --git a/src/os/unix.rs b/src/os/unix.rs index b30dfea..12417ef 100644 --- a/src/os/unix.rs +++ b/src/os/unix.rs @@ -153,6 +153,7 @@ fn icmpv6_handler(ip_packet: &pnet_packet::ipv6::Ipv6Packet) -> Option { } } +// Get network interfaces pub fn interfaces() -> Vec { let mut result: Vec = vec![]; let local_ip: IpAddr = match super::get_local_ipaddr(){ @@ -206,7 +207,7 @@ pub fn interfaces() -> Vec { return result; } -/// Get default Interface index +// Get default Interface index pub fn default_interface_index() -> Option { let local_ip: IpAddr = match super::get_local_ipaddr(){ Some(local_ip) => local_ip, @@ -223,7 +224,7 @@ pub fn default_interface_index() -> Option { return None; } -/// Get default Interface name +// Get default Interface name pub fn default_interface_name() -> Option { let local_ip: IpAddr = match super::get_local_ipaddr(){ Some(local_ip) => local_ip, diff --git a/src/os/windows.rs b/src/os/windows.rs index 5e44ad5..df93119 100644 --- a/src/os/windows.rs +++ b/src/os/windows.rs @@ -166,22 +166,50 @@ pub fn interfaces() -> Vec { return interfaces; } -#[cfg(test)] -mod tests { - use std::net::Ipv4Addr; - use super::*; - #[test] - fn test_nw_interfaces() { - let interfaces = get_interfaces(); - for interface in interfaces { - println!("{:?}", interface); +// Get default Interface index +pub fn default_interface_index() -> Option { + let local_ip: IpAddr = match super::get_local_ipaddr(){ + Some(local_ip) => local_ip, + None => return None, + }; + let interfaces = interfaces(); + for iface in interfaces { + match local_ip { + IpAddr::V4(local_ipv4) => { + if iface.ipv4.contains(&local_ipv4) { + return Some(iface.index); + } + }, + IpAddr::V6(local_ipv6) => { + if iface.ipv6.contains(&local_ipv6) { + return Some(iface.index); + } + }, } } - #[test] - fn test_arp() { - let src_ip: Ipv4Addr = Ipv4Addr::new(192, 168, 1, 2); - let dst_ip: Ipv4Addr = Ipv4Addr::new(192, 168, 1, 1); - let mac_addr = get_mac_through_arp(src_ip, dst_ip); - println!("{}", mac_addr); + None +} + +// Get default Interface name +pub fn default_interface_name() -> Option { + let local_ip: IpAddr = match super::get_local_ipaddr(){ + Some(local_ip) => local_ip, + None => return None, + }; + let interfaces = interfaces(); + for iface in interfaces { + match local_ip { + IpAddr::V4(local_ipv4) => { + if iface.ipv4.contains(&local_ipv4) { + return Some(iface.name); + } + }, + IpAddr::V6(local_ipv6) => { + if iface.ipv6.contains(&local_ipv6) { + return Some(iface.name); + } + }, + } } + None }