diff options
| author | Odd Stranne <odd@mullvad.net> | 2020-04-15 17:51:25 +0200 |
|---|---|---|
| committer | Odd Stranne <odd@mullvad.net> | 2020-04-16 15:31:06 +0200 |
| commit | 926c13becd8a7d2075f8228be3515a91cb19428b (patch) | |
| tree | 35778cf94bd66515483fb3b61db2c8bf3ecc42aa | |
| parent | 6a93497fc6dd434d9dfd10111017824f3b62621e (diff) | |
| download | mullvadvpn-926c13becd8a7d2075f8228be3515a91cb19428b.tar.xz mullvadvpn-926c13becd8a7d2075f8228be3515a91cb19428b.zip | |
Move logging code
| -rw-r--r-- | windows/winnet/src/winnet/offlinemonitor.cpp | 218 | ||||
| -rw-r--r-- | windows/winnet/src/winnet/offlinemonitor.h | 2 |
2 files changed, 106 insertions, 114 deletions
diff --git a/windows/winnet/src/winnet/offlinemonitor.cpp b/windows/winnet/src/winnet/offlinemonitor.cpp index 6da73aa723..5ab4ee9e1e 100644 --- a/windows/winnet/src/winnet/offlinemonitor.cpp +++ b/windows/winnet/src/winnet/offlinemonitor.cpp @@ -58,56 +58,79 @@ bool IsConnectedAdapter(const MIB_IF_ROW2 &iface) ); } -} // 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_connected(false) - , m_netAdapterMonitor( - m_logSink, - std::bind(&OfflineMonitor::callback, this, _1, _2, _3), - IsConnectedAdapter, - dataProvider - ) +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", + }; -OfflineMonitor::OfflineMonitor -( - std::shared_ptr<common::logging::ILogSink> logSink, - Notifier notifier -) : OfflineMonitor(logSink, notifier, std::make_shared<NetworkAdapterMonitor::SystemDataProvider>()) -{ -} + for (const auto &black : blacklist) + { + if (nullptr != wcsstr(iface.Description, black.c_str())) + { + return; + } + } -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(); + std::stringstream ss; + + ss << "Detailed interface logging" << std::endl; - if (previousConnectivity != m_connected) { - std::stringstream ss; + const auto s = std::wstring(L" Alias: ").append(iface.Alias); + ss << common::string::ToAnsi(s) << std::endl; + } - ss << "Connectivity changed. Machine is: " << (m_connected ? "ONLINE" : "OFFLINE"); - m_logSink->info(ss.str().c_str()); + { + const auto s = std::wstring(L" Description: ").append(iface.Description); + ss << common::string::ToAnsi(s) << std::endl; + } - if (false == m_connected) - { - LogOfflineState(); - } + 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; - m_notifier(m_connected); - } + // + // 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 OfflineMonitor::LogOfflineState() +void LogAdapters(std::shared_ptr<common::logging::ILogSink> logSink) { // // There is a race condition here because logging is not done using the @@ -122,7 +145,7 @@ void OfflineMonitor::LogOfflineState() if (NO_ERROR != status) { - m_logSink->error("Failed to acquire list of network interfaces. Aborting detailed logging"); + logSink->error("Failed to acquire list of network interfaces. Aborting detailed logging"); return; } @@ -133,90 +156,61 @@ void OfflineMonitor::LogOfflineState() FreeMibTable(table); }; - m_logSink->info("Begin detailed listing of network interfaces"); + logSink->info("Begin detailed listing of network interfaces"); for (ULONG i = 0; i < table->NumEntries; ++i) { - const auto &iface = table->Table[i]; + LogAdapter(logSink, table->Table[i]); + } - // - // 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", - L"WAN Miniport", - L"WiFi Filter Driver", - L"Microsoft Wi-Fi Direct Virtual Adapter", - }; + logSink->info("End detailed listing of network interfaces"); +} - bool blacklisted = false; +} // anonymous namespace - for (const auto &black : blacklist) - { - if (nullptr != wcsstr(iface.Description, black.c_str())) - { - blacklisted = true; - break; - } - } +OfflineMonitor::OfflineMonitor +( + std::shared_ptr<common::logging::ILogSink> logSink, + Notifier notifier, + std::shared_ptr<NetworkAdapterMonitor::IDataProvider> dataProvider +) + : m_logSink(logSink) + , m_notifier(notifier) + , m_connected(false) + , m_netAdapterMonitor( + m_logSink, + std::bind(&OfflineMonitor::callback, this, _1, _2, _3), + IsConnectedAdapter, + dataProvider + ) +{ +} - if (blacklisted) - { - continue; - } +OfflineMonitor::OfflineMonitor +( + std::shared_ptr<common::logging::ILogSink> logSink, + Notifier notifier +) : OfflineMonitor(logSink, notifier, std::make_shared<NetworkAdapterMonitor::SystemDataProvider>()) +{ +} - std::stringstream ss; +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(); - ss << "Detailed interface logging" << std::endl; - ss << "Interface ordinal " << i << std::endl; + if (previousConnectivity != m_connected) + { + std::stringstream ss; - { - const auto s = std::wstring(L" Alias: ").append(iface.Alias); - ss << common::string::ToAnsi(s) << std::endl; - } + ss << "Connectivity changed. Machine is: " << (m_connected ? "ONLINE" : "OFFLINE"); + m_logSink->info(ss.str().c_str()); + if (false == m_connected) { - const auto s = std::wstring(L" Description: ").append(iface.Description); - ss << common::string::ToAnsi(s) << std::endl; + LogAdapters(m_logSink); } - 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; - - m_logSink->info(ss.str().c_str()); + m_notifier(m_connected); } - - m_logSink->info("End detailed listing of network interfaces"); } diff --git a/windows/winnet/src/winnet/offlinemonitor.h b/windows/winnet/src/winnet/offlinemonitor.h index 230fef48d7..e4d18fe77e 100644 --- a/windows/winnet/src/winnet/offlinemonitor.h +++ b/windows/winnet/src/winnet/offlinemonitor.h @@ -29,7 +29,5 @@ private: bool m_connected; NetworkAdapterMonitor m_netAdapterMonitor; - void LogOfflineState(); - void callback(const std::vector<MIB_IF_ROW2> &adapters, const MIB_IF_ROW2 *adapter, NetworkAdapterMonitor::UpdateType type); }; |
