diff options
| author | Linus Färnstrand <linus@mullvad.net> | 2017-11-10 09:44:00 +0100 |
|---|---|---|
| committer | Linus Färnstrand <linus@mullvad.net> | 2017-11-13 11:30:46 +0100 |
| commit | 6279cb71d8f22fc869e7ce0539b7f7c647078f31 (patch) | |
| tree | 25983378d79e10c845e9bbd64e5f0f0a0318bf6b | |
| parent | d6b67aca6b07c903e235875f85fbacd3599f40c6 (diff) | |
| download | mullvadvpn-6279cb71d8f22fc869e7ce0539b7f7c647078f31.tar.xz mullvadvpn-6279cb71d8f22fc869e7ce0539b7f7c647078f31.zip | |
Add TunnelEndpoint type
| -rw-r--r-- | talpid-core/src/tunnel/mod.rs | 14 | ||||
| -rw-r--r-- | talpid-types/src/net.rs | 22 |
2 files changed, 33 insertions, 3 deletions
diff --git a/talpid-core/src/tunnel/mod.rs b/talpid-core/src/tunnel/mod.rs index 9b42b64355..af0002c61d 100644 --- a/talpid-core/src/tunnel/mod.rs +++ b/talpid-core/src/tunnel/mod.rs @@ -12,7 +12,7 @@ use std::io::{self, Write}; use std::net::Ipv4Addr; use std::path::{Path, PathBuf}; -use talpid_types::net; +use talpid_types::net::{Endpoint, TunnelEndpoint}; /// A module for all OpenVPN related tunnel management. pub mod openvpn; @@ -38,6 +38,10 @@ mod errors { UnsupportedPlatform { description("Running on an unsupported operating system") } + /// This type of VPN tunnel is not supported. + UnsupportedTunnelTechnology { + description("This tunnel technology is not supported") + } } } } @@ -108,7 +112,7 @@ impl TunnelMonitor { /// Creates a new `TunnelMonitor` that connects to the given remote and notifies `on_event` /// on tunnel state changes. pub fn new<L>( - remote: net::Endpoint, + remote: TunnelEndpoint, account_token: &str, log: Option<&Path>, on_event: L, @@ -116,6 +120,10 @@ impl TunnelMonitor { where L: Fn(TunnelEvent) + Send + Sync + 'static, { + let remote = match remote { + TunnelEndpoint::OpenVpn(endpoint) => endpoint, + _ => bail!(ErrorKind::UnsupportedTunnelTechnology), + }; let user_pass_file = Self::create_user_pass_file(account_token) .chain_err(|| ErrorKind::CredentialsWriteError)?; let cmd = Self::create_openvpn_cmd(remote, user_pass_file.as_ref(), log); @@ -141,7 +149,7 @@ impl TunnelMonitor { } fn create_openvpn_cmd( - remote: net::Endpoint, + remote: Endpoint, user_pass_file: &Path, log: Option<&Path>, ) -> OpenVpnCommand { diff --git a/talpid-types/src/net.rs b/talpid-types/src/net.rs index f470e8a24a..a293592f18 100644 --- a/talpid-types/src/net.rs +++ b/talpid-types/src/net.rs @@ -3,6 +3,28 @@ use std::fmt; use std::net::{IpAddr, SocketAddr}; use std::str::FromStr; +/// Represents one tunnel endpoint. Tunnel technology plus address. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum TunnelEndpoint { + /// An OpenVPN tunnel endpoint. + OpenVpn(Endpoint), + /// A Wireguard tunnel endpoint. + Wireguard(SocketAddr), +} + +impl TunnelEndpoint { + /// Returns this tunnel endpoint as an `Endpoint`. + pub fn to_endpoint(&self) -> Endpoint { + match *self { + TunnelEndpoint::OpenVpn(endpoint) => endpoint, + TunnelEndpoint::Wireguard(address) => Endpoint { + address, + protocol: TransportProtocol::Udp, + }, + } + } +} + /// Represents a network layer IP address together with the transport layer protocol and port. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct Endpoint { |
