diff options
| author | Linus Färnstrand <linus@mullvad.net> | 2017-05-18 13:13:41 +0200 |
|---|---|---|
| committer | Linus Färnstrand <linus@mullvad.net> | 2017-05-19 06:47:31 +0200 |
| commit | 13a6bc72fa0febb295d0fbef4bdc501897123fda (patch) | |
| tree | 587fe220203c13d566c3f04d4019107f48a32f32 | |
| parent | 24b07e9b1ce3b315b619c8de37e8b6a6194b3ca9 (diff) | |
| download | mullvadvpn-13a6bc72fa0febb295d0fbef4bdc501897123fda.tar.xz mullvadvpn-13a6bc72fa0febb295d0fbef4bdc501897123fda.zip | |
Add OpenVpnEventDispatcher struct
| -rw-r--r-- | talpid_core/Cargo.toml | 2 | ||||
| -rw-r--r-- | talpid_core/src/lib.rs | 7 | ||||
| -rw-r--r-- | talpid_core/src/tunnel/mod.rs | 2 | ||||
| -rw-r--r-- | talpid_core/src/tunnel/openvpn.rs | 79 |
4 files changed, 90 insertions, 0 deletions
diff --git a/talpid_core/Cargo.toml b/talpid_core/Cargo.toml index c098d204d9..18c998cf93 100644 --- a/talpid_core/Cargo.toml +++ b/talpid_core/Cargo.toml @@ -8,6 +8,8 @@ description = "Core backend functionality of the Mullvad VPN client" duct = "0.8" error-chain = "0.10" log = "0.3" +jsonrpc-core = { git = "https://github.com/faern/jsonrpc", branch = "bind-zero" } +jsonrpc-macros = { git = "https://github.com/faern/jsonrpc", branch = "bind-zero" } [dependencies.talpid_ipc] path = "../talpid_ipc" diff --git a/talpid_core/src/lib.rs b/talpid_core/src/lib.rs index 1a10532787..8a053bffa8 100644 --- a/talpid_core/src/lib.rs +++ b/talpid_core/src/lib.rs @@ -13,11 +13,18 @@ extern crate log; #[macro_use] extern crate error_chain; +extern crate jsonrpc_core; +#[macro_use] +extern crate jsonrpc_macros; extern crate talpid_ipc; +extern crate openvpn_ffi; /// Working with processes. pub mod process; /// Network primitives. pub mod net; + +/// Abstracts over different VPN tunnel technologies +pub mod tunnel; diff --git a/talpid_core/src/tunnel/mod.rs b/talpid_core/src/tunnel/mod.rs new file mode 100644 index 0000000000..0b477ca1c4 --- /dev/null +++ b/talpid_core/src/tunnel/mod.rs @@ -0,0 +1,2 @@ +/// A module for all OpenVPN related tunnel management. +pub mod openvpn; diff --git a/talpid_core/src/tunnel/openvpn.rs b/talpid_core/src/tunnel/openvpn.rs new file mode 100644 index 0000000000..883c0a7cad --- /dev/null +++ b/talpid_core/src/tunnel/openvpn.rs @@ -0,0 +1,79 @@ +use jsonrpc_core::{Error, IoHandler}; +use openvpn_ffi::{OpenVpnEnv, OpenVpnPluginEvent}; + +use talpid_ipc; + +/// IPC server for listening on events coming from plugin loaded into OpenVPN +pub struct OpenVpnEventDispatcher { + server: talpid_ipc::IpcServer, +} + +impl OpenVpnEventDispatcher { + /// Construct and start the IPC server with the given event listener callback. + pub fn start<L>(on_event: L) -> talpid_ipc::Result<Self> + where L: Fn(OpenVpnPluginEvent, OpenVpnEnv) + 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() + } + + /// Consumes the server and waits for it to finish. + 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, OpenVpnEnv) -> Result<(), Error>; + } + } +} +use self::api::*; + +struct OpenVpnEventApiImpl<L> + where L: Fn(OpenVpnPluginEvent, OpenVpnEnv) + Send + Sync + 'static +{ + on_event: L, +} + +impl<L> OpenVpnEventApi for OpenVpnEventApiImpl<L> + where L: Fn(OpenVpnPluginEvent, OpenVpnEnv) + Send + Sync + 'static +{ + fn openvpn_event(&self, event: OpenVpnPluginEvent, env: OpenVpnEnv) -> Result<(), Error> { + debug!("OpenVPN event {:?}", event); + (self.on_event)(event, env); + Ok(()) + } +} + + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + #[ignore] + fn openvpn_event_dispatcher_server() { + let server = OpenVpnEventDispatcher::start( + |event, env| { + println!("event: {:?}. env: {:?}", event, env); + }, + ) + .unwrap(); + println!("plugin server listening on {}", server.address()); + server.wait().unwrap(); + } +} |
