diff options
| author | Odd Stranne <odd@mullvad.net> | 2019-09-10 11:21:55 +0200 |
|---|---|---|
| committer | Odd Stranne <odd@mullvad.net> | 2019-09-10 11:21:55 +0200 |
| commit | 235506bc6ec9b453716b2f85e4abdb57dd54380d (patch) | |
| tree | d1a18ee4e56206a7a2b94324ba8928fd0b402770 | |
| parent | 812a37076e8a92782f33b26ad66dc9745ce82df2 (diff) | |
| parent | 62e6298c204daaf31ed3ff380eb9baede116b718 (diff) | |
| download | mullvadvpn-235506bc6ec9b453716b2f85e4abdb57dd54380d.tar.xz mullvadvpn-235506bc6ec9b453716b2f85e4abdb57dd54380d.zip | |
Merge branch 'win-log-offline'
| -rw-r--r-- | talpid-core/src/firewall/windows.rs | 8 | ||||
| -rw-r--r-- | talpid-core/src/offline/windows.rs | 2 | ||||
| -rw-r--r-- | talpid-core/src/winnet.rs | 51 | ||||
| -rw-r--r-- | windows/shared/logsink.h | 26 | ||||
| -rw-r--r-- | windows/shared/logsinkadapter.h | 52 | ||||
| m--------- | windows/windows-libraries | 0 | ||||
| -rw-r--r-- | windows/winnet/src/winnet/netmonitor.cpp | 188 | ||||
| -rw-r--r-- | windows/winnet/src/winnet/netmonitor.h | 13 | ||||
| -rw-r--r-- | windows/winnet/src/winnet/winnet.cpp | 101 | ||||
| -rw-r--r-- | windows/winnet/src/winnet/winnet.h | 49 |
10 files changed, 368 insertions, 122 deletions
diff --git a/talpid-core/src/firewall/windows.rs b/talpid-core/src/firewall/windows.rs index e4af592f38..4f32013c39 100644 --- a/talpid-core/src/firewall/windows.rs +++ b/talpid-core/src/firewall/windows.rs @@ -55,7 +55,7 @@ impl FirewallT for Firewall { WinFw_InitializeBlocked( WINFW_TIMEOUT_SECONDS, &cfg, - Some(winnet::error_sink), + Some(winnet::log_sink), ptr::null_mut(), ) .into_result()? @@ -64,7 +64,7 @@ impl FirewallT for Firewall { unsafe { WinFw_Initialize( WINFW_TIMEOUT_SECONDS, - Some(winnet::error_sink), + Some(winnet::log_sink), ptr::null_mut(), ) .into_result()? @@ -253,7 +253,7 @@ mod winfw { #[link_name = "WinFw_Initialize"] pub fn WinFw_Initialize( timeout: libc::c_uint, - sink: Option<winnet::ErrorSink>, + sink: Option<winnet::LogSink>, sink_context: *mut libc::c_void, ) -> InitializationResult; @@ -261,7 +261,7 @@ mod winfw { pub fn WinFw_InitializeBlocked( timeout: libc::c_uint, settings: &WinFwSettings, - sink: Option<winnet::ErrorSink>, + sink: Option<winnet::LogSink>, sink_context: *mut libc::c_void, ) -> InitializationResult; diff --git a/talpid-core/src/offline/windows.rs b/talpid-core/src/offline/windows.rs index 81a9344d3f..6d19bbb6c5 100644 --- a/talpid-core/src/offline/windows.rs +++ b/talpid-core/src/offline/windows.rs @@ -201,7 +201,7 @@ impl BroadcastListener { Some(Self::connectivity_callback), callback_context, &mut current_connectivity as *mut _, - Some(winnet::error_sink), + Some(winnet::log_sink), ptr::null_mut(), ) { return Err(Error::ConnectivityMonitorError); diff --git a/talpid-core/src/winnet.rs b/talpid-core/src/winnet.rs index eb8694bf27..a99bf8bdef 100644 --- a/talpid-core/src/winnet.rs +++ b/talpid-core/src/winnet.rs @@ -1,6 +1,6 @@ use self::api::*; pub use self::api::{ - ErrorSink, WinNet_ActivateConnectivityMonitor, WinNet_DeactivateConnectivityMonitor, + LogSink, WinNet_ActivateConnectivityMonitor, WinNet_DeactivateConnectivityMonitor, }; use libc::{c_char, c_void, wchar_t}; use std::{ffi::OsString, ptr}; @@ -30,13 +30,28 @@ pub enum Error { ConnectivityUnkown, } -/// Error callback used with `winnet.dll`. -pub extern "system" fn error_sink(msg: *const c_char, _ctx: *mut c_void) { +#[allow(dead_code)] +#[repr(u8)] +pub enum LogSeverity { + Error = 0, + Warning, + Info, + Trace, +} + +/// Logging callback used with `winnet.dll`. +pub extern "system" fn log_sink(severity: LogSeverity, msg: *const c_char, _ctx: *mut c_void) { use std::ffi::CStr; if msg.is_null() { log::error!("Log message from FFI boundary is NULL"); } else { - log::error!("{}", unsafe { CStr::from_ptr(msg).to_string_lossy() }); + let managed_msg = unsafe { CStr::from_ptr(msg).to_string_lossy() }; + match severity { + LogSeverity::Warning => log::warn!("{}", managed_msg), + LogSeverity::Info => log::info!("{}", managed_msg), + LogSeverity::Trace => log::trace!("{}", managed_msg), + _ => log::error!("{}", managed_msg), + } } } @@ -46,11 +61,7 @@ pub fn ensure_top_metric_for_interface(interface_alias: &str) -> Result<bool, Er WideCString::from_str(interface_alias).map_err(Error::InvalidInterfaceAlias)?; let metric_result = unsafe { - WinNet_EnsureTopMetric( - interface_alias_ws.as_ptr(), - Some(error_sink), - ptr::null_mut(), - ) + WinNet_EnsureTopMetric(interface_alias_ws.as_ptr(), Some(log_sink), ptr::null_mut()) }; match metric_result { @@ -71,7 +82,7 @@ pub fn ensure_top_metric_for_interface(interface_alias: &str) -> Result<bool, Er /// Checks if IPv6 is enabled for the TAP interface pub fn get_tap_interface_ipv6_status() -> Result<bool, Error> { let tap_ipv6_status = - unsafe { WinNet_GetTapInterfaceIpv6Status(Some(error_sink), ptr::null_mut()) }; + unsafe { WinNet_GetTapInterfaceIpv6Status(Some(log_sink), ptr::null_mut()) }; match tap_ipv6_status { // Enabled @@ -95,7 +106,7 @@ pub fn get_tap_interface_ipv6_status() -> Result<bool, Error> { pub fn get_tap_interface_alias() -> Result<OsString, Error> { let mut alias_ptr: *mut wchar_t = ptr::null_mut(); let status = unsafe { - WinNet_GetTapInterfaceAlias(&mut alias_ptr as *mut _, Some(error_sink), ptr::null_mut()) + WinNet_GetTapInterfaceAlias(&mut alias_ptr as *mut _, Some(log_sink), ptr::null_mut()) }; if !status { @@ -110,7 +121,7 @@ pub fn get_tap_interface_alias() -> Result<OsString, Error> { /// Returns true if current host is not connected to any network pub fn is_offline() -> Result<bool, Error> { - match unsafe { WinNet_CheckConnectivity(Some(error_sink), ptr::null_mut()) } { + match unsafe { WinNet_CheckConnectivity(Some(log_sink), ptr::null_mut()) } { // Not connected 0 => Ok(true), // Connected @@ -123,10 +134,12 @@ pub fn is_offline() -> Result<bool, Error> { #[allow(non_snake_case)] mod api { + use super::LogSeverity; use libc::{c_char, c_void, wchar_t}; - /// Error callback type for use with `winnet.dll`. - pub type ErrorSink = extern "system" fn(msg: *const c_char, ctx: *mut c_void); + /// logging callback type for use with `winnet.dll`. + pub type LogSink = + extern "system" fn(severity: LogSeverity, msg: *const c_char, ctx: *mut c_void); pub type ConnectivityCallback = unsafe extern "system" fn(is_connected: bool, ctx: *mut c_void); @@ -134,20 +147,20 @@ mod api { #[link_name = "WinNet_EnsureTopMetric"] pub fn WinNet_EnsureTopMetric( tunnel_interface_alias: *const wchar_t, - sink: Option<ErrorSink>, + sink: Option<LogSink>, sink_context: *mut c_void, ) -> u32; #[link_name = "WinNet_GetTapInterfaceIpv6Status"] pub fn WinNet_GetTapInterfaceIpv6Status( - sink: Option<ErrorSink>, + sink: Option<LogSink>, sink_context: *mut c_void, ) -> u32; #[link_name = "WinNet_GetTapInterfaceAlias"] pub fn WinNet_GetTapInterfaceAlias( tunnel_interface_alias: *mut *mut wchar_t, - sink: Option<ErrorSink>, + sink: Option<LogSink>, sink_context: *mut c_void, ) -> bool; @@ -159,7 +172,7 @@ mod api { callback: Option<ConnectivityCallback>, callbackContext: *mut libc::c_void, currentConnectivity: *mut bool, - sink: Option<ErrorSink>, + sink: Option<LogSink>, sink_context: *mut c_void, ) -> bool; @@ -167,6 +180,6 @@ mod api { pub fn WinNet_DeactivateConnectivityMonitor() -> bool; #[link_name = "WinNet_CheckConnectivity"] - pub fn WinNet_CheckConnectivity(sink: Option<ErrorSink>, sink_context: *mut c_void) -> u32; + pub fn WinNet_CheckConnectivity(sink: Option<LogSink>, sink_context: *mut c_void) -> u32; } } diff --git a/windows/shared/logsink.h b/windows/shared/logsink.h new file mode 100644 index 0000000000..16a84d4def --- /dev/null +++ b/windows/shared/logsink.h @@ -0,0 +1,26 @@ +#pragma once + +// +// This file is shared between DLL modules to help define their public interface. +// It should always be C-compatible. +// + +enum MULLVAD_LOG_SINK_SEVERITY +{ + MULLVAD_LOG_SINK_SEVERITY_ERROR = 0, + MULLVAD_LOG_SINK_SEVERITY_WARNING, + MULLVAD_LOG_SINK_SEVERITY_INFO, + MULLVAD_LOG_SINK_SEVERITY_TRACE +}; + +// +// The log sink is registered with a DLL during e.g. initialization. +// It may later be activated as a direct or indirect result of calling into the DLL. +// +// The parameters are: +// +// `MULLVAD_LOG_SINK_SEVERITY` - Severity of the message. +// `const char *` - The message itself. +// `void *` - The sink context that was registered along with the sink. +// +typedef void (__stdcall *MullvadLogSink)(MULLVAD_LOG_SINK_SEVERITY, const char *, void *); diff --git a/windows/shared/logsinkadapter.h b/windows/shared/logsinkadapter.h new file mode 100644 index 0000000000..cbeca9e148 --- /dev/null +++ b/windows/shared/logsinkadapter.h @@ -0,0 +1,52 @@ +#include "logsink.h" +#include <libcommon/logging/logsink.h> + +namespace shared +{ + +// +// Adapt common::logging::LogSink C++ world to +// MullvadLogSink C world. +// +class LogSinkAdapter : public common::logging::LogSink +{ +public: + + LogSinkAdapter(MullvadLogSink target, void *context) + : LogSink(MakeAdapter(target, context)) + { + } + +private: + + static common::logging::LogTarget MakeAdapter(MullvadLogSink target, void *context) + { + return [target, context](common::logging::Severity s, const char *msg) + { + if (nullptr == target) + { + return; + } + + const MULLVAD_LOG_SINK_SEVERITY severity = [s]() + { + switch (s) + { + case common::logging::Severity::Warning: + return MULLVAD_LOG_SINK_SEVERITY_WARNING; + case common::logging::Severity::Info: + return MULLVAD_LOG_SINK_SEVERITY_INFO; + case common::logging::Severity::Trace: + return MULLVAD_LOG_SINK_SEVERITY_TRACE; + case common::logging::Severity::Error: + default: + return MULLVAD_LOG_SINK_SEVERITY_ERROR; + } + }(); + + target(severity, msg, context); + }; + } +}; + +} diff --git a/windows/windows-libraries b/windows/windows-libraries -Subproject 4811e79d118b454397c8930ad18338e25b29db1 +Subproject ca2c530673c4cfe1dfbafe115508fe3ba284e34 diff --git a/windows/winnet/src/winnet/netmonitor.cpp b/windows/winnet/src/winnet/netmonitor.cpp index a6c2903397..99a31f7abb 100644 --- a/windows/winnet/src/winnet/netmonitor.cpp +++ b/windows/winnet/src/winnet/netmonitor.cpp @@ -2,7 +2,8 @@ #include "netmonitor.h" #include <libcommon/error.h> #include <libcommon/memory.h> -#include <libcommon/synchronization.h> +#include <libcommon/string.h> +#include <sstream> namespace { @@ -28,11 +29,17 @@ bool ValidInterfaceType(const MIB_IF_ROW2 &iface) return true; } -} // anonyomus namespace +} // anonymous namespace -NetMonitor::NetMonitor(NetMonitor::Notifier notifier, bool ¤tConnectivity) - : m_connected(false) +NetMonitor::NetMonitor +( + std::shared_ptr<common::logging::ILogSink> logSink, + NetMonitor::Notifier notifier, + bool ¤tConnectivity +) + : m_logSink(logSink) , m_notifier(notifier) + , m_connected(false) , m_notificationHandle(nullptr) { m_cache = CreateCache(); @@ -43,6 +50,11 @@ NetMonitor::NetMonitor(NetMonitor::Notifier notifier, bool ¤tConnectivity) const auto status = NotifyIpInterfaceChange(AF_UNSPEC, Callback, this, FALSE, &m_notificationHandle); THROW_UNLESS(NO_ERROR, status, "Register interface change notification"); + + if (false == m_connected) + { + LogOfflineState(m_logSink); + } } NetMonitor::~NetMonitor() @@ -51,9 +63,23 @@ NetMonitor::~NetMonitor() } // static -bool NetMonitor::CheckConnectivity() +bool NetMonitor::CheckConnectivity(std::shared_ptr<common::logging::ILogSink> logSink) { - return CheckConnectivity(CreateCache()); + static bool loggedOffline = false; + + const auto connected = CheckConnectivity(CreateCache()); + + if (connected) + { + loggedOffline = false; + } + else if (false == loggedOffline) + { + LogOfflineState(logSink); + loggedOffline = true; + } + + return connected; } // static @@ -78,6 +104,7 @@ NetMonitor::Cache NetMonitor::CreateCache() { AddCacheEntry(cache, table->Table[i]); } + return cache; } @@ -126,9 +153,12 @@ void NetMonitor::updateConnectivity() //static void __stdcall NetMonitor::Callback(void *context, MIB_IPINTERFACE_ROW *hint, MIB_NOTIFICATION_TYPE updateType) { - auto thiz = reinterpret_cast<NetMonitor *>(context); + reinterpret_cast<NetMonitor *>(context)->callback(hint, updateType); +} - common::sync::ScopeLock<> processingLock(thiz->m_processingMutex); +void NetMonitor::callback(MIB_IPINTERFACE_ROW *hint, MIB_NOTIFICATION_TYPE updateType) +{ + std::scoped_lock<std::mutex> processingLock(m_processingMutex); switch (updateType) { @@ -143,15 +173,15 @@ void __stdcall NetMonitor::Callback(void *context, MIB_IPINTERFACE_ROW *hint, MI return; } - thiz->AddCacheEntry(thiz->m_cache, iface); + AddCacheEntry(m_cache, iface); break; } case MibDeleteInstance: { - const auto cacheEntry = thiz->m_cache.find(hint->InterfaceLuid.Value); + const auto cacheEntry = m_cache.find(hint->InterfaceLuid.Value); - if (thiz->m_cache.end() != cacheEntry) + if (m_cache.end() != cacheEntry) { cacheEntry->second.connected = false; } @@ -160,9 +190,9 @@ void __stdcall NetMonitor::Callback(void *context, MIB_IPINTERFACE_ROW *hint, MI } case MibParameterNotification: { - auto cacheEntry = thiz->m_cache.find(hint->InterfaceLuid.Value); + auto cacheEntry = m_cache.find(hint->InterfaceLuid.Value); - if (thiz->m_cache.end() == cacheEntry) + if (m_cache.end() == cacheEntry) { // // A change occurred on an interface that we're not tracking. @@ -178,7 +208,7 @@ void __stdcall NetMonitor::Callback(void *context, MIB_IPINTERFACE_ROW *hint, MI return; } - thiz->AddCacheEntry(thiz->m_cache, iface); + AddCacheEntry(m_cache, iface); } else { @@ -207,12 +237,134 @@ void __stdcall NetMonitor::Callback(void *context, MIB_IPINTERFACE_ROW *hint, MI } } - const auto previousConnectivity = thiz->m_connected; + const auto previousConnectivity = m_connected; + + updateConnectivity(); + + if (previousConnectivity != m_connected) + { + m_notifier(m_connected); + + if (false == m_connected) + { + LogOfflineState(m_logSink); + } + } +} + +//static +void NetMonitor::LogOfflineState(std::shared_ptr<common::logging::ILogSink> logSink) +{ + // + // There is a race condition here because logging is not done using the + // same data set that the online/offline logic processes. + // + // Not much of a problem really, this is temporary logging. + // + + logSink->info("Machine is offline"); + + MIB_IF_TABLE2 *table; + + const auto status = GetIfTable2(&table); + + if (NO_ERROR != status) + { + logSink->error("Failed to acquire list of network interfaces. Aborting detailed logging"); + return; + } + + common::memory::ScopeDestructor sd; - thiz->updateConnectivity(); + sd += [table]() + { + FreeMibTable(table); + }; - if (previousConnectivity != thiz->m_connected) + logSink->info("Begin detailed listing of network interfaces"); + + for (ULONG i = 0; i < table->NumEntries; ++i) { - thiz->m_notifier(thiz->m_connected); + const auto &iface = table->Table[i]; + + std::stringstream ss; + + ss << "Detailed interface logging" << std::endl; + ss << "Interface ordinal " << i << std::endl; + + // + // Don't flood the log with garbage. + // + const auto blacklist = std::vector<std::wstring> + { + L"WFP Native MAC Layer LightWeight Filter", + L"QoS Packet Scheduler", + L"WFP 802.3 MAC Layer LightWeight Filter", + L"Microsoft Kernel Debug Network Adapter", + L"Software Loopback Interface", + L"Microsoft Teredo Tunneling Adapter", + L"Microsoft IP-HTTPS Platform Adapter", + L"Microsoft 6to4 Adapter", + }; + + bool blacklisted = false; + + for (const auto &black : blacklist) + { + if (nullptr != wcsstr(iface.Description, black.c_str())) + { + blacklisted = true; + break; + } + } + + if (blacklisted) + { + ss << " filtered out to avoid flooding log"; + logSink->info(ss.str().c_str()); + + continue; + } + + { + const auto s = std::wstring(L" Alias: ").append(iface.Alias); + ss << common::string::ToAnsi(s) << std::endl; + } + + { + const auto s = std::wstring(L" \"Description\": ").append(iface.Description); + ss << common::string::ToAnsi(s) << std::endl; + } + + ss << " PhysicalAddressLength: " << iface.PhysicalAddressLength << std::endl; + ss << " Type: " << iface.Type << std::endl; + ss << " MediaType: " << iface.MediaType << std::endl; + ss << " PhysicalMediumType: " << iface.PhysicalMediumType << std::endl; + ss << " AccessType: " << iface.AccessType << std::endl; + + // + // Bool cast prevents idiot stream from inserting literal 0/1. + // + + ss << " InterfaceAndOperStatusFlags.HardwareInterface: " << (bool)iface.InterfaceAndOperStatusFlags.HardwareInterface << std::endl; + ss << " InterfaceAndOperStatusFlags.FilterInterface: " << (bool)iface.InterfaceAndOperStatusFlags.FilterInterface << std::endl; + ss << " InterfaceAndOperStatusFlags.ConnectorPresent: " << (bool)iface.InterfaceAndOperStatusFlags.ConnectorPresent << std::endl; + ss << " InterfaceAndOperStatusFlags.NotAuthenticated: " << (bool)iface.InterfaceAndOperStatusFlags.NotAuthenticated << std::endl; + ss << " InterfaceAndOperStatusFlags.NotMediaConnected: " << (bool)iface.InterfaceAndOperStatusFlags.NotMediaConnected << std::endl; + ss << " InterfaceAndOperStatusFlags.Paused: " << (bool)iface.InterfaceAndOperStatusFlags.Paused << std::endl; + ss << " InterfaceAndOperStatusFlags.LowPower: " << (bool)iface.InterfaceAndOperStatusFlags.LowPower << std::endl; + ss << " InterfaceAndOperStatusFlags.EndPointInterface: " << (bool)iface.InterfaceAndOperStatusFlags.EndPointInterface << std::endl; + + ss << " OperStatus: " << iface.OperStatus << std::endl; + ss << " AdminStatus: " << iface.AdminStatus << std::endl; + ss << " MediaConnectState: " << iface.MediaConnectState << std::endl; + ss << " TransmitLinkSpeed: " << iface.TransmitLinkSpeed << std::endl; + + ss << " ReceiveLinkSpeed: " << iface.ReceiveLinkSpeed << std::endl; + ss << " InUcastPkts:" << iface.InUcastPkts; + + logSink->info(ss.str().c_str()); } + + logSink->info("End detailed listing of network interfaces"); } diff --git a/windows/winnet/src/winnet/netmonitor.h b/windows/winnet/src/winnet/netmonitor.h index 77e4417b31..a3e0a28281 100644 --- a/windows/winnet/src/winnet/netmonitor.h +++ b/windows/winnet/src/winnet/netmonitor.h @@ -1,5 +1,7 @@ #pragma once +#include <libcommon/logging/ilogsink.h> +#include <memory> #include <map> #include <string> #include <cstdint> @@ -20,13 +22,16 @@ public: // using Notifier = std::function<void(bool)>; - NetMonitor(Notifier notifier, bool ¤tConnectivity); + NetMonitor(std::shared_ptr<common::logging::ILogSink> logSink, Notifier notifier, bool ¤tConnectivity); ~NetMonitor(); - static bool CheckConnectivity(); + static bool CheckConnectivity(std::shared_ptr<common::logging::ILogSink> logSink); private: + std::shared_ptr<common::logging::ILogSink> m_logSink; + Notifier m_notifier; + struct CacheEntry { // Unique interface identifier. @@ -44,7 +49,6 @@ private: std::mutex m_processingMutex; Cache m_cache; bool m_connected; - Notifier m_notifier; HANDLE m_notificationHandle; @@ -55,4 +59,7 @@ private: void updateConnectivity(); static void __stdcall Callback(void *context, MIB_IPINTERFACE_ROW *hint, MIB_NOTIFICATION_TYPE updateType); + void callback(MIB_IPINTERFACE_ROW *hint, MIB_NOTIFICATION_TYPE updateType); + + static void LogOfflineState(std::shared_ptr<common::logging::ILogSink> logSink); }; diff --git a/windows/winnet/src/winnet/winnet.cpp b/windows/winnet/src/winnet/winnet.cpp index 2465ec667b..a3e47c7b7c 100644 --- a/windows/winnet/src/winnet/winnet.cpp +++ b/windows/winnet/src/winnet/winnet.cpp @@ -2,16 +2,30 @@ #include "winnet.h"
#include "NetworkInterfaces.h"
#include "interfaceutils.h"
-#include "libcommon/error.h"
#include "netmonitor.h"
+#include "../../shared/logsinkadapter.h"
+#include <libcommon/error.h>
#include <cstdint>
#include <stdexcept>
+#include <memory>
namespace
{
NetMonitor *g_NetMonitor = nullptr;
+void UnwindAndLog(MullvadLogSink logSink, void *logSinkContext, const std::exception &err)
+{
+ if (nullptr == logSink)
+ {
+ return;
+ }
+
+ auto logger = std::make_shared<shared::LogSinkAdapter>(logSink, logSinkContext);
+
+ common::error::UnwindException(err, logger);
+}
+
} //anonymous namespace
extern "C"
@@ -20,28 +34,24 @@ WINNET_ETM_STATUS WINNET_API
WinNet_EnsureTopMetric(
const wchar_t *deviceAlias,
- WinNetErrorSink errorSink,
- void *errorSinkContext
+ MullvadLogSink logSink,
+ void *logSinkContext
)
{
try
{
NetworkInterfaces interfaces;
bool metrics_set = interfaces.SetTopMetricForInterfacesByAlias(deviceAlias);
- return metrics_set ? WINNET_ETM_STATUS::METRIC_SET : WINNET_ETM_STATUS::METRIC_NO_CHANGE;
+ return metrics_set ? WINNET_ETM_STATUS_METRIC_SET : WINNET_ETM_STATUS_METRIC_NO_CHANGE;
}
- catch (std::exception &err)
+ catch (const std::exception &err)
{
- if (nullptr != errorSink)
- {
- errorSink(err.what(), errorSinkContext);
- }
-
- return WINNET_ETM_STATUS::FAILURE;
+ UnwindAndLog(logSink, logSinkContext, err);
+ return WINNET_ETM_STATUS_FAILURE;
}
catch (...)
{
- return WINNET_ETM_STATUS::FAILURE;
+ return WINNET_ETM_STATUS_FAILURE;
}
};
@@ -50,8 +60,8 @@ WINNET_LINKAGE WINNET_GTII_STATUS
WINNET_API
WinNet_GetTapInterfaceIpv6Status(
- WinNetErrorSink errorSink,
- void *errorSinkContext
+ MullvadLogSink logSink,
+ void *logSinkContext
)
{
try
@@ -65,28 +75,24 @@ WinNet_GetTapInterfaceIpv6Status( if (NO_ERROR == status)
{
- return WINNET_GTII_STATUS::ENABLED;
+ return WINNET_GTII_STATUS_ENABLED;
}
if (ERROR_NOT_FOUND == status)
{
- return WINNET_GTII_STATUS::DISABLED;
+ return WINNET_GTII_STATUS_DISABLED;
}
common::error::Throw("Resolve TAP IPv6 interface", status);
}
- catch (std::exception &err)
+ catch (const std::exception &err)
{
- if (nullptr != errorSink)
- {
- errorSink(err.what(), errorSinkContext);
- }
-
- return WINNET_GTII_STATUS::FAILURE;
+ UnwindAndLog(logSink, logSinkContext, err);
+ return WINNET_GTII_STATUS_FAILURE;
}
catch (...)
{
- return WINNET_GTII_STATUS::FAILURE;
+ return WINNET_GTII_STATUS_FAILURE;
}
}
@@ -96,8 +102,8 @@ bool WINNET_API
WinNet_GetTapInterfaceAlias(
wchar_t **alias,
- WinNetErrorSink errorSink,
- void *errorSinkContext
+ MullvadLogSink logSink,
+ void *logSinkContext
)
{
try
@@ -111,13 +117,9 @@ WinNet_GetTapInterfaceAlias( return true;
}
- catch (std::exception &err)
+ catch (const std::exception &err)
{
- if (nullptr != errorSink)
- {
- errorSink(err.what(), errorSinkContext);
- }
-
+ UnwindAndLog(logSink, logSinkContext, err);
return false;
}
catch (...)
@@ -151,8 +153,8 @@ WinNet_ActivateConnectivityMonitor( WinNetConnectivityMonitorCallback callback,
void *callbackContext,
bool *currentConnectivity,
- WinNetErrorSink errorSink,
- void *errorSinkContext
+ MullvadLogSink logSink,
+ void *logSinkContext
)
{
try
@@ -169,7 +171,9 @@ WinNet_ActivateConnectivityMonitor( bool connected = false;
- g_NetMonitor = new NetMonitor(forwarder, connected);
+ auto logger = std::make_shared<shared::LogSinkAdapter>(logSink, logSinkContext);
+
+ g_NetMonitor = new NetMonitor(logger, forwarder, connected);
if (nullptr != currentConnectivity)
{
@@ -178,13 +182,9 @@ WinNet_ActivateConnectivityMonitor( return true;
}
- catch (std::exception &err)
+ catch (const std::exception &err)
{
- if (nullptr != errorSink)
- {
- errorSink(err.what(), errorSinkContext);
- }
-
+ UnwindAndLog(logSink, logSinkContext, err);
return false;
}
catch (...)
@@ -215,25 +215,22 @@ WINNET_LINKAGE WINNET_CC_STATUS
WINNET_API
WinNet_CheckConnectivity(
- WinNetErrorSink errorSink,
- void *errorSinkContext
+ MullvadLogSink logSink,
+ void *logSinkContext
)
{
try
{
- return (NetMonitor::CheckConnectivity() ? WINNET_CC_STATUS::CONNECTED : WINNET_CC_STATUS::NOT_CONNECTED);
+ return (NetMonitor::CheckConnectivity(std::make_shared<shared::LogSinkAdapter>(logSink, logSinkContext))
+ ? WINNET_CC_STATUS_CONNECTED : WINNET_CC_STATUS_NOT_CONNECTED);
}
- catch (std::exception &err)
+ catch (const std::exception &err)
{
- if (nullptr != errorSink)
- {
- errorSink(err.what(), errorSinkContext);
- }
-
- return WINNET_CC_STATUS::CONNECTIVITY_UNKNOWN;
+ UnwindAndLog(logSink, logSinkContext, err);
+ return WINNET_CC_STATUS_CONNECTIVITY_UNKNOWN;
}
catch (...)
{
- return WINNET_CC_STATUS::CONNECTIVITY_UNKNOWN;
+ return WINNET_CC_STATUS_CONNECTIVITY_UNKNOWN;
}
}
diff --git a/windows/winnet/src/winnet/winnet.h b/windows/winnet/src/winnet/winnet.h index 40ed25f567..210c2ff349 100644 --- a/windows/winnet/src/winnet/winnet.h +++ b/windows/winnet/src/winnet/winnet.h @@ -1,5 +1,6 @@ #pragma once -#include <stdint.h> + +#include "../../shared/logsink.h" #include <stdbool.h> #ifdef WINNET_EXPORTS @@ -10,13 +11,11 @@ #define WINNET_API __stdcall -typedef void (WINNET_API *WinNetErrorSink)(const char *errorMessage, void *context); - -enum class WINNET_ETM_STATUS : uint32_t +enum WINNET_ETM_STATUS { - METRIC_NO_CHANGE = 0, - METRIC_SET = 1, - FAILURE = 2, + WINNET_ETM_STATUS_METRIC_NO_CHANGE = 0, + WINNET_ETM_STATUS_METRIC_SET = 1, + WINNET_ETM_STATUS_FAILURE = 2, }; extern "C" @@ -25,15 +24,15 @@ WINNET_ETM_STATUS WINNET_API WinNet_EnsureTopMetric( const wchar_t *deviceAlias, - WinNetErrorSink errorSink, - void *errorSinkContext + MullvadLogSink logSink, + void *logSinkContext ); -enum class WINNET_GTII_STATUS : uint32_t +enum WINNET_GTII_STATUS { - ENABLED = 0, - DISABLED = 1, - FAILURE = 2, + WINNET_GTII_STATUS_ENABLED = 0, + WINNET_GTII_STATUS_DISABLED = 1, + WINNET_GTII_STATUS_FAILURE = 2, }; extern "C" @@ -41,8 +40,8 @@ WINNET_LINKAGE WINNET_GTII_STATUS WINNET_API WinNet_GetTapInterfaceIpv6Status( - WinNetErrorSink errorSink, - void *errorSinkContext + MullvadLogSink logSink, + void *logSinkContext ); extern "C" @@ -51,8 +50,8 @@ bool WINNET_API WinNet_GetTapInterfaceAlias( wchar_t **alias, - WinNetErrorSink errorSink, - void *errorSinkContext + MullvadLogSink logSink, + void *logSinkContext ); // @@ -77,8 +76,8 @@ WinNet_ActivateConnectivityMonitor( WinNetConnectivityMonitorCallback callback, void *callbackContext, bool *currentConnectivity, - WinNetErrorSink errorSink, - void *errorSinkContext + MullvadLogSink logSink, + void *logSinkContext ); extern "C" @@ -88,11 +87,11 @@ WINNET_API WinNet_DeactivateConnectivityMonitor( ); -enum class WINNET_CC_STATUS : uint32_t +enum WINNET_CC_STATUS { - NOT_CONNECTED = 0, - CONNECTED = 1, - CONNECTIVITY_UNKNOWN = 2, + WINNET_CC_STATUS_NOT_CONNECTED = 0, + WINNET_CC_STATUS_CONNECTED = 1, + WINNET_CC_STATUS_CONNECTIVITY_UNKNOWN = 2, }; extern "C" @@ -100,6 +99,6 @@ WINNET_LINKAGE WINNET_CC_STATUS WINNET_API WinNet_CheckConnectivity( - WinNetErrorSink errorSink, - void *errorSinkContext + MullvadLogSink logSink, + void *logSinkContext ); |
