blob: e98cc4ace51ebbf7013a4e10e6998397925f5f5d [file] [log] [blame]
Vishwanatha Subbannaf00182e2017-10-16 19:08:59 +05301#include "dns_updater.hpp"
2
Patrick Venture189d44e2018-07-09 12:30:59 -07003#include "config.h"
Vishwanatha Subbannaf00182e2017-10-16 19:08:59 +05304
5#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
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +053020void updateDNSEntries(const fs::path& inFile,
21 const fs::path& outFile)
Vishwanatha Subbannaf00182e2017-10-16 19:08:59 +053022{
23 using namespace phosphor::logging;
24 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
25
26 std::fstream outStream(outFile, std::fstream::out);
27 if (!outStream.is_open())
28 {
29 log<level::ERR>("Unable to open output file",
30 entry("FILE=%s", outFile.c_str()));
31 elog<InternalFailure>();
32 }
33
34 std::fstream inStream(inFile, std::fstream::in);
35 if (!inStream.is_open())
36 {
37 log<level::ERR>("Unable to open the input file",
38 entry("FILE=%s", inFile.c_str()));
39 elog<InternalFailure>();
40 }
41
42 outStream << "### Generated by phosphor-networkd ###\n";
43
44 for (std::string line; std::getline(inStream, line);)
45 {
46 auto index = line.find("DNS=");
47 if(index != std::string::npos)
48 {
49 auto dns = line.substr(index + 4);
50 outStream << "nameserver " << dns << "\n" ;
51 }
52 }
53 return;
54}
55
56} // namespace updater
57} // namespace dns
58} // namespace network
59} // namespace phosphor