|
|
|
@ -1,16 +1,16 @@ |
|
|
|
|
use super::Interface; |
|
|
|
|
use super::MacAddr; |
|
|
|
|
use crate::sys; |
|
|
|
|
use crate::gateway; |
|
|
|
|
use crate::ip::{Ipv4Net, Ipv6Net}; |
|
|
|
|
use crate::sys; |
|
|
|
|
|
|
|
|
|
use crate::interface::InterfaceType; |
|
|
|
|
use libc; |
|
|
|
|
use std::ffi::{CStr, CString}; |
|
|
|
|
use std::mem::{self, MaybeUninit}; |
|
|
|
|
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; |
|
|
|
|
use std::os::raw::c_char; |
|
|
|
|
use std::str::from_utf8_unchecked; |
|
|
|
|
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; |
|
|
|
|
use crate::interface::InterfaceType; |
|
|
|
|
|
|
|
|
|
#[cfg(any(target_os = "openbsd", target_os = "freebsd", target_os = "netbsd"))] |
|
|
|
|
pub fn interfaces() -> Vec<Interface> { |
|
|
|
@ -26,21 +26,21 @@ pub fn interfaces() -> Vec<Interface> { |
|
|
|
|
match gateway::unix::get_default_gateway(iface.name.clone()) { |
|
|
|
|
Ok(gateway) => { |
|
|
|
|
iface.gateway = Some(gateway); |
|
|
|
|
}, |
|
|
|
|
Err(_) => {}, |
|
|
|
|
} |
|
|
|
|
Err(_) => {} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
IpAddr::V6(local_ipv6) => { |
|
|
|
|
if iface.ipv6.iter().any(|x| x.addr == local_ipv6) { |
|
|
|
|
match gateway::unix::get_default_gateway(iface.name.clone()) { |
|
|
|
|
Ok(gateway) => { |
|
|
|
|
iface.gateway = Some(gateway); |
|
|
|
|
}, |
|
|
|
|
Err(_) => {}, |
|
|
|
|
} |
|
|
|
|
Err(_) => {} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
interfaces |
|
|
|
@ -64,21 +64,21 @@ pub fn interfaces() -> Vec<Interface> { |
|
|
|
|
match gateway::unix::get_default_gateway(iface.name.clone()) { |
|
|
|
|
Ok(gateway) => { |
|
|
|
|
iface.gateway = Some(gateway); |
|
|
|
|
}, |
|
|
|
|
Err(_) => {}, |
|
|
|
|
} |
|
|
|
|
Err(_) => {} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
IpAddr::V6(local_ipv6) => { |
|
|
|
|
if iface.ipv6.iter().any(|x| x.addr == local_ipv6) { |
|
|
|
|
match gateway::unix::get_default_gateway(iface.name.clone()) { |
|
|
|
|
Ok(gateway) => { |
|
|
|
|
iface.gateway = Some(gateway); |
|
|
|
|
}, |
|
|
|
|
Err(_) => {}, |
|
|
|
|
} |
|
|
|
|
Err(_) => {} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
interfaces |
|
|
|
@ -104,21 +104,21 @@ pub fn interfaces() -> Vec<Interface> { |
|
|
|
|
match gateway::linux::get_default_gateway(iface.name.clone()) { |
|
|
|
|
Ok(gateway) => { |
|
|
|
|
iface.gateway = Some(gateway); |
|
|
|
|
}, |
|
|
|
|
Err(_) => {}, |
|
|
|
|
} |
|
|
|
|
Err(_) => {} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
IpAddr::V6(local_ipv6) => { |
|
|
|
|
if iface.ipv6.iter().any(|x| x.addr == local_ipv6) { |
|
|
|
|
match gateway::linux::get_default_gateway(iface.name.clone()) { |
|
|
|
|
Ok(gateway) => { |
|
|
|
|
iface.gateway = Some(gateway); |
|
|
|
|
}, |
|
|
|
|
Err(_) => {}, |
|
|
|
|
} |
|
|
|
|
Err(_) => {} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
interfaces |
|
|
|
@ -144,10 +144,8 @@ fn sockaddr_to_network_addr(sa: *const libc::sockaddr) -> (Option<MacAddr>, Opti |
|
|
|
|
|
|
|
|
|
(Some(mac), None) |
|
|
|
|
} else { |
|
|
|
|
let addr = sys::sockaddr_to_addr( |
|
|
|
|
mem::transmute(sa), |
|
|
|
|
mem::size_of::<libc::sockaddr_storage>(), |
|
|
|
|
); |
|
|
|
|
let addr = |
|
|
|
|
sys::sockaddr_to_addr(mem::transmute(sa), mem::size_of::<libc::sockaddr_storage>()); |
|
|
|
|
|
|
|
|
|
match addr { |
|
|
|
|
Ok(SocketAddr::V4(sa)) => (None, Some(IpAddr::V4(*sa.ip()))), |
|
|
|
@ -158,7 +156,13 @@ fn sockaddr_to_network_addr(sa: *const libc::sockaddr) -> (Option<MacAddr>, Opti |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[cfg(any(target_os = "openbsd", target_os = "freebsd", target_os = "netbsd", target_os = "macos", target_os = "ios"))] |
|
|
|
|
#[cfg(any(
|
|
|
|
|
target_os = "openbsd", |
|
|
|
|
target_os = "freebsd", |
|
|
|
|
target_os = "netbsd", |
|
|
|
|
target_os = "macos", |
|
|
|
|
target_os = "ios" |
|
|
|
|
))] |
|
|
|
|
fn sockaddr_to_network_addr(sa: *const libc::sockaddr) -> (Option<MacAddr>, Option<IpAddr>) { |
|
|
|
|
use crate::bpf; |
|
|
|
|
use std::net::SocketAddr; |
|
|
|
@ -180,10 +184,8 @@ fn sockaddr_to_network_addr(sa: *const libc::sockaddr) -> (Option<MacAddr>, Opti |
|
|
|
|
|
|
|
|
|
(Some(mac), None) |
|
|
|
|
} else { |
|
|
|
|
let addr = sys::sockaddr_to_addr( |
|
|
|
|
mem::transmute(sa), |
|
|
|
|
mem::size_of::<libc::sockaddr_storage>(), |
|
|
|
|
); |
|
|
|
|
let addr = |
|
|
|
|
sys::sockaddr_to_addr(mem::transmute(sa), mem::size_of::<libc::sockaddr_storage>()); |
|
|
|
|
|
|
|
|
|
match addr { |
|
|
|
|
Ok(SocketAddr::V4(sa)) => (None, Some(IpAddr::V4(*sa.ip()))), |
|
|
|
@ -215,30 +217,26 @@ pub fn unix_interfaces() -> Vec<Interface> { |
|
|
|
|
match ip { |
|
|
|
|
IpAddr::V4(ipv4) => { |
|
|
|
|
let netmask: Ipv4Addr = match netmask { |
|
|
|
|
Some(netmask) => { |
|
|
|
|
match netmask { |
|
|
|
|
Some(netmask) => match netmask { |
|
|
|
|
IpAddr::V4(netmask) => netmask, |
|
|
|
|
IpAddr::V6(_) => Ipv4Addr::UNSPECIFIED, |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
None => Ipv4Addr::UNSPECIFIED, |
|
|
|
|
}; |
|
|
|
|
let ipv4_net: Ipv4Net = Ipv4Net::new_with_netmask(ipv4, netmask); |
|
|
|
|
ini_ipv4.push(ipv4_net); |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
IpAddr::V6(ipv6) => { |
|
|
|
|
let netmask: Ipv6Addr = match netmask { |
|
|
|
|
Some(netmask) => { |
|
|
|
|
match netmask { |
|
|
|
|
Some(netmask) => match netmask { |
|
|
|
|
IpAddr::V4(_) => Ipv6Addr::UNSPECIFIED, |
|
|
|
|
IpAddr::V6(netmask) => netmask, |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
None => Ipv6Addr::UNSPECIFIED, |
|
|
|
|
}; |
|
|
|
|
let ipv6_net: Ipv6Net = Ipv6Net::new_with_netmask(ipv6, netmask); |
|
|
|
|
ini_ipv6.push(ipv6_net); |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
let interface: Interface = Interface { |
|
|
|
@ -265,30 +263,26 @@ pub fn unix_interfaces() -> Vec<Interface> { |
|
|
|
|
match ip { |
|
|
|
|
IpAddr::V4(ipv4) => { |
|
|
|
|
let netmask: Ipv4Addr = match netmask { |
|
|
|
|
Some(netmask) => { |
|
|
|
|
match netmask { |
|
|
|
|
Some(netmask) => match netmask { |
|
|
|
|
IpAddr::V4(netmask) => netmask, |
|
|
|
|
IpAddr::V6(_) => Ipv4Addr::UNSPECIFIED, |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
None => Ipv4Addr::UNSPECIFIED, |
|
|
|
|
}; |
|
|
|
|
let ipv4_net: Ipv4Net = Ipv4Net::new_with_netmask(ipv4, netmask); |
|
|
|
|
iface.ipv4.push(ipv4_net); |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
IpAddr::V6(ipv6) => { |
|
|
|
|
let netmask: Ipv6Addr = match netmask { |
|
|
|
|
Some(netmask) => { |
|
|
|
|
match netmask { |
|
|
|
|
Some(netmask) => match netmask { |
|
|
|
|
IpAddr::V4(_) => Ipv6Addr::UNSPECIFIED, |
|
|
|
|
IpAddr::V6(netmask) => netmask, |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
None => Ipv6Addr::UNSPECIFIED, |
|
|
|
|
}; |
|
|
|
|
let ipv6_net: Ipv6Net = Ipv6Net::new_with_netmask(ipv6, netmask); |
|
|
|
|
iface.ipv6.push(ipv6_net); |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
found = true; |
|
|
|
@ -299,10 +293,14 @@ pub fn unix_interfaces() -> Vec<Interface> { |
|
|
|
|
} |
|
|
|
|
addr = addr_ref.ifa_next; |
|
|
|
|
} |
|
|
|
|
unsafe{ libc::freeifaddrs(addrs); }
|
|
|
|
|
unsafe { |
|
|
|
|
libc::freeifaddrs(addrs); |
|
|
|
|
} |
|
|
|
|
for iface in &mut ifaces { |
|
|
|
|
let name = CString::new(iface.name.as_bytes()).unwrap(); |
|
|
|
|
unsafe { iface.index = libc::if_nametoindex(name.as_ptr()); } |
|
|
|
|
unsafe { |
|
|
|
|
iface.index = libc::if_nametoindex(name.as_ptr()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
ifaces |
|
|
|
|
} |
|
|
|
|