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), |
Jishnu CM | 57dfea9 | 2023-05-05 06:07:26 -0500 | [diff] [blame^] | 29 | parent(parent), type(type) |
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 | |
Jishnu CM | 57dfea9 | 2023-05-05 06:07:26 -0500 | [diff] [blame^] | 52 | 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 III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 61 | emit_object_added(); |
| 62 | } |
| 63 | |
Nagaraju Goruganti | e8fca1d | 2018-02-05 20:32:45 -0600 | [diff] [blame] | 64 | bool Configuration::sendHostNameEnabled(bool value) |
| 65 | { |
| 66 | if (value == sendHostNameEnabled()) |
| 67 | { |
| 68 | return value; |
| 69 | } |
| 70 | |
| 71 | auto name = ConfigIntf::sendHostNameEnabled(value); |
Jishnu CM | 57dfea9 | 2023-05-05 06:07:26 -0500 | [diff] [blame^] | 72 | parent.get().writeConfigurationFile(); |
| 73 | parent.get().reloadConfigs(); |
Nagaraju Goruganti | e8fca1d | 2018-02-05 20:32:45 -0600 | [diff] [blame] | 74 | return name; |
| 75 | } |
| 76 | |
Ratan Gupta | 935bc33 | 2017-07-11 17:47:14 +0530 | [diff] [blame] | 77 | bool Configuration::hostNameEnabled(bool value) |
| 78 | { |
| 79 | if (value == hostNameEnabled()) |
| 80 | { |
| 81 | return value; |
| 82 | } |
| 83 | |
| 84 | auto name = ConfigIntf::hostNameEnabled(value); |
Jishnu CM | 57dfea9 | 2023-05-05 06:07:26 -0500 | [diff] [blame^] | 85 | parent.get().writeConfigurationFile(); |
| 86 | parent.get().reloadConfigs(); |
Ratan Gupta | 935bc33 | 2017-07-11 17:47:14 +0530 | [diff] [blame] | 87 | |
| 88 | return name; |
| 89 | } |
| 90 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 91 | bool Configuration::ntpEnabled(bool value) |
Ratan Gupta | 935bc33 | 2017-07-11 17:47:14 +0530 | [diff] [blame] | 92 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 93 | if (value == ntpEnabled()) |
Ratan Gupta | 935bc33 | 2017-07-11 17:47:14 +0530 | [diff] [blame] | 94 | { |
| 95 | return value; |
| 96 | } |
| 97 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 98 | auto ntp = ConfigIntf::ntpEnabled(value); |
Jishnu CM | 57dfea9 | 2023-05-05 06:07:26 -0500 | [diff] [blame^] | 99 | parent.get().writeConfigurationFile(); |
| 100 | parent.get().reloadConfigs(); |
Ratan Gupta | 935bc33 | 2017-07-11 17:47:14 +0530 | [diff] [blame] | 101 | |
| 102 | return ntp; |
| 103 | } |
| 104 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 105 | bool Configuration::dnsEnabled(bool value) |
Ratan Gupta | 935bc33 | 2017-07-11 17:47:14 +0530 | [diff] [blame] | 106 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 107 | if (value == dnsEnabled()) |
Ratan Gupta | 935bc33 | 2017-07-11 17:47:14 +0530 | [diff] [blame] | 108 | { |
| 109 | return value; |
| 110 | } |
| 111 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 112 | auto dns = ConfigIntf::dnsEnabled(value); |
Jishnu CM | 57dfea9 | 2023-05-05 06:07:26 -0500 | [diff] [blame^] | 113 | parent.get().writeConfigurationFile(); |
| 114 | parent.get().reloadConfigs(); |
Ratan Gupta | 935bc33 | 2017-07-11 17:47:14 +0530 | [diff] [blame] | 115 | |
| 116 | return dns; |
| 117 | } |
| 118 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 119 | } // namespace dhcp |
| 120 | } // namespace network |
| 121 | } // namespace phosphor |