xref: /openbmc/phosphor-networkd/src/dhcp_configuration.cpp (revision 9ede1b74c0feaee6abaa228cd097133dd74edb3e)
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,
26*9ede1b74SWilliam A. Kennington III                              stdplus::const_zstring objPath,
27*9ede1b74SWilliam A. Kennington III                              stdplus::PinnedRef<Manager> parent) :
28e94c9ffcSWilliam A. Kennington III     Iface(bus, objPath.c_str(), Iface::action::defer_emit),
29*9ede1b74SWilliam A. Kennington III     manager(parent)
30e94c9ffcSWilliam A. Kennington III {
31e94c9ffcSWilliam A. Kennington III     config::Parser conf;
32180498c5SWilliam A. Kennington III     std::filesystem::directory_entry newest_file;
33180498c5SWilliam A. Kennington III     time_t newest_time = 0;
34180498c5SWilliam A. Kennington III     for (const auto& dirent :
35*9ede1b74SWilliam A. Kennington III          std::filesystem::directory_iterator(manager.get().getConfDir()))
36e94c9ffcSWilliam A. Kennington III     {
37180498c5SWilliam A. Kennington III         struct stat st = {};
38180498c5SWilliam A. Kennington III         stat(dirent.path().native().c_str(), &st);
39180498c5SWilliam A. Kennington III         if (st.st_mtime > newest_time)
40e94c9ffcSWilliam A. Kennington III         {
41180498c5SWilliam A. Kennington III             newest_file = dirent;
42180498c5SWilliam A. Kennington III             newest_time = st.st_mtime;
43e94c9ffcSWilliam A. Kennington III         }
44e94c9ffcSWilliam A. Kennington III     }
45180498c5SWilliam A. Kennington III     if (newest_file != std::filesystem::directory_entry{})
46180498c5SWilliam A. Kennington III     {
47180498c5SWilliam A. Kennington III         log<level::INFO>(fmt::format("Using DHCP options from {}",
48180498c5SWilliam A. Kennington III                                      newest_file.path().native())
49180498c5SWilliam A. Kennington III                              .c_str());
50180498c5SWilliam A. Kennington III         conf.setFile(newest_file.path());
51180498c5SWilliam A. Kennington III     }
52e94c9ffcSWilliam A. Kennington III 
53d99e6dbaSWilliam A. Kennington III     ConfigIntf::dnsEnabled(getDHCPProp(conf, "UseDNS"), true);
54d99e6dbaSWilliam A. Kennington III     ConfigIntf::ntpEnabled(getDHCPProp(conf, "UseNTP"), true);
55d99e6dbaSWilliam A. Kennington III     ConfigIntf::hostNameEnabled(getDHCPProp(conf, "UseHostname"), true);
56d99e6dbaSWilliam A. Kennington III     ConfigIntf::sendHostNameEnabled(getDHCPProp(conf, "SendHostname"), true);
57e94c9ffcSWilliam A. Kennington III     emit_object_added();
58e94c9ffcSWilliam A. Kennington III }
59e94c9ffcSWilliam A. Kennington III 
601bbe3d1eSWilliam A. Kennington III bool Configuration::sendHostNameEnabled(bool value)
611bbe3d1eSWilliam A. Kennington III {
621bbe3d1eSWilliam A. Kennington III     if (value == sendHostNameEnabled())
631bbe3d1eSWilliam A. Kennington III     {
641bbe3d1eSWilliam A. Kennington III         return value;
651bbe3d1eSWilliam A. Kennington III     }
661bbe3d1eSWilliam A. Kennington III 
671bbe3d1eSWilliam A. Kennington III     auto name = ConfigIntf::sendHostNameEnabled(value);
68bd649af9SWilliam A. Kennington III 
69*9ede1b74SWilliam A. Kennington III     manager.get().writeToConfigurationFile();
70*9ede1b74SWilliam A. Kennington III     manager.get().reloadConfigs();
711bbe3d1eSWilliam A. Kennington III 
721bbe3d1eSWilliam A. Kennington III     return name;
731bbe3d1eSWilliam A. Kennington III }
741bbe3d1eSWilliam A. Kennington III 
751bbe3d1eSWilliam A. Kennington III bool Configuration::hostNameEnabled(bool value)
761bbe3d1eSWilliam A. Kennington III {
771bbe3d1eSWilliam A. Kennington III     if (value == hostNameEnabled())
781bbe3d1eSWilliam A. Kennington III     {
791bbe3d1eSWilliam A. Kennington III         return value;
801bbe3d1eSWilliam A. Kennington III     }
811bbe3d1eSWilliam A. Kennington III 
821bbe3d1eSWilliam A. Kennington III     auto name = ConfigIntf::hostNameEnabled(value);
83*9ede1b74SWilliam A. Kennington III     manager.get().writeToConfigurationFile();
84*9ede1b74SWilliam A. Kennington III     manager.get().reloadConfigs();
851bbe3d1eSWilliam A. Kennington III 
861bbe3d1eSWilliam A. Kennington III     return name;
871bbe3d1eSWilliam A. Kennington III }
881bbe3d1eSWilliam A. Kennington III 
891bbe3d1eSWilliam A. Kennington III bool Configuration::ntpEnabled(bool value)
901bbe3d1eSWilliam A. Kennington III {
911bbe3d1eSWilliam A. Kennington III     if (value == ntpEnabled())
921bbe3d1eSWilliam A. Kennington III     {
931bbe3d1eSWilliam A. Kennington III         return value;
941bbe3d1eSWilliam A. Kennington III     }
951bbe3d1eSWilliam A. Kennington III 
961bbe3d1eSWilliam A. Kennington III     auto ntp = ConfigIntf::ntpEnabled(value);
97*9ede1b74SWilliam A. Kennington III     manager.get().writeToConfigurationFile();
98*9ede1b74SWilliam A. Kennington III     manager.get().reloadConfigs();
991bbe3d1eSWilliam A. Kennington III 
1001bbe3d1eSWilliam A. Kennington III     return ntp;
1011bbe3d1eSWilliam A. Kennington III }
1021bbe3d1eSWilliam A. Kennington III 
1031bbe3d1eSWilliam A. Kennington III bool Configuration::dnsEnabled(bool value)
1041bbe3d1eSWilliam A. Kennington III {
1051bbe3d1eSWilliam A. Kennington III     if (value == dnsEnabled())
1061bbe3d1eSWilliam A. Kennington III     {
1071bbe3d1eSWilliam A. Kennington III         return value;
1081bbe3d1eSWilliam A. Kennington III     }
1091bbe3d1eSWilliam A. Kennington III 
1101bbe3d1eSWilliam A. Kennington III     auto dns = ConfigIntf::dnsEnabled(value);
111*9ede1b74SWilliam A. Kennington III     manager.get().writeToConfigurationFile();
112*9ede1b74SWilliam A. Kennington III     manager.get().reloadConfigs();
1131bbe3d1eSWilliam A. Kennington III 
1141bbe3d1eSWilliam A. Kennington III     return dns;
1151bbe3d1eSWilliam A. Kennington III }
1161bbe3d1eSWilliam A. Kennington III 
1171bbe3d1eSWilliam A. Kennington III } // namespace dhcp
1181bbe3d1eSWilliam A. Kennington III } // namespace network
1191bbe3d1eSWilliam A. Kennington III } // namespace phosphor
120