summaryrefslogtreecommitdiff
path: root/crates/tor-rtcompat/src/unix.rs
blob: 86c7f122c5ffa9352ede04944a9df4d3c459f919 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//! Definitions related to unix socket support.
//!
//! To avoid confusion, don't import `SocketAddr` from this module directly;
//! instead, import the module and refer to `unix::SocketAddr`.

#[cfg(not(unix))]
use std::path::Path;

#[cfg(unix)]
pub use std::os::unix::net::SocketAddr;

/// Helper: construct an unnamed SocketAddr.
#[cfg(unix)]
pub(crate) fn new_unnamed_socketaddr() -> std::io::Result<SocketAddr> {
    // There SHOULD be a better way to do this in legitimate Rust!
    // But sadly, there isn't.
    SocketAddr::from_pathname("")
}

/// Address for an AF_UNIX socket.
///
/// (This is an uninhabited placeholder implementations for platforms without AF_UNIX support.)
///
/// Note that we currently include Windows on platforms without AF_UNIX support:
/// When we use Unix sockets in Arti, we rely on their filesystem-based security properties,
/// which we haven't yet had a chance to fully analyze on non-Unix platforms.
#[cfg(not(unix))]
#[derive(Debug, Clone)]
pub struct SocketAddr(void::Void);

#[cfg(not(unix))]
impl SocketAddr {
    /// Return true if this is an "unnamed" socket address.
    ///
    /// (Because this type is uninhabited, this method cannot actually be called.)
    pub fn is_unnamed(&self) -> bool {
        void::unreachable(self.0)
    }
    /// Return the pathname for this socket address, if it is "named".
    ///
    /// (Because this type is uninhabited, this method cannot actually be called.)
    pub fn as_pathname(&self) -> Option<&Path> {
        void::unreachable(self.0)
    }
    /// Attempt to construct an AF_UNIX socket address from the provided `path`.
    ///
    /// (Because this platform lacks AF_UNIX support, this method will always return an error.)
    pub fn from_pathname<P: AsRef<Path>>(path: P) -> std::io::Result<Self> {
        let _ = path;
        Err(NoUnixAddressSupport.into())
    }
}

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

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

/// Error: Tried to perform an operation on an unsupported kind of unix address.
///
/// (For example, you can't bind or connect to an unnamed address.)
#[derive(Clone, Debug, thiserror::Error)]
#[error("Operation not supported on this kind of AF_UNIX address")]
#[non_exhaustive]
pub struct UnsupportedUnixAddressType;

impl From<UnsupportedUnixAddressType> for std::io::Error {
    fn from(value: UnsupportedUnixAddressType) -> Self {
        std::io::Error::new(std::io::ErrorKind::Unsupported, value)
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    #[cfg(unix)]
    fn unnamed() {
        let u = new_unnamed_socketaddr().expect("couldn't construct unnamed unix socketaddr");
        assert!(u.is_unnamed());
        assert!(u.as_pathname().is_none());

        let n = SocketAddr::from_pathname("/home/arachnidsGrip/.arti/SOCKET")
            .expect("Couldn't construct named socketaddr");
        assert!(!n.is_unnamed());
    }
}