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 <phosphor-logging/elog-errors.hpp>
10 #include <phosphor-logging/lg2.hpp>
11 #include <xyz/openbmc_project/Common/error.hpp>
12 
13 #include <filesystem>
14 
15 namespace phosphor
16 {
17 namespace network
18 {
19 namespace dhcp
20 {
21 
22 using namespace phosphor::network;
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         lg2::info("Using DHCP options from {CFG_FILE}", "CFG_FILE",
48                   newest_file.path().native());
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.get().writeToConfigurationFile();
69     manager.get().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.get().writeToConfigurationFile();
83     manager.get().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.get().writeToConfigurationFile();
97     manager.get().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.get().writeToConfigurationFile();
111     manager.get().reloadConfigs();
112 
113     return dns;
114 }
115 
116 } // namespace dhcp
117 } // namespace network
118 } // namespace phosphor
119