11bbe3d1eSWilliam A. Kennington III #include "dhcp_configuration.hpp"
21bbe3d1eSWilliam A. Kennington III 
3e94c9ffcSWilliam A. Kennington III #include "config_parser.hpp"
41bbe3d1eSWilliam A. Kennington III #include "network_manager.hpp"
595530ec9SWilliam A. Kennington III #include "util.hpp"
61bbe3d1eSWilliam A. Kennington III 
7180498c5SWilliam A. Kennington III #include <sys/stat.h>
8180498c5SWilliam A. Kennington III 
9180498c5SWilliam A. Kennington III #include <filesystem>
101bbe3d1eSWilliam A. Kennington III #include <phosphor-logging/elog-errors.hpp>
111bbe3d1eSWilliam A. Kennington III #include <phosphor-logging/log.hpp>
121bbe3d1eSWilliam A. Kennington III #include <xyz/openbmc_project/Common/error.hpp>
131bbe3d1eSWilliam A. Kennington III 
141bbe3d1eSWilliam A. Kennington III namespace phosphor
151bbe3d1eSWilliam A. Kennington III {
161bbe3d1eSWilliam A. Kennington III namespace network
171bbe3d1eSWilliam A. Kennington III {
181bbe3d1eSWilliam A. Kennington III namespace dhcp
191bbe3d1eSWilliam A. Kennington III {
201bbe3d1eSWilliam A. Kennington III 
211bbe3d1eSWilliam A. Kennington III using namespace phosphor::network;
221bbe3d1eSWilliam A. Kennington III using namespace phosphor::logging;
231bbe3d1eSWilliam A. Kennington III using namespace sdbusplus::xyz::openbmc_project::Common::Error;
24e94c9ffcSWilliam A. Kennington III 
25be3bd2fcSWilliam A. Kennington III Configuration::Configuration(sdbusplus::bus_t& bus,
26be3bd2fcSWilliam A. Kennington III                              stdplus::const_zstring objPath, Manager& parent) :
27e94c9ffcSWilliam A. Kennington III     Iface(bus, objPath.c_str(), Iface::action::defer_emit),
28e94c9ffcSWilliam A. Kennington III     bus(bus), manager(parent)
29e94c9ffcSWilliam A. Kennington III {
30e94c9ffcSWilliam A. Kennington III     config::Parser conf;
31180498c5SWilliam A. Kennington III     std::filesystem::directory_entry newest_file;
32180498c5SWilliam A. Kennington III     time_t newest_time = 0;
33180498c5SWilliam A. Kennington III     for (const auto& dirent :
34180498c5SWilliam A. Kennington III          std::filesystem::directory_iterator(manager.getConfDir()))
35e94c9ffcSWilliam A. Kennington III     {
36180498c5SWilliam A. Kennington III         struct stat st = {};
37180498c5SWilliam A. Kennington III         stat(dirent.path().native().c_str(), &st);
38180498c5SWilliam A. Kennington III         if (st.st_mtime > newest_time)
39e94c9ffcSWilliam A. Kennington III         {
40180498c5SWilliam A. Kennington III             newest_file = dirent;
41180498c5SWilliam A. Kennington III             newest_time = st.st_mtime;
42e94c9ffcSWilliam A. Kennington III         }
43e94c9ffcSWilliam A. Kennington III     }
44180498c5SWilliam A. Kennington III     if (newest_file != std::filesystem::directory_entry{})
45180498c5SWilliam A. Kennington III     {
46180498c5SWilliam A. Kennington III         log<level::INFO>(fmt::format("Using DHCP options from {}",
47180498c5SWilliam A. Kennington III                                      newest_file.path().native())
48180498c5SWilliam A. Kennington III                              .c_str());
49180498c5SWilliam A. Kennington III         conf.setFile(newest_file.path());
50180498c5SWilliam A. Kennington III     }
51e94c9ffcSWilliam A. Kennington III 
52e94c9ffcSWilliam A. Kennington III     ConfigIntf::dnsEnabled(getDHCPProp(conf, "UseDNS"));
53e94c9ffcSWilliam A. Kennington III     ConfigIntf::ntpEnabled(getDHCPProp(conf, "UseNTP"));
54e94c9ffcSWilliam A. Kennington III     ConfigIntf::hostNameEnabled(getDHCPProp(conf, "UseHostname"));
55e94c9ffcSWilliam A. Kennington III     ConfigIntf::sendHostNameEnabled(getDHCPProp(conf, "SendHostname"));
56e94c9ffcSWilliam A. Kennington III     emit_object_added();
57e94c9ffcSWilliam A. Kennington III }
58e94c9ffcSWilliam A. Kennington III 
591bbe3d1eSWilliam A. Kennington III bool Configuration::sendHostNameEnabled(bool value)
601bbe3d1eSWilliam A. Kennington III {
611bbe3d1eSWilliam A. Kennington III     if (value == sendHostNameEnabled())
621bbe3d1eSWilliam A. Kennington III     {
631bbe3d1eSWilliam A. Kennington III         return value;
641bbe3d1eSWilliam A. Kennington III     }
651bbe3d1eSWilliam A. Kennington III 
661bbe3d1eSWilliam A. Kennington III     auto name = ConfigIntf::sendHostNameEnabled(value);
67bd649af9SWilliam A. Kennington III 
681bbe3d1eSWilliam A. Kennington III     manager.writeToConfigurationFile();
69*5b17938cSWilliam A. Kennington III     manager.reloadConfigs();
701bbe3d1eSWilliam A. Kennington III 
711bbe3d1eSWilliam A. Kennington III     return name;
721bbe3d1eSWilliam A. Kennington III }
731bbe3d1eSWilliam A. Kennington III 
741bbe3d1eSWilliam A. Kennington III bool Configuration::hostNameEnabled(bool value)
751bbe3d1eSWilliam A. Kennington III {
761bbe3d1eSWilliam A. Kennington III     if (value == hostNameEnabled())
771bbe3d1eSWilliam A. Kennington III     {
781bbe3d1eSWilliam A. Kennington III         return value;
791bbe3d1eSWilliam A. Kennington III     }
801bbe3d1eSWilliam A. Kennington III 
811bbe3d1eSWilliam A. Kennington III     auto name = ConfigIntf::hostNameEnabled(value);
821bbe3d1eSWilliam A. Kennington III     manager.writeToConfigurationFile();
83*5b17938cSWilliam A. Kennington III     manager.reloadConfigs();
841bbe3d1eSWilliam A. Kennington III 
851bbe3d1eSWilliam A. Kennington III     return name;
861bbe3d1eSWilliam A. Kennington III }
871bbe3d1eSWilliam A. Kennington III 
881bbe3d1eSWilliam A. Kennington III bool Configuration::ntpEnabled(bool value)
891bbe3d1eSWilliam A. Kennington III {
901bbe3d1eSWilliam A. Kennington III     if (value == ntpEnabled())
911bbe3d1eSWilliam A. Kennington III     {
921bbe3d1eSWilliam A. Kennington III         return value;
931bbe3d1eSWilliam A. Kennington III     }
941bbe3d1eSWilliam A. Kennington III 
951bbe3d1eSWilliam A. Kennington III     auto ntp = ConfigIntf::ntpEnabled(value);
961bbe3d1eSWilliam A. Kennington III     manager.writeToConfigurationFile();
97*5b17938cSWilliam A. Kennington III     manager.reloadConfigs();
981bbe3d1eSWilliam A. Kennington III 
991bbe3d1eSWilliam A. Kennington III     return ntp;
1001bbe3d1eSWilliam A. Kennington III }
1011bbe3d1eSWilliam A. Kennington III 
1021bbe3d1eSWilliam A. Kennington III bool Configuration::dnsEnabled(bool value)
1031bbe3d1eSWilliam A. Kennington III {
1041bbe3d1eSWilliam A. Kennington III     if (value == dnsEnabled())
1051bbe3d1eSWilliam A. Kennington III     {
1061bbe3d1eSWilliam A. Kennington III         return value;
1071bbe3d1eSWilliam A. Kennington III     }
1081bbe3d1eSWilliam A. Kennington III 
1091bbe3d1eSWilliam A. Kennington III     auto dns = ConfigIntf::dnsEnabled(value);
1101bbe3d1eSWilliam A. Kennington III     manager.writeToConfigurationFile();
111*5b17938cSWilliam A. Kennington III     manager.reloadConfigs();
1121bbe3d1eSWilliam A. Kennington III 
1131bbe3d1eSWilliam A. Kennington III     return dns;
1141bbe3d1eSWilliam A. Kennington III }
1151bbe3d1eSWilliam A. Kennington III 
1161bbe3d1eSWilliam A. Kennington III } // namespace dhcp
1171bbe3d1eSWilliam A. Kennington III } // namespace network
1181bbe3d1eSWilliam A. Kennington III } // namespace phosphor
119