blob: 04a56a06ecf937a70fd564b3d8c14af15bef6eed [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),
Jishnu CM57dfea92023-05-05 06:07:26 -050029 parent(parent), type(type)
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
Jishnu CM57dfea92023-05-05 06:07:26 -050052 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
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -070061 emit_object_added();
62}
63
Nagaraju Gorugantie8fca1d2018-02-05 20:32:45 -060064bool Configuration::sendHostNameEnabled(bool value)
65{
66 if (value == sendHostNameEnabled())
67 {
68 return value;
69 }
70
71 auto name = ConfigIntf::sendHostNameEnabled(value);
Jishnu CM57dfea92023-05-05 06:07:26 -050072 parent.get().writeConfigurationFile();
73 parent.get().reloadConfigs();
Nagaraju Gorugantie8fca1d2018-02-05 20:32:45 -060074 return name;
75}
76
Ratan Gupta935bc332017-07-11 17:47:14 +053077bool Configuration::hostNameEnabled(bool value)
78{
79 if (value == hostNameEnabled())
80 {
81 return value;
82 }
83
84 auto name = ConfigIntf::hostNameEnabled(value);
Jishnu CM57dfea92023-05-05 06:07:26 -050085 parent.get().writeConfigurationFile();
86 parent.get().reloadConfigs();
Ratan Gupta935bc332017-07-11 17:47:14 +053087
88 return name;
89}
90
Patrick Williams6aef7692021-05-01 06:39:41 -050091bool Configuration::ntpEnabled(bool value)
Ratan Gupta935bc332017-07-11 17:47:14 +053092{
Patrick Williams6aef7692021-05-01 06:39:41 -050093 if (value == ntpEnabled())
Ratan Gupta935bc332017-07-11 17:47:14 +053094 {
95 return value;
96 }
97
Patrick Williams6aef7692021-05-01 06:39:41 -050098 auto ntp = ConfigIntf::ntpEnabled(value);
Jishnu CM57dfea92023-05-05 06:07:26 -050099 parent.get().writeConfigurationFile();
100 parent.get().reloadConfigs();
Ratan Gupta935bc332017-07-11 17:47:14 +0530101
102 return ntp;
103}
104
Patrick Williams6aef7692021-05-01 06:39:41 -0500105bool Configuration::dnsEnabled(bool value)
Ratan Gupta935bc332017-07-11 17:47:14 +0530106{
Patrick Williams6aef7692021-05-01 06:39:41 -0500107 if (value == dnsEnabled())
Ratan Gupta935bc332017-07-11 17:47:14 +0530108 {
109 return value;
110 }
111
Patrick Williams6aef7692021-05-01 06:39:41 -0500112 auto dns = ConfigIntf::dnsEnabled(value);
Jishnu CM57dfea92023-05-05 06:07:26 -0500113 parent.get().writeConfigurationFile();
114 parent.get().reloadConfigs();
Ratan Gupta935bc332017-07-11 17:47:14 +0530115
116 return dns;
117}
118
Gunnar Mills57d9c502018-09-14 14:42:34 -0500119} // namespace dhcp
120} // namespace network
121} // namespace phosphor