tor_rtcompat/
unix.rs

1//! Functionality for working with AF\_UNIX addresses.
2
3use tor_general_addr::unix;
4
5/// Helper: construct an unnamed SocketAddr.
6#[cfg(unix)]
7pub(crate) fn new_unnamed_socketaddr() -> std::io::Result<unix::SocketAddr> {
8    // There SHOULD be a better way to do this in legitimate Rust!
9    // But sadly, there isn't.
10    unix::SocketAddr::from_pathname("")
11}
12
13/// Error: Tried to perform an operation on an unsupported kind of AF\_UNIX address.
14///
15/// (For example, you can't bind or connect to an unnamed address.)
16#[derive(Clone, Debug, thiserror::Error)]
17#[error("Operation not supported on this kind of AF_UNIX address")]
18#[non_exhaustive]
19pub struct UnsupportedAfUnixAddressType;
20
21/// Deprecated name for `UnsupportedAfUnixAddressType`
22#[deprecated]
23pub type UnsupportedUnixAddressType = UnsupportedAfUnixAddressType;
24
25impl From<UnsupportedAfUnixAddressType> for std::io::Error {
26    fn from(value: UnsupportedAfUnixAddressType) -> Self {
27        std::io::Error::new(std::io::ErrorKind::Unsupported, value)
28    }
29}
30
31#[cfg(test)]
32mod test {
33    use super::*;
34
35    #[test]
36    #[cfg(unix)]
37    fn unnamed() {
38        let u = new_unnamed_socketaddr().expect("couldn't construct unnamed AF_UNIX socketaddr");
39        assert!(u.is_unnamed());
40        assert!(u.as_pathname().is_none());
41
42        let n = unix::SocketAddr::from_pathname("/home/arachnidsGrip/.arti/SOCKET")
43            .expect("Couldn't construct named socketaddr");
44        assert!(!n.is_unnamed());
45    }
46}