blob: a3e2fa46f50d677a32a354af9afcec5fc303b6d6 [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
11#include <base/file_util.h>
12#include <base/string_util.h>
13#include <base/stringprintf.h>
14
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
Ben Chanbbdef5f2012-04-23 13:58:15 -070024namespace {
25base::LazyInstance<Resolver> g_resolver = LAZY_INSTANCE_INITIALIZER;
26} // namespace
Paul Stewart0d2ada32011-08-09 17:01:57 -070027
Paul Stewart4d5efb72012-09-17 12:24:34 -070028const char Resolver::kDefaultIgnoredSearchList[] = "gateway.2wire.net";
Paul Stewartbf667612012-06-29 14:49:54 -070029
Paul Stewartb6063942011-08-05 10:17:29 -070030Resolver::Resolver() {}
31
32Resolver::~Resolver() {}
33
34Resolver* Resolver::GetInstance() {
Paul Stewart0d2ada32011-08-09 17:01:57 -070035 return g_resolver.Pointer();
Paul Stewartb6063942011-08-05 10:17:29 -070036}
37
mukesh agrawal23ac6b72013-01-31 18:52:37 -080038bool Resolver::SetDNSFromLists(const std::vector<std::string> &dns_servers,
39 const std::vector<std::string> &domain_search) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070040 SLOG(Resolver, 2) << __func__;
Paul Stewarte6132022011-08-16 09:11:02 -070041
Paul Stewartdd60e452011-08-08 11:38:36 -070042 if (dns_servers.empty() && domain_search.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070043 SLOG(Resolver, 2) << "DNS list is empty";
Paul Stewartb6063942011-08-05 10:17:29 -070044 return ClearDNS();
45 }
46
47 vector<string> lines;
48 vector<string>::const_iterator iter;
Paul Stewartdd60e452011-08-08 11:38:36 -070049 for (iter = dns_servers.begin();
50 iter != dns_servers.end(); ++iter) {
Paul Stewartb6063942011-08-05 10:17:29 -070051 lines.push_back("nameserver " + *iter);
52 }
53
Paul Stewart4d5efb72012-09-17 12:24:34 -070054 vector<string> filtered_domain_search;
55 for (iter = domain_search.begin();
56 iter != domain_search.end(); ++iter) {
57 if (std::find(ignored_search_list_.begin(),
58 ignored_search_list_.end(),
59 *iter) == ignored_search_list_.end()) {
60 filtered_domain_search.push_back(*iter);
61 }
62 }
63
64 if (!filtered_domain_search.empty()) {
65 lines.push_back("search " + JoinString(filtered_domain_search, ' '));
Paul Stewartb6063942011-08-05 10:17:29 -070066 }
67
mukesh agrawalf01ddd42013-01-31 18:59:42 -080068 // - Send queries one-at-a-time, rather than parallelizing IPv4
69 // and IPv6 queries for a single host.
70 // - Override the default 5-second request timeout and use a
71 // 1-second timeout instead. (NOTE: Chrome's ADNS will use
72 // one second, regardless of what we put here.)
73 // - Allow 5 attempts, rather than the default of 2.
74 // - For glibc, the worst case number of queries will be
75 // attempts * count(servers) * (count(search domains)+1)
76 // - For Chrome, the worst case number of queries will be
77 // attempts * count(servers) + 3 * glibc
78 // See crbug.com/224756 for supporting data.
79 lines.push_back("options single-request timeout:1 attempts:5");
Thieu Le3bd0faa2012-02-15 15:44:46 -080080
Paul Stewartb6063942011-08-05 10:17:29 -070081 // Newline at end of file
82 lines.push_back("");
83
84 string contents = JoinString(lines, '\n');
Paul Stewarte6132022011-08-16 09:11:02 -070085
Ben Chanfad4a0b2012-04-18 15:49:59 -070086 SLOG(Resolver, 2) << "Writing DNS out to " << path_.value();
Paul Stewartb6063942011-08-05 10:17:29 -070087 int count = file_util::WriteFile(path_, contents.c_str(), contents.size());
88
89 return count == static_cast<int>(contents.size());
90}
91
92bool Resolver::ClearDNS() {
Ben Chanfad4a0b2012-04-18 15:49:59 -070093 SLOG(Resolver, 2) << __func__;
Paul Stewarte6132022011-08-16 09:11:02 -070094
Paul Stewartb6063942011-08-05 10:17:29 -070095 CHECK(!path_.empty());
96
97 return file_util::Delete(path_, false);
98}
99
100} // namespace shill