blob: 16f503c59940117ce142371ea3d19740408015a5 [file] [log] [blame]
Paul Stewartb6063942011-08-05 10:17:29 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2// 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
22static base::LazyInstance<Resolver> g_resolver(
23 base::LINKER_INITIALIZED);
24
Paul Stewartb6063942011-08-05 10:17:29 -070025Resolver::Resolver() {}
26
27Resolver::~Resolver() {}
28
29Resolver* Resolver::GetInstance() {
Paul Stewart0d2ada32011-08-09 17:01:57 -070030 return g_resolver.Pointer();
Paul Stewartb6063942011-08-05 10:17:29 -070031}
32
33bool Resolver::SetDNS(const IPConfigRefPtr &ipconfig) {
34 CHECK(!path_.empty());
35
36 const IPConfig::Properties &props = ipconfig->properties();
37
38 if (!props.dns_servers.size() && !props.domain_search.size()) {
39 return ClearDNS();
40 }
41
42 vector<string> lines;
43 vector<string>::const_iterator iter;
44 for (iter = props.dns_servers.begin();
45 iter != props.dns_servers.end(); ++iter) {
46 lines.push_back("nameserver " + *iter);
47 }
48
49 if (props.domain_search.size()) {
50 lines.push_back("search " + JoinString(props.domain_search, ' '));
51 }
52
53 // Newline at end of file
54 lines.push_back("");
55
56 string contents = JoinString(lines, '\n');
57 int count = file_util::WriteFile(path_, contents.c_str(), contents.size());
58
59 return count == static_cast<int>(contents.size());
60}
61
62bool Resolver::ClearDNS() {
63 CHECK(!path_.empty());
64
65 return file_util::Delete(path_, false);
66}
67
68} // namespace shill