1//! Linux and Android-specific extensions to socket addresses.
2
3use crate::os::unix::net::SocketAddr;
4use crate::sealed::Sealed;
5
6/// Platform-specific extensions to [`SocketAddr`].
7#[stable(feature = "unix_socket_abstract", since = "1.70.0")]
8pub trait SocketAddrExt: Sealed {
9 /// Creates a Unix socket address in the abstract namespace.
10 ///
11 /// The abstract namespace is a Linux-specific extension that allows Unix
12 /// sockets to be bound without creating an entry in the filesystem.
13 /// Abstract sockets are unaffected by filesystem layout or permissions,
14 /// and no cleanup is necessary when the socket is closed.
15 ///
16 /// An abstract socket address name may contain any bytes, including zero.
17 ///
18 /// # Errors
19 ///
20 /// Returns an error if the name is longer than `SUN_LEN - 1`.
21 ///
22 /// # Examples
23 ///
24 /// ```no_run
25 /// use std::os::unix::net::{UnixListener, SocketAddr};
26 /// use std::os::linux::net::SocketAddrExt;
27 ///
28 /// fn main() -> std::io::Result<()> {
29 /// let addr = SocketAddr::from_abstract_name(b"hidden")?;
30 /// let listener = match UnixListener::bind_addr(&addr) {
31 /// Ok(sock) => sock,
32 /// Err(err) => {
33 /// println!("Couldn't bind: {err:?}");
34 /// return Err(err);
35 /// }
36 /// };
37 /// Ok(())
38 /// }
39 /// ```
40 #[stable(feature = "unix_socket_abstract", since = "1.70.0")]
41 fn from_abstract_name<N>(name: N) -> crate::io::Result<SocketAddr>
42 where
43 N: AsRef<[u8]>;
44
45 /// Returns the contents of this address if it is in the abstract namespace.
46 ///
47 /// # Examples
48 ///
49 /// ```no_run
50 /// use std::os::unix::net::{UnixListener, SocketAddr};
51 /// use std::os::linux::net::SocketAddrExt;
52 ///
53 /// fn main() -> std::io::Result<()> {
54 /// let name = b"hidden";
55 /// let name_addr = SocketAddr::from_abstract_name(name)?;
56 /// let socket = UnixListener::bind_addr(&name_addr)?;
57 /// let local_addr = socket.local_addr().expect("Couldn't get local address");
58 /// assert_eq!(local_addr.as_abstract_name(), Some(&name[..]));
59 /// Ok(())
60 /// }
61 /// ```
62 #[stable(feature = "unix_socket_abstract", since = "1.70.0")]
63 fn as_abstract_name(&self) -> Option<&[u8]>;
64}
65