| 1 | // Copyright (C) Microsoft Corporation. All rights reserved. |
| 2 | |
| 3 | #pragma once |
| 4 | #include <string> |
| 5 | #include <vector> |
| 6 | |
| 7 | #include <iptypes.h> |
| 8 | #include <wil/registry.h> |
| 9 | |
| 10 | #include "WslCoreNetworkingSupport.h" |
| 11 | #include "RegistryWatcher.h" |
| 12 | |
| 13 | namespace wsl::core::networking { |
| 14 | struct DnsInfo |
| 15 | { |
| 16 | std::vector<std::string> Servers; |
| 17 | std::vector<std::string> Domains; |
| 18 | }; |
| 19 | |
| 20 | enum class DnsSettingsFlags |
| 21 | { |
| 22 | None = 0x0, |
| 23 | IncludeVpn = 0x1, |
| 24 | IncludeIpv6Servers = 0x2, |
| 25 | IncludeAllSuffixes = 0x4 |
| 26 | }; |
| 27 | DEFINE_ENUM_FLAG_OPERATORS(DnsSettingsFlags); |
| 28 | |
| 29 | inline bool operator==(const DnsInfo& lhs, const DnsInfo& rhs) noexcept |
| 30 | { |
| 31 | return lhs.Servers == rhs.Servers && lhs.Domains == rhs.Domains; |
| 32 | } |
| 33 | inline bool operator!=(const DnsInfo& lhs, const DnsInfo& rhs) noexcept |
| 34 | { |
| 35 | return !(lhs == rhs); |
| 36 | } |
| 37 | |
| 38 | std::string GenerateResolvConf(_In_ const DnsInfo& Info); |
| 39 | |
| 40 | /// <summary> |
| 41 | /// Builds an hns::DNS notification from DnsInfo settings. |
| 42 | /// </summary> |
| 43 | /// <param name="settings">The DNS settings to convert</param> |
| 44 | /// <param name="options">The resolv.conf header options (defaults to LX_INIT_RESOLVCONF_FULL_HEADER)</param> |
| 45 | /// <returns>The hns::DNS notification ready to send via GNS channel</returns> |
| 46 | wsl::shared::hns::DNS BuildDnsNotification(const DnsInfo& settings, PCWSTR options = LX_INIT_RESOLVCONF_FULL_HEADER); |
| 47 | |
| 48 | std::vector<std::string> GetAllDnsSuffixes(const std::vector<IpAdapterAddress>& AdapterAddresses); |
| 49 | |
| 50 | DWORD GetBestInterface(); |
| 51 | |
| 52 | class HostDnsInfo |
| 53 | { |
| 54 | public: |
| 55 | static DnsInfo GetDnsSettings(_In_ DnsSettingsFlags Flags); |
| 56 | |
| 57 | static DnsInfo GetDnsTunnelingSettings(const std::wstring& dnsTunnelingNameserver); |
| 58 | |
| 59 | private: |
| 60 | /// <summary> |
| 61 | /// Internal function to retrieve interface DNS servers. |
| 62 | /// </summary> |
| 63 | static std::vector<std::string> GetInterfaceDnsServers(const std::vector<IpAdapterAddress>& AdapterAddresses, _In_ DnsSettingsFlags Flags); |
| 64 | |
| 65 | /// <summary> |
| 66 | /// Internal function to retrieve all Windows DNS suffixes. |
| 67 | /// </summary> |
| 68 | static std::vector<std::string> GetInterfaceDnsSuffixes(const std::vector<IpAdapterAddress>& AdapterAddresses); |
| 69 | |
| 70 | /// <summary> |
| 71 | /// Internal function to convert DNS server addresses into strings. |
| 72 | /// </summary> |
| 73 | static std::vector<std::string> GetDnsServerStrings(_In_ const PIP_ADAPTER_DNS_SERVER_ADDRESS& DnsServer, _In_ USHORT IpFamilyFilter, _In_ USHORT MaxValues); |
| 74 | }; |
| 75 | |
| 76 | using RegistryChangeCallback = std::function<void()>; |
| 77 | |
| 78 | /// <summary> |
| 79 | /// Class used to get notifications when Windows DNS suffixes are updated in registry. |
| 80 | /// </summary> |
| 81 | class DnsSuffixRegistryWatcher |
| 82 | { |
| 83 | public: |
| 84 | DnsSuffixRegistryWatcher(RegistryChangeCallback&& reportRegistryChange); |
| 85 | ~DnsSuffixRegistryWatcher() noexcept = default; |
| 86 | |
| 87 | private: |
| 88 | RegistryChangeCallback m_reportRegistryChange; |
| 89 | |
| 90 | std::vector<wistd::unique_ptr<wsl::windows::common::slim_registry_watcher>> m_registryWatchers; |
| 91 | }; |
| 92 | |
| 93 | } // namespace wsl::core::networking |