|
|
|
@ -9,7 +9,8 @@ use core::ffi::c_void; |
|
|
|
|
use crate::interface::{Interface, MacAddr}; |
|
|
|
|
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
|
|
|
|
|
fn bytes_to_string(bytes: &[u8]) -> String { |
|
|
|
@ -76,92 +77,90 @@ pub fn get_interfaces() -> Vec<Interface> { |
|
|
|
|
//Enumerate all adapters
|
|
|
|
|
p_adaptor = unsafe { mem::transmute(&raw_adaptor_mem) }; |
|
|
|
|
while p_adaptor as u64 != 0 { |
|
|
|
|
unsafe { |
|
|
|
|
let adapter: IP_ADAPTER_INFO = *p_adaptor; |
|
|
|
|
if adapter.Index == EXCEPTION_INTERFACE_INDEX{ |
|
|
|
|
p_adaptor = (*p_adaptor).Next; |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
let adapter_name: String = bytes_to_string(&adapter.AdapterName); |
|
|
|
|
let adapter_desc: String = bytes_to_string(&adapter.Description); |
|
|
|
|
let mac_addr:[u8; 6] = adapter.Address[..6].try_into().unwrap_or([0, 0, 0, 0, 0, 0]); |
|
|
|
|
//Enumerate all IPs
|
|
|
|
|
let mut ipv4_vec: Vec<Ipv4Addr> = vec![]; |
|
|
|
|
let mut ipv6_vec: Vec<Ipv6Addr> = vec![]; |
|
|
|
|
let mut p_ip_addr: *mut IP_ADDR_STRING; |
|
|
|
|
p_ip_addr = mem::transmute(&(*p_adaptor).IpAddressList); |
|
|
|
|
while p_ip_addr as u64 != 0 { |
|
|
|
|
let ip_addr_string: IP_ADDR_STRING = *p_ip_addr; |
|
|
|
|
let ip_addr: String = bytes_to_string(&ip_addr_string.IpAddress.String); |
|
|
|
|
match ip_addr.parse::<IpAddr>() { |
|
|
|
|
Ok(ip_addr) => { |
|
|
|
|
match ip_addr { |
|
|
|
|
IpAddr::V4(ipv4_addr) => { |
|
|
|
|
ipv4_vec.push(ipv4_addr); |
|
|
|
|
}, |
|
|
|
|
IpAddr::V6(ipv6_addr) => { |
|
|
|
|
ipv6_vec.push(ipv6_addr); |
|
|
|
|
} |
|
|
|
|
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; |
|
|
|
|
} |
|
|
|
|
let adapter_name: String = bytes_to_string(&adapter.AdapterName); |
|
|
|
|
let adapter_desc: String = bytes_to_string(&adapter.Description); |
|
|
|
|
let mac_addr:[u8; 6] = adapter.Address[..6].try_into().unwrap_or([0, 0, 0, 0, 0, 0]); |
|
|
|
|
//Enumerate all IPs
|
|
|
|
|
let mut ipv4_vec: Vec<Ipv4Addr> = vec![]; |
|
|
|
|
let mut ipv6_vec: Vec<Ipv6Addr> = vec![]; |
|
|
|
|
let mut p_ip_addr: *mut IP_ADDR_STRING; |
|
|
|
|
p_ip_addr = unsafe { mem::transmute(&(*p_adaptor).IpAddressList) }; |
|
|
|
|
while p_ip_addr as u64 != 0 { |
|
|
|
|
let ip_addr_string: IP_ADDR_STRING = unsafe{ *p_ip_addr }; |
|
|
|
|
let ip_addr: String = bytes_to_string(&ip_addr_string.IpAddress.String); |
|
|
|
|
match ip_addr.parse::<IpAddr>() { |
|
|
|
|
Ok(ip_addr) => { |
|
|
|
|
match ip_addr { |
|
|
|
|
IpAddr::V4(ipv4_addr) => { |
|
|
|
|
ipv4_vec.push(ipv4_addr); |
|
|
|
|
}, |
|
|
|
|
IpAddr::V6(ipv6_addr) => { |
|
|
|
|
ipv6_vec.push(ipv6_addr); |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
Err(_) => {}, |
|
|
|
|
} |
|
|
|
|
p_ip_addr = (*p_ip_addr).Next; |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
Err(_) => {}, |
|
|
|
|
} |
|
|
|
|
//Enumerate all gateways
|
|
|
|
|
let mut gateway_ips: Vec<IpAddr> = vec![]; |
|
|
|
|
let mut p_gateway_addr: *mut IP_ADDR_STRING; |
|
|
|
|
p_gateway_addr = mem::transmute(&(*p_adaptor).GatewayList); |
|
|
|
|
while p_gateway_addr as u64 != 0 { |
|
|
|
|
let gateway_addr_string: IP_ADDR_STRING = *p_gateway_addr; |
|
|
|
|
let gateway_addr: String = bytes_to_string(&gateway_addr_string.IpAddress.String); |
|
|
|
|
match gateway_addr.parse::<IpAddr>() { |
|
|
|
|
Ok(ip_addr) => { |
|
|
|
|
gateway_ips.push(ip_addr); |
|
|
|
|
}, |
|
|
|
|
Err(_) => {}, |
|
|
|
|
} |
|
|
|
|
p_gateway_addr = (*p_gateway_addr).Next; |
|
|
|
|
unsafe { p_ip_addr = (*p_ip_addr).Next; } |
|
|
|
|
} |
|
|
|
|
//Enumerate all gateways
|
|
|
|
|
let mut gateway_ips: Vec<IpAddr> = vec![]; |
|
|
|
|
let mut p_gateway_addr: *mut IP_ADDR_STRING; |
|
|
|
|
p_gateway_addr = unsafe { mem::transmute(&(*p_adaptor).GatewayList) }; |
|
|
|
|
while p_gateway_addr as u64 != 0 { |
|
|
|
|
let gateway_addr_string: IP_ADDR_STRING = unsafe { *p_gateway_addr }; |
|
|
|
|
let gateway_addr: String = bytes_to_string(&gateway_addr_string.IpAddress.String); |
|
|
|
|
match gateway_addr.parse::<IpAddr>() { |
|
|
|
|
Ok(ip_addr) => { |
|
|
|
|
gateway_ips.push(ip_addr); |
|
|
|
|
}, |
|
|
|
|
Err(_) => {}, |
|
|
|
|
} |
|
|
|
|
let default_gateway: Option<Gateway> = match gateway_ips.get(0) { |
|
|
|
|
Some(gateway_ip) => { |
|
|
|
|
let gateway_ip: IpAddr = *gateway_ip; |
|
|
|
|
let default_gateway: Option<Gateway> = if gateway_ip != IpAddr::V4(Ipv4Addr::UNSPECIFIED) { |
|
|
|
|
match gateway_ip { |
|
|
|
|
IpAddr::V4(dst_ip) => { |
|
|
|
|
if let Some(src_ip) = ipv4_vec.get(0) { |
|
|
|
|
let mac_addr = get_mac_through_arp(*src_ip, dst_ip); |
|
|
|
|
let gateway = Gateway { |
|
|
|
|
mac_addr: mac_addr, |
|
|
|
|
ip_addr: IpAddr::V4(dst_ip), |
|
|
|
|
}; |
|
|
|
|
Some(gateway) |
|
|
|
|
}else{ |
|
|
|
|
None |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
IpAddr::V6(_dst_ip) => { |
|
|
|
|
unsafe { p_gateway_addr = (*p_gateway_addr).Next; } |
|
|
|
|
} |
|
|
|
|
let default_gateway: Option<Gateway> = match gateway_ips.get(0) { |
|
|
|
|
Some(gateway_ip) => { |
|
|
|
|
let gateway_ip: IpAddr = *gateway_ip; |
|
|
|
|
let default_gateway: Option<Gateway> = if gateway_ip != IpAddr::V4(Ipv4Addr::UNSPECIFIED) { |
|
|
|
|
match gateway_ip { |
|
|
|
|
IpAddr::V4(dst_ip) => { |
|
|
|
|
if let Some(src_ip) = ipv4_vec.get(0) { |
|
|
|
|
let mac_addr = get_mac_through_arp(*src_ip, dst_ip); |
|
|
|
|
let gateway = Gateway { |
|
|
|
|
mac_addr: mac_addr, |
|
|
|
|
ip_addr: IpAddr::V4(dst_ip), |
|
|
|
|
}; |
|
|
|
|
Some(gateway) |
|
|
|
|
}else{ |
|
|
|
|
None |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
}else{ |
|
|
|
|
None |
|
|
|
|
}; |
|
|
|
|
default_gateway |
|
|
|
|
}, |
|
|
|
|
None => None, |
|
|
|
|
}; |
|
|
|
|
let interface: Interface = Interface{ |
|
|
|
|
index: adapter.Index, |
|
|
|
|
name: adapter_name, |
|
|
|
|
description: Some(adapter_desc), |
|
|
|
|
mac_addr: Some(MacAddr::new(mac_addr)), |
|
|
|
|
ipv4: ipv4_vec, |
|
|
|
|
ipv6: ipv6_vec, |
|
|
|
|
gateway: default_gateway, |
|
|
|
|
}; |
|
|
|
|
interfaces.push(interface); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
IpAddr::V6(_dst_ip) => { |
|
|
|
|
None |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
}else{ |
|
|
|
|
None |
|
|
|
|
}; |
|
|
|
|
default_gateway |
|
|
|
|
}, |
|
|
|
|
None => None, |
|
|
|
|
}; |
|
|
|
|
let interface: Interface = Interface{ |
|
|
|
|
index: adapter.Index, |
|
|
|
|
name: adapter_name, |
|
|
|
|
description: Some(adapter_desc), |
|
|
|
|
mac_addr: Some(MacAddr::new(mac_addr)), |
|
|
|
|
ipv4: ipv4_vec, |
|
|
|
|
ipv6: ipv6_vec, |
|
|
|
|
gateway: default_gateway, |
|
|
|
|
}; |
|
|
|
|
interfaces.push(interface); |
|
|
|
|
unsafe { p_adaptor = (*p_adaptor).Next; } |
|
|
|
|
} |
|
|
|
|
return interfaces; |
|
|
|
|