1
//! Functionality for working with AF\_UNIX addresses.
2

            
3
#[cfg(unix)]
4
use tor_general_addr::unix;
5

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

            
14
/// Error: Tried to perform an operation on an unsupported kind of AF\_UNIX address.
15
///
16
/// (For example, you can't bind or connect to an unnamed address.)
17
#[derive(Clone, Debug, thiserror::Error)]
18
#[error("Operation not supported on this kind of AF_UNIX address")]
19
#[non_exhaustive]
20
pub struct UnsupportedAfUnixAddressType;
21

            
22
/// Deprecated name for `UnsupportedAfUnixAddressType`
23
#[deprecated]
24
pub type UnsupportedUnixAddressType = UnsupportedAfUnixAddressType;
25

            
26
impl From<UnsupportedAfUnixAddressType> for std::io::Error {
27
    fn from(value: UnsupportedAfUnixAddressType) -> Self {
28
        std::io::Error::new(std::io::ErrorKind::Unsupported, value)
29
    }
30
}
31

            
32
#[cfg(test)]
33
mod test {
34
    #[cfg(unix)]
35
    use super::*;
36

            
37
    #[test]
38
    #[cfg(unix)]
39
    fn unnamed() {
40
        let u = new_unnamed_socketaddr().expect("couldn't construct unnamed AF_UNIX socketaddr");
41
        assert!(u.is_unnamed());
42
        assert!(u.as_pathname().is_none());
43

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