Skip to main content

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// Void is used only as a stand-in for unix::SocketAddr.  Avoid unused crate warning.
18//
19// (We can't express the right dependency condition in Cargo.toml,
20// and anyway it's not worth trying to conditionally suppress the dependency.)
21#[cfg(unix)]
22use void as _;
23
24/// Address for an AF_UNIX socket.
25///
26/// (This is an uninhabited placeholder implementations for platforms without AF_UNIX support.)
27///
28/// Note that we currently include Windows on platforms without AF_UNIX support:
29/// When we use unix domain sockets in Arti, we rely on their filesystem-based security properties,
30/// which we haven't yet had a chance to fully analyze on non-Unix platforms.
31#[cfg(not(unix))]
32#[derive(Debug, Clone)]
33pub struct SocketAddr(void::Void);
34
35#[cfg(not(unix))]
36impl SocketAddr {
37    /// Return true if this is an "unnamed" socket address.
38    ///
39    /// (Because this type is uninhabited, this method cannot actually be called.)
40    pub fn is_unnamed(&self) -> bool {
41        void::unreachable(self.0)
42    }
43    /// Return the pathname for this socket address, if it is "named".
44    ///
45    /// (Because this type is uninhabited, this method cannot actually be called.)
46    pub fn as_pathname(&self) -> Option<&Path> {
47        void::unreachable(self.0)
48    }
49    /// Attempt to construct an AF_UNIX socket address from the provided `path`.
50    ///
51    /// (Because this platform lacks AF_UNIX support, this method will always return an error.)
52    pub fn from_pathname<P: AsRef<Path>>(path: P) -> std::io::Result<Self> {
53        let _ = path;
54        Err(NoAfUnixSocketSupport.into())
55    }
56}
57
58/// Error: Unix domain sockets are not supported on this platform.
59#[derive(Clone, Debug, Default, thiserror::Error)]
60#[error("No support for unix domain sockets on this platform")]
61#[non_exhaustive]
62pub struct NoAfUnixSocketSupport;
63
64/// Deprecated name for `NoAfUnixSocketSupport`
65#[deprecated]
66pub type NoUnixAddressSupport = NoAfUnixSocketSupport;
67
68impl From<NoAfUnixSocketSupport> for std::io::Error {
69    fn from(value: NoAfUnixSocketSupport) -> Self {
70        std::io::Error::new(std::io::ErrorKind::Unsupported, value)
71    }
72}