[][src]Struct uds::UnixSocketAddr

pub struct UnixSocketAddr { /* fields omitted */ }

A unix domain socket address.

Differences from std's unix::net::SocketAddr

This type fully supports abstract socket addresses, and can be created by user code and not returned by accept() and similar.

Examples

Creating an abstract address:

use uds::UnixSocketAddr;

let addr = UnixSocketAddr::new("@abstract").expect("too long");
assert!(addr.is_abstract());
assert_eq!(addr.to_string(), "@abstract");

Methods

impl UnixSocketAddr[src]

pub fn new<A: AsRef<[u8]> + ?Sized>(addr: &A) -> Result<Self, Error>[src]

Allows creating abstract, path or unspecified address based on an user-supplied string.

A leading '@' or '\0' signifies an abstract address, an empty slice is takes as the unnamed address, and anything else is a path address.
If a relative path address starts with '@', escape it by prepending "./".

Errors

  • A path or abstract address is too long.
  • A path address contains '\0'.

Examples

Abstract address:

assert!(UnixSocketAddr::new("@abstract").unwrap().is_abstract());
assert!(UnixSocketAddr::new("\0abstract").unwrap().is_abstract());

Escaped path address:

assert!(UnixSocketAddr::new("./@path").unwrap().is_relative_path());

Unnamed address:

assert!(UnixSocketAddr::new("").unwrap().is_unnamed());

pub fn unspecified() -> Self[src]

Creates an unnamed address, which on Linux can be used for auto-bind.

Binding a socket to the unnamed address is different from not binding at all:

On Linux that binds the socket to an abstract address determined by the OS.

Examples

let addr = UnixSocketAddr::unspecified();
assert!(addr.is_unnamed());
let socket = UnixDatagram::unbound().unwrap();
socket.bind_to_unix_addr(&addr).unwrap();
assert!(socket.local_unix_addr().unwrap().is_abstract());

pub fn max_path_len() -> usize[src]

The maximum size of pathname addesses supported by UnixSocketAddr.

Returns the size of the underlying sun_path field, minus 1 if the OS is known to require a trailing NUL ('\0') byte.

pub fn from_path<P: AsRef<Path> + ?Sized>(path: &P) -> Result<Self, Error>[src]

Create a pathname unix socket address.

Errors

This function will return an error if the path is too long for the underlying sockaddr_un type, or contains NUL ('\0') bytes.

pub fn max_abstract_len() -> usize[src]

The maximum size of abstract addesses supported by UnixSocketAddr.

Returns the size of the underlying sun_path field minus 1 for the leading '\0' byte.

pub const fn can_use_abstract_addresses() -> bool[src]

Whether the operating system is known to support abstract socket addresses.

pub fn from_abstract<N: AsRef<[u8]> + ?Sized>(name: &N) -> Result<Self, Error>[src]

Create an abstract unix socket address.

Abstract addresses is a non-standard feature which is only available on Linux and FreeBSD.
This function is always present, and abstract UnixSocketAddrs can be created even if the operating system doesn't support them. Actually using the address will hopefully fail when bind()ing or connect()ing it.

Abstract names can contain NUL bytes.

Errors

This function will return an error if the name is too long. Call max_abstract_len() get the limit.

pub fn from_std(addr: SocketAddr) -> Option<Self>[src]

Try to convert a std::os::unix::net::SocketAddr into an UnixSocketAddr.

This can fail (produce None) if the std ``SocketAddr represents an abstract address, because it doesn't provide any method for viewing it. (other than parsing its Debug output, anyway.)

pub fn from_c_str(path: &CStr) -> Result<Self, Error>[src]

This method can create unnamed and path addresses, but not abstract ones.

Creates unnamed addres for empty strings, and path addresses otherwise.

Errors

Returns ENAMETOOLONG if path (without the trailing '\0') is too long for sockaddr_un.sun_path.

pub fn is_unnamed(&self) -> bool[src]

pub fn is_abstract(&self) -> bool[src]

pub fn is_absolute_path(&self) -> bool[src]

pub fn is_relative_path(&self) -> bool[src]

pub fn is_path(&self) -> bool[src]

pub fn as_ref(&self) -> UnixSocketAddrRef[src]

Get a view that can be pattern matched to the differnt types of addresses.

pub fn new_from_ffi<R, F>(call: F) -> Result<(R, Self), Error> where
    F: FnOnce(&mut sockaddr, &mut socklen_t) -> Result<R, Error>, 
[src]

Prepare a struct sockaddr* and socklen_t* for passing to ffi (such as getsockname(), getpeername(), accept()), and sanitize and normalize the produced address afterwards.

Sanitizations:

  • checking that the address family is AF_UNIX.
  • checking that the address wasn't truncated (the socklen_t is too big).

Normalization:

  • Path addresses have a trailing NUL byte appended if not already there and there is space.

pub unsafe fn from_raw(
    addr: *const sockaddr,
    len: socklen_t
) -> Result<Self, Error>
[src]

Create an UnixSocketAddr from a pointer to a generic sockaddr and a length.

Safety

  • len must not be greater than the size of the memory addr points to.
  • addr must point to valid memory if len is greater than zero, or be NULL.

pub unsafe fn from_raw_unchecked(addr: sockaddr_un, len: socklen_t) -> Self[src]

Create an UnixSocketAddr without any validation.

Safety

  • len must be <= size_of::<sockaddr_un>().
  • addr.sun_family should be AF_UNIX or strange things could happen.
  • addr.sun_len if it exists should be zero.

pub fn into_raw(self) -> (sockaddr_un, socklen_t)[src]

Split the address into its inner, raw parts.

pub fn as_raw_general(&self) -> (&sockaddr, socklen_t)[src]

Get a general sockaddr reference to the address and its length.

Useful for passing to bind(), connect(), sendto() or other FFI.

pub fn as_raw(&self) -> (&sockaddr_un, socklen_t)[src]

Get a reference to the inner struct sockaddr_un, and length.

pub unsafe fn as_raw_mut_general(&mut self) -> (&mut sockaddr, &mut socklen_t)[src]

Get mutable references to a general struct sockaddr and socklen_t.

If passing to getpeername(), accept() or similar, remember to set the length to the capacity, and consider using new_from_ffi() instead.

Safety

Assigning a value > sizeof(struct sockaddr_un) to the socklen_t reference might lead to out-of-bounds reads later.

pub unsafe fn as_raw_mut(&mut self) -> (&mut sockaddr_un, &mut socklen_t)[src]

Get mutable references to the inner struct sockaddr_un and length.

Safety

Assigning a value > sizeof(struct sockaddr_un) to the socklen_t reference might lead to out-of-bounds reads later.

Trait Implementations

impl<'a> From<&'a UnixSocketAddr> for UnixSocketAddrRef<'a>[src]

impl Clone for UnixSocketAddr[src]

impl Copy for UnixSocketAddr[src]

impl Default for UnixSocketAddr[src]

impl Eq for UnixSocketAddr[src]

impl PartialEq<UnixSocketAddr> for UnixSocketAddr[src]

impl PartialEq<[u8]> for UnixSocketAddr[src]

impl PartialEq<UnixSocketAddr> for [u8][src]

impl Display for UnixSocketAddr[src]

impl Debug for UnixSocketAddr[src]

impl Hash for UnixSocketAddr[src]

Auto Trait Implementations

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]