Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(362)

Side by Side Diff: net/dns/dns_config_service_posix.cc

Issue 7518028: DnsConfigService and a posix implementation (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Deflaked test. Addressed memory leaks and comments. Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/dns/dns_config_service_posix.h ('k') | net/dns/dns_config_service_posix_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2011 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 #include "net/dns/dns_config_service_posix.h"
6
7 #include "base/bind.h"
8 #include "base/compiler_specific.h"
9 #include "base/file_path.h"
10 #include "base/files/file_path_watcher.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/message_loop.h"
13 #include "base/message_loop_proxy.h"
14 #include "base/observer_list.h"
15 #include "base/scoped_ptr.h"
16 #include "base/threading/worker_pool.h"
17 #include "net/base/ip_endpoint.h"
18 #include "net/base/net_util.h"
19
20 #ifndef _PATH_RESCONF // Normally defined in <resolv.h>
21 #define _PATH_RESCONF "/etc/resolv.conf"
22 #endif
23
24 namespace net {
25
26 // FilePathWatcher::Delegate is refcounted, so we separate it from the Service
27 // It also hosts callbacks on the WorkerPool.
28 class DnsConfigServicePosix::WatcherDelegate :
29 public base::files::FilePathWatcher::Delegate {
30 public:
31 // Takes ownership of |lib|.
32 WatcherDelegate(DnsConfigServicePosix* service,
33 DnsConfigServicePosix::ResolverLib* lib)
34 : service_(service),
35 resolver_lib_(lib),
36 message_loop_(base::MessageLoopProxy::current()),
37 reading_(false),
38 read_pending_(false) {}
39
40 void Cancel() {
41 DCHECK(message_loop_->BelongsToCurrentThread());
42 service_ = NULL;
43 }
44
45 void RescheduleWatch() {
46 DCHECK(message_loop_->BelongsToCurrentThread());
47 // Retry Watch in 100ms or so.
48 message_loop_->PostDelayedTask(
49 FROM_HERE, base::Bind(&WatcherDelegate::StartWatch, this), 100);
50 }
51
52 // FilePathWatcher::Delegate interface
53 virtual void OnFilePathChanged(const FilePath& path) OVERRIDE {
54 DCHECK(message_loop_->BelongsToCurrentThread());
55 if (!service_)
56 return;
57 ScheduleRead();
58 }
59
60 virtual void OnFilePathError(const FilePath& path) OVERRIDE {
61 DCHECK(message_loop_->BelongsToCurrentThread());
62 StartWatch();
63 }
64
65 private:
66 virtual ~WatcherDelegate() {}
67
68 // Unless already scheduled, post DoRead to WorkerPool.
69 void ScheduleRead() {
70 if (reading_) {
71 // Mark that we need to re-read after DoRead posts results.
72 read_pending_ = true;
73 } else {
74 if (!base::WorkerPool::PostTask(FROM_HERE, base::Bind(
75 &WatcherDelegate::DoRead, this), false)) {
76 // See worker_pool_posix.cc.
77 NOTREACHED() << "WorkerPool::PostTask is not expected to fail on posix";
78 }
79 reading_ = true;
80 read_pending_ = false;
81 }
82 }
83
84 // Reads DnsConfig and posts OnResultAvailable to |message_loop_|.
85 // Must be called on the worker thread.
86 void DoRead() {
87 DnsConfig config;
88 struct __res_state res;
89 bool success = false;
90 if (resolver_lib_->ninit(&res) == 0) {
91 success = ConvertResToConfig(res, &config);
92 resolver_lib_->nclose(&res);
93 }
94 // If this fails, the loop is gone, so there is no point retrying.
95 message_loop_->PostTask(FROM_HERE, base::Bind(
96 &WatcherDelegate::OnResultAvailable, this, config, success));
97 }
98
99 // Communicates result to the service. Must be called on the the same thread
100 // that constructed WatcherDelegate.
101 void OnResultAvailable(const DnsConfig &config, bool success) {
102 DCHECK(message_loop_->BelongsToCurrentThread());
103 if (!service_)
104 return;
105 reading_ = false;
106 if (read_pending_) {
107 // Discard this result and re-schedule.
108 ScheduleRead();
109 return;
110 }
111 if (!success) {
112 VLOG(1) << "Failed to read DnsConfig";
113 } else {
114 service_->OnConfigRead(config);
115 }
116 }
117
118 // To avoid refcounting the service, use this method in tasks/callbacks.
119 void StartWatch() {
120 if (!service_)
121 return;
122 service_->StartWatch();
123 }
124
125 DnsConfigServicePosix* service_;
126 scoped_ptr<DnsConfigServicePosix::ResolverLib> resolver_lib_;
127 // Message loop for the thread on which Watch is called (of TYPE_IO).
128 scoped_refptr<base::MessageLoopProxy> message_loop_;
129 // True after DoRead before OnResultsAvailable.
130 bool reading_;
131 // True after OnFilePathChanged fires while |reading_| is true.
132 bool read_pending_;
133 };
134
135 DnsConfigServicePosix::DnsConfigServicePosix()
136 : have_config_(false),
137 resolver_lib_(new ResolverLib()),
138 watcher_factory_(new FilePathWatcherFactory()) {
139 }
140
141 DnsConfigServicePosix::~DnsConfigServicePosix() {
142 DCHECK(CalledOnValidThread());
143 if (watcher_delegate_.get())
144 watcher_delegate_->Cancel();
145 }
146
147 void DnsConfigServicePosix::AddObserver(Observer* observer) {
148 DCHECK(CalledOnValidThread());
149 observers_.AddObserver(observer);
150 if (have_config_) {
151 observer->OnConfigChanged(dns_config_);
152 }
153 }
154
155 void DnsConfigServicePosix::RemoveObserver(Observer* observer) {
156 DCHECK(CalledOnValidThread());
157 observers_.RemoveObserver(observer);
158 }
159
160 void DnsConfigServicePosix::Watch() {
161 DCHECK(CalledOnValidThread());
162 DCHECK(!watcher_delegate_.get());
163 DCHECK(!resolv_file_watcher_.get());
164 DCHECK(resolver_lib_.get());
165 DCHECK(watcher_factory_.get());
166
167 watcher_delegate_ = new WatcherDelegate(this, resolver_lib_.release());
168 StartWatch();
169 }
170
171 void DnsConfigServicePosix::OnConfigRead(const DnsConfig& config) {
cbentzel 2011/08/16 17:44:24 Maybe DCHECK(CalledOnValidThread()). It's not a pu
172 if (!config.Equals(dns_config_)) {
173 dns_config_ = config;
174 have_config_ = true;
175 FOR_EACH_OBSERVER(Observer, observers_, OnConfigChanged(config));
176 }
177 }
178
179 void DnsConfigServicePosix::StartWatch() {
180 DCHECK(watcher_delegate_.get());
181
182 FilePath path(FILE_PATH_LITERAL(_PATH_RESCONF));
183
184 // FilePathWatcher allows only one Watch per lifetime, so we need a new one.
185 resolv_file_watcher_.reset(watcher_factory_->CreateFilePathWatcher());
186 if (resolv_file_watcher_->Watch(path, watcher_delegate_.get())) {
187 // Make the initial read after watch is installed.
188 watcher_delegate_->OnFilePathChanged(path);
189 } else {
190 VLOG(1) << "Watch failed, scheduling restart";
191 watcher_delegate_->RescheduleWatch();
192 }
193 }
194
195 // static
196 DnsConfigService* DnsConfigService::CreateSystemService() {
197 return new DnsConfigServicePosix();
198 }
199
200 bool ConvertResToConfig(const struct __res_state& res, DnsConfig* dns_config) {
201 CHECK(dns_config != NULL);
202 DCHECK(res.options & RES_INIT);
203
204 dns_config->nameservers.clear();
205
206 #if OS_LINUX
207 // Initially, glibc stores IPv6 in _ext.nsaddrs and IPv4 in nsaddr_list.
208 // Next (res_send.c::__libc_res_nsend), it copies nsaddr_list after nsaddrs.
209 // If RES_ROTATE is enabled, the list is shifted left after each res_send.
210 // However, if nsaddr_list changes, it will refill nsaddr_list (IPv4) but
211 // leave the IPv6 entries in nsaddr in the same (shifted) order.
212
213 // Put IPv6 addresses ahead of IPv4.
214 for (int i = 0; i < res._u._ext.nscount6; ++i) {
215 IPEndPoint ipe;
216 if (ipe.FromSockAddr(
217 reinterpret_cast<const struct sockaddr*>(res._u._ext.nsaddrs[i]),
218 sizeof *res._u._ext.nsaddrs[i])) {
219 dns_config->nameservers.push_back(ipe);
220 } else {
221 return false;
222 }
223 }
224 #endif
225
226 for (int i = 0; i < res.nscount; ++i) {
227 IPEndPoint ipe;
228 if (ipe.FromSockAddr(
229 reinterpret_cast<const struct sockaddr*>(&res.nsaddr_list[i]),
230 sizeof res.nsaddr_list[i])) {
231 dns_config->nameservers.push_back(ipe);
232 } else {
233 return false;
234 }
235 }
236
237 dns_config->search.clear();
238 for (int i = 0; (i < MAXDNSRCH) && res.dnsrch[i]; ++i) {
239 dns_config->search.push_back(std::string(res.dnsrch[i]));
240 }
241
242 dns_config->ndots = res.ndots;
243 dns_config->timeout = base::TimeDelta::FromSeconds(res.retrans);
244 dns_config->attempts = res.retry;
245 dns_config->rotate = res.options & RES_ROTATE;
246 dns_config->edns0 = res.options & RES_USE_EDNS0;
247
248 return true;
249 }
250
251 } // namespace net
252
OLDNEW
« no previous file with comments | « net/dns/dns_config_service_posix.h ('k') | net/dns/dns_config_service_posix_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698