Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 1 | #include "dns_updater.hpp" |
| 2 | |
Vishwanatha Subbanna | f00182e | 2017-10-16 19:08:59 +0530 | [diff] [blame] | 3 | #include <fstream> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 4 | #include <phosphor-logging/elog-errors.hpp> |
| 5 | #include <phosphor-logging/log.hpp> |
| 6 | #include <sdbusplus/bus.hpp> |
| 7 | #include <xyz/openbmc_project/Common/error.hpp> |
Vishwanatha Subbanna | f00182e | 2017-10-16 19:08:59 +0530 | [diff] [blame] | 8 | |
| 9 | namespace phosphor |
| 10 | { |
| 11 | namespace network |
| 12 | { |
| 13 | namespace dns |
| 14 | { |
| 15 | namespace updater |
| 16 | { |
| 17 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 18 | void updateDNSEntries(const fs::path& inFile, const fs::path& outFile) |
Vishwanatha Subbanna | f00182e | 2017-10-16 19:08:59 +0530 | [diff] [blame] | 19 | { |
| 20 | using namespace phosphor::logging; |
| 21 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 22 | |
| 23 | std::fstream outStream(outFile, std::fstream::out); |
| 24 | if (!outStream.is_open()) |
| 25 | { |
| 26 | log<level::ERR>("Unable to open output file", |
| 27 | entry("FILE=%s", outFile.c_str())); |
| 28 | elog<InternalFailure>(); |
| 29 | } |
| 30 | |
| 31 | std::fstream inStream(inFile, std::fstream::in); |
| 32 | if (!inStream.is_open()) |
| 33 | { |
| 34 | log<level::ERR>("Unable to open the input file", |
| 35 | entry("FILE=%s", inFile.c_str())); |
| 36 | elog<InternalFailure>(); |
| 37 | } |
| 38 | |
| 39 | outStream << "### Generated by phosphor-networkd ###\n"; |
| 40 | |
| 41 | for (std::string line; std::getline(inStream, line);) |
| 42 | { |
| 43 | auto index = line.find("DNS="); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 44 | if (index != std::string::npos) |
Vishwanatha Subbanna | f00182e | 2017-10-16 19:08:59 +0530 | [diff] [blame] | 45 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 46 | auto dns = line.substr(index + 4); |
| 47 | outStream << "nameserver " << dns << "\n"; |
Vishwanatha Subbanna | f00182e | 2017-10-16 19:08:59 +0530 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | } // namespace updater |
| 54 | } // namespace dns |
| 55 | } // namespace network |
| 56 | } // namespace phosphor |