openvpn-plugin 0.2.0

A crate allowing easy creation of OpenVPN plugins in Rust
Documentation

openvpn-plugin is a crate that makes it easy to write OpenVPN plugins in Rust.

The crate contains two main things:

  • The openvpn_plugin! macro for generating the FFI interface OpenVPN will interact with
  • The FFI and safe Rust types needed to communicate with OpenVPN.

Usage

Edit your Cargo.toml to depend on this crate and set the type of your crate to a cdylib in order to make it compile to a shared library that OpenVPN will understand:

[lib]
crate-type = ["cdylib"]

[dependencies]
openvpn-plugin = "0.1"

Import the crate, including macros, in your crate root (lib.rs):

#[macro_use] extern crate openvpn_plugin;

Also in your crate root (lib.rs) define your handle type, the three callback functions and call the openvpn_plugin! macro to generate the corresponding FFI bindings. More details on the handle and the callback functions can be found in the documentation for the openvpn_plugin! macro.

pub struct Handle {
    // Fields needed for the plugin to keep state between callbacks
}

fn openvpn_open(
    args: &[CString],
    env: &HashMap<CString, CString>,
) -> Result<(Vec<openvpn_plugin::types::OpenVpnPluginEvent>, Handle), ::std::io::Error> {
    // Listen to only the `Up` event, which will be fired when a tunnel has been established.
    let events = vec![OpenVpnPluginEvent::Up];
    // Create the handle instance.
    let handle = Handle { /* ... */ };
    Ok((events, handle))
}

pub fn openvpn_close(handle: Handle) {
    println!("Plugin is closing down");
}

fn openvpn_event(
    event: openvpn_plugin::types::OpenVpnPluginEvent,
    args: &[CString],
    env: &HashMap<CString, CString>,
    handle: &mut Handle,
) -> Result<EventResult, ::std::io::Error> {
    /* Process the event */

    // If the processing worked fine and/or the request the callback represents should be
    // accepted, return `EventResult::Success`. See docs on this enum for more info.
    Ok(EventResult::Success)
}

openvpn_plugin!(::openvpn_open, ::openvpn_close, ::openvpn_event, Handle);