blob: bc3542d6092cbb123a4325dde1e7b4a00df7598f [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]6998a662011-09-03 00:17:2913#include "base/gtest_prod_util.h"
[email protected]b3601bc22012-02-21 21:23:2014#include "base/memory/scoped_ptr.h"
[email protected]76d7f722011-10-10 17:22:4115#include "base/threading/non_thread_safe.h"
[email protected]d84316a2011-08-18 04:41:2116#include "base/time.h"
[email protected]b4481b222012-03-16 17:13:1117#include "base/timer.h"
[email protected]7054e78f2012-05-07 21:44:5618// Needed on shared build with MSVS2010 to avoid multiple definitions of
19// std::vector<IPEndPoint>.
20#include "net/base/address_list.h"
[email protected]6998a662011-09-03 00:17:2921#include "net/base/ip_endpoint.h" // win requires size of IPEndPoint
[email protected]05aad32d2012-05-16 18:10:5322#include "net/base/network_change_notifier.h"
[email protected]d84316a2011-08-18 04:41:2123#include "net/base/net_export.h"
[email protected]6998a662011-09-03 00:17:2924#include "net/dns/dns_hosts.h"
[email protected]d84316a2011-08-18 04:41:2125
[email protected]17e92032012-03-29 00:56:2426namespace base {
27class Value;
28}
29
[email protected]d84316a2011-08-18 04:41:2130namespace net {
31
[email protected]d84316a2011-08-18 04:41:2132// DnsConfig stores configuration of the system resolver.
33struct NET_EXPORT_PRIVATE DnsConfig {
34 DnsConfig();
35 virtual ~DnsConfig();
36
37 bool Equals(const DnsConfig& d) const;
38
[email protected]6998a662011-09-03 00:17:2939 bool EqualsIgnoreHosts(const DnsConfig& d) const;
40
[email protected]b4481b222012-03-16 17:13:1141 void CopyIgnoreHosts(const DnsConfig& src);
42
[email protected]17e92032012-03-29 00:56:2443 // Returns a Value representation of |this|. Caller takes ownership of the
44 // returned Value. For performance reasons, the Value only contains the
45 // number of hosts rather than the full list.
46 base::Value* ToValue() const;
47
[email protected]6998a662011-09-03 00:17:2948 bool IsValid() const {
[email protected]d84316a2011-08-18 04:41:2149 return !nameservers.empty();
50 }
51
52 // List of name server addresses.
53 std::vector<IPEndPoint> nameservers;
54 // Suffix search list; used on first lookup when number of dots in given name
55 // is less than |ndots|.
56 std::vector<std::string> search;
57
[email protected]6998a662011-09-03 00:17:2958 DnsHosts hosts;
59
[email protected]fea484e2012-02-07 23:21:5760 // AppendToMultiLabelName: is suffix search performed for multi-label names?
61 // True, except on Windows where it can be configured.
62 bool append_to_multi_label_name;
63
[email protected]d84316a2011-08-18 04:41:2164 // Resolver options; see man resolv.conf.
[email protected]d84316a2011-08-18 04:41:2165
66 // Minimum number of dots before global resolution precedes |search|.
67 int ndots;
68 // Time between retransmissions, see res_state.retrans.
69 base::TimeDelta timeout;
[email protected]d0d49dd82012-01-26 00:03:5970 // Maximum number of attempts, see res_state.retry.
[email protected]d84316a2011-08-18 04:41:2171 int attempts;
72 // Round robin entries in |nameservers| for subsequent requests.
73 bool rotate;
74 // Enable EDNS0 extensions.
75 bool edns0;
76};
77
[email protected]6998a662011-09-03 00:17:2978
[email protected]05aad32d2012-05-16 18:10:5379// Service for reading system DNS settings, on demand or when signalled by
80// NetworkChangeNotifier.
[email protected]6998a662011-09-03 00:17:2981class NET_EXPORT_PRIVATE DnsConfigService
[email protected]05aad32d2012-05-16 18:10:5382 : NON_EXPORTED_BASE(public base::NonThreadSafe),
83 public NetworkChangeNotifier::DNSObserver {
[email protected]d84316a2011-08-18 04:41:2184 public:
[email protected]05aad32d2012-05-16 18:10:5385 // Callback interface for the client, called on the same thread as Read() and
86 // Watch().
[email protected]b4481b222012-03-16 17:13:1187 typedef base::Callback<void(const DnsConfig& config)> CallbackType;
[email protected]d84316a2011-08-18 04:41:2188
89 // Creates the platform-specific DnsConfigService.
[email protected]b3601bc22012-02-21 21:23:2090 static scoped_ptr<DnsConfigService> CreateSystemService();
[email protected]d84316a2011-08-18 04:41:2191
[email protected]6998a662011-09-03 00:17:2992 DnsConfigService();
93 virtual ~DnsConfigService();
[email protected]d84316a2011-08-18 04:41:2194
[email protected]05aad32d2012-05-16 18:10:5395 // Attempts to read the configuration. Will run |callback| when succeeded.
96 // Can be called at most once.
97 void Read(const CallbackType& callback);
98
99 // Registers for notifications at NetworkChangeNotifier. Will attempt to read
100 // config after watch is started by NetworkChangeNotifier. Will run |callback|
101 // iff config changes from last call or should be withdrawn.
102 // Can be called at most once.
103 virtual void Watch(const CallbackType& callback);
[email protected]d84316a2011-08-18 04:41:21104
[email protected]6998a662011-09-03 00:17:29105 protected:
[email protected]6998a662011-09-03 00:17:29106 friend class DnsHostsReader;
[email protected]b4481b222012-03-16 17:13:11107
108 // Called when the current config (except hosts) has changed.
109 void InvalidateConfig();
110 // Called when the current hosts have changed.
111 void InvalidateHosts();
112
113 // Called with new config. |config|.hosts is ignored.
114 void OnConfigRead(const DnsConfig& config);
115 // Called with new hosts. Rest of the config is assumed unchanged.
116 void OnHostsRead(const DnsHosts& hosts);
117
[email protected]05aad32d2012-05-16 18:10:53118 // NetworkChangeNotifier::DNSObserver:
119 // Must be defined by implementations.
120 virtual void OnDNSChanged(unsigned detail) OVERRIDE = 0;
[email protected]b4481b222012-03-16 17:13:11121
122 private:
123 void StartTimer();
124 // Called when the timer expires.
125 void OnTimeout();
126 // Called when the config becomes complete.
127 void OnCompleteConfig();
128
129 CallbackType callback_;
[email protected]6998a662011-09-03 00:17:29130
131 DnsConfig dns_config_;
[email protected]b4481b222012-03-16 17:13:11132
[email protected]05aad32d2012-05-16 18:10:53133 // True after On*Read, before Invalidate*. Tells if the config is complete.
[email protected]6998a662011-09-03 00:17:29134 bool have_config_;
135 bool have_hosts_;
[email protected]b4481b222012-03-16 17:13:11136 // True if receiver needs to be updated when the config becomes complete.
137 bool need_update_;
[email protected]6998a662011-09-03 00:17:29138
[email protected]b4481b222012-03-16 17:13:11139 // Started in Invalidate*, cleared in On*Read.
140 base::OneShotTimer<DnsConfigService> timer_;
141
[email protected]d84316a2011-08-18 04:41:21142 DISALLOW_COPY_AND_ASSIGN(DnsConfigService);
143};
144
145} // namespace net
146
147#endif // NET_DNS_DNS_CONFIG_SERVICE_H_