Skip to content

Commit

Permalink
fixup: macos
Browse files Browse the repository at this point in the history
  • Loading branch information
hulthe committed Jan 23, 2025
1 parent fbe3d58 commit 598176e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
29 changes: 16 additions & 13 deletions leak-checker/src/traceroute/unix/macos.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::ascii::escape_default;
use std::future::pending;
use std::io;
use std::net::IpAddr;
use std::num::NonZero;
use std::os::fd::{FromRawFd, IntoRawFd};
use std::{
ascii::escape_default,
future::pending,
io,
net::IpAddr,
num::NonZero,
os::fd::{FromRawFd, IntoRawFd},
};

use anyhow::{anyhow, bail, ensure, Context};
use nix::net::if_::if_nametoindex;
Expand All @@ -22,8 +24,11 @@ use tokio::{
time::{sleep_until, Instant},
};

use crate::traceroute::{Ip, TracerouteOpt, RECV_GRACE_TIME};
use crate::{Interface, LeakInfo, LeakStatus};
use crate::{
traceroute::{TracerouteOpt, RECV_GRACE_TIME},
util::Ip,
Interface, LeakInfo, LeakStatus,
};

use super::{parse_icmp_probe, too_small, AsyncIcmpSocket, Traceroute, PROBE_PAYLOAD};

Expand All @@ -45,7 +50,7 @@ impl Traceroute for TracerouteMacos {
}

impl AsyncIcmpSocket for AsyncIcmpSocketImpl {
fn from_socket2(socket: Socket) -> anyhow::Result<Self> {
fn from_socket2(socket: Socket, _ip_version: Ip) -> anyhow::Result<Self> {
let raw_socket = socket.into_raw_fd();
let std_socket = unsafe { std::net::UdpSocket::from_raw_fd(raw_socket) };
let tokio_socket = tokio::net::UdpSocket::from_std(std_socket).unwrap();
Expand Down Expand Up @@ -82,7 +87,7 @@ fn bind_socket_to_interface(
log::info!("Binding socket to {interface:?}");

let interface_index = match interface {
Interface::Index(&index) => index,
&Interface::Index(index) => index,
Interface::Name(interface) => if_nametoindex(interface.as_str())
.map_err(anyhow::Error::from)
.and_then(|code| NonZero::new(code).ok_or(anyhow!("Non-zero error code")))
Expand Down Expand Up @@ -140,9 +145,7 @@ async fn recv_ttl_responses(
Ip::V4(ip_packet) => parse_icmp4_time_exceeded(&ip_packet),
Ip::V6(ip_packet) => parse_icmp6_time_exceeded(&ip_packet),
})
.map_err(|e| {
anyhow!("Ignoring packet (len={n}, ip.src={source}): {e}")
});
.map_err(|e| anyhow!("Ignoring packet (len={n}, ip.src={source}): {e}"));

match result {
Ok(ip) => {
Expand Down
24 changes: 22 additions & 2 deletions leak-checker/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,30 @@ pub fn get_interface_ip(interface: &Interface, ip_version: Ip) -> anyhow::Result

#[cfg(unix)]
pub fn get_interface_ip(interface: &Interface, ip_version: Ip) -> anyhow::Result<IpAddr> {
let Interface::Name(interface) = interface;
#[cfg(target_os = "macos")]
let interface_name;

let interface_name = match interface {
Interface::Name(name) => name.as_str(),

#[cfg(target_os = "macos")]
&Interface::Index(index) => {
use anyhow::{anyhow, Context};
use std::ffi::c_uint;

// nix getifaddrs provides no way of getting an interface by index, so we need to get
// the interface name
interface_name = nix::net::if_::if_indextoname(c_uint::from(index))
.with_context(|| anyhow!("Failed to get name of iface with index {index}"))?;

interface_name
.to_str()
.context("Network interface name was not UTF-8")?
}
};

for interface_address in nix::ifaddrs::getifaddrs()? {
if &interface_address.interface_name != interface {
if interface_address.interface_name != interface_name {
continue;
};
let Some(address) = interface_address.address else {
Expand Down
3 changes: 2 additions & 1 deletion mullvad-daemon/src/leak_checker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ async fn check_for_leaks(
v6_route.interface_index
};

let index = std::num::NonZeroU32::try_from(index).context("Interface index was 0")?;
let index =
std::num::NonZeroU32::try_from(u32::from(index)).context("Interface index was 0")?;
leak_checker::Interface::Index(index)
};

Expand Down

0 comments on commit 598176e

Please sign in to comment.