Struct otter_nodejs_tests::uds::UnixSocketAddr
[−]pub struct UnixSocketAddr { /* private fields */ }
Expand description
A unix domain socket address.
Differences from std
’s unix::net::SocketAddr
This type fully supports Linux’s abstract socket addresses,
and can be created by user code instead of just returned by accept()
and similar.
Examples
Creating an abstract address (fails if the OS doesn’t support them):
use uds::UnixSocketAddr;
let addr = UnixSocketAddr::new("@abstract").unwrap();
assert!(addr.is_abstract());
assert_eq!(addr.to_string(), "@abstract");
Implementations
impl UnixSocketAddr
impl UnixSocketAddr
pub fn new<A>(addr: &A) -> Result<UnixSocketAddr, Error> where
A: AsRef<[u8]> + ?Sized,
pub fn new<A>(addr: &A) -> Result<UnixSocketAddr, Error> where
A: AsRef<[u8]> + ?Sized,
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 taken as the unnamed address, and anything else is a
path address.
If a relative path address starts with @
, escape it by prepending
"./"
.
To avoid surprises, abstract addresses will be detected regargsless of
wheither the OS supports them, and result in an error if it doesn’t.
Errors
- A path or abstract address is too long.
- A path address contains
'\0'
. - An abstract name was supplied on an OS that doesn’t support them.
Examples
Abstract address:
if UnixSocketAddr::has_abstract_addresses() {
assert!(UnixSocketAddr::new("@abstract").unwrap().is_abstract());
assert!(UnixSocketAddr::new("\0abstract").unwrap().is_abstract());
} else {
assert!(UnixSocketAddr::new("@abstract").is_err());
assert!(UnixSocketAddr::new("\0abstract").is_err());
}
Escaped path address:
assert!(UnixSocketAddr::new("./@path").unwrap().is_relative_path());
Unnamed address:
assert!(UnixSocketAddr::new("").unwrap().is_unnamed());
pub fn new_unspecified() -> UnixSocketAddr
pub fn new_unspecified() -> UnixSocketAddr
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 doing so binds the socket to a random abstract address determined by the OS.
Examples
let addr = UnixSocketAddr::new_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
pub fn max_path_len() -> usize
Returns the maximum size of pathname addresses supported by UnixSocketAddr
.
Is 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>(path: &P) -> Result<UnixSocketAddr, Error> where
P: AsRef<Path> + ?Sized,
pub fn from_path<P>(path: &P) -> Result<UnixSocketAddr, Error> where
P: AsRef<Path> + ?Sized,
Creates 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
pub fn max_abstract_len() -> usize
Returns maximum size of abstract addesses supported by UnixSocketAddr
.
Is the size of the underlying sun_path
field minus 1 for the
leading '\0'
byte.
This value is also returned on operating systems that doesn’t support abstract addresses.
pub const fn has_abstract_addresses() -> bool
pub const fn has_abstract_addresses() -> bool
Returns whether the operating system is known to support abstract unix domain socket addresses.
Is true
for Linux & Android, and false
for all other OSes.
pub fn from_abstract<N>(name: &N) -> Result<UnixSocketAddr, Error> where
N: AsRef<[u8]> + ?Sized,
pub fn from_abstract<N>(name: &N) -> Result<UnixSocketAddr, Error> where
N: AsRef<[u8]> + ?Sized,
Creates an abstract unix domain socket address.
Abstract addresses use a namespace separate from the file system, that doesn’t have directories (ie. is flat) or permissions. The advandage of it is that the address disappear when the socket bound to it is closed, which frees one from dealing with removing it when shutting down cleanly.
They are a Linux-only feature though, and this function will fail if abstract addresses are not supported.
Errors
This function will return an error if the name is too long.
Call max_abstract_len()
get the limit.
It will also fail on operating systems that don’t support abstract addresses. (ie. anything other than Linux and Android)
pub fn from_std(addr: SocketAddr) -> Option<UnixSocketAddr>
pub fn from_std(addr: SocketAddr) -> Option<UnixSocketAddr>
Tries to convert a std::os::unix::net::SocketAddr
into an UnixSocketAddr
.
This can fail (produce None
) on Linux and Android
if the std
SocketAddr
represents an abstract address,
as it provides no method for viewing abstract addresses.
(other than parsing its Debug
output, anyway.)
pub fn from_c_str(path: &CStr) -> Result<UnixSocketAddr, Error>
pub fn from_c_str(path: &CStr) -> Result<UnixSocketAddr, Error>
Returns 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
pub fn is_unnamed(&self) -> bool
Checks whether the address is unnamed.
pub fn is_abstract(&self) -> bool
pub fn is_abstract(&self) -> bool
Checks whether the address is a name in the abstract namespace.
Always returns false
on operating systems that don’t support abstract
addresses.
pub fn is_absolute_path(&self) -> bool
pub fn is_absolute_path(&self) -> bool
Checks whether the address is a path that begins with ‘/’.
pub fn is_relative_path(&self) -> bool
pub fn is_relative_path(&self) -> bool
Checks whether the address is a path that doesn’t begin with ‘/’.
pub fn name(&self) -> AddrName<'_>
pub fn name(&self) -> AddrName<'_>
Returns a view of the address that can be pattern matched to the differnt types of addresses.
Examples
use uds::{UnixSocketAddr, AddrName};
use std::path::Path;
assert_eq!(
UnixSocketAddr::new_unspecified().name(),
AddrName::Unnamed
);
assert_eq!(
UnixSocketAddr::from_path("/var/run/socket.sock").unwrap().name(),
AddrName::Path(Path::new("/var/run/socket.sock"))
);
if UnixSocketAddr::has_abstract_addresses() {
assert_eq!(
UnixSocketAddr::from_abstract("tcartsba").unwrap().name(),
AddrName::Abstract(b"tcartsba")
);
}
pub fn as_pathname(&self) -> Option<&Path>
pub fn as_pathname(&self) -> Option<&Path>
Returns the path of a path-based address.
pub fn as_abstract(&self) -> Option<&[u8]>
pub fn as_abstract(&self) -> Option<&[u8]>
Returns the name of an address which is in the abstract namespace.
pub fn as_ref(&self) -> AddrName<'_>
pub fn as_ref(&self) -> AddrName<'_>
Returns a view that can be pattern matched to the differnt types of addresses.
Examples
use uds::{UnixDatagramExt, UnixSocketAddr, UnixSocketAddrRef};
use std::os::unix::net::UnixDatagram;
use std::path::Path;
let receiver = UnixDatagram::bind("dgram.socket").expect("create datagram socket");
assert_eq!(
receiver.local_unix_addr().unwrap().as_ref(),
UnixSocketAddrRef::Path(Path::new("dgram.socket"))
);
let sender = UnixDatagram::unbound().expect("create unbound datagram socket");
sender.send_to(b"I can't hear you", "dgram.socket").expect("send");
let mut buf = [0; 100];
let (len, addr) = receiver.recv_from_unix_addr(&mut buf).unwrap();
assert_eq!(addr.as_ref(), UnixSocketAddrRef::Unnamed);
pub fn from_raw_bytes(addr: &[u8]) -> Result<UnixSocketAddr, Error>
pub fn from_raw_bytes(addr: &[u8]) -> Result<UnixSocketAddr, Error>
Creates an address from a slice of bytes to place in sun_path
.
This is a low-level but simple interface for creating addresses by
other unix socket wrappers without exposing any libc types.
The meaning of a slice can vary between operating systems.
addr
should point to thes start of the “path” part of a socket
address, with length being the number of valid bytes of the path.
(Trailing NULs are not stripped by this function.)
Errors
If the slice is longer than sun_path
, an error of kind Other
is
returned. No other validation of the bytes is performed.
Examples
A normal path-based address
let addr = UnixSocketAddr::from_raw_bytes(b"/var/run/a.sock\0").unwrap();
assert_eq!(addr.as_pathname(), Some(Path::new("/var/run/a.sock")));
assert_eq!(addr.as_raw_bytes(), b"/var/run/a.sock\0");
On Linux:
let addr = UnixSocketAddr::from_raw_bytes(b"\0a").unwrap();
assert_eq!(addr.as_abstract(), Some(&b"a"[..]));
assert_eq!(addr.as_raw_bytes(), b"\0a");
Elsewhere:
let addr = UnixSocketAddr::from_raw_bytes(b"\0a").unwrap();
assert!(addr.is_unnamed());
assert_eq!(addr.as_raw_bytes().len(), 2);
A portable unnamed address:
let addr = UnixSocketAddr::from_raw_bytes(&[]).expect("not too long");
assert!(addr.is_unnamed());
assert!(addr.as_raw_bytes().is_empty());
pub fn as_raw_bytes(&self) -> &[u8]ⓘNotable traits for &mut [u8]impl<'_> Write for &mut [u8]impl<'_> Read for &[u8]
pub fn as_raw_bytes(&self) -> &[u8]ⓘNotable traits for &mut [u8]impl<'_> Write for &mut [u8]impl<'_> Read for &[u8]
Returns a low-level but view of the address without using any libc types.
The returned slice points to the start of sun_addr
of the contained
sockaddr_un
, and the length is the number of bytes of sun_addr
that were filled out by the OS. Any trailing NUL(s) will be preserved.
Examples
A normal path-based address
let pathname = "a file";
let socket = UnixDatagram::bind(pathname).expect("create datagram socket");
let addr = socket.local_unix_addr().expect("get its address");
assert!(addr.as_raw_bytes().starts_with(pathname.as_bytes()));
assert!(addr.as_raw_bytes()[pathname.len()..].iter().all(|&b| b == b'\0' ));
assert_eq!(addr.as_pathname(), Some(Path::new(pathname)));
Abstract address:
let addr = UnixSocketAddr::new("@someone@").unwrap();
assert_eq!(addr.as_raw_bytes(), b"\0someone@");
Unnamed address on macOS, OpenBSD and maybe others:
let socket = UnixDatagram::unbound().expect("create datagram socket");
let addr = socket.local_unix_addr().expect("get its unbound address");
let bytes = addr.as_raw_bytes();
assert!(bytes.len() > 0);
assert!(bytes.iter().all(|&b| b == b'\0' ));
assert!(addr.is_unnamed());
pub fn new_from_ffi<R, F>(call: F) -> Result<(R, UnixSocketAddr), Error> where
F: for<'_, '_> FnOnce(&mut sockaddr, &mut u32) -> Result<R, Error>,
pub fn new_from_ffi<R, F>(call: F) -> Result<(R, UnixSocketAddr), Error> where
F: for<'_, '_> FnOnce(&mut sockaddr, &mut u32) -> Result<R, Error>,
Prepares a struct sockaddr*
and socklen_t*
for passing to FFI
(such as getsockname()
, getpeername()
, or accept()
),
and validate and normalize the produced address afterwards.
Validation:
- Check that the address family is
AF_UNIX
. - Check that the address wasn’t truncated (the
socklen_t
is too big).
Normalization:
- Ensure path addresses have a trailing NUL byte if there is space.
pub unsafe fn from_raw(
addr: *const sockaddr,
len: u32
) -> Result<UnixSocketAddr, Error>
pub unsafe fn from_raw(
addr: *const sockaddr,
len: u32
) -> Result<UnixSocketAddr, Error>
Creates an UnixSocketAddr
from a pointer to a generic sockaddr
and
a length.
Safety
len
must not be greater than the size of the memoryaddr
points to.addr
must point to valid memory iflen
is greater than zero, or be NULL.
pub unsafe fn from_raw_unchecked(addr: sockaddr_un, len: u32) -> UnixSocketAddr
pub unsafe fn from_raw_unchecked(addr: sockaddr_un, len: u32) -> UnixSocketAddr
Creates an UnixSocketAddr
without any validation.
Safety
len
must be<= size_of::<sockaddr_un>()
.addr.sun_family
should beAF_UNIX
or strange things might happen.addr.sun_len
, if it exists, should be zero (but is probably ignored).
pub fn into_raw(self) -> (sockaddr_un, u32)
pub fn into_raw(self) -> (sockaddr_un, u32)
Splits the address into its inner, raw parts.
pub fn as_raw_general(&self) -> (&sockaddr, u32)
pub fn as_raw_general(&self) -> (&sockaddr, u32)
Returns 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, u32)
pub fn as_raw(&self) -> (&sockaddr_un, u32)
Returns a reference to the inner struct sockaddr_un
, and length.
pub unsafe fn as_raw_mut_general(&mut self) -> (&mut sockaddr, &mut u32)
pub unsafe fn as_raw_mut_general(&mut self) -> (&mut sockaddr, &mut u32)
Returns 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 u32)
pub unsafe fn as_raw_mut(&mut self) -> (&mut sockaddr_un, &mut u32)
Returns 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 Clone for UnixSocketAddr
impl Clone for UnixSocketAddr
fn clone(&self) -> UnixSocketAddr
fn clone(&self) -> UnixSocketAddr
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
impl Debug for UnixSocketAddr
impl Debug for UnixSocketAddr
impl Default for UnixSocketAddr
impl Default for UnixSocketAddr
fn default() -> UnixSocketAddr
fn default() -> UnixSocketAddr
Returns the “default value” for a type. Read more
impl Display for UnixSocketAddr
impl Display for UnixSocketAddr
impl<'a> From<&'a UnixSocketAddr> for AddrName<'a>
impl<'a> From<&'a UnixSocketAddr> for AddrName<'a>
fn from(addr: &'a UnixSocketAddr) -> AddrName<'a>
fn from(addr: &'a UnixSocketAddr) -> AddrName<'a>
Converts to this type from the input type.
impl Hash for UnixSocketAddr
impl Hash for UnixSocketAddr
impl PartialEq<[u8]> for UnixSocketAddr
impl PartialEq<[u8]> for UnixSocketAddr
impl PartialEq<UnixSocketAddr> for UnixSocketAddr
impl PartialEq<UnixSocketAddr> for UnixSocketAddr
impl PartialEq<UnixSocketAddr> for [u8]
impl PartialEq<UnixSocketAddr> for [u8]
impl Copy for UnixSocketAddr
impl Eq for UnixSocketAddr
Auto Trait Implementations
impl RefUnwindSafe for UnixSocketAddr
impl Send for UnixSocketAddr
impl Sync for UnixSocketAddr
impl Unpin for UnixSocketAddr
impl UnwindSafe for UnixSocketAddr
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<T> Downcast for T where
T: Any,
impl<T> Downcast for T where
T: Any,
fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>ⓘNotable traits for Box<W, Global>impl<W> Write for Box<W, Global> where
W: Write + ?Sized, impl<R> Read for Box<R, Global> where
R: Read + ?Sized, impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;
fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>ⓘNotable traits for Box<W, Global>impl<W> Write for Box<W, Global> where
W: Write + ?Sized, impl<R> Read for Box<R, Global> where
R: Read + ?Sized, impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;
W: Write + ?Sized, impl<R> Read for Box<R, Global> where
R: Read + ?Sized, impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;
Convert Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
. Read more
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
Convert Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
. Read more
fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Convert &Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s. Read more
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Convert &mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s. Read more
impl<A> DynCastExt for A
impl<A> DynCastExt for A
fn dyn_cast<T>(
self
) -> Result<<A as DynCastExtHelper<T>>::Target, <A as DynCastExtHelper<T>>::Source> where
A: DynCastExtHelper<T>,
T: ?Sized,
fn dyn_cast<T>(
self
) -> Result<<A as DynCastExtHelper<T>>::Target, <A as DynCastExtHelper<T>>::Source> where
A: DynCastExtHelper<T>,
T: ?Sized,
Use this to cast from one trait object type to another. Read more
fn dyn_upcast<T>(self) -> <A as DynCastExtAdvHelper<T, T>>::Target where
A: DynCastExtAdvHelper<T, T, Source = <A as DynCastExtAdvHelper<T, T>>::Target>,
T: ?Sized,
fn dyn_upcast<T>(self) -> <A as DynCastExtAdvHelper<T, T>>::Target where
A: DynCastExtAdvHelper<T, T, Source = <A as DynCastExtAdvHelper<T, T>>::Target>,
T: ?Sized,
Use this to upcast a trait to one of its supertraits. Read more
fn dyn_cast_adv<F, T>(
self
) -> Result<<A as DynCastExtAdvHelper<F, T>>::Target, <A as DynCastExtAdvHelper<F, T>>::Source> where
A: DynCastExtAdvHelper<F, T>,
F: ?Sized,
T: ?Sized,
fn dyn_cast_adv<F, T>(
self
) -> Result<<A as DynCastExtAdvHelper<F, T>>::Target, <A as DynCastExtAdvHelper<F, T>>::Source> where
A: DynCastExtAdvHelper<F, T>,
F: ?Sized,
T: ?Sized,
fn dyn_cast_with_config<C>(
self
) -> Result<<A as DynCastExtAdvHelper<<C as DynCastConfig>::Source, <C as DynCastConfig>::Target>>::Target, <A as DynCastExtAdvHelper<<C as DynCastConfig>::Source, <C as DynCastConfig>::Target>>::Source> where
C: DynCastConfig,
A: DynCastExtAdvHelper<<C as DynCastConfig>::Source, <C as DynCastConfig>::Target>,
fn dyn_cast_with_config<C>(
self
) -> Result<<A as DynCastExtAdvHelper<<C as DynCastConfig>::Source, <C as DynCastConfig>::Target>>::Target, <A as DynCastExtAdvHelper<<C as DynCastConfig>::Source, <C as DynCastConfig>::Target>>::Source> where
C: DynCastConfig,
A: DynCastExtAdvHelper<<C as DynCastConfig>::Source, <C as DynCastConfig>::Target>,
Use this to cast from one trait object type to another. With this method the type parameter is a config type that uniquely specifies which cast should be preformed. Read more
sourceimpl<Q, K> Equivalent<K> for Q where
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Q where
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
sourcefn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to key
and return true
if they are equal.
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>ⓘNotable traits for Instrumented<T>impl<T> Future for Instrumented<T> where
T: Future, type Output = <T as Future>::Output;
fn instrument(self, span: Span) -> Instrumented<Self>ⓘNotable traits for Instrumented<T>impl<T> Future for Instrumented<T> where
T: Future, type Output = <T as Future>::Output;
T: Future, type Output = <T as Future>::Output;
sourcefn in_current_span(self) -> Instrumented<Self>ⓘNotable traits for Instrumented<T>impl<T> Future for Instrumented<T> where
T: Future, type Output = <T as Future>::Output;
fn in_current_span(self) -> Instrumented<Self>ⓘNotable traits for Instrumented<T>impl<T> Future for Instrumented<T> where
T: Future, type Output = <T as Future>::Output;
T: Future, type Output = <T as Future>::Output;
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T> Future for WithDispatch<T> where
T: Future, type Output = <T as Future>::Output;
where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T> Future for WithDispatch<T> where
T: Future, type Output = <T as Future>::Output;
where
S: Into<Dispatch>,
T: Future, type Output = <T as Future>::Output;
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T> Future for WithDispatch<T> where
T: Future, type Output = <T as Future>::Output;
fn with_current_subscriber(self) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T> Future for WithDispatch<T> where
T: Future, type Output = <T as Future>::Output;
T: Future, type Output = <T as Future>::Output;
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more