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