summaryrefslogtreecommitdiffhomepage
path: root/talpid-core/src
diff options
context:
space:
mode:
authorJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2019-05-27 13:55:43 +0000
committerJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2019-05-28 16:37:59 +0000
commit9d7e7ef6bf4a29aef451721349e3eae5707bbe51 (patch)
tree9f34612f3972db42cc18d4fda370240d292147c4 /talpid-core/src
parentb5dffdf062a38f280c8d13a19e2574330e5190fc (diff)
downloadmullvadvpn-9d7e7ef6bf4a29aef451721349e3eae5707bbe51.tar.xz
mullvadvpn-9d7e7ef6bf4a29aef451721349e3eae5707bbe51.zip
Create `TunProvider` trait
Diffstat (limited to 'talpid-core/src')
-rw-r--r--talpid-core/src/tunnel/mod.rs3
-rw-r--r--talpid-core/src/tunnel/tun_provider/mod.rs41
2 files changed, 44 insertions, 0 deletions
diff --git a/talpid-core/src/tunnel/mod.rs b/talpid-core/src/tunnel/mod.rs
index 4d27be32ec..a2b4bf276a 100644
--- a/talpid-core/src/tunnel/mod.rs
+++ b/talpid-core/src/tunnel/mod.rs
@@ -19,6 +19,9 @@ pub mod openvpn;
#[cfg(any(target_os = "linux", target_os = "macos"))]
pub mod wireguard;
+/// A module for low level platform specific tunnel device management.
+pub mod tun_provider;
+
const OPENVPN_LOG_FILENAME: &str = "openvpn.log";
const WIREGUARD_LOG_FILENAME: &str = "wireguard.log";
diff --git a/talpid-core/src/tunnel/tun_provider/mod.rs b/talpid-core/src/tunnel/tun_provider/mod.rs
new file mode 100644
index 0000000000..394fbfc1bb
--- /dev/null
+++ b/talpid-core/src/tunnel/tun_provider/mod.rs
@@ -0,0 +1,41 @@
+use std::net::IpAddr;
+#[cfg(unix)]
+use std::os::unix::io::AsRawFd;
+use talpid_types::BoxedError;
+
+/// Generic tunnel device.
+///
+/// Must be associated with a platform specific file descriptor representing the device.
+#[cfg(unix)]
+pub trait Tun: AsRawFd + Send {
+ /// Retrieve the tunnel interface name.
+ fn interface_name(&self) -> &str;
+}
+
+/// Stub tunnel device.
+#[cfg(windows)]
+pub trait Tun: Send {
+ /// Retrieve the tunnel interface name.
+ fn interface_name(&self) -> &str;
+}
+
+/// Factory of tunnel devices.
+pub trait TunProvider: Send + 'static {
+ /// Create a tunnel device using the provided configuration.
+ fn create_tun(&self, config: TunConfig) -> Result<Box<dyn Tun>, BoxedError>;
+}
+
+/// Configuration for creating a tunnel device.
+pub struct TunConfig {
+ /// IP addresses for the tunnel interface.
+ pub addresses: Vec<IpAddr>,
+}
+
+impl TunConfig {
+ /// Create a new tunnel device configuration using the specified tunnel addresses.
+ pub fn new(addresses: impl IntoIterator<Item = IpAddr>) -> Self {
+ TunConfig {
+ addresses: addresses.into_iter().collect(),
+ }
+ }
+}