blob: 5b4cf5bd6ea27b1bf598f9e1d3a7bd944c621ad4 [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
16namespace shill {
17
18using base::StringPrintf;
19using std::string;
20using std::vector;
21
22Resolver::Resolver() {}
23
24Resolver::~Resolver() {}
25
26Resolver* Resolver::GetInstance() {
27 return Singleton<Resolver>::get();
28}
29
30bool Resolver::SetDNS(const IPConfigRefPtr &ipconfig) {
31 CHECK(!path_.empty());
32
33 const IPConfig::Properties &props = ipconfig->properties();
34
35 if (!props.dns_servers.size() && !props.domain_search.size()) {
36 return ClearDNS();
37 }
38
39 vector<string> lines;
40 vector<string>::const_iterator iter;
41 for (iter = props.dns_servers.begin();
42 iter != props.dns_servers.end(); ++iter) {
43 lines.push_back("nameserver " + *iter);
44 }
45
46 if (props.domain_search.size()) {
47 lines.push_back("search " + JoinString(props.domain_search, ' '));
48 }
49
50 // Newline at end of file
51 lines.push_back("");
52
53 string contents = JoinString(lines, '\n');
54 int count = file_util::WriteFile(path_, contents.c_str(), contents.size());
55
56 return count == static_cast<int>(contents.size());
57}
58
59bool Resolver::ClearDNS() {
60 CHECK(!path_.empty());
61
62 return file_util::Delete(path_, false);
63}
64
65} // namespace shill