1 #include "dns_updater.hpp"
2 
3 #include <phosphor-logging/elog-errors.hpp>
4 #include <phosphor-logging/lg2.hpp>
5 #include <sdbusplus/bus.hpp>
6 #include <xyz/openbmc_project/Common/error.hpp>
7 
8 #include <fstream>
9 
10 namespace phosphor
11 {
12 namespace network
13 {
14 namespace dns
15 {
16 namespace updater
17 {
18 
updateDNSEntries(const fs::path & inFile,const fs::path & outFile)19 void updateDNSEntries(const fs::path& inFile, const fs::path& outFile)
20 {
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     {
27         lg2::error("Unable to open output file {FILE}", "FILE", outFile);
28         elog<InternalFailure>();
29     }
30 
31     std::fstream inStream(inFile, std::fstream::in);
32     if (!inStream.is_open())
33     {
34         lg2::error("Unable to open the input file {FILE}", "FILE", inFile);
35         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=");
43         if (index != std::string::npos)
44         {
45             auto dns = line.substr(index + 4);
46             outStream << "nameserver " << dns << "\n";
47         }
48     }
49     return;
50 }
51 
52 } // namespace updater
53 } // namespace dns
54 } // namespace network
55 } // namespace phosphor
56