Home | History | Annotate | Download | only in dns
      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 
      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 
     13 #include <string>
     14 #include <vector>
     15 
     16 #include "base/memory/ref_counted.h"
     17 #include "base/memory/scoped_ptr.h"
     18 #include "base/strings/string16.h"
     19 #include "net/base/net_export.h"
     20 #include "net/dns/dns_config_service.h"
     21 
     22 // The general effort of DnsConfigServiceWin is to configure |nameservers| and
     23 // |search| in DnsConfig. The settings are stored in the Windows registry, but
     24 // to simplify the task we use the IP Helper API wherever possible. That API
     25 // yields the complete and ordered |nameservers|, but to determine |search| we
     26 // need to use the registry. On Windows 7, WMI does return the correct |search|
     27 // but on earlier versions it is insufficient.
     28 //
     29 // Experimental evaluation of Windows behavior suggests that domain parsing is
     30 // naive. Domain suffixes in |search| are not validated until they are appended
     31 // to the resolved name. We attempt to replicate this behavior.
     32 
     33 namespace net {
     34 
     35 namespace internal {
     36 
     37 // Registry key paths.
     38 const wchar_t* const kTcpipPath =
     39     L"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters";
     40 const wchar_t* const kTcpip6Path =
     41     L"SYSTEM\\CurrentControlSet\\Services\\Tcpip6\\Parameters";
     42 const wchar_t* const kDnscachePath =
     43     L"SYSTEM\\CurrentControlSet\\Services\\Dnscache\\Parameters";
     44 const wchar_t* const kPolicyPath =
     45     L"SOFTWARE\\Policies\\Microsoft\\Windows NT\\DNSClient";
     46 
     47 // Returns the path to the HOSTS file.
     48 base::FilePath GetHostsPath();
     49 
     50 // Parses |value| as search list (comma-delimited list of domain names) from
     51 // a registry key and stores it in |out|. Returns true on success. Empty
     52 // entries (e.g., "chromium.org,,org") terminate the list. Non-ascii hostnames
     53 // are converted to punycode.
     54 bool NET_EXPORT_PRIVATE ParseSearchList(const base::string16& value,
     55                                         std::vector<std::string>* out);
     56 
     57 // All relevant settings read from registry and IP Helper. This isolates our
     58 // logic from system calls and is exposed for unit tests. Keep it an aggregate
     59 // struct for easy initialization.
     60 struct NET_EXPORT_PRIVATE DnsSystemSettings {
     61   // The |set| flag distinguishes between empty and unset values.
     62   struct RegString {
     63     bool set;
     64     base::string16 value;
     65   };
     66 
     67   struct RegDword {
     68     bool set;
     69     DWORD value;
     70   };
     71 
     72   struct DevolutionSetting {
     73     // UseDomainNameDevolution
     74     RegDword enabled;
     75     // DomainNameDevolutionLevel
     76     RegDword level;
     77   };
     78 
     79   // Filled in by GetAdapterAddresses. Note that the alternative
     80   // GetNetworkParams does not include IPv6 addresses.
     81   scoped_ptr_malloc<IP_ADAPTER_ADDRESSES> addresses;
     82 
     83   // SOFTWARE\Policies\Microsoft\Windows NT\DNSClient\SearchList
     84   RegString policy_search_list;
     85   // SYSTEM\CurrentControlSet\Tcpip\Parameters\SearchList
     86   RegString tcpip_search_list;
     87   // SYSTEM\CurrentControlSet\Tcpip\Parameters\Domain
     88   RegString tcpip_domain;
     89   // SOFTWARE\Policies\Microsoft\System\DNSClient\PrimaryDnsSuffix
     90   RegString primary_dns_suffix;
     91 
     92   // SOFTWARE\Policies\Microsoft\Windows NT\DNSClient
     93   DevolutionSetting policy_devolution;
     94   // SYSTEM\CurrentControlSet\Dnscache\Parameters
     95   DevolutionSetting dnscache_devolution;
     96   // SYSTEM\CurrentControlSet\Tcpip\Parameters
     97   DevolutionSetting tcpip_devolution;
     98 
     99   // SOFTWARE\Policies\Microsoft\Windows NT\DNSClient\AppendToMultiLabelName
    100   RegDword append_to_multi_label_name;
    101 };
    102 
    103 enum ConfigParseWinResult {
    104   CONFIG_PARSE_WIN_OK = 0,
    105   CONFIG_PARSE_WIN_READ_IPHELPER,
    106   CONFIG_PARSE_WIN_READ_POLICY_SEARCHLIST,
    107   CONFIG_PARSE_WIN_READ_TCPIP_SEARCHLIST,
    108   CONFIG_PARSE_WIN_READ_DOMAIN,
    109   CONFIG_PARSE_WIN_READ_POLICY_DEVOLUTION,
    110   CONFIG_PARSE_WIN_READ_DNSCACHE_DEVOLUTION,
    111   CONFIG_PARSE_WIN_READ_TCPIP_DEVOLUTION,
    112   CONFIG_PARSE_WIN_READ_APPEND_MULTILABEL,
    113   CONFIG_PARSE_WIN_READ_PRIMARY_SUFFIX,
    114   CONFIG_PARSE_WIN_BAD_ADDRESS,
    115   CONFIG_PARSE_WIN_NO_NAMESERVERS,
    116   CONFIG_PARSE_WIN_MAX  // Bounding values for enumeration.
    117 };
    118 
    119 // Fills in |dns_config| from |settings|. Exposed for tests.
    120 ConfigParseWinResult NET_EXPORT_PRIVATE ConvertSettingsToDnsConfig(
    121     const DnsSystemSettings& settings,
    122     DnsConfig* dns_config);
    123 
    124 // Use DnsConfigService::CreateSystemService to use it outside of tests.
    125 class NET_EXPORT_PRIVATE DnsConfigServiceWin : public DnsConfigService {
    126  public:
    127   DnsConfigServiceWin();
    128   virtual ~DnsConfigServiceWin();
    129 
    130  private:
    131   class Watcher;
    132   class ConfigReader;
    133   class HostsReader;
    134 
    135   // DnsConfigService:
    136   virtual void ReadNow() OVERRIDE;
    137   virtual bool StartWatching() OVERRIDE;
    138 
    139   void OnConfigChanged(bool succeeded);
    140   void OnHostsChanged(bool succeeded);
    141 
    142   scoped_ptr<Watcher> watcher_;
    143   scoped_refptr<ConfigReader> config_reader_;
    144   scoped_refptr<HostsReader> hosts_reader_;
    145 
    146   DISALLOW_COPY_AND_ASSIGN(DnsConfigServiceWin);
    147 };
    148 
    149 }  // namespace internal
    150 
    151 }  // namespace net
    152 
    153 #endif  // NET_DNS_DNS_CONFIG_SERVICE_WIN_H_
    154 
    155