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) |
szym
2014/01/16 17:03:32
Add comment:
// DnsConfigWatcher for OS_MACOSX is
| |
108 NotifyWatcherMac watcher_; | |
109 }; | |
110 #else | |
111 | 51 |
112 #ifndef _PATH_RESCONF // Normally defined in <resolv.h> | 52 #ifndef _PATH_RESCONF // Normally defined in <resolv.h> |
113 #define _PATH_RESCONF "/etc/resolv.conf" | 53 #define _PATH_RESCONF "/etc/resolv.conf" |
114 #endif | 54 #endif |
115 | 55 |
116 static const base::FilePath::CharType* kFilePathConfig = | 56 static const base::FilePath::CharType* kFilePathConfig = |
117 FILE_PATH_LITERAL(_PATH_RESCONF); | 57 FILE_PATH_LITERAL(_PATH_RESCONF); |
118 | 58 |
119 class ConfigWatcher { | 59 class DnsConfigWatcher { |
120 public: | 60 public: |
121 typedef base::Callback<void(bool succeeded)> CallbackType; | 61 typedef base::Callback<void(bool succeeded)> CallbackType; |
122 | 62 |
123 bool Watch(const CallbackType& callback) { | 63 bool Watch(const CallbackType& callback) { |
124 callback_ = callback; | 64 callback_ = callback; |
125 return watcher_.Watch(base::FilePath(kFilePathConfig), false, | 65 return watcher_.Watch(base::FilePath(kFilePathConfig), false, |
126 base::Bind(&ConfigWatcher::OnCallback, | 66 base::Bind(&DnsConfigWatcher::OnCallback, |
127 base::Unretained(this))); | 67 base::Unretained(this))); |
128 } | 68 } |
129 | 69 |
130 private: | 70 private: |
131 void OnCallback(const base::FilePath& path, bool error) { | 71 void OnCallback(const base::FilePath& path, bool error) { |
132 callback_.Run(!error); | 72 callback_.Run(!error); |
133 } | 73 } |
134 | 74 |
135 base::FilePathWatcher watcher_; | 75 base::FilePathWatcher watcher_; |
136 CallbackType callback_; | 76 CallbackType callback_; |
(...skipping 21 matching lines...) Expand all Loading... | |
158 result = CONFIG_PARSE_POSIX_RES_INIT_FAILED; | 98 result = CONFIG_PARSE_POSIX_RES_INIT_FAILED; |
159 } | 99 } |
160 // Prefer res_ndestroy where available. | 100 // Prefer res_ndestroy where available. |
161 #if defined(OS_MACOSX) || defined(OS_FREEBSD) | 101 #if defined(OS_MACOSX) || defined(OS_FREEBSD) |
162 res_ndestroy(&res); | 102 res_ndestroy(&res); |
163 #else | 103 #else |
164 res_nclose(&res); | 104 res_nclose(&res); |
165 #endif | 105 #endif |
166 #endif | 106 #endif |
167 | 107 |
168 #if defined(OS_MACOSX) | 108 #if defined(OS_MACOSX) && !defined(OS_IOS) |
169 if (!GetDnsInfoApi().dns_configuration_copy) | 109 ConfigParsePosixResult error = DnsConfigWatcher::GetDnsConfigError(); |
szym
2014/01/16 17:03:32
I'd suggest CheckDnsConfig() instead of GetDnsConf
| |
170 return CONFIG_PARSE_POSIX_NO_DNSINFO; | 110 switch (error) { |
171 scoped_ptr<dns_config_t, DnsConfigTDeleter> dns_config( | 111 case CONFIG_PARSE_POSIX_OK: |
172 GetDnsInfoApi().dns_configuration_copy()); | 112 break; |
173 if (!dns_config) | 113 case CONFIG_PARSE_POSIX_UNHANDLED_OPTIONS: |
174 return CONFIG_PARSE_POSIX_NO_DNSINFO; | 114 LOG(WARNING) << "dns_config has unhandled options!"; |
175 | 115 config->unhandled_options = true; |
176 // TODO(szym): Parse dns_config_t for resolvers rather than res_state. | 116 default: |
177 // DnsClient can't handle domain-specific unscoped resolvers. | 117 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 } | 118 } |
187 if (num_resolvers > 1) { | 119 #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. | 120 // Override timeout value to match default setting on Windows. |
194 config->timeout = base::TimeDelta::FromSeconds(kDnsTimeoutSeconds); | 121 config->timeout = base::TimeDelta::FromSeconds(kDnsTimeoutSeconds); |
195 return result; | 122 return result; |
196 } | 123 } |
197 | 124 |
198 } // namespace | 125 } // namespace |
199 | 126 |
200 class DnsConfigServicePosix::Watcher { | 127 class DnsConfigServicePosix::Watcher { |
201 public: | 128 public: |
202 explicit Watcher(DnsConfigServicePosix* service) | 129 explicit Watcher(DnsConfigServicePosix* service) |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
239 } | 166 } |
240 void OnConfigChangedDelayed(bool succeeded) { | 167 void OnConfigChangedDelayed(bool succeeded) { |
241 service_->OnConfigChanged(succeeded); | 168 service_->OnConfigChanged(succeeded); |
242 } | 169 } |
243 void OnHostsChanged(const base::FilePath& path, bool error) { | 170 void OnHostsChanged(const base::FilePath& path, bool error) { |
244 service_->OnHostsChanged(!error); | 171 service_->OnHostsChanged(!error); |
245 } | 172 } |
246 | 173 |
247 base::WeakPtrFactory<Watcher> weak_factory_; | 174 base::WeakPtrFactory<Watcher> weak_factory_; |
248 DnsConfigServicePosix* service_; | 175 DnsConfigServicePosix* service_; |
249 ConfigWatcher config_watcher_; | 176 DnsConfigWatcher config_watcher_; |
250 base::FilePathWatcher hosts_watcher_; | 177 base::FilePathWatcher hosts_watcher_; |
251 | 178 |
252 DISALLOW_COPY_AND_ASSIGN(Watcher); | 179 DISALLOW_COPY_AND_ASSIGN(Watcher); |
253 }; | 180 }; |
254 | 181 |
255 // A SerialWorker that uses libresolv to initialize res_state and converts | 182 // A SerialWorker that uses libresolv to initialize res_state and converts |
256 // it to DnsConfig. | 183 // it to DnsConfig. |
257 class DnsConfigServicePosix::ConfigReader : public SerialWorker { | 184 class DnsConfigServicePosix::ConfigReader : public SerialWorker { |
258 public: | 185 public: |
259 explicit ConfigReader(DnsConfigServicePosix* service) | 186 explicit ConfigReader(DnsConfigServicePosix* service) |
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
500 virtual void ReadNow() OVERRIDE {} | 427 virtual void ReadNow() OVERRIDE {} |
501 virtual bool StartWatching() OVERRIDE { return false; } | 428 virtual bool StartWatching() OVERRIDE { return false; } |
502 }; | 429 }; |
503 // static | 430 // static |
504 scoped_ptr<DnsConfigService> DnsConfigService::CreateSystemService() { | 431 scoped_ptr<DnsConfigService> DnsConfigService::CreateSystemService() { |
505 return scoped_ptr<DnsConfigService>(new StubDnsConfigService()); | 432 return scoped_ptr<DnsConfigService>(new StubDnsConfigService()); |
506 } | 433 } |
507 #endif | 434 #endif |
508 | 435 |
509 } // namespace net | 436 } // namespace net |
OLD | NEW |