Skip to content

Commit f90e4e5

Browse files
committed
Changed ip availability to an enum
1 parent 3c99ea8 commit f90e4e5

File tree

4 files changed

+53
-25
lines changed

4 files changed

+53
-25
lines changed

mullvad-daemon/src/tunnel.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ impl InnerParametersGenerator {
166166
let data = self.device().await?;
167167
let selected_relay = self
168168
.relay_selector
169-
.get_relay(retry_attempt as usize, RuntimeParameters { ipv4, ipv6 })?;
169+
.get_relay(retry_attempt as usize, RuntimeParameters::new(ipv4, ipv6))?;
170170

171171
match selected_relay {
172172
#[cfg(not(target_os = "android"))]

mullvad-relay-selector/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ mod relay_selector;
1010
pub use error::Error;
1111
pub use relay_selector::{
1212
detailer, matcher, matcher::filter_matching_relay_list, query, relays::WireguardConfig,
13-
AdditionalRelayConstraints, AdditionalWireguardConstraints, GetRelay, RelaySelector,
14-
RuntimeParameters, SelectedBridge, SelectedObfuscator, SelectorConfig, OPENVPN_RETRY_ORDER,
15-
WIREGUARD_RETRY_ORDER,
13+
AdditionalRelayConstraints, AdditionalWireguardConstraints, GetRelay, IpAvailability,
14+
RelaySelector, RuntimeParameters, SelectedBridge, SelectedObfuscator, SelectorConfig,
15+
OPENVPN_RETRY_ORDER, WIREGUARD_RETRY_ORDER,
1616
};

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

+43-11
Original file line numberDiff line numberDiff line change
@@ -181,32 +181,64 @@ pub struct AdditionalWireguardConstraints {
181181
/// Values which affect the choice of relay but are only known at runtime.
182182
#[derive(Clone, Debug)]
183183
pub struct RuntimeParameters {
184-
/// Whether IPv4 is available
185-
pub ipv4: bool,
186-
/// Whether IPv6 is available
187-
pub ipv6: bool,
184+
/// Whether IPv4, IPv6 or both is available
185+
pub ip_availability: IpAvailability,
188186
}
189187

190188
impl RuntimeParameters {
191189
/// Return whether a given [query][`RelayQuery`] is valid given the current runtime parameters
192190
pub fn compatible(&self, query: &RelayQuery) -> bool {
193191
match query.wireguard_constraints().ip_version {
194-
Constraint::Any => self.ipv4 || self.ipv6,
195-
Constraint::Only(talpid_types::net::IpVersion::V4) => self.ipv4,
196-
Constraint::Only(talpid_types::net::IpVersion::V6) => self.ipv6,
192+
Constraint::Any => true,
193+
Constraint::Only(talpid_types::net::IpVersion::V4) => self.ip_availability.has_ipv4(),
194+
Constraint::Only(talpid_types::net::IpVersion::V6) => self.ip_availability.has_ipv6(),
195+
}
196+
}
197+
198+
pub fn new(ipv4: bool, ipv6: bool) -> RuntimeParameters {
199+
if ipv4 && ipv6 {
200+
RuntimeParameters {
201+
ip_availability: IpAvailability::All,
202+
}
203+
} else if !ipv6 {
204+
RuntimeParameters {
205+
ip_availability: IpAvailability::Ipv4,
206+
}
207+
} else if !ipv4 {
208+
RuntimeParameters {
209+
ip_availability: IpAvailability::Ipv6,
210+
}
211+
} else {
212+
panic!("Device is offline!")
197213
}
198214
}
199215
}
200216

201217
impl Default for RuntimeParameters {
202218
fn default() -> Self {
203219
RuntimeParameters {
204-
ipv4: true,
205-
ipv6: false,
220+
ip_availability: IpAvailability::Ipv4,
206221
}
207222
}
208223
}
209224

225+
#[derive(Clone, Debug, PartialEq, Eq)]
226+
pub enum IpAvailability {
227+
Ipv4,
228+
Ipv6,
229+
All,
230+
}
231+
232+
impl IpAvailability {
233+
fn has_ipv4(&self) -> bool {
234+
self.clone() == IpAvailability::Ipv4 || self.clone() == IpAvailability::All
235+
}
236+
237+
fn has_ipv6(&self) -> bool {
238+
self.clone() == IpAvailability::Ipv6 || self.clone() == IpAvailability::All
239+
}
240+
}
241+
210242
/// This enum exists to separate the two types of [`SelectorConfig`] that exists.
211243
///
212244
/// The first one is a "regular" config, where [`SelectorConfig::relay_settings`] is
@@ -1184,10 +1216,10 @@ impl RelaySelector {
11841216
fn resolve_valid_ip_version(query: &RelayQuery, runtime_params: &RuntimeParameters) -> RelayQuery {
11851217
let mut wireguard_constraints = query.wireguard_constraints().clone();
11861218
if wireguard_constraints.ip_version.is_any() {
1187-
if runtime_params.ipv4 && !runtime_params.ipv6 {
1219+
if runtime_params.ip_availability == IpAvailability::Ipv4 {
11881220
wireguard_constraints.ip_version = Constraint::Only(talpid_types::net::IpVersion::V4)
11891221
}
1190-
if runtime_params.ipv6 && !runtime_params.ipv4 {
1222+
if runtime_params.ip_availability == IpAvailability::Ipv6 {
11911223
wireguard_constraints.ip_version = Constraint::Only(talpid_types::net::IpVersion::V6)
11921224
}
11931225
}

mullvad-relay-selector/tests/relay_selector.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ use talpid_types::net::{
1515

1616
use mullvad_relay_selector::{
1717
query::{builder::RelayQueryBuilder, BridgeQuery, ObfuscationQuery, OpenVpnRelayQuery},
18-
Error, GetRelay, RelaySelector, RuntimeParameters, SelectedObfuscator, SelectorConfig,
19-
WireguardConfig, OPENVPN_RETRY_ORDER, WIREGUARD_RETRY_ORDER,
18+
Error, GetRelay, IpAvailability, RelaySelector, RuntimeParameters, SelectedObfuscator,
19+
SelectorConfig, WireguardConfig, OPENVPN_RETRY_ORDER, WIREGUARD_RETRY_ORDER,
2020
};
2121
use mullvad_types::{
2222
constraints::Constraint,
@@ -393,8 +393,7 @@ fn test_wireguard_retry_order() {
393393
.get_relay(
394394
retry_attempt,
395395
RuntimeParameters {
396-
ipv4: true,
397-
ipv6: true,
396+
ip_availability: IpAvailability::All,
398397
},
399398
)
400399
.unwrap_or_else(|_| panic!("Retry attempt {retry_attempt} did not yield any relay"));
@@ -457,8 +456,7 @@ fn test_openvpn_retry_order() {
457456
.get_relay(
458457
retry_attempt,
459458
RuntimeParameters {
460-
ipv4: true,
461-
ipv6: true,
459+
ip_availability: IpAvailability::All,
462460
},
463461
)
464462
.unwrap_or_else(|_| panic!("Retry attempt {retry_attempt} did not yield any relay"));
@@ -1645,8 +1643,7 @@ fn test_shadowsocks_runtime_ipv4_unavailable() {
16451643
};
16461644
let relay_selector = RelaySelector::from_list(config, RELAYS.clone());
16471645
let runtime_parameters = RuntimeParameters {
1648-
ipv4: false,
1649-
ipv6: true,
1646+
ip_availability: IpAvailability::Ipv6,
16501647
};
16511648
let user_result = relay_selector.get_relay(0, runtime_parameters).unwrap();
16521649
assert!(
@@ -1676,8 +1673,7 @@ fn test_runtime_ipv4_unavailable() {
16761673
};
16771674
let relay_selector = RelaySelector::from_list(config, RELAYS.clone());
16781675
let runtime_parameters = RuntimeParameters {
1679-
ipv4: false,
1680-
ipv6: true,
1676+
ip_availability: IpAvailability::Ipv6,
16811677
};
16821678
let relay = relay_selector.get_relay(0, runtime_parameters).unwrap();
16831679
match relay {

0 commit comments

Comments
 (0)