Struct privsep::net::SocketAncillary[][src]

pub struct SocketAncillary<'a> { /* fields omitted */ }
Expand description

A Unix socket Ancillary data struct.

Example

use privsep::net::{SocketAncillary, AncillaryData, StdUnixStreamExt};
use std::io::IoSliceMut;
use std::os::unix::net::UnixStream;

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().flatten() {
        if let AncillaryData::ScmRights(scm_rights) = ancillary_result {
            for fd in scm_rights {
                println!("receive file descriptor: {}", fd);
            }
        }
    }
    Ok(())
}

Implementations

Create an ancillary data with the given buffer.

Example

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

Returns the capacity of the buffer.

Returns the number of used bytes.

Checks if the buffer is empty

Returns the iterator of the control messages.

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

Example

use privsep::net::{SocketAncillary, AncillaryData, StdUnixStreamExt};
use std::io::IoSliceMut;
use std::os::unix::net::UnixStream;

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(())
}

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

use privsep::net::{SocketAncillary, AncillaryData, StdUnixStreamExt};
use std::io::IoSlice;
use std::os::unix::net::UnixStream;
use std::os::unix::io::AsRawFd;

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 mut buf = [1; 8];
    let mut bufs = &mut [IoSlice::new(&mut buf[..])][..];
    sock.send_vectored_with_ancillary(bufs, &mut ancillary)?;
    Ok(())
}

Add credentials to the ancillary data.

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

Clears the ancillary data, removing all values.

Example

use privsep::net::{SocketAncillary, AncillaryData, StdUnixStreamExt};
use std::io::IoSliceMut;
use std::os::unix::net::UnixStream;

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().flatten() {
        if let AncillaryData::ScmRights(scm_rights) = ancillary_result {
            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().flatten() {
        if let AncillaryData::ScmRights(scm_rights) = ancillary_result {
            for fd in scm_rights {
                println!("receive file descriptor: {}", fd);
            }
        }
    }
    Ok(())
}

Trait Implementations

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.