summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorLinus Färnstrand <linus@mullvad.net>2018-10-04 13:58:04 +0200
committerLinus Färnstrand <linus@mullvad.net>2018-10-10 12:36:00 +0200
commit496c25ab04da48fa5f12608f61c0d668e9614b9d (patch)
tree4e6127d11091a779c2df280ea0d31ecfa59bdcd2
parent6dc00a8cd1985451bd9d89c8b0f5105af88241f8 (diff)
downloadmullvadvpn-496c25ab04da48fa5f12608f61c0d668e9614b9d.tar.xz
mullvadvpn-496c25ab04da48fa5f12608f61c0d668e9614b9d.zip
Add wireframe for TunnelParametersGenerator
-rw-r--r--talpid-core/src/tunnel_state_machine/mod.rs24
1 files changed, 22 insertions, 2 deletions
diff --git a/talpid-core/src/tunnel_state_machine/mod.rs b/talpid-core/src/tunnel_state_machine/mod.rs
index 91ca9aa7ad..00286427d2 100644
--- a/talpid-core/src/tunnel_state_machine/mod.rs
+++ b/talpid-core/src/tunnel_state_machine/mod.rs
@@ -44,6 +44,7 @@ error_chain! {
/// Spawn the tunnel state machine thread, returning a channel for sending tunnel commands.
pub fn spawn<P, T>(
allow_lan: bool,
+ tunnel_parameters_generator: impl TunnelParametersGenerator,
log_dir: Option<PathBuf>,
resource_dir: PathBuf,
cache_dir: P,
@@ -59,6 +60,7 @@ where
thread::spawn(move || {
match create_event_loop(
allow_lan,
+ tunnel_parameters_generator,
log_dir,
resource_dir,
cache_dir,
@@ -92,6 +94,7 @@ where
fn create_event_loop<T>(
allow_lan: bool,
+ tunnel_parameters_generator: impl TunnelParametersGenerator,
log_dir: Option<PathBuf>,
resource_dir: PathBuf,
cache_dir: impl AsRef<Path>,
@@ -102,8 +105,14 @@ where
T: From<TunnelStateTransition> + Send + 'static,
{
let reactor = Core::new().chain_err(|| ErrorKind::ReactorError)?;
- let state_machine =
- TunnelStateMachine::new(allow_lan, log_dir, resource_dir, cache_dir, commands)?;
+ let state_machine = TunnelStateMachine::new(
+ allow_lan,
+ tunnel_parameters_generator,
+ log_dir,
+ resource_dir,
+ cache_dir,
+ commands,
+ )?;
let future = state_machine.for_each(move |state_change_event| {
state_change_listener
@@ -152,6 +161,7 @@ struct TunnelStateMachine {
impl TunnelStateMachine {
fn new(
allow_lan: bool,
+ tunnel_parameters_generator: impl TunnelParametersGenerator,
log_dir: Option<PathBuf>,
resource_dir: PathBuf,
cache_dir: impl AsRef<Path>,
@@ -162,6 +172,7 @@ impl TunnelStateMachine {
let mut shared_values = SharedTunnelStateValues {
security,
allow_lan,
+ tunnel_parameters_generator: Box::new(tunnel_parameters_generator),
log_dir,
resource_dir,
};
@@ -225,12 +236,21 @@ impl<T: TunnelState> From<EventConsequence<T>> for TunnelStateMachineAction {
}
}
+/// Trait for any type that can provide a stream of `TunnelParameters` to the `TunnelStateMachine`.
+pub trait TunnelParametersGenerator: Send + 'static {
+ /// Given the number of consecutive failed retry attempts, it should yield a `TunnelParameters`
+ /// to establish a tunnel with.
+ /// If this returns `None` then the state machine goes into the `Blocked` state.
+ fn generate(&mut self, retry_attempt: u32) -> Option<TunnelParameters>;
+}
/// Values that are common to all tunnel states.
struct SharedTunnelStateValues {
security: NetworkSecurity,
/// Should LAN access be allowed outside the tunnel.
allow_lan: bool,
+ /// The generator of new `TunnelParameter`s
+ tunnel_parameters_generator: Box<dyn TunnelParametersGenerator>,
/// Directory to store tunnel log file.
log_dir: Option<PathBuf>,
/// Resource directory path.