1 #include "config.h"
2 
3 #include "dhcp_configuration.hpp"
4 
5 #include "network_manager.hpp"
6 
7 #include <fmt/format.h>
8 
9 #include <phosphor-logging/elog-errors.hpp>
10 #include <phosphor-logging/log.hpp>
11 #include <xyz/openbmc_project/Common/error.hpp>
12 
13 namespace phosphor
14 {
15 namespace network
16 {
17 namespace dhcp
18 {
19 
20 using namespace phosphor::network;
21 using namespace phosphor::logging;
22 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
23 bool Configuration::sendHostNameEnabled(bool value)
24 {
25     if (value == sendHostNameEnabled())
26     {
27         return value;
28     }
29 
30     auto name = ConfigIntf::sendHostNameEnabled(value);
31 
32     manager.writeToConfigurationFile();
33     manager.reloadConfigs();
34 
35     return name;
36 }
37 
38 bool Configuration::hostNameEnabled(bool value)
39 {
40     if (value == hostNameEnabled())
41     {
42         return value;
43     }
44 
45     auto name = ConfigIntf::hostNameEnabled(value);
46     manager.writeToConfigurationFile();
47     manager.reloadConfigs();
48 
49     return name;
50 }
51 
52 bool Configuration::ntpEnabled(bool value)
53 {
54     if (value == ntpEnabled())
55     {
56         return value;
57     }
58 
59     auto ntp = ConfigIntf::ntpEnabled(value);
60     manager.writeToConfigurationFile();
61     manager.reloadConfigs();
62 
63     return ntp;
64 }
65 
66 bool Configuration::dnsEnabled(bool value)
67 {
68     if (value == dnsEnabled())
69     {
70         return value;
71     }
72 
73     auto dns = ConfigIntf::dnsEnabled(value);
74     manager.writeToConfigurationFile();
75     manager.reloadConfigs();
76 
77     return dns;
78 }
79 
80 bool Configuration::getDHCPPropFromConf(const std::string& prop)
81 {
82     fs::path confPath = manager.getConfDir();
83     auto interfaceStrList = getInterfaces();
84     // get the first interface name, we need it to know config file name.
85     auto interface = *interfaceStrList.begin();
86     auto fileName = systemd::config::networkFilePrefix + interface +
87                     systemd::config::networkFileSuffix;
88 
89     confPath /= fileName;
90     // systemd default behaviour is all DHCP fields should be enabled by
91     // default.
92     config::Parser parser(confPath);
93 
94     const auto& values = parser.getValues("DHCP", prop);
95     if (values.empty())
96     {
97         auto msg = fmt::format("Missing config section DHCP[{}]", prop);
98         log<level::NOTICE>(msg.c_str(), entry("PROP=%s", prop.c_str()));
99         return true;
100     }
101     return values.back() != "false";
102 }
103 } // namespace dhcp
104 } // namespace network
105 } // namespace phosphor
106