Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup talpid tunnel unix module #7411

Merged
merged 4 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 1 addition & 2 deletions talpid-tunnel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = { workspace = true }

[target.'cfg(windows)'.dependencies]
talpid-windows = { path = "../talpid-windows" }
Expand Down
2 changes: 2 additions & 0 deletions talpid-tunnel/src/tun_provider/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,

/// IP addresses for the tunnel interface.
Expand Down Expand Up @@ -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,
Expand Down
42 changes: 9 additions & 33 deletions talpid-tunnel/src/tun_provider/unix.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -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),
Expand Down Expand Up @@ -66,10 +61,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()?
};
Expand Down Expand Up @@ -106,28 +102,21 @@ impl Deref for UnixTun {

/// A tunnel device
pub struct TunnelDevice {
dev: tun::Device,
dev: tun::AsyncDevice,
}

/// A tunnel device builder.
///
/// Call [`Self::create`] to create [`TunnelDevice`] from the config.
#[derive(Default)]
pub struct TunnelDeviceBuilder {
config: Configuration,
}

impl TunnelDeviceBuilder {
/// Create a [`TunnelDevice`] from this builder.
pub fn create(self) -> Result<TunnelDevice, Error> {
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 })
}

Expand All @@ -152,25 +141,12 @@ 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()
}
}

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> {
Expand Down
Loading