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 // 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) { |
| 172 DCHECK(CalledOnValidThread()); |
| 173 if (!config.Equals(dns_config_)) { |
| 174 dns_config_ = config; |
| 175 have_config_ = true; |
| 176 FOR_EACH_OBSERVER(Observer, observers_, OnConfigChanged(config)); |
| 177 } |
| 178 } |
| 179 |
| 180 void DnsConfigServicePosix::StartWatch() { |
| 181 DCHECK(CalledOnValidThread()); |
| 182 DCHECK(watcher_delegate_.get()); |
| 183 |
| 184 FilePath path(FILE_PATH_LITERAL(_PATH_RESCONF)); |
| 185 |
| 186 // FilePathWatcher allows only one Watch per lifetime, so we need a new one. |
| 187 resolv_file_watcher_.reset(watcher_factory_->CreateFilePathWatcher()); |
| 188 if (resolv_file_watcher_->Watch(path, watcher_delegate_.get())) { |
| 189 // Make the initial read after watch is installed. |
| 190 watcher_delegate_->OnFilePathChanged(path); |
| 191 } else { |
| 192 VLOG(1) << "Watch failed, scheduling restart"; |
| 193 watcher_delegate_->RescheduleWatch(); |
| 194 } |
| 195 } |
| 196 |
| 197 int DnsConfigServicePosix::ResolverLib::ninit(res_state statp) { |
| 198 return ::res_ninit(statp); |
| 199 } |
| 200 |
| 201 void DnsConfigServicePosix::ResolverLib::nclose(res_state statp) { |
| 202 return ::res_nclose(statp); |
| 203 } |
| 204 |
| 205 DnsConfigServicePosix::FilePathWatcherShim::FilePathWatcherShim() |
| 206 : watcher_(new base::files::FilePathWatcher()) {} |
| 207 DnsConfigServicePosix::FilePathWatcherShim::~FilePathWatcherShim() {} |
| 208 |
| 209 bool DnsConfigServicePosix::FilePathWatcherShim::Watch( |
| 210 const FilePath& path, |
| 211 base::files::FilePathWatcher::Delegate* delegate) { |
| 212 return watcher_->Watch(path, delegate); |
| 213 } |
| 214 |
| 215 DnsConfigServicePosix::FilePathWatcherShim* |
| 216 DnsConfigServicePosix::FilePathWatcherFactory::CreateFilePathWatcher() { |
| 217 return new FilePathWatcherShim(); |
| 218 } |
| 219 |
| 220 // static |
| 221 DnsConfigService* DnsConfigService::CreateSystemService() { |
| 222 return new DnsConfigServicePosix(); |
| 223 } |
| 224 |
| 225 bool ConvertResToConfig(const struct __res_state& res, DnsConfig* dns_config) { |
| 226 CHECK(dns_config != NULL); |
| 227 DCHECK(res.options & RES_INIT); |
| 228 |
| 229 dns_config->nameservers.clear(); |
| 230 |
| 231 #if OS_LINUX |
| 232 // Initially, glibc stores IPv6 in _ext.nsaddrs and IPv4 in nsaddr_list. |
| 233 // Next (res_send.c::__libc_res_nsend), it copies nsaddr_list after nsaddrs. |
| 234 // If RES_ROTATE is enabled, the list is shifted left after each res_send. |
| 235 // However, if nsaddr_list changes, it will refill nsaddr_list (IPv4) but |
| 236 // leave the IPv6 entries in nsaddr in the same (shifted) order. |
| 237 |
| 238 // Put IPv6 addresses ahead of IPv4. |
| 239 for (int i = 0; i < res._u._ext.nscount6; ++i) { |
| 240 IPEndPoint ipe; |
| 241 if (ipe.FromSockAddr( |
| 242 reinterpret_cast<const struct sockaddr*>(res._u._ext.nsaddrs[i]), |
| 243 sizeof *res._u._ext.nsaddrs[i])) { |
| 244 dns_config->nameservers.push_back(ipe); |
| 245 } else { |
| 246 return false; |
| 247 } |
| 248 } |
| 249 #endif |
| 250 |
| 251 for (int i = 0; i < res.nscount; ++i) { |
| 252 IPEndPoint ipe; |
| 253 if (ipe.FromSockAddr( |
| 254 reinterpret_cast<const struct sockaddr*>(&res.nsaddr_list[i]), |
| 255 sizeof res.nsaddr_list[i])) { |
| 256 dns_config->nameservers.push_back(ipe); |
| 257 } else { |
| 258 return false; |
| 259 } |
| 260 } |
| 261 |
| 262 dns_config->search.clear(); |
| 263 for (int i = 0; (i < MAXDNSRCH) && res.dnsrch[i]; ++i) { |
| 264 dns_config->search.push_back(std::string(res.dnsrch[i])); |
| 265 } |
| 266 |
| 267 dns_config->ndots = res.ndots; |
| 268 dns_config->timeout = base::TimeDelta::FromSeconds(res.retrans); |
| 269 dns_config->attempts = res.retry; |
| 270 dns_config->rotate = res.options & RES_ROTATE; |
| 271 dns_config->edns0 = res.options & RES_USE_EDNS0; |
| 272 |
| 273 return true; |
| 274 } |
| 275 |
| 276 } // namespace net |
| 277 |
OLD | NEW |