xref: /openbmc/phosphor-networkd/src/dhcp_configuration.cpp (revision 57dfea94549af7bb60ad1d420abc98900efb5891)
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"
51bbe3d1eSWilliam A. Kennington III 
6180498c5SWilliam A. Kennington III #include <sys/stat.h>
7180498c5SWilliam A. Kennington III 
81bbe3d1eSWilliam A. Kennington III #include <phosphor-logging/elog-errors.hpp>
9a2947b4bSJagpal Singh Gill #include <phosphor-logging/lg2.hpp>
101bbe3d1eSWilliam A. Kennington III #include <xyz/openbmc_project/Common/error.hpp>
111bbe3d1eSWilliam A. Kennington III 
1289d734b9SPatrick Williams #include <filesystem>
1389d734b9SPatrick Williams 
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 sdbusplus::xyz::openbmc_project::Common::Error;
23e94c9ffcSWilliam A. Kennington III 
24be3bd2fcSWilliam A. Kennington III Configuration::Configuration(sdbusplus::bus_t& bus,
259ede1b74SWilliam A. Kennington III                              stdplus::const_zstring objPath,
26*57dfea94SJishnu CM                              stdplus::PinnedRef<EthernetInterface> parent,
27*57dfea94SJishnu CM                              DHCPType type) :
28e94c9ffcSWilliam A. Kennington III     Iface(bus, objPath.c_str(), Iface::action::defer_emit),
29*57dfea94SJishnu CM     parent(parent), type(type)
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;
34*57dfea94SJishnu CM     for (const auto& dirent : std::filesystem::directory_iterator(
35*57dfea94SJishnu CM              parent.get().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     {
471d25ca4eSWilliam A. Kennington III         lg2::info("Using DHCP options from {CFG_FILE}", "CFG_FILE",
48a2947b4bSJagpal Singh Gill                   newest_file.path().native());
49180498c5SWilliam A. Kennington III         conf.setFile(newest_file.path());
50180498c5SWilliam A. Kennington III     }
51e94c9ffcSWilliam A. Kennington III 
52*57dfea94SJishnu CM     ConfigIntf::dnsEnabled(getDHCPProp(conf, type, "UseDNS"), true);
53*57dfea94SJishnu CM     ConfigIntf::ntpEnabled(getDHCPProp(conf, type, "UseNTP"), true);
54*57dfea94SJishnu CM     ConfigIntf::hostNameEnabled(getDHCPProp(conf, type, "UseHostname"), true);
55*57dfea94SJishnu CM     if (type == DHCPType::v4)
56*57dfea94SJishnu CM     {
57*57dfea94SJishnu CM         ConfigIntf::sendHostNameEnabled(getDHCPProp(conf, type, "SendHostname"),
58*57dfea94SJishnu CM                                         true);
59*57dfea94SJishnu CM     }
60*57dfea94SJishnu CM 
61e94c9ffcSWilliam A. Kennington III     emit_object_added();
62e94c9ffcSWilliam A. Kennington III }
63e94c9ffcSWilliam A. Kennington III 
641bbe3d1eSWilliam A. Kennington III bool Configuration::sendHostNameEnabled(bool value)
651bbe3d1eSWilliam A. Kennington III {
661bbe3d1eSWilliam A. Kennington III     if (value == sendHostNameEnabled())
671bbe3d1eSWilliam A. Kennington III     {
681bbe3d1eSWilliam A. Kennington III         return value;
691bbe3d1eSWilliam A. Kennington III     }
701bbe3d1eSWilliam A. Kennington III 
711bbe3d1eSWilliam A. Kennington III     auto name = ConfigIntf::sendHostNameEnabled(value);
72*57dfea94SJishnu CM     parent.get().writeConfigurationFile();
73*57dfea94SJishnu CM     parent.get().reloadConfigs();
741bbe3d1eSWilliam A. Kennington III     return name;
751bbe3d1eSWilliam A. Kennington III }
761bbe3d1eSWilliam A. Kennington III 
771bbe3d1eSWilliam A. Kennington III bool Configuration::hostNameEnabled(bool value)
781bbe3d1eSWilliam A. Kennington III {
791bbe3d1eSWilliam A. Kennington III     if (value == hostNameEnabled())
801bbe3d1eSWilliam A. Kennington III     {
811bbe3d1eSWilliam A. Kennington III         return value;
821bbe3d1eSWilliam A. Kennington III     }
831bbe3d1eSWilliam A. Kennington III 
841bbe3d1eSWilliam A. Kennington III     auto name = ConfigIntf::hostNameEnabled(value);
85*57dfea94SJishnu CM     parent.get().writeConfigurationFile();
86*57dfea94SJishnu CM     parent.get().reloadConfigs();
871bbe3d1eSWilliam A. Kennington III 
881bbe3d1eSWilliam A. Kennington III     return name;
891bbe3d1eSWilliam A. Kennington III }
901bbe3d1eSWilliam A. Kennington III 
911bbe3d1eSWilliam A. Kennington III bool Configuration::ntpEnabled(bool value)
921bbe3d1eSWilliam A. Kennington III {
931bbe3d1eSWilliam A. Kennington III     if (value == ntpEnabled())
941bbe3d1eSWilliam A. Kennington III     {
951bbe3d1eSWilliam A. Kennington III         return value;
961bbe3d1eSWilliam A. Kennington III     }
971bbe3d1eSWilliam A. Kennington III 
981bbe3d1eSWilliam A. Kennington III     auto ntp = ConfigIntf::ntpEnabled(value);
99*57dfea94SJishnu CM     parent.get().writeConfigurationFile();
100*57dfea94SJishnu CM     parent.get().reloadConfigs();
1011bbe3d1eSWilliam A. Kennington III 
1021bbe3d1eSWilliam A. Kennington III     return ntp;
1031bbe3d1eSWilliam A. Kennington III }
1041bbe3d1eSWilliam A. Kennington III 
1051bbe3d1eSWilliam A. Kennington III bool Configuration::dnsEnabled(bool value)
1061bbe3d1eSWilliam A. Kennington III {
1071bbe3d1eSWilliam A. Kennington III     if (value == dnsEnabled())
1081bbe3d1eSWilliam A. Kennington III     {
1091bbe3d1eSWilliam A. Kennington III         return value;
1101bbe3d1eSWilliam A. Kennington III     }
1111bbe3d1eSWilliam A. Kennington III 
1121bbe3d1eSWilliam A. Kennington III     auto dns = ConfigIntf::dnsEnabled(value);
113*57dfea94SJishnu CM     parent.get().writeConfigurationFile();
114*57dfea94SJishnu CM     parent.get().reloadConfigs();
1151bbe3d1eSWilliam A. Kennington III 
1161bbe3d1eSWilliam A. Kennington III     return dns;
1171bbe3d1eSWilliam A. Kennington III }
1181bbe3d1eSWilliam A. Kennington III 
1191bbe3d1eSWilliam A. Kennington III } // namespace dhcp
1201bbe3d1eSWilliam A. Kennington III } // namespace network
1211bbe3d1eSWilliam A. Kennington III } // namespace phosphor
122