Skip to content

Commit 630bc17

Browse files
committed
Rename and change to tuple struct
1 parent 2de990f commit 630bc17

File tree

4 files changed

+54
-66
lines changed

4 files changed

+54
-66
lines changed

mullvad-daemon/src/tunnel.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::{
88

99
use tokio::sync::Mutex;
1010

11-
use mullvad_relay_selector::{GetRelay, RelaySelector, RuntimeParameters, WireguardConfig};
11+
use mullvad_relay_selector::{GetRelay, RelaySelector, RuntimeIpAvailability, WireguardConfig};
1212
use mullvad_types::{
1313
endpoint::MullvadWireguardEndpoint, location::GeoIpLocation, relay_list::Relay,
1414
settings::TunnelOptions,
@@ -164,9 +164,10 @@ impl InnerParametersGenerator {
164164
ipv6: bool,
165165
) -> Result<TunnelParameters, Error> {
166166
let data = self.device().await?;
167-
let selected_relay = self
168-
.relay_selector
169-
.get_relay(retry_attempt as usize, RuntimeParameters::new(ipv4, ipv6))?;
167+
let selected_relay = self.relay_selector.get_relay(
168+
retry_attempt as usize,
169+
RuntimeIpAvailability::new(ipv4, ipv6),
170+
)?;
170171

171172
match selected_relay {
172173
#[cfg(not(target_os = "android"))]

mullvad-relay-selector/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ pub use error::Error;
1111
pub use relay_selector::{
1212
detailer, matcher, matcher::filter_matching_relay_list, query, relays::WireguardConfig,
1313
AdditionalRelayConstraints, AdditionalWireguardConstraints, GetRelay, RelaySelector,
14-
RuntimeParameters, SelectedBridge, SelectedObfuscator, SelectorConfig, OPENVPN_RETRY_ORDER,
14+
RuntimeIpAvailability, SelectedBridge, SelectedObfuscator, SelectorConfig, OPENVPN_RETRY_ORDER,
1515
WIREGUARD_RETRY_ORDER,
1616
};

mullvad-relay-selector/src/relay_selector/mod.rs

+31-36
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ use talpid_types::{
5252
ErrorExt,
5353
};
5454

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
5757
/// preferences. See [the documentation on `RelayQuery`][RelayQuery] for further details.
5858
///
5959
/// 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(|| {
8080
]
8181
});
8282

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
8585
/// preferences. See [the documentation on `RelayQuery`][RelayQuery] for further details.
8686
///
8787
/// This list should be kept in sync with the expected behavior defined in `docs/relay-selector.md`
@@ -178,31 +178,26 @@ pub struct AdditionalWireguardConstraints {
178178
pub quantum_resistant: QuantumResistantState,
179179
}
180180

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,
182184
#[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+
})
198195
}
199196
}
200197

201-
impl Default for RuntimeParameters {
198+
impl Default for RuntimeIpAvailability {
202199
fn default() -> Self {
203-
RuntimeParameters {
204-
ip_availability: Some(Constraint::Only(IpVersion::V4)),
205-
}
200+
RuntimeIpAvailability(Some(Constraint::Only(IpVersion::V4)))
206201
}
207202
}
208203

@@ -555,7 +550,7 @@ impl RelaySelector {
555550
pub fn get_relay(
556551
&self,
557552
retry_attempt: usize,
558-
runtime_params: RuntimeParameters,
553+
runtime_params: RuntimeIpAvailability,
559554
) -> Result<GetRelay, Error> {
560555
let config_guard = self.config.lock().unwrap();
561556
let config = SpecializedSelectorConfig::from(&*config_guard);
@@ -589,7 +584,7 @@ impl RelaySelector {
589584
&self,
590585
retry_attempt: usize,
591586
retry_order: &[RelayQuery],
592-
runtime_params: RuntimeParameters,
587+
runtime_ip_availability: RuntimeIpAvailability,
593588
) -> Result<GetRelay, Error> {
594589
let config_guard = self.config.lock().unwrap();
595590
let config = SpecializedSelectorConfig::from(&*config_guard);
@@ -606,7 +601,7 @@ impl RelaySelector {
606601
let query = Self::pick_and_merge_query(
607602
retry_attempt,
608603
retry_order,
609-
runtime_params,
604+
runtime_ip_availability,
610605
&normal_config,
611606
&relay_list,
612607
)?;
@@ -632,12 +627,12 @@ impl RelaySelector {
632627
fn pick_and_merge_query(
633628
retry_attempt: usize,
634629
retry_order: &[RelayQuery],
635-
runtime_params: RuntimeParameters,
630+
runtime_ip_availability: RuntimeIpAvailability,
636631
user_config: &NormalSelectorConfig<'_>,
637632
parsed_relays: &RelayList,
638633
) -> Result<RelayQuery, Error> {
639634
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)?;
641636
log::trace!("Merging user preferences {user_query:?} with default retry strategy");
642637
retry_order
643638
.iter()
@@ -683,10 +678,10 @@ impl RelaySelector {
683678
parsed_relays: &RelayList,
684679
custom_lists: &CustomListsSettings,
685680
) -> 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.
690685
let mut query = query.clone();
691686
query.set_tunnel_protocol(TunnelType::Wireguard)?;
692687
Self::get_wireguard_relay_inner(&query, custom_lists, parsed_relays)
@@ -1174,15 +1169,15 @@ impl RelaySelector {
11741169
}
11751170

11761171
fn apply_ip_availability(
1177-
runtime_params: RuntimeParameters,
1172+
runtime_ip_availability: RuntimeIpAvailability,
11781173
user_query: &mut RelayQuery,
11791174
) -> Result<(), Error> {
11801175
let wireguard_constraints = user_query
11811176
.wireguard_constraints()
11821177
.to_owned()
11831178
.intersection(WireguardRelayQuery {
1184-
ip_version: runtime_params
1185-
.ip_availability
1179+
ip_version: runtime_ip_availability
1180+
.0
11861181
.ok_or(Error::IpVersionUnavailable)?,
11871182
..Default::default()
11881183
})

mullvad-relay-selector/tests/relay_selector.rs

+17-25
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use talpid_types::net::{
1515

1616
use mullvad_relay_selector::{
1717
query::{builder::RelayQueryBuilder, BridgeQuery, ObfuscationQuery, OpenVpnRelayQuery},
18-
Error, GetRelay, RelaySelector, RuntimeParameters, SelectedObfuscator, SelectorConfig,
18+
Error, GetRelay, RelaySelector, RuntimeIpAvailability, SelectedObfuscator, SelectorConfig,
1919
WireguardConfig, OPENVPN_RETRY_ORDER, WIREGUARD_RETRY_ORDER,
2020
};
2121
use mullvad_types::{
@@ -379,7 +379,8 @@ fn assert_openvpn_retry_order() {
379379
);
380380
}
381381

382-
/// Test whether the relay selector seems to respect the order as defined by [`WIREGUARD_RETRY_ORDER`].
382+
/// Test whether the relay selector seems to respect the order as defined by
383+
/// [`WIREGUARD_RETRY_ORDER`].
383384
#[test]
384385
fn test_wireguard_retry_order() {
385386
// In order to for the relay queries defined by `RETRY_ORDER` to always take precedence,
@@ -390,12 +391,7 @@ fn test_wireguard_retry_order() {
390391
let relay_selector = default_relay_selector();
391392
for (retry_attempt, query) in WIREGUARD_RETRY_ORDER.iter().enumerate() {
392393
let relay = relay_selector
393-
.get_relay(
394-
retry_attempt,
395-
RuntimeParameters {
396-
ip_availability: Some(Constraint::Any),
397-
},
398-
)
394+
.get_relay(retry_attempt, RuntimeIpAvailability(Some(Constraint::Any)))
399395
.unwrap_or_else(|_| panic!("Retry attempt {retry_attempt} did not yield any relay"));
400396
// For each relay, cross-check that the it has the expected tunnel protocol
401397
let tunnel_type = tunnel_type(&unwrap_relay(relay.clone()));
@@ -434,7 +430,8 @@ fn test_wireguard_retry_order() {
434430
}
435431
}
436432

437-
/// Test whether the relay selector seems to respect the order as defined by [`OPENVPN_RETRY_ORDER`].
433+
/// Test whether the relay selector seems to respect the order as defined by
434+
/// [`OPENVPN_RETRY_ORDER`].
438435
#[test]
439436
fn test_openvpn_retry_order() {
440437
// In order to for the relay queries defined by `RETRY_ORDER` to always take precedence,
@@ -453,12 +450,7 @@ fn test_openvpn_retry_order() {
453450

454451
for (retry_attempt, query) in OPENVPN_RETRY_ORDER.iter().enumerate() {
455452
let relay = relay_selector
456-
.get_relay(
457-
retry_attempt,
458-
RuntimeParameters {
459-
ip_availability: Some(Constraint::Any),
460-
},
461-
)
453+
.get_relay(retry_attempt, RuntimeIpAvailability(Some(Constraint::Any)))
462454
.unwrap_or_else(|_| panic!("Retry attempt {retry_attempt} did not yield any relay"));
463455
// For each relay, cross-check that the it has the expected tunnel protocol
464456
let tunnel_type = tunnel_type(&unwrap_relay(relay.clone()));
@@ -1164,7 +1156,11 @@ fn test_openvpn_auto_bridge() {
11641156
.take(100 * retry_order.len())
11651157
{
11661158
let relay = relay_selector
1167-
.get_relay_with_custom_params(retry_attempt, &retry_order, RuntimeParameters::default())
1159+
.get_relay_with_custom_params(
1160+
retry_attempt,
1161+
&retry_order,
1162+
RuntimeIpAvailability::default(),
1163+
)
11681164
.unwrap();
11691165
match relay {
11701166
GetRelay::OpenVpn { bridge, .. } => {
@@ -1273,7 +1269,7 @@ fn test_include_in_country() {
12731269
// If include_in_country is false for all relays, a relay must be selected anyway.
12741270
let relay_selector = RelaySelector::from_list(SelectorConfig::default(), relay_list.clone());
12751271
assert!(relay_selector
1276-
.get_relay(0, RuntimeParameters::default())
1272+
.get_relay(0, RuntimeIpAvailability::default())
12771273
.is_ok());
12781274

12791275
// If include_in_country is true for some relay, it must always be selected.
@@ -1282,7 +1278,7 @@ fn test_include_in_country() {
12821278
let relay_selector = RelaySelector::from_list(SelectorConfig::default(), relay_list);
12831279
let relay = unwrap_relay(
12841280
relay_selector
1285-
.get_relay(0, RuntimeParameters::default())
1281+
.get_relay(0, RuntimeIpAvailability::default())
12861282
.expect("expected match"),
12871283
);
12881284

@@ -1619,7 +1615,7 @@ fn valid_user_setting_should_yield_relay() {
16191615
let user_result = relay_selector.get_relay_by_query(user_query.clone());
16201616
for retry_attempt in 0..WIREGUARD_RETRY_ORDER.len() {
16211617
let post_unification_result =
1622-
relay_selector.get_relay(retry_attempt, RuntimeParameters::default());
1618+
relay_selector.get_relay(retry_attempt, RuntimeIpAvailability::default());
16231619
if user_result.is_ok() {
16241620
assert!(post_unification_result.is_ok(), "Expected Post-unification query to be valid because original query {:#?} yielded a connection configuration", user_query)
16251621
}
@@ -1642,9 +1638,7 @@ fn test_shadowsocks_runtime_ipv4_unavailable() {
16421638
..SelectorConfig::default()
16431639
};
16441640
let relay_selector = RelaySelector::from_list(config, RELAYS.clone());
1645-
let runtime_parameters = RuntimeParameters {
1646-
ip_availability: Some(Constraint::Only(IpVersion::V6)),
1647-
};
1641+
let runtime_parameters = RuntimeIpAvailability(Some(Constraint::Only(IpVersion::V6)));
16481642
let user_result = relay_selector.get_relay(0, runtime_parameters).unwrap();
16491643
assert!(
16501644
matches!(user_result, GetRelay::Wireguard {
@@ -1672,9 +1666,7 @@ fn test_runtime_ipv4_unavailable() {
16721666
..SelectorConfig::default()
16731667
};
16741668
let relay_selector = RelaySelector::from_list(config, RELAYS.clone());
1675-
let runtime_parameters = RuntimeParameters {
1676-
ip_availability: Some(Constraint::Only(IpVersion::V6)),
1677-
};
1669+
let runtime_parameters = RuntimeIpAvailability(Some(Constraint::Only(IpVersion::V6)));
16781670
let relay = relay_selector.get_relay(0, runtime_parameters).unwrap();
16791671
match relay {
16801672
GetRelay::Wireguard { endpoint, .. } => {

0 commit comments

Comments
 (0)