diff --git a/src/interface/unix.rs b/src/interface/unix.rs index 22addf0..6a1a458 100644 --- a/src/interface/unix.rs +++ b/src/interface/unix.rs @@ -52,37 +52,42 @@ pub fn interfaces() -> Vec { let type_map = macos::get_if_type_map(); let mut interfaces: Vec = unix_interfaces(); + #[cfg(feature = "gateway")] - let local_ip: IpAddr = match super::get_local_ipaddr() { - Some(local_ip) => local_ip, - None => return interfaces, - }; + let local_ip_opt: Option = super::get_local_ipaddr(); + #[cfg(feature = "gateway")] let gateway_map = gateway::macos::get_gateway_map(); + for iface in &mut interfaces { if let Some(sc_interface) = type_map.get(&iface.name) { iface.if_type = sc_interface.interface_type; iface.friendly_name = sc_interface.friendly_name.clone(); } + #[cfg(feature = "gateway")] { if let Some(gateway) = gateway_map.get(&iface.index) { iface.gateway = Some(gateway.clone()); } - iface.ipv4.iter().for_each(|ipv4| { - if IpAddr::V4(ipv4.addr()) == local_ip { - iface.dns_servers = get_system_dns_conf(); - iface.default = true; - } - }); - iface.ipv6.iter().for_each(|ipv6| { - if IpAddr::V6(ipv6.addr()) == local_ip { - iface.dns_servers = get_system_dns_conf(); - iface.default = true; - } - }); + + if let Some(local_ip) = local_ip_opt { + iface.ipv4.iter().for_each(|ipv4| { + if IpAddr::V4(ipv4.addr()) == local_ip { + iface.dns_servers = get_system_dns_conf(); + iface.default = true; + } + }); + iface.ipv6.iter().for_each(|ipv6| { + if IpAddr::V6(ipv6.addr()) == local_ip { + iface.dns_servers = get_system_dns_conf(); + iface.default = true; + } + }); + } } } + interfaces } @@ -139,32 +144,36 @@ pub fn interfaces() -> Vec { #[cfg(any(target_os = "openbsd", target_os = "freebsd", target_os = "netbsd"))] pub fn interfaces() -> Vec { let mut interfaces: Vec = unix_interfaces(); + #[cfg(feature = "gateway")] - let local_ip: IpAddr = match super::get_local_ipaddr() { - Some(local_ip) => local_ip, - None => return interfaces, - }; + let local_ip_opt: Option = super::get_local_ipaddr(); + #[cfg(feature = "gateway")] { let gateway_map = gateway::bsd::get_gateway_map(); + for iface in &mut interfaces { if let Some(gateway) = gateway_map.get(&iface.index) { iface.gateway = Some(gateway.clone()); } - iface.ipv4.iter().for_each(|ipv4| { - if IpAddr::V4(ipv4.addr()) == local_ip { - iface.dns_servers = get_system_dns_conf(); - iface.default = true; - } - }); - iface.ipv6.iter().for_each(|ipv6| { - if IpAddr::V6(ipv6.addr()) == local_ip { - iface.dns_servers = get_system_dns_conf(); - iface.default = true; - } - }); + + if let Some(local_ip) = local_ip_opt { + iface.ipv4.iter().for_each(|ipv4| { + if IpAddr::V4(ipv4.addr()) == local_ip { + iface.dns_servers = get_system_dns_conf(); + iface.default = true; + } + }); + iface.ipv6.iter().for_each(|ipv6| { + if IpAddr::V6(ipv6.addr()) == local_ip { + iface.dns_servers = get_system_dns_conf(); + iface.default = true; + } + }); + } } } + interfaces }