Skip to content

Commit

Permalink
fixup: Deduplicate
Browse files Browse the repository at this point in the history
  • Loading branch information
hulthe committed Jan 8, 2025
1 parent 3cbed2f commit dae74db
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 54 deletions.
56 changes: 5 additions & 51 deletions leak-checker/src/traceroute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,63 +458,17 @@ fn parse_icmp_time_exceeded_raw(ip_payload: Ip<&[u8], &[u8]>) -> anyhow::Result<
Ok(())
}

IpProtocol::Icmpv6 => {
let original_icmp_packet =
icmpv6::echo_request::EchoRequestPacket::new(original_ip_payload)
.ok_or_else(too_small)?;
IpProtocol::Icmp => parse_icmp_probe(Ip::V4(original_ip_payload)),

ensure!(
original_icmp_packet.get_icmpv6_type() == Icmpv6Types::EchoRequest,
"Not ICMP6/EchoRequest"
);

// check if payload looks right
// some network nodes will strip the payload, that's fine.
let echo_payload = original_icmp_packet.payload();
if !echo_payload.is_empty() && !echo_payload.starts_with(&PROBE_PAYLOAD) {
let echo_payload: String = echo_payload
.iter()
.copied()
.flat_map(escape_default)
.map(char::from)
.collect();
bail!("Wrong ICMP6/Echo payload: {echo_payload:?}");
}

Ok(())
}

IpProtocol::Icmp => {
let original_icmp_packet =
icmp::echo_request::EchoRequestPacket::new(original_ip_payload)
.ok_or_else(too_small)?;

ensure!(
original_icmp_packet.get_icmp_type() == IcmpTypes::EchoRequest,
"Not ICMP/EchoRequest"
);

// check if payload looks right
// some network nodes will strip the payload, that's fine.
let echo_payload = original_icmp_packet.payload();
if !echo_payload.is_empty() && !echo_payload.starts_with(&PROBE_PAYLOAD) {
let echo_payload: String = echo_payload
.iter()
.copied()
.flat_map(escape_default)
.map(char::from)
.collect();
bail!("Wrong ICMP/Echo payload: {echo_payload:?}");
}

Ok(())
}
IpProtocol::Icmpv6 => parse_icmp_probe(Ip::V6(original_ip_payload)),

_ => bail!("Not UDP/ICMP"),
}
}

fn parse_icmp_echo_raw(icmp_bytes: Ip<&[u8], &[u8]>) -> anyhow::Result<()> {
/// Try to parse bytes as an ICMP/ICMP6 Echo Request matching the probe packets send by
/// [send_icmp_probes].
fn parse_icmp_probe(icmp_bytes: Ip<&[u8], &[u8]>) -> anyhow::Result<()> {
let echo_packet_v4;
let echo_packet_v6;
let echo_payload = match icmp_bytes {
Expand Down
6 changes: 3 additions & 3 deletions leak-checker/src/traceroute/platform/linux.rs
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::{parse_icmp_echo_raw, Ip, TracerouteOpt, RECV_GRACE_TIME};
use crate::traceroute::{parse_icmp_probe, Ip, TracerouteOpt, RECV_GRACE_TIME};
use crate::{Interface, LeakInfo, LeakStatus};

use super::{unix, AsyncIcmpSocket, Traceroute};
Expand Down Expand Up @@ -205,7 +205,7 @@ async fn recv_ttl_responses(

// Ensure that this is the original Echo packet that we sent.
skip_if!(
parse_icmp_echo_raw(Ip::V4(packet)).is_err(),
parse_icmp_probe(Ip::V4(packet)).is_err(),
"Not a response to us"
);

Expand Down Expand Up @@ -242,7 +242,7 @@ async fn recv_ttl_responses(

// Ensure that this is the original Echo packet that we sent.
skip_if!(
parse_icmp_echo_raw(Ip::V6(packet)).is_err(),
parse_icmp_probe(Ip::V6(packet)).is_err(),
"Not a response to us"
);

Expand Down

0 comments on commit dae74db

Please sign in to comment.