UnixSeqpacketConn

Struct UnixSeqpacketConn 

Source
pub struct UnixSeqpacketConn { /* private fields */ }
Expand description

A non-blocking unix domain sequential-packet connection.

Differs from uds_fork::UnixSeqpacketConn in that all operations that send or receive data will return an Error of kind ErrorKind::WouldBlock instead of blocking. This is done by creating the socket as non-blocking, and not by passing MSG_DONTWAIT. If creating this type from a raw file descriptor, ensure the fd is set to nonblocking before using it through this type.

§Examples

Sending or receiving when it would block a normal socket:

use uds_fork::nonblocking::UnixSeqpacketConn;
use std::io::ErrorKind;

let (a, b) = UnixSeqpacketConn::pair().expect("create nonblocking seqpacket pair");

// trying to receive when there are no packets waiting
assert_eq!(a.recv(&mut[0]).unwrap_err().kind(), ErrorKind::WouldBlock);

// trying to send when the OS buffer for the connection is full
loop {
    if let Err(error) = a.send(&[0u8; 1000]) {
        assert_eq!(error.kind(), ErrorKind::WouldBlock);
        break;
    }
}

Implementations§

Source§

impl NonblockingUnixSeqpacketConn

Source

pub fn connect<P: AsRef<Path>>(path: P) -> Result<Self, Error>

Connects to an unix seqpacket server listening at path.

This is a wrapper around connect_unix_addr() for convenience and compatibility with std.

Source

pub fn connect_unix_addr(addr: &UnixSocketAddr) -> Result<Self, Error>

Connects to an unix seqpacket server listening at addr.

Source

pub fn connect_from_to_unix_addr( from: &UnixSocketAddr, to: &UnixSocketAddr, ) -> Result<Self, Error>

Binds to an address before connecting to a listening seqpacket socket.

Source

pub fn pair() -> Result<(Self, Self), Error>

Creates a pair of nonblocking unix-domain seqpacket conneections connected to each other.

§Examples
let (a, b) = uds_fork::nonblocking::UnixSeqpacketConn::pair().unwrap();
assert!(a.local_unix_addr().unwrap().is_unnamed());
assert!(b.local_unix_addr().unwrap().is_unnamed());
assert_eq!(b.recv(&mut[0; 20]).unwrap_err().kind(), std::io::ErrorKind::WouldBlock);
a.send(b"hello").unwrap();
assert_eq!(b.recv(&mut[0; 20]).unwrap(), 5);
Source

pub fn local_unix_addr(&self) -> Result<UnixSocketAddr, Error>

Returns the address of this side of the connection.

Source

pub fn peer_unix_addr(&self) -> Result<UnixSocketAddr, Error>

Returns the address of the other side of the connection.

Source

pub fn initial_peer_credentials(&self) -> Result<ConnCredentials, Error>

Returns information about the process of the peer when the connection was established.

See documentation of the returned type for details.

Source

pub fn initial_peer_selinux_context( &self, buf: &mut [u8], ) -> Result<usize, Error>

Returns the SELinux security context of the process that created the other end of this connection.

Will return an error on other operating systems than Linux or Android, and also if running inside kubernetes. On success the number of bytes used is returned. (like Read)

The default security context is unconfined, without any trailing NUL.
A buffor of 50 bytes is probably always big enough.

Source

pub fn send(&self, packet: &[u8]) -> Result<usize, Error>

Sends a packet to the peer.

Source

pub fn send_flags(&self, packet: &[u8], flags: i32) -> Result<usize, Error>

Source

pub fn recv(&self, buffer: &mut [u8]) -> Result<usize, Error>

Receives a packet from the peer.

Source

pub fn send_vectored(&self, slices: &[IoSlice<'_>]) -> Result<usize, Error>

Sends a packet assembled from multiple byte slices.

Source

pub fn recv_vectored( &self, buffers: &mut [IoSliceMut<'_>], ) -> Result<(usize, bool), Error>

Reads a packet into multiple buffers.

The returned bool indicates whether the packet was truncated due to too short buffers.

Source

pub fn send_fds(&self, bytes: &[u8], fds: Vec<OwnedFd>) -> Result<usize, Error>

Sends a packet with associated file descriptors.

Source

pub fn recv_fds( &self, byte_buffer: &mut [u8], fd_buffer: &mut Vec<OwnedFd>, ) -> Result<(usize, bool, usize), Error>

Receives a packet and associated file descriptors.

Source

pub fn peek(&self, buffer: &mut [u8]) -> Result<usize, Error>

Receives a packet without removing it from the incoming queue.

§Examples
let (a, b) = uds_fork::nonblocking::UnixSeqpacketConn::pair().unwrap();
let mut buf = [0u8; 10];
assert_eq!(b.peek(&mut buf).unwrap_err().kind(), WouldBlock);
a.send(b"hello").unwrap();
assert_eq!(b.peek(&mut buf).unwrap(), 5);
assert_eq!(&buf[..5], b"hello");
assert_eq!(b.recv(&mut buf).unwrap(), 5);
assert_eq!(&buf[..5], b"hello");
assert_eq!(b.peek(&mut buf).unwrap_err().kind(), WouldBlock);
Source

pub fn peek_vectored( &self, buffers: &mut [IoSliceMut<'_>], ) -> Result<(usize, bool), Error>

Receives a packet without removing it from the incoming queue.

The returned bool indicates whether the packet was truncated due to the combined buffers being too small.

Source

pub fn take_error(&self) -> Result<Option<Error>, Error>

Returns the value of the SO_ERROR option.

This might only provide errors generated from nonblocking connect()s, which this library doesn’t support. It is therefore unlikely to be useful, but is provided for parity with mios UnixStream.

§Examples
let (a, _b) = uds_fork::nonblocking::UnixSeqpacketConn::pair().unwrap();

assert!(a.recv(&mut[0u8; 1024]).is_err());
assert!(a.take_error().unwrap().is_none());
Source

pub fn try_clone(&self) -> Result<Self, Error>

Creates a new file descriptor also pointing to this side of this connection.

§Examples
let (a1, b) = UnixSeqpacketConn::pair().unwrap();
b.send(b"first come first serve").unwrap();
let a2 = a1.try_clone().unwrap();
a2.recv(&mut[0u8; 10]).unwrap();
assert_eq!(a1.recv(&mut[0u8; 10]).unwrap_err().kind(), ErrorKind::WouldBlock);

b.send(b"more").unwrap();
a1.recv(&mut[0u8; 10]).unwrap();
assert_eq!(a2.recv(&mut[0u8; 10]).unwrap_err().kind(), ErrorKind::WouldBlock);
Source

pub fn shutdown(&self, how: Shutdown) -> Result<()>

Shuts down the read, write, or both halves of this connection.

Trait Implementations§

Source§

impl AsFd for NonblockingUnixSeqpacketConn

Source§

fn as_fd(&self) -> BorrowedFd<'_>

Borrows the file descriptor. Read more
Source§

impl AsRawFd for NonblockingUnixSeqpacketConn

Source§

fn as_raw_fd(&self) -> RawFd

Extracts the raw file descriptor. Read more
Source§

impl Debug for NonblockingUnixSeqpacketConn

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<NonblockingUnixSeqpacketConn> for OwnedFd

Source§

fn from(value: NonblockingUnixSeqpacketConn) -> Self

Converts to this type from the input type.
Source§

impl FromRawFd for NonblockingUnixSeqpacketConn

Source§

unsafe fn from_raw_fd(fd: RawFd) -> Self

Constructs a new instance of Self from the given raw file descriptor. Read more
Source§

impl IntoRawFd for NonblockingUnixSeqpacketConn

Source§

fn into_raw_fd(self) -> RawFd

Consumes this object, returning the raw underlying file descriptor. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.