diff options
| author | Emīls <emils@mullvad.net> | 2020-02-19 11:27:06 +0000 |
|---|---|---|
| committer | Emīls <emils@mullvad.net> | 2020-02-20 16:21:54 +0000 |
| commit | 7bc14a9dbb3745f6b0289c49696304fd1febbeb6 (patch) | |
| tree | 4d05ff214d540751441fc737d02e11a54fa499b3 /talpid-core/src | |
| parent | 59cc56cbc161bf21e1964ebadbcf73334a4bd71a (diff) | |
| download | mullvadvpn-7bc14a9dbb3745f6b0289c49696304fd1febbeb6.tar.xz mullvadvpn-7bc14a9dbb3745f6b0289c49696304fd1febbeb6.zip | |
Add docstrings and make some types not public
Diffstat (limited to 'talpid-core/src')
| -rw-r--r-- | talpid-core/src/tunnel/mod.rs | 1 | ||||
| -rw-r--r-- | talpid-core/src/tunnel/wireguard/config.rs | 19 | ||||
| -rw-r--r-- | talpid-core/src/tunnel/wireguard/connectivity_check.rs | 6 | ||||
| -rw-r--r-- | talpid-core/src/tunnel/wireguard/mod.rs | 21 |
4 files changed, 34 insertions, 13 deletions
diff --git a/talpid-core/src/tunnel/mod.rs b/talpid-core/src/tunnel/mod.rs index 5a0d87452a..22da520ce0 100644 --- a/talpid-core/src/tunnel/mod.rs +++ b/talpid-core/src/tunnel/mod.rs @@ -15,6 +15,7 @@ use talpid_types::net::{wireguard as wireguard_types, TunnelParameters}; #[cfg(not(target_os = "android"))] pub mod openvpn; +/// A module for all WireGuard related tunnel management. pub mod wireguard; /// A module for low level platform specific tunnel device management. diff --git a/talpid-core/src/tunnel/wireguard/config.rs b/talpid-core/src/tunnel/wireguard/config.rs index e433483a55..62c8d18144 100644 --- a/talpid-core/src/tunnel/wireguard/config.rs +++ b/talpid-core/src/tunnel/wireguard/config.rs @@ -5,11 +5,17 @@ use std::{ }; use talpid_types::net::{wireguard, GenericTunnelOptions}; +/// Config required to set up a single WireGuard tunnel pub struct Config { + /// Contains tunnel endpoint specific config pub tunnel: wireguard::TunnelConfig, + /// List of peer configurations pub peers: Vec<wireguard::PeerConfig>, + /// IPv4 gateway pub ipv4_gateway: Ipv4Addr, + /// IPv6 gateway pub ipv6_gateway: Option<Ipv6Addr>, + /// Maximum transmission unit for the tunnel pub mtu: u16, } @@ -17,19 +23,24 @@ pub struct Config { const SMALLEST_IPV6_MTU: u16 = 1380; const DEFAULT_MTU: u16 = SMALLEST_IPV6_MTU; +/// Configuration errors #[derive(err_derive::Error, Debug)] pub enum Error { + /// Supplied parameters don't contain a valid tunnel IP #[error(display = "No valid tunnel IP")] InvalidTunnelIpError, + /// Peer has no valid IPs #[error(display = "Supplied peer has no valid IPs")] InvalidPeerIpError, + /// Parameters don't contain any peers #[error(display = "No peers supplied")] NoPeersSuppliedError, } impl Config { + /// Constructs a Config from parameters pub fn from_parameters(params: &wireguard::TunnelParameters) -> Result<Config, Error> { let tunnel = params.connection.tunnel.clone(); let peer = vec![params.connection.peer.clone()]; @@ -42,6 +53,7 @@ impl Config { ) } + /// Constructs a new Config struct pub fn new( mut tunnel: wireguard::TunnelConfig, mut peers: Vec<wireguard::PeerConfig>, @@ -90,7 +102,8 @@ impl Config { }) } - // should probably take a flag that alters between additive and overwriting conf + /// Returns a CString with the appropriate config for WireGuard-go + // TODO: Consider outputting both overriding and additive configs pub fn to_userspace_format(&self) -> CString { // the order of insertion matters, public key entry denotes a new peer entry let mut wg_conf = WgConfigBuffer::new(); @@ -115,7 +128,7 @@ impl Config { } } -pub enum ConfValue<'a> { +enum ConfValue<'a> { String(&'a str), Bytes(&'a [u8]), } @@ -142,7 +155,7 @@ impl<'a> ConfValue<'a> { } } -pub struct WgConfigBuffer { +struct WgConfigBuffer { buf: Vec<u8>, } diff --git a/talpid-core/src/tunnel/wireguard/connectivity_check.rs b/talpid-core/src/tunnel/wireguard/connectivity_check.rs index 8b6e03c04a..2b85b748bd 100644 --- a/talpid-core/src/tunnel/wireguard/connectivity_check.rs +++ b/talpid-core/src/tunnel/wireguard/connectivity_check.rs @@ -60,7 +60,7 @@ pub struct ConnectivityMonitor { impl ConnectivityMonitor { - pub fn new( + pub(super) fn new( addr: Ipv4Addr, interface: String, tunnel_handle: Weak<Mutex<Option<Box<dyn Tunnel>>>>, @@ -82,7 +82,7 @@ impl ConnectivityMonitor { // checks if the tunnel has ever worked. Intended to check if a connection to a tunnel is // successfull at the start of a connection. - pub fn establish_connectivity(&mut self) -> Result<bool, Error> { + pub(super) fn establish_connectivity(&mut self) -> Result<bool, Error> { if self.conn_state.connected() { return Ok(true); } @@ -99,7 +99,7 @@ impl ConnectivityMonitor { Ok(false) } - pub fn run(&mut self) -> Result<(), Error> { + pub(super) fn run(&mut self) -> Result<(), Error> { self.wait_loop(REGULAR_LOOP_SLEEP) } diff --git a/talpid-core/src/tunnel/wireguard/mod.rs b/talpid-core/src/tunnel/wireguard/mod.rs index efb44ebbc6..1381681a5b 100644 --- a/talpid-core/src/tunnel/wireguard/mod.rs +++ b/talpid-core/src/tunnel/wireguard/mod.rs @@ -1,5 +1,3 @@ -#![allow(missing_docs)] - use self::config::Config; #[cfg(not(windows))] use super::tun_provider; @@ -13,15 +11,16 @@ use std::{ }; use talpid_types::ErrorExt; +/// WireGuard config data-types pub mod config; mod connectivity_check; mod logging; mod stats; -pub mod wireguard_go; +mod wireguard_go; -pub use self::wireguard_go::WgGoTunnel; +use self::wireguard_go::WgGoTunnel; -pub type Result<T> = std::result::Result<T, Error>; +type Result<T> = std::result::Result<T, Error>; /// Errors that can happen in the Wireguard tunnel monitor. #[derive(err_derive::Error, Debug)] @@ -49,7 +48,10 @@ pub enum Error { /// Failed to tear down wireguard tunnel. #[error(display = "Failed to stop wireguard tunnel - {}", status)] - StopWireguardError { status: i32 }, + StopWireguardError { + /// Returned error code + status: i32, + }, /// Failed to get tunnel config #[error(display = "Failed to obtain tunnel config")] @@ -114,6 +116,7 @@ pub struct WireguardMonitor { } impl WireguardMonitor { + /// Starts a WireGuard tunnel with the given config pub fn start<F: Fn(TunnelEvent) + Send + Sync + Clone + 'static>( config: &Config, log_path: Option<&Path>, @@ -184,12 +187,14 @@ impl WireguardMonitor { Ok(monitor) } + /// Returns a close handle for the tunnel pub fn close_handle(&self) -> CloseHandle { CloseHandle { chan: self.close_msg_sender.clone(), } } + /// Blocks the current thread until tunnel disconnects pub fn wait(mut self) -> Result<()> { let wait_result = match self.close_msg_receiver.recv() { Ok(CloseMsg::PingErr) => Err(Error::TimeoutError), @@ -274,12 +279,14 @@ enum CloseMsg { PingErr, } +/// Close handle for a WireGuard tunnel. #[derive(Clone, Debug)] pub struct CloseHandle { chan: mpsc::Sender<CloseMsg>, } impl CloseHandle { + /// Closes a WireGuard tunnel pub fn close(&mut self) { if let Err(e) = self.chan.send(CloseMsg::Stop) { log::trace!("Failed to send close message to wireguard tunnel - {}", e); @@ -287,7 +294,7 @@ impl CloseHandle { } } -pub trait Tunnel: Send { +pub(crate) trait Tunnel: Send { fn get_interface_name(&self) -> &str; fn stop(self: Box<Self>) -> Result<()>; fn get_config(&self) -> Result<stats::Stats>; |
