blob: e10c5874b99436962bd9efe000e32f77e7b73a79 [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
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 Wileyb691efd2012-08-09 13:51:51 -070015#include "shill/logging.h"
Paul Stewartb6063942011-08-05 10:17:29 -070016
Paul Stewartb6063942011-08-05 10:17:29 -070017using base::StringPrintf;
18using std::string;
19using std::vector;
20
Paul Stewart0d2ada32011-08-09 17:01:57 -070021namespace shill {
22
Ben Chanbbdef5f2012-04-23 13:58:15 -070023namespace {
24base::LazyInstance<Resolver> g_resolver = LAZY_INSTANCE_INITIALIZER;
25} // namespace
Paul Stewart0d2ada32011-08-09 17:01:57 -070026
Paul Stewartbf667612012-06-29 14:49:54 -070027const char Resolver::kDefaultShortTimeoutTechnologies[] = "ethernet,wifi";
28const char Resolver::kDefaultTimeoutOptions[] =
29 "options single-request timeout:1 attempts:3";
30const char Resolver::kShortTimeoutOptions[] =
31 "options single-request timeout-ms:300 attempts:15";
32
Paul Stewartb6063942011-08-05 10:17:29 -070033Resolver::Resolver() {}
34
35Resolver::~Resolver() {}
36
37Resolver* Resolver::GetInstance() {
Paul Stewart0d2ada32011-08-09 17:01:57 -070038 return g_resolver.Pointer();
Paul Stewartb6063942011-08-05 10:17:29 -070039}
40
Paul Stewartdd60e452011-08-08 11:38:36 -070041bool Resolver::SetDNSFromLists(const std::vector<std::string> &dns_servers,
Paul Stewartbf667612012-06-29 14:49:54 -070042 const std::vector<std::string> &domain_search,
43 TimeoutParameters timeout) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070044 SLOG(Resolver, 2) << __func__;
Paul Stewarte6132022011-08-16 09:11:02 -070045
Paul Stewart6f65c0b2012-09-11 14:57:32 -070046 CHECK(!path_.empty());
47
Paul Stewartdd60e452011-08-08 11:38:36 -070048 if (dns_servers.empty() && domain_search.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070049 SLOG(Resolver, 2) << "DNS list is empty";
Paul Stewartb6063942011-08-05 10:17:29 -070050 return ClearDNS();
51 }
52
53 vector<string> lines;
54 vector<string>::const_iterator iter;
Paul Stewartdd60e452011-08-08 11:38:36 -070055 for (iter = dns_servers.begin();
56 iter != dns_servers.end(); ++iter) {
Paul Stewartb6063942011-08-05 10:17:29 -070057 lines.push_back("nameserver " + *iter);
58 }
59
Paul Stewartdd60e452011-08-08 11:38:36 -070060 if (!domain_search.empty()) {
61 lines.push_back("search " + JoinString(domain_search, ' '));
Paul Stewartb6063942011-08-05 10:17:29 -070062 }
63
Thieu Le3bd0faa2012-02-15 15:44:46 -080064 // Send queries one-at-a-time, rather than parallelizing IPv4
Paul Stewartabc41aa2012-04-07 13:15:37 -070065 // and IPv6 queries for a single host. Also override the default
Paul Stewartbf667612012-06-29 14:49:54 -070066 // 5-second request timeout and 2 retries.
67 if (timeout == kDefaultTimeout) {
68 lines.push_back(kDefaultTimeoutOptions);
69 } else if (timeout == kShortTimeout) {
70 lines.push_back(kShortTimeoutOptions);
71 } else {
72 NOTIMPLEMENTED() << "Unknown Resolver timeout parameters";
73 }
Thieu Le3bd0faa2012-02-15 15:44:46 -080074
Paul Stewartb6063942011-08-05 10:17:29 -070075 // Newline at end of file
76 lines.push_back("");
77
78 string contents = JoinString(lines, '\n');
Paul Stewarte6132022011-08-16 09:11:02 -070079
Ben Chanfad4a0b2012-04-18 15:49:59 -070080 SLOG(Resolver, 2) << "Writing DNS out to " << path_.value();
Paul Stewartb6063942011-08-05 10:17:29 -070081 int count = file_util::WriteFile(path_, contents.c_str(), contents.size());
82
83 return count == static_cast<int>(contents.size());
84}
85
86bool Resolver::ClearDNS() {
Ben Chanfad4a0b2012-04-18 15:49:59 -070087 SLOG(Resolver, 2) << __func__;
Paul Stewarte6132022011-08-16 09:11:02 -070088
Paul Stewartb6063942011-08-05 10:17:29 -070089 CHECK(!path_.empty());
90
91 return file_util::Delete(path_, false);
92}
93
94} // namespace shill