|
|
@ -9,7 +9,8 @@ use core::ffi::c_void; |
|
|
|
use crate::interface::{Interface, MacAddr}; |
|
|
|
use crate::interface::{Interface, MacAddr}; |
|
|
|
use crate::gateway::Gateway; |
|
|
|
use crate::gateway::Gateway; |
|
|
|
|
|
|
|
|
|
|
|
pub const EXCEPTION_INTERFACE_INDEX: u32 = 0; |
|
|
|
const EXCEPTION_INTERFACE_INDEX: u32 = 0; |
|
|
|
|
|
|
|
const EXCEPTION_INTERFACE_COMBOINDEX: u32 = 4928; |
|
|
|
|
|
|
|
|
|
|
|
// Convert C string to Rust string without trailing null bytes
|
|
|
|
// Convert C string to Rust string without trailing null bytes
|
|
|
|
fn bytes_to_string(bytes: &[u8]) -> String { |
|
|
|
fn bytes_to_string(bytes: &[u8]) -> String { |
|
|
@ -76,10 +77,9 @@ pub fn get_interfaces() -> Vec<Interface> { |
|
|
|
//Enumerate all adapters
|
|
|
|
//Enumerate all adapters
|
|
|
|
p_adaptor = unsafe { mem::transmute(&raw_adaptor_mem) }; |
|
|
|
p_adaptor = unsafe { mem::transmute(&raw_adaptor_mem) }; |
|
|
|
while p_adaptor as u64 != 0 { |
|
|
|
while p_adaptor as u64 != 0 { |
|
|
|
unsafe { |
|
|
|
let adapter: IP_ADAPTER_INFO = unsafe { *p_adaptor }; |
|
|
|
let adapter: IP_ADAPTER_INFO = *p_adaptor; |
|
|
|
if adapter.Index == EXCEPTION_INTERFACE_INDEX || adapter.ComboIndex == EXCEPTION_INTERFACE_COMBOINDEX{ |
|
|
|
if adapter.Index == EXCEPTION_INTERFACE_INDEX{ |
|
|
|
unsafe { p_adaptor = (*p_adaptor).Next; } |
|
|
|
p_adaptor = (*p_adaptor).Next; |
|
|
|
|
|
|
|
continue; |
|
|
|
continue; |
|
|
|
} |
|
|
|
} |
|
|
|
let adapter_name: String = bytes_to_string(&adapter.AdapterName); |
|
|
|
let adapter_name: String = bytes_to_string(&adapter.AdapterName); |
|
|
@ -89,9 +89,9 @@ pub fn get_interfaces() -> Vec<Interface> { |
|
|
|
let mut ipv4_vec: Vec<Ipv4Addr> = vec![]; |
|
|
|
let mut ipv4_vec: Vec<Ipv4Addr> = vec![]; |
|
|
|
let mut ipv6_vec: Vec<Ipv6Addr> = vec![]; |
|
|
|
let mut ipv6_vec: Vec<Ipv6Addr> = vec![]; |
|
|
|
let mut p_ip_addr: *mut IP_ADDR_STRING; |
|
|
|
let mut p_ip_addr: *mut IP_ADDR_STRING; |
|
|
|
p_ip_addr = mem::transmute(&(*p_adaptor).IpAddressList); |
|
|
|
p_ip_addr = unsafe { mem::transmute(&(*p_adaptor).IpAddressList) }; |
|
|
|
while p_ip_addr as u64 != 0 { |
|
|
|
while p_ip_addr as u64 != 0 { |
|
|
|
let ip_addr_string: IP_ADDR_STRING = *p_ip_addr; |
|
|
|
let ip_addr_string: IP_ADDR_STRING = unsafe{ *p_ip_addr }; |
|
|
|
let ip_addr: String = bytes_to_string(&ip_addr_string.IpAddress.String); |
|
|
|
let ip_addr: String = bytes_to_string(&ip_addr_string.IpAddress.String); |
|
|
|
match ip_addr.parse::<IpAddr>() { |
|
|
|
match ip_addr.parse::<IpAddr>() { |
|
|
|
Ok(ip_addr) => { |
|
|
|
Ok(ip_addr) => { |
|
|
@ -106,14 +106,14 @@ pub fn get_interfaces() -> Vec<Interface> { |
|
|
|
}, |
|
|
|
}, |
|
|
|
Err(_) => {}, |
|
|
|
Err(_) => {}, |
|
|
|
} |
|
|
|
} |
|
|
|
p_ip_addr = (*p_ip_addr).Next; |
|
|
|
unsafe { p_ip_addr = (*p_ip_addr).Next; } |
|
|
|
} |
|
|
|
} |
|
|
|
//Enumerate all gateways
|
|
|
|
//Enumerate all gateways
|
|
|
|
let mut gateway_ips: Vec<IpAddr> = vec![]; |
|
|
|
let mut gateway_ips: Vec<IpAddr> = vec![]; |
|
|
|
let mut p_gateway_addr: *mut IP_ADDR_STRING; |
|
|
|
let mut p_gateway_addr: *mut IP_ADDR_STRING; |
|
|
|
p_gateway_addr = mem::transmute(&(*p_adaptor).GatewayList); |
|
|
|
p_gateway_addr = unsafe { mem::transmute(&(*p_adaptor).GatewayList) }; |
|
|
|
while p_gateway_addr as u64 != 0 { |
|
|
|
while p_gateway_addr as u64 != 0 { |
|
|
|
let gateway_addr_string: IP_ADDR_STRING = *p_gateway_addr; |
|
|
|
let gateway_addr_string: IP_ADDR_STRING = unsafe { *p_gateway_addr }; |
|
|
|
let gateway_addr: String = bytes_to_string(&gateway_addr_string.IpAddress.String); |
|
|
|
let gateway_addr: String = bytes_to_string(&gateway_addr_string.IpAddress.String); |
|
|
|
match gateway_addr.parse::<IpAddr>() { |
|
|
|
match gateway_addr.parse::<IpAddr>() { |
|
|
|
Ok(ip_addr) => { |
|
|
|
Ok(ip_addr) => { |
|
|
@ -121,7 +121,7 @@ pub fn get_interfaces() -> Vec<Interface> { |
|
|
|
}, |
|
|
|
}, |
|
|
|
Err(_) => {}, |
|
|
|
Err(_) => {}, |
|
|
|
} |
|
|
|
} |
|
|
|
p_gateway_addr = (*p_gateway_addr).Next; |
|
|
|
unsafe { p_gateway_addr = (*p_gateway_addr).Next; } |
|
|
|
} |
|
|
|
} |
|
|
|
let default_gateway: Option<Gateway> = match gateway_ips.get(0) { |
|
|
|
let default_gateway: Option<Gateway> = match gateway_ips.get(0) { |
|
|
|
Some(gateway_ip) => { |
|
|
|
Some(gateway_ip) => { |
|
|
@ -161,7 +161,6 @@ pub fn get_interfaces() -> Vec<Interface> { |
|
|
|
gateway: default_gateway, |
|
|
|
gateway: default_gateway, |
|
|
|
}; |
|
|
|
}; |
|
|
|
interfaces.push(interface); |
|
|
|
interfaces.push(interface); |
|
|
|
} |
|
|
|
|
|
|
|
unsafe { p_adaptor = (*p_adaptor).Next; } |
|
|
|
unsafe { p_adaptor = (*p_adaptor).Next; } |
|
|
|
} |
|
|
|
} |
|
|
|
return interfaces; |
|
|
|
return interfaces; |
|
|
|