Share *RawFd trait implementations on Unix

They were previously only present on Apple platforms.
This commit is contained in:
Conrad Kramer 2023-05-08 18:23:38 -04:00
parent c1507ba37f
commit cf95ac819c
2 changed files with 24 additions and 15 deletions

View file

@ -1,11 +1,11 @@
use fehler::throws; use fehler::throws;
use libc::c_char; use libc::c_char;
use std::net::Ipv4Addr;
use std::os::unix::io::{AsRawFd, IntoRawFd, RawFd};
use std::io::Error; use std::io::Error;
use std::net::Ipv4Addr;
use std::os::fd::AsRawFd;
mod sys;
mod kern_control; mod kern_control;
mod sys;
pub use super::queue::TunQueue; pub use super::queue::TunQueue;
@ -62,15 +62,3 @@ impl TunInterface {
todo!() todo!()
} }
} }
impl AsRawFd for TunInterface {
fn as_raw_fd(&self) -> RawFd {
self.socket.as_raw_fd()
}
}
impl IntoRawFd for TunInterface {
fn into_raw_fd(self) -> RawFd {
self.socket.into_raw_fd()
}
}

View file

@ -1,3 +1,5 @@
use std::os::fd::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
mod queue; mod queue;
#[cfg(target_vendor = "apple")] #[cfg(target_vendor = "apple")]
@ -11,6 +13,25 @@ mod imp;
pub use imp::TunInterface; pub use imp::TunInterface;
pub use queue::TunQueue; pub use queue::TunQueue;
impl AsRawFd for TunInterface {
fn as_raw_fd(&self) -> RawFd {
self.socket.as_raw_fd()
}
}
impl FromRawFd for TunInterface {
unsafe fn from_raw_fd(fd: RawFd) -> TunInterface {
TunInterface {
socket: socket2::Socket::from_raw_fd(fd),
}
}
}
impl IntoRawFd for TunInterface {
fn into_raw_fd(self) -> RawFd {
self.socket.into_raw_fd()
}
}
pub fn ifname_to_string(buf: [libc::c_char; libc::IFNAMSIZ]) -> String { pub fn ifname_to_string(buf: [libc::c_char; libc::IFNAMSIZ]) -> String {
// TODO: Switch to `CStr::from_bytes_until_nul` when stabilized // TODO: Switch to `CStr::from_bytes_until_nul` when stabilized
unsafe { unsafe {