blob: d2c1a28d5a0f6216240afa9433733093fafce356 [file] [log] [blame]
Gunnar Mills57d9c502018-09-14 14:42:34 -05001#include "dns_updater.hpp"
2
Patrick Venture189d44e2018-07-09 12:30:59 -07003#include <phosphor-logging/elog-errors.hpp>
Jagpal Singh Gilla2947b42023-04-17 21:10:14 -07004#include <phosphor-logging/lg2.hpp>
Patrick Venture189d44e2018-07-09 12:30:59 -07005#include <sdbusplus/bus.hpp>
6#include <xyz/openbmc_project/Common/error.hpp>
Vishwanatha Subbannaf00182e2017-10-16 19:08:59 +05307
Patrick Williams89d734b2023-05-10 07:50:25 -05008#include <fstream>
9
Vishwanatha Subbannaf00182e2017-10-16 19:08:59 +053010namespace phosphor
11{
12namespace network
13{
14namespace dns
15{
16namespace updater
17{
18
Gunnar Mills57d9c502018-09-14 14:42:34 -050019void updateDNSEntries(const fs::path& inFile, const fs::path& outFile)
Vishwanatha Subbannaf00182e2017-10-16 19:08:59 +053020{
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 Gilla2947b42023-04-17 21:10:14 -070027 lg2::error("Unable to open output file {FILE}", "FILE", outFile);
Vishwanatha Subbannaf00182e2017-10-16 19:08:59 +053028 elog<InternalFailure>();
29 }
30
31 std::fstream inStream(inFile, std::fstream::in);
32 if (!inStream.is_open())
33 {
Jagpal Singh Gilla2947b42023-04-17 21:10:14 -070034 lg2::error("Unable to open the input file {FILE}", "FILE", inFile);
Vishwanatha Subbannaf00182e2017-10-16 19:08:59 +053035 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 Mills57d9c502018-09-14 14:42:34 -050043 if (index != std::string::npos)
Vishwanatha Subbannaf00182e2017-10-16 19:08:59 +053044 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050045 auto dns = line.substr(index + 4);
46 outStream << "nameserver " << dns << "\n";
Vishwanatha Subbannaf00182e2017-10-16 19:08:59 +053047 }
48 }
49 return;
50}
51
52} // namespace updater
53} // namespace dns
54} // namespace network
55} // namespace phosphor