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, Manager& parent) :
27     Iface(bus, objPath.c_str(), Iface::action::defer_emit),
28     bus(bus), manager(parent)
29 {
30     config::Parser conf;
31     std::filesystem::directory_entry newest_file;
32     time_t newest_time = 0;
33     for (const auto& dirent :
34          std::filesystem::directory_iterator(manager.getConfDir()))
35     {
36         struct stat st = {};
37         stat(dirent.path().native().c_str(), &st);
38         if (st.st_mtime > newest_time)
39         {
40             newest_file = dirent;
41             newest_time = st.st_mtime;
42         }
43     }
44     if (newest_file != std::filesystem::directory_entry{})
45     {
46         log<level::INFO>(fmt::format("Using DHCP options from {}",
47                                      newest_file.path().native())
48                              .c_str());
49         conf.setFile(newest_file.path());
50     }
51 
52     ConfigIntf::dnsEnabled(getDHCPProp(conf, "UseDNS"), true);
53     ConfigIntf::ntpEnabled(getDHCPProp(conf, "UseNTP"), true);
54     ConfigIntf::hostNameEnabled(getDHCPProp(conf, "UseHostname"), true);
55     ConfigIntf::sendHostNameEnabled(getDHCPProp(conf, "SendHostname"), true);
56     emit_object_added();
57 }
58 
59 bool Configuration::sendHostNameEnabled(bool value)
60 {
61     if (value == sendHostNameEnabled())
62     {
63         return value;
64     }
65 
66     auto name = ConfigIntf::sendHostNameEnabled(value);
67 
68     manager.writeToConfigurationFile();
69     manager.reloadConfigs();
70 
71     return name;
72 }
73 
74 bool Configuration::hostNameEnabled(bool value)
75 {
76     if (value == hostNameEnabled())
77     {
78         return value;
79     }
80 
81     auto name = ConfigIntf::hostNameEnabled(value);
82     manager.writeToConfigurationFile();
83     manager.reloadConfigs();
84 
85     return name;
86 }
87 
88 bool Configuration::ntpEnabled(bool value)
89 {
90     if (value == ntpEnabled())
91     {
92         return value;
93     }
94 
95     auto ntp = ConfigIntf::ntpEnabled(value);
96     manager.writeToConfigurationFile();
97     manager.reloadConfigs();
98 
99     return ntp;
100 }
101 
102 bool Configuration::dnsEnabled(bool value)
103 {
104     if (value == dnsEnabled())
105     {
106         return value;
107     }
108 
109     auto dns = ConfigIntf::dnsEnabled(value);
110     manager.writeToConfigurationFile();
111     manager.reloadConfigs();
112 
113     return dns;
114 }
115 
116 } // namespace dhcp
117 } // namespace network
118 } // namespace phosphor
119