diff options
| author | David Lönnhager <david.l@mullvad.net> | 2023-08-10 12:38:21 +0200 |
|---|---|---|
| committer | David Lönnhager <david.l@mullvad.net> | 2023-08-31 13:25:41 +0200 |
| commit | 7feb1a1d73cbdbcbef0107f90f3a0550a0427a84 (patch) | |
| tree | 149ca9fb2c1ac9a551ec24432aa424d8f0f440ef /talpid-wireguard | |
| parent | f33aecedd72c0067a6d48b4e87f1eb36cbe48272 (diff) | |
| download | mullvadvpn-7feb1a1d73cbdbcbef0107f90f3a0550a0427a84.tar.xz mullvadvpn-7feb1a1d73cbdbcbef0107f90f3a0550a0427a84.zip | |
Hide implementation details in logging module
Diffstat (limited to 'talpid-wireguard')
| -rw-r--r-- | talpid-wireguard/src/logging.rs | 39 | ||||
| -rw-r--r-- | talpid-wireguard/src/wireguard_go.rs | 46 |
2 files changed, 42 insertions, 43 deletions
diff --git a/talpid-wireguard/src/logging.rs b/talpid-wireguard/src/logging.rs index 99cf405d23..b237b5cd09 100644 --- a/talpid-wireguard/src/logging.rs +++ b/talpid-wireguard/src/logging.rs @@ -1,7 +1,5 @@ use once_cell::sync::Lazy; use parking_lot::Mutex; -#[cfg(unix)] -use std::ffi::{c_char, c_void}; use std::{collections::HashMap, fmt, fs, io::Write, path::Path}; static LOG_MUTEX: Lazy<Mutex<HashMap<u32, fs::File>>> = Lazy::new(|| Mutex::new(HashMap::new())); @@ -46,10 +44,12 @@ pub fn clean_up_logging(ordinal: u32) { map.remove(&ordinal); } -#[allow(dead_code)] pub enum LogLevel { + #[cfg_attr(windows, allow(dead_code))] Verbose, + #[cfg_attr(not(windows), allow(dead_code))] Info, + #[cfg_attr(not(windows), allow(dead_code))] Warning, Error, } @@ -71,7 +71,6 @@ impl AsRef<str> for LogLevel { } } -#[cfg(windows)] pub fn log(context: u32, level: LogLevel, tag: &str, msg: &str) { let mut map = LOG_MUTEX.lock(); if let Some(logfile) = map.get_mut(&{ context }) { @@ -89,35 +88,3 @@ fn log_inner(logfile: &mut fs::File, level: LogLevel, tag: &str, msg: &str) { msg, ); } - -// Callback that receives messages from WireGuard -#[cfg(unix)] -pub unsafe extern "system" fn wg_go_logging_callback( - level: WgLogLevel, - msg: *const c_char, - context: *mut c_void, -) { - let mut map = LOG_MUTEX.lock(); - if let Some(logfile) = map.get_mut(&(context as u32)) { - let managed_msg = if !msg.is_null() { - std::ffi::CStr::from_ptr(msg).to_string_lossy().to_string() - } else { - "Logging message from WireGuard is NULL".to_string() - }; - - let level = match level { - WG_GO_LOG_VERBOSE => LogLevel::Verbose, - _ => LogLevel::Error, - }; - log_inner(logfile, level, "wireguard-go", &managed_msg); - } -} - -// 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; -#[cfg(unix)] -const WG_GO_LOG_VERBOSE: WgLogLevel = 2; - -#[cfg(unix)] -pub type WgLogLevel = u32; diff --git a/talpid-wireguard/src/wireguard_go.rs b/talpid-wireguard/src/wireguard_go.rs index 476507ea16..0d651f00bc 100644 --- a/talpid-wireguard/src/wireguard_go.rs +++ b/talpid-wireguard/src/wireguard_go.rs @@ -2,7 +2,7 @@ use super::{ stats::{Stats, StatsMap}, Config, Tunnel, TunnelError, }; -use crate::logging::{clean_up_logging, initialize_logging, wg_go_logging_callback, WgLogLevel}; +use crate::logging::{clean_up_logging, initialize_logging}; use ipnetwork::IpNetwork; use std::{ ffi::{c_char, c_void, CStr}, @@ -76,7 +76,7 @@ impl WgGoTunnel { mtu, wg_config_str.as_ptr() as _, tunnel_fd, - Some(wg_go_logging_callback), + Some(logging::wg_go_logging_callback), logging_context.0 as *mut c_void, ) }; @@ -269,9 +269,6 @@ fn check_wg_status(wg_code: i32) -> Result<()> { pub type Fd = std::os::unix::io::RawFd; -pub type LoggingCallback = - unsafe extern "system" fn(level: WgLogLevel, msg: *const c_char, context: *mut c_void); - const ERROR_GENERAL_FAILURE: i32 = -1; const ERROR_INTERMITTENT_FAILURE: i32 = -2; @@ -286,7 +283,7 @@ extern "C" { mtu: isize, settings: *const i8, fd: Fd, - logging_callback: Option<LoggingCallback>, + logging_callback: Option<logging::LoggingCallback>, logging_context: *mut c_void, ) -> i32; @@ -295,7 +292,7 @@ extern "C" { fn wgTurnOn( settings: *const i8, fd: Fd, - logging_callback: Option<LoggingCallback>, + logging_callback: Option<logging::LoggingCallback>, logging_context: *mut c_void, ) -> i32; @@ -427,3 +424,38 @@ mod stats { } } } + +mod logging { + use super::super::logging::{log, LogLevel}; + use std::ffi::{c_char, c_void}; + + // Callback that receives messages from WireGuard + pub unsafe extern "system" fn wg_go_logging_callback( + level: WgLogLevel, + msg: *const c_char, + context: *mut c_void, + ) { + let managed_msg = if !msg.is_null() { + std::ffi::CStr::from_ptr(msg).to_string_lossy().to_string() + } else { + "Logging message from WireGuard is NULL".to_string() + }; + + let level = match level { + WG_GO_LOG_VERBOSE => LogLevel::Verbose, + _ => LogLevel::Error, + }; + + log(context as u32, level, "wireguard-go", &managed_msg); + } + + // 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_VERBOSE: WgLogLevel = 2; + + pub type WgLogLevel = u32; + + pub type LoggingCallback = + unsafe extern "system" fn(level: WgLogLevel, msg: *const c_char, context: *mut c_void); +} |
