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