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" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 15 | #include "shill/logging.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 | |
Ben Chan | bbdef5f | 2012-04-23 13:58:15 -0700 | [diff] [blame] | 23 | namespace { |
| 24 | base::LazyInstance<Resolver> g_resolver = LAZY_INSTANCE_INITIALIZER; |
| 25 | } // namespace |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 26 | |
Paul Stewart | bf66761 | 2012-06-29 14:49:54 -0700 | [diff] [blame] | 27 | const char Resolver::kDefaultShortTimeoutTechnologies[] = "ethernet,wifi"; |
| 28 | const char Resolver::kDefaultTimeoutOptions[] = |
| 29 | "options single-request timeout:1 attempts:3"; |
| 30 | const char Resolver::kShortTimeoutOptions[] = |
| 31 | "options single-request timeout-ms:300 attempts:15"; |
| 32 | |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 33 | Resolver::Resolver() {} |
| 34 | |
| 35 | Resolver::~Resolver() {} |
| 36 | |
| 37 | Resolver* Resolver::GetInstance() { |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 38 | return g_resolver.Pointer(); |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Paul Stewart | bf66761 | 2012-06-29 14:49:54 -0700 | [diff] [blame] | 41 | bool Resolver::SetDNSFromIPConfig(const IPConfigRefPtr &ipconfig, |
| 42 | TimeoutParameters timeout) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 43 | SLOG(Resolver, 2) << __func__; |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 44 | |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 45 | CHECK(!path_.empty()); |
| 46 | |
| 47 | const IPConfig::Properties &props = ipconfig->properties(); |
| 48 | |
Paul Stewart | bf66761 | 2012-06-29 14:49:54 -0700 | [diff] [blame] | 49 | return SetDNSFromLists(props.dns_servers, props.domain_search, timeout); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | bool Resolver::SetDNSFromLists(const std::vector<std::string> &dns_servers, |
Paul Stewart | bf66761 | 2012-06-29 14:49:54 -0700 | [diff] [blame] | 53 | const std::vector<std::string> &domain_search, |
| 54 | TimeoutParameters timeout) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 55 | SLOG(Resolver, 2) << __func__; |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 56 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 57 | if (dns_servers.empty() && domain_search.empty()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 58 | SLOG(Resolver, 2) << "DNS list is empty"; |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 59 | return ClearDNS(); |
| 60 | } |
| 61 | |
| 62 | vector<string> lines; |
| 63 | vector<string>::const_iterator iter; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 64 | for (iter = dns_servers.begin(); |
| 65 | iter != dns_servers.end(); ++iter) { |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 66 | lines.push_back("nameserver " + *iter); |
| 67 | } |
| 68 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 69 | if (!domain_search.empty()) { |
| 70 | lines.push_back("search " + JoinString(domain_search, ' ')); |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Thieu Le | 3bd0faa | 2012-02-15 15:44:46 -0800 | [diff] [blame] | 73 | // Send queries one-at-a-time, rather than parallelizing IPv4 |
Paul Stewart | abc41aa | 2012-04-07 13:15:37 -0700 | [diff] [blame] | 74 | // and IPv6 queries for a single host. Also override the default |
Paul Stewart | bf66761 | 2012-06-29 14:49:54 -0700 | [diff] [blame] | 75 | // 5-second request timeout and 2 retries. |
| 76 | if (timeout == kDefaultTimeout) { |
| 77 | lines.push_back(kDefaultTimeoutOptions); |
| 78 | } else if (timeout == kShortTimeout) { |
| 79 | lines.push_back(kShortTimeoutOptions); |
| 80 | } else { |
| 81 | NOTIMPLEMENTED() << "Unknown Resolver timeout parameters"; |
| 82 | } |
Thieu Le | 3bd0faa | 2012-02-15 15:44:46 -0800 | [diff] [blame] | 83 | |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 84 | // Newline at end of file |
| 85 | lines.push_back(""); |
| 86 | |
| 87 | string contents = JoinString(lines, '\n'); |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 88 | |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 89 | SLOG(Resolver, 2) << "Writing DNS out to " << path_.value(); |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 90 | int count = file_util::WriteFile(path_, contents.c_str(), contents.size()); |
| 91 | |
| 92 | return count == static_cast<int>(contents.size()); |
| 93 | } |
| 94 | |
| 95 | bool Resolver::ClearDNS() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 96 | SLOG(Resolver, 2) << __func__; |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 97 | |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 98 | CHECK(!path_.empty()); |
| 99 | |
| 100 | return file_util::Delete(path_, false); |
| 101 | } |
| 102 | |
| 103 | } // namespace shill |