diff options
| author | David Lönnhager <david.l@mullvad.net> | 2024-03-08 16:00:44 +0100 |
|---|---|---|
| committer | David Lönnhager <david.l@mullvad.net> | 2024-03-13 17:49:29 +0100 |
| commit | 72aeb6bc99cb7b332e3cfb5902b28dea5632d532 (patch) | |
| tree | 305a86d4492f870d502a2cb6c488ccba70cd711b | |
| parent | 611390267de24347f15c19b34e02af72c6009ee5 (diff) | |
| download | mullvadvpn-72aeb6bc99cb7b332e3cfb5902b28dea5632d532.tar.xz mullvadvpn-72aeb6bc99cb7b332e3cfb5902b28dea5632d532.zip | |
Add helper for checking if routing socket has been shut down
| -rw-r--r-- | talpid-routing/src/unix/macos/routing_socket.rs | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/talpid-routing/src/unix/macos/routing_socket.rs b/talpid-routing/src/unix/macos/routing_socket.rs index ab02f1cff2..142e9b0aa3 100644 --- a/talpid-routing/src/unix/macos/routing_socket.rs +++ b/talpid-routing/src/unix/macos/routing_socket.rs @@ -34,6 +34,23 @@ pub enum Error { ResponseTimeout, } +impl Error { + /// Return the underlying `io::Error` (or `None`) + pub fn as_io_error(&self) -> Option<&io::Error> { + use std::error::Error; + self.source() + .and_then(|source| source.downcast_ref::<io::Error>()) + } + + /// Return whether an operation failed because the socket has been shut down + pub fn is_shutdown(&self) -> bool { + // ENOTCONN is returned when the socket is shut down (e.g., due to `pid_shutdown_sockets`) + self.as_io_error() + .map(|io_error| io_error.kind() == io::ErrorKind::NotConnected) + .unwrap_or(false) + } +} + type Result<T> = std::result::Result<T, Error>; const RESPONSE_TIMEOUT: Duration = Duration::from_secs(10); |
