Skip to content

Commit 0dbfd3c

Browse files
committed
Remove TunConfig default implementation
1 parent cdd7476 commit 0dbfd3c

File tree

7 files changed

+34
-37
lines changed

7 files changed

+34
-37
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

talpid-core/src/tunnel_state_machine/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ pub async fn spawn(
136136
let tun_provider = TunProvider::new(
137137
#[cfg(target_os = "android")]
138138
android_context.clone(),
139+
talpid_tunnel::tun_provider::blocking_config(),
139140
);
140141

141142
let (shutdown_tx, shutdown_rx) = oneshot::channel();

talpid-tunnel/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ rust-version.workspace = true
1111
workspace = true
1212

1313
[dependencies]
14+
once_cell = { workspace = true }
1415
thiserror = { workspace = true }
1516
cfg-if = "1.0"
1617
ipnetwork = { workspace = true }

talpid-tunnel/src/tun_provider/android/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub struct AndroidTunProvider {
6060

6161
impl AndroidTunProvider {
6262
/// Create a new AndroidTunProvider interfacing with Android's VpnService.
63-
pub fn new(context: AndroidContext) -> Self {
63+
pub fn new(context: AndroidContext, config: TunConfig) -> Self {
6464
let env = JnixEnv::from(
6565
context
6666
.jvm
@@ -73,7 +73,7 @@ impl AndroidTunProvider {
7373
jvm: context.jvm,
7474
class: talpid_vpn_service_class,
7575
object: context.vpn_service,
76-
config: TunConfig::default(),
76+
config,
7777
}
7878
}
7979

talpid-tunnel/src/tun_provider/mod.rs

+25-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use cfg_if::cfg_if;
22
use ipnetwork::IpNetwork;
3+
use once_cell::sync::Lazy;
34
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
45

56
cfg_if! {
@@ -68,22 +69,29 @@ impl TunConfig {
6869
}
6970
}
7071

71-
impl Default for TunConfig {
72-
fn default() -> Self {
73-
TunConfig {
74-
addresses: vec![IpAddr::V4(Ipv4Addr::new(10, 0, 0, 1))],
75-
mtu: 1380,
76-
ipv4_gateway: Ipv4Addr::new(10, 64, 0, 1),
77-
ipv6_gateway: None,
78-
routes: vec![
79-
IpNetwork::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 0)
80-
.expect("Invalid IP network prefix for IPv4 address"),
81-
IpNetwork::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)), 0)
82-
.expect("Invalid IP network prefix for IPv6 address"),
83-
],
84-
allow_lan: false,
85-
dns_servers: None,
86-
excluded_packages: vec![],
87-
}
72+
/// Return a tunnel configuration that routes all traffic inside the tunnel.
73+
/// Most values except the routes are nonsensical. This is mostly used as a reasonable default on
74+
/// Android to route all traffic inside the tunnel.
75+
pub fn blocking_config() -> TunConfig {
76+
TunConfig {
77+
addresses: vec![IpAddr::V4(Ipv4Addr::new(10, 0, 0, 1))],
78+
mtu: 1380,
79+
ipv4_gateway: Ipv4Addr::new(10, 64, 0, 1),
80+
ipv6_gateway: None,
81+
routes: DEFAULT_ROUTES.clone(),
82+
allow_lan: false,
83+
dns_servers: None,
84+
excluded_packages: vec![],
8885
}
8986
}
87+
88+
static DEFAULT_ROUTES: Lazy<Vec<IpNetwork>> =
89+
Lazy::new(|| vec![*IPV4_DEFAULT_ROUTE, *IPV6_DEFAULT_ROUTE]);
90+
static IPV4_DEFAULT_ROUTE: Lazy<IpNetwork> = Lazy::new(|| {
91+
IpNetwork::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), 0)
92+
.expect("Invalid IP network prefix for IPv4 address")
93+
});
94+
static IPV6_DEFAULT_ROUTE: Lazy<IpNetwork> = Lazy::new(|| {
95+
IpNetwork::new(IpAddr::V6(Ipv6Addr::UNSPECIFIED), 0)
96+
.expect("Invalid IP network prefix for IPv6 address")
97+
});

talpid-tunnel/src/tun_provider/stub.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,11 @@ pub enum Error {}
77
pub struct StubTunProvider;
88

99
impl StubTunProvider {
10-
pub fn new() -> Self {
10+
pub fn new(_: TunConfig) -> Self {
1111
StubTunProvider
1212
}
1313

14-
pub fn open_tun(&mut self, _: TunConfig) -> Result<(), Error> {
14+
pub fn open_tun(&mut self) -> Result<(), Error> {
1515
unimplemented!();
1616
}
1717
}
18-
19-
impl Default for StubTunProvider {
20-
fn default() -> Self {
21-
Self::new()
22-
}
23-
}

talpid-tunnel/src/tun_provider/unix.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ pub struct UnixTunProvider {
3030
}
3131

3232
impl UnixTunProvider {
33-
pub fn new() -> Self {
34-
UnixTunProvider {
35-
config: TunConfig::default(),
36-
}
33+
pub fn new(config: TunConfig) -> Self {
34+
UnixTunProvider { config }
3735
}
3836

3937
/// Get the current tunnel config. Note that the tunnel must be recreated for any changes to
@@ -58,12 +56,6 @@ impl UnixTunProvider {
5856
}
5957
}
6058

61-
impl Default for UnixTunProvider {
62-
fn default() -> Self {
63-
Self::new()
64-
}
65-
}
66-
6759
/// Generic tunnel device.
6860
///
6961
/// Contains the file descriptor representing the device.

0 commit comments

Comments
 (0)