Skip to content

Commit dae74db

Browse files
committed
fixup: Deduplicate
1 parent 3cbed2f commit dae74db

File tree

2 files changed

+8
-54
lines changed

2 files changed

+8
-54
lines changed

leak-checker/src/traceroute.rs

+5-51
Original file line numberDiff line numberDiff line change
@@ -458,63 +458,17 @@ fn parse_icmp_time_exceeded_raw(ip_payload: Ip<&[u8], &[u8]>) -> anyhow::Result<
458458
Ok(())
459459
}
460460

461-
IpProtocol::Icmpv6 => {
462-
let original_icmp_packet =
463-
icmpv6::echo_request::EchoRequestPacket::new(original_ip_payload)
464-
.ok_or_else(too_small)?;
461+
IpProtocol::Icmp => parse_icmp_probe(Ip::V4(original_ip_payload)),
465462

466-
ensure!(
467-
original_icmp_packet.get_icmpv6_type() == Icmpv6Types::EchoRequest,
468-
"Not ICMP6/EchoRequest"
469-
);
470-
471-
// check if payload looks right
472-
// some network nodes will strip the payload, that's fine.
473-
let echo_payload = original_icmp_packet.payload();
474-
if !echo_payload.is_empty() && !echo_payload.starts_with(&PROBE_PAYLOAD) {
475-
let echo_payload: String = echo_payload
476-
.iter()
477-
.copied()
478-
.flat_map(escape_default)
479-
.map(char::from)
480-
.collect();
481-
bail!("Wrong ICMP6/Echo payload: {echo_payload:?}");
482-
}
483-
484-
Ok(())
485-
}
486-
487-
IpProtocol::Icmp => {
488-
let original_icmp_packet =
489-
icmp::echo_request::EchoRequestPacket::new(original_ip_payload)
490-
.ok_or_else(too_small)?;
491-
492-
ensure!(
493-
original_icmp_packet.get_icmp_type() == IcmpTypes::EchoRequest,
494-
"Not ICMP/EchoRequest"
495-
);
496-
497-
// check if payload looks right
498-
// some network nodes will strip the payload, that's fine.
499-
let echo_payload = original_icmp_packet.payload();
500-
if !echo_payload.is_empty() && !echo_payload.starts_with(&PROBE_PAYLOAD) {
501-
let echo_payload: String = echo_payload
502-
.iter()
503-
.copied()
504-
.flat_map(escape_default)
505-
.map(char::from)
506-
.collect();
507-
bail!("Wrong ICMP/Echo payload: {echo_payload:?}");
508-
}
509-
510-
Ok(())
511-
}
463+
IpProtocol::Icmpv6 => parse_icmp_probe(Ip::V6(original_ip_payload)),
512464

513465
_ => bail!("Not UDP/ICMP"),
514466
}
515467
}
516468

517-
fn parse_icmp_echo_raw(icmp_bytes: Ip<&[u8], &[u8]>) -> anyhow::Result<()> {
469+
/// Try to parse bytes as an ICMP/ICMP6 Echo Request matching the probe packets send by
470+
/// [send_icmp_probes].
471+
fn parse_icmp_probe(icmp_bytes: Ip<&[u8], &[u8]>) -> anyhow::Result<()> {
518472
let echo_packet_v4;
519473
let echo_packet_v6;
520474
let echo_payload = match icmp_bytes {

leak-checker/src/traceroute/platform/linux.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use pnet_packet::icmpv6::{Icmpv6Code, Icmpv6Type, Icmpv6Types};
1515
use socket2::Socket;
1616
use tokio::time::{sleep, Instant};
1717

18-
use crate::traceroute::{parse_icmp_echo_raw, Ip, TracerouteOpt, RECV_GRACE_TIME};
18+
use crate::traceroute::{parse_icmp_probe, Ip, TracerouteOpt, RECV_GRACE_TIME};
1919
use crate::{Interface, LeakInfo, LeakStatus};
2020

2121
use super::{unix, AsyncIcmpSocket, Traceroute};
@@ -205,7 +205,7 @@ async fn recv_ttl_responses(
205205

206206
// Ensure that this is the original Echo packet that we sent.
207207
skip_if!(
208-
parse_icmp_echo_raw(Ip::V4(packet)).is_err(),
208+
parse_icmp_probe(Ip::V4(packet)).is_err(),
209209
"Not a response to us"
210210
);
211211

@@ -242,7 +242,7 @@ async fn recv_ttl_responses(
242242

243243
// Ensure that this is the original Echo packet that we sent.
244244
skip_if!(
245-
parse_icmp_echo_raw(Ip::V6(packet)).is_err(),
245+
parse_icmp_probe(Ip::V6(packet)).is_err(),
246246
"Not a response to us"
247247
);
248248

0 commit comments

Comments
 (0)