Skip to content

Commit afd1b7b

Browse files
Enable the async feature of tun
Use `tun::AsyncDevice` instead of hand-rolling an async tunnel device with `tun::Device` + sys calls.
1 parent cb658af commit afd1b7b

File tree

3 files changed

+8
-25
lines changed

3 files changed

+8
-25
lines changed

Cargo.lock

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

talpid-tunnel/Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ jnix = { version = "0.5.1", features = ["derive"] }
2424
log = { workspace = true }
2525

2626
[target.'cfg(any(target_os = "linux", target_os = "macos"))'.dependencies]
27-
tun = "0.7"
28-
nix = "0.23"
27+
tun = { version = "0.7", features = ["async"] }
2928

3029
[target.'cfg(windows)'.dependencies]
3130
talpid-windows = { path = "../talpid-windows" }

talpid-tunnel/src/tun_provider/unix.rs

+3-22
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use super::TunConfig;
2-
use nix::fcntl;
32
#[cfg(target_os = "macos")]
43
use std::io;
54
use std::{
65
net::IpAddr,
76
ops::Deref,
8-
os::unix::io::{AsRawFd, IntoRawFd, RawFd},
7+
os::unix::io::{AsRawFd, RawFd},
98
};
109
use tun::{AbstractDevice, Configuration};
1110

@@ -31,10 +30,6 @@ pub enum Error {
3130
#[error("Unable to open a tunnel device")]
3231
CreateDevice(#[source] tun::Error),
3332

34-
/// Failed to apply async flags to tunnel device
35-
#[error("Failed to apply async flags to tunnel device")]
36-
SetDeviceAsync(#[source] nix::Error),
37-
3833
/// Failed to enable/disable link device
3934
#[error("Failed to enable/disable link device")]
4035
ToggleDevice(#[source] tun::Error),
@@ -107,7 +102,7 @@ impl Deref for UnixTun {
107102

108103
/// A tunnel device
109104
pub struct TunnelDevice {
110-
dev: tun::Device,
105+
dev: tun::AsyncDevice,
111106
}
112107

113108
/// A tunnel device builder.
@@ -121,15 +116,7 @@ pub struct TunnelDeviceBuilder {
121116
impl TunnelDeviceBuilder {
122117
/// Create a [`TunnelDevice`] from this builder.
123118
pub fn create(self) -> Result<TunnelDevice, Error> {
124-
fn apply_async_flags(fd: RawFd) -> Result<(), nix::Error> {
125-
fcntl::fcntl(fd, fcntl::FcntlArg::F_GETFL)?;
126-
let arg = fcntl::FcntlArg::F_SETFL(fcntl::OFlag::O_RDWR | fcntl::OFlag::O_NONBLOCK);
127-
fcntl::fcntl(fd, arg)?;
128-
Ok(())
129-
}
130-
131-
let dev = tun::create(&self.config).map_err(Error::CreateDevice)?;
132-
apply_async_flags(dev.as_raw_fd()).map_err(Error::SetDeviceAsync)?;
119+
let dev = tun::create_as_async(&self.config).map_err(Error::CreateDevice)?;
133120
Ok(TunnelDevice { dev })
134121
}
135122

@@ -160,12 +147,6 @@ impl AsRawFd for TunnelDevice {
160147
}
161148
}
162149

163-
impl IntoRawFd for TunnelDevice {
164-
fn into_raw_fd(self) -> RawFd {
165-
self.dev.into_raw_fd()
166-
}
167-
}
168-
169150
impl TunnelDevice {
170151
#[cfg(target_os = "linux")]
171152
fn set_ip(&mut self, ip: IpAddr) -> Result<(), Error> {

0 commit comments

Comments
 (0)