@@ -999,7 +999,7 @@ async fn auto_mtu_detection(
999
999
#[ cfg( any( target_os = "macos" , target_os = "linux" ) ) ] iface_name : String ,
1000
1000
current_mtu : u16 ,
1001
1001
) -> Result < u16 > {
1002
- use futures:: { stream:: FuturesOrdered , TryStreamExt } ;
1002
+ use futures:: { future , stream:: FuturesUnordered , TryStreamExt } ;
1003
1003
use surge_ping:: { Client , Config , PingIdentifier , PingSequence , SurgeError } ;
1004
1004
use talpid_tunnel:: { ICMP_HEADER_SIZE , MIN_IPV4_MTU } ;
1005
1005
use tokio_stream:: StreamExt ;
@@ -1015,34 +1015,35 @@ async fn auto_mtu_detection(
1015
1015
let step_size = 20 ;
1016
1016
1017
1017
let linspace = mtu_spacing ( MIN_IPV4_MTU , current_mtu, step_size) ;
1018
+ let largest_payload = vec ! [ 0 ; current_mtu as usize ] ;
1018
1019
1019
- let set : FuturesOrdered < _ > = linspace
1020
+ let ping_stream : FuturesUnordered < _ > = linspace
1020
1021
. iter ( )
1021
1022
. enumerate ( )
1022
1023
. map ( |( i, & mtu) | {
1023
1024
let client = client. clone ( ) ;
1025
+ let payload = & largest_payload[ 0 ..( mtu - IPV4_HEADER_SIZE - ICMP_HEADER_SIZE ) as usize ] ;
1024
1026
async move {
1025
1027
log:: trace!( "Sending ICMP ping of total size {mtu}" ) ;
1026
- let payload = vec ! [ 0 ; ( mtu - IPV4_HEADER_SIZE - ICMP_HEADER_SIZE ) as usize ] ; //? Can we avoid allocating here?
1027
1028
client
1028
1029
. pinger ( IpAddr :: V4 ( gateway) , PingIdentifier ( 111 ) ) //? Is a static identified ok, or should we randomize?
1029
1030
. await
1030
1031
. timeout ( PING_TIMEOUT ) // TODO: choose a good timeout
1031
- . ping ( PingSequence ( i as u16 ) , & payload)
1032
+ . ping ( PingSequence ( i as u16 ) , payload)
1032
1033
. await
1033
1034
}
1034
1035
} )
1035
1036
. collect ( ) ;
1036
1037
1037
- let res = set
1038
+ let res = ping_stream
1038
1039
. map ( |res| match res {
1039
1040
// Map successful pings to packet size
1040
1041
Ok ( ( packet, _rtt) ) => {
1041
1042
let surge_ping:: IcmpPacket :: V4 ( packet) = packet else {
1042
1043
panic ! ( "ICMP ping response was not of IPv4 type" ) ;
1043
1044
} ;
1044
1045
let size = packet. get_size ( ) as u16 + IPV4_HEADER_SIZE ;
1045
- log:: debug !( "Got ICMP ping response of total size {size}" ) ;
1046
+ log:: trace !( "Got ICMP ping response of total size {size}" ) ;
1046
1047
debug_assert_eq ! ( size, linspace[ packet. get_sequence( ) . 0 as usize ] ) ;
1047
1048
Ok ( Some ( size) )
1048
1049
}
@@ -1054,7 +1055,7 @@ async fn auto_mtu_detection(
1054
1055
// Short circuit and return error on unexpected error types
1055
1056
Err ( e) => Err ( e) ,
1056
1057
} )
1057
- . try_fold ( None , |acc, x| async move { Ok ( std :: cmp :: max ( acc, x ) ) } )
1058
+ . try_fold ( None , |acc, mtu| future :: ready ( Ok ( acc. max ( mtu ) ) ) )
1058
1059
. await ;
1059
1060
match res {
1060
1061
Ok ( Some ( verified_mtu) ) => Ok ( verified_mtu) ,
0 commit comments