1 #include "dns_updater.hpp"
2 
3 #include <fstream>
4 #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>
8 
9 namespace phosphor
10 {
11 namespace network
12 {
13 namespace dns
14 {
15 namespace updater
16 {
17 
18 void updateDNSEntries(const fs::path& inFile, const fs::path& outFile)
19 {
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=");
44         if (index != std::string::npos)
45         {
46             auto dns = line.substr(index + 4);
47             outStream << "nameserver " << dns << "\n";
48         }
49     }
50     return;
51 }
52 
53 } // namespace updater
54 } // namespace dns
55 } // namespace network
56 } // namespace phosphor
57