Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 1 | // 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 Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 16 | using base::StringPrintf; |
| 17 | using std::string; |
| 18 | using std::vector; |
| 19 | |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame^] | 20 | namespace shill { |
| 21 | |
| 22 | static base::LazyInstance<Resolver> g_resolver( |
| 23 | base::LINKER_INITIALIZED); |
| 24 | |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 25 | Resolver::Resolver() {} |
| 26 | |
| 27 | Resolver::~Resolver() {} |
| 28 | |
| 29 | Resolver* Resolver::GetInstance() { |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame^] | 30 | return g_resolver.Pointer(); |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | bool 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 | |
| 62 | bool Resolver::ClearDNS() { |
| 63 | CHECK(!path_.empty()); |
| 64 | |
| 65 | return file_util::Delete(path_, false); |
| 66 | } |
| 67 | |
| 68 | } // namespace shill |