blob: 4acfa3d5079688c3225104d11b35922bb4467211 [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 IIIe94c9ffc2022-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 IIIe94c9ffc2022-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 IIIe94c9ffc2022-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 IIIe94c9ffc2022-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 IIIe94c9ffc2022-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 IIIe94c9ffc2022-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 IIIe94c9ffc2022-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 IIIe94c9ffc2022-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);
56 if (type == DHCPType::v4)
57 {
58 ConfigIntf::sendHostNameEnabled(getDHCPProp(conf, type, "SendHostname"),
59 true);
60 }
61
William A. Kennington IIIe94c9ffc2022-08-18 20:12:27 -070062 emit_object_added();
63}
64
Nagaraju Gorugantie8fca1d2018-02-05 20:32:45 -060065bool Configuration::sendHostNameEnabled(bool value)
66{
67 if (value == sendHostNameEnabled())
68 {
69 return value;
70 }
71
72 auto name = ConfigIntf::sendHostNameEnabled(value);
Jishnu CM57dfea92023-05-05 06:07:26 -050073 parent.get().writeConfigurationFile();
74 parent.get().reloadConfigs();
Nagaraju Gorugantie8fca1d2018-02-05 20:32:45 -060075 return name;
76}
77
Ratan Gupta935bc332017-07-11 17:47:14 +053078bool Configuration::hostNameEnabled(bool value)
79{
80 if (value == hostNameEnabled())
81 {
82 return value;
83 }
84
85 auto name = ConfigIntf::hostNameEnabled(value);
Jishnu CM57dfea92023-05-05 06:07:26 -050086 parent.get().writeConfigurationFile();
87 parent.get().reloadConfigs();
Ratan Gupta935bc332017-07-11 17:47:14 +053088
89 return name;
90}
91
Patrick Williams6aef7692021-05-01 06:39:41 -050092bool Configuration::ntpEnabled(bool value)
Ratan Gupta935bc332017-07-11 17:47:14 +053093{
Patrick Williams6aef7692021-05-01 06:39:41 -050094 if (value == ntpEnabled())
Ratan Gupta935bc332017-07-11 17:47:14 +053095 {
96 return value;
97 }
98
Patrick Williams6aef7692021-05-01 06:39:41 -050099 auto ntp = ConfigIntf::ntpEnabled(value);
Jishnu CM57dfea92023-05-05 06:07:26 -0500100 parent.get().writeConfigurationFile();
101 parent.get().reloadConfigs();
Ratan Gupta935bc332017-07-11 17:47:14 +0530102
103 return ntp;
104}
105
Patrick Williams6aef7692021-05-01 06:39:41 -0500106bool Configuration::dnsEnabled(bool value)
Ratan Gupta935bc332017-07-11 17:47:14 +0530107{
Patrick Williams6aef7692021-05-01 06:39:41 -0500108 if (value == dnsEnabled())
Ratan Gupta935bc332017-07-11 17:47:14 +0530109 {
110 return value;
111 }
112
Patrick Williams6aef7692021-05-01 06:39:41 -0500113 auto dns = ConfigIntf::dnsEnabled(value);
Jishnu CM57dfea92023-05-05 06:07:26 -0500114 parent.get().writeConfigurationFile();
115 parent.get().reloadConfigs();
Ratan Gupta935bc332017-07-11 17:47:14 +0530116
117 return dns;
118}
119
Ravi Tejab6595b22024-02-23 05:00:23 -0600120bool 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
Gunnar Mills57d9c502018-09-14 14:42:34 -0500134} // namespace dhcp
135} // namespace network
136} // namespace phosphor