Skip to content

Commit

Permalink
Enable the async feature of tun
Browse files Browse the repository at this point in the history
Use `tun::AsyncDevice` instead of hand-rolling an async tunnel device
with `tun::Device` + sys calls.
  • Loading branch information
MarkusPettersson98 committed Jan 2, 2025
1 parent 4ca7f6b commit 7a5fc10
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 17 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion talpid-tunnel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jnix = { version = "0.5.1", features = ["derive"] }
log = { workspace = true }

[target.'cfg(any(target_os = "linux", target_os = "macos"))'.dependencies]
tun = "0.7"
tun = { version = "0.7", features = ["async"] }

[target.'cfg(windows)'.dependencies]
talpid-windows = { path = "../talpid-windows" }
Expand Down
18 changes: 2 additions & 16 deletions talpid-tunnel/src/tun_provider/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl Deref for UnixTun {

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

/// A tunnel device builder.
Expand All @@ -125,15 +125,7 @@ pub struct TunnelDeviceBuilder {
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 Down Expand Up @@ -164,12 +156,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> {
Expand Down

0 comments on commit 7a5fc10

Please sign in to comment.