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_DNS_CONFIG_SERVICE_WIN_H_ 6 #define NET_DNS_DNS_CONFIG_SERVICE_WIN_H_ 7 8 // The sole purpose of dns_config_service_win.h is for unittests so we just 9 // include these headers here. 10 #include <winsock2.h> 11 #include <iphlpapi.h> 12 #include <iptypes.h> 13 14 #include <memory> 15 #include <string> 16 #include <string_view> 17 #include <vector> 18 19 #include "base/memory/free_deleter.h" 20 #include "net/base/net_export.h" 21 #include "net/dns/dns_config_service.h" 22 #include "net/dns/public/win_dns_system_settings.h" 23 24 // The general effort of DnsConfigServiceWin is to configure |nameservers| and 25 // |search| in DnsConfig. The settings are stored in the Windows registry, but 26 // to simplify the task we use the IP Helper API wherever possible. That API 27 // yields the complete and ordered |nameservers|, but to determine |search| we 28 // need to use the registry. On Windows 7, WMI does return the correct |search| 29 // but on earlier versions it is insufficient. 30 // 31 // Experimental evaluation of Windows behavior suggests that domain parsing is 32 // naive. Domain suffixes in |search| are not validated until they are appended 33 // to the resolved name. We attempt to replicate this behavior. 34 35 namespace net { 36 37 namespace internal { 38 39 // Converts a UTF-16 domain name to ASCII, possibly using punycode. 40 // Returns empty string on failure. 41 std::string NET_EXPORT_PRIVATE ParseDomainASCII(std::wstring_view widestr); 42 43 // Parses |value| as search list (comma-delimited list of domain names) from 44 // a registry key and stores it in |out|. Returns empty vector on failure. Empty 45 // entries (e.g., "chromium.org,,org") terminate the list. Non-ascii hostnames 46 // are converted to punycode. 47 std::vector<std::string> NET_EXPORT_PRIVATE 48 ParseSearchList(std::wstring_view value); 49 50 // Fills in |dns_config| from |settings|. Exposed for tests. Returns nullopt if 51 // a valid config could not be determined. 52 absl::optional<DnsConfig> NET_EXPORT_PRIVATE 53 ConvertSettingsToDnsConfig(const WinDnsSystemSettings& settings); 54 55 // Service for reading and watching Windows system DNS settings. This object is 56 // not thread-safe and methods may perform blocking I/O so methods must be 57 // called on a sequence that allows blocking (i.e. base::MayBlock). It may be 58 // constructed on a different sequence than which it's later called on. 59 // WatchConfig() must be called prior to ReadConfig(). 60 // Use DnsConfigService::CreateSystemService to use it outside of tests. 61 class NET_EXPORT_PRIVATE DnsConfigServiceWin : public DnsConfigService { 62 public: 63 DnsConfigServiceWin(); 64 65 DnsConfigServiceWin(const DnsConfigServiceWin&) = delete; 66 DnsConfigServiceWin& operator=(const DnsConfigServiceWin&) = delete; 67 68 ~DnsConfigServiceWin() override; 69 70 private: 71 class Watcher; 72 class ConfigReader; 73 class HostsReader; 74 75 // DnsConfigService: 76 void ReadConfigNow() override; 77 void ReadHostsNow() override; 78 bool StartWatching() override; 79 80 std::unique_ptr<Watcher> watcher_; 81 std::unique_ptr<ConfigReader> config_reader_; 82 std::unique_ptr<HostsReader> hosts_reader_; 83 }; 84 85 } // namespace internal 86 87 } // namespace net 88 89 #endif // NET_DNS_DNS_CONFIG_SERVICE_WIN_H_ 90