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 | |
Paul Stewart | 4d5efb7 | 2012-09-17 12:24:34 -0700 | [diff] [blame] | 7 | #include <algorithm> |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
Ben Chan | 11c213f | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 11 | #include <base/files/file_util.h> |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 12 | #include <base/strings/string_util.h> |
| 13 | #include <base/strings/stringprintf.h> |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 14 | |
| 15 | #include "shill/ipconfig.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 16 | #include "shill/logging.h" |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 17 | |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 18 | using base::StringPrintf; |
| 19 | using std::string; |
| 20 | using std::vector; |
| 21 | |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 22 | namespace shill { |
| 23 | |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 24 | namespace Logging { |
| 25 | static auto kModuleLogScope = ScopeLogger::kResolver; |
| 26 | static string ObjectID(Resolver *r) { return "(resolver)"; } |
| 27 | } |
| 28 | |
Ben Chan | bbdef5f | 2012-04-23 13:58:15 -0700 | [diff] [blame] | 29 | namespace { |
| 30 | base::LazyInstance<Resolver> g_resolver = LAZY_INSTANCE_INITIALIZER; |
| 31 | } // namespace |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 32 | |
Paul Stewart | 4d5efb7 | 2012-09-17 12:24:34 -0700 | [diff] [blame] | 33 | const char Resolver::kDefaultIgnoredSearchList[] = "gateway.2wire.net"; |
Paul Stewart | bf66761 | 2012-06-29 14:49:54 -0700 | [diff] [blame] | 34 | |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 35 | Resolver::Resolver() {} |
| 36 | |
| 37 | Resolver::~Resolver() {} |
| 38 | |
| 39 | Resolver* Resolver::GetInstance() { |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 40 | return g_resolver.Pointer(); |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 41 | } |
| 42 | |
mukesh agrawal | 23ac6b7 | 2013-01-31 18:52:37 -0800 | [diff] [blame] | 43 | bool Resolver::SetDNSFromLists(const std::vector<std::string> &dns_servers, |
| 44 | const std::vector<std::string> &domain_search) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 45 | SLOG(this, 2) << __func__; |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 46 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 47 | if (dns_servers.empty() && domain_search.empty()) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 48 | SLOG(this, 2) << "DNS list is empty"; |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 49 | return ClearDNS(); |
| 50 | } |
| 51 | |
| 52 | vector<string> lines; |
| 53 | vector<string>::const_iterator iter; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 54 | for (iter = dns_servers.begin(); |
| 55 | iter != dns_servers.end(); ++iter) { |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 56 | lines.push_back("nameserver " + *iter); |
| 57 | } |
| 58 | |
Paul Stewart | 4d5efb7 | 2012-09-17 12:24:34 -0700 | [diff] [blame] | 59 | vector<string> filtered_domain_search; |
| 60 | for (iter = domain_search.begin(); |
| 61 | iter != domain_search.end(); ++iter) { |
| 62 | if (std::find(ignored_search_list_.begin(), |
| 63 | ignored_search_list_.end(), |
| 64 | *iter) == ignored_search_list_.end()) { |
| 65 | filtered_domain_search.push_back(*iter); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | if (!filtered_domain_search.empty()) { |
| 70 | lines.push_back("search " + JoinString(filtered_domain_search, ' ')); |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 71 | } |
| 72 | |
mukesh agrawal | f01ddd4 | 2013-01-31 18:59:42 -0800 | [diff] [blame] | 73 | // - Send queries one-at-a-time, rather than parallelizing IPv4 |
| 74 | // and IPv6 queries for a single host. |
| 75 | // - Override the default 5-second request timeout and use a |
| 76 | // 1-second timeout instead. (NOTE: Chrome's ADNS will use |
| 77 | // one second, regardless of what we put here.) |
| 78 | // - Allow 5 attempts, rather than the default of 2. |
| 79 | // - For glibc, the worst case number of queries will be |
| 80 | // attempts * count(servers) * (count(search domains)+1) |
| 81 | // - For Chrome, the worst case number of queries will be |
| 82 | // attempts * count(servers) + 3 * glibc |
| 83 | // See crbug.com/224756 for supporting data. |
| 84 | lines.push_back("options single-request timeout:1 attempts:5"); |
Thieu Le | 3bd0faa | 2012-02-15 15:44:46 -0800 | [diff] [blame] | 85 | |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 86 | // Newline at end of file |
| 87 | lines.push_back(""); |
| 88 | |
| 89 | string contents = JoinString(lines, '\n'); |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 90 | |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 91 | SLOG(this, 2) << "Writing DNS out to " << path_.value(); |
Ben Chan | 6fbf64f | 2014-05-21 18:07:01 -0700 | [diff] [blame] | 92 | int count = base::WriteFile(path_, contents.c_str(), contents.size()); |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 93 | |
| 94 | return count == static_cast<int>(contents.size()); |
| 95 | } |
| 96 | |
| 97 | bool Resolver::ClearDNS() { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 98 | SLOG(this, 2) << __func__; |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 99 | |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 100 | CHECK(!path_.empty()); |
| 101 | |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 102 | return base::DeleteFile(path_, false); |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | } // namespace shill |