1
//! Definitions related to unix 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))]
7
use 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)]
15
pub 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 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)]
26
pub struct SocketAddr(void::Void);
27

            
28
#[cfg(not(unix))]
29
impl 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(NoUnixAddressSupport.into())
48
    }
49
}
50

            
51
/// Error: Unix addresses are not supported on this platform.
52
#[derive(Clone, Debug, Default, thiserror::Error)]
53
#[error("No support for AF_UNIX addresses on this platform")]
54
#[non_exhaustive]
55
pub struct NoUnixAddressSupport;
56

            
57
impl From<NoUnixAddressSupport> for std::io::Error {
58
    fn from(value: NoUnixAddressSupport) -> Self {
59
        std::io::Error::new(std::io::ErrorKind::Unsupported, value)
60
    }
61
}