Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 using base::files::FilePathWatcher; | |
| 27 | |
| 28 FilePathWatcherShim::FilePathWatcherShim() : watcher_(new FilePathWatcher()) {} | |
| 29 | |
| 30 bool FilePathWatcherShim::Watch(const FilePath& path, | |
| 31 FilePathWatcher::Delegate* delegate) { | |
| 32 return watcher_->Watch(path, delegate); | |
| 33 } | |
| 34 | |
| 35 bool ConvertResToConfig(const struct __res_state& res, DnsConfig* dns_config) { | |
| 36 CHECK(dns_config != NULL); | |
| 37 DCHECK(res.options & RES_INIT); | |
| 38 | |
| 39 dns_config->nameservers.clear(); | |
| 40 | |
| 41 #if OS_LINUX | |
| 42 // Initially, glibc stores IPv6 in _ext.nsaddrs and IPv4 in nsaddr_list. | |
| 43 // Next (res_send.c::__libc_res_nsend), it copies nsaddr_list after nsaddrs. | |
| 44 // If RES_ROTATE is enabled, the list is shifted left after each res_send. | |
| 45 // However, if nsaddr_list changes, it will refill nsaddr_list (IPv4) but | |
| 46 // leave the IPv6 entries in nsaddr in the same (shifted) order. | |
| 47 | |
| 48 // Put IPv6 addresses ahead of IPv4. | |
| 49 for (int i = 0; i < res._u._ext.nscount6; ++i) { | |
| 50 IPEndPoint ipe; | |
| 51 if (ipe.FromSockAddr( | |
| 52 reinterpret_cast<const struct sockaddr*>(res._u._ext.nsaddrs[i]), | |
| 53 sizeof *res._u._ext.nsaddrs[i])) { | |
| 54 dns_config->nameservers.push_back(ipe); | |
| 55 } else { | |
| 56 return false; | |
| 57 } | |
| 58 } | |
| 59 #endif | |
| 60 | |
| 61 for (int i = 0; i < res.nscount; ++i) { | |
| 62 IPEndPoint ipe; | |
| 63 if (ipe.FromSockAddr( | |
| 64 reinterpret_cast<const struct sockaddr*>(&res.nsaddr_list[i]), | |
| 65 sizeof res.nsaddr_list[i])) { | |
| 66 dns_config->nameservers.push_back(ipe); | |
| 67 } else { | |
| 68 return false; | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 dns_config->search.clear(); | |
| 73 for (int i = 0; (i < MAXDNSRCH) && res.dnsrch[i]; ++i) { | |
| 74 dns_config->search.push_back(std::string(res.dnsrch[i])); | |
| 75 } | |
| 76 | |
| 77 dns_config->ndots = res.ndots; | |
| 78 dns_config->timeout = base::TimeDelta::FromSeconds(res.retrans); | |
| 79 dns_config->attempts = res.retry; | |
| 80 dns_config->rotate = res.options & RES_ROTATE; | |
| 81 dns_config->edns0 = res.options & RES_USE_EDNS0; | |
| 82 | |
| 83 return true; | |
| 84 } | |
| 85 | |
| 86 // FilePathWatcher::Delegate is refcounted, so we separate it from the Service | |
| 87 // It also hosts callbacks on the WorkerPool. | |
| 88 class WatcherDelegate : public FilePathWatcher::Delegate { | |
| 89 public: | |
| 90 // Takes ownership of |lib|. | |
| 91 WatcherDelegate(DnsConfigServicePosix* service, ResolverLib* lib) | |
| 92 : service_(service), | |
| 93 resolver_lib_(lib), | |
| 94 message_loop_(base::MessageLoopProxy::CreateForCurrentThread()), | |
| 95 reading_(false), | |
| 96 read_pending_(false) {} | |
| 97 | |
| 98 void Cancel() { | |
| 99 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 100 service_ = NULL; | |
| 101 } | |
| 102 | |
| 103 void ScheduleWatch() { | |
| 104 // Retry Watch in 100ms or so. | |
|
cbentzel
2011/08/15 18:16:34
Should this do a bit of an exponential backoff?
cbentzel
2011/08/15 18:16:34
Add a DCHECK(message_loop_->BelongsToCurrentThread
szym
2011/08/15 22:02:01
Done.
szym
2011/08/15 22:02:01
Looking at _linux, indeed it cannot fail. However,
| |
| 105 message_loop_->PostDelayedTask( | |
| 106 FROM_HERE, base::Bind(&WatcherDelegate::StartWatch, this), 100); | |
| 107 } | |
| 108 | |
| 109 // FilePathWatcher::Delegate interface | |
| 110 virtual void OnFilePathChanged(const FilePath& path) OVERRIDE { | |
| 111 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 112 if (!service_) | |
| 113 return; | |
| 114 ScheduleRead(); | |
| 115 } | |
| 116 | |
| 117 virtual void OnFilePathError(const FilePath& path) OVERRIDE { | |
| 118 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 119 StartWatch(); | |
| 120 } | |
| 121 | |
| 122 private: | |
| 123 virtual ~WatcherDelegate() {} | |
|
cbentzel
2011/08/15 18:16:34
Is it possible for the FilePathWatcher to outlive
szym
2011/08/15 22:02:01
It's possible for the WatcherDelegate to outlive b
| |
| 124 | |
| 125 // Unless already scheduled, post DoRead to WorkerPool. | |
| 126 void ScheduleRead() { | |
| 127 if (reading_) { | |
| 128 // Mark that we need to re-read after DoRead posts results. | |
| 129 read_pending_ = true; | |
| 130 } else { | |
| 131 // This can't fail on posix. See worker_pool_posix.cc. | |
| 132 base::WorkerPool::PostTask(FROM_HERE, base::Bind( | |
|
cbentzel
2011/08/15 18:16:34
Perhaps do a NOTREACHED() if this ever returns fal
szym
2011/08/15 22:02:01
Done.
| |
| 133 &WatcherDelegate::DoRead, this), false); | |
| 134 reading_ = true; | |
| 135 read_pending_ = false; | |
| 136 } | |
| 137 } | |
| 138 | |
| 139 // Reads DnsConfig and posts OnResultAvailable to |message_loop_|. | |
| 140 // Must be called on the worker thread. | |
| 141 void DoRead() { | |
| 142 DnsConfig config; | |
| 143 struct __res_state res; | |
| 144 bool success = false; | |
| 145 if (resolver_lib_->ninit(&res) == 0) { | |
| 146 success = ConvertResToConfig(res, &config); | |
| 147 resolver_lib_->nclose(&res); | |
| 148 } | |
| 149 // If this fails, the loop is gone, so there is no point retrying. | |
| 150 message_loop_->PostTask(FROM_HERE, base::Bind( | |
| 151 &WatcherDelegate::OnResultAvailable, this, config, success)); | |
| 152 } | |
| 153 | |
| 154 // Communicates result to the service. Must be called on the the same thread | |
| 155 // that constructed WatcherDelegate. | |
| 156 void OnResultAvailable(const DnsConfig &config, bool success) { | |
| 157 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 158 if (!service_) | |
| 159 return; | |
| 160 reading_ = false; | |
| 161 if (read_pending_) { | |
| 162 // Discard this result and re-schedule. | |
| 163 ScheduleRead(); | |
| 164 return; | |
| 165 } | |
| 166 if (!success) { | |
| 167 VLOG(1) << "Failed to read DnsConfig"; | |
| 168 } else { | |
| 169 service_->OnConfigRead(config); | |
| 170 } | |
| 171 } | |
| 172 | |
| 173 void StartWatch() { | |
| 174 if (!service_) | |
|
cbentzel
2011/08/15 18:16:34
Good, glad this check is here.
| |
| 175 return; | |
| 176 service_->StartWatch(); | |
| 177 } | |
| 178 | |
| 179 DnsConfigServicePosix* service_; | |
| 180 scoped_ptr<ResolverLib> resolver_lib_; | |
| 181 // Message loop for the thread on which Watch is called (of TYPE_IO). | |
| 182 scoped_refptr<base::MessageLoopProxy> message_loop_; | |
| 183 // True after DoRead before OnResultsAvailable. | |
| 184 bool reading_; | |
| 185 // True after OnFilePathChanged fires while |reading_| is true. | |
| 186 bool read_pending_; | |
| 187 }; | |
| 188 | |
| 189 DnsConfigServicePosix::DnsConfigServicePosix() | |
| 190 : have_config_(false), | |
| 191 resolver_lib_(new ResolverLib()), | |
| 192 watcher_factory_(new FilePathWatcherFactory()) { | |
| 193 } | |
| 194 | |
| 195 DnsConfigServicePosix::~DnsConfigServicePosix() { | |
| 196 // The watcher must be destroyed on the same thread that called Watch. | |
|
cbentzel
2011/08/15 18:16:34
This comment doesn't seem too useful - I'd get rid
szym
2011/08/15 22:02:01
Done.
| |
| 197 if (watcher_delegate_.get()) | |
| 198 watcher_delegate_->Cancel(); | |
| 199 } | |
| 200 | |
| 201 void DnsConfigServicePosix::Watch() { | |
| 202 DCHECK(!watcher_delegate_.get()); | |
| 203 DCHECK(!resolv_file_watcher_.get()); | |
| 204 DCHECK(resolver_lib_.get()); | |
|
cbentzel
2011/08/15 18:16:34
May as well add DCHECK(watcher_factory_.get()) as
szym
2011/08/15 22:02:01
Done.
| |
| 205 | |
| 206 watcher_delegate_ = new WatcherDelegate(this, resolver_lib_.release()); | |
| 207 StartWatch(); | |
| 208 } | |
| 209 | |
| 210 void DnsConfigServicePosix::StartWatch() { | |
| 211 DCHECK(watcher_delegate_.get()); | |
| 212 | |
| 213 FilePath path(FILE_PATH_LITERAL(_PATH_RESCONF)); | |
| 214 | |
| 215 // FilePathWatcher allows only one Watch per lifetime, so we need a new one. | |
| 216 resolv_file_watcher_.reset(watcher_factory_->CreateFilePathWatcher()); | |
| 217 if (resolv_file_watcher_->Watch(path, watcher_delegate_.get())) { | |
| 218 // Make the initial read after watch is installed. | |
| 219 watcher_delegate_->OnFilePathChanged(path); | |
| 220 } else { | |
| 221 VLOG(1) << "Watch failed, scheduling restart"; | |
| 222 watcher_delegate_->ScheduleWatch(); | |
|
cbentzel
2011/08/15 18:16:34
It's a little strange to me to have this on the Wa
szym
2011/08/15 22:02:01
Added a comment to WatcherDelegate::ScheduleWatch
| |
| 223 } | |
| 224 } | |
| 225 | |
| 226 DnsConfigService* DnsConfigService::CreateSystemService() { | |
| 227 return new DnsConfigServicePosix(); | |
| 228 } | |
| 229 | |
| 230 } // namespace net | |
| 231 | |
| OLD | NEW |