Thieu Le | 3bd0faa | 2012-02-15 15:44:46 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 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 "shill/resolver.h" |
| 6 | |
| 7 | #include <string> |
| 8 | #include <vector> |
| 9 | |
| 10 | #include <base/file_util.h> |
| 11 | #include <base/string_util.h> |
| 12 | #include <base/stringprintf.h> |
| 13 | |
| 14 | #include "shill/ipconfig.h" |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 15 | #include "shill/scope_logger.h" |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 16 | |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 17 | using base::StringPrintf; |
| 18 | using std::string; |
| 19 | using std::vector; |
| 20 | |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 21 | namespace shill { |
| 22 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 23 | // TODO(ers): not using LAZY_INSTANCE_INITIALIZER |
| 24 | // because of http://crbug.com/114828 |
| 25 | static base::LazyInstance<Resolver> g_resolver = {0, {{0}}}; |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 26 | |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 27 | Resolver::Resolver() {} |
| 28 | |
| 29 | Resolver::~Resolver() {} |
| 30 | |
| 31 | Resolver* Resolver::GetInstance() { |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 32 | return g_resolver.Pointer(); |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 35 | bool Resolver::SetDNSFromIPConfig(const IPConfigRefPtr &ipconfig) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 36 | SLOG(Resolver, 2) << __func__; |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 37 | |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 38 | CHECK(!path_.empty()); |
| 39 | |
| 40 | const IPConfig::Properties &props = ipconfig->properties(); |
| 41 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 42 | return SetDNSFromLists(props.dns_servers, props.domain_search); |
| 43 | } |
| 44 | |
| 45 | bool Resolver::SetDNSFromLists(const std::vector<std::string> &dns_servers, |
| 46 | const std::vector<std::string> &domain_search) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 47 | SLOG(Resolver, 2) << __func__; |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 48 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 49 | if (dns_servers.empty() && domain_search.empty()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 50 | SLOG(Resolver, 2) << "DNS list is empty"; |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 51 | return ClearDNS(); |
| 52 | } |
| 53 | |
| 54 | vector<string> lines; |
| 55 | vector<string>::const_iterator iter; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 56 | for (iter = dns_servers.begin(); |
| 57 | iter != dns_servers.end(); ++iter) { |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 58 | lines.push_back("nameserver " + *iter); |
| 59 | } |
| 60 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 61 | if (!domain_search.empty()) { |
| 62 | lines.push_back("search " + JoinString(domain_search, ' ')); |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Thieu Le | 3bd0faa | 2012-02-15 15:44:46 -0800 | [diff] [blame] | 65 | // Send queries one-at-a-time, rather than parallelizing IPv4 |
Paul Stewart | abc41aa | 2012-04-07 13:15:37 -0700 | [diff] [blame] | 66 | // and IPv6 queries for a single host. Also override the default |
| 67 | // 5-second request timeout and use a 1-second tiemout instead. |
| 68 | lines.push_back("options single-request timeout:1"); |
Thieu Le | 3bd0faa | 2012-02-15 15:44:46 -0800 | [diff] [blame] | 69 | |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 70 | // Newline at end of file |
| 71 | lines.push_back(""); |
| 72 | |
| 73 | string contents = JoinString(lines, '\n'); |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 74 | |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 75 | SLOG(Resolver, 2) << "Writing DNS out to " << path_.value(); |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 76 | int count = file_util::WriteFile(path_, contents.c_str(), contents.size()); |
| 77 | |
| 78 | return count == static_cast<int>(contents.size()); |
| 79 | } |
| 80 | |
| 81 | bool Resolver::ClearDNS() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 82 | SLOG(Resolver, 2) << __func__; |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 83 | |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 84 | CHECK(!path_.empty()); |
| 85 | |
| 86 | return file_util::Delete(path_, false); |
| 87 | } |
| 88 | |
| 89 | } // namespace shill |