blob: bf29fff0ae7b53f350572c1960b26cef7dffff95 [file] [log] [blame]
Patrick Venture189d44e2018-07-09 12:30:59 -07001#include "config.h"
Vishwanatha Subbannaf00182e2017-10-16 19:08:59 +05302
Gunnar Mills57d9c502018-09-14 14:42:34 -05003#include "dns_updater.hpp"
4
Vishwanatha Subbannaf00182e2017-10-16 19:08:59 +05305#include <fstream>
Patrick Venture189d44e2018-07-09 12:30:59 -07006#include <phosphor-logging/elog-errors.hpp>
7#include <phosphor-logging/log.hpp>
8#include <sdbusplus/bus.hpp>
9#include <xyz/openbmc_project/Common/error.hpp>
Vishwanatha Subbannaf00182e2017-10-16 19:08:59 +053010
11namespace phosphor
12{
13namespace network
14{
15namespace dns
16{
17namespace updater
18{
19
Gunnar Mills57d9c502018-09-14 14:42:34 -050020void updateDNSEntries(const fs::path& inFile, const fs::path& outFile)
Vishwanatha Subbannaf00182e2017-10-16 19:08:59 +053021{
22 using namespace phosphor::logging;
23 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
24
25 std::fstream outStream(outFile, std::fstream::out);
26 if (!outStream.is_open())
27 {
28 log<level::ERR>("Unable to open output file",
29 entry("FILE=%s", outFile.c_str()));
30 elog<InternalFailure>();
31 }
32
33 std::fstream inStream(inFile, std::fstream::in);
34 if (!inStream.is_open())
35 {
36 log<level::ERR>("Unable to open the input file",
37 entry("FILE=%s", inFile.c_str()));
38 elog<InternalFailure>();
39 }
40
41 outStream << "### Generated by phosphor-networkd ###\n";
42
43 for (std::string line; std::getline(inStream, line);)
44 {
45 auto index = line.find("DNS=");
Gunnar Mills57d9c502018-09-14 14:42:34 -050046 if (index != std::string::npos)
Vishwanatha Subbannaf00182e2017-10-16 19:08:59 +053047 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050048 auto dns = line.substr(index + 4);
49 outStream << "nameserver " << dns << "\n";
Vishwanatha Subbannaf00182e2017-10-16 19:08:59 +053050 }
51 }
52 return;
53}
54
55} // namespace updater
56} // namespace dns
57} // namespace network
58} // namespace phosphor