From 4bd95d22dc3b100c446641d3b0fb68aff74e96b8 Mon Sep 17 00:00:00 2001 From: Aleksandr Morozov Date: Mon, 28 Oct 2024 01:30:21 +0100 Subject: [PATCH 1/5] 1) Replaced RawFd with OwnedFd in order to be used with nix crate. 2) Updated crates version. 3)Removed unused deps --- Cargo.toml | 6 +- src/helpers.rs | 1 + src/seqpacket.rs | 156 +++++++++++++++++++++++------------------ src/tokio/seqpacket.rs | 18 ++++- 4 files changed, 110 insertions(+), 71 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f4458a3..42617c7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,16 +14,16 @@ edition = "2021" exclude = ["tests", "src/bin", ".vscode"] [target."cfg(unix)".dependencies] -libc = "0.2.90" # peer credentials for DragonFly BSD and NetBSD, SO_PEERSEC on all Linux architectures +libc = "0.2.161" # peer credentials for DragonFly BSD and NetBSD, SO_PEERSEC on all Linux architectures # enabling this feature implements the extension traits for mio 0.8's unix socket types # and Source for this crate's non-blocking seqpacket types. mio_08 = { package = "mio", version = "0.8", features = ["os-ext", "net"], optional = true } # .28 for AsyncFd.async_io() helper -tokio = { version = "1.28", features = ["net"], optional=true } +tokio = { version = "1.41", features = ["net"], optional=true } [target."cfg(unix)".dev-dependencies] # rt-multi-thread is required for #[tokio::main] in doctests -tokio = { version = "1.28", features = ["io-util", "macros", "rt", 'rt-multi-thread'] } +tokio = { version = "1.41", features = ["io-util", "macros", "rt", 'rt-multi-thread'] } [package.metadata.docs.rs] features = ["mio_08", "tokio"] diff --git a/src/helpers.rs b/src/helpers.rs index 9639cf9..963696a 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -234,6 +234,7 @@ impl AsRawFd for Socket { } } + impl Socket { /// Enable / disable Apple-only SO_NOSIGPIPE fn set_nosigpipe(&self, nosigpipe: bool) -> Result<(), io::Error> { diff --git a/src/seqpacket.rs b/src/seqpacket.rs index 4b0dd81..cdb5c4a 100644 --- a/src/seqpacket.rs +++ b/src/seqpacket.rs @@ -1,11 +1,11 @@ use std::io::{self, ErrorKind, IoSlice, IoSliceMut}; -use std::mem; use std::net::Shutdown; -use std::os::unix::io::{RawFd, FromRawFd, AsRawFd, IntoRawFd}; +use std::os::fd::{OwnedFd, BorrowedFd}; +use std::os::unix::io::{AsFd, RawFd, FromRawFd, AsRawFd, IntoRawFd}; use std::path::Path; use std::time::Duration; -use libc::{SOCK_SEQPACKET, MSG_EOR, MSG_PEEK, c_void, close, send, recv}; +use libc::{SOCK_SEQPACKET, MSG_EOR, MSG_PEEK, c_void, send, recv}; #[cfg(feature = "mio_08")] use mio_08::{event::Source as Source_08, unix::SourceFd as SourceFd_08, Interest as Interest_08, Registry as Registry_08, Token as Token_08}; @@ -19,25 +19,33 @@ use crate::credentials::*; macro_rules! impl_rawfd_traits {($type:tt) => { impl FromRawFd for $type { unsafe fn from_raw_fd(fd: RawFd) -> Self { - $type { fd } + $type { fd: OwnedFd::from_raw_fd(fd) } } } impl AsRawFd for $type { fn as_raw_fd(&self) -> RawFd { - self.fd + self.fd.as_raw_fd() } } impl IntoRawFd for $type { fn into_raw_fd(self) -> RawFd { - let fd = self.fd; - mem::forget(self); + let fd = self.fd.into_raw_fd(); + //mem::forget(self); fd } } + /* OwnedFd closes the file descriptor on drop. It is guaranteed that nobody else will close the file descriptor. impl Drop for $type { fn drop(&mut self) { let _ = unsafe { close(self.fd) }; } + }*/ + impl AsFd for $type + { + fn as_fd(&self) -> BorrowedFd<'_> + { + self.fd.as_fd() + } } }} @@ -154,7 +162,7 @@ macro_rules! impl_mio_if_enabled {($type:tt) => { #[derive(Debug)] #[repr(transparent)] pub struct UnixSeqpacketConn { - fd: RawFd, + fd: OwnedFd, } impl_rawfd_traits!{UnixSeqpacketConn} @@ -172,7 +180,7 @@ impl UnixSeqpacketConn { pub fn connect_unix_addr(addr: &UnixSocketAddr) -> Result { let socket = Socket::new(SOCK_SEQPACKET, false)?; set_unix_addr(socket.as_raw_fd(), SetAddr::PEER, addr)?; - Ok(UnixSeqpacketConn { fd: socket.into_raw_fd() }) + Ok(UnixSeqpacketConn { fd: unsafe { OwnedFd::from_raw_fd(socket.into_raw_fd()) } }) } /// Binds to an address before connecting to a listening seqpacet socket. pub fn connect_from_to_unix_addr(from: &UnixSocketAddr, to: &UnixSocketAddr) @@ -180,7 +188,7 @@ impl UnixSeqpacketConn { let socket = Socket::new(SOCK_SEQPACKET, false)?; set_unix_addr(socket.as_raw_fd(), SetAddr::LOCAL, from)?; set_unix_addr(socket.as_raw_fd(), SetAddr::PEER, to)?; - Ok(UnixSeqpacketConn { fd: socket.into_raw_fd() }) + Ok(UnixSeqpacketConn { fd: unsafe { OwnedFd::from_raw_fd(socket.into_raw_fd()) } }) } /// Creates a pair of unix-domain seqpacket conneections connected to each other. @@ -198,24 +206,24 @@ impl UnixSeqpacketConn { /// ``` pub fn pair() -> Result<(Self, Self), io::Error> { let (a, b) = Socket::pair(SOCK_SEQPACKET, false)?; - let a = UnixSeqpacketConn { fd: a.into_raw_fd() }; - let b = UnixSeqpacketConn { fd: b.into_raw_fd() }; + let a = UnixSeqpacketConn { fd: unsafe { OwnedFd::from_raw_fd(a.into_raw_fd()) } }; + let b = UnixSeqpacketConn { fd: unsafe { OwnedFd::from_raw_fd(b.into_raw_fd()) } }; Ok((a, b)) } /// Returns the address of this side of the connection. pub fn local_unix_addr(&self) -> Result { - get_unix_addr(self.fd, GetAddr::LOCAL) + get_unix_addr(self.fd.as_raw_fd(), GetAddr::LOCAL) } /// Returns the address of the other side of the connection. pub fn peer_unix_addr(&self) -> Result { - get_unix_addr(self.fd, GetAddr::PEER) + get_unix_addr(self.fd.as_raw_fd(), GetAddr::PEER) } /// Returns information about the process of the peer when the connection was established. /// /// See documentation of the returned type for details. pub fn initial_peer_credentials(&self) -> Result { - peer_credentials(self.fd) + peer_credentials(self.fd.as_raw_fd()) } /// Returns the SELinux security context of the process that created the other /// end of this connection. @@ -235,13 +243,21 @@ impl UnixSeqpacketConn { pub fn send(&self, packet: &[u8]) -> Result { let ptr = packet.as_ptr() as *const c_void; let flags = MSG_NOSIGNAL | MSG_EOR; - let sent = cvt_r!(unsafe { send(self.fd, ptr, packet.len(), flags) })?; + let sent = cvt_r!(unsafe { send(self.fd.as_raw_fd(), ptr, packet.len(), flags) })?; + Ok(sent as usize) + } + + /// Sends a packet to the peer. + pub fn send_flags(&self, packet: &[u8], flags: i32) -> Result { + let ptr = packet.as_ptr() as *const c_void; + let sent = cvt_r!(unsafe { send(self.fd.as_raw_fd(), ptr, packet.len(), flags) })?; Ok(sent as usize) } + /// Receives a packet from the peer. pub fn recv(&self, buffer: &mut[u8]) -> Result { let ptr = buffer.as_ptr() as *mut c_void; - let received = cvt_r!(unsafe { recv(self.fd, ptr, buffer.len(), MSG_NOSIGNAL) })?; + let received = cvt_r!(unsafe { recv(self.fd.as_raw_fd(), ptr, buffer.len(), MSG_NOSIGNAL) })?; Ok(received as usize) } /// Sends a packet assembled from multiple byte slices. @@ -257,18 +273,18 @@ impl UnixSeqpacketConn { /// too short buffers. pub fn recv_vectored(&self, buffers: &mut[IoSliceMut]) -> Result<(usize, bool), io::Error> { - recv_ancillary(self.fd, None, 0, buffers, &mut[]) + recv_ancillary(self.fd.as_raw_fd(), None, 0, buffers, &mut[]) .map(|(bytes, ancillary)| (bytes, ancillary.message_truncated()) ) } /// Sends a packet with associated file descriptors. pub fn send_fds(&self, bytes: &[u8], fds: &[RawFd]) -> Result { - send_ancillary(self.fd, None, MSG_EOR, &[IoSlice::new(bytes)], fds, None) + send_ancillary(self.fd.as_raw_fd(), None, MSG_EOR, &[IoSlice::new(bytes)], fds, None) } /// Receives a packet and associated file descriptors. pub fn recv_fds(&self, byte_buffer: &mut[u8], fd_buffer: &mut[RawFd]) -> Result<(usize, bool, usize), io::Error> { - recv_fds(self.fd, None, &mut[IoSliceMut::new(byte_buffer)], fd_buffer) + recv_fds(self.fd.as_raw_fd(), None, &mut[IoSliceMut::new(byte_buffer)], fd_buffer) } /// Receives a packet without removing it from the incoming queue. /// @@ -289,7 +305,7 @@ impl UnixSeqpacketConn { pub fn peek(&self, buffer: &mut[u8]) -> Result { let ptr = buffer.as_ptr() as *mut c_void; let flags = MSG_NOSIGNAL | MSG_PEEK; - let received = cvt_r!(unsafe { recv(self.fd, ptr, buffer.len(), flags) })?; + let received = cvt_r!(unsafe { recv(self.fd.as_raw_fd(), ptr, buffer.len(), flags) })?; Ok(received as usize) } /// Receives a packet without removing it from the incoming queue. @@ -298,7 +314,7 @@ impl UnixSeqpacketConn { /// the combined buffers being too small. pub fn peek_vectored(&self, buffers: &mut[IoSliceMut]) -> Result<(usize, bool), io::Error> { - recv_ancillary(self.fd, None, MSG_PEEK, buffers, &mut[]) + recv_ancillary(self.fd.as_raw_fd(), None, MSG_PEEK, buffers, &mut[]) .map(|(bytes, ancillary)| (bytes, ancillary.message_truncated()) ) } @@ -319,7 +335,7 @@ impl UnixSeqpacketConn { /// assert!(a.take_error().unwrap().is_none()); /// ``` pub fn take_error(&self) -> Result, io::Error> { - take_error(self.fd) + take_error(self.fd.as_raw_fd()) } @@ -363,8 +379,8 @@ impl UnixSeqpacketConn { /// assert_eq!(b2.recv(&mut[0; 10]).unwrap(), "hello".len()); /// ``` pub fn try_clone(&self) -> Result { - let cloned = Socket::try_clone_from(self.fd)?; - Ok(UnixSeqpacketConn { fd: cloned.into_raw_fd() }) + let cloned = Socket::try_clone_from(self.fd.as_raw_fd())?; + Ok(UnixSeqpacketConn { fd: unsafe { OwnedFd::from_raw_fd(cloned.into_raw_fd() )} }) } /// Sets the read timeout to the duration specified. @@ -396,7 +412,7 @@ impl UnixSeqpacketConn { /// ``` pub fn set_read_timeout(&self, timeout: Option) -> Result<(), io::Error> { - set_timeout(self.fd, TimeoutDirection::READ, timeout) + set_timeout(self.fd.as_raw_fd(), TimeoutDirection::READ, timeout) } /// Returns the read timeout of this socket. /// @@ -418,7 +434,7 @@ impl UnixSeqpacketConn { /// assert_eq!(conn.read_timeout().unwrap(), timeout); /// ``` pub fn read_timeout(&self) -> Result, io::Error> { - get_timeout(self.fd, TimeoutDirection::READ) + get_timeout(self.fd.as_raw_fd(), TimeoutDirection::READ) } /// Sets the write timeout to the duration specified. /// @@ -450,7 +466,7 @@ impl UnixSeqpacketConn { /// ``` pub fn set_write_timeout(&self, timeout: Option) -> Result<(), io::Error> { - set_timeout(self.fd, TimeoutDirection::WRITE, timeout) + set_timeout(self.fd.as_raw_fd(), TimeoutDirection::WRITE, timeout) } /// Returns the write timeout of this socket. /// @@ -464,7 +480,7 @@ impl UnixSeqpacketConn { /// assert!(conn.write_timeout().unwrap().is_none()); /// ``` pub fn write_timeout(&self) -> Result, io::Error> { - get_timeout(self.fd, TimeoutDirection::WRITE) + get_timeout(self.fd.as_raw_fd(), TimeoutDirection::WRITE) } /// Enables or disables nonblocking mode. @@ -501,7 +517,7 @@ impl UnixSeqpacketConn { /// } /// ``` pub fn set_nonblocking(&self, nonblocking: bool) -> Result<(), io::Error> { - set_nonblocking(self.fd, nonblocking) + set_nonblocking(self.fd.as_raw_fd(), nonblocking) } /// Shuts down the read, write, or both halves of this connection. @@ -538,7 +554,7 @@ impl UnixSeqpacketConn { #[derive(Debug)] #[repr(transparent)] pub struct UnixSeqpacketListener { - fd: RawFd + fd: OwnedFd } impl_rawfd_traits!{UnixSeqpacketListener} impl UnixSeqpacketListener { @@ -552,19 +568,19 @@ impl UnixSeqpacketListener { let socket = Socket::new(SOCK_SEQPACKET, false)?; set_unix_addr(socket.as_raw_fd(), SetAddr::LOCAL, addr)?; socket.start_listening()?; - Ok(UnixSeqpacketListener { fd: socket.into_raw_fd() }) + Ok(UnixSeqpacketListener { fd: unsafe { OwnedFd::from_raw_fd(socket.into_raw_fd()) } }) } /// Returns the address the socket is listening on. pub fn local_unix_addr(&self) -> Result { - get_unix_addr(self.fd, GetAddr::LOCAL) + get_unix_addr(self.fd.as_raw_fd(), GetAddr::LOCAL) } /// Accepts a new incoming connection to this listener. pub fn accept_unix_addr(&self) -> Result<(UnixSeqpacketConn, UnixSocketAddr), io::Error> { - let (socket, addr) = Socket::accept_from(self.fd, false)?; - let conn = UnixSeqpacketConn { fd: socket.into_raw_fd() }; + let (socket, addr) = Socket::accept_from(self.fd.as_raw_fd(), false)?; + let conn = UnixSeqpacketConn { fd: unsafe { OwnedFd::from_raw_fd(socket.into_raw_fd()) } }; Ok((conn, addr)) } @@ -574,13 +590,13 @@ impl UnixSeqpacketListener { /// unlikely to be useful, but is provided for parity with /// `std::unix::net::UnixListener`. pub fn take_error(&self) -> Result, io::Error> { - take_error(self.fd) + take_error(self.fd.as_raw_fd()) } /// Creates a new file descriptor listening for the same connections. pub fn try_clone(&self) -> Result { - let cloned = Socket::try_clone_from(self.fd)?; - Ok(UnixSeqpacketListener { fd: cloned.into_raw_fd() }) + let cloned = Socket::try_clone_from(self.fd.as_raw_fd())?; + Ok(UnixSeqpacketListener { fd: unsafe { OwnedFd::from_raw_fd(cloned.into_raw_fd()) } }) } /// Sets a maximum duration to wait in a single `accept()` on this socket. @@ -613,7 +629,7 @@ impl UnixSeqpacketListener { /// ``` pub fn set_timeout(&self, timeout: Option) -> Result<(), io::Error> { - match set_timeout(self.fd, TimeoutDirection::READ, timeout) { + match set_timeout(self.fd.as_raw_fd(), TimeoutDirection::READ, timeout) { #[cfg(any( target_vendor="apple", target_os="freebsd", target_os="netbsd", @@ -648,7 +664,7 @@ impl UnixSeqpacketListener { /// assert_eq!(listener.timeout().unwrap(), timeout); /// ``` pub fn timeout(&self) -> Result, io::Error> { - get_timeout(self.fd, TimeoutDirection::READ) + get_timeout(self.fd.as_raw_fd(), TimeoutDirection::READ) } /// Enables or disables nonblocking-ness of [`accept_unix_addr()`](#method.accept_unix addr). @@ -673,7 +689,7 @@ impl UnixSeqpacketListener { /// # std::fs::remove_file("nonblocking_seqpacket_listener.socket").expect("delete socket file"); /// ``` pub fn set_nonblocking(&self, nonblocking: bool) -> Result<(), io::Error> { - set_nonblocking(self.fd, nonblocking) + set_nonblocking(self.fd.as_raw_fd(), nonblocking) } } @@ -746,7 +762,7 @@ impl UnixSeqpacketListener { #[derive(Debug)] #[repr(transparent)] pub struct NonblockingUnixSeqpacketConn { - fd: RawFd, + fd: OwnedFd, } impl_rawfd_traits!{NonblockingUnixSeqpacketConn} @@ -767,7 +783,7 @@ impl NonblockingUnixSeqpacketConn { pub fn connect_unix_addr(addr: &UnixSocketAddr) -> Result { let socket = Socket::new(SOCK_SEQPACKET, true)?; set_unix_addr(socket.as_raw_fd(), SetAddr::PEER, addr)?; - Ok(NonblockingUnixSeqpacketConn { fd: socket.into_raw_fd() }) + Ok(NonblockingUnixSeqpacketConn { fd: unsafe { OwnedFd::from_raw_fd(socket.into_raw_fd()) } }) } /// Binds to an address before connecting to a listening seqpacket socket. pub fn connect_from_to_unix_addr(from: &UnixSocketAddr, to: &UnixSocketAddr) @@ -775,7 +791,7 @@ impl NonblockingUnixSeqpacketConn { let socket = Socket::new(SOCK_SEQPACKET, true)?; set_unix_addr(socket.as_raw_fd(), SetAddr::LOCAL, from)?; set_unix_addr(socket.as_raw_fd(), SetAddr::PEER, to)?; - Ok(NonblockingUnixSeqpacketConn { fd: socket.into_raw_fd() }) + Ok(NonblockingUnixSeqpacketConn { fd: unsafe { OwnedFd::from_raw_fd(socket.into_raw_fd()) } }) } /// Creates a pair of nonblocking unix-domain seqpacket conneections connected to each other. @@ -793,24 +809,24 @@ impl NonblockingUnixSeqpacketConn { /// ``` pub fn pair() -> Result<(Self, Self), io::Error> { let (a, b) = Socket::pair(SOCK_SEQPACKET, true)?; - let a = NonblockingUnixSeqpacketConn { fd: a.into_raw_fd() }; - let b = NonblockingUnixSeqpacketConn { fd: b.into_raw_fd() }; + let a = NonblockingUnixSeqpacketConn { fd: unsafe { OwnedFd::from_raw_fd(a.into_raw_fd()) } }; + let b = NonblockingUnixSeqpacketConn { fd: unsafe { OwnedFd::from_raw_fd(b.into_raw_fd()) } }; Ok((a, b)) } /// Returns the address of this side of the connection. pub fn local_unix_addr(&self) -> Result { - get_unix_addr(self.fd, GetAddr::LOCAL) + get_unix_addr(self.fd.as_raw_fd(), GetAddr::LOCAL) } /// Returns the address of the other side of the connection. pub fn peer_unix_addr(&self) -> Result { - get_unix_addr(self.fd, GetAddr::PEER) + get_unix_addr(self.fd.as_raw_fd(), GetAddr::PEER) } /// Returns information about the process of the peer when the connection was established. /// /// See documentation of the returned type for details. pub fn initial_peer_credentials(&self) -> Result { - peer_credentials(self.fd) + peer_credentials(self.fd.as_raw_fd()) } /// Returns the SELinux security context of the process that created the other /// end of this connection. @@ -829,13 +845,19 @@ impl NonblockingUnixSeqpacketConn { pub fn send(&self, packet: &[u8]) -> Result { let ptr = packet.as_ptr() as *const c_void; let flags = MSG_NOSIGNAL | MSG_EOR; - let sent = cvt_r!(unsafe { send(self.fd, ptr, packet.len(), flags) })?; + let sent = cvt_r!(unsafe { send(self.fd.as_raw_fd(), ptr, packet.len(), flags) })?; + Ok(sent as usize) + } + pub fn send_flags(&self, packet: &[u8], flags: i32) -> Result { + let ptr = packet.as_ptr() as *const c_void; + let flags = flags; + let sent = cvt_r!(unsafe { send(self.fd.as_raw_fd(), ptr, packet.len(), flags) })?; Ok(sent as usize) } /// Receives a packet from the peer. pub fn recv(&self, buffer: &mut[u8]) -> Result { let ptr = buffer.as_ptr() as *mut c_void; - let received = cvt_r!(unsafe { recv(self.fd, ptr, buffer.len(), MSG_NOSIGNAL) })?; + let received = cvt_r!(unsafe { recv(self.fd.as_raw_fd(), ptr, buffer.len(), MSG_NOSIGNAL) })?; Ok(received as usize) } /// Sends a packet assembled from multiple byte slices. @@ -851,18 +873,18 @@ impl NonblockingUnixSeqpacketConn { /// too short buffers. pub fn recv_vectored(&self, buffers: &mut[IoSliceMut]) -> Result<(usize, bool), io::Error> { - recv_ancillary(self.fd, None, 0, buffers, &mut[]) + recv_ancillary(self.fd.as_raw_fd(), None, 0, buffers, &mut[]) .map(|(bytes, ancillary)| (bytes, ancillary.message_truncated()) ) } /// Sends a packet with associated file descriptors. pub fn send_fds(&self, bytes: &[u8], fds: &[RawFd]) -> Result { - send_ancillary(self.fd, None, MSG_EOR, &[IoSlice::new(bytes)], fds, None) + send_ancillary(self.fd.as_raw_fd(), None, MSG_EOR, &[IoSlice::new(bytes)], fds, None) } /// Receives a packet and associated file descriptors. pub fn recv_fds(&self, byte_buffer: &mut[u8], fd_buffer: &mut[RawFd]) -> Result<(usize, bool, usize), io::Error> { - recv_fds(self.fd, None, &mut[IoSliceMut::new(byte_buffer)], fd_buffer) + recv_fds(self.fd.as_raw_fd(), None, &mut[IoSliceMut::new(byte_buffer)], fd_buffer) } /// Receives a packet without removing it from the incoming queue. /// @@ -884,7 +906,7 @@ impl NonblockingUnixSeqpacketConn { pub fn peek(&self, buffer: &mut[u8]) -> Result { let ptr = buffer.as_ptr() as *mut c_void; let flags = MSG_NOSIGNAL | MSG_PEEK; - let received = cvt_r!(unsafe { recv(self.fd, ptr, buffer.len(), flags) })?; + let received = cvt_r!(unsafe { recv(self.fd.as_raw_fd(), ptr, buffer.len(), flags) })?; Ok(received as usize) } /// Receives a packet without removing it from the incoming queue. @@ -893,7 +915,7 @@ impl NonblockingUnixSeqpacketConn { /// the combined buffers being too small. pub fn peek_vectored(&self, buffers: &mut[IoSliceMut]) -> Result<(usize, bool), io::Error> { - recv_ancillary(self.fd, None, MSG_PEEK, buffers, &mut[]) + recv_ancillary(self.fd.as_raw_fd(), None, MSG_PEEK, buffers, &mut[]) .map(|(bytes, ancillary)| (bytes, ancillary.message_truncated()) ) } @@ -913,7 +935,7 @@ impl NonblockingUnixSeqpacketConn { /// assert!(a.take_error().unwrap().is_none()); /// ``` pub fn take_error(&self) -> Result, io::Error> { - take_error(self.fd) + take_error(self.fd.as_raw_fd()) } @@ -937,9 +959,9 @@ impl NonblockingUnixSeqpacketConn { /// assert_eq!(a2.recv(&mut[0u8; 10]).unwrap_err().kind(), ErrorKind::WouldBlock); /// ``` pub fn try_clone(&self) -> Result { - let cloned = Socket::try_clone_from(self.fd)?; + let cloned = Socket::try_clone_from(self.fd.as_raw_fd())?; // nonblockingness is shared and therefore inherited - Ok(NonblockingUnixSeqpacketConn { fd: cloned.into_raw_fd() }) + Ok(NonblockingUnixSeqpacketConn { fd: unsafe { OwnedFd::from_raw_fd(cloned.into_raw_fd()) } }) } /// Shuts down the read, write, or both halves of this connection. @@ -1023,7 +1045,7 @@ impl NonblockingUnixSeqpacketConn { #[derive(Debug)] #[repr(transparent)] pub struct NonblockingUnixSeqpacketListener { - fd: RawFd + fd: OwnedFd } impl_rawfd_traits!{NonblockingUnixSeqpacketListener} @@ -1040,12 +1062,12 @@ impl NonblockingUnixSeqpacketListener { let socket = Socket::new(SOCK_SEQPACKET, true)?; set_unix_addr(socket.as_raw_fd(), SetAddr::LOCAL, addr)?; socket.start_listening()?; - Ok(NonblockingUnixSeqpacketListener { fd: socket.into_raw_fd() }) + Ok(NonblockingUnixSeqpacketListener { fd: unsafe { OwnedFd::from_raw_fd(socket.into_raw_fd()) } }) } /// Returns the address this listener was bound to. pub fn local_unix_addr(&self) -> Result { - get_unix_addr(self.fd, GetAddr::LOCAL) + get_unix_addr(self.fd.as_raw_fd(), GetAddr::LOCAL) } /// Accepts a non-blocking connection, non-blockingly. @@ -1067,8 +1089,8 @@ impl NonblockingUnixSeqpacketListener { /// ``` pub fn accept_unix_addr(&self) -> Result<(NonblockingUnixSeqpacketConn, UnixSocketAddr), io::Error> { - let (socket, addr) = Socket::accept_from(self.fd, true)?; - let conn = NonblockingUnixSeqpacketConn { fd: socket.into_raw_fd() }; + let (socket, addr) = Socket::accept_from(self.fd.as_raw_fd(), true)?; + let conn = NonblockingUnixSeqpacketConn { fd: unsafe { OwnedFd::from_raw_fd(socket.into_raw_fd()) } }; Ok((conn, addr)) } @@ -1090,13 +1112,13 @@ impl NonblockingUnixSeqpacketListener { /// assert!(listener.take_error().unwrap().is_none()); /// ``` pub fn take_error(&self) -> Result, io::Error> { - take_error(self.fd) + take_error(self.fd.as_raw_fd()) } /// Creates a new file descriptor listening for the same connections. pub fn try_clone(&self) -> Result { - let cloned = Socket::try_clone_from(self.fd)?; + let cloned = Socket::try_clone_from(self.fd.as_raw_fd())?; // nonblockingness is shared and therefore inherited - Ok(NonblockingUnixSeqpacketListener { fd: cloned.into_raw_fd() }) + Ok(NonblockingUnixSeqpacketListener { fd: unsafe { OwnedFd::from_raw_fd(cloned.into_raw_fd()) } }) } } diff --git a/src/tokio/seqpacket.rs b/src/tokio/seqpacket.rs index cbd7f93..84f6228 100644 --- a/src/tokio/seqpacket.rs +++ b/src/tokio/seqpacket.rs @@ -2,6 +2,7 @@ use crate::{nonblocking, UnixSocketAddr, ConnCredentials}; use std::io::{self, IoSlice, IoSliceMut}; use std::net::Shutdown; +use std::os::fd::AsFd; use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd}; use std::path::Path; @@ -9,6 +10,7 @@ use tokio_crate::io::Interest; use tokio_crate::io::unix::AsyncFd; /// An I/O object representing a Unix Sequenced-packet socket. +#[derive(Debug)] pub struct UnixSeqpacketConn { io: AsyncFd, } @@ -115,6 +117,10 @@ impl UnixSeqpacketConn { pub async fn send(&mut self, packet: &[u8]) -> io::Result { self.io.async_io(Interest::WRITABLE, |conn| conn.send(packet) ).await } + /// Sends a packet to the socket's peer with specified flags. + pub async fn send_flags(&mut self, packet: &[u8], flags: i32) -> io::Result { + self.io.async_io(Interest::WRITABLE, |conn| conn.send_flags(packet, flags) ).await + } /// Receives a packet from the socket's peer. pub async fn recv(&mut self, buffer: &mut[u8]) -> io::Result { self.io.async_io(Interest::READABLE, |conn| conn.recv(buffer) ).await @@ -176,7 +182,11 @@ impl IntoRawFd for UnixSeqpacketConn { } } - +impl AsFd for UnixSeqpacketConn { + fn as_fd(&self) -> std::os::unix::prelude::BorrowedFd<'_> { + self.io.get_ref().as_fd() + } +} /// An I/O object representing a Unix Sequenced-packet socket. pub struct UnixSeqpacketListener { @@ -265,3 +275,9 @@ impl IntoRawFd for UnixSeqpacketListener { self.io.into_inner().into_raw_fd() } } + +impl AsFd for UnixSeqpacketListener { + fn as_fd(&self) -> std::os::unix::prelude::BorrowedFd<'_> { + self.io.get_ref().as_fd() + } +} From 1479196aff05d09d561ad26665a4adc67e8405ba Mon Sep 17 00:00:00 2001 From: Aleksandr Morozov Date: Mon, 28 Oct 2024 02:21:34 +0100 Subject: [PATCH 2/5] Replaced RawFd in helper.rs. Removing the conversion from seqpacker.rs --- src/helpers.rs | 73 +++++++++++++++++++++++++++++++----------------- src/seqpacket.rs | 32 ++++++++++----------- 2 files changed, 64 insertions(+), 41 deletions(-) diff --git a/src/helpers.rs b/src/helpers.rs index 963696a..715869a 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -4,6 +4,7 @@ /// Several adapted from std. use std::convert::TryInto; +use std::os::fd::{AsFd, BorrowedFd, FromRawFd, OwnedFd}; use std::os::unix::io::{RawFd, AsRawFd, IntoRawFd}; use std::io::{self, ErrorKind}; use std::mem; @@ -11,7 +12,7 @@ use std::time::Duration; use libc::{c_int, sockaddr, socklen_t, AF_UNIX}; use libc::{bind, connect, getsockname, getpeername}; -use libc::{socket, accept, close, listen, socketpair}; +use libc::{socket, accept, listen, socketpair}; use libc::{ioctl, FIONBIO}; #[cfg(not(target_os = "haiku"))] use libc::{FIOCLEX,FIONCLEX}; @@ -211,29 +212,46 @@ pub fn get_timeout(socket: RawFd, direction: TimeoutDirection) /// Used in setup of sockets to ensure the file descriptor is always closed /// if later parts of the setup fails. -pub struct Socket(RawFd); +pub struct Socket(OwnedFd); +//OwnedFd closes the file descriptor on drop. It is guaranteed that nobody else will close the file descriptor. +/* impl Drop for Socket { fn drop(&mut self) { // ignore errors - unlikely and there is nowhere to return them unsafe { close(self.0) }; } } +*/ impl IntoRawFd for Socket { fn into_raw_fd(self) -> RawFd { - let fd = self.0; - mem::forget(self); - fd + self.0.into_raw_fd() + //let fd = self.0; + //mem::forget(self); + //fd } } impl AsRawFd for Socket { fn as_raw_fd(&self) -> RawFd { - self.0 + self.0.as_raw_fd() } } +impl AsFd for Socket { + fn as_fd(&self) -> BorrowedFd<'_> + { + self.0.as_fd() + } +} + +impl From for OwnedFd +{ + fn from(value: Socket) -> Self { + value.0 + } +} impl Socket { /// Enable / disable Apple-only SO_NOSIGPIPE @@ -249,6 +267,11 @@ impl Socket { Ok(()) } + fn new_int(fd: RawFd) -> Self + { + Self(unsafe { OwnedFd::from_raw_fd(fd) }) + } + pub fn new(socket_type: c_int, nonblocking: bool) -> Result { // Set close-on-exec atomically with SOCK_CLOEXEC if possible. // Falls through to the portable but race-prone way for compatibility @@ -258,7 +281,7 @@ impl Socket { #[cfg(not(any(target_vendor="apple", target_os = "haiku")))] { let type_flags = socket_type | SOCK_CLOEXEC | if nonblocking {SOCK_NONBLOCK} else {0}; match cvt!(unsafe { socket(AF_UNIX, type_flags, 0) }) { - Ok(fd) => return Ok(Socket(fd)), + Ok(fd) => return Ok(Socket::new_int(fd)), Err(ref e) if e.raw_os_error() == Some(EINVAL) => {/*try without*/} Err(e) => return Err(e), } @@ -266,11 +289,11 @@ impl Socket { // portable but race-prone let fd = cvt!(unsafe { socket(AF_UNIX, socket_type, 0) })?; - let socket = Socket(fd); - set_cloexec(socket.0, true)?; + let socket = Socket::new_int(fd); + set_cloexec(socket.0.as_raw_fd(), true)?; socket.set_nosigpipe(true)?; if nonblocking { - set_nonblocking(socket.0, true)?; + set_nonblocking(socket.0.as_raw_fd(), true)?; } Ok(socket) } @@ -291,7 +314,7 @@ impl Socket { )))] { let flags = SOCK_CLOEXEC | if nonblocking {SOCK_NONBLOCK} else {0}; match cvt_r!(accept4(fd, addr_ptr, len_ptr, flags)) { - Ok(fd) => return Ok(Socket(fd)), + Ok(fd) => return Ok(Socket::new_int(fd)), Err(ref e) if e.raw_os_error() == Some(ENOSYS) => {/*try normal accept()*/}, Err(e) => return Err(e), } @@ -299,18 +322,18 @@ impl Socket { // Portable but not as efficient: let fd = cvt_r!(accept(fd, addr_ptr, len_ptr))?; - let socket = Socket(fd); - set_cloexec(socket.0, true)?; + let socket = Socket::new_int(fd); + set_cloexec(socket.0.as_raw_fd(), true)?; socket.set_nosigpipe(true)?; if nonblocking { - set_nonblocking(socket.0, true)?; + set_nonblocking(socket.0.as_raw_fd(), true)?; } Ok(socket) }) } } pub fn start_listening(&self) -> Result<(), io::Error> { - cvt!(unsafe { listen(self.0, LISTEN_BACKLOG) }).map(|_| () ) + cvt!(unsafe { listen(self.0.as_raw_fd(), LISTEN_BACKLOG) }).map(|_| () ) } pub fn try_clone_from(fd: RawFd) -> Result { @@ -324,7 +347,7 @@ impl Socket { // for compatibility with Linux < 2.6.24 match cvt!(unsafe { fcntl(fd, F_DUPFD_CLOEXEC, 0) }) { Ok(cloned) => { - let socket = Socket(cloned); + let socket = Socket::new_int(cloned); socket.set_nosigpipe(true)?; return Ok(socket); }, @@ -333,8 +356,8 @@ impl Socket { } let cloned = cvt!(unsafe { dup(fd) })?; - let socket = Socket(cloned); - set_cloexec(socket.0, true)?; + let socket = Socket::new_int(cloned); + set_cloexec(socket.0.as_raw_fd(), true)?; socket.set_nosigpipe(true)?; Ok(socket) } @@ -346,22 +369,22 @@ impl Socket { #[cfg(not(any(target_vendor="apple", target_os = "haiku")))] { let type_flags = socket_type | SOCK_CLOEXEC | if nonblocking {SOCK_NONBLOCK} else {0}; match cvt!(unsafe { socketpair(AF_UNIX, type_flags, 0, fd_buf[..].as_mut_ptr()) }) { - Ok(_) => return Ok((Socket(fd_buf[0]), Socket(fd_buf[1]))), + Ok(_) => return Ok((Socket::new_int(fd_buf[0]), Socket::new_int(fd_buf[1]))), Err(ref e) if e.raw_os_error() == Some(EINVAL) => {/*try without*/} Err(e) => return Err(e), } } cvt!(unsafe { socketpair(AF_UNIX, socket_type, 0, fd_buf[..].as_mut_ptr()) })?; - let a = Socket(fd_buf[0]); - let b = Socket(fd_buf[1]); - set_cloexec(a.0, true)?; - set_cloexec(b.0, true)?; + let a = Socket::new_int(fd_buf[0]); + let b = Socket::new_int(fd_buf[1]); + set_cloexec(a.0.as_raw_fd(), true)?; + set_cloexec(b.0.as_raw_fd(), true)?; a.set_nosigpipe(true)?; b.set_nosigpipe(true)?; if nonblocking { - set_nonblocking(a.0, true)?; - set_nonblocking(b.0, true)?; + set_nonblocking(a.0.as_raw_fd(), true)?; + set_nonblocking(b.0.as_raw_fd(), true)?; } Ok((a, b)) } diff --git a/src/seqpacket.rs b/src/seqpacket.rs index cdb5c4a..c65209c 100644 --- a/src/seqpacket.rs +++ b/src/seqpacket.rs @@ -180,7 +180,7 @@ impl UnixSeqpacketConn { pub fn connect_unix_addr(addr: &UnixSocketAddr) -> Result { let socket = Socket::new(SOCK_SEQPACKET, false)?; set_unix_addr(socket.as_raw_fd(), SetAddr::PEER, addr)?; - Ok(UnixSeqpacketConn { fd: unsafe { OwnedFd::from_raw_fd(socket.into_raw_fd()) } }) + Ok(UnixSeqpacketConn { fd: socket.into() }) } /// Binds to an address before connecting to a listening seqpacet socket. pub fn connect_from_to_unix_addr(from: &UnixSocketAddr, to: &UnixSocketAddr) @@ -188,7 +188,7 @@ impl UnixSeqpacketConn { let socket = Socket::new(SOCK_SEQPACKET, false)?; set_unix_addr(socket.as_raw_fd(), SetAddr::LOCAL, from)?; set_unix_addr(socket.as_raw_fd(), SetAddr::PEER, to)?; - Ok(UnixSeqpacketConn { fd: unsafe { OwnedFd::from_raw_fd(socket.into_raw_fd()) } }) + Ok(UnixSeqpacketConn { fd: socket.into() }) } /// Creates a pair of unix-domain seqpacket conneections connected to each other. @@ -206,8 +206,8 @@ impl UnixSeqpacketConn { /// ``` pub fn pair() -> Result<(Self, Self), io::Error> { let (a, b) = Socket::pair(SOCK_SEQPACKET, false)?; - let a = UnixSeqpacketConn { fd: unsafe { OwnedFd::from_raw_fd(a.into_raw_fd()) } }; - let b = UnixSeqpacketConn { fd: unsafe { OwnedFd::from_raw_fd(b.into_raw_fd()) } }; + let a = UnixSeqpacketConn { fd: a.into() }; + let b = UnixSeqpacketConn { fd: b.into() }; Ok((a, b)) } @@ -380,7 +380,7 @@ impl UnixSeqpacketConn { /// ``` pub fn try_clone(&self) -> Result { let cloned = Socket::try_clone_from(self.fd.as_raw_fd())?; - Ok(UnixSeqpacketConn { fd: unsafe { OwnedFd::from_raw_fd(cloned.into_raw_fd() )} }) + Ok(UnixSeqpacketConn { fd: cloned.into() }) } /// Sets the read timeout to the duration specified. @@ -568,7 +568,7 @@ impl UnixSeqpacketListener { let socket = Socket::new(SOCK_SEQPACKET, false)?; set_unix_addr(socket.as_raw_fd(), SetAddr::LOCAL, addr)?; socket.start_listening()?; - Ok(UnixSeqpacketListener { fd: unsafe { OwnedFd::from_raw_fd(socket.into_raw_fd()) } }) + Ok(UnixSeqpacketListener { fd: socket.into() }) } /// Returns the address the socket is listening on. @@ -580,7 +580,7 @@ impl UnixSeqpacketListener { pub fn accept_unix_addr(&self) -> Result<(UnixSeqpacketConn, UnixSocketAddr), io::Error> { let (socket, addr) = Socket::accept_from(self.fd.as_raw_fd(), false)?; - let conn = UnixSeqpacketConn { fd: unsafe { OwnedFd::from_raw_fd(socket.into_raw_fd()) } }; + let conn = UnixSeqpacketConn { fd: socket.into() }; Ok((conn, addr)) } @@ -596,7 +596,7 @@ impl UnixSeqpacketListener { /// Creates a new file descriptor listening for the same connections. pub fn try_clone(&self) -> Result { let cloned = Socket::try_clone_from(self.fd.as_raw_fd())?; - Ok(UnixSeqpacketListener { fd: unsafe { OwnedFd::from_raw_fd(cloned.into_raw_fd()) } }) + Ok(UnixSeqpacketListener { fd: cloned.into() }) } /// Sets a maximum duration to wait in a single `accept()` on this socket. @@ -783,7 +783,7 @@ impl NonblockingUnixSeqpacketConn { pub fn connect_unix_addr(addr: &UnixSocketAddr) -> Result { let socket = Socket::new(SOCK_SEQPACKET, true)?; set_unix_addr(socket.as_raw_fd(), SetAddr::PEER, addr)?; - Ok(NonblockingUnixSeqpacketConn { fd: unsafe { OwnedFd::from_raw_fd(socket.into_raw_fd()) } }) + Ok(NonblockingUnixSeqpacketConn { fd: socket.into() }) } /// Binds to an address before connecting to a listening seqpacket socket. pub fn connect_from_to_unix_addr(from: &UnixSocketAddr, to: &UnixSocketAddr) @@ -791,7 +791,7 @@ impl NonblockingUnixSeqpacketConn { let socket = Socket::new(SOCK_SEQPACKET, true)?; set_unix_addr(socket.as_raw_fd(), SetAddr::LOCAL, from)?; set_unix_addr(socket.as_raw_fd(), SetAddr::PEER, to)?; - Ok(NonblockingUnixSeqpacketConn { fd: unsafe { OwnedFd::from_raw_fd(socket.into_raw_fd()) } }) + Ok(NonblockingUnixSeqpacketConn { fd: socket.into() }) } /// Creates a pair of nonblocking unix-domain seqpacket conneections connected to each other. @@ -809,8 +809,8 @@ impl NonblockingUnixSeqpacketConn { /// ``` pub fn pair() -> Result<(Self, Self), io::Error> { let (a, b) = Socket::pair(SOCK_SEQPACKET, true)?; - let a = NonblockingUnixSeqpacketConn { fd: unsafe { OwnedFd::from_raw_fd(a.into_raw_fd()) } }; - let b = NonblockingUnixSeqpacketConn { fd: unsafe { OwnedFd::from_raw_fd(b.into_raw_fd()) } }; + let a = NonblockingUnixSeqpacketConn { fd: a.into() }; + let b = NonblockingUnixSeqpacketConn { fd: b.into() }; Ok((a, b)) } @@ -961,7 +961,7 @@ impl NonblockingUnixSeqpacketConn { pub fn try_clone(&self) -> Result { let cloned = Socket::try_clone_from(self.fd.as_raw_fd())?; // nonblockingness is shared and therefore inherited - Ok(NonblockingUnixSeqpacketConn { fd: unsafe { OwnedFd::from_raw_fd(cloned.into_raw_fd()) } }) + Ok(NonblockingUnixSeqpacketConn { fd: cloned.into() }) } /// Shuts down the read, write, or both halves of this connection. @@ -1062,7 +1062,7 @@ impl NonblockingUnixSeqpacketListener { let socket = Socket::new(SOCK_SEQPACKET, true)?; set_unix_addr(socket.as_raw_fd(), SetAddr::LOCAL, addr)?; socket.start_listening()?; - Ok(NonblockingUnixSeqpacketListener { fd: unsafe { OwnedFd::from_raw_fd(socket.into_raw_fd()) } }) + Ok(NonblockingUnixSeqpacketListener { fd: socket.into() }) } /// Returns the address this listener was bound to. @@ -1090,7 +1090,7 @@ impl NonblockingUnixSeqpacketListener { pub fn accept_unix_addr(&self) -> Result<(NonblockingUnixSeqpacketConn, UnixSocketAddr), io::Error> { let (socket, addr) = Socket::accept_from(self.fd.as_raw_fd(), true)?; - let conn = NonblockingUnixSeqpacketConn { fd: unsafe { OwnedFd::from_raw_fd(socket.into_raw_fd()) } }; + let conn = NonblockingUnixSeqpacketConn { fd: socket.into() }; Ok((conn, addr)) } @@ -1119,6 +1119,6 @@ impl NonblockingUnixSeqpacketListener { pub fn try_clone(&self) -> Result { let cloned = Socket::try_clone_from(self.fd.as_raw_fd())?; // nonblockingness is shared and therefore inherited - Ok(NonblockingUnixSeqpacketListener { fd: unsafe { OwnedFd::from_raw_fd(cloned.into_raw_fd()) } }) + Ok(NonblockingUnixSeqpacketListener { fd: cloned.into() }) } } From 72870c8cdaf59d30e7d1da47ae57f33625b502fb Mon Sep 17 00:00:00 2001 From: Aleksandr Morozov Date: Mon, 28 Oct 2024 02:32:10 +0100 Subject: [PATCH 3/5] Fixed compile error for mio_08 dep --- src/seqpacket.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/seqpacket.rs b/src/seqpacket.rs index c65209c..42c2e3c 100644 --- a/src/seqpacket.rs +++ b/src/seqpacket.rs @@ -55,14 +55,14 @@ macro_rules! impl_mio_if_enabled {($type:tt) => { impl Source_08 for $type { fn register(&mut self, registry: &Registry_08, token: Token_08, interest: Interest_08) -> Result<(), io::Error> { - SourceFd_08(&self.fd).register(registry, token, interest) + SourceFd_08(&self.fd.as_raw_fd()).register(registry, token, interest) } fn reregister(&mut self, registry: &Registry_08, token: Token_08, interest: Interest_08) -> Result<(), io::Error> { - SourceFd_08(&self.fd).reregister(registry, token, interest) + SourceFd_08(&self.fd.as_raw_fd()).reregister(registry, token, interest) } fn deregister(&mut self, registry: &Registry_08) -> Result<(), io::Error> { - SourceFd_08(&self.fd).deregister(registry) + SourceFd_08(&self.fd.as_raw_fd()).deregister(registry) } } @@ -70,14 +70,14 @@ macro_rules! impl_mio_if_enabled {($type:tt) => { impl<'a> Source_08 for &'a $type { fn register(&mut self, registry: &Registry_08, token: Token_08, interest: Interest_08) -> Result<(), io::Error> { - SourceFd_08(&self.fd).register(registry, token, interest) + SourceFd_08(&self.fd.as_raw_fd()).register(registry, token, interest) } fn reregister(&mut self, registry: &Registry_08, token: Token_08, interest: Interest_08) -> Result<(), io::Error> { - SourceFd_08(&self.fd).reregister(registry, token, interest) + SourceFd_08(&self.fd.as_raw_fd()).reregister(registry, token, interest) } fn deregister(&mut self, registry: &Registry_08) -> Result<(), io::Error> { - SourceFd_08(&self.fd).deregister(registry) + SourceFd_08(&self.fd.as_raw_fd()).deregister(registry) } } }} From 7ba38107f2c6d7590e8452191e38ec51de56d8ea Mon Sep 17 00:00:00 2001 From: Aleksandr Morozov Date: Mon, 28 Oct 2024 02:38:52 +0100 Subject: [PATCH 4/5] Fixes the duplicate let flags = flags --- src/seqpacket.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/seqpacket.rs b/src/seqpacket.rs index 42c2e3c..806dbfa 100644 --- a/src/seqpacket.rs +++ b/src/seqpacket.rs @@ -29,9 +29,9 @@ macro_rules! impl_rawfd_traits {($type:tt) => { } impl IntoRawFd for $type { fn into_raw_fd(self) -> RawFd { - let fd = self.fd.into_raw_fd(); + self.fd.into_raw_fd() //mem::forget(self); - fd + //fd } } /* OwnedFd closes the file descriptor on drop. It is guaranteed that nobody else will close the file descriptor. @@ -850,7 +850,6 @@ impl NonblockingUnixSeqpacketConn { } pub fn send_flags(&self, packet: &[u8], flags: i32) -> Result { let ptr = packet.as_ptr() as *const c_void; - let flags = flags; let sent = cvt_r!(unsafe { send(self.fd.as_raw_fd(), ptr, packet.len(), flags) })?; Ok(sent as usize) } From 55b8f1208d79080267fcc2e0b6f9021a6758960a Mon Sep 17 00:00:00 2001 From: Aleksandr Morozov Date: Mon, 28 Oct 2024 02:51:55 +0100 Subject: [PATCH 5/5] Fixes the problems for macos and linux import problem --- src/helpers.rs | 5 ++--- src/seqpacket.rs | 3 +-- src/tokio/seqpacket.rs | 3 +-- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/helpers.rs b/src/helpers.rs index 715869a..b1001f2 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -4,8 +4,7 @@ /// Several adapted from std. use std::convert::TryInto; -use std::os::fd::{AsFd, BorrowedFd, FromRawFd, OwnedFd}; -use std::os::unix::io::{RawFd, AsRawFd, IntoRawFd}; +use std::os::unix::io::{RawFd, AsFd, AsRawFd, IntoRawFd, BorrowedFd, FromRawFd, OwnedFd}; use std::io::{self, ErrorKind}; use std::mem; use std::time::Duration; @@ -261,7 +260,7 @@ impl Socket { unsafe { let nosigpipe = &(nosigpipe as c_int) as *const c_int as *const c_void; let int_size = mem::size_of::() as socklen_t; - cvt!(setsockopt(self.0, SOL_SOCKET, SO_NOSIGPIPE, nosigpipe, int_size))?; + cvt!(setsockopt(self.0.as_raw_fd(), SOL_SOCKET, SO_NOSIGPIPE, nosigpipe, int_size))?; } } Ok(()) diff --git a/src/seqpacket.rs b/src/seqpacket.rs index 806dbfa..ee6853f 100644 --- a/src/seqpacket.rs +++ b/src/seqpacket.rs @@ -1,7 +1,6 @@ use std::io::{self, ErrorKind, IoSlice, IoSliceMut}; use std::net::Shutdown; -use std::os::fd::{OwnedFd, BorrowedFd}; -use std::os::unix::io::{AsFd, RawFd, FromRawFd, AsRawFd, IntoRawFd}; +use std::os::unix::io::{OwnedFd, BorrowedFd, AsFd, RawFd, FromRawFd, AsRawFd, IntoRawFd}; use std::path::Path; use std::time::Duration; diff --git a/src/tokio/seqpacket.rs b/src/tokio/seqpacket.rs index 84f6228..681aa75 100644 --- a/src/tokio/seqpacket.rs +++ b/src/tokio/seqpacket.rs @@ -2,8 +2,7 @@ use crate::{nonblocking, UnixSocketAddr, ConnCredentials}; use std::io::{self, IoSlice, IoSliceMut}; use std::net::Shutdown; -use std::os::fd::AsFd; -use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd}; +use std::os::unix::io::{AsFd, AsRawFd, FromRawFd, IntoRawFd, RawFd}; use std::path::Path; use tokio_crate::io::Interest;