Skip to content

Commit

Permalink
fixup: Less unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
hulthe committed Jan 10, 2025
1 parent 7c84dd8 commit 84b4f18
Show file tree
Hide file tree
Showing 10 changed files with 356 additions and 385 deletions.
18 changes: 12 additions & 6 deletions leak-checker/src/traceroute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ use std::{net::IpAddr, ops::Range, time::Duration};

use crate::{Interface, LeakStatus};

mod platform;
/// Traceroute implementation for windows.
#[cfg(target_os = "windows")]
mod windows;

/// Traceroute implementation for unix.
#[cfg(unix)]
mod unix;

#[derive(Clone, clap::Args)]
pub struct TracerouteOpt {
Expand Down Expand Up @@ -69,17 +75,17 @@ pub async fn try_run_leak_test(opt: &TracerouteOpt) -> anyhow::Result<LeakStatus
#[cfg(unix)]
return {
#[cfg(target_os = "android")]
type Impl = platform::unix::android::TracerouteAndroid;
type Impl = unix::android::TracerouteAndroid;
#[cfg(target_os = "linux")]
type Impl = platform::unix::linux::TracerouteLinux;
type Impl = unix::linux::TracerouteLinux;
#[cfg(target_os = "macos")]
type Impl = platform::unix::macos::TracerouteMacos;
type Impl = unix::macos::TracerouteMacos;

platform::unix::try_run_leak_test::<Impl>(opt).await
unix::try_run_leak_test::<Impl>(opt).await
};

#[cfg(target_os = "windows")]
return platform::windows::traceroute_using_ping(opt).await;
return windows::traceroute_using_ping(opt).await;
}

/// IP version, v4 or v6, with some associated data.
Expand Down
7 changes: 0 additions & 7 deletions leak-checker/src/traceroute/platform/mod.rs

This file was deleted.

137 changes: 0 additions & 137 deletions leak-checker/src/traceroute/platform/unix/common.rs

This file was deleted.

88 changes: 0 additions & 88 deletions leak-checker/src/traceroute/platform/unix/macos.rs

This file was deleted.

File renamed without changes.
52 changes: 52 additions & 0 deletions leak-checker/src/traceroute/unix/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#![allow(dead_code)] // some code here is not used on some targets.

use std::net::{IpAddr, SocketAddr};

use anyhow::Context;
use socket2::Socket;

use crate::{traceroute::Ip, Interface};

pub(crate) fn get_interface_ip(interface: &Interface, ip_version: Ip) -> anyhow::Result<IpAddr> {
let Interface::Name(interface) = interface;

for interface_address in nix::ifaddrs::getifaddrs()? {
if &interface_address.interface_name != interface {
continue;
};
let Some(address) = interface_address.address else {
continue;
};

match ip_version {
Ip::V4(()) => {
if let Some(address) = address.as_sockaddr_in() {
return Ok(IpAddr::V4(address.ip()));
};
}
Ip::V6(()) => {
if let Some(address) = address.as_sockaddr_in6() {
return Ok(IpAddr::V6(address.ip()));
};
}
}
}

anyhow::bail!("Interface {interface:?} has no valid IP to bind to");
}

pub(crate) fn bind_socket_to_interface(
socket: &Socket,
interface: &Interface,
ip_version: Ip,
) -> anyhow::Result<()> {
let interface_ip = get_interface_ip(interface, ip_version)?;

log::info!("Binding socket to {interface_ip} ({interface:?})");

socket
.bind(&SocketAddr::new(interface_ip, 0).into())
.context("Failed to bind socket to interface address")?;

Ok(())
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use pnet_packet::icmpv6::{Icmpv6Code, Icmpv6Type, Icmpv6Types};
use socket2::Socket;
use tokio::time::{sleep, Instant};

use crate::traceroute::platform::unix::parse_icmp_probe;
use crate::traceroute::unix::parse_icmp_probe;
use crate::traceroute::{Ip, TracerouteOpt, RECV_GRACE_TIME};
use crate::{Interface, LeakInfo, LeakStatus};

Expand Down
Loading

0 comments on commit 84b4f18

Please sign in to comment.