Skip to main content

std/os/unix/net/
addr.rs

1use crate::bstr::ByteStr;
2use crate::ffi::OsStr;
3#[cfg(any(doc, target_os = "android", target_os = "linux", target_os = "cygwin"))]
4use crate::os::net::linux_ext;
5use crate::os::unix::ffi::OsStrExt;
6use crate::path::Path;
7use crate::sys::cvt;
8use crate::{fmt, io, mem, ptr};
9
10// FIXME(#43348): Make libc adapt #[doc(cfg(...))] so we don't need these fake definitions here?
11#[cfg(not(unix))]
12#[allow(non_camel_case_types)]
13mod libc {
14    pub use core::ffi::c_int;
15    pub type socklen_t = u32;
16    pub struct sockaddr;
17    #[derive(Clone)]
18    pub struct sockaddr_un {
19        pub sun_path: [u8; 1],
20    }
21}
22
23const SUN_PATH_OFFSET: usize = mem::offset_of!(libc::sockaddr_un, sun_path);
24
25pub(super) fn sockaddr_un(path: &Path) -> io::Result<(libc::sockaddr_un, libc::socklen_t)> {
26    // SAFETY: All zeros is a valid representation for `sockaddr_un`.
27    let mut addr: libc::sockaddr_un = unsafe { mem::zeroed() };
28    addr.sun_family = libc::AF_UNIX as libc::sa_family_t;
29
30    let bytes = path.as_os_str().as_bytes();
31
32    if bytes.contains(&0) {
33        return Err(io::const_error!(
34            io::ErrorKind::InvalidInput,
35            "paths must not contain interior null bytes",
36        ));
37    }
38
39    if bytes.len() >= addr.sun_path.len() {
40        return Err(io::const_error!(
41            io::ErrorKind::InvalidInput,
42            "path must be shorter than SUN_LEN",
43        ));
44    }
45    // SAFETY: `bytes` and `addr.sun_path` are not overlapping and
46    // both point to valid memory.
47    // NOTE: We zeroed the memory above, so the path is already null
48    // terminated.
49    unsafe {
50        ptr::copy_nonoverlapping(bytes.as_ptr(), addr.sun_path.as_mut_ptr().cast(), bytes.len())
51    };
52
53    let mut len = SUN_PATH_OFFSET + bytes.len();
54    match bytes.get(0) {
55        Some(&0) | None => {}
56        Some(_) => {
57            // on QNX7.1 and QNX8 the `len` value returned by the SUN_LEN
58            // macro in its libc does not include the null byte in the count so
59            // don't add it here to match what a C program passes to bind(2) and
60            // similar functions
61            if cfg!(not(any(target_env = "nto80", target_env = "nto71"))) {
62                len += 1
63            }
64        }
65    }
66    Ok((addr, len as libc::socklen_t))
67}
68
69enum AddressKind<'a> {
70    Unnamed,
71    Pathname(&'a Path),
72    Abstract(&'a ByteStr),
73}
74
75/// An address associated with a Unix socket.
76///
77/// # Examples
78///
79/// ```
80/// use std::os::unix::net::UnixListener;
81///
82/// let socket = match UnixListener::bind("/tmp/sock") {
83///     Ok(sock) => sock,
84///     Err(e) => {
85///         println!("Couldn't bind: {e:?}");
86///         return
87///     }
88/// };
89/// let addr = socket.local_addr().expect("Couldn't get local address");
90/// ```
91#[derive(Clone)]
92#[stable(feature = "unix_socket", since = "1.10.0")]
93pub struct SocketAddr {
94    pub(super) addr: libc::sockaddr_un,
95    pub(super) len: libc::socklen_t,
96}
97
98impl SocketAddr {
99    pub(super) fn new<F>(f: F) -> io::Result<SocketAddr>
100    where
101        F: FnOnce(*mut libc::sockaddr, *mut libc::socklen_t) -> libc::c_int,
102    {
103        unsafe {
104            let mut addr: libc::sockaddr_un = mem::zeroed();
105            let mut len = size_of::<libc::sockaddr_un>() as libc::socklen_t;
106            cvt(f((&raw mut addr) as *mut _, &mut len))?;
107            SocketAddr::from_parts(addr, len)
108        }
109    }
110
111    pub(super) fn from_parts(
112        addr: libc::sockaddr_un,
113        mut len: libc::socklen_t,
114    ) -> io::Result<SocketAddr> {
115        if cfg!(target_os = "openbsd") {
116            // on OpenBSD, getsockname(2) returns the actual size of the socket address,
117            // and not the len of the content. Figure out the length for ourselves.
118            // https://marc.info/?l=openbsd-bugs&m=170105481926736&w=2
119            let sun_path: &[u8] =
120                unsafe { mem::transmute::<&[libc::c_char], &[u8]>(&addr.sun_path) };
121            len = core::slice::memchr::memchr(0, sun_path)
122                .map_or(len, |new_len| (new_len + SUN_PATH_OFFSET) as libc::socklen_t);
123        }
124
125        if len == 0 {
126            // When there is a datagram from unnamed unix socket
127            // linux returns zero bytes of address
128            len = SUN_PATH_OFFSET as libc::socklen_t; // i.e., zero-length address
129        } else if addr.sun_family != libc::AF_UNIX as libc::sa_family_t {
130            return Err(io::const_error!(
131                io::ErrorKind::InvalidInput,
132                "file descriptor did not correspond to a Unix socket",
133            ));
134        }
135
136        Ok(SocketAddr { addr, len })
137    }
138
139    /// Constructs a `SockAddr` with the family `AF_UNIX` and the provided path.
140    ///
141    /// # Errors
142    ///
143    /// Returns an error if the path is longer than `SUN_LEN` or if it contains
144    /// NULL bytes.
145    ///
146    /// # Examples
147    ///
148    /// ```
149    /// use std::os::unix::net::SocketAddr;
150    /// use std::path::Path;
151    ///
152    /// # fn main() -> std::io::Result<()> {
153    /// let address = SocketAddr::from_pathname("/path/to/socket")?;
154    /// assert_eq!(address.as_pathname(), Some(Path::new("/path/to/socket")));
155    /// # Ok(())
156    /// # }
157    /// ```
158    ///
159    /// Creating a `SocketAddr` with a NULL byte results in an error.
160    ///
161    /// ```
162    /// use std::os::unix::net::SocketAddr;
163    ///
164    /// assert!(SocketAddr::from_pathname("/path/with/\0/bytes").is_err());
165    /// ```
166    #[stable(feature = "unix_socket_creation", since = "1.61.0")]
167    pub fn from_pathname<P>(path: P) -> io::Result<SocketAddr>
168    where
169        P: AsRef<Path>,
170    {
171        sockaddr_un(path.as_ref()).map(|(addr, len)| SocketAddr { addr, len })
172    }
173
174    /// Returns `true` if the address is unnamed.
175    ///
176    /// # Examples
177    ///
178    /// A named address:
179    ///
180    /// ```no_run
181    /// use std::os::unix::net::UnixListener;
182    ///
183    /// fn main() -> std::io::Result<()> {
184    ///     let socket = UnixListener::bind("/tmp/sock")?;
185    ///     let addr = socket.local_addr().expect("Couldn't get local address");
186    ///     assert_eq!(addr.is_unnamed(), false);
187    ///     Ok(())
188    /// }
189    /// ```
190    ///
191    /// An unnamed address:
192    ///
193    /// ```
194    /// use std::os::unix::net::UnixDatagram;
195    ///
196    /// fn main() -> std::io::Result<()> {
197    ///     let socket = UnixDatagram::unbound()?;
198    ///     let addr = socket.local_addr().expect("Couldn't get local address");
199    ///     assert_eq!(addr.is_unnamed(), true);
200    ///     Ok(())
201    /// }
202    /// ```
203    #[must_use]
204    #[stable(feature = "unix_socket", since = "1.10.0")]
205    pub fn is_unnamed(&self) -> bool {
206        matches!(self.address(), AddressKind::Unnamed)
207    }
208
209    /// Returns the contents of this address if it is a `pathname` address.
210    ///
211    /// # Examples
212    ///
213    /// With a pathname:
214    ///
215    /// ```no_run
216    /// use std::os::unix::net::UnixListener;
217    /// use std::path::Path;
218    ///
219    /// fn main() -> std::io::Result<()> {
220    ///     let socket = UnixListener::bind("/tmp/sock")?;
221    ///     let addr = socket.local_addr().expect("Couldn't get local address");
222    ///     assert_eq!(addr.as_pathname(), Some(Path::new("/tmp/sock")));
223    ///     Ok(())
224    /// }
225    /// ```
226    ///
227    /// Without a pathname:
228    ///
229    /// ```
230    /// use std::os::unix::net::UnixDatagram;
231    ///
232    /// fn main() -> std::io::Result<()> {
233    ///     let socket = UnixDatagram::unbound()?;
234    ///     let addr = socket.local_addr().expect("Couldn't get local address");
235    ///     assert_eq!(addr.as_pathname(), None);
236    ///     Ok(())
237    /// }
238    /// ```
239    #[stable(feature = "unix_socket", since = "1.10.0")]
240    #[must_use]
241    pub fn as_pathname(&self) -> Option<&Path> {
242        if let AddressKind::Pathname(path) = self.address() { Some(path) } else { None }
243    }
244
245    fn address(&self) -> AddressKind<'_> {
246        let len = self.len as usize - SUN_PATH_OFFSET;
247        let path = unsafe { mem::transmute::<&[libc::c_char], &[u8]>(&self.addr.sun_path) };
248
249        // macOS seems to return a len of 16 and a zeroed sun_path for unnamed addresses
250        if len == 0
251            || (cfg!(not(any(target_os = "linux", target_os = "android", target_os = "cygwin")))
252                && self.addr.sun_path[0] == 0)
253        {
254            AddressKind::Unnamed
255        } else if self.addr.sun_path[0] == 0 {
256            AddressKind::Abstract(ByteStr::from_bytes(&path[1..len]))
257        } else {
258            // the value returned by getsockname(2) and similar on QNX7.1 and
259            // QNX8 does not count the NUL byte terminator of the path string,
260            // which matches the behavior of the SUN_LEN macro in libc, but
261            // other OSes do count the NUL byte so adjust accordingly
262            let end =
263                if cfg!(any(target_env = "nto80", target_env = "nto71")) { len } else { len - 1 };
264            AddressKind::Pathname(OsStr::from_bytes(&path[..end]).as_ref())
265        }
266    }
267}
268
269#[doc(cfg(any(target_os = "android", target_os = "linux", target_os = "cygwin")))]
270#[cfg(any(doc, target_os = "android", target_os = "linux", target_os = "cygwin"))]
271#[stable(feature = "unix_socket_abstract", since = "1.70.0")]
272impl linux_ext::addr::SocketAddrExt for SocketAddr {
273    fn as_abstract_name(&self) -> Option<&[u8]> {
274        if let AddressKind::Abstract(name) = self.address() { Some(name.as_bytes()) } else { None }
275    }
276
277    fn from_abstract_name<N>(name: N) -> io::Result<Self>
278    where
279        N: AsRef<[u8]>,
280    {
281        let name = name.as_ref();
282        unsafe {
283            let mut addr: libc::sockaddr_un = mem::zeroed();
284            addr.sun_family = libc::AF_UNIX as libc::sa_family_t;
285
286            if name.len() + 1 > addr.sun_path.len() {
287                return Err(io::const_error!(
288                    io::ErrorKind::InvalidInput,
289                    "abstract socket name must be shorter than SUN_LEN",
290                ));
291            }
292
293            crate::ptr::copy_nonoverlapping(
294                name.as_ptr(),
295                addr.sun_path.as_mut_ptr().add(1) as *mut u8,
296                name.len(),
297            );
298            let len = (SUN_PATH_OFFSET + 1 + name.len()) as libc::socklen_t;
299            SocketAddr::from_parts(addr, len)
300        }
301    }
302}
303
304#[stable(feature = "unix_socket", since = "1.10.0")]
305impl fmt::Debug for SocketAddr {
306    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
307        match self.address() {
308            AddressKind::Unnamed => write!(fmt, "(unnamed)"),
309            AddressKind::Abstract(name) => write!(fmt, "{name:?} (abstract)"),
310            AddressKind::Pathname(path) => write!(fmt, "{path:?} (pathname)"),
311        }
312    }
313}