Skip to content

Commit

Permalink
Update WireGuard FFI in talpid-core
Browse files Browse the repository at this point in the history
  • Loading branch information
mvd-ows committed Feb 13, 2020
1 parent 2781b61 commit 57f3160
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 120 deletions.
86 changes: 86 additions & 0 deletions talpid-core/src/tunnel/wireguard/logging.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use super::{Error, Result};
use chrono;
use parking_lot::Mutex;
use std::{collections::HashMap, fs, io::Write, path::Path};

lazy_static::lazy_static! {
static ref LOG_MUTEX: Mutex<HashMap<u32, fs::File>> = Mutex::new(HashMap::new());
}

static mut LOG_CONTEXT_NEXT_ORDINAL: u32 = 0;

pub fn initialize_logging(log_path: Option<&Path>) -> Result<u32> {
let log_file = create_log_file(log_path)?;

let log_context_ordinal = unsafe {
let mut map = LOG_MUTEX.lock();
let ordinal = LOG_CONTEXT_NEXT_ORDINAL;
LOG_CONTEXT_NEXT_ORDINAL += 1;
map.insert(ordinal, log_file);
ordinal
};

Ok(log_context_ordinal)
}

#[cfg(target_os = "windows")]
static NULL_DEVICE: &str = "NUL";

#[cfg(not(target_os = "windows"))]
static NULL_DEVICE: &str = "/dev/null";

fn create_log_file(log_path: Option<&Path>) -> Result<fs::File> {
fs::File::create(log_path.unwrap_or(NULL_DEVICE.as_ref())).map_err(Error::PrepareLogFileError)
}

pub fn clean_up_logging(ordinal: u32) {
let mut map = LOG_MUTEX.lock();
map.remove(&ordinal);
}

// Callback that receives messages from WireGuard
pub unsafe extern "system" fn logging_callback(
level: WgLogLevel,
msg: *const libc::c_char,
context: *mut libc::c_void,
) {
let map = LOG_MUTEX.lock();
if let Some(mut logfile) = map.get(&(context as u32)) {
let managed_msg = if !msg.is_null() {
#[cfg(not(target_os = "windows"))]
let m = std::ffi::CStr::from_ptr(msg).to_string_lossy().to_string();
#[cfg(target_os = "windows")]
let m = std::ffi::CStr::from_ptr(msg)
.to_string_lossy()
.to_string()
.replace("\n", "\r\n");
m
} else {
"Logging message from WireGuard is NULL".to_string()
};

let level_str = match level {
WG_GO_LOG_DEBUG => "DEBUG",
WG_GO_LOG_INFO => "INFO",
WG_GO_LOG_ERROR | _ => "ERROR",
};

let _ = write!(
logfile,
"{}[{}][{}] {}",
chrono::Local::now().format("[%Y-%m-%d %H:%M:%S%.3f]"),
"wireguard-go",
level_str,
managed_msg
);
}
}

// unsafe fn

pub type WgLogLevel = u32;
// wireguard-go supports log levels 0 through 3 with 3 being the most verbose
// const WG_GO_LOG_SILENT: WgLogLevel = 0;
const WG_GO_LOG_ERROR: WgLogLevel = 1;
const WG_GO_LOG_INFO: WgLogLevel = 2;
const WG_GO_LOG_DEBUG: WgLogLevel = 3;
2 changes: 1 addition & 1 deletion talpid-core/src/tunnel/wireguard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use talpid_types::ErrorExt;

pub mod config;
mod connectivity_check;
mod logging;
mod stats;
pub mod wireguard_go;

Expand Down Expand Up @@ -134,7 +135,6 @@ impl WireguardMonitor {
route_handle
.add_default_route_callback(Some(WgGoTunnel::default_route_changed_callback), ());


let event_callback = Box::new(on_event.clone());
let (close_msg_sender, close_msg_receiver) = mpsc::channel();
let (pinger_tx, pinger_rx) = mpsc::channel();
Expand Down
Loading

0 comments on commit 57f3160

Please sign in to comment.