1 // Copyright 2012 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef NET_DNS_PUBLIC_WIN_DNS_SYSTEM_SETTINGS_H_ 6 #define NET_DNS_PUBLIC_WIN_DNS_SYSTEM_SETTINGS_H_ 7 8 #include <winsock2.h> 9 10 #include <iphlpapi.h> 11 #include <iptypes.h> 12 13 #include <memory> 14 #include <optional> 15 #include <string> 16 #include <vector> 17 18 #include "base/memory/free_deleter.h" 19 #include "base/strings/string_piece.h" 20 #include "base/types/expected.h" 21 #include "net/base/ip_endpoint.h" 22 #include "net/base/net_export.h" 23 24 namespace net { 25 26 // This is an aggregate representation of Windows system DNS configuration and 27 // can be easily built manually in tests. 28 struct NET_EXPORT WinDnsSystemSettings { 29 struct NET_EXPORT DevolutionSetting { 30 DevolutionSetting(); 31 DevolutionSetting(std::optional<DWORD> enabled, std::optional<DWORD> level); 32 DevolutionSetting(const DevolutionSetting&); 33 DevolutionSetting& operator=(const DevolutionSetting&); 34 ~DevolutionSetting(); 35 36 // UseDomainNameDevolution 37 std::optional<DWORD> enabled; 38 // DomainNameDevolutionLevel 39 std::optional<DWORD> level; 40 }; 41 42 // Returns true iff |address| is DNS address from IPv6 stateless discovery, 43 // i.e., matches fec0:0:0:ffff::{1,2,3}. 44 // http://tools.ietf.org/html/draft-ietf-ipngwg-dns-discovery 45 static bool IsStatelessDiscoveryAddress(const IPAddress& address); 46 47 WinDnsSystemSettings(); 48 ~WinDnsSystemSettings(); 49 50 WinDnsSystemSettings(WinDnsSystemSettings&&); 51 WinDnsSystemSettings& operator=(WinDnsSystemSettings&&); 52 53 // List of nameserver IP addresses. 54 std::unique_ptr<IP_ADAPTER_ADDRESSES, base::FreeDeleter> addresses; 55 56 // SOFTWARE\Policies\Microsoft\Windows NT\DNSClient\SearchList 57 std::optional<std::wstring> policy_search_list; 58 // SYSTEM\CurrentControlSet\Tcpip\Parameters\SearchList 59 std::optional<std::wstring> tcpip_search_list; 60 // SYSTEM\CurrentControlSet\Tcpip\Parameters\Domain 61 std::optional<std::wstring> tcpip_domain; 62 // SOFTWARE\Policies\Microsoft\System\DNSClient\PrimaryDnsSuffix 63 std::optional<std::wstring> primary_dns_suffix; 64 65 // SOFTWARE\Policies\Microsoft\Windows NT\DNSClient 66 DevolutionSetting policy_devolution; 67 // SYSTEM\CurrentControlSet\Dnscache\Parameters 68 DevolutionSetting dnscache_devolution; 69 // SYSTEM\CurrentControlSet\Tcpip\Parameters 70 DevolutionSetting tcpip_devolution; 71 72 // SOFTWARE\Policies\Microsoft\Windows NT\DNSClient\AppendToMultiLabelName 73 std::optional<DWORD> append_to_multi_label_name; 74 75 // True when the Name Resolution Policy Table (NRPT) has at least one rule: 76 // SOFTWARE\Policies\Microsoft\Windows NT\DNSClient\DnsPolicyConfig\Rule* 77 // (or) 78 // SYSTEM\CurrentControlSet\Services\Dnscache\Parameters\DnsPolicyConfig\Rule* 79 bool have_name_resolution_policy = false; 80 81 // True when a proxy is configured via at least one rule: 82 // SYSTEM\CurrentControlSet\Services\Dnscache\Parameters\DnsConnections 83 // (or) 84 // SYSTEM\CurrentControlSet\Services\Dnscache\Parameters\DnsActiveIfs 85 // (or) 86 // SYSTEM\CurrentControlSet\Services\Dnscache\Parameters\DnsConnectionsProxies 87 bool have_proxy = false; 88 89 // Gets Windows configured DNS servers from all network adapters, with the 90 // exception of stateless discovery addresses (see IsStatelessDiscoveryAddress 91 // above). 92 std::optional<std::vector<IPEndPoint>> GetAllNameservers(); 93 }; 94 95 // These values are persisted to logs. Entries should not be renumbered and 96 // numeric values should never be reused. 97 enum class ReadWinSystemDnsSettingsError { 98 kOk = 0, 99 kReadAdapterDnsAddressesFailed = 1, 100 kReadPolicySearchListFailed = 2, 101 kReadTcpipSearchListFailed = 3, 102 kReadTcpipDomainFailed = 4, 103 kReadPolicyDevolutionSettingFailed = 5, 104 kReadDnscacheDevolutionSettingFailed = 6, 105 kReadTcpipDevolutionSettingFailed = 7, 106 kReadPolicyAppendToMultiLabelNameFailed = 8, 107 kReadPrimaryDnsSuffixPathFailed = 9, 108 kGetNameServersFailed = 10, 109 kNoNameServerFound = 11, 110 kMaxValue = kNoNameServerFound 111 }; 112 // Reads WinDnsSystemSettings from IpHelper and the registry, or nullopt on 113 // errors reading them. 114 NET_EXPORT base::expected<WinDnsSystemSettings, ReadWinSystemDnsSettingsError> 115 ReadWinSystemDnsSettings(); 116 117 } // namespace net 118 119 #endif // NET_DNS_PUBLIC_WIN_DNS_SYSTEM_SETTINGS_H_ 120