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)
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::domainEnabled(getDHCPProp(conf, type, "UseDomains"), true);
53     ConfigIntf::dnsEnabled(getDHCPProp(conf, type, "UseDNS"), true);
54     ConfigIntf::ntpEnabled(getDHCPProp(conf, type, "UseNTP"), true);
55     ConfigIntf::hostNameEnabled(getDHCPProp(conf, type, "UseHostname"), true);
56     if (type == DHCPType::v4)
57     {
58         ConfigIntf::sendHostNameEnabled(getDHCPProp(conf, type, "SendHostname"),
59                                         true);
60     }
61 
62     emit_object_added();
63 }
64 
65 bool Configuration::sendHostNameEnabled(bool value)
66 {
67     if (value == sendHostNameEnabled())
68     {
69         return value;
70     }
71 
72     auto name = ConfigIntf::sendHostNameEnabled(value);
73     parent.get().writeConfigurationFile();
74     parent.get().reloadConfigs();
75     return name;
76 }
77 
78 bool Configuration::hostNameEnabled(bool value)
79 {
80     if (value == hostNameEnabled())
81     {
82         return value;
83     }
84 
85     auto name = ConfigIntf::hostNameEnabled(value);
86     parent.get().writeConfigurationFile();
87     parent.get().reloadConfigs();
88 
89     return name;
90 }
91 
92 bool Configuration::ntpEnabled(bool value)
93 {
94     if (value == ntpEnabled())
95     {
96         return value;
97     }
98 
99     auto ntp = ConfigIntf::ntpEnabled(value);
100     parent.get().writeConfigurationFile();
101     parent.get().reloadConfigs();
102 
103     return ntp;
104 }
105 
106 bool Configuration::dnsEnabled(bool value)
107 {
108     if (value == dnsEnabled())
109     {
110         return value;
111     }
112 
113     auto dns = ConfigIntf::dnsEnabled(value);
114     parent.get().writeConfigurationFile();
115     parent.get().reloadConfigs();
116 
117     return dns;
118 }
119 
120 bool Configuration::domainEnabled(bool value)
121 {
122     if (value == domainEnabled())
123     {
124         return value;
125     }
126 
127     auto domain = ConfigIntf::domainEnabled(value);
128     parent.get().writeConfigurationFile();
129     parent.get().reloadConfigs();
130 
131     return domain;
132 }
133 
134 } // namespace dhcp
135 } // namespace network
136 } // namespace phosphor
137