xref: /openbmc/phosphor-networkd/src/dhcp_configuration.cpp (revision 44937b1adbe49f0876492f566b3cc7e0c71ea4ad)
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     ConfigIntf::sendHostNameEnabled(getDHCPProp(conf, type, "SendHostname"),
57                                     true);
58 
59     emit_object_added();
60 }
61 
62 bool Configuration::sendHostNameEnabled(bool value)
63 {
64     if (value == sendHostNameEnabled())
65     {
66         return value;
67     }
68 
69     auto name = ConfigIntf::sendHostNameEnabled(value);
70     parent.get().writeConfigurationFile();
71     parent.get().reloadConfigs();
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     parent.get().writeConfigurationFile();
84     parent.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     parent.get().writeConfigurationFile();
98     parent.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     parent.get().writeConfigurationFile();
112     parent.get().reloadConfigs();
113 
114     return dns;
115 }
116 
117 bool Configuration::domainEnabled(bool value)
118 {
119     if (value == domainEnabled())
120     {
121         return value;
122     }
123 
124     auto domain = ConfigIntf::domainEnabled(value);
125     parent.get().writeConfigurationFile();
126     parent.get().reloadConfigs();
127 
128     return domain;
129 }
130 
131 } // namespace dhcp
132 } // namespace network
133 } // namespace phosphor
134