tor_general_addr/
unix.rs

1//! Definitions related to unix domain socket support.
2//!
3//! To avoid confusion, don't import `SocketAddr` from this module directly;
4//! instead, import the module and refer to `unix::SocketAddr`.
5
6#[cfg(not(unix))]
7use std::path::Path;
8
9/// A replacement/re-export [`std::os::unix::net::SocketAddr`].
10///
11/// On Unix platforms, this is just a re-export of [`std::os::unix::net::SocketAddr`].
12///
13/// On non-Unix platforms, this type is an uninhabited placeholder that can never be instantiated.
14#[cfg(unix)]
15pub use std::os::unix::net::SocketAddr;
16
17/// Address for an AF_UNIX socket.
18///
19/// (This is an uninhabited placeholder implementations for platforms without AF_UNIX support.)
20///
21/// Note that we currently include Windows on platforms without AF_UNIX support:
22/// When we use unix domain sockets in Arti, we rely on their filesystem-based security properties,
23/// which we haven't yet had a chance to fully analyze on non-Unix platforms.
24#[cfg(not(unix))]
25#[derive(Debug, Clone)]
26pub struct SocketAddr(void::Void);
27
28#[cfg(not(unix))]
29impl SocketAddr {
30    /// Return true if this is an "unnamed" socket address.
31    ///
32    /// (Because this type is uninhabited, this method cannot actually be called.)
33    pub fn is_unnamed(&self) -> bool {
34        void::unreachable(self.0)
35    }
36    /// Return the pathname for this socket address, if it is "named".
37    ///
38    /// (Because this type is uninhabited, this method cannot actually be called.)
39    pub fn as_pathname(&self) -> Option<&Path> {
40        void::unreachable(self.0)
41    }
42    /// Attempt to construct an AF_UNIX socket address from the provided `path`.
43    ///
44    /// (Because this platform lacks AF_UNIX support, this method will always return an error.)
45    pub fn from_pathname<P: AsRef<Path>>(path: P) -> std::io::Result<Self> {
46        let _ = path;
47        Err(NoAfUnixSocketSupport.into())
48    }
49}
50
51/// Error: Unix domain sockets are not supported on this platform.
52#[derive(Clone, Debug, Default, thiserror::Error)]
53#[error("No support for unix domain sockets on this platform")]
54#[non_exhaustive]
55pub struct NoAfUnixSocketSupport;
56
57/// Deprecated name for `NoAfUnixSocketSupport`
58#[deprecated]
59pub type NoUnixAddressSupport = NoAfUnixSocketSupport;
60
61impl From<NoAfUnixSocketSupport> for std::io::Error {
62    fn from(value: NoAfUnixSocketSupport) -> Self {
63        std::io::Error::new(std::io::ErrorKind::Unsupported, value)
64    }
65}