@@ -181,32 +181,64 @@ pub struct AdditionalWireguardConstraints {
181
181
/// Values which affect the choice of relay but are only known at runtime.
182
182
#[ derive( Clone , Debug ) ]
183
183
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 ,
188
186
}
189
187
190
188
impl RuntimeParameters {
191
189
/// Return whether a given [query][`RelayQuery`] is valid given the current runtime parameters
192
190
pub fn compatible ( & self , query : & RelayQuery ) -> bool {
193
191
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!" )
197
213
}
198
214
}
199
215
}
200
216
201
217
impl Default for RuntimeParameters {
202
218
fn default ( ) -> Self {
203
219
RuntimeParameters {
204
- ipv4 : true ,
205
- ipv6 : false ,
220
+ ip_availability : IpAvailability :: Ipv4 ,
206
221
}
207
222
}
208
223
}
209
224
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
+
210
242
/// This enum exists to separate the two types of [`SelectorConfig`] that exists.
211
243
///
212
244
/// The first one is a "regular" config, where [`SelectorConfig::relay_settings`] is
@@ -1184,10 +1216,10 @@ impl RelaySelector {
1184
1216
fn resolve_valid_ip_version ( query : & RelayQuery , runtime_params : & RuntimeParameters ) -> RelayQuery {
1185
1217
let mut wireguard_constraints = query. wireguard_constraints ( ) . clone ( ) ;
1186
1218
if wireguard_constraints. ip_version . is_any ( ) {
1187
- if runtime_params. ipv4 && !runtime_params . ipv6 {
1219
+ if runtime_params. ip_availability == IpAvailability :: Ipv4 {
1188
1220
wireguard_constraints. ip_version = Constraint :: Only ( talpid_types:: net:: IpVersion :: V4 )
1189
1221
}
1190
- if runtime_params. ipv6 && !runtime_params . ipv4 {
1222
+ if runtime_params. ip_availability == IpAvailability :: Ipv6 {
1191
1223
wireguard_constraints. ip_version = Constraint :: Only ( talpid_types:: net:: IpVersion :: V6 )
1192
1224
}
1193
1225
}
0 commit comments