Skip to content

Commit

Permalink
Remove TunConfig default implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
dlon committed Aug 12, 2024
1 parent cdd7476 commit 0dbfd3c
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 37 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions talpid-core/src/tunnel_state_machine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ pub async fn spawn(
let tun_provider = TunProvider::new(
#[cfg(target_os = "android")]
android_context.clone(),
talpid_tunnel::tun_provider::blocking_config(),
);

let (shutdown_tx, shutdown_rx) = oneshot::channel();
Expand Down
1 change: 1 addition & 0 deletions talpid-tunnel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ rust-version.workspace = true
workspace = true

[dependencies]
once_cell = { workspace = true }
thiserror = { workspace = true }
cfg-if = "1.0"
ipnetwork = { workspace = true }
Expand Down
4 changes: 2 additions & 2 deletions talpid-tunnel/src/tun_provider/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub struct AndroidTunProvider {

impl AndroidTunProvider {
/// Create a new AndroidTunProvider interfacing with Android's VpnService.
pub fn new(context: AndroidContext) -> Self {
pub fn new(context: AndroidContext, config: TunConfig) -> Self {
let env = JnixEnv::from(
context
.jvm
Expand All @@ -73,7 +73,7 @@ impl AndroidTunProvider {
jvm: context.jvm,
class: talpid_vpn_service_class,
object: context.vpn_service,
config: TunConfig::default(),
config,
}
}

Expand Down
42 changes: 25 additions & 17 deletions talpid-tunnel/src/tun_provider/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use cfg_if::cfg_if;
use ipnetwork::IpNetwork;
use once_cell::sync::Lazy;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

cfg_if! {
Expand Down Expand Up @@ -68,22 +69,29 @@ impl TunConfig {
}
}

impl Default for TunConfig {
fn default() -> Self {
TunConfig {
addresses: vec![IpAddr::V4(Ipv4Addr::new(10, 0, 0, 1))],
mtu: 1380,
ipv4_gateway: Ipv4Addr::new(10, 64, 0, 1),
ipv6_gateway: None,
routes: vec![
IpNetwork::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 0)
.expect("Invalid IP network prefix for IPv4 address"),
IpNetwork::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)), 0)
.expect("Invalid IP network prefix for IPv6 address"),
],
allow_lan: false,
dns_servers: None,
excluded_packages: vec![],
}
/// Return a tunnel configuration that routes all traffic inside the tunnel.
/// Most values except the routes are nonsensical. This is mostly used as a reasonable default on
/// Android to route all traffic inside the tunnel.
pub fn blocking_config() -> TunConfig {
TunConfig {
addresses: vec![IpAddr::V4(Ipv4Addr::new(10, 0, 0, 1))],
mtu: 1380,
ipv4_gateway: Ipv4Addr::new(10, 64, 0, 1),
ipv6_gateway: None,
routes: DEFAULT_ROUTES.clone(),
allow_lan: false,
dns_servers: None,
excluded_packages: vec![],
}
}

static DEFAULT_ROUTES: Lazy<Vec<IpNetwork>> =
Lazy::new(|| vec![*IPV4_DEFAULT_ROUTE, *IPV6_DEFAULT_ROUTE]);
static IPV4_DEFAULT_ROUTE: Lazy<IpNetwork> = Lazy::new(|| {
IpNetwork::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), 0)
.expect("Invalid IP network prefix for IPv4 address")
});
static IPV6_DEFAULT_ROUTE: Lazy<IpNetwork> = Lazy::new(|| {
IpNetwork::new(IpAddr::V6(Ipv6Addr::UNSPECIFIED), 0)
.expect("Invalid IP network prefix for IPv6 address")
});
10 changes: 2 additions & 8 deletions talpid-tunnel/src/tun_provider/stub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,11 @@ pub enum Error {}
pub struct StubTunProvider;

impl StubTunProvider {
pub fn new() -> Self {
pub fn new(_: TunConfig) -> Self {
StubTunProvider
}

pub fn open_tun(&mut self, _: TunConfig) -> Result<(), Error> {
pub fn open_tun(&mut self) -> Result<(), Error> {
unimplemented!();
}
}

impl Default for StubTunProvider {
fn default() -> Self {
Self::new()
}
}
12 changes: 2 additions & 10 deletions talpid-tunnel/src/tun_provider/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ pub struct UnixTunProvider {
}

impl UnixTunProvider {
pub fn new() -> Self {
UnixTunProvider {
config: TunConfig::default(),
}
pub fn new(config: TunConfig) -> Self {
UnixTunProvider { config }
}

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

impl Default for UnixTunProvider {
fn default() -> Self {
Self::new()
}
}

/// Generic tunnel device.
///
/// Contains the file descriptor representing the device.
Expand Down

0 comments on commit 0dbfd3c

Please sign in to comment.