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