blob: f45c337048f1595898d32a58781c16cde286cd87 [file] [log] [blame]
Thieu Le3bd0faa2012-02-15 15:44:46 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Paul Stewartb6063942011-08-05 10:17:29 -07002// 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 Stewart4d5efb72012-09-17 12:24:34 -07007#include <algorithm>
Paul Stewartb6063942011-08-05 10:17:29 -07008#include <string>
9#include <vector>
10
Ben Chan11c213f2014-09-05 08:21:06 -070011#include <base/files/file_util.h>
Ben Chana0ddf462014-02-06 11:32:42 -080012#include <base/strings/string_util.h>
13#include <base/strings/stringprintf.h>
Paul Stewartb6063942011-08-05 10:17:29 -070014
15#include "shill/ipconfig.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070016#include "shill/logging.h"
Paul Stewartb6063942011-08-05 10:17:29 -070017
Paul Stewartb6063942011-08-05 10:17:29 -070018using base::StringPrintf;
19using std::string;
20using std::vector;
21
Paul Stewart0d2ada32011-08-09 17:01:57 -070022namespace shill {
23
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070024namespace Logging {
25static auto kModuleLogScope = ScopeLogger::kResolver;
26static string ObjectID(Resolver *r) { return "(resolver)"; }
27}
28
Ben Chanbbdef5f2012-04-23 13:58:15 -070029namespace {
30base::LazyInstance<Resolver> g_resolver = LAZY_INSTANCE_INITIALIZER;
31} // namespace
Paul Stewart0d2ada32011-08-09 17:01:57 -070032
Paul Stewart4d5efb72012-09-17 12:24:34 -070033const char Resolver::kDefaultIgnoredSearchList[] = "gateway.2wire.net";
Paul Stewartbf667612012-06-29 14:49:54 -070034
Paul Stewartb6063942011-08-05 10:17:29 -070035Resolver::Resolver() {}
36
37Resolver::~Resolver() {}
38
39Resolver* Resolver::GetInstance() {
Paul Stewart0d2ada32011-08-09 17:01:57 -070040 return g_resolver.Pointer();
Paul Stewartb6063942011-08-05 10:17:29 -070041}
42
mukesh agrawal23ac6b72013-01-31 18:52:37 -080043bool Resolver::SetDNSFromLists(const std::vector<std::string> &dns_servers,
44 const std::vector<std::string> &domain_search) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070045 SLOG(this, 2) << __func__;
Paul Stewarte6132022011-08-16 09:11:02 -070046
Paul Stewartdd60e452011-08-08 11:38:36 -070047 if (dns_servers.empty() && domain_search.empty()) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070048 SLOG(this, 2) << "DNS list is empty";
Paul Stewartb6063942011-08-05 10:17:29 -070049 return ClearDNS();
50 }
51
52 vector<string> lines;
53 vector<string>::const_iterator iter;
Paul Stewartdd60e452011-08-08 11:38:36 -070054 for (iter = dns_servers.begin();
55 iter != dns_servers.end(); ++iter) {
Paul Stewartb6063942011-08-05 10:17:29 -070056 lines.push_back("nameserver " + *iter);
57 }
58
Paul Stewart4d5efb72012-09-17 12:24:34 -070059 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 Stewartb6063942011-08-05 10:17:29 -070071 }
72
mukesh agrawalf01ddd42013-01-31 18:59:42 -080073 // - 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 Le3bd0faa2012-02-15 15:44:46 -080085
Paul Stewartb6063942011-08-05 10:17:29 -070086 // Newline at end of file
87 lines.push_back("");
88
89 string contents = JoinString(lines, '\n');
Paul Stewarte6132022011-08-16 09:11:02 -070090
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070091 SLOG(this, 2) << "Writing DNS out to " << path_.value();
Ben Chan6fbf64f2014-05-21 18:07:01 -070092 int count = base::WriteFile(path_, contents.c_str(), contents.size());
Paul Stewartb6063942011-08-05 10:17:29 -070093
94 return count == static_cast<int>(contents.size());
95}
96
97bool Resolver::ClearDNS() {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070098 SLOG(this, 2) << __func__;
Paul Stewarte6132022011-08-16 09:11:02 -070099
Paul Stewartb6063942011-08-05 10:17:29 -0700100 CHECK(!path_.empty());
101
Ben Chana0ddf462014-02-06 11:32:42 -0800102 return base::DeleteFile(path_, false);
Paul Stewartb6063942011-08-05 10:17:29 -0700103}
104
105} // namespace shill