blob: 1e4e9992fbd74369fdf1fcd2bc2526b32e6d9673 [file] [log] [blame]
Ratan Gupta935bc332017-07-11 17:47:14 +05301#include "dhcp_configuration.hpp"
Patrick Venture189d44e2018-07-09 12:30:59 -07002
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -07003#include "config_parser.hpp"
Ratan Gupta935bc332017-07-11 17:47:14 +05304#include "network_manager.hpp"
Patrick Venture189d44e2018-07-09 12:30:59 -07005
William A. Kennington III180498c2022-11-14 18:11:45 -08006#include <sys/stat.h>
7
Nagaraju Goruganti210420a2018-03-07 09:22:28 -06008#include <phosphor-logging/elog-errors.hpp>
Jagpal Singh Gilla2947b42023-04-17 21:10:14 -07009#include <phosphor-logging/lg2.hpp>
Patrick Venture189d44e2018-07-09 12:30:59 -070010#include <xyz/openbmc_project/Common/error.hpp>
Ratan Gupta935bc332017-07-11 17:47:14 +053011
Patrick Williams89d734b2023-05-10 07:50:25 -050012#include <filesystem>
13
Ratan Gupta935bc332017-07-11 17:47:14 +053014namespace phosphor
15{
16namespace network
17{
18namespace dhcp
19{
20
Nagaraju Goruganti210420a2018-03-07 09:22:28 -060021using namespace phosphor::network;
Nagaraju Goruganti210420a2018-03-07 09:22:28 -060022using namespace sdbusplus::xyz::openbmc_project::Common::Error;
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -070023
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -070024Configuration::Configuration(sdbusplus::bus_t& bus,
William A. Kennington III9ede1b72022-11-21 01:59:28 -080025 stdplus::const_zstring objPath,
Jishnu CM57dfea92023-05-05 06:07:26 -050026 stdplus::PinnedRef<EthernetInterface> parent,
27 DHCPType type) :
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -070028 Iface(bus, objPath.c_str(), Iface::action::defer_emit),
William A. Kennington IIIf179e702024-04-17 00:50:28 -070029 parent(parent)
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -070030{
31 config::Parser conf;
William A. Kennington III180498c2022-11-14 18:11:45 -080032 std::filesystem::directory_entry newest_file;
33 time_t newest_time = 0;
Jishnu CM57dfea92023-05-05 06:07:26 -050034 for (const auto& dirent : std::filesystem::directory_iterator(
35 parent.get().manager.get().getConfDir()))
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -070036 {
William A. Kennington III180498c2022-11-14 18:11:45 -080037 struct stat st = {};
38 stat(dirent.path().native().c_str(), &st);
39 if (st.st_mtime > newest_time)
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -070040 {
William A. Kennington III180498c2022-11-14 18:11:45 -080041 newest_file = dirent;
42 newest_time = st.st_mtime;
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -070043 }
44 }
William A. Kennington III180498c2022-11-14 18:11:45 -080045 if (newest_file != std::filesystem::directory_entry{})
46 {
William A. Kennington III1d25ca42023-05-30 16:55:28 -070047 lg2::info("Using DHCP options from {CFG_FILE}", "CFG_FILE",
Jagpal Singh Gilla2947b42023-04-17 21:10:14 -070048 newest_file.path().native());
William A. Kennington III180498c2022-11-14 18:11:45 -080049 conf.setFile(newest_file.path());
50 }
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -070051
Ravi Tejab6595b22024-02-23 05:00:23 -060052 ConfigIntf::domainEnabled(getDHCPProp(conf, type, "UseDomains"), true);
Jishnu CM57dfea92023-05-05 06:07:26 -050053 ConfigIntf::dnsEnabled(getDHCPProp(conf, type, "UseDNS"), true);
54 ConfigIntf::ntpEnabled(getDHCPProp(conf, type, "UseNTP"), true);
55 ConfigIntf::hostNameEnabled(getDHCPProp(conf, type, "UseHostname"), true);
William A. Kennington III44937b12024-04-17 00:28:20 -070056 ConfigIntf::sendHostNameEnabled(getDHCPProp(conf, type, "SendHostname"),
57 true);
Jishnu CM57dfea92023-05-05 06:07:26 -050058
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -070059 emit_object_added();
60}
61
Nagaraju Gorugantie8fca1d2018-02-05 20:32:45 -060062bool Configuration::sendHostNameEnabled(bool value)
63{
64 if (value == sendHostNameEnabled())
65 {
66 return value;
67 }
68
69 auto name = ConfigIntf::sendHostNameEnabled(value);
Jishnu CM57dfea92023-05-05 06:07:26 -050070 parent.get().writeConfigurationFile();
71 parent.get().reloadConfigs();
Nagaraju Gorugantie8fca1d2018-02-05 20:32:45 -060072 return name;
73}
74
Ratan Gupta935bc332017-07-11 17:47:14 +053075bool Configuration::hostNameEnabled(bool value)
76{
77 if (value == hostNameEnabled())
78 {
79 return value;
80 }
81
82 auto name = ConfigIntf::hostNameEnabled(value);
Jishnu CM57dfea92023-05-05 06:07:26 -050083 parent.get().writeConfigurationFile();
84 parent.get().reloadConfigs();
Ratan Gupta935bc332017-07-11 17:47:14 +053085
86 return name;
87}
88
Patrick Williams6aef7692021-05-01 06:39:41 -050089bool Configuration::ntpEnabled(bool value)
Ratan Gupta935bc332017-07-11 17:47:14 +053090{
Patrick Williams6aef7692021-05-01 06:39:41 -050091 if (value == ntpEnabled())
Ratan Gupta935bc332017-07-11 17:47:14 +053092 {
93 return value;
94 }
95
Patrick Williams6aef7692021-05-01 06:39:41 -050096 auto ntp = ConfigIntf::ntpEnabled(value);
Jishnu CM57dfea92023-05-05 06:07:26 -050097 parent.get().writeConfigurationFile();
98 parent.get().reloadConfigs();
Ratan Gupta935bc332017-07-11 17:47:14 +053099
100 return ntp;
101}
102
Patrick Williams6aef7692021-05-01 06:39:41 -0500103bool Configuration::dnsEnabled(bool value)
Ratan Gupta935bc332017-07-11 17:47:14 +0530104{
Patrick Williams6aef7692021-05-01 06:39:41 -0500105 if (value == dnsEnabled())
Ratan Gupta935bc332017-07-11 17:47:14 +0530106 {
107 return value;
108 }
109
Patrick Williams6aef7692021-05-01 06:39:41 -0500110 auto dns = ConfigIntf::dnsEnabled(value);
Jishnu CM57dfea92023-05-05 06:07:26 -0500111 parent.get().writeConfigurationFile();
112 parent.get().reloadConfigs();
Ratan Gupta935bc332017-07-11 17:47:14 +0530113
114 return dns;
115}
116
Ravi Tejab6595b22024-02-23 05:00:23 -0600117bool 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
Gunnar Mills57d9c502018-09-14 14:42:34 -0500131} // namespace dhcp
132} // namespace network
133} // namespace phosphor