summaryrefslogtreecommitdiffhomepage
path: root/talpid-tunnel/src/lib.rs
blob: 8a916c668d4b94409315885687a90032f2af8990 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use std::{
    net::{IpAddr, Ipv4Addr, Ipv6Addr},
    path::Path,
    sync::{Arc, Mutex},
};

#[cfg(windows)]
#[path = "windows.rs"]
pub mod network_interface;

pub mod tun_provider;
use futures::{channel::oneshot, future::BoxFuture};
use talpid_routing::RouteManagerHandle;
use talpid_types::net::AllowedTunnelTraffic;
use tun_provider::TunProvider;

/// Arguments for creating a tunnel.
pub struct TunnelArgs<'a, L>
where
    L: (Fn(TunnelEvent) -> BoxFuture<'static, ()>) + Send + Clone + Sync + 'static,
{
    /// Toktio runtime handle.
    pub runtime: tokio::runtime::Handle,
    /// Resource directory path.
    pub resource_dir: &'a Path,
    /// Callback function called when an event happens.
    pub on_event: L,
    /// Receiver oneshot channel for closing the tunnel.
    pub tunnel_close_rx: oneshot::Receiver<()>,
    /// Mutex to tunnel provider.
    pub tun_provider: Arc<Mutex<TunProvider>>,
    /// Connection retry attempts.
    pub retry_attempt: u32,
    /// Route manager handle.
    pub route_manager: RouteManagerHandle,
}

/// Information about a VPN tunnel.
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct TunnelMetadata {
    /// The name of the device which the tunnel is running on.
    pub interface: String,
    /// The local IPs on the tunnel interface.
    pub ips: Vec<IpAddr>,
    /// The IP to the default gateway on the tunnel interface.
    pub ipv4_gateway: Ipv4Addr,
    /// The IP to the IPv6 default gateway on the tunnel interface.
    pub ipv6_gateway: Option<Ipv6Addr>,
}

/// Possible events from the VPN tunnel and the child process managing it.
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub enum TunnelEvent {
    /// Sent when the tunnel fails to connect due to an authentication error.
    AuthFailed(Option<String>),
    /// Sent when the tunnel interface has been created, before routes are set up.
    InterfaceUp(TunnelMetadata, AllowedTunnelTraffic),
    /// Sent when the tunnel comes up and is ready for traffic.
    Up(TunnelMetadata),
    /// Sent when the tunnel goes down, but before destroying the tunnel device.
    Down,
}