| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/dns/dns_config_service_posix.h" | 5 #include "net/dns/dns_config_service_posix.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/files/file_path_watcher.h" | 12 #include "base/files/file_path_watcher.h" |
| 13 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/metrics/histogram.h" | 15 #include "base/metrics/histogram.h" |
| 16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 17 #include "net/base/ip_endpoint.h" | 17 #include "net/base/ip_endpoint.h" |
| 18 #include "net/base/net_util.h" | 18 #include "net/base/net_util.h" |
| 19 #include "net/dns/dns_hosts.h" | 19 #include "net/dns/dns_hosts.h" |
| 20 #include "net/dns/dns_protocol.h" | 20 #include "net/dns/dns_protocol.h" |
| 21 #include "net/dns/notify_watcher_mac.h" | 21 #include "net/dns/notify_watcher_mac.h" |
| 22 #include "net/dns/serial_worker.h" | 22 #include "net/dns/serial_worker.h" |
| 23 | 23 |
| 24 #if defined(OS_MACOSX) | 24 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 25 #include <dlfcn.h> | 25 #include "net/dns/dns_config_watcher_mac.h" |
| 26 | 26 #endif |
| 27 #include "third_party/apple_apsl/dnsinfo.h" | |
| 28 | |
| 29 namespace { | |
| 30 | |
| 31 // dnsinfo symbols are available via libSystem.dylib, but can also be present in | |
| 32 // SystemConfiguration.framework. To avoid confusion, load them explicitly from | |
| 33 // libSystem.dylib. | |
| 34 class DnsInfoApi { | |
| 35 public: | |
| 36 typedef const char* (*dns_configuration_notify_key_t)(); | |
| 37 typedef dns_config_t* (*dns_configuration_copy_t)(); | |
| 38 typedef void (*dns_configuration_free_t)(dns_config_t*); | |
| 39 | |
| 40 DnsInfoApi() | |
| 41 : dns_configuration_notify_key(NULL), | |
| 42 dns_configuration_copy(NULL), | |
| 43 dns_configuration_free(NULL) { | |
| 44 handle_ = dlopen("/usr/lib/libSystem.dylib", | |
| 45 RTLD_LAZY | RTLD_NOLOAD); | |
| 46 if (!handle_) | |
| 47 return; | |
| 48 dns_configuration_notify_key = | |
| 49 reinterpret_cast<dns_configuration_notify_key_t>( | |
| 50 dlsym(handle_, "dns_configuration_notify_key")); | |
| 51 dns_configuration_copy = | |
| 52 reinterpret_cast<dns_configuration_copy_t>( | |
| 53 dlsym(handle_, "dns_configuration_copy")); | |
| 54 dns_configuration_free = | |
| 55 reinterpret_cast<dns_configuration_free_t>( | |
| 56 dlsym(handle_, "dns_configuration_free")); | |
| 57 } | |
| 58 | |
| 59 ~DnsInfoApi() { | |
| 60 if (handle_) | |
| 61 dlclose(handle_); | |
| 62 } | |
| 63 | |
| 64 dns_configuration_notify_key_t dns_configuration_notify_key; | |
| 65 dns_configuration_copy_t dns_configuration_copy; | |
| 66 dns_configuration_free_t dns_configuration_free; | |
| 67 | |
| 68 private: | |
| 69 void* handle_; | |
| 70 }; | |
| 71 | |
| 72 const DnsInfoApi& GetDnsInfoApi() { | |
| 73 static base::LazyInstance<DnsInfoApi>::Leaky api = LAZY_INSTANCE_INITIALIZER; | |
| 74 return api.Get(); | |
| 75 } | |
| 76 | |
| 77 struct DnsConfigTDeleter { | |
| 78 inline void operator()(dns_config_t* ptr) const { | |
| 79 if (GetDnsInfoApi().dns_configuration_free) | |
| 80 GetDnsInfoApi().dns_configuration_free(ptr); | |
| 81 } | |
| 82 }; | |
| 83 | |
| 84 } // namespace | |
| 85 #endif // defined(OS_MACOSX) | |
| 86 | 27 |
| 87 namespace net { | 28 namespace net { |
| 88 | 29 |
| 89 #if !defined(OS_ANDROID) | 30 #if !defined(OS_ANDROID) |
| 90 namespace internal { | 31 namespace internal { |
| 91 | 32 |
| 92 namespace { | 33 namespace { |
| 93 | 34 |
| 94 const base::FilePath::CharType* kFilePathHosts = | 35 const base::FilePath::CharType* kFilePathHosts = |
| 95 FILE_PATH_LITERAL("/etc/hosts"); | 36 FILE_PATH_LITERAL("/etc/hosts"); |
| 96 | 37 |
| 97 #if defined(OS_MACOSX) | 38 #if defined(OS_IOS) |
| 98 class ConfigWatcher { | 39 |
| 40 // There is no plublic API to watch the DNS configuration on iOS. |
| 41 class DnsConfigWatcher { |
| 99 public: | 42 public: |
| 100 bool Watch(const base::Callback<void(bool succeeded)>& callback) { | 43 typedef base::Callback<void(bool succeeded)> CallbackType; |
| 101 if (!GetDnsInfoApi().dns_configuration_notify_key) | 44 |
| 102 return false; | 45 bool Watch(const CallbackType& callback) { |
| 103 return watcher_.Watch(GetDnsInfoApi().dns_configuration_notify_key(), | 46 return false; |
| 104 callback); | |
| 105 } | 47 } |
| 48 }; |
| 106 | 49 |
| 107 private: | 50 #elif !defined(OS_MACOSX) |
| 108 NotifyWatcherMac watcher_; | 51 // DnsConfigWatcher for OS_MACOSX is in dns_config_watcher_mac.{hh,cc}. |
| 109 }; | |
| 110 #else | |
| 111 | 52 |
| 112 #ifndef _PATH_RESCONF // Normally defined in <resolv.h> | 53 #ifndef _PATH_RESCONF // Normally defined in <resolv.h> |
| 113 #define _PATH_RESCONF "/etc/resolv.conf" | 54 #define _PATH_RESCONF "/etc/resolv.conf" |
| 114 #endif | 55 #endif |
| 115 | 56 |
| 116 static const base::FilePath::CharType* kFilePathConfig = | 57 static const base::FilePath::CharType* kFilePathConfig = |
| 117 FILE_PATH_LITERAL(_PATH_RESCONF); | 58 FILE_PATH_LITERAL(_PATH_RESCONF); |
| 118 | 59 |
| 119 class ConfigWatcher { | 60 class DnsConfigWatcher { |
| 120 public: | 61 public: |
| 121 typedef base::Callback<void(bool succeeded)> CallbackType; | 62 typedef base::Callback<void(bool succeeded)> CallbackType; |
| 122 | 63 |
| 123 bool Watch(const CallbackType& callback) { | 64 bool Watch(const CallbackType& callback) { |
| 124 callback_ = callback; | 65 callback_ = callback; |
| 125 return watcher_.Watch(base::FilePath(kFilePathConfig), false, | 66 return watcher_.Watch(base::FilePath(kFilePathConfig), false, |
| 126 base::Bind(&ConfigWatcher::OnCallback, | 67 base::Bind(&DnsConfigWatcher::OnCallback, |
| 127 base::Unretained(this))); | 68 base::Unretained(this))); |
| 128 } | 69 } |
| 129 | 70 |
| 130 private: | 71 private: |
| 131 void OnCallback(const base::FilePath& path, bool error) { | 72 void OnCallback(const base::FilePath& path, bool error) { |
| 132 callback_.Run(!error); | 73 callback_.Run(!error); |
| 133 } | 74 } |
| 134 | 75 |
| 135 base::FilePathWatcher watcher_; | 76 base::FilePathWatcher watcher_; |
| 136 CallbackType callback_; | 77 CallbackType callback_; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 158 result = CONFIG_PARSE_POSIX_RES_INIT_FAILED; | 99 result = CONFIG_PARSE_POSIX_RES_INIT_FAILED; |
| 159 } | 100 } |
| 160 // Prefer res_ndestroy where available. | 101 // Prefer res_ndestroy where available. |
| 161 #if defined(OS_MACOSX) || defined(OS_FREEBSD) | 102 #if defined(OS_MACOSX) || defined(OS_FREEBSD) |
| 162 res_ndestroy(&res); | 103 res_ndestroy(&res); |
| 163 #else | 104 #else |
| 164 res_nclose(&res); | 105 res_nclose(&res); |
| 165 #endif | 106 #endif |
| 166 #endif | 107 #endif |
| 167 | 108 |
| 168 #if defined(OS_MACOSX) | 109 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 169 if (!GetDnsInfoApi().dns_configuration_copy) | 110 ConfigParsePosixResult error = DnsConfigWatcher::CheckDnsConfig(); |
| 170 return CONFIG_PARSE_POSIX_NO_DNSINFO; | 111 switch (error) { |
| 171 scoped_ptr<dns_config_t, DnsConfigTDeleter> dns_config( | 112 case CONFIG_PARSE_POSIX_OK: |
| 172 GetDnsInfoApi().dns_configuration_copy()); | 113 break; |
| 173 if (!dns_config) | 114 case CONFIG_PARSE_POSIX_UNHANDLED_OPTIONS: |
| 174 return CONFIG_PARSE_POSIX_NO_DNSINFO; | 115 LOG(WARNING) << "dns_config has unhandled options!"; |
| 175 | 116 config->unhandled_options = true; |
| 176 // TODO(szym): Parse dns_config_t for resolvers rather than res_state. | 117 default: |
| 177 // DnsClient can't handle domain-specific unscoped resolvers. | 118 return error; |
| 178 unsigned num_resolvers = 0; | |
| 179 for (int i = 0; i < dns_config->n_resolver; ++i) { | |
| 180 dns_resolver_t* resolver = dns_config->resolver[i]; | |
| 181 if (!resolver->n_nameserver) | |
| 182 continue; | |
| 183 if (resolver->options && !strcmp(resolver->options, "mdns")) | |
| 184 continue; | |
| 185 ++num_resolvers; | |
| 186 } | 119 } |
| 187 if (num_resolvers > 1) { | 120 #endif // defined(OS_MACOSX) && !defined(OS_IOS) |
| 188 LOG(WARNING) << "dns_config has unhandled options!"; | |
| 189 config->unhandled_options = true; | |
| 190 return CONFIG_PARSE_POSIX_UNHANDLED_OPTIONS; | |
| 191 } | |
| 192 #endif // defined(OS_MACOSX) | |
| 193 // Override timeout value to match default setting on Windows. | 121 // Override timeout value to match default setting on Windows. |
| 194 config->timeout = base::TimeDelta::FromSeconds(kDnsTimeoutSeconds); | 122 config->timeout = base::TimeDelta::FromSeconds(kDnsTimeoutSeconds); |
| 195 return result; | 123 return result; |
| 196 } | 124 } |
| 197 | 125 |
| 198 } // namespace | 126 } // namespace |
| 199 | 127 |
| 200 class DnsConfigServicePosix::Watcher { | 128 class DnsConfigServicePosix::Watcher { |
| 201 public: | 129 public: |
| 202 explicit Watcher(DnsConfigServicePosix* service) | 130 explicit Watcher(DnsConfigServicePosix* service) |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 } | 167 } |
| 240 void OnConfigChangedDelayed(bool succeeded) { | 168 void OnConfigChangedDelayed(bool succeeded) { |
| 241 service_->OnConfigChanged(succeeded); | 169 service_->OnConfigChanged(succeeded); |
| 242 } | 170 } |
| 243 void OnHostsChanged(const base::FilePath& path, bool error) { | 171 void OnHostsChanged(const base::FilePath& path, bool error) { |
| 244 service_->OnHostsChanged(!error); | 172 service_->OnHostsChanged(!error); |
| 245 } | 173 } |
| 246 | 174 |
| 247 base::WeakPtrFactory<Watcher> weak_factory_; | 175 base::WeakPtrFactory<Watcher> weak_factory_; |
| 248 DnsConfigServicePosix* service_; | 176 DnsConfigServicePosix* service_; |
| 249 ConfigWatcher config_watcher_; | 177 DnsConfigWatcher config_watcher_; |
| 250 base::FilePathWatcher hosts_watcher_; | 178 base::FilePathWatcher hosts_watcher_; |
| 251 | 179 |
| 252 DISALLOW_COPY_AND_ASSIGN(Watcher); | 180 DISALLOW_COPY_AND_ASSIGN(Watcher); |
| 253 }; | 181 }; |
| 254 | 182 |
| 255 // A SerialWorker that uses libresolv to initialize res_state and converts | 183 // A SerialWorker that uses libresolv to initialize res_state and converts |
| 256 // it to DnsConfig. | 184 // it to DnsConfig. |
| 257 class DnsConfigServicePosix::ConfigReader : public SerialWorker { | 185 class DnsConfigServicePosix::ConfigReader : public SerialWorker { |
| 258 public: | 186 public: |
| 259 explicit ConfigReader(DnsConfigServicePosix* service) | 187 explicit ConfigReader(DnsConfigServicePosix* service) |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 500 virtual void ReadNow() OVERRIDE {} | 428 virtual void ReadNow() OVERRIDE {} |
| 501 virtual bool StartWatching() OVERRIDE { return false; } | 429 virtual bool StartWatching() OVERRIDE { return false; } |
| 502 }; | 430 }; |
| 503 // static | 431 // static |
| 504 scoped_ptr<DnsConfigService> DnsConfigService::CreateSystemService() { | 432 scoped_ptr<DnsConfigService> DnsConfigService::CreateSystemService() { |
| 505 return scoped_ptr<DnsConfigService>(new StubDnsConfigService()); | 433 return scoped_ptr<DnsConfigService>(new StubDnsConfigService()); |
| 506 } | 434 } |
| 507 #endif | 435 #endif |
| 508 | 436 |
| 509 } // namespace net | 437 } // namespace net |
| OLD | NEW |