diff options
| author | Odd Stranne <odd@mullvad.net> | 2021-10-22 16:16:26 +0200 |
|---|---|---|
| committer | Odd Stranne <odd@mullvad.net> | 2021-10-25 12:07:46 +0200 |
| commit | 8aec8e1c6ec6b487f1916258d899d8c68ebfa84c (patch) | |
| tree | 7e8bbf714bb8a8c773b8dfd275b0ae2504a3eb40 /talpid-core/src | |
| parent | 5689f098651e15ea1d81ff301539e8a01aa393c9 (diff) | |
| download | mullvadvpn-8aec8e1c6ec6b487f1916258d899d8c68ebfa84c.tar.xz mullvadvpn-8aec8e1c6ec6b487f1916258d899d8c68ebfa84c.zip | |
Emit more granular errors
Diffstat (limited to 'talpid-core/src')
| -rw-r--r-- | talpid-core/src/split_tunnel/windows/driver.rs | 67 | ||||
| -rw-r--r-- | talpid-core/src/split_tunnel/windows/mod.rs | 8 |
2 files changed, 63 insertions, 12 deletions
diff --git a/talpid-core/src/split_tunnel/windows/driver.rs b/talpid-core/src/split_tunnel/windows/driver.rs index 7422972dba..e82f932dd0 100644 --- a/talpid-core/src/split_tunnel/windows/driver.rs +++ b/talpid-core/src/split_tunnel/windows/driver.rs @@ -27,7 +27,9 @@ use winapi::{ inaddr::IN_ADDR, minwindef::{FALSE, TRUE}, ntdef::NTSTATUS, - winerror::{ERROR_INVALID_PARAMETER, ERROR_IO_PENDING}, + winerror::{ + ERROR_ACCESS_DENIED, ERROR_FILE_NOT_FOUND, ERROR_INVALID_PARAMETER, ERROR_IO_PENDING, + }, }, um::{ handleapi::CloseHandle, @@ -129,8 +131,42 @@ pub struct DeviceHandle { unsafe impl Sync for DeviceHandle {} unsafe impl Send for DeviceHandle {} +#[derive(err_derive::Error, Debug)] +#[error(no_from)] +pub enum DeviceHandleError { + /// Failed to connect because there's no such device + #[error(display = "Failed to connect to driver, no such device. \ + The driver is probably not loaded")] + ConnectionFailed, + + /// Failed to connect because the connection was denied + #[error(display = "Failed to connect to driver, connection denied. \ + The exclusive connection is probably hogged")] + ConnectionDenied, + + /// Failed to connect to driver + #[error(display = "Failed to connect to driver")] + ConnectionError(#[error(source)] io::Error), + + /// Failed to inquire about driver state + #[error(display = "Failed to inquire about driver state")] + GetStateError(#[error(source)] io::Error), + + /// Failed to initialize driver + #[error(display = "Failed to initialize driver")] + InitializationError(#[error(source)] io::Error), + + /// Failed to register process tree with driver + #[error(display = "Failed to register process tree with driver")] + RegisterProcessesError(#[error(source)] io::Error), + + /// Failed to clear configuration in driver + #[error(display = "Failed to clear configuration in driver")] + ClearConfigError(#[error(source)] io::Error), +} + impl DeviceHandle { - pub fn new() -> io::Result<Self> { + pub fn new() -> Result<Self, DeviceHandleError> { // Connect to the driver log::trace!("Connecting to the driver"); let handle = OpenOptions::new() @@ -139,26 +175,41 @@ impl DeviceHandle { .share_mode(0) .custom_flags(FILE_FLAG_OVERLAPPED) .attributes(0) - .open(DRIVER_SYMBOLIC_NAME)?; + .open(DRIVER_SYMBOLIC_NAME) + .map_err(|e| match e.raw_os_error().map(|raw| raw as u32) { + Some(ERROR_FILE_NOT_FOUND) => DeviceHandleError::ConnectionFailed, + Some(ERROR_ACCESS_DENIED) => DeviceHandleError::ConnectionDenied, + _ => DeviceHandleError::ConnectionError(e), + })?; let device = Self { handle }; // Initialize the driver - let state = device.get_driver_state()?; + let state = device + .get_driver_state() + .map_err(DeviceHandleError::GetStateError)?; if state == DriverState::Started { log::trace!("Initializing driver"); - device.initialize()?; + device + .initialize() + .map_err(DeviceHandleError::InitializationError)?; } // Initialize process tree - let state = device.get_driver_state()?; + let state = device + .get_driver_state() + .map_err(DeviceHandleError::GetStateError)?; if state == DriverState::Initialized { log::trace!("Registering processes"); - device.register_processes()?; + device + .register_processes() + .map_err(DeviceHandleError::RegisterProcessesError)?; } log::trace!("Clearing any existing exclusion config"); - device.clear_config()?; + device + .clear_config() + .map_err(DeviceHandleError::ClearConfigError)?; Ok(device) } diff --git a/talpid-core/src/split_tunnel/windows/mod.rs b/talpid-core/src/split_tunnel/windows/mod.rs index e826eda8d3..55c45c83df 100644 --- a/talpid-core/src/split_tunnel/windows/mod.rs +++ b/talpid-core/src/split_tunnel/windows/mod.rs @@ -42,9 +42,9 @@ const RESERVED_IP_V4: Ipv4Addr = Ipv4Addr::new(192, 0, 2, 123); #[derive(err_derive::Error, Debug)] #[error(no_from)] pub enum Error { - /// Failed to identify or initialize the driver - #[error(display = "Failed to find or initialize driver")] - InitializationFailed(#[error(source)] io::Error), + /// Failed to initialize the driver + #[error(display = "Failed to initialize driver")] + InitializationError(#[error(source)] driver::DeviceHandleError), /// Failed to set paths to excluded applications #[error(display = "Failed to set list of excluded applications")] @@ -312,7 +312,7 @@ impl SplitTunnel { std::thread::spawn(move || { let result = driver::DeviceHandle::new() .map(Arc::new) - .map_err(Error::InitializationFailed); + .map_err(Error::InitializationError); let handle = match result { Ok(handle) => { let _ = init_tx.send(Ok(handle.clone())); |
