From 5a9e31bdedce33787e7b32dc00f7a7716ec49ce5 Mon Sep 17 00:00:00 2001 From: lbl8603 <49143209+lbl8603@users.noreply.github.com> Date: Thu, 9 May 2024 20:11:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=BC=82=E6=AD=A5tun?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnt/tun/src/android/mod.rs | 5 +++++ vnt/tun/src/macos/device.rs | 5 +++++ vnt/tun/src/unix/fd.rs | 8 ++++++++ vnt/tun/src/unix/mod.rs | 1 + 4 files changed, 19 insertions(+) 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;