@@ -52,8 +52,8 @@ use talpid_types::{
52
52
ErrorExt ,
53
53
} ;
54
54
55
- /// [`WIREGUARD_RETRY_ORDER`] defines an ordered set of relay parameters which the relay selector should
56
- /// prioritize on successive connection attempts. Note that these will *never* override user
55
+ /// [`WIREGUARD_RETRY_ORDER`] defines an ordered set of relay parameters which the relay selector
56
+ /// should prioritize on successive connection attempts. Note that these will *never* override user
57
57
/// preferences. See [the documentation on `RelayQuery`][RelayQuery] for further details.
58
58
///
59
59
/// This list should be kept in sync with the expected behavior defined in `docs/relay-selector.md`
@@ -80,8 +80,8 @@ pub static WIREGUARD_RETRY_ORDER: LazyLock<Vec<RelayQuery>> = LazyLock::new(|| {
80
80
]
81
81
} ) ;
82
82
83
- /// [`OPENVPN_RETRY_ORDER`] defines an ordered set of relay parameters which the relay selector should
84
- /// prioritize on successive connection attempts. Note that these will *never* override user
83
+ /// [`OPENVPN_RETRY_ORDER`] defines an ordered set of relay parameters which the relay selector
84
+ /// should prioritize on successive connection attempts. Note that these will *never* override user
85
85
/// preferences. See [the documentation on `RelayQuery`][RelayQuery] for further details.
86
86
///
87
87
/// This list should be kept in sync with the expected behavior defined in `docs/relay-selector.md`
@@ -178,31 +178,26 @@ pub struct AdditionalWireguardConstraints {
178
178
pub quantum_resistant : QuantumResistantState ,
179
179
}
180
180
181
- /// Values which affect the choice of relay but are only known at runtime.
181
+ /// Whether IPv4, IPv6 is available at runtime.
182
+ ///
183
+ /// `None` means that no IP version is available, `Some(Any)` means that both are available,
182
184
#[ derive( Clone , Debug ) ]
183
- pub struct RuntimeParameters {
184
- /// Whether IPv4, IPv6 or both is available
185
- pub ip_availability : Option < Constraint < IpVersion > > ,
186
- }
187
-
188
- impl RuntimeParameters {
189
- pub fn new ( ipv4 : bool , ipv6 : bool ) -> RuntimeParameters {
190
- RuntimeParameters {
191
- ip_availability : match ( ipv4, ipv6) {
192
- ( true , true ) => Some ( Constraint :: Any ) ,
193
- ( false , true ) => Some ( Constraint :: Only ( IpVersion :: V6 ) ) ,
194
- ( true , false ) => Some ( Constraint :: Only ( IpVersion :: V4 ) ) ,
195
- ( false , false ) => None ,
196
- } ,
197
- }
185
+ pub struct RuntimeIpAvailability ( pub Option < Constraint < IpVersion > > ) ;
186
+
187
+ impl RuntimeIpAvailability {
188
+ pub fn new ( ipv4 : bool , ipv6 : bool ) -> RuntimeIpAvailability {
189
+ RuntimeIpAvailability ( match ( ipv4, ipv6) {
190
+ ( true , true ) => Some ( Constraint :: Any ) ,
191
+ ( false , true ) => Some ( Constraint :: Only ( IpVersion :: V6 ) ) ,
192
+ ( true , false ) => Some ( Constraint :: Only ( IpVersion :: V4 ) ) ,
193
+ ( false , false ) => None ,
194
+ } )
198
195
}
199
196
}
200
197
201
- impl Default for RuntimeParameters {
198
+ impl Default for RuntimeIpAvailability {
202
199
fn default ( ) -> Self {
203
- RuntimeParameters {
204
- ip_availability : Some ( Constraint :: Only ( IpVersion :: V4 ) ) ,
205
- }
200
+ RuntimeIpAvailability ( Some ( Constraint :: Only ( IpVersion :: V4 ) ) )
206
201
}
207
202
}
208
203
@@ -555,7 +550,7 @@ impl RelaySelector {
555
550
pub fn get_relay (
556
551
& self ,
557
552
retry_attempt : usize ,
558
- runtime_params : RuntimeParameters ,
553
+ runtime_params : RuntimeIpAvailability ,
559
554
) -> Result < GetRelay , Error > {
560
555
let config_guard = self . config . lock ( ) . unwrap ( ) ;
561
556
let config = SpecializedSelectorConfig :: from ( & * config_guard) ;
@@ -589,7 +584,7 @@ impl RelaySelector {
589
584
& self ,
590
585
retry_attempt : usize ,
591
586
retry_order : & [ RelayQuery ] ,
592
- runtime_params : RuntimeParameters ,
587
+ runtime_ip_availability : RuntimeIpAvailability ,
593
588
) -> Result < GetRelay , Error > {
594
589
let config_guard = self . config . lock ( ) . unwrap ( ) ;
595
590
let config = SpecializedSelectorConfig :: from ( & * config_guard) ;
@@ -606,7 +601,7 @@ impl RelaySelector {
606
601
let query = Self :: pick_and_merge_query (
607
602
retry_attempt,
608
603
retry_order,
609
- runtime_params ,
604
+ runtime_ip_availability ,
610
605
& normal_config,
611
606
& relay_list,
612
607
) ?;
@@ -632,12 +627,12 @@ impl RelaySelector {
632
627
fn pick_and_merge_query (
633
628
retry_attempt : usize ,
634
629
retry_order : & [ RelayQuery ] ,
635
- runtime_params : RuntimeParameters ,
630
+ runtime_ip_availability : RuntimeIpAvailability ,
636
631
user_config : & NormalSelectorConfig < ' _ > ,
637
632
parsed_relays : & RelayList ,
638
633
) -> Result < RelayQuery , Error > {
639
634
let mut user_query = RelayQuery :: try_from ( user_config. clone ( ) ) ?;
640
- apply_ip_availability ( runtime_params , & mut user_query) ?;
635
+ apply_ip_availability ( runtime_ip_availability , & mut user_query) ?;
641
636
log:: trace!( "Merging user preferences {user_query:?} with default retry strategy" ) ;
642
637
retry_order
643
638
. iter ( )
@@ -683,10 +678,10 @@ impl RelaySelector {
683
678
parsed_relays : & RelayList ,
684
679
custom_lists : & CustomListsSettings ,
685
680
) -> Result < GetRelay , Error > {
686
- // FIXME: A bit of defensive programming - calling `get_wireguard_relay_inner` with a query that
687
- // doesn't specify Wireguard as the desired tunnel type is not valid and will lead
688
- // to unwanted behavior. This should be seen as a workaround, and it would be nicer
689
- // to lift this invariant to be checked by the type system instead.
681
+ // FIXME: A bit of defensive programming - calling `get_wireguard_relay_inner` with a query
682
+ // that doesn't specify Wireguard as the desired tunnel type is not valid and will
683
+ // lead to unwanted behavior. This should be seen as a workaround, and it would be
684
+ // nicer to lift this invariant to be checked by the type system instead.
690
685
let mut query = query. clone ( ) ;
691
686
query. set_tunnel_protocol ( TunnelType :: Wireguard ) ?;
692
687
Self :: get_wireguard_relay_inner ( & query, custom_lists, parsed_relays)
@@ -1174,15 +1169,15 @@ impl RelaySelector {
1174
1169
}
1175
1170
1176
1171
fn apply_ip_availability (
1177
- runtime_params : RuntimeParameters ,
1172
+ runtime_ip_availability : RuntimeIpAvailability ,
1178
1173
user_query : & mut RelayQuery ,
1179
1174
) -> Result < ( ) , Error > {
1180
1175
let wireguard_constraints = user_query
1181
1176
. wireguard_constraints ( )
1182
1177
. to_owned ( )
1183
1178
. intersection ( WireguardRelayQuery {
1184
- ip_version : runtime_params
1185
- . ip_availability
1179
+ ip_version : runtime_ip_availability
1180
+ . 0
1186
1181
. ok_or ( Error :: IpVersionUnavailable ) ?,
1187
1182
..Default :: default ( )
1188
1183
} )
0 commit comments