blob: 5fdd2b6848dddfdd5bbc1cd3e6cdb305a6188e5c [file] [log] [blame]
[email protected]d0d49dd82012-01-26 00:03:591// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]d84316a2011-08-18 04:41:212// 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_H_
6#define NET_DNS_DNS_CONFIG_SERVICE_H_
7#pragma once
8
[email protected]6998a662011-09-03 00:17:299#include <map>
[email protected]d84316a2011-08-18 04:41:2110#include <string>
11#include <vector>
12
[email protected]76d7f722011-10-10 17:22:4113#include "base/file_path.h"
[email protected]6998a662011-09-03 00:17:2914#include "base/gtest_prod_util.h"
15#include "base/observer_list.h"
[email protected]76d7f722011-10-10 17:22:4116#include "base/threading/non_thread_safe.h"
[email protected]d84316a2011-08-18 04:41:2117#include "base/time.h"
[email protected]6998a662011-09-03 00:17:2918#include "net/base/ip_endpoint.h" // win requires size of IPEndPoint
[email protected]d84316a2011-08-18 04:41:2119#include "net/base/net_export.h"
[email protected]6998a662011-09-03 00:17:2920#include "net/dns/dns_hosts.h"
[email protected]76d7f722011-10-10 17:22:4121#include "net/dns/serial_worker.h"
[email protected]d84316a2011-08-18 04:41:2122
23namespace net {
24
[email protected]d84316a2011-08-18 04:41:2125// DnsConfig stores configuration of the system resolver.
26struct NET_EXPORT_PRIVATE DnsConfig {
27 DnsConfig();
28 virtual ~DnsConfig();
29
30 bool Equals(const DnsConfig& d) const;
31
[email protected]6998a662011-09-03 00:17:2932 bool EqualsIgnoreHosts(const DnsConfig& d) const;
33
34 bool IsValid() const {
[email protected]d84316a2011-08-18 04:41:2135 return !nameservers.empty();
36 }
37
38 // List of name server addresses.
39 std::vector<IPEndPoint> nameservers;
40 // Suffix search list; used on first lookup when number of dots in given name
41 // is less than |ndots|.
[email protected]d0d49dd82012-01-26 00:03:5942 // TODO(szym): Filter out duplicate entries from this list.
[email protected]d84316a2011-08-18 04:41:2143 std::vector<std::string> search;
44
[email protected]6998a662011-09-03 00:17:2945 DnsHosts hosts;
46
[email protected]d84316a2011-08-18 04:41:2147 // Resolver options; see man resolv.conf.
[email protected]6998a662011-09-03 00:17:2948 // TODO(szym): implement DNS Devolution for windows
[email protected]d84316a2011-08-18 04:41:2149
50 // Minimum number of dots before global resolution precedes |search|.
51 int ndots;
52 // Time between retransmissions, see res_state.retrans.
53 base::TimeDelta timeout;
[email protected]d0d49dd82012-01-26 00:03:5954 // Maximum number of attempts, see res_state.retry.
[email protected]d84316a2011-08-18 04:41:2155 int attempts;
56 // Round robin entries in |nameservers| for subsequent requests.
57 bool rotate;
58 // Enable EDNS0 extensions.
59 bool edns0;
60};
61
[email protected]6998a662011-09-03 00:17:2962
[email protected]d84316a2011-08-18 04:41:2163// Service for watching when the system DNS settings have changed.
64// Depending on the platform, watches files in /etc/ or win registry.
[email protected]6998a662011-09-03 00:17:2965class NET_EXPORT_PRIVATE DnsConfigService
66 : NON_EXPORTED_BASE(public base::NonThreadSafe) {
[email protected]d84316a2011-08-18 04:41:2167 public:
68 // Callback interface for the client. The observer is called on the same
69 // thread as Watch(). Observer must outlive the service.
70 class Observer {
71 public:
72 virtual ~Observer() {}
73
[email protected]6998a662011-09-03 00:17:2974 // Called only when |dns_config| is different from the last call.
[email protected]d84316a2011-08-18 04:41:2175 virtual void OnConfigChanged(const DnsConfig& dns_config) = 0;
76 };
77
78 // Creates the platform-specific DnsConfigService.
79 static DnsConfigService* CreateSystemService();
80
[email protected]6998a662011-09-03 00:17:2981 DnsConfigService();
82 virtual ~DnsConfigService();
[email protected]d84316a2011-08-18 04:41:2183
84 // Immediately starts watching system configuration for changes and attempts
85 // to read the configuration. For some platform implementations, the current
86 // thread must have an IO loop (for base::files::FilePathWatcher).
[email protected]6998a662011-09-03 00:17:2987 virtual void Watch() {}
[email protected]d84316a2011-08-18 04:41:2188
89 // If a config is available, |observer| will immediately be called with
90 // OnConfigChanged.
[email protected]6998a662011-09-03 00:17:2991 void AddObserver(Observer* observer);
92 void RemoveObserver(Observer* observer);
[email protected]d84316a2011-08-18 04:41:2193
[email protected]6998a662011-09-03 00:17:2994 protected:
95 FRIEND_TEST_ALL_PREFIXES(DnsConfigServiceTest, NotifyOnChange);
96 friend class DnsHostsReader;
97 // To be called with new config. |config|.hosts is ignored.
98 virtual void OnConfigRead(const DnsConfig& config);
99 // To be called with new hosts. Rest of the config is assumed unchanged.
100 virtual void OnHostsRead(const DnsHosts& hosts);
101
102 DnsConfig dns_config_;
103 // True after first OnConfigRead and OnHostsRead; indicate complete config.
104 bool have_config_;
105 bool have_hosts_;
106
107 ObserverList<Observer> observers_;
[email protected]d84316a2011-08-18 04:41:21108 private:
109 DISALLOW_COPY_AND_ASSIGN(DnsConfigService);
110};
111
[email protected]6998a662011-09-03 00:17:29112// A WatchingFileReader that reads a HOSTS file and notifies
113// DnsConfigService::OnHostsRead().
[email protected]76d7f722011-10-10 17:22:41114// Client should call Cancel() when |service| is going away.
115class NET_EXPORT_PRIVATE DnsHostsReader
116 : NON_EXPORTED_BASE(public SerialWorker) {
[email protected]6998a662011-09-03 00:17:29117 public:
[email protected]76d7f722011-10-10 17:22:41118 DnsHostsReader(const FilePath& path, DnsConfigService* service);
[email protected]6998a662011-09-03 00:17:29119
[email protected]76d7f722011-10-10 17:22:41120 virtual void DoWork() OVERRIDE;
121 virtual void OnWorkFinished() OVERRIDE;
[email protected]6998a662011-09-03 00:17:29122
123 private:
124 virtual ~DnsHostsReader();
125
[email protected]76d7f722011-10-10 17:22:41126 FilePath path_;
[email protected]6998a662011-09-03 00:17:29127 DnsConfigService* service_;
128 // Written in DoRead, read in OnReadFinished, no locking necessary.
129 DnsHosts dns_hosts_;
130 bool success_;
131
132 DISALLOW_COPY_AND_ASSIGN(DnsHostsReader);
133};
134
[email protected]d84316a2011-08-18 04:41:21135} // namespace net
136
137#endif // NET_DNS_DNS_CONFIG_SERVICE_H_