diff options
| -rw-r--r-- | talpid-core/src/winnet.rs | 20 | ||||
| -rw-r--r-- | windows/winnet/src/extras/loader/loader.cpp | 12 | ||||
| -rw-r--r-- | windows/winnet/src/winnet/networkadaptermonitor.cpp | 356 | ||||
| -rw-r--r-- | windows/winnet/src/winnet/networkadaptermonitor.h | 129 | ||||
| -rw-r--r-- | windows/winnet/src/winnet/offlinemonitor.cpp | 223 | ||||
| -rw-r--r-- | windows/winnet/src/winnet/offlinemonitor.h | 36 | ||||
| -rw-r--r-- | windows/winnet/src/winnet/winnet.cpp | 65 | ||||
| -rw-r--r-- | windows/winnet/src/winnet/winnet.def | 2 | ||||
| -rw-r--r-- | windows/winnet/src/winnet/winnet.h | 20 | ||||
| -rw-r--r-- | windows/winnet/src/winnet/winnet.vcxproj | 4 | ||||
| -rw-r--r-- | windows/winnet/src/winnet/winnet.vcxproj.filters | 4 |
11 files changed, 1 insertions, 870 deletions
diff --git a/talpid-core/src/winnet.rs b/talpid-core/src/winnet.rs index 641ffa636e..7c9489ecfb 100644 --- a/talpid-core/src/winnet.rs +++ b/talpid-core/src/winnet.rs @@ -1,5 +1,4 @@ use self::api::*; -pub use self::api::{WinNet_ActivateConnectivityMonitor, WinNet_DeactivateConnectivityMonitor}; use crate::{logging::windows::log_sink, routing::Node}; use ipnetwork::IpNetwork; use libc::c_void; @@ -48,10 +47,6 @@ pub enum Error { /// Failed to read IPv6 status on the TAP network interface. #[error(display = "Failed to read IPv6 status on the TAP network interface")] GetIpv6Status, - - /// Can't establish whether host is connected to a non-virtual network - #[error(display = "Network connectivity undecideable")] - ConnectivityUnkown, } fn logging_context() -> *const u8 { @@ -424,9 +419,7 @@ pub fn add_device_ip_addresses(iface: &String, addresses: &Vec<IpAddr>) -> bool mod api { use super::DefaultRouteChangedCallback; use crate::logging::windows::LogSink; - use libc::{c_void, wchar_t}; - - pub type ConnectivityCallback = unsafe extern "system" fn(is_connected: bool, ctx: *mut c_void); + use libc::wchar_t; #[allow(dead_code)] #[repr(u32)] @@ -495,14 +488,6 @@ mod api { sink_context: *const u8, ) -> WinNetStatus; - #[link_name = "WinNet_ActivateConnectivityMonitor"] - pub fn WinNet_ActivateConnectivityMonitor( - callback: Option<ConnectivityCallback>, - callbackContext: *mut libc::c_void, - sink: Option<LogSink>, - sink_context: *const u8, - ) -> bool; - #[link_name = "WinNet_RegisterDefaultRouteChangedCallback"] pub fn WinNet_RegisterDefaultRouteChangedCallback( callback: Option<DefaultRouteChangedCallback>, @@ -513,9 +498,6 @@ mod api { #[link_name = "WinNet_UnregisterDefaultRouteChangedCallback"] pub fn WinNet_UnregisterDefaultRouteChangedCallback(registrationHandle: *mut libc::c_void); - #[link_name = "WinNet_DeactivateConnectivityMonitor"] - pub fn WinNet_DeactivateConnectivityMonitor(); - #[link_name = "WinNet_AddDeviceIpAddresses"] pub fn WinNet_AddDeviceIpAddresses( interface_alias: *const wchar_t, diff --git a/windows/winnet/src/extras/loader/loader.cpp b/windows/winnet/src/extras/loader/loader.cpp index 00d795c032..acb5083b36 100644 --- a/windows/winnet/src/extras/loader/loader.cpp +++ b/windows/winnet/src/extras/loader/loader.cpp @@ -3,11 +3,6 @@ #include <libshared/logging/stdoutlogger.h> #include <iostream> -void __stdcall ConnectivityChanged(bool connected, void *) -{ - std::wcout << (0 != connected? L"Connected" : L"NOT connected") << std::endl; -} - int main() { //wchar_t *alias = nullptr; @@ -28,13 +23,6 @@ int main() // } //}; - const auto status = WinNet_ActivateConnectivityMonitor( - ConnectivityChanged, - nullptr, - shared::logging::StdoutLogger, - nullptr - ); - _getwch(); return 0; diff --git a/windows/winnet/src/winnet/networkadaptermonitor.cpp b/windows/winnet/src/winnet/networkadaptermonitor.cpp deleted file mode 100644 index 3cc21ece13..0000000000 --- a/windows/winnet/src/winnet/networkadaptermonitor.cpp +++ /dev/null @@ -1,356 +0,0 @@ -#include "stdafx.h" - -#include "networkadaptermonitor.h" -#include <libcommon/memory.h> -#include <sstream> -#include <cstring> - -using namespace std::placeholders; - - -NetworkAdapterMonitor::NetworkAdapterMonitor( - std::shared_ptr<common::logging::ILogSink> logSink, - UpdateSinkType updateSink, - FilterType filter, - std::shared_ptr<IDataProvider> dataProvider -) - : m_logSink(logSink) - , m_notificationHandle(nullptr) - , m_updateSink(updateSink) - , m_filter(filter) - , m_dataProvider(dataProvider) -{ - // - // Initialize adapters - // - - MIB_IF_TABLE2 *table; - - const auto status = m_dataProvider->getIfTable2(&table); - - if (NO_ERROR != status) - { - THROW_WINDOWS_ERROR(status, "Acquire network interface table"); - } - - common::memory::ScopeDestructor sd; - - sd += [this, table]() - { - m_dataProvider->freeMibTable(table); - }; - - for (ULONG i = 0; i < table->NumEntries; ++i) - { - m_adapters[table->Table[i].InterfaceLuid.Value] = table->Table[i]; - - if (filter(table->Table[i])) - { - m_filteredAdapters.push_back(table->Table[i]); - } - } - - // - // Send initial notification - // - - if (m_filteredAdapters.empty()) - { - m_updateSink(m_filteredAdapters, nullptr, UpdateType::Update); - } - else - { - m_updateSink(m_filteredAdapters, nullptr, UpdateType::Add); - } - - // - // Listen to adapter events - // - - const auto statusCb = m_dataProvider->notifyIpInterfaceChange( - AF_UNSPEC, - Callback, - this, - FALSE, - &m_notificationHandle - ); - - if (NO_ERROR != statusCb) - { - THROW_WINDOWS_ERROR(statusCb, "Register interface change notification"); - } -} - -NetworkAdapterMonitor::NetworkAdapterMonitor( - std::shared_ptr<common::logging::ILogSink> logSink - , UpdateSinkType updateSink - , FilterType filter -) : NetworkAdapterMonitor(logSink, updateSink, filter, std::make_shared<SystemDataProvider>()) -{ -} - -NetworkAdapterMonitor::~NetworkAdapterMonitor() -{ - if (nullptr != m_notificationHandle) - { - m_dataProvider->cancelMibChangeNotify2(m_notificationHandle); - m_notificationHandle = nullptr; - } -} - -bool NetworkAdapterMonitor::hasIPv4Interface(NET_LUID luid) const -{ - MIB_IPINTERFACE_ROW iprow = { 0 }; - iprow.InterfaceLuid = luid; - iprow.Family = AF_INET; - - const auto status = m_dataProvider->getIpInterfaceEntry(&iprow); - - if (NO_ERROR == status) - { - return true; - } - else if (ERROR_NOT_FOUND != status) - { - THROW_WINDOWS_ERROR(status, "Resolve IPv4 interface"); - } - - return false; -} - -bool NetworkAdapterMonitor::hasIPv6Interface(NET_LUID luid) const -{ - MIB_IPINTERFACE_ROW iprow = { 0 }; - iprow.InterfaceLuid = luid; - iprow.Family = AF_INET6; - - const auto status = m_dataProvider->getIpInterfaceEntry(&iprow); - - if (NO_ERROR == status) - { - return true; - } - else if (ERROR_NOT_FOUND != status) - { - THROW_WINDOWS_ERROR(status, "Resolve IPv6 interface"); - } - - return false; -} - -std::vector<MIB_IF_ROW2>::iterator NetworkAdapterMonitor::findFilteredAdapter(const NET_LUID adapter) -{ - return std::find_if(m_filteredAdapters.begin(), m_filteredAdapters.end(), [&adapter](const MIB_IF_ROW2 &elem) - { - return elem.InterfaceLuid.Value == adapter.Value; - }); -} - -std::optional<MIB_IF_ROW2> NetworkAdapterMonitor::getAdapter(NET_LUID luid) const -{ - MIB_IF_ROW2 rowOut = {0}; - rowOut.InterfaceLuid = luid; - const auto status = m_dataProvider->getIfEntry2(&rowOut); - - if (NO_ERROR == status) - { - return std::make_optional(rowOut); - } - if (ERROR_FILE_NOT_FOUND == status) - { - return std::nullopt; - } - - std::stringstream ss; - - ss << "GetIfEntry2() failed for LUID 0x" << std::hex << rowOut.InterfaceLuid.Value; - - THROW_WINDOWS_ERROR(status, ss.str().c_str()); -} - -void NetworkAdapterMonitor::callback(const MIB_IPINTERFACE_ROW *hint, MIB_NOTIFICATION_TYPE) -{ - const auto ifaceOpt = getAdapter(hint->InterfaceLuid); - - bool adapterEnabled = ifaceOpt.has_value() - && NET_IF_ADMIN_STATUS_UP == ifaceOpt->AdminStatus - && (hasIPv4Interface(ifaceOpt->InterfaceLuid) - || hasIPv6Interface(ifaceOpt->InterfaceLuid)); - - const auto adapterIt = m_adapters.find(hint->InterfaceLuid.Value); - - if (adapterEnabled) - { - const auto &iface = *ifaceOpt; - - // - // Check if the adapter has been added or updated - // - - bool fieldsChanged; - - if (m_adapters.end() == adapterIt) - { - const auto pair = m_adapters.emplace( - iface.InterfaceLuid.Value, - iface - ); - fieldsChanged = true; - } - else - { - // - // Only send an Update event if the fields have changed - // - fieldsChanged = std::memcmp( - &adapterIt->second, - &iface, - sizeof(MIB_IF_ROW2) - ) != 0; - - // update stored adapter - adapterIt->second = iface; - } - - if (m_filter(iface)) - { - // - // Report Add event if this is new - // - if (m_filteredAdapters.end() == findFilteredAdapter(iface.InterfaceLuid)) - { - m_filteredAdapters.push_back(iface); - m_updateSink(m_filteredAdapters, &iface, UpdateType::Add); - } - else if (fieldsChanged) - { - m_updateSink(m_filteredAdapters, &iface, UpdateType::Update); - } - } - else - { - // - // Synthesize a Delete event if we're no longer interested - // in this adapter - // - const auto filteredIt = findFilteredAdapter(iface.InterfaceLuid); - - if (m_filteredAdapters.end() != filteredIt) - { - m_filteredAdapters.erase(filteredIt); - m_updateSink( - m_filteredAdapters, - &iface, - UpdateType::Delete - ); - } - } - } - else - { - if (m_adapters.end() == adapterIt) - { - return; - } - - // - // Remove the adapter - // - - m_adapters.erase(adapterIt); - - const auto filteredIt = findFilteredAdapter(hint->InterfaceLuid); - - if (m_filteredAdapters.end() != filteredIt) - { - const auto &iface = ifaceOpt.value_or(*filteredIt); - - m_filteredAdapters.erase(filteredIt); - - // - // We report 'Delete' for any adapter that was - // approved by the filter when reported. - // - m_updateSink( - m_filteredAdapters, - &iface, - UpdateType::Delete - ); - } - } -} - -//static -void __stdcall NetworkAdapterMonitor::Callback(void *context, MIB_IPINTERFACE_ROW *hint, MIB_NOTIFICATION_TYPE updateType) -{ - auto inst = reinterpret_cast<NetworkAdapterMonitor *>(context); - - // - // Calls into this function are supposed to be serialized by Windows. - // That's not true on Windows 10 :-( - // - // This can be easily reproduced by changing the callback to never return, - // and observing more events being delivered. - // - - std::scoped_lock<std::mutex> lock(inst->m_callbackLock); - - try - { - inst->callback(hint, updateType); - } - catch (const std::exception &err) - { - inst->m_logSink->error(err.what()); - } - catch (...) - { - inst->m_logSink->error("Unspecified error in NetworkAdapterMonitor::Callback()"); - } -} - -// -// SystemDataProvider -// - -DWORD NetworkAdapterMonitor::SystemDataProvider::notifyIpInterfaceChange( - ADDRESS_FAMILY Family, - PIPINTERFACE_CHANGE_CALLBACK Callback, - PVOID CallerContext, - BOOLEAN InitialNotification, - HANDLE *NotificationHandle -) -{ - return NotifyIpInterfaceChange( - Family, - Callback, - CallerContext, - InitialNotification, - NotificationHandle - ); -} - -DWORD NetworkAdapterMonitor::SystemDataProvider::cancelMibChangeNotify2(HANDLE NotificationHandle) -{ - return CancelMibChangeNotify2(NotificationHandle); -} - -DWORD NetworkAdapterMonitor::SystemDataProvider::getIfEntry2(PMIB_IF_ROW2 Row) -{ - return GetIfEntry2(Row); -} - -DWORD NetworkAdapterMonitor::SystemDataProvider::getIfTable2(PMIB_IF_TABLE2 *Table) -{ - return GetIfTable2(Table); -} - -DWORD NetworkAdapterMonitor::SystemDataProvider::getIpInterfaceEntry(PMIB_IPINTERFACE_ROW Row) -{ - return GetIpInterfaceEntry(Row); -} - -void NetworkAdapterMonitor::SystemDataProvider::freeMibTable(PVOID Memory) -{ - FreeMibTable(Memory); -} diff --git a/windows/winnet/src/winnet/networkadaptermonitor.h b/windows/winnet/src/winnet/networkadaptermonitor.h deleted file mode 100644 index decf305b31..0000000000 --- a/windows/winnet/src/winnet/networkadaptermonitor.h +++ /dev/null @@ -1,129 +0,0 @@ -#pragma once - -#include <libcommon/logging/ilogsink.h> -#include <libcommon/error.h> -#include <map> -#include <winsock2.h> -#include <ws2ipdef.h> -#include <iphlpapi.h> -#include <windows.h> -#include <functional> -#include <vector> -#include <mutex> -#include <optional> - -class NetworkAdapterMonitor -{ -public: - - enum class UpdateType - { - Add, - Delete, - Update - }; - - using FilterType = std::function<bool(const MIB_IF_ROW2 &adapter)>; - - // - // An event may apply to a specific adapter, or it may apply to all adapters. - // In the latter case, 'adapter' will be set to nullptr. - // - using UpdateSinkType = std::function<void(const std::vector<MIB_IF_ROW2> &adapters, const MIB_IF_ROW2 *adapter, UpdateType updateType)>; - - struct IDataProvider; - class SystemDataProvider; - - NetworkAdapterMonitor( - std::shared_ptr<common::logging::ILogSink> logSink - , UpdateSinkType updateSink - , FilterType filter - , std::shared_ptr<IDataProvider> dataProvider - ); - NetworkAdapterMonitor( - std::shared_ptr<common::logging::ILogSink> logSink - , UpdateSinkType updateSink - , FilterType filter - ); - ~NetworkAdapterMonitor(); - - NetworkAdapterMonitor(const NetworkAdapterMonitor &) = delete; - NetworkAdapterMonitor& operator=(const NetworkAdapterMonitor &) = delete; - NetworkAdapterMonitor(NetworkAdapterMonitor &&) = delete; - NetworkAdapterMonitor& operator=(NetworkAdapterMonitor &&) = delete; - -private: - - std::shared_ptr<common::logging::ILogSink> m_logSink; - UpdateSinkType m_updateSink; - FilterType m_filter; - - std::shared_ptr<IDataProvider> m_dataProvider; - - std::mutex m_callbackLock; - - std::optional<MIB_IF_ROW2> getAdapter(NET_LUID luid) const; - - bool hasIPv4Interface(NET_LUID luid) const; - bool hasIPv6Interface(NET_LUID luid) const; - - std::map<ULONG64, MIB_IF_ROW2> m_adapters; - std::vector<MIB_IF_ROW2> m_filteredAdapters; - - std::vector<MIB_IF_ROW2>::iterator findFilteredAdapter(const NET_LUID adapter); - - HANDLE m_notificationHandle; - static void __stdcall Callback(void *context, MIB_IPINTERFACE_ROW *hint, MIB_NOTIFICATION_TYPE updateType); - virtual void callback(const MIB_IPINTERFACE_ROW *hint, MIB_NOTIFICATION_TYPE updateType); -}; - - -struct NetworkAdapterMonitor::IDataProvider -{ - virtual ~IDataProvider() = 0 - { - } - - virtual DWORD notifyIpInterfaceChange( - ADDRESS_FAMILY Family, - PIPINTERFACE_CHANGE_CALLBACK Callback, - PVOID CallerContext, - BOOLEAN InitialNotification, - HANDLE *NotificationHandle - ) = 0; - virtual DWORD cancelMibChangeNotify2(HANDLE NotificationHandle) = 0; - - virtual DWORD getIfTable2(PMIB_IF_TABLE2 *Table) = 0; - virtual void freeMibTable(PVOID Memory) = 0; - - virtual DWORD getIfEntry2(PMIB_IF_ROW2 Row) = 0; - virtual DWORD getIpInterfaceEntry(PMIB_IPINTERFACE_ROW Row) = 0; -}; - -class NetworkAdapterMonitor::SystemDataProvider : public IDataProvider -{ -public: - - SystemDataProvider() = default; - virtual ~SystemDataProvider() = default; - - SystemDataProvider(const SystemDataProvider&) = delete; - SystemDataProvider(SystemDataProvider&&) = delete; - SystemDataProvider& operator=(const SystemDataProvider&) = delete; - SystemDataProvider& operator=(const SystemDataProvider&&) = delete; - - DWORD notifyIpInterfaceChange( - ADDRESS_FAMILY Family, - PIPINTERFACE_CHANGE_CALLBACK Callback, - PVOID CallerContext, - BOOLEAN InitialNotification, - HANDLE *NotificationHandle - ) override; - DWORD cancelMibChangeNotify2(HANDLE NotificationHandle) override; - - DWORD getIfTable2(PMIB_IF_TABLE2 *Table) override; - void freeMibTable(PVOID Memory) override; - - DWORD getIfEntry2(PMIB_IF_ROW2 Row) override; - DWORD getIpInterfaceEntry(PMIB_IPINTERFACE_ROW Row) override; -}; diff --git a/windows/winnet/src/winnet/offlinemonitor.cpp b/windows/winnet/src/winnet/offlinemonitor.cpp deleted file mode 100644 index 67a90d92f0..0000000000 --- a/windows/winnet/src/winnet/offlinemonitor.cpp +++ /dev/null @@ -1,223 +0,0 @@ -#include "stdafx.h" -#include "offlinemonitor.h" -#include <libcommon/error.h> -#include <libcommon/memory.h> -#include <libcommon/string.h> -#include <sstream> - -using namespace std::placeholders; // for _1, _2 etc. - -namespace -{ - -bool IsConnectedAdapter(const MIB_IF_ROW2 &iface) -{ - switch (iface.InterfaceLuid.Info.IfType) - { - case IF_TYPE_SOFTWARE_LOOPBACK: - case IF_TYPE_TUNNEL: - { - return false; - } - } - - // - // (Windows 10, and possibly others.) - // - // The BT adapter is erronously not marked as representing hardware. - // By filtering on this we currently do not support BT tethering. - // - // Specifically, the following settings are problematic: - // - // InterfaceAndOperStatusFlags.HardwareInterface: 0 - // InterfaceAndOperStatusFlags.ConnectorPresent: 0 - // - - if (FALSE == iface.InterfaceAndOperStatusFlags.HardwareInterface - && FALSE == iface.InterfaceAndOperStatusFlags.ConnectorPresent) - { - return false; - } - - // - // Maybe checking iface.InterfaceAndOperStatusFlags.NotMediaConnected here - // would be a good thing? - // - - if (FALSE != iface.InterfaceAndOperStatusFlags.FilterInterface - || 0 == iface.PhysicalAddressLength - || FALSE != iface.InterfaceAndOperStatusFlags.EndPointInterface) - { - return false; - } - - return - ( - IfOperStatusUp == iface.OperStatus - && MediaConnectStateConnected == iface.MediaConnectState - ); -} - -void LogAdapter(std::shared_ptr<common::logging::ILogSink> logSink, const MIB_IF_ROW2 &iface) -{ - // - // Don't flood the log with garbage. - // - static 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", - L"WAN Miniport", - L"WiFi Filter Driver", - L"Microsoft Wi-Fi Direct Virtual Adapter", - }; - - for (const auto &black : blacklist) - { - if (nullptr != wcsstr(iface.Description, black.c_str())) - { - return; - } - } - - std::stringstream ss; - - ss << "Detailed interface logging" << std::endl; - - { - 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()); -} - -void LogAdapters(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. - // - - 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; - - sd += [table]() - { - FreeMibTable(table); - }; - - logSink->info("Begin detailed listing of network interfaces"); - - for (ULONG i = 0; i < table->NumEntries; ++i) - { - LogAdapter(logSink, table->Table[i]); - } - - logSink->info("End detailed listing of network interfaces"); -} - -} // anonymous namespace - -OfflineMonitor::OfflineMonitor -( - std::shared_ptr<common::logging::ILogSink> logSink, - Notifier notifier, - std::shared_ptr<NetworkAdapterMonitor::IDataProvider> dataProvider -) - : m_logSink(logSink) - , m_notifier(notifier) - , m_netAdapterMonitor( - m_logSink, - std::bind(&OfflineMonitor::callback, this, _1, _2, _3), - IsConnectedAdapter, - dataProvider - ) -{ -} - -OfflineMonitor::OfflineMonitor -( - std::shared_ptr<common::logging::ILogSink> logSink, - Notifier notifier -) : OfflineMonitor(logSink, notifier, std::make_shared<NetworkAdapterMonitor::SystemDataProvider>()) -{ -} - -void OfflineMonitor::callback(const std::vector<MIB_IF_ROW2> &adapters, const MIB_IF_ROW2 *, NetworkAdapterMonitor::UpdateType) -{ - const auto previousConnectivity = m_connected; - m_connected = !adapters.empty(); - - if (previousConnectivity != m_connected) - { - std::stringstream ss; - - if (previousConnectivity.has_value()) - { - ss << "Connectivity changed. Machine is: " << (*m_connected ? "ONLINE" : "OFFLINE"); - m_logSink->info(ss.str().c_str()); - } - else - { - ss << "Initial connectivity established. Machine is: " << (*m_connected ? "ONLINE" : "OFFLINE"); - m_logSink->info(ss.str().c_str()); - } - - if (false == *m_connected) - { - LogAdapters(m_logSink); - } - - m_notifier(*m_connected); - } -} diff --git a/windows/winnet/src/winnet/offlinemonitor.h b/windows/winnet/src/winnet/offlinemonitor.h deleted file mode 100644 index 6219874687..0000000000 --- a/windows/winnet/src/winnet/offlinemonitor.h +++ /dev/null @@ -1,36 +0,0 @@ -#pragma once - -#include <libcommon/logging/ilogsink.h> -#include <mutex> -#include <optional> -#include "networkadaptermonitor.h" - -class OfflineMonitor -{ -public: - - // - // Connectivity changed. - // true = connected, false = disconnected. - // - using Notifier = std::function<void(bool)>; - - OfflineMonitor( - std::shared_ptr<common::logging::ILogSink> logSink, - Notifier notifier, - std::shared_ptr<NetworkAdapterMonitor::IDataProvider> dataProvider - ); - - OfflineMonitor(std::shared_ptr<common::logging::ILogSink> logSink, Notifier notifier); - -private: - - std::shared_ptr<common::logging::ILogSink> m_logSink; - Notifier m_notifier; - - std::optional<bool> m_connected; - - NetworkAdapterMonitor m_netAdapterMonitor; - - void callback(const std::vector<MIB_IF_ROW2> &adapters, const MIB_IF_ROW2 *adapter, NetworkAdapterMonitor::UpdateType type); -}; diff --git a/windows/winnet/src/winnet/winnet.cpp b/windows/winnet/src/winnet/winnet.cpp index d7b34c1bc0..c1b864612e 100644 --- a/windows/winnet/src/winnet/winnet.cpp +++ b/windows/winnet/src/winnet/winnet.cpp @@ -1,7 +1,6 @@ #include "stdafx.h"
#include "winnet.h"
#include "NetworkInterfaces.h"
-#include "offlinemonitor.h"
#include "routing/routemanager.h"
#include "converters.h"
#include <libshared/logging/logsinkadapter.h>
@@ -24,8 +23,6 @@ using namespace shared::network; namespace
{
-OfflineMonitor *g_OfflineMonitor = nullptr;
-
std::mutex g_RouteManagerLock;
RouteManager *g_RouteManager = nullptr;
std::shared_ptr<shared::logging::LogSinkAdapter> g_RouteManagerLogSink;
@@ -186,68 +183,6 @@ extern "C" WINNET_LINKAGE
bool
WINNET_API
-WinNet_ActivateConnectivityMonitor(
- WinNetConnectivityMonitorCallback callback,
- void *callbackContext,
- MullvadLogSink logSink,
- void *logSinkContext
-)
-{
- try
- {
- if (nullptr != g_OfflineMonitor)
- {
- THROW_ERROR("Cannot activate connectivity monitor twice");
- }
-
- if (nullptr == callback)
- {
- THROW_ERROR("Invalid argument: callback");
- }
-
- auto forwarder = [callback, callbackContext](bool connected)
- {
- callback(connected, callbackContext);
- };
-
- auto logger = std::make_shared<shared::logging::LogSinkAdapter>(logSink, logSinkContext);
-
- g_OfflineMonitor = new OfflineMonitor(logger, forwarder);
-
- return true;
- }
- catch (const std::exception &err)
- {
- shared::logging::UnwindAndLog(logSink, logSinkContext, err);
- return false;
- }
- catch (...)
- {
- return false;
- }
-}
-
-extern "C"
-WINNET_LINKAGE
-void
-WINNET_API
-WinNet_DeactivateConnectivityMonitor(
-)
-{
- try
- {
- delete g_OfflineMonitor;
- g_OfflineMonitor = nullptr;
- }
- catch (...)
- {
- }
-}
-
-extern "C"
-WINNET_LINKAGE
-bool
-WINNET_API
WinNet_ActivateRouteManager(
MullvadLogSink logSink,
void *logSinkContext
diff --git a/windows/winnet/src/winnet/winnet.def b/windows/winnet/src/winnet/winnet.def index 4b840ee818..bafbd58aeb 100644 --- a/windows/winnet/src/winnet/winnet.def +++ b/windows/winnet/src/winnet/winnet.def @@ -1,8 +1,6 @@ LIBRARY winnet EXPORTS WinNet_EnsureBestMetric - WinNet_ActivateConnectivityMonitor - WinNet_DeactivateConnectivityMonitor WinNet_ActivateRouteManager WinNet_DeactivateRouteManager WinNet_AddDeviceIpAddresses diff --git a/windows/winnet/src/winnet/winnet.h b/windows/winnet/src/winnet/winnet.h index 8cd1703f79..3f2f80a5e5 100644 --- a/windows/winnet/src/winnet/winnet.h +++ b/windows/winnet/src/winnet/winnet.h @@ -33,26 +33,6 @@ WinNet_EnsureBestMetric( void *logSinkContext ); -typedef void (WINNET_API *WinNetConnectivityMonitorCallback)(bool connected, void *context); - -extern "C" -WINNET_LINKAGE -bool -WINNET_API -WinNet_ActivateConnectivityMonitor( - WinNetConnectivityMonitorCallback callback, - void *callbackContext, - MullvadLogSink logSink, - void *logSinkContext -); - -extern "C" -WINNET_LINKAGE -void -WINNET_API -WinNet_DeactivateConnectivityMonitor( -); - enum WINNET_ADDR_FAMILY { WINNET_ADDR_FAMILY_IPV4 = 0, diff --git a/windows/winnet/src/winnet/winnet.vcxproj b/windows/winnet/src/winnet/winnet.vcxproj index 7b4578d4b1..b6d5ce1d5b 100644 --- a/windows/winnet/src/winnet/winnet.vcxproj +++ b/windows/winnet/src/winnet/winnet.vcxproj @@ -28,10 +28,8 @@ </ItemGroup> <ItemGroup> <ClCompile Include="converters.cpp" /> - <ClCompile Include="networkadaptermonitor.cpp" /> <ClCompile Include="dllmain.cpp" /> <ClCompile Include="InterfacePair.cpp" /> - <ClCompile Include="offlinemonitor.cpp" /> <ClCompile Include="NetworkInterfaces.cpp" /> <ClCompile Include="routing\defaultroutemonitor.cpp" /> <ClCompile Include="routing\helpers.cpp" /> @@ -42,9 +40,7 @@ </ItemGroup> <ItemGroup> <ClInclude Include="converters.h" /> - <ClInclude Include="networkadaptermonitor.h" /> <ClInclude Include="InterfacePair.h" /> - <ClInclude Include="offlinemonitor.h" /> <ClInclude Include="NetworkInterfaces.h" /> <ClInclude Include="routing\defaultroutemonitor.h" /> <ClInclude Include="routing\helpers.h" /> diff --git a/windows/winnet/src/winnet/winnet.vcxproj.filters b/windows/winnet/src/winnet/winnet.vcxproj.filters index 2d5a039c6b..3576579961 100644 --- a/windows/winnet/src/winnet/winnet.vcxproj.filters +++ b/windows/winnet/src/winnet/winnet.vcxproj.filters @@ -6,8 +6,6 @@ <ClCompile Include="winnet.cpp" /> <ClCompile Include="NetworkInterfaces.cpp" /> <ClCompile Include="InterfacePair.cpp" /> - <ClCompile Include="networkadaptermonitor.cpp" /> - <ClCompile Include="offlinemonitor.cpp" /> <ClCompile Include="routing\types.cpp"> <Filter>routing</Filter> </ClCompile> @@ -28,8 +26,6 @@ <ClInclude Include="winnet.h" /> <ClInclude Include="NetworkInterfaces.h" /> <ClInclude Include="InterfacePair.h" /> - <ClInclude Include="networkadaptermonitor.h" /> - <ClInclude Include="offlinemonitor.h" /> <ClInclude Include="routing\types.h"> <Filter>routing</Filter> </ClInclude> |
