diff options
| author | Odd Stranne <odd@mullvad.net> | 2018-10-05 23:13:41 +0200 |
|---|---|---|
| committer | Odd Stranne <odd@mullvad.net> | 2018-10-06 23:12:19 +0200 |
| commit | 2a791b5a050ae028359a5b796a6b6d4abe418d6f (patch) | |
| tree | 88e9a1c268c204fff7f8e96094c5b08da80c2755 | |
| parent | f4baf026b6aff919356dc9d9e8b3b00a0409ee03 (diff) | |
| download | mullvadvpn-2a791b5a050ae028359a5b796a6b6d4abe418d6f.tar.xz mullvadvpn-2a791b5a050ae028359a5b796a6b6d4abe418d6f.zip | |
Make code more resilient
| -rw-r--r-- | windows/windns/src/windns/dnsagent.cpp | 113 | ||||
| -rw-r--r-- | windows/windns/src/windns/dnsagent.h | 1 |
2 files changed, 54 insertions, 60 deletions
diff --git a/windows/windns/src/windns/dnsagent.cpp b/windows/windns/src/windns/dnsagent.cpp index 13fca9613a..e9b3a4246d 100644 --- a/windows/windns/src/windns/dnsagent.cpp +++ b/windows/windns/src/windns/dnsagent.cpp @@ -2,8 +2,10 @@ #include "dnsagent.h" #include "registrypaths.h" #include "netsh.h" +#include "confineoperation.h" #include <libcommon/trace/xtrace.h> #include <libcommon/error.h> +#include <libcommon/string.h> #include <process.h> #include <algorithm> @@ -214,15 +216,23 @@ void DnsAgent::processServerSourceEvent() return interfaceData.interfaceGuid; }); - const auto updatedSnaps = createSnaps(interfaces); const auto enforcedServers = m_nameServerSource->getNameServers(m_protocol); - for (const auto snap : updatedSnaps) + for (const auto &iface : interfaces) { - if (snap.needsOverriding(enforcedServers)) + const auto literalOperation = std::wstring(L"Verifying settings on interface ").append(iface); + + XTRACE(literalOperation); + + ConfineOperation(common::string::ToAnsi(literalOperation).c_str(), m_logSink, [&]() { - setNameServers(snap.interfaceGuid(), enforcedServers); - } + InterfaceSnap snap(m_protocol, iface); + + if (snap.needsOverriding(enforcedServers)) + { + setNameServers(iface, enforcedServers); + } + }); } } @@ -293,36 +303,32 @@ DnsAgent::ProcessingResult DnsAgent::processInterfaceEvent(const HANDLE *interfa continue; } - auto &interface = m_trackedInterfaces[i]; + auto &iface = m_trackedInterfaces[i]; + + const auto literalOperation = std::wstring(L"Processing event for interface ").append(iface.interfaceGuid); - XTRACE(L"Processing event for interface ", interface.interfaceGuid); + XTRACE(literalOperation); - try + ConfineOperation(common::string::ToAnsi(literalOperation).c_str(), m_logSink, [&]() { - InterfaceSnap updatedSnap(m_protocol, interface.interfaceGuid); + InterfaceSnap updatedSnap(m_protocol, iface.interfaceGuid); - if (updatedSnap.needsOverriding(enforcedNameServers)) + if ((iface.preservedSettings.internalInterface() && updatedSnap.internalInterface()) + || updatedSnap.nameServers() == enforcedNameServers) { - result = ProcessingResult::TrackingUpdated; - - interface.preservedSettings = std::move(updatedSnap); - setNameServers(interface.interfaceGuid, enforcedNameServers); + return; } - } - catch (std::exception &err) - { - const char *what = err.what(); - m_logSink->error("Could not fetch updated interface settings. Probably because the interface was removed.", &what, 1); + const auto shouldOverride = updatedSnap.needsOverriding(enforcedNameServers); - continue; - } - catch (...) - { - m_logSink->error("Could not fetch updated interface settings. Probably because the interface was removed."); + result = ProcessingResult::TrackingUpdated; + iface.preservedSettings = std::move(updatedSnap); - continue; - } + if (shouldOverride) + { + setNameServers(iface.interfaceGuid, enforcedNameServers); + } + }); } return result; @@ -345,20 +351,6 @@ std::vector<std::wstring> DnsAgent::discoverInterfaces() return interfaces; } -std::vector<InterfaceSnap> DnsAgent::createSnaps(const std::vector<std::wstring> &interfaces) -{ - std::vector<InterfaceSnap> snaps; - - snaps.reserve(interfaces.size()); - - for (const auto &interface : interfaces) - { - snaps.emplace_back(m_protocol, interface); - } - - return snaps; -} - void DnsAgent::setNameServers(const std::wstring &interfaceGuid, const std::vector<std::wstring> &enforcedServers) { XTRACE(L"Overriding name servers for interface ", interfaceGuid); @@ -391,33 +383,36 @@ void DnsAgent::setNameServers(const std::wstring &interfaceGuid, const std::vect void DnsAgent::startTrackingInterfaces(const std::vector<std::wstring> &interfaces) { - const auto snaps = createSnaps(interfaces); - - // - // Override configured name servers on all interfaces, as necessary. - // - const auto enforcedServers = m_nameServerSource->getNameServers(m_protocol); - for (const auto &snap : snaps) + for (const auto &iface : interfaces) { - if (snap.needsOverriding(enforcedServers)) + const auto literalOperation = std::wstring(L"Start tracking interface ").append(iface); + + XTRACE(literalOperation); + + ConfineOperation(common::string::ToAnsi(literalOperation).c_str(), m_logSink, [&]() { - setNameServers(snap.interfaceGuid(), enforcedServers); - } - } + InterfaceSnap snap(m_protocol, iface); - // - // Create a tracking record for each interface. - // + const auto shouldOverride = snap.needsOverriding(enforcedServers); - for (const auto &snap : snaps) - { - const auto interfaceGuid = snap.interfaceGuid(); + // + // Create a tracking record. + // + + m_trackedInterfaces.emplace_back(iface, std::move(snap), + std::make_unique<InterfaceMonitor>(m_protocol, iface)); - XTRACE(L"Creating tracking entry for interface ", interfaceGuid); + // + // Override configured name servers, as necessary. + // - m_trackedInterfaces.emplace_back(interfaceGuid, snap, std::make_unique<InterfaceMonitor>(m_protocol, interfaceGuid)); + if (shouldOverride) + { + setNameServers(iface, enforcedServers); + } + }); } } diff --git a/windows/windns/src/windns/dnsagent.h b/windows/windns/src/windns/dnsagent.h index 48dd12c52d..db73257330 100644 --- a/windows/windns/src/windns/dnsagent.h +++ b/windows/windns/src/windns/dnsagent.h @@ -73,7 +73,6 @@ private: ProcessingResult processInterfaceEvent(const HANDLE *interfaceEvents, size_t startIndex); std::vector<std::wstring> discoverInterfaces(); - std::vector<InterfaceSnap> createSnaps(const std::vector<std::wstring> &interfaces); void setNameServers(const std::wstring &interfaceGuid, const std::vector<std::wstring> &enforcedServers); |
