diff --git a/vnt/tun/src/android/mod.rs b/vnt/tun/src/android/mod.rs index d1d3f1c..d9c7957 100644 --- a/vnt/tun/src/android/mod.rs +++ b/vnt/tun/src/android/mod.rs @@ -13,6 +13,11 @@ impl Device { Ok(Self { fd: Fd::new(fd)? }) } } +impl Device { + pub fn as_tun_fd(&self) ->&Fd{ + &self.fd + } +} impl IFace for Device { fn version(&self) -> io::Result { Ok(String::new()) diff --git a/vnt/tun/src/macos/device.rs b/vnt/tun/src/macos/device.rs index 5b971af..b423632 100644 --- a/vnt/tun/src/macos/device.rs +++ b/vnt/tun/src/macos/device.rs @@ -224,6 +224,11 @@ impl Device { } } } +impl Device { + pub fn as_tun_fd(&self) ->&Fd{ + &self.tun + } +} impl IFace for Device { fn version(&self) -> io::Result { diff --git a/vnt/tun/src/unix/fd.rs b/vnt/tun/src/unix/fd.rs index c377870..bdb0098 100644 --- a/vnt/tun/src/unix/fd.rs +++ b/vnt/tun/src/unix/fd.rs @@ -1,5 +1,6 @@ use std::io; use std::os::fd::{AsRawFd, IntoRawFd, RawFd}; +use libc::{F_GETFL, F_SETFL, fcntl, O_NONBLOCK}; pub struct Fd(pub RawFd); @@ -10,6 +11,12 @@ impl Fd { } Ok(Fd(value)) } + pub fn set_nonblock(&self) -> io::Result<()> { + match unsafe { fcntl(self.0, F_SETFL, fcntl(self.0, F_GETFL) | O_NONBLOCK) } { + 0 => Ok(()), + _ => Err(io::Error::last_os_error()), + } + } } impl Fd { @@ -51,6 +58,7 @@ impl IntoRawFd for Fd { } } +#[cfg(not(target_os = "android"))] impl Drop for Fd { fn drop(&mut self) { unsafe { diff --git a/vnt/tun/src/unix/mod.rs b/vnt/tun/src/unix/mod.rs index 8d4537a..9d99dda 100644 --- a/vnt/tun/src/unix/mod.rs +++ b/vnt/tun/src/unix/mod.rs @@ -1,6 +1,7 @@ mod fd; pub use fd::Fd; +#[cfg(any(target_os = "macos", target_os = "linux"))] use std::process::Output; #[cfg(any(target_os = "macos", target_os = "linux"))] mod sockaddr;