1 #include "dhcp_configuration.hpp"
2 
3 #include "config_parser.hpp"
4 #include "network_manager.hpp"
5 #include "util.hpp"
6 
7 #include <sys/stat.h>
8 
9 #include <filesystem>
10 #include <phosphor-logging/elog-errors.hpp>
11 #include <phosphor-logging/log.hpp>
12 #include <xyz/openbmc_project/Common/error.hpp>
13 
14 namespace phosphor
15 {
16 namespace network
17 {
18 namespace dhcp
19 {
20 
21 using namespace phosphor::network;
22 using namespace phosphor::logging;
23 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
24 
25 Configuration::Configuration(sdbusplus::bus_t& bus,
26                              stdplus::const_zstring objPath,
27                              stdplus::PinnedRef<Manager> parent) :
28     Iface(bus, objPath.c_str(), Iface::action::defer_emit),
29     manager(parent)
30 {
31     config::Parser conf;
32     std::filesystem::directory_entry newest_file;
33     time_t newest_time = 0;
34     for (const auto& dirent :
35          std::filesystem::directory_iterator(manager.get().getConfDir()))
36     {
37         struct stat st = {};
38         stat(dirent.path().native().c_str(), &st);
39         if (st.st_mtime > newest_time)
40         {
41             newest_file = dirent;
42             newest_time = st.st_mtime;
43         }
44     }
45     if (newest_file != std::filesystem::directory_entry{})
46     {
47         log<level::INFO>(fmt::format("Using DHCP options from {}",
48                                      newest_file.path().native())
49                              .c_str());
50         conf.setFile(newest_file.path());
51     }
52 
53     ConfigIntf::dnsEnabled(getDHCPProp(conf, "UseDNS"), true);
54     ConfigIntf::ntpEnabled(getDHCPProp(conf, "UseNTP"), true);
55     ConfigIntf::hostNameEnabled(getDHCPProp(conf, "UseHostname"), true);
56     ConfigIntf::sendHostNameEnabled(getDHCPProp(conf, "SendHostname"), true);
57     emit_object_added();
58 }
59 
60 bool Configuration::sendHostNameEnabled(bool value)
61 {
62     if (value == sendHostNameEnabled())
63     {
64         return value;
65     }
66 
67     auto name = ConfigIntf::sendHostNameEnabled(value);
68 
69     manager.get().writeToConfigurationFile();
70     manager.get().reloadConfigs();
71 
72     return name;
73 }
74 
75 bool Configuration::hostNameEnabled(bool value)
76 {
77     if (value == hostNameEnabled())
78     {
79         return value;
80     }
81 
82     auto name = ConfigIntf::hostNameEnabled(value);
83     manager.get().writeToConfigurationFile();
84     manager.get().reloadConfigs();
85 
86     return name;
87 }
88 
89 bool Configuration::ntpEnabled(bool value)
90 {
91     if (value == ntpEnabled())
92     {
93         return value;
94     }
95 
96     auto ntp = ConfigIntf::ntpEnabled(value);
97     manager.get().writeToConfigurationFile();
98     manager.get().reloadConfigs();
99 
100     return ntp;
101 }
102 
103 bool Configuration::dnsEnabled(bool value)
104 {
105     if (value == dnsEnabled())
106     {
107         return value;
108     }
109 
110     auto dns = ConfigIntf::dnsEnabled(value);
111     manager.get().writeToConfigurationFile();
112     manager.get().reloadConfigs();
113 
114     return dns;
115 }
116 
117 } // namespace dhcp
118 } // namespace network
119 } // namespace phosphor
120