summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2019-05-27 17:35:40 +0000
committerJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2019-05-28 16:37:59 +0000
commitc536211a6bb1dbce706daa83e28b3d1c08a82776 (patch)
tree336c6731ed4a9118517f12a5d9b35a8e800bd86a
parent8acd7dc97a02a95228e7b219141a02b2a4493aa2 (diff)
downloadmullvadvpn-c536211a6bb1dbce706daa83e28b3d1c08a82776.tar.xz
mullvadvpn-c536211a6bb1dbce706daa83e28b3d1c08a82776.zip
Create `UnixTunProvider`
-rw-r--r--Cargo.lock1
-rw-r--r--talpid-core/Cargo.toml1
-rw-r--r--talpid-core/src/tunnel/tun_provider/mod.rs9
-rw-r--r--talpid-core/src/tunnel/tun_provider/unix.rs48
4 files changed, 59 insertions, 0 deletions
diff --git a/Cargo.lock b/Cargo.lock
index f31d6c06c5..d6232ef395 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2095,6 +2095,7 @@ name = "talpid-core"
version = "0.1.0"
dependencies = [
"atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
+ "cfg-if 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
"dbus 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)",
"derive_more 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)",
"duct 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
diff --git a/talpid-core/Cargo.toml b/talpid-core/Cargo.toml
index 833b2cb9b6..2402bd2e52 100644
--- a/talpid-core/Cargo.toml
+++ b/talpid-core/Cargo.toml
@@ -8,6 +8,7 @@ edition = "2018"
[dependencies]
atty = "0.2"
+cfg-if = "0.1"
derive_more = "0.14"
duct = "0.12"
err-derive = "0.1.5"
diff --git a/talpid-core/src/tunnel/tun_provider/mod.rs b/talpid-core/src/tunnel/tun_provider/mod.rs
index e30752701a..63d8800d1f 100644
--- a/talpid-core/src/tunnel/tun_provider/mod.rs
+++ b/talpid-core/src/tunnel/tun_provider/mod.rs
@@ -1,3 +1,4 @@
+use cfg_if::cfg_if;
use std::net::IpAddr;
#[cfg(unix)]
use std::os::unix::io::AsRawFd;
@@ -6,6 +7,14 @@ use talpid_types::BoxedError;
mod stub;
pub use self::stub::StubTunProvider;
+cfg_if! {
+ if #[cfg(all(unix, not(target_os = "android")))] {
+ #[path = "unix.rs"]
+ mod imp;
+ use self::imp::UnixTunProvider;
+ }
+}
+
/// Generic tunnel device.
///
/// Must be associated with a platform specific file descriptor representing the device.
diff --git a/talpid-core/src/tunnel/tun_provider/unix.rs b/talpid-core/src/tunnel/tun_provider/unix.rs
new file mode 100644
index 0000000000..d0191622bf
--- /dev/null
+++ b/talpid-core/src/tunnel/tun_provider/unix.rs
@@ -0,0 +1,48 @@
+use super::{Tun, TunConfig, TunProvider};
+use crate::network_interface::{self, NetworkInterface, TunnelDevice};
+use std::net::IpAddr;
+use talpid_types::BoxedError;
+
+/// Errors that can occur while setting up a tunnel device.
+#[derive(Debug, err_derive::Error)]
+pub enum Error {
+ /// Failure to create a tunnel device.
+ #[error(display = "Failed to create a tunnel device")]
+ CreateTunnelDevice(#[cause] network_interface::Error),
+
+ /// Failure to set a tunnel device IP address.
+ #[error(display = "Failed to set tunnel IP address: {}", _0)]
+ SetIpAddr(IpAddr, #[cause] network_interface::Error),
+
+ /// Failure to set the tunnel device as up.
+ #[error(display = "Failed to set the tunnel device as up")]
+ SetUp(#[cause] network_interface::Error),
+}
+
+/// Factory of tunnel devices on Unix systems.
+pub struct UnixTunProvider;
+
+impl TunProvider for UnixTunProvider {
+ fn create_tun(&self, config: TunConfig) -> Result<Box<dyn Tun>, BoxedError> {
+ let mut tunnel_device = TunnelDevice::new()
+ .map_err(|cause| BoxedError::new(Error::CreateTunnelDevice(cause)))?;
+
+ for ip in config.addresses.iter() {
+ tunnel_device
+ .set_ip(*ip)
+ .map_err(|cause| BoxedError::new(Error::SetIpAddr(*ip, cause)))?;
+ }
+
+ tunnel_device
+ .set_up(true)
+ .map_err(|cause| BoxedError::new(Error::SetUp(cause)))?;
+
+ Ok(Box::new(tunnel_device))
+ }
+}
+
+impl Tun for TunnelDevice {
+ fn interface_name(&self) -> &str {
+ self.get_name()
+ }
+}