Chromium Code Reviews| 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/file_path.h" | 11 #include "base/file_path.h" |
| 12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/metrics/histogram.h" | |
| 15 #include "base/time.h" | |
| 14 #include "net/base/ip_endpoint.h" | 16 #include "net/base/ip_endpoint.h" |
| 15 #include "net/base/net_util.h" | 17 #include "net/base/net_util.h" |
| 16 #include "net/dns/dns_hosts.h" | 18 #include "net/dns/dns_hosts.h" |
| 17 #include "net/dns/dns_protocol.h" | 19 #include "net/dns/dns_protocol.h" |
| 18 #include "net/dns/serial_worker.h" | 20 #include "net/dns/serial_worker.h" |
| 19 | 21 |
| 20 namespace net { | 22 namespace net { |
| 21 | 23 |
| 22 namespace { | 24 namespace { |
| 23 | 25 |
| 24 #ifndef _PATH_RESCONF // Normally defined in <resolv.h> | 26 #ifndef _PATH_RESCONF // Normally defined in <resolv.h> |
| 25 #define _PATH_RESCONF "/etc/resolv.conf" | 27 #define _PATH_RESCONF "/etc/resolv.conf" |
| 26 #endif | 28 #endif |
| 27 | 29 |
| 28 const FilePath::CharType* kFilePathHosts = FILE_PATH_LITERAL("/etc/hosts"); | 30 const FilePath::CharType* kFilePathHosts = FILE_PATH_LITERAL("/etc/hosts"); |
| 29 | 31 |
| 32 #if !defined(OS_ANDROID) | |
|
cbentzel
2012/06/15 20:29:36
Doesn't seem like the #if !defined(OS_ANDROID) is
szym
2012/06/15 21:22:19
Android compile job fails with "error: 'void net::
| |
| 33 enum ConfigParseStatus { | |
|
cbentzel
2012/06/15 20:29:36
Normally I wouldn't like the ideas of different en
| |
| 34 CONFIG_PARSE_OK = 0, | |
|
cbentzel
2012/06/15 20:29:36
You should make the enum prefixed CONFIG_PARSE_POS
szym
2012/06/15 21:22:19
Done.
| |
| 35 CONFIG_PARSE_RES_INIT_FAILED, | |
| 36 CONFIG_PARSE_RES_INIT_UNSET, | |
| 37 CONFIG_PARSE_BAD_ADDRESS, | |
| 38 CONFIG_PARSE_BAD_EXT_STRUCT, | |
| 39 CONFIG_PARSE_NULL_ADDRESS, | |
| 40 CONFIG_PARSE_NO_NAMESERVERS, | |
| 41 CONFIG_PARSE_MISSING_OPTIONS, | |
| 42 CONFIG_PARSE_UNHANDLED_OPTIONS, | |
| 43 CONFIG_PARSE_MAX // Bounding values for enumeration. | |
| 44 }; | |
| 45 | |
| 46 static void ConfigParseResult(ConfigParseStatus result) { | |
|
cbentzel
2012/06/15 20:29:36
Nit: Should start with a verb, such as RecordConfi
szym
2012/06/15 21:22:19
Done.
| |
| 47 UMA_HISTOGRAM_ENUMERATION("AsyncDNS.ConfigParsePosix", | |
| 48 result, CONFIG_PARSE_MAX); | |
| 49 } | |
| 50 #endif // !defined(OS_ANDROID) | |
| 51 | |
| 30 // A SerialWorker that uses libresolv to initialize res_state and converts | 52 // A SerialWorker that uses libresolv to initialize res_state and converts |
| 31 // it to DnsConfig. | 53 // it to DnsConfig. |
| 32 class ConfigReader : public SerialWorker { | 54 class ConfigReader : public SerialWorker { |
| 33 public: | 55 public: |
| 34 typedef base::Callback<void(const DnsConfig& config)> CallbackType; | 56 typedef base::Callback<void(const DnsConfig& config)> CallbackType; |
| 35 explicit ConfigReader(const CallbackType& callback) | 57 explicit ConfigReader(const CallbackType& callback) |
| 36 : callback_(callback), success_(false) {} | 58 : callback_(callback), success_(false) {} |
| 37 | 59 |
| 38 void DoWork() OVERRIDE { | 60 void DoWork() OVERRIDE { |
| 61 base::TimeTicks start_time = base::TimeTicks::Now(); | |
| 39 success_ = false; | 62 success_ = false; |
| 40 #if defined(OS_ANDROID) | 63 #if defined(OS_ANDROID) |
| 41 NOTIMPLEMENTED(); | 64 NOTIMPLEMENTED(); |
| 42 #elif defined(OS_OPENBSD) | 65 #elif defined(OS_OPENBSD) |
| 43 // Note: res_ninit in glibc always returns 0 and sets RES_INIT. | 66 // Note: res_ninit in glibc always returns 0 and sets RES_INIT. |
| 44 // res_init behaves the same way. | 67 // res_init behaves the same way. |
| 45 memset(&_res, 0, sizeof(_res)); | 68 memset(&_res, 0, sizeof(_res)); |
| 46 if ((res_init() == 0) && (_res.options & RES_INIT)) | 69 if (res_init() == 0) { |
| 47 success_ = internal::ConvertResStateToDnsConfig(_res, &dns_config_); | 70 success_ = internal::ConvertResStateToDnsConfig(_res, &dns_config_); |
| 71 } else { | |
| 72 ConfigParseResult(CONFIG_PARSE_RES_INIT_FAILED); | |
| 73 } | |
| 48 #else // all other OS_POSIX | 74 #else // all other OS_POSIX |
| 49 struct __res_state res; | 75 struct __res_state res; |
| 50 memset(&res, 0, sizeof(res)); | 76 memset(&res, 0, sizeof(res)); |
| 51 if ((res_ninit(&res) == 0) && (res.options & RES_INIT)) | 77 if (res_ninit(&res) == 0) { |
| 52 success_ = internal::ConvertResStateToDnsConfig(res, &dns_config_); | 78 success_ = internal::ConvertResStateToDnsConfig(res, &dns_config_); |
| 53 | 79 } else { |
| 80 ConfigParseResult(CONFIG_PARSE_RES_INIT_FAILED); | |
| 81 } | |
| 54 // Prefer res_ndestroy where available. | 82 // Prefer res_ndestroy where available. |
| 55 #if defined(OS_MACOSX) || defined(OS_FREEBSD) | 83 #if defined(OS_MACOSX) || defined(OS_FREEBSD) |
| 56 res_ndestroy(&res); | 84 res_ndestroy(&res); |
| 57 #else | 85 #else |
| 58 res_nclose(&res); | 86 res_nclose(&res); |
| 59 #endif | 87 #endif |
| 60 #endif | 88 #endif |
| 89 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.ConfigParseResult", success_); | |
| 90 UMA_HISTOGRAM_TIMES("AsyncDNS.ConfigParseDuration", | |
| 91 base::TimeTicks::Now() - start_time); | |
| 61 } | 92 } |
| 62 | 93 |
| 63 void OnWorkFinished() OVERRIDE { | 94 void OnWorkFinished() OVERRIDE { |
| 64 DCHECK(!IsCancelled()); | 95 DCHECK(!IsCancelled()); |
| 65 if (success_) { | 96 if (success_) { |
| 66 callback_.Run(dns_config_); | 97 callback_.Run(dns_config_); |
| 67 } else { | 98 } else { |
| 68 LOG(WARNING) << "Failed to read DnsConfig."; | 99 LOG(WARNING) << "Failed to read DnsConfig."; |
| 69 } | 100 } |
| 70 } | 101 } |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 84 class HostsReader : public SerialWorker { | 115 class HostsReader : public SerialWorker { |
| 85 public: | 116 public: |
| 86 typedef base::Callback<void(const DnsHosts& hosts)> CallbackType; | 117 typedef base::Callback<void(const DnsHosts& hosts)> CallbackType; |
| 87 explicit HostsReader(const CallbackType& callback) | 118 explicit HostsReader(const CallbackType& callback) |
| 88 : path_(kFilePathHosts), callback_(callback), success_(false) {} | 119 : path_(kFilePathHosts), callback_(callback), success_(false) {} |
| 89 | 120 |
| 90 private: | 121 private: |
| 91 virtual ~HostsReader() {} | 122 virtual ~HostsReader() {} |
| 92 | 123 |
| 93 virtual void DoWork() OVERRIDE { | 124 virtual void DoWork() OVERRIDE { |
| 125 base::TimeTicks start_time = base::TimeTicks::Now(); | |
| 94 success_ = ParseHostsFile(path_, &hosts_); | 126 success_ = ParseHostsFile(path_, &hosts_); |
| 127 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.HostParseResult", success_); | |
| 128 UMA_HISTOGRAM_TIMES("AsyncDNS.HostsParseDuration", | |
| 129 base::TimeTicks::Now() - start_time); | |
| 95 } | 130 } |
| 96 | 131 |
| 97 virtual void OnWorkFinished() OVERRIDE { | 132 virtual void OnWorkFinished() OVERRIDE { |
| 98 if (success_) { | 133 if (success_) { |
| 99 callback_.Run(hosts_); | 134 callback_.Run(hosts_); |
| 100 } else { | 135 } else { |
| 101 LOG(WARNING) << "Failed to read DnsHosts."; | 136 LOG(WARNING) << "Failed to read DnsHosts."; |
| 102 } | 137 } |
| 103 } | 138 } |
| 104 | 139 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 147 if (detail & NetworkChangeNotifier::CHANGE_DNS_HOSTS) { | 182 if (detail & NetworkChangeNotifier::CHANGE_DNS_HOSTS) { |
| 148 InvalidateHosts(); | 183 InvalidateHosts(); |
| 149 hosts_reader_->WorkNow(); | 184 hosts_reader_->WorkNow(); |
| 150 } | 185 } |
| 151 } | 186 } |
| 152 | 187 |
| 153 #if !defined(OS_ANDROID) | 188 #if !defined(OS_ANDROID) |
| 154 bool ConvertResStateToDnsConfig(const struct __res_state& res, | 189 bool ConvertResStateToDnsConfig(const struct __res_state& res, |
| 155 DnsConfig* dns_config) { | 190 DnsConfig* dns_config) { |
| 156 CHECK(dns_config != NULL); | 191 CHECK(dns_config != NULL); |
| 157 DCHECK(res.options & RES_INIT); | 192 if (!(res.options & RES_INIT)) { |
| 193 ConfigParseResult(CONFIG_PARSE_RES_INIT_UNSET); | |
| 194 return false; | |
| 195 } | |
| 158 | 196 |
| 159 dns_config->nameservers.clear(); | 197 dns_config->nameservers.clear(); |
| 160 | 198 |
| 161 #if defined(OS_MACOSX) || defined(OS_FREEBSD) | 199 #if defined(OS_MACOSX) || defined(OS_FREEBSD) |
| 162 union res_sockaddr_union addresses[MAXNS]; | 200 union res_sockaddr_union addresses[MAXNS]; |
| 163 int nscount = res_getservers(const_cast<res_state>(&res), addresses, MAXNS); | 201 int nscount = res_getservers(const_cast<res_state>(&res), addresses, MAXNS); |
| 164 DCHECK_GE(nscount, 0); | 202 DCHECK_GE(nscount, 0); |
| 165 DCHECK_LE(nscount, MAXNS); | 203 DCHECK_LE(nscount, MAXNS); |
| 166 for (int i = 0; i < nscount; ++i) { | 204 for (int i = 0; i < nscount; ++i) { |
| 167 IPEndPoint ipe; | 205 IPEndPoint ipe; |
| 168 if (!ipe.FromSockAddr( | 206 if (!ipe.FromSockAddr( |
| 169 reinterpret_cast<const struct sockaddr*>(&addresses[i]), | 207 reinterpret_cast<const struct sockaddr*>(&addresses[i]), |
| 170 sizeof addresses[i])) { | 208 sizeof addresses[i])) { |
| 209 ConfigParseResult(CONFIG_PARSE_BAD_ADDRESS); | |
| 171 return false; | 210 return false; |
| 172 } | 211 } |
| 173 dns_config->nameservers.push_back(ipe); | 212 dns_config->nameservers.push_back(ipe); |
| 174 } | 213 } |
| 175 #elif defined(OS_LINUX) | 214 #elif defined(OS_LINUX) |
| 176 COMPILE_ASSERT(arraysize(res.nsaddr_list) >= MAXNS && | 215 COMPILE_ASSERT(arraysize(res.nsaddr_list) >= MAXNS && |
| 177 arraysize(res._u._ext.nsaddrs) >= MAXNS, | 216 arraysize(res._u._ext.nsaddrs) >= MAXNS, |
| 178 incompatible_libresolv_res_state); | 217 incompatible_libresolv_res_state); |
| 179 DCHECK_LE(res.nscount, MAXNS); | 218 DCHECK_LE(res.nscount, MAXNS); |
| 180 // Initially, glibc stores IPv6 in |_ext.nsaddrs| and IPv4 in |nsaddr_list|. | 219 // Initially, glibc stores IPv6 in |_ext.nsaddrs| and IPv4 in |nsaddr_list|. |
| 181 // In res_send.c:res_nsend, it merges |nsaddr_list| into |nsaddrs|, | 220 // In res_send.c:res_nsend, it merges |nsaddr_list| into |nsaddrs|, |
| 182 // but we have to combine the two arrays ourselves. | 221 // but we have to combine the two arrays ourselves. |
| 183 for (int i = 0; i < res.nscount; ++i) { | 222 for (int i = 0; i < res.nscount; ++i) { |
| 184 IPEndPoint ipe; | 223 IPEndPoint ipe; |
| 185 const struct sockaddr* addr = NULL; | 224 const struct sockaddr* addr = NULL; |
| 186 size_t addr_len = 0; | 225 size_t addr_len = 0; |
| 187 if (res.nsaddr_list[i].sin_family) { // The indicator used by res_nsend. | 226 if (res.nsaddr_list[i].sin_family) { // The indicator used by res_nsend. |
| 188 addr = reinterpret_cast<const struct sockaddr*>(&res.nsaddr_list[i]); | 227 addr = reinterpret_cast<const struct sockaddr*>(&res.nsaddr_list[i]); |
| 189 addr_len = sizeof res.nsaddr_list[i]; | 228 addr_len = sizeof res.nsaddr_list[i]; |
| 190 } else if (res._u._ext.nsaddrs[i] != NULL) { | 229 } else if (res._u._ext.nsaddrs[i] != NULL) { |
| 191 addr = reinterpret_cast<const struct sockaddr*>(res._u._ext.nsaddrs[i]); | 230 addr = reinterpret_cast<const struct sockaddr*>(res._u._ext.nsaddrs[i]); |
| 192 addr_len = sizeof *res._u._ext.nsaddrs[i]; | 231 addr_len = sizeof *res._u._ext.nsaddrs[i]; |
| 193 } else { | 232 } else { |
| 233 ConfigParseResult(CONFIG_PARSE_BAD_EXT_STRUCT); | |
| 194 return false; | 234 return false; |
| 195 } | 235 } |
| 196 if (!ipe.FromSockAddr(addr, addr_len)) | 236 if (!ipe.FromSockAddr(addr, addr_len)) { |
| 237 ConfigParseResult(CONFIG_PARSE_BAD_ADDRESS); | |
| 197 return false; | 238 return false; |
| 239 } | |
| 198 dns_config->nameservers.push_back(ipe); | 240 dns_config->nameservers.push_back(ipe); |
| 199 } | 241 } |
| 200 #else // !(defined(OS_LINUX) || defined(OS_MACOSX) || defined(OS_FREEBSD)) | 242 #else // !(defined(OS_LINUX) || defined(OS_MACOSX) || defined(OS_FREEBSD)) |
| 201 DCHECK_LE(res.nscount, MAXNS); | 243 DCHECK_LE(res.nscount, MAXNS); |
| 202 for (int i = 0; i < res.nscount; ++i) { | 244 for (int i = 0; i < res.nscount; ++i) { |
| 203 IPEndPoint ipe; | 245 IPEndPoint ipe; |
| 204 if (!ipe.FromSockAddr( | 246 if (!ipe.FromSockAddr( |
| 205 reinterpret_cast<const struct sockaddr*>(&res.nsaddr_list[i]), | 247 reinterpret_cast<const struct sockaddr*>(&res.nsaddr_list[i]), |
| 206 sizeof res.nsaddr_list[i])) { | 248 sizeof res.nsaddr_list[i])) { |
| 249 ConfigParseResult(CONFIG_PARSE_BAD_ADDRESS); | |
| 207 return false; | 250 return false; |
| 208 } | 251 } |
| 209 dns_config->nameservers.push_back(ipe); | 252 dns_config->nameservers.push_back(ipe); |
| 210 } | 253 } |
| 211 #endif | 254 #endif |
| 212 | 255 |
| 213 dns_config->search.clear(); | 256 dns_config->search.clear(); |
| 214 for (int i = 0; (i < MAXDNSRCH) && res.dnsrch[i]; ++i) { | 257 for (int i = 0; (i < MAXDNSRCH) && res.dnsrch[i]; ++i) { |
| 215 dns_config->search.push_back(std::string(res.dnsrch[i])); | 258 dns_config->search.push_back(std::string(res.dnsrch[i])); |
| 216 } | 259 } |
| 217 | 260 |
| 218 dns_config->ndots = res.ndots; | 261 dns_config->ndots = res.ndots; |
| 219 dns_config->timeout = base::TimeDelta::FromSeconds(res.retrans); | 262 dns_config->timeout = base::TimeDelta::FromSeconds(res.retrans); |
| 220 dns_config->attempts = res.retry; | 263 dns_config->attempts = res.retry; |
| 221 #if defined(RES_ROTATE) | 264 #if defined(RES_ROTATE) |
| 222 dns_config->rotate = res.options & RES_ROTATE; | 265 dns_config->rotate = res.options & RES_ROTATE; |
| 223 #endif | 266 #endif |
| 224 dns_config->edns0 = res.options & RES_USE_EDNS0; | 267 dns_config->edns0 = res.options & RES_USE_EDNS0; |
| 225 | 268 |
| 269 // The current implementation assumes these options are set. They normally | |
| 270 // cannot be overwritten by /etc/resolv.conf | |
| 271 unsigned kRequiredOptions = RES_RECURSE | RES_DEFNAMES | RES_DNSRCH; | |
| 272 if ((res.options & kRequiredOptions) != kRequiredOptions) { | |
| 273 ConfigParseResult(CONFIG_PARSE_MISSING_OPTIONS); | |
| 274 return false; | |
| 275 } | |
| 276 | |
| 277 unsigned kUnhandledOptions = RES_USEVC | RES_IGNTC | RES_USE_DNSSEC; | |
| 278 if (res.options & kUnhandledOptions) { | |
| 279 ConfigParseResult(CONFIG_PARSE_UNHANDLED_OPTIONS); | |
| 280 return false; | |
| 281 } | |
| 282 | |
| 283 if (dns_config->nameservers.empty()) { | |
| 284 ConfigParseResult(CONFIG_PARSE_NO_NAMESERVERS); | |
| 285 return false; | |
| 286 } | |
| 287 | |
| 226 // If any name server is 0.0.0.0, assume the configuration is invalid. | 288 // If any name server is 0.0.0.0, assume the configuration is invalid. |
| 227 // TODO(szym): Measure how often this happens. http://crbug.com/125599 | 289 // TODO(szym): Measure how often this happens. http://crbug.com/125599 |
| 228 const IPAddressNumber kEmptyAddress(kIPv4AddressSize); | 290 const IPAddressNumber kEmptyAddress(kIPv4AddressSize); |
| 229 for (unsigned i = 0; i < dns_config->nameservers.size(); ++i) | 291 for (unsigned i = 0; i < dns_config->nameservers.size(); ++i) { |
| 230 if (dns_config->nameservers[i].address() == kEmptyAddress) | 292 if (dns_config->nameservers[i].address() == kEmptyAddress) { |
| 293 ConfigParseResult(CONFIG_PARSE_NULL_ADDRESS); | |
| 231 return false; | 294 return false; |
| 295 } | |
| 296 } | |
| 297 ConfigParseResult(CONFIG_PARSE_OK); | |
| 232 return true; | 298 return true; |
| 233 } | 299 } |
| 234 #endif // !defined(OS_ANDROID) | 300 #endif // !defined(OS_ANDROID) |
| 235 | 301 |
| 236 } // namespace internal | 302 } // namespace internal |
| 237 | 303 |
| 238 // static | 304 // static |
| 239 scoped_ptr<DnsConfigService> DnsConfigService::CreateSystemService() { | 305 scoped_ptr<DnsConfigService> DnsConfigService::CreateSystemService() { |
| 240 return scoped_ptr<DnsConfigService>(new internal::DnsConfigServicePosix()); | 306 return scoped_ptr<DnsConfigService>(new internal::DnsConfigServicePosix()); |
| 241 } | 307 } |
| 242 | 308 |
| 243 } // namespace net | 309 } // namespace net |
| OLD | NEW |