blob: 22632c712e19ee2401052064a0e66691faece826 [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"
Ben Chanfad4a0b2012-04-18 15:49:59 -070015#include "shill/scope_logger.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
Eric Shienbrood3e20a232012-02-16 11:35:56 -050023// TODO(ers): not using LAZY_INSTANCE_INITIALIZER
24// because of http://crbug.com/114828
25static base::LazyInstance<Resolver> g_resolver = {0, {{0}}};
Paul Stewart0d2ada32011-08-09 17:01:57 -070026
Paul Stewartb6063942011-08-05 10:17:29 -070027Resolver::Resolver() {}
28
29Resolver::~Resolver() {}
30
31Resolver* Resolver::GetInstance() {
Paul Stewart0d2ada32011-08-09 17:01:57 -070032 return g_resolver.Pointer();
Paul Stewartb6063942011-08-05 10:17:29 -070033}
34
Paul Stewartdd60e452011-08-08 11:38:36 -070035bool Resolver::SetDNSFromIPConfig(const IPConfigRefPtr &ipconfig) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070036 SLOG(Resolver, 2) << __func__;
Paul Stewarte6132022011-08-16 09:11:02 -070037
Paul Stewartb6063942011-08-05 10:17:29 -070038 CHECK(!path_.empty());
39
40 const IPConfig::Properties &props = ipconfig->properties();
41
Paul Stewartdd60e452011-08-08 11:38:36 -070042 return SetDNSFromLists(props.dns_servers, props.domain_search);
43}
44
45bool Resolver::SetDNSFromLists(const std::vector<std::string> &dns_servers,
46 const std::vector<std::string> &domain_search) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070047 SLOG(Resolver, 2) << __func__;
Paul Stewarte6132022011-08-16 09:11:02 -070048
Paul Stewartdd60e452011-08-08 11:38:36 -070049 if (dns_servers.empty() && domain_search.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070050 SLOG(Resolver, 2) << "DNS list is empty";
Paul Stewartb6063942011-08-05 10:17:29 -070051 return ClearDNS();
52 }
53
54 vector<string> lines;
55 vector<string>::const_iterator iter;
Paul Stewartdd60e452011-08-08 11:38:36 -070056 for (iter = dns_servers.begin();
57 iter != dns_servers.end(); ++iter) {
Paul Stewartb6063942011-08-05 10:17:29 -070058 lines.push_back("nameserver " + *iter);
59 }
60
Paul Stewartdd60e452011-08-08 11:38:36 -070061 if (!domain_search.empty()) {
62 lines.push_back("search " + JoinString(domain_search, ' '));
Paul Stewartb6063942011-08-05 10:17:29 -070063 }
64
Thieu Le3bd0faa2012-02-15 15:44:46 -080065 // Send queries one-at-a-time, rather than parallelizing IPv4
Paul Stewartabc41aa2012-04-07 13:15:37 -070066 // and IPv6 queries for a single host. Also override the default
67 // 5-second request timeout and use a 1-second tiemout instead.
68 lines.push_back("options single-request timeout:1");
Thieu Le3bd0faa2012-02-15 15:44:46 -080069
Paul Stewartb6063942011-08-05 10:17:29 -070070 // Newline at end of file
71 lines.push_back("");
72
73 string contents = JoinString(lines, '\n');
Paul Stewarte6132022011-08-16 09:11:02 -070074
Ben Chanfad4a0b2012-04-18 15:49:59 -070075 SLOG(Resolver, 2) << "Writing DNS out to " << path_.value();
Paul Stewartb6063942011-08-05 10:17:29 -070076 int count = file_util::WriteFile(path_, contents.c_str(), contents.size());
77
78 return count == static_cast<int>(contents.size());
79}
80
81bool Resolver::ClearDNS() {
Ben Chanfad4a0b2012-04-18 15:49:59 -070082 SLOG(Resolver, 2) << __func__;
Paul Stewarte6132022011-08-16 09:11:02 -070083
Paul Stewartb6063942011-08-05 10:17:29 -070084 CHECK(!path_.empty());
85
86 return file_util::Delete(path_, false);
87}
88
89} // namespace shill