支持异步tun

This commit is contained in:
lbl8603
2024-05-09 20:11:08 +08:00
parent b61c140c24
commit 5a9e31bded
4 changed files with 19 additions and 0 deletions
+5
View File
@@ -13,6 +13,11 @@ impl Device {
Ok(Self { fd: Fd::new(fd)? }) Ok(Self { fd: Fd::new(fd)? })
} }
} }
impl Device {
pub fn as_tun_fd(&self) ->&Fd{
&self.fd
}
}
impl IFace for Device { impl IFace for Device {
fn version(&self) -> io::Result<String> { fn version(&self) -> io::Result<String> {
Ok(String::new()) Ok(String::new())
+5
View File
@@ -224,6 +224,11 @@ impl Device {
} }
} }
} }
impl Device {
pub fn as_tun_fd(&self) ->&Fd{
&self.tun
}
}
impl IFace for Device { impl IFace for Device {
fn version(&self) -> io::Result<String> { fn version(&self) -> io::Result<String> {
+8
View File
@@ -1,5 +1,6 @@
use std::io; use std::io;
use std::os::fd::{AsRawFd, IntoRawFd, RawFd}; use std::os::fd::{AsRawFd, IntoRawFd, RawFd};
use libc::{F_GETFL, F_SETFL, fcntl, O_NONBLOCK};
pub struct Fd(pub RawFd); pub struct Fd(pub RawFd);
@@ -10,6 +11,12 @@ impl Fd {
} }
Ok(Fd(value)) 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 { impl Fd {
@@ -51,6 +58,7 @@ impl IntoRawFd for Fd {
} }
} }
#[cfg(not(target_os = "android"))]
impl Drop for Fd { impl Drop for Fd {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe {
+1
View File
@@ -1,6 +1,7 @@
mod fd; mod fd;
pub use fd::Fd; pub use fd::Fd;
#[cfg(any(target_os = "macos", target_os = "linux"))]
use std::process::Output; use std::process::Output;
#[cfg(any(target_os = "macos", target_os = "linux"))] #[cfg(any(target_os = "macos", target_os = "linux"))]
mod sockaddr; mod sockaddr;