Index: net/dns/dns_config_service_posix.cc |
diff --git a/net/dns/dns_config_service_posix.cc b/net/dns/dns_config_service_posix.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..94a44e3d29cdaedfc329a718dbfffe93054a9b03 |
--- /dev/null |
+++ b/net/dns/dns_config_service_posix.cc |
@@ -0,0 +1,242 @@ |
+// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <resolv.h> |
+ |
+#include "base/bind.h" |
+#include "base/compiler_specific.h" |
+#include "base/file_path.h" |
+#include "base/files/file_path_watcher.h" |
+#include "base/memory/ref_counted.h" |
+#include "base/message_loop.h" |
+#include "base/message_loop_proxy.h" |
+#include "base/observer_list.h" |
+#include "base/scoped_ptr.h" |
+#include "base/threading/worker_pool.h" |
+#include "net/base/ip_endpoint.h" |
+#include "net/base/net_util.h" |
+#include "net/dns/dns_config_service.h" |
+ |
+#ifndef _PATH_RESCONF |
+#define _PATH_RESCONF "/etc/resolv.conf" |
+#endif |
+ |
+namespace net { |
+ |
+namespace { |
+ |
+using base::files::FilePathWatcher; |
+ |
+// Fills in |dns_config| from |res|. |
+bool ConfigFromResolver(const struct __res_state& res, DnsConfig* dns_config) { |
+ CHECK(dns_config != NULL); |
+ DCHECK(res.options & RES_INIT); |
+ |
+ dns_config->nameservers.clear(); |
+ for (int i = 0; i < res.nscount; ++i) { |
+ IPEndPoint ipe; |
+ if (ipe.FromSockAddr( |
+ reinterpret_cast<const struct sockaddr*>(&res.nsaddr_list[i]), |
+ sizeof res.nsaddr_list[i])) { |
+ dns_config->nameservers.push_back(ipe); |
+ } else { |
+ return false; |
+ } |
+ } |
+ |
+ // TODO(szym): this might not compile on mac. |
cbentzel
2011/08/05 19:11:39
Can you run try jobs to confirm?
May also want t
szym
2011/08/15 14:27:42
glibc uses funny code to determine the ordering of
|
+ for (int i = 0; i < res._u._ext.nscount6; ++i) { |
+ IPEndPoint ipe; |
+ if (ipe.FromSockAddr( |
+ reinterpret_cast<const struct sockaddr*>(&res._u._ext.nsaddrs[i]), |
+ sizeof res._u._ext.nsaddrs[i])) { |
+ dns_config->nameservers.push_back(ipe); |
+ } else { |
+ return false; |
+ } |
+ } |
+ |
+ dns_config->search.clear(); |
+ for (int i = 0; (i < MAXDNSRCH) && res.dnsrch[i]; ++i) { |
+ dns_config->search.push_back(std::string(res.dnsrch[i])); |
+ } |
+ |
+ dns_config->ndots = res.ndots; |
+ dns_config->timeout = res.retrans; |
+ dns_config->attempts = res.retry; |
+ dns_config->rotate = res.options & RES_ROTATE; |
+ dns_config->edns0 = res.options & RES_USE_EDNS0; |
+ |
+ return true; |
+} |
+ |
+// Fills in |dns_config| using res_ninit, returns true on success. |
+bool ReadConfig(DnsConfig* dns_config) { |
+ // res_ninit is available on Linux (glibc 2+), Mac OS X, and Android. |
+ // It might be thread-safe, but to ensure ordering we run only one at a time. |
+ struct __res_state res; |
+ if (res_ninit(&res) != 0) |
+ return false; |
+ |
+ return ConfigFromResolver(res, dns_config); |
+} |
+ |
+class DnsConfigServicePosix : public DnsConfigService { |
+ public: |
+ DnsConfigServicePosix(); |
+ virtual ~DnsConfigServicePosix() { |
+ // The watcher must be destroyed on the same thread that called Watch. |
+ if (watcher_delegate_.get()) |
+ watcher_delegate_->Cancel(); |
+ } |
+ |
+ virtual void AddObserver(Observer* observer) { |
+ observers_.AddObserver(observer); |
+ } |
+ |
+ virtual void RemoveObserver(Observer* observer) { |
+ observers_.RemoveObserver(observer); |
+ } |
+ |
+ private: |
+ // FilePathWatcher::Delegate is refcounted, so we separate it from the Service |
+ // We'll also use it on the WorkerPool. |
+ class WatcherDelegate : public FilePathWatcher::Delegate { |
cbentzel
2011/08/05 19:11:39
Might be clearer to make this a non-nested class -
szym
2011/08/15 14:27:42
Done.
|
+ public: |
+ explicit WatcherDelegate(DnsConfigServicePosix* service) |
+ : message_loop_(base::MessageLoopProxy::CreateForCurrentThread()), |
+ service_(service) {} |
cbentzel
2011/08/05 19:11:39
You need to initialize reading_(false) and read_pe
szym
2011/08/15 14:27:42
Done. Good catch. I wrongly assumed bool() == fals
|
+ |
+ // FilePathWatcher::Delegate interface |
+ virtual void OnFilePathChanged(const FilePath& path) OVERRIDE { |
+ DCHECK(message_loop_->BelongsToCurrentThread()); |
+ if (!service_) |
+ return; |
+ ScheduleRead(); |
+ } |
+ |
+ virtual void OnFilePathError(const FilePath& path) OVERRIDE { |
+ DCHECK(message_loop_->BelongsToCurrentThread()); |
+ StartWatch(); |
+ } |
+ |
+ void Cancel() { |
+ DCHECK(message_loop_->BelongsToCurrentThread()); |
+ service_ = NULL; |
+ } |
+ |
+ void ScheduleWatch() { |
+ // Retry Watch in 100ms or more. |
+ message_loop_->PostDelayedTask( |
+ FROM_HERE, base::Bind(&WatcherDelegate::StartWatch, this), 100); |
+ } |
+ |
+ private: |
+ virtual ~WatcherDelegate() {} |
+ |
+ void ScheduleRead() { |
+ if (reading_) { |
+ read_pending_ = true; |
+ } else { |
+ // This can't fail on Linux. |
+ base::WorkerPool::PostTask(FROM_HERE, base::Bind( |
+ &WatcherDelegate::DoRead, this), false); |
+ reading_ = true; |
+ read_pending_ = false; |
+ } |
+ } |
+ |
+ // Reads DnsConfig and posts OnResultAvailable to |message_loop_|. |
+ // Must be called on the worker thread. |
+ void DoRead() { |
+ DnsConfig config; |
+ bool success = ReadConfig(&config); |
+ // If this fails, the loop is gone, so there is no point retrying. |
+ message_loop_->PostTask(FROM_HERE, base::Bind( |
+ &WatcherDelegate::OnResultAvailable, this, config, success)); |
+ } |
+ |
+ // Communicates result to the service. Must be called on the the same thread |
+ // that constructed WatcherDelegate. |
+ void OnResultAvailable(const DnsConfig &config, bool success) { |
+ DCHECK(message_loop_->BelongsToCurrentThread()); |
+ if (!service_) |
+ return; |
+ reading_ = false; |
+ if (read_pending_) { |
+ // Discard this result and re-schedule. |
+ ScheduleRead(); |
+ return; |
+ } |
+ if (!success) { |
+ VLOG(1) << "Failed to read DnsConfig"; |
+ } else { |
+ service_->OnConfigRead(config); |
+ } |
+ } |
+ |
+ void StartWatch() { |
+ if (!service_) |
+ return; |
+ service_->StartWatch(); |
+ } |
+ |
+ // Message loop for the thread on which Watch is called (of TYPE_IO). |
+ scoped_refptr<base::MessageLoopProxy> message_loop_; |
+ DnsConfigServicePosix* service_; |
+ // True after DoRead before OnResultsAvailable |
+ bool reading_; |
+ // True after OnFilePathChanged fires while |reading_| is true. |
+ bool read_pending_; |
+ }; |
+ |
+ void OnConfigRead(const DnsConfig& config) { |
+ if (!config.Equals(dns_config_)) { |
+ dns_config_ = config; |
+ FOR_EACH_OBSERVER(Observer, observers_, OnConfigChanged(config)); |
+ } |
+ } |
+ |
+ // Configure a FilePathWatcher and install watcher_delegate_. |
+ void StartWatch(); |
+ |
+ scoped_ptr<FilePathWatcher> resolv_file_watcher_; |
+ scoped_refptr<WatcherDelegate> watcher_delegate_; |
+ ObserverList<Observer> observers_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(DnsConfigServicePosix); |
+}; |
+ |
+DnsConfigServicePosix::DnsConfigServicePosix() { |
+ DCHECK(!resolv_file_watcher_.get()); |
+ DCHECK(!watcher_delegate_.get()); |
+ DCHECK(MessageLoopForIO::current()); |
+ |
+ watcher_delegate_ = new WatcherDelegate(this); |
+ StartWatch(); |
+} |
+ |
+void DnsConfigServicePosix::StartWatch() { |
+ DCHECK(watcher_delegate_.get()); |
+ |
+ FilePath path(FILE_PATH_LITERAL(_PATH_RESCONF)); |
+ |
+ // FilePathWatcher allows only one Watch per lifetime, so we need a new one. |
+ resolv_file_watcher_.reset(new FilePathWatcher()); |
+ if (!resolv_file_watcher_->Watch(path, watcher_delegate_)) { |
+ watcher_delegate_->ScheduleWatch(); |
+ } else { |
+ // Make the initial read after watch is installed. |
+ watcher_delegate_->OnFilePathChanged(path); |
+ } |
+} |
+ |
+} // namespace |
+ |
+DnsConfigService* DnsConfigService::CreateSystemService() { |
+ return new DnsConfigServicePosix(); |
+} |
+ |
+} // namespace net |
+ |