summaryrefslogtreecommitdiffstats
path: root/dns_updater.cpp
blob: bf29fff0ae7b53f350572c1960b26cef7dffff95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "config.h"

#include "dns_updater.hpp"

#include <fstream>
#include <phosphor-logging/elog-errors.hpp>
#include <phosphor-logging/log.hpp>
#include <sdbusplus/bus.hpp>
#include <xyz/openbmc_project/Common/error.hpp>

namespace phosphor
{
namespace network
{
namespace dns
{
namespace updater
{

void updateDNSEntries(const fs::path& inFile, const fs::path& outFile)
{
    using namespace phosphor::logging;
    using namespace sdbusplus::xyz::openbmc_project::Common::Error;

    std::fstream outStream(outFile, std::fstream::out);
    if (!outStream.is_open())
    {
        log<level::ERR>("Unable to open output file",
                        entry("FILE=%s", outFile.c_str()));
        elog<InternalFailure>();
    }

    std::fstream inStream(inFile, std::fstream::in);
    if (!inStream.is_open())
    {
        log<level::ERR>("Unable to open the input file",
                        entry("FILE=%s", inFile.c_str()));
        elog<InternalFailure>();
    }

    outStream << "### Generated by phosphor-networkd ###\n";

    for (std::string line; std::getline(inStream, line);)
    {
        auto index = line.find("DNS=");
        if (index != std::string::npos)
        {
            auto dns = line.substr(index + 4);
            outStream << "nameserver " << dns << "\n";
        }
    }
    return;
}

} // namespace updater
} // namespace dns
} // namespace network
} // namespace phosphor
OpenPOWER on IntegriCloud