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