diff options
| author | David Lönnhager <david.l@mullvad.net> | 2021-02-24 13:12:03 +0100 |
|---|---|---|
| committer | David Lönnhager <david.l@mullvad.net> | 2021-03-01 16:50:42 +0100 |
| commit | 4e3cb22bd067e3e1b0d28afcde1d63ec720f8250 (patch) | |
| tree | 99bc0c98fe13d38f9e3929ea8690ddd0efda0a08 /talpid-core/src | |
| parent | 575fb84c82416aaeed780a33af4b8abb819488d5 (diff) | |
| download | mullvadvpn-4e3cb22bd067e3e1b0d28afcde1d63ec720f8250.tar.xz mullvadvpn-4e3cb22bd067e3e1b0d28afcde1d63ec720f8250.zip | |
Handle new winnet routing errors in talpid-core
Diffstat (limited to 'talpid-core/src')
| -rw-r--r-- | talpid-core/src/routing/windows.rs | 10 | ||||
| -rw-r--r-- | talpid-core/src/winnet.rs | 39 |
2 files changed, 39 insertions, 10 deletions
diff --git a/talpid-core/src/routing/windows.rs b/talpid-core/src/routing/windows.rs index 47a45672dc..25c80899e5 100644 --- a/talpid-core/src/routing/windows.rs +++ b/talpid-core/src/routing/windows.rs @@ -20,7 +20,7 @@ pub enum Error { FailedToStartManager, /// Failure to add routes #[error(display = "Failed to add routes")] - AddRoutesFailed, + AddRoutesFailed(#[error(source)] winnet::Error), /// Failure to clear routes #[error(display = "Failed to clear applied routes")] ClearRoutesFailed, @@ -118,11 +118,9 @@ impl RouteManager { }) .collect(); - if winnet::routing_manager_add_routes(&routes) { - let _ = tx.send(Ok(())); - } else { - let _ = tx.send(Err(Error::AddRoutesFailed)); - } + let _ = tx.send( + winnet::routing_manager_add_routes(&routes).map_err(Error::AddRoutesFailed), + ); } RouteManagerCommand::Shutdown => { break; diff --git a/talpid-core/src/winnet.rs b/talpid-core/src/winnet.rs index c00a22d73f..79008f8cbc 100644 --- a/talpid-core/src/winnet.rs +++ b/talpid-core/src/winnet.rs @@ -28,6 +28,18 @@ pub enum Error { #[error(display = "Failed to obtain default route")] GetDefaultRoute, + /// Failed to get a network device. + #[error(display = "Failed to obtain network interface by name")] + GetDeviceByName, + + /// Failed to get a network device. + #[error(display = "Failed to obtain network interface by gateway")] + GetDeviceByGateway, + + /// Unexpected error while adding routes + #[error(display = "Winnet returned an error while adding routes")] + GeneralAddRoutesError, + /// Failed to obtain an IP address given a LUID. #[error(display = "Failed to obtain IP address for the given interface")] GetIpAddressFromLuid, @@ -310,10 +322,16 @@ pub fn add_default_route_change_callback<T: 'static>( } } -pub fn routing_manager_add_routes(routes: &[WinNetRoute]) -> bool { +pub fn routing_manager_add_routes(routes: &[WinNetRoute]) -> Result<(), Error> { let ptr = routes.as_ptr(); let length: u32 = routes.len() as u32; - unsafe { WinNet_AddRoutes(ptr, length) } + match unsafe { WinNet_AddRoutes(ptr, length) } { + WinNetAddRouteStatus::Success => Ok(()), + WinNetAddRouteStatus::GeneralError => Err(Error::GeneralAddRoutesError), + WinNetAddRouteStatus::NoDefaultRoute => Err(Error::GetDefaultRoute), + WinNetAddRouteStatus::NameNotFound => Err(Error::GetDeviceByName), + WinNetAddRouteStatus::GatewayNotFound => Err(Error::GetDeviceByGateway), + } } pub fn routing_manager_delete_applied_routes() -> bool { @@ -394,15 +412,28 @@ mod api { Failure = 2, } + #[allow(dead_code)] + #[repr(u32)] + pub enum WinNetAddRouteStatus { + Success = 0, + GeneralError = 1, + NoDefaultRoute = 2, + NameNotFound = 3, + GatewayNotFound = 4, + } + extern "system" { #[link_name = "WinNet_ActivateRouteManager"] pub fn WinNet_ActivateRouteManager(sink: Option<LogSink>, sink_context: *const u8) -> bool; #[link_name = "WinNet_AddRoutes"] - pub fn WinNet_AddRoutes(routes: *const super::WinNetRoute, num_routes: u32) -> bool; + pub fn WinNet_AddRoutes( + routes: *const super::WinNetRoute, + num_routes: u32, + ) -> WinNetAddRouteStatus; // #[link_name = "WinNet_AddRoute"] - // pub fn WinNet_AddRoute(route: *const super::WinNetRoute) -> bool; + // pub fn WinNet_AddRoute(route: *const super::WinNetRoute) -> WinNetAddRouteStatus; // #[link_name = "WinNet_DeleteRoutes"] // pub fn WinNet_DeleteRoutes(routes: *const super::WinNetRoute, num_routes: u32) -> bool; |
