blob: 1d1cacfd62229dce621989f4e772736d36fb6ec6 [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"
15
Paul Stewartb6063942011-08-05 10:17:29 -070016using base::StringPrintf;
17using std::string;
18using std::vector;
19
Paul Stewart0d2ada32011-08-09 17:01:57 -070020namespace shill {
21
Eric Shienbrood3e20a232012-02-16 11:35:56 -050022// TODO(ers): not using LAZY_INSTANCE_INITIALIZER
23// because of http://crbug.com/114828
24static base::LazyInstance<Resolver> g_resolver = {0, {{0}}};
Paul Stewart0d2ada32011-08-09 17:01:57 -070025
Paul Stewartb6063942011-08-05 10:17:29 -070026Resolver::Resolver() {}
27
28Resolver::~Resolver() {}
29
30Resolver* Resolver::GetInstance() {
Paul Stewart0d2ada32011-08-09 17:01:57 -070031 return g_resolver.Pointer();
Paul Stewartb6063942011-08-05 10:17:29 -070032}
33
Paul Stewartdd60e452011-08-08 11:38:36 -070034bool Resolver::SetDNSFromIPConfig(const IPConfigRefPtr &ipconfig) {
Paul Stewarte6132022011-08-16 09:11:02 -070035 VLOG(2) << __func__;
36
Paul Stewartb6063942011-08-05 10:17:29 -070037 CHECK(!path_.empty());
38
39 const IPConfig::Properties &props = ipconfig->properties();
40
Paul Stewartdd60e452011-08-08 11:38:36 -070041 return SetDNSFromLists(props.dns_servers, props.domain_search);
42}
43
44bool Resolver::SetDNSFromLists(const std::vector<std::string> &dns_servers,
45 const std::vector<std::string> &domain_search) {
Paul Stewarte6132022011-08-16 09:11:02 -070046 VLOG(2) << __func__;
47
Paul Stewartdd60e452011-08-08 11:38:36 -070048 if (dns_servers.empty() && domain_search.empty()) {
Paul Stewarte6132022011-08-16 09:11:02 -070049 VLOG(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
66 // 5-second request timeout and use a 1-second tiemout instead.
67 lines.push_back("options single-request timeout:1");
Thieu Le3bd0faa2012-02-15 15:44:46 -080068
Paul Stewartb6063942011-08-05 10:17:29 -070069 // Newline at end of file
70 lines.push_back("");
71
72 string contents = JoinString(lines, '\n');
Paul Stewarte6132022011-08-16 09:11:02 -070073
74 VLOG(2) << "Writing DNS out to " << path_.value();
Paul Stewartb6063942011-08-05 10:17:29 -070075 int count = file_util::WriteFile(path_, contents.c_str(), contents.size());
76
77 return count == static_cast<int>(contents.size());
78}
79
80bool Resolver::ClearDNS() {
Paul Stewarte6132022011-08-16 09:11:02 -070081 VLOG(2) << __func__;
82
Paul Stewartb6063942011-08-05 10:17:29 -070083 CHECK(!path_.empty());
84
85 return file_util::Delete(path_, false);
86}
87
88} // namespace shill