blob: 22693f843bfae936cb2463b085ef252af45e20c4 [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>
5#include <phosphor-logging/log.hpp>
6#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 {
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 Mills57d9c502018-09-14 14:42:34 -050044 if (index != std::string::npos)
Vishwanatha Subbannaf00182e2017-10-16 19:08:59 +053045 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050046 auto dns = line.substr(index + 4);
47 outStream << "nameserver " << dns << "\n";
Vishwanatha Subbannaf00182e2017-10-16 19:08:59 +053048 }
49 }
50 return;
51}
52
53} // namespace updater
54} // namespace dns
55} // namespace network
56} // namespace phosphor