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