diff options
| author | Linus Färnstrand <linus@mullvad.net> | 2019-03-29 13:29:38 +0100 |
|---|---|---|
| committer | Linus Färnstrand <linus@mullvad.net> | 2019-03-29 13:35:18 +0100 |
| commit | 4ab2334b60a90d3dd31b958429a3a3164c6d64cd (patch) | |
| tree | c403d9f6a799082c8e58026d28ee296ed8b036b8 | |
| parent | 97e8ce0f5c47c593740aafb20a56d8a06974b4a9 (diff) | |
| download | mullvadvpn-4ab2334b60a90d3dd31b958429a3a3164c6d64cd.tar.xz mullvadvpn-4ab2334b60a90d3dd31b958429a3a3164c6d64cd.zip | |
Get rid of error-chain in various wireguard modules
| -rw-r--r-- | talpid-core/src/tunnel/wireguard/config.rs | 39 | ||||
| -rw-r--r-- | talpid-core/src/tunnel/wireguard/ping_monitor.rs | 39 |
2 files changed, 40 insertions, 38 deletions
diff --git a/talpid-core/src/tunnel/wireguard/config.rs b/talpid-core/src/tunnel/wireguard/config.rs index 97d7832a60..63c99dfdce 100644 --- a/talpid-core/src/tunnel/wireguard/config.rs +++ b/talpid-core/src/tunnel/wireguard/config.rs @@ -17,24 +17,20 @@ pub struct Config { const SMALLEST_IPV6_MTU: u16 = 1420; const DEFAULT_MTU: u16 = SMALLEST_IPV6_MTU; -error_chain! { - errors { - InvalidTunnelIpError { - description("No valid tunnel IP"), - } +#[derive(err_derive::Error, Debug)] +pub enum Error { + #[error(display = "No valid tunnel IP")] + InvalidTunnelIpError, - InvalidPeerIpError { - description("Supplied peer has no valid IPs") - } + #[error(display = "Supplied peer has no valid IPs")] + InvalidPeerIpError, - NoPeersSuppliedError{ - description("No peers supplied") - } - } + #[error(display = "No peers supplied")] + NoPeersSuppliedError, } impl Config { - pub fn from_parameters(params: &wireguard::TunnelParameters) -> Result<Config> { + pub fn from_parameters(params: &wireguard::TunnelParameters) -> Result<Config, Error> { let tunnel = params.connection.tunnel.clone(); let peer = vec![params.connection.peer.clone()]; Self::new( @@ -52,8 +48,10 @@ impl Config { connection_config: &wireguard::ConnectionConfig, wg_options: &wireguard::TunnelOptions, generic_options: &GenericTunnelOptions, - ) -> Result<Config> { - ensure!(!peers.is_empty(), ErrorKind::NoPeersSuppliedError); + ) -> Result<Config, Error> { + if peers.is_empty() { + return Err(Error::NoPeersSuppliedError); + } let mtu = wg_options.mtu.unwrap_or(DEFAULT_MTU); let is_ipv6_enabled = mtu >= SMALLEST_IPV6_MTU && generic_options.enable_ipv6; @@ -64,7 +62,9 @@ impl Config { .cloned() .filter(|ip| ip.is_ipv4() || is_ipv6_enabled) .collect(); - ensure!(!peer.allowed_ips.is_empty(), ErrorKind::InvalidPeerIpError); + if peer.allowed_ips.is_empty() { + return Err(Error::InvalidPeerIpError); + } } tunnel.addresses = tunnel @@ -72,10 +72,9 @@ impl Config { .into_iter() .filter(|ip| ip.is_ipv4() || is_ipv6_enabled) .collect(); - ensure!( - !tunnel.addresses.is_empty(), - ErrorKind::InvalidTunnelIpError - ); + if tunnel.addresses.is_empty() { + return Err(Error::InvalidTunnelIpError); + } Ok(Config { tunnel, diff --git a/talpid-core/src/tunnel/wireguard/ping_monitor.rs b/talpid-core/src/tunnel/wireguard/ping_monitor.rs index d471971ca6..69f22fc83f 100644 --- a/talpid-core/src/tunnel/wireguard/ping_monitor.rs +++ b/talpid-core/src/tunnel/wireguard/ping_monitor.rs @@ -1,23 +1,25 @@ -use std::{net::IpAddr, thread, time}; +use std::{ + io, + net::IpAddr, + thread, + time::{Duration, Instant}, +}; -error_chain! { - errors { - PingError{ - description("Failed to run ping") - } +#[derive(err_derive::Error, Debug)] +pub enum Error { + #[error(display = "Failed to run ping command")] + PingError(#[error(cause)] io::Error), - TimeoutError { - description("Ping timed out") - } - } + #[error(display = "Ping timed out")] + TimeoutError, } -pub fn monitor_ping(ip: IpAddr, timeout_secs: u16, interface: &str) -> Result<()> { +pub fn monitor_ping(ip: IpAddr, timeout_secs: u16, interface: &str) -> Result<(), Error> { loop { - let start = time::Instant::now(); + let start = Instant::now(); ping(ip, timeout_secs, &interface, false)?; if let Some(remaining) = - time::Duration::from_secs(timeout_secs.into()).checked_sub(start.elapsed()) + Duration::from_secs(timeout_secs.into()).checked_sub(start.elapsed()) { thread::sleep(remaining); } @@ -29,14 +31,15 @@ pub fn ping( timeout_secs: u16, interface: &str, exit_on_first_reply: bool, -) -> Result<()> { +) -> Result<(), Error> { let output = ping_cmd(ip, timeout_secs, interface, exit_on_first_reply) .run() - .chain_err(|| ErrorKind::PingError)?; - if !output.status.success() { - bail!(ErrorKind::TimeoutError); + .map_err(Error::PingError)?; + if output.status.success() { + Ok(()) + } else { + Err(Error::TimeoutError) } - Ok(()) } fn ping_cmd( |
