From 8b31a8417142d1c5a61aed2e080eb13f177b7d87 Mon Sep 17 00:00:00 2001 From: Markus Pettersson Date: Thu, 2 Jan 2025 11:59:52 +0100 Subject: [PATCH 1/4] Derive `Default` for `TunnelDeviceBuilder` --- talpid-tunnel/src/tun_provider/unix.rs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/talpid-tunnel/src/tun_provider/unix.rs b/talpid-tunnel/src/tun_provider/unix.rs index 84ee9ec3d200..b045dad87726 100644 --- a/talpid-tunnel/src/tun_provider/unix.rs +++ b/talpid-tunnel/src/tun_provider/unix.rs @@ -112,6 +112,7 @@ pub struct TunnelDevice { /// A tunnel device builder. /// /// Call [`Self::create`] to create [`TunnelDevice`] from the config. +#[derive(Default)] pub struct TunnelDeviceBuilder { config: Configuration, } @@ -152,13 +153,6 @@ impl TunnelDeviceBuilder { } } -impl Default for TunnelDeviceBuilder { - fn default() -> Self { - let config = Configuration::default(); - Self { config } - } -} - impl AsRawFd for TunnelDevice { fn as_raw_fd(&self) -> RawFd { self.dev.as_raw_fd() From cb658aff39d14f14fda94ef81406e7da7d22ee83 Mon Sep 17 00:00:00 2001 From: Markus Pettersson Date: Thu, 2 Jan 2025 13:21:47 +0100 Subject: [PATCH 2/4] Clean up API for setting tunnel device name --- talpid-tunnel/src/tun_provider/mod.rs | 2 ++ talpid-tunnel/src/tun_provider/unix.rs | 9 +++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/talpid-tunnel/src/tun_provider/mod.rs b/talpid-tunnel/src/tun_provider/mod.rs index 94a82735d183..1bf4e1abb483 100644 --- a/talpid-tunnel/src/tun_provider/mod.rs +++ b/talpid-tunnel/src/tun_provider/mod.rs @@ -35,6 +35,7 @@ cfg_if! { #[derive(Clone, Debug, Eq, PartialEq)] pub struct TunConfig { /// Interface name to use. + #[cfg(target_os = "linux")] pub name: Option, /// IP addresses for the tunnel interface. @@ -80,6 +81,7 @@ impl TunConfig { /// Android to route all traffic inside the tunnel. pub fn blocking_config() -> TunConfig { TunConfig { + #[cfg(target_os = "linux")] name: None, addresses: vec![IpAddr::V4(Ipv4Addr::new(10, 0, 0, 1))], mtu: 1380, diff --git a/talpid-tunnel/src/tun_provider/unix.rs b/talpid-tunnel/src/tun_provider/unix.rs index b045dad87726..6286779ceec8 100644 --- a/talpid-tunnel/src/tun_provider/unix.rs +++ b/talpid-tunnel/src/tun_provider/unix.rs @@ -66,10 +66,11 @@ impl UnixTunProvider { #[allow(unused_mut)] let mut builder = TunnelDeviceBuilder::default(); #[cfg(target_os = "linux")] - builder.enable_packet_information(); - #[cfg(target_os = "linux")] - if let Some(ref name) = self.config.name { - builder.name(name); + { + builder.enable_packet_information(); + if let Some(ref name) = self.config.name { + builder.name(name); + } } builder.create()? }; From afd1b7b9687a8ff895a9180eb705a70c7d04c836 Mon Sep 17 00:00:00 2001 From: Markus Pettersson Date: Thu, 2 Jan 2025 13:43:58 +0100 Subject: [PATCH 3/4] Enable the `async` feature of `tun` Use `tun::AsyncDevice` instead of hand-rolling an async tunnel device with `tun::Device` + sys calls. --- Cargo.lock | 5 ++++- talpid-tunnel/Cargo.toml | 3 +-- talpid-tunnel/src/tun_provider/unix.rs | 25 +++---------------------- 3 files changed, 8 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6f27b9917d98..5ad9214f1b6a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4644,7 +4644,6 @@ dependencies = [ "ipnetwork", "jnix", "log", - "nix 0.23.2", "talpid-routing", "talpid-types", "talpid-windows", @@ -5178,11 +5177,15 @@ checksum = "5b5ea2466ffcdd0be0831f7d3981daa0b953586c0062f6d33398cb374689b090" dependencies = [ "bytes", "cfg-if", + "futures", + "futures-core", "ipnet", "libc", "log", "nix 0.29.0", "thiserror 2.0.9", + "tokio", + "tokio-util 0.7.10", "windows-sys 0.59.0", "wintun-bindings", ] diff --git a/talpid-tunnel/Cargo.toml b/talpid-tunnel/Cargo.toml index 2b91b142e62a..a3400889e041 100644 --- a/talpid-tunnel/Cargo.toml +++ b/talpid-tunnel/Cargo.toml @@ -24,8 +24,7 @@ jnix = { version = "0.5.1", features = ["derive"] } log = { workspace = true } [target.'cfg(any(target_os = "linux", target_os = "macos"))'.dependencies] -tun = "0.7" -nix = "0.23" +tun = { version = "0.7", features = ["async"] } [target.'cfg(windows)'.dependencies] talpid-windows = { path = "../talpid-windows" } diff --git a/talpid-tunnel/src/tun_provider/unix.rs b/talpid-tunnel/src/tun_provider/unix.rs index 6286779ceec8..1d7441ed2edb 100644 --- a/talpid-tunnel/src/tun_provider/unix.rs +++ b/talpid-tunnel/src/tun_provider/unix.rs @@ -1,11 +1,10 @@ use super::TunConfig; -use nix::fcntl; #[cfg(target_os = "macos")] use std::io; use std::{ net::IpAddr, ops::Deref, - os::unix::io::{AsRawFd, IntoRawFd, RawFd}, + os::unix::io::{AsRawFd, RawFd}, }; use tun::{AbstractDevice, Configuration}; @@ -31,10 +30,6 @@ pub enum Error { #[error("Unable to open a tunnel device")] CreateDevice(#[source] tun::Error), - /// Failed to apply async flags to tunnel device - #[error("Failed to apply async flags to tunnel device")] - SetDeviceAsync(#[source] nix::Error), - /// Failed to enable/disable link device #[error("Failed to enable/disable link device")] ToggleDevice(#[source] tun::Error), @@ -107,7 +102,7 @@ impl Deref for UnixTun { /// A tunnel device pub struct TunnelDevice { - dev: tun::Device, + dev: tun::AsyncDevice, } /// A tunnel device builder. @@ -121,15 +116,7 @@ pub struct TunnelDeviceBuilder { impl TunnelDeviceBuilder { /// Create a [`TunnelDevice`] from this builder. pub fn create(self) -> Result { - fn apply_async_flags(fd: RawFd) -> Result<(), nix::Error> { - fcntl::fcntl(fd, fcntl::FcntlArg::F_GETFL)?; - let arg = fcntl::FcntlArg::F_SETFL(fcntl::OFlag::O_RDWR | fcntl::OFlag::O_NONBLOCK); - fcntl::fcntl(fd, arg)?; - Ok(()) - } - - let dev = tun::create(&self.config).map_err(Error::CreateDevice)?; - apply_async_flags(dev.as_raw_fd()).map_err(Error::SetDeviceAsync)?; + let dev = tun::create_as_async(&self.config).map_err(Error::CreateDevice)?; Ok(TunnelDevice { dev }) } @@ -160,12 +147,6 @@ impl AsRawFd for TunnelDevice { } } -impl IntoRawFd for TunnelDevice { - fn into_raw_fd(self) -> RawFd { - self.dev.into_raw_fd() - } -} - impl TunnelDevice { #[cfg(target_os = "linux")] fn set_ip(&mut self, ip: IpAddr) -> Result<(), Error> { From 8713e93ae51245da487b7e46ed8d09c2b47a5fbe Mon Sep 17 00:00:00 2001 From: Markus Pettersson Date: Thu, 2 Jan 2025 14:08:02 +0100 Subject: [PATCH 4/4] Promote `tun` to a workspace dependency --- Cargo.toml | 1 + talpid-tunnel/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index fb239fa091c8..4f9828d603c9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -137,6 +137,7 @@ serde = "1.0.204" serde_json = "1.0.122" ipnetwork = "0.20" +tun = { version = "0.7", features = ["async"] } # Test dependencies proptest = "1.4" diff --git a/talpid-tunnel/Cargo.toml b/talpid-tunnel/Cargo.toml index a3400889e041..542e30fb0ffd 100644 --- a/talpid-tunnel/Cargo.toml +++ b/talpid-tunnel/Cargo.toml @@ -24,7 +24,7 @@ jnix = { version = "0.5.1", features = ["derive"] } log = { workspace = true } [target.'cfg(any(target_os = "linux", target_os = "macos"))'.dependencies] -tun = { version = "0.7", features = ["async"] } +tun = { workspace = true } [target.'cfg(windows)'.dependencies] talpid-windows = { path = "../talpid-windows" }