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