Skip to content

Commit 98c58b8

Browse files
committed
Refactor auto_mtu_detection
1 parent 9a7e881 commit 98c58b8

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

talpid-wireguard/src/lib.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -989,11 +989,11 @@ impl WireguardMonitor {
989989
}
990990
}
991991

992-
#[cfg(target_os = "linux")]
993992
/// Detects the maximum MTU that does not cause dropped packets.
994993
///
995994
/// The detection works by sending evenly spread out range of pings between 576 and the given
996995
/// current tunnel MTU, and returning the maximum packet size that war returned within a timeout.
996+
#[cfg(target_os = "linux")]
997997
async fn auto_mtu_detection(
998998
gateway: std::net::Ipv4Addr,
999999
#[cfg(any(target_os = "macos", target_os = "linux"))] iface_name: String,
@@ -1009,26 +1009,24 @@ async fn auto_mtu_detection(
10091009
let config_builder = Config::builder().kind(surge_ping::ICMP::V4);
10101010
#[cfg(any(target_os = "macos", target_os = "linux"))]
10111011
let config_builder = config_builder.interface(&iface_name);
1012-
let config = config_builder.build();
1013-
let client = Client::new(&config).unwrap();
1012+
let client = Client::new(&config_builder.build()).unwrap();
10141013

10151014
let step_size = 20;
1016-
10171015
let linspace = mtu_spacing(MIN_IPV4_MTU, current_mtu, step_size);
1018-
let largest_payload = vec![0; current_mtu as usize];
10191016

1020-
let ping_stream: FuturesUnordered<_> = linspace
1017+
let payload_buf = vec![0; current_mtu as usize];
10211018
.iter()
10221019
.enumerate()
10231020
.map(|(i, &mtu)| {
10241021
let client = client.clone();
1025-
let payload = &largest_payload[0..(mtu - IPV4_HEADER_SIZE - ICMP_HEADER_SIZE) as usize];
1022+
let payload_size = (mtu - IPV4_HEADER_SIZE - ICMP_HEADER_SIZE) as usize;
1023+
let payload = &payload_buf[0..payload_size];
10261024
async move {
10271025
log::trace!("Sending ICMP ping of total size {mtu}");
10281026
client
10291027
.pinger(IpAddr::V4(gateway), PingIdentifier(111)) //? Is a static identified ok, or should we randomize?
10301028
.await
1031-
.timeout(PING_TIMEOUT) // TODO: choose a good timeout
1029+
.timeout(PING_TIMEOUT)
10321030
.ping(PingSequence(i as u16), payload)
10331031
.await
10341032
}

0 commit comments

Comments
 (0)