diff options
| -rw-r--r-- | windows/windns/src/windns/netconfigeventsink.cpp | 8 | ||||
| -rw-r--r-- | windows/windns/src/windns/netconfighelpers.cpp | 52 | ||||
| -rw-r--r-- | windows/windns/src/windns/netconfighelpers.h | 6 | ||||
| -rw-r--r-- | windows/windns/src/windns/windnscontext.cpp | 7 |
4 files changed, 22 insertions, 51 deletions
diff --git a/windows/windns/src/windns/netconfigeventsink.cpp b/windows/windns/src/windns/netconfigeventsink.cpp index 117775a18e..cd544af924 100644 --- a/windows/windns/src/windns/netconfigeventsink.cpp +++ b/windows/windns/src/windns/netconfigeventsink.cpp @@ -10,13 +10,16 @@ NetConfigEventSink::NetConfigEventSink(std::shared_ptr<wmi::IConnection> connect void NetConfigEventSink::update(CComPtr<IWbemClassObject> previous, CComPtr<IWbemClassObject> target) { + InterfaceConfig previousConfig(previous); + InterfaceConfig targetConfig(target); + ConfigManager::Mutex mutex(*m_configManager); // // This is OK because the config manager will reject updates // that set our DNS servers. // - if (ConfigManager::UpdateType::WinDnsEnforced == m_configManager->updateConfig(DnsConfig(previous), DnsConfig(target))) + if (ConfigManager::UpdateType::WinDnsEnforced == m_configManager->updateConfig(previousConfig, targetConfig)) { return; } @@ -25,6 +28,5 @@ void NetConfigEventSink::update(CComPtr<IWbemClassObject> previous, CComPtr<IWbe // The update was initiated from an external source. // Override current settings to enforce our selected DNS servers. // - auto servers = m_configManager->getServers(); - nchelpers::SetDnsServers(*m_connection, target, &servers); + nchelpers::SetDnsServers(targetConfig.interfaceIndex(), m_configManager->getServers()); } diff --git a/windows/windns/src/windns/netconfighelpers.cpp b/windows/windns/src/windns/netconfighelpers.cpp index a793ff2484..58677b30a1 100644 --- a/windows/windns/src/windns/netconfighelpers.cpp +++ b/windows/windns/src/windns/netconfighelpers.cpp @@ -1,10 +1,7 @@ #include "stdafx.h" #include "netconfighelpers.h" #include "comhelpers.h" -#include "wmi/methodcall.h" -#include <cstdint> -#include <sstream> -#include <stdexcept> +#include "netsh.h" namespace nchelpers { @@ -26,50 +23,19 @@ OptionalStringList GetDnsServers(CComPtr<IWbemClassObject> instance) return result; } -void SetDnsServers(wmi::IConnection &connection, CComPtr<IWbemClassObject> instance, - const std::vector<std::wstring> *servers) +uint32_t GetInterfaceIndex(CComPtr<IWbemClassObject> instance) { - wmi::MethodCall methodCall; - - if (nullptr == servers) - { - methodCall.addNullArgument(L"DNSServerSearchOrder", VT_ARRAY | VT_BSTR); - } - else - { - auto comServers = ComConvertIntoStringArray(*servers); - methodCall.addArgument(L"DNSServerSearchOrder", ComPackageStringArray(comServers)); - } - - auto status = methodCall.invoke(connection, instance, L"SetDNSServerSearchOrder"); - - const uint32_t STATUS_SUCCESS_NO_REBOOT_REQUIRED = 0; - - if (STATUS_SUCCESS_NO_REBOOT_REQUIRED == V_UI4(&status)) - { - return; - } - - std::string msg("Unable to update adapter configuration with new DNS servers"); - - try - { - auto configIndex = ComGetPropertyAlways(instance, L"Index"); - auto interfaceIndex = ComGetPropertyAlways(instance, L"InterfaceIndex"); - - std::stringstream ss; + return V_UI4(&ComGetPropertyAlways(instance, L"InterfaceIndex")); +} - ss << "Unable to update adapter with interfaceIndex = " << V_UI4(&interfaceIndex) \ - << ", configuration index = " << V_UI4(&configIndex) \ - << " with new DNS servers. Error: " << V_UI4(&status); +void SetDnsServers(uint32_t interfaceIndex, const std::vector<std::wstring> &servers) +{ + NetSh::SetIpv4PrimaryDns(interfaceIndex, servers[0]); - msg = ss.str(); - } - catch (...) + if (servers.size() > 1) { + NetSh::SetIpv4SecondaryDns(interfaceIndex, servers[1]); } - - throw std::runtime_error(msg); } } diff --git a/windows/windns/src/windns/netconfighelpers.h b/windows/windns/src/windns/netconfighelpers.h index 42602b1b9f..f1c796b445 100644 --- a/windows/windns/src/windns/netconfighelpers.h +++ b/windows/windns/src/windns/netconfighelpers.h @@ -4,6 +4,7 @@ #include <string> #include <memory> #include <vector> +#include <cstdint> #include <atlbase.h> #include <wbemidl.h> @@ -16,7 +17,8 @@ using OptionalStringList = std::shared_ptr<std::vector<std::wstring> >; OptionalStringList GetDnsServers(CComPtr<IWbemClassObject> instance); // instance = Win32_NetworkAdapterConfiguration -void SetDnsServers(wmi::IConnection &connection, CComPtr<IWbemClassObject> instance, - const std::vector<std::wstring> *servers); +uint32_t GetInterfaceIndex(CComPtr<IWbemClassObject> instance); + +void SetDnsServers(uint32_t interfaceIndex, const std::vector<std::wstring> &servers); } diff --git a/windows/windns/src/windns/windnscontext.cpp b/windows/windns/src/windns/windnscontext.cpp index aac564f6a6..5bcd986bf6 100644 --- a/windows/windns/src/windns/windnscontext.cpp +++ b/windows/windns/src/windns/windnscontext.cpp @@ -2,6 +2,7 @@ #include "windnscontext.h" #include "wmi/connection.h" #include "netconfigeventsink.h" +#include "netconfighelpers.h" #include "dnsreverter.h" WinDnsContext::WinDnsContext() @@ -41,7 +42,7 @@ bool WinDnsContext::set(const std::vector<std::wstring> &servers, const ClientSi while (resultSet.advance()) { - nchelpers::SetDnsServers(*m_connection, resultSet.result(), &servers); + nchelpers::SetDnsServers(nchelpers::GetInterfaceIndex(resultSet.result()), servers); } return true; @@ -63,9 +64,9 @@ bool WinDnsContext::reset() DnsReverter dnsReverter; - m_configManager->processConfigs([&](const DnsConfig &config) + m_configManager->processConfigs([&](const InterfaceConfig &config) { - dnsReverter.revert(*m_connection, config); + dnsReverter.revert(config); return true; }); |
