Ratan Gupta | 935bc33 | 2017-07-11 17:47:14 +0530 | [diff] [blame] | 1 | #include "dhcp_configuration.hpp" |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 2 | |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 3 | #include "config_parser.hpp" |
Ratan Gupta | 935bc33 | 2017-07-11 17:47:14 +0530 | [diff] [blame] | 4 | #include "network_manager.hpp" |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 5 | |
William A. Kennington III | 180498c | 2022-11-14 18:11:45 -0800 | [diff] [blame] | 6 | #include <sys/stat.h> |
| 7 | |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 8 | #include <phosphor-logging/elog-errors.hpp> |
Jagpal Singh Gill | a2947b4 | 2023-04-17 21:10:14 -0700 | [diff] [blame] | 9 | #include <phosphor-logging/lg2.hpp> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 10 | #include <xyz/openbmc_project/Common/error.hpp> |
Ratan Gupta | 935bc33 | 2017-07-11 17:47:14 +0530 | [diff] [blame] | 11 | |
Patrick Williams | 89d734b | 2023-05-10 07:50:25 -0500 | [diff] [blame] | 12 | #include <filesystem> |
| 13 | |
Ratan Gupta | 935bc33 | 2017-07-11 17:47:14 +0530 | [diff] [blame] | 14 | namespace phosphor |
| 15 | { |
| 16 | namespace network |
| 17 | { |
| 18 | namespace dhcp |
| 19 | { |
| 20 | |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 21 | using namespace phosphor::network; |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 22 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 23 | |
William A. Kennington III | be3bd2f | 2022-10-11 14:11:27 -0700 | [diff] [blame] | 24 | Configuration::Configuration(sdbusplus::bus_t& bus, |
William A. Kennington III | 9ede1b7 | 2022-11-21 01:59:28 -0800 | [diff] [blame] | 25 | stdplus::const_zstring objPath, |
Jishnu CM | 57dfea9 | 2023-05-05 06:07:26 -0500 | [diff] [blame] | 26 | stdplus::PinnedRef<EthernetInterface> parent, |
| 27 | DHCPType type) : |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 28 | Iface(bus, objPath.c_str(), Iface::action::defer_emit), |
William A. Kennington III | f179e70 | 2024-04-17 00:50:28 -0700 | [diff] [blame] | 29 | parent(parent) |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 30 | { |
| 31 | config::Parser conf; |
William A. Kennington III | 180498c | 2022-11-14 18:11:45 -0800 | [diff] [blame] | 32 | std::filesystem::directory_entry newest_file; |
| 33 | time_t newest_time = 0; |
Jishnu CM | 57dfea9 | 2023-05-05 06:07:26 -0500 | [diff] [blame] | 34 | for (const auto& dirent : std::filesystem::directory_iterator( |
| 35 | parent.get().manager.get().getConfDir())) |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 36 | { |
William A. Kennington III | 180498c | 2022-11-14 18:11:45 -0800 | [diff] [blame] | 37 | struct stat st = {}; |
| 38 | stat(dirent.path().native().c_str(), &st); |
| 39 | if (st.st_mtime > newest_time) |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 40 | { |
William A. Kennington III | 180498c | 2022-11-14 18:11:45 -0800 | [diff] [blame] | 41 | newest_file = dirent; |
| 42 | newest_time = st.st_mtime; |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 43 | } |
| 44 | } |
William A. Kennington III | 180498c | 2022-11-14 18:11:45 -0800 | [diff] [blame] | 45 | if (newest_file != std::filesystem::directory_entry{}) |
| 46 | { |
William A. Kennington III | 1d25ca4 | 2023-05-30 16:55:28 -0700 | [diff] [blame] | 47 | lg2::info("Using DHCP options from {CFG_FILE}", "CFG_FILE", |
Jagpal Singh Gill | a2947b4 | 2023-04-17 21:10:14 -0700 | [diff] [blame] | 48 | newest_file.path().native()); |
William A. Kennington III | 180498c | 2022-11-14 18:11:45 -0800 | [diff] [blame] | 49 | conf.setFile(newest_file.path()); |
| 50 | } |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 51 | |
Ravi Teja | b6595b2 | 2024-02-23 05:00:23 -0600 | [diff] [blame] | 52 | ConfigIntf::domainEnabled(getDHCPProp(conf, type, "UseDomains"), true); |
Jishnu CM | 57dfea9 | 2023-05-05 06:07:26 -0500 | [diff] [blame] | 53 | 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 III | 44937b1 | 2024-04-17 00:28:20 -0700 | [diff] [blame^] | 56 | ConfigIntf::sendHostNameEnabled(getDHCPProp(conf, type, "SendHostname"), |
| 57 | true); |
Jishnu CM | 57dfea9 | 2023-05-05 06:07:26 -0500 | [diff] [blame] | 58 | |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 59 | emit_object_added(); |
| 60 | } |
| 61 | |
Nagaraju Goruganti | e8fca1d | 2018-02-05 20:32:45 -0600 | [diff] [blame] | 62 | bool Configuration::sendHostNameEnabled(bool value) |
| 63 | { |
| 64 | if (value == sendHostNameEnabled()) |
| 65 | { |
| 66 | return value; |
| 67 | } |
| 68 | |
| 69 | auto name = ConfigIntf::sendHostNameEnabled(value); |
Jishnu CM | 57dfea9 | 2023-05-05 06:07:26 -0500 | [diff] [blame] | 70 | parent.get().writeConfigurationFile(); |
| 71 | parent.get().reloadConfigs(); |
Nagaraju Goruganti | e8fca1d | 2018-02-05 20:32:45 -0600 | [diff] [blame] | 72 | return name; |
| 73 | } |
| 74 | |
Ratan Gupta | 935bc33 | 2017-07-11 17:47:14 +0530 | [diff] [blame] | 75 | bool Configuration::hostNameEnabled(bool value) |
| 76 | { |
| 77 | if (value == hostNameEnabled()) |
| 78 | { |
| 79 | return value; |
| 80 | } |
| 81 | |
| 82 | auto name = ConfigIntf::hostNameEnabled(value); |
Jishnu CM | 57dfea9 | 2023-05-05 06:07:26 -0500 | [diff] [blame] | 83 | parent.get().writeConfigurationFile(); |
| 84 | parent.get().reloadConfigs(); |
Ratan Gupta | 935bc33 | 2017-07-11 17:47:14 +0530 | [diff] [blame] | 85 | |
| 86 | return name; |
| 87 | } |
| 88 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 89 | bool Configuration::ntpEnabled(bool value) |
Ratan Gupta | 935bc33 | 2017-07-11 17:47:14 +0530 | [diff] [blame] | 90 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 91 | if (value == ntpEnabled()) |
Ratan Gupta | 935bc33 | 2017-07-11 17:47:14 +0530 | [diff] [blame] | 92 | { |
| 93 | return value; |
| 94 | } |
| 95 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 96 | auto ntp = ConfigIntf::ntpEnabled(value); |
Jishnu CM | 57dfea9 | 2023-05-05 06:07:26 -0500 | [diff] [blame] | 97 | parent.get().writeConfigurationFile(); |
| 98 | parent.get().reloadConfigs(); |
Ratan Gupta | 935bc33 | 2017-07-11 17:47:14 +0530 | [diff] [blame] | 99 | |
| 100 | return ntp; |
| 101 | } |
| 102 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 103 | bool Configuration::dnsEnabled(bool value) |
Ratan Gupta | 935bc33 | 2017-07-11 17:47:14 +0530 | [diff] [blame] | 104 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 105 | if (value == dnsEnabled()) |
Ratan Gupta | 935bc33 | 2017-07-11 17:47:14 +0530 | [diff] [blame] | 106 | { |
| 107 | return value; |
| 108 | } |
| 109 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 110 | auto dns = ConfigIntf::dnsEnabled(value); |
Jishnu CM | 57dfea9 | 2023-05-05 06:07:26 -0500 | [diff] [blame] | 111 | parent.get().writeConfigurationFile(); |
| 112 | parent.get().reloadConfigs(); |
Ratan Gupta | 935bc33 | 2017-07-11 17:47:14 +0530 | [diff] [blame] | 113 | |
| 114 | return dns; |
| 115 | } |
| 116 | |
Ravi Teja | b6595b2 | 2024-02-23 05:00:23 -0600 | [diff] [blame] | 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 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 131 | } // namespace dhcp |
| 132 | } // namespace network |
| 133 | } // namespace phosphor |