blob: 5d9e41720aa1cb72c2a9477818e2ba8103402937 [file] [log] [blame]
Gunnar Mills57d9c502018-09-14 14:42:34 -05001#include "dns_updater.hpp"
2
Vishwanatha Subbannaf00182e2017-10-16 19:08:59 +05303#include <fstream>
Patrick Venture189d44e2018-07-09 12:30:59 -07004#include <phosphor-logging/elog-errors.hpp>
Jagpal Singh Gilla2947b42023-04-17 21:10:14 -07005#include <phosphor-logging/lg2.hpp>
Patrick Venture189d44e2018-07-09 12:30:59 -07006#include <sdbusplus/bus.hpp>
7#include <xyz/openbmc_project/Common/error.hpp>
Vishwanatha Subbannaf00182e2017-10-16 19:08:59 +05308
9namespace phosphor
10{
11namespace network
12{
13namespace dns
14{
15namespace updater
16{
17
Gunnar Mills57d9c502018-09-14 14:42:34 -050018void updateDNSEntries(const fs::path& inFile, const fs::path& outFile)
Vishwanatha Subbannaf00182e2017-10-16 19:08:59 +053019{
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 {
Jagpal Singh Gilla2947b42023-04-17 21:10:14 -070026 lg2::error("Unable to open output file {FILE}", "FILE", outFile);
Vishwanatha Subbannaf00182e2017-10-16 19:08:59 +053027 elog<InternalFailure>();
28 }
29
30 std::fstream inStream(inFile, std::fstream::in);
31 if (!inStream.is_open())
32 {
Jagpal Singh Gilla2947b42023-04-17 21:10:14 -070033 lg2::error("Unable to open the input file {FILE}", "FILE", inFile);
Vishwanatha Subbannaf00182e2017-10-16 19:08:59 +053034 elog<InternalFailure>();
35 }
36
37 outStream << "### Generated by phosphor-networkd ###\n";
38
39 for (std::string line; std::getline(inStream, line);)
40 {
41 auto index = line.find("DNS=");
Gunnar Mills57d9c502018-09-14 14:42:34 -050042 if (index != std::string::npos)
Vishwanatha Subbannaf00182e2017-10-16 19:08:59 +053043 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050044 auto dns = line.substr(index + 4);
45 outStream << "nameserver " << dns << "\n";
Vishwanatha Subbannaf00182e2017-10-16 19:08:59 +053046 }
47 }
48 return;
49}
50
51} // namespace updater
52} // namespace dns
53} // namespace network
54} // namespace phosphor