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 | |
| 16 | namespace shill { |
| 17 | |
| 18 | using base::StringPrintf; |
| 19 | using std::string; |
| 20 | using std::vector; |
| 21 | |
| 22 | Resolver::Resolver() {} |
| 23 | |
| 24 | Resolver::~Resolver() {} |
| 25 | |
| 26 | Resolver* Resolver::GetInstance() { |
| 27 | return Singleton<Resolver>::get(); |
| 28 | } |
| 29 | |
| 30 | bool 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 | |
| 59 | bool Resolver::ClearDNS() { |
| 60 | CHECK(!path_.empty()); |
| 61 | |
| 62 | return file_util::Delete(path_, false); |
| 63 | } |
| 64 | |
| 65 | } // namespace shill |