Struct SocketAncillary

pub struct SocketAncillary<'a> { pub(in ::os::unix::net::ancillary) buffer: &'a mut [u8], pub(in ::os::unix::net::ancillary) length: usize, pub(in ::os::unix::net::ancillary) truncated: bool }

A Unix socket Ancillary data struct.

Example

#![feature(unix_socket_ancillary_data)]
use std::os::unix::net::{UnixStream, SocketAncillary, AncillaryData};
use std::io::IoSliceMut;

fn main() -> std::io::Result<()> {
    let sock = UnixStream::connect("/tmp/sock")?;

    let mut fds = [0; 8];
    let mut ancillary_buffer = [0; 128];
    let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);

    let mut buf = [1; 8];
    let mut bufs = &mut [IoSliceMut::new(&mut buf[..])][..];
    sock.recv_vectored_with_ancillary(bufs, &mut ancillary)?;

    for ancillary_result in ancillary.messages() {
        if let AncillaryData::ScmRights(scm_rights) = ancillary_result.unwrap() {
            for fd in scm_rights {
                println!("receive file descriptor: {fd}");
            }
        }
    }
    Ok(())
}

Fields

buffer: &'a mut [u8]
length: usize
truncated: bool

Implementations

impl<'a> SocketAncillary<'a>

fn new(buffer: &'a mut [u8]) -> Self

Creates an ancillary data with the given buffer.

Example

# #![allow(unused_mut)]
#![feature(unix_socket_ancillary_data)]
use std::os::unix::net::SocketAncillary;
let mut ancillary_buffer = [0; 128];
let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
fn capacity(&self) -> usize

Returns the capacity of the buffer.

fn is_empty(&self) -> bool

Returns true if the ancillary data is empty.

fn len(&self) -> usize

Returns the number of used bytes.

fn messages(&self) -> Messages<'_>

Returns the iterator of the control messages.

fn truncated(&self) -> bool

Is true if during a recv operation the ancillary was truncated.

Example

#![feature(unix_socket_ancillary_data)]
use std::os::unix::net::{UnixStream, SocketAncillary};
use std::io::IoSliceMut;

fn main() -> std::io::Result<()> {
    let sock = UnixStream::connect("/tmp/sock")?;

    let mut ancillary_buffer = [0; 128];
    let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);

    let mut buf = [1; 8];
    let mut bufs = &mut [IoSliceMut::new(&mut buf[..])][..];
    sock.recv_vectored_with_ancillary(bufs, &mut ancillary)?;

    println!("Is truncated: {}", ancillary.truncated());
    Ok(())
}
fn add_fds(&mut self, fds: &[RawFd]) -> bool

Add file descriptors to the ancillary data.

The function returns true if there was enough space in the buffer. If there was not enough space then no file descriptors was appended. Technically, that means this operation adds a control message with the level SOL_SOCKET and type SCM_RIGHTS.

Example

#![feature(unix_socket_ancillary_data)]
use std::os::unix::net::{UnixStream, SocketAncillary};
use std::os::unix::io::AsRawFd;
use std::io::IoSlice;

fn main() -> std::io::Result<()> {
    let sock = UnixStream::connect("/tmp/sock")?;

    let mut ancillary_buffer = [0; 128];
    let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
    ancillary.add_fds(&[sock.as_raw_fd()][..]);

    let buf = [1; 8];
    let mut bufs = &mut [IoSlice::new(&buf[..])][..];
    sock.send_vectored_with_ancillary(bufs, &mut ancillary)?;
    Ok(())
}
fn add_creds(&mut self, creds: &[SocketCred]) -> bool

Add credentials to the ancillary data.

The function returns true if there is enough space in the buffer. If there is not enough space then no credentials will be appended. Technically, that means this operation adds a control message with the level SOL_SOCKET and type SCM_CREDENTIALS, SCM_CREDS, or SCM_CREDS2.

fn clear(&mut self)

Clears the ancillary data, removing all values.

Example

#![feature(unix_socket_ancillary_data)]
use std::os::unix::net::{UnixStream, SocketAncillary, AncillaryData};
use std::io::IoSliceMut;

fn main() -> std::io::Result<()> {
    let sock = UnixStream::connect("/tmp/sock")?;

    let mut fds1 = [0; 8];
    let mut fds2 = [0; 8];
    let mut ancillary_buffer = [0; 128];
    let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);

    let mut buf = [1; 8];
    let mut bufs = &mut [IoSliceMut::new(&mut buf[..])][..];

    sock.recv_vectored_with_ancillary(bufs, &mut ancillary)?;
    for ancillary_result in ancillary.messages() {
        if let AncillaryData::ScmRights(scm_rights) = ancillary_result.unwrap() {
            for fd in scm_rights {
                println!("receive file descriptor: {fd}");
            }
        }
    }

    ancillary.clear();

    sock.recv_vectored_with_ancillary(bufs, &mut ancillary)?;
    for ancillary_result in ancillary.messages() {
        if let AncillaryData::ScmRights(scm_rights) = ancillary_result.unwrap() {
            for fd in scm_rights {
                println!("receive file descriptor: {fd}");
            }
        }
    }
    Ok(())
}

Trait Implementations

impl<'a> Debug for SocketAncillary<'a>

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

Auto Trait Implementations

impl<'a> !UnwindSafe for SocketAncillary<'a>

impl<'a> Freeze for SocketAncillary<'a>

impl<'a> RefUnwindSafe for SocketAncillary<'a>

impl<'a> Send for SocketAncillary<'a>

impl<'a> Sync for SocketAncillary<'a>

impl<'a> Unpin for SocketAncillary<'a>

impl<'a> UnsafeUnpin for SocketAncillary<'a>

Blanket Implementations

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

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for SocketAncillary<'a> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for SocketAncillary<'a> where T: ?Sized,

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

impl<T> From<T> for SocketAncillary<'a>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> SizeHint for SocketAncillary<'a> where T: ?Sized,

fn lower_bound(&self) -> usize
fn upper_bound(&self) -> Option<usize>

impl<T> SizedTypeProperties for SocketAncillary<'a>

impl<T, U> Into<U> for SocketAncillary<'a> where U: From<T>,

fn into(self) -> U

Calls U::from(self).

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

impl<T, U> TryFrom<U> for SocketAncillary<'a> where U: Into<T>,

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

impl<T, U> TryInto<U> for SocketAncillary<'a> where U: TryFrom<T>,

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