panoramic_courier/
probe.rs1use std::time::Duration;
2
3use crate::{net, Address, Error, UnsupportedSnafu};
4
5pub 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 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}