summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorLinus Färnstrand <linus@mullvad.net>2017-09-27 13:21:53 +0200
committerLinus Färnstrand <linus@mullvad.net>2017-09-27 13:21:53 +0200
commited4bc11810888e19974c4770e8cd8ad73376cafe (patch)
tree08480c4aa4bbd907ca03e22336c1dfdc178ad9db
parent30ce3cccbba086f7a0c668e7950cb539a0eefccc (diff)
parent7ef43ee157e737799d2b69c9f1806b9acec2baa6 (diff)
downloadmullvadvpn-ed4bc11810888e19974c4770e8cd8ad73376cafe.tar.xz
mullvadvpn-ed4bc11810888e19974c4770e8cd8ad73376cafe.zip
Merge branch 'clean-up-openvpn-event-dispatcher'
-rw-r--r--Cargo.lock1
-rw-r--r--talpid-core/Cargo.toml2
-rw-r--r--talpid-core/src/lib.rs5
-rw-r--r--talpid-core/src/tunnel/openvpn.rs89
4 files changed, 29 insertions, 68 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 563a7f25c1..69675812ef 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1186,7 +1186,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
name = "talpid-core"
version = "0.1.0"
dependencies = [
- "assert_matches 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"duct 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)",
"error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"jsonrpc-core 7.1.1 (git+https://github.com/paritytech/jsonrpc?tag=v7.1.1)",
diff --git a/talpid-core/Cargo.toml b/talpid-core/Cargo.toml
index 36357ee594..fc2df075ec 100644
--- a/talpid-core/Cargo.toml
+++ b/talpid-core/Cargo.toml
@@ -26,5 +26,3 @@ pfctl = { git = "ssh://git@github.com/mullvad/pfctl-rs.git" }
socket-relay = { path = "../socket-relay" }
tokio-core = "0.1"
-[dev-dependencies]
-assert_matches = "1.0"
diff --git a/talpid-core/src/lib.rs b/talpid-core/src/lib.rs
index c14377919e..da6c49c580 100644
--- a/talpid-core/src/lib.rs
+++ b/talpid-core/src/lib.rs
@@ -10,11 +10,6 @@
//! GNU General Public License as published by the Free Software Foundation, either version 3 of
//! the License, or (at your option) any later version.
-
-#[cfg(test)]
-#[macro_use]
-extern crate assert_matches;
-
extern crate duct;
#[macro_use]
diff --git a/talpid-core/src/tunnel/openvpn.rs b/talpid-core/src/tunnel/openvpn.rs
index c9ca9b49ef..f2cc1da7da 100644
--- a/talpid-core/src/tunnel/openvpn.rs
+++ b/talpid-core/src/tunnel/openvpn.rs
@@ -1,5 +1,4 @@
use duct;
-use jsonrpc_core::{Error, IoHandler};
use openvpn_plugin::types::OpenVpnPluginEvent;
use process::openvpn::OpenVpnCommand;
@@ -7,7 +6,6 @@ use std::collections::HashMap;
use std::io;
use std::path::Path;
use std::process::ExitStatus;
-use std::result::Result as StdResult;
use std::sync::{mpsc, Arc};
use std::sync::atomic::{AtomicBool, Ordering};
use std::thread;
@@ -42,7 +40,7 @@ lazy_static!{
#[derive(Debug)]
pub struct OpenVpnMonitor<C: OpenVpnBuilder = OpenVpnCommand> {
child: Arc<C::ProcessHandle>,
- event_dispatcher: Option<OpenVpnEventDispatcher>,
+ event_dispatcher: Option<talpid_ipc::IpcServer>,
closed: Arc<AtomicBool>,
}
@@ -65,7 +63,7 @@ impl<C: OpenVpnBuilder> OpenVpnMonitor<C> {
P: AsRef<Path>,
{
let event_dispatcher =
- OpenVpnEventDispatcher::start(on_event).chain_err(|| ErrorKind::EventDispatcherError)?;
+ event_server::start(on_event).chain_err(|| ErrorKind::EventDispatcherError)?;
cmd.plugin(plugin_path, vec![event_dispatcher.address().to_owned()]);
let child = cmd.start()
@@ -108,7 +106,7 @@ impl<C: OpenVpnBuilder> OpenVpnMonitor<C> {
Err(e).chain_err(|| ErrorKind::ChildProcessError("Error when waiting"))
}
WaitResult::EventDispatcher(result) => {
- error!("OpenVpnEventDispatcher exited unexpectedly: {:?}", result);
+ error!("OpenVPN Event server exited unexpectedly: {:?}", result);
match result {
Ok(()) => Err(ErrorKind::EventDispatcherError.into()),
Err(e) => Err(e).chain_err(|| ErrorKind::EventDispatcherError),
@@ -225,78 +223,49 @@ impl ProcessHandle for duct::Handle {
}
-/// IPC server for listening to events coming from plugin loaded into OpenVPN.
-#[derive(Debug)]
-pub struct OpenVpnEventDispatcher {
- server: talpid_ipc::IpcServer,
-}
-impl OpenVpnEventDispatcher {
+mod event_server {
+ use super::OpenVpnPluginEvent;
+ use jsonrpc_core::{Error, IoHandler};
+ use std::collections::HashMap;
+ use talpid_ipc;
+
/// Construct and start the IPC server with the given event listener callback.
- pub fn start<L>(on_event: L) -> talpid_ipc::Result<Self>
+ pub fn start<L>(on_event: L) -> talpid_ipc::Result<talpid_ipc::IpcServer>
where
L: Fn(OpenVpnPluginEvent, HashMap<String, String>) + Send + Sync + 'static,
{
let rpc = OpenVpnEventApiImpl { on_event };
let mut io = IoHandler::new();
io.extend_with(rpc.to_delegate());
- let server = talpid_ipc::IpcServer::start(io.into())?;
- Ok(OpenVpnEventDispatcher { server })
- }
-
- /// Returns the local address this server is listening on.
- pub fn address(&self) -> &str {
- self.server.address()
+ talpid_ipc::IpcServer::start(io.into())
}
- /// Creates a handle to this event dispatcher, allowing the listening server to be closed
- /// while
- /// some other thread is blocked in `wait`.
- pub fn close_handle(&self) -> talpid_ipc::CloseHandle {
- self.server.close_handle()
- }
-
- /// Consumes the server and waits for it to finish. Returns an error if the server exited
- /// due to an error.
- pub fn wait(self) -> talpid_ipc::Result<()> {
- self.server.wait()
- }
-}
-
-
-mod api {
- use super::*;
build_rpc_trait! {
pub trait OpenVpnEventApi {
#[rpc(name = "openvpn_event")]
- fn openvpn_event(&self,
- OpenVpnPluginEvent,
- HashMap<String, String>)
- -> StdResult<(), Error>;
+ fn openvpn_event(&self, OpenVpnPluginEvent, HashMap<String, String>)
+ -> Result<(), Error>;
}
}
-}
-use self::api::*;
-struct OpenVpnEventApiImpl<L>
-where
- L: Fn(OpenVpnPluginEvent, HashMap<String, String>) + Send + Sync + 'static,
-{
- on_event: L,
-}
+ struct OpenVpnEventApiImpl<L> {
+ on_event: L,
+ }
-impl<L> OpenVpnEventApi for OpenVpnEventApiImpl<L>
-where
- L: Fn(OpenVpnPluginEvent, HashMap<String, String>) + Send + Sync + 'static,
-{
- fn openvpn_event(
- &self,
- event: OpenVpnPluginEvent,
- env: HashMap<String, String>,
- ) -> StdResult<(), Error> {
- debug!("OpenVPN event {:?}", event);
- (self.on_event)(event, env);
- Ok(())
+ impl<L> OpenVpnEventApi for OpenVpnEventApiImpl<L>
+ where
+ L: Fn(OpenVpnPluginEvent, HashMap<String, String>) + Send + Sync + 'static,
+ {
+ fn openvpn_event(
+ &self,
+ event: OpenVpnPluginEvent,
+ env: HashMap<String, String>,
+ ) -> Result<(), Error> {
+ debug!("OpenVPN event {:?}", event);
+ (self.on_event)(event, env);
+ Ok(())
+ }
}
}