blob: da5140a6aa19bfc7726fcea532ea3392c4b594cd [file] [log] [blame]
Ratan Gupta82e1ef92017-06-15 08:39:15 +05301#include "system_configuration.hpp"
Patrick Venture189d44e2018-07-09 12:30:59 -07002
3#include "config.h"
Ratan Gupta82e1ef92017-06-15 08:39:15 +05304#include "network_manager.hpp"
5#include "routing_table.hpp"
Ratan Gupta82e1ef92017-06-15 08:39:15 +05306
Ratan Gupta82e1ef92017-06-15 08:39:15 +05307#include <phosphor-logging/elog-errors.hpp>
Patrick Venture189d44e2018-07-09 12:30:59 -07008#include <phosphor-logging/log.hpp>
9#include <xyz/openbmc_project/Common/error.hpp>
Ratan Gupta82e1ef92017-06-15 08:39:15 +053010
11namespace phosphor
12{
13namespace network
14{
15
16// systemd service to kick start a target.
17constexpr auto HOSTNAMED_SERVICE = "org.freedesktop.hostname1";
18constexpr auto HOSTNAMED_SERVICE_PATH = "/org/freedesktop/hostname1";
19constexpr auto HOSTNAMED_INTERFACE = "org.freedesktop.hostname1";
20constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties";
21constexpr auto METHOD_GET = "Get";
22constexpr auto METHOD_SET = "SetStaticHostname";
23
24using namespace phosphor::logging;
25using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Gunnar Millsa3629222018-06-19 16:32:04 -050026using InvalidArgumentMetadata = xyz::openbmc_project::Common::InvalidArgument;
Ratan Gupta82e1ef92017-06-15 08:39:15 +053027
28using SystemConfigIntf =
29 sdbusplus::xyz::openbmc_project::Network::server::SystemConfiguration;
30
31SystemConfiguration::SystemConfiguration(sdbusplus::bus::bus& bus,
32 const std::string& objPath,
33 Manager& parent) :
34 Iface(bus, objPath.c_str(), true),
35 bus(bus),
36 manager(parent)
37{
38 auto name = getHostNameFromSystem();
39 route::Table routingTable;
40
41 SystemConfigIntf::hostName(name);
42 SystemConfigIntf::defaultGateway(routingTable.getDefaultGateway());
43
44 this->emit_object_added();
45}
46
47std::string SystemConfiguration::hostName(std::string name)
48{
49 if (SystemConfigIntf::hostName() == name)
50 {
51 return name;
52 }
53 auto method = bus.new_method_call(
54 HOSTNAMED_SERVICE,
55 HOSTNAMED_SERVICE_PATH,
56 HOSTNAMED_INTERFACE,
57 METHOD_SET);
58
59 method.append(name, true);
60
61 if (!bus.call(method))
62 {
63 log<level::ERR>("Failed to set the hostname");
64 report<InternalFailure>();
65 return SystemConfigIntf::hostName();
66 }
67
68 return SystemConfigIntf::hostName(name);
69}
70
71std::string SystemConfiguration::getHostNameFromSystem() const
72{
73 sdbusplus::message::variant<std::string> name;
74 auto method = bus.new_method_call(
75 HOSTNAMED_SERVICE,
76 HOSTNAMED_SERVICE_PATH,
77 PROPERTY_INTERFACE,
78 METHOD_GET);
79
80 method.append(HOSTNAMED_INTERFACE, "Hostname");
81
82 auto reply = bus.call(method);
83
84 if (reply)
85 {
86 reply.read(name);
87 }
88 else
89 {
90 log<level::ERR>("Failed to get hostname");
91 report<InternalFailure>();
92 return "";
93 }
94 return name.get<std::string>();
95}
96
97
98std::string SystemConfiguration::defaultGateway(std::string gateway)
99{
Nagaraju Gorugantie44acde2017-11-02 05:22:21 -0500100 auto gw = SystemConfigIntf::defaultGateway();
101 if (gw == gateway)
Ratan Gupta82e1ef92017-06-15 08:39:15 +0530102 {
Nagaraju Gorugantie44acde2017-11-02 05:22:21 -0500103 return gw;
Ratan Gupta82e1ef92017-06-15 08:39:15 +0530104 }
Nagaraju Gorugantie44acde2017-11-02 05:22:21 -0500105
106 if (!isValidIP(AF_INET, gateway))
107 {
108 log<level::ERR>("Not a valid Gateway",
109 entry("GATEWAY=%s", gateway.c_str()));
Gunnar Millsa3629222018-06-19 16:32:04 -0500110 elog<InvalidArgument>(InvalidArgumentMetadata::ARGUMENT_NAME("GATEWAY"),
111 InvalidArgumentMetadata::ARGUMENT_VALUE(
112 gateway.c_str()));
Nagaraju Gorugantie44acde2017-11-02 05:22:21 -0500113 }
114 gw = SystemConfigIntf::defaultGateway(gateway);
Ratan Gupta82e1ef92017-06-15 08:39:15 +0530115 manager.writeToConfigurationFile();
116 return gw;
117}
118
119}// namespace network
120}// namespace phosphor