1 #include "dhcp_configuration.hpp"
2 
3 #include "config_parser.hpp"
4 #include "network_manager.hpp"
5 
6 #include <sys/stat.h>
7 
8 #include <phosphor-logging/elog-errors.hpp>
9 #include <phosphor-logging/lg2.hpp>
10 #include <xyz/openbmc_project/Common/error.hpp>
11 
12 #include <filesystem>
13 
14 namespace phosphor
15 {
16 namespace network
17 {
18 namespace dhcp
19 {
20 
21 using namespace phosphor::network;
22 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
23 
24 Configuration::Configuration(sdbusplus::bus_t& bus,
25                              stdplus::const_zstring objPath,
26                              stdplus::PinnedRef<EthernetInterface> parent,
27                              DHCPType type) :
28     Iface(bus, objPath.c_str(), Iface::action::defer_emit),
29     parent(parent), type(type)
30 {
31     config::Parser conf;
32     std::filesystem::directory_entry newest_file;
33     time_t newest_time = 0;
34     for (const auto& dirent : std::filesystem::directory_iterator(
35              parent.get().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, type, "UseDNS"), true);
53     ConfigIntf::ntpEnabled(getDHCPProp(conf, type, "UseNTP"), true);
54     ConfigIntf::hostNameEnabled(getDHCPProp(conf, type, "UseHostname"), true);
55     if (type == DHCPType::v4)
56     {
57         ConfigIntf::sendHostNameEnabled(getDHCPProp(conf, type, "SendHostname"),
58                                         true);
59     }
60 
61     emit_object_added();
62 }
63 
64 bool Configuration::sendHostNameEnabled(bool value)
65 {
66     if (value == sendHostNameEnabled())
67     {
68         return value;
69     }
70 
71     auto name = ConfigIntf::sendHostNameEnabled(value);
72     parent.get().writeConfigurationFile();
73     parent.get().reloadConfigs();
74     return name;
75 }
76 
77 bool Configuration::hostNameEnabled(bool value)
78 {
79     if (value == hostNameEnabled())
80     {
81         return value;
82     }
83 
84     auto name = ConfigIntf::hostNameEnabled(value);
85     parent.get().writeConfigurationFile();
86     parent.get().reloadConfigs();
87 
88     return name;
89 }
90 
91 bool Configuration::ntpEnabled(bool value)
92 {
93     if (value == ntpEnabled())
94     {
95         return value;
96     }
97 
98     auto ntp = ConfigIntf::ntpEnabled(value);
99     parent.get().writeConfigurationFile();
100     parent.get().reloadConfigs();
101 
102     return ntp;
103 }
104 
105 bool Configuration::dnsEnabled(bool value)
106 {
107     if (value == dnsEnabled())
108     {
109         return value;
110     }
111 
112     auto dns = ConfigIntf::dnsEnabled(value);
113     parent.get().writeConfigurationFile();
114     parent.get().reloadConfigs();
115 
116     return dns;
117 }
118 
119 } // namespace dhcp
120 } // namespace network
121 } // namespace phosphor
122