Skip to main content

panoramic_courier/
probe.rs

1use std::time::Duration;
2
3use crate::{net, Address, Error, UnsupportedSnafu};
4
5/// Checks that something accepts a connection at `address`.
6///
7/// A `tcp`, `http` or `grpc` probe opens a TCP connection and closes it. A `unix` probe connects to the stream
8/// socket, and a `unixgram` probe connects a datagram socket to it, which fails unless a socket is bound at the path.
9/// A `pipe` probe opens the named pipe, and counts a pipe that is busy with other clients as present.
10///
11/// `limit` bounds the call, but a name lookup it cuts short keeps running on its blocking thread until the resolver
12/// answers.
13///
14/// # Errors
15///
16/// Fails with [`Error::Unsupported`] for a `udp` address, which a probe cannot confirm, and for a socket or pipe this
17/// OS does not have. Fails with [`Error::Target`] when nothing accepts, and [`Error::TimedOut`] when `limit` passes.
18pub async fn probe(address: &Address, limit: Duration) -> Result<(), Error> {
19    let unsupported = |reason| {
20        UnsupportedSnafu {
21            operation: "probe",
22            address: address.clone(),
23            reason,
24        }
25        .fail()
26    };
27    net::within("probe", address, limit, async {
28        match address {
29            Address::Tcp(host_port) | Address::Http(host_port) | Address::Grpc(host_port) => {
30                net::connect_tcp(host_port).await.map(drop)
31            }
32            Address::Udp(_) => unsupported("a probe cannot confirm a UDP listener"),
33            #[cfg(unix)]
34            Address::UnixStream(path) => unix::connect_stream(path).await,
35            #[cfg(unix)]
36            Address::UnixDatagram(path) => unix::connect_datagram(path),
37            #[cfg(not(unix))]
38            Address::UnixStream(_) | Address::UnixDatagram(_) => unsupported("this OS has no Unix sockets"),
39            #[cfg(windows)]
40            Address::NamedPipe(name) => pipe(address, name),
41            #[cfg(not(windows))]
42            Address::NamedPipe(_) => unsupported("named pipes exist only on Windows"),
43        }
44    })
45    .await
46}
47
48#[cfg(windows)]
49fn pipe(address: &Address, name: &str) -> Result<(), Error> {
50    use snafu::ResultExt as _;
51
52    // Win32 ERROR_PIPE_BUSY: the pipe exists and every instance has a client.
53    const ERROR_PIPE_BUSY: i32 = 231;
54
55    match net::open_pipe(name) {
56        Ok(_) => Ok(()),
57        Err(e) if e.raw_os_error() == Some(ERROR_PIPE_BUSY) => Ok(()),
58        Err(e) => Err(e).context(crate::TargetSnafu {
59            action: "open",
60            target: address.to_string(),
61        }),
62    }
63}
64
65#[cfg(unix)]
66mod unix {
67    use std::path::Path;
68
69    use snafu::ResultExt as _;
70    use tokio::net::{UnixDatagram, UnixStream};
71
72    use crate::{Error, TargetSnafu};
73
74    pub(super) async fn connect_stream(path: &Path) -> Result<(), Error> {
75        UnixStream::connect(path).await.map(drop).context(TargetSnafu {
76            action: "connect to",
77            target: path.display().to_string(),
78        })
79    }
80
81    pub(super) fn connect_datagram(path: &Path) -> Result<(), Error> {
82        let context = || TargetSnafu {
83            action: "connect to",
84            target: path.display().to_string(),
85        };
86        let socket = UnixDatagram::unbound().with_context(|_| context())?;
87        socket.connect(path).with_context(|_| context())
88    }
89}