dns_config_service_win.h 3.2 KB

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