blob: 603fa15e60f757529d250db931adaf11842d3edb [file] [log] [blame]
Gunnar Mills57d9c502018-09-14 14:42:34 -05001#include "config.h"
2
Ratan Gupta82e1ef92017-06-15 08:39:15 +05303#include "system_configuration.hpp"
Patrick Venture189d44e2018-07-09 12:30:59 -07004
Ratan Gupta82e1ef92017-06-15 08:39:15 +05305#include "network_manager.hpp"
6#include "routing_table.hpp"
Ratan Gupta82e1ef92017-06-15 08:39:15 +05307
Ratan Gupta82e1ef92017-06-15 08:39:15 +05308#include <phosphor-logging/elog-errors.hpp>
Patrick Venture189d44e2018-07-09 12:30:59 -07009#include <phosphor-logging/log.hpp>
10#include <xyz/openbmc_project/Common/error.hpp>
Ratan Gupta82e1ef92017-06-15 08:39:15 +053011
12namespace phosphor
13{
14namespace network
15{
16
17// systemd service to kick start a target.
Gunnar Mills57d9c502018-09-14 14:42:34 -050018constexpr auto HOSTNAMED_SERVICE = "org.freedesktop.hostname1";
19constexpr auto HOSTNAMED_SERVICE_PATH = "/org/freedesktop/hostname1";
20constexpr auto HOSTNAMED_INTERFACE = "org.freedesktop.hostname1";
Ratan Gupta82e1ef92017-06-15 08:39:15 +053021constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties";
22constexpr auto METHOD_GET = "Get";
23constexpr auto METHOD_SET = "SetStaticHostname";
24
25using namespace phosphor::logging;
26using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Gunnar Millsa3629222018-06-19 16:32:04 -050027using InvalidArgumentMetadata = xyz::openbmc_project::Common::InvalidArgument;
Ratan Gupta82e1ef92017-06-15 08:39:15 +053028
29using SystemConfigIntf =
30 sdbusplus::xyz::openbmc_project::Network::server::SystemConfiguration;
31
32SystemConfiguration::SystemConfiguration(sdbusplus::bus::bus& bus,
33 const std::string& objPath,
34 Manager& parent) :
Gunnar Mills57d9c502018-09-14 14:42:34 -050035 Iface(bus, objPath.c_str(), true),
36 bus(bus), manager(parent)
Ratan Gupta82e1ef92017-06-15 08:39:15 +053037{
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 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050053 auto method = bus.new_method_call(HOSTNAMED_SERVICE, HOSTNAMED_SERVICE_PATH,
54 HOSTNAMED_INTERFACE, METHOD_SET);
Ratan Gupta82e1ef92017-06-15 08:39:15 +053055
56 method.append(name, true);
57
58 if (!bus.call(method))
59 {
60 log<level::ERR>("Failed to set the hostname");
61 report<InternalFailure>();
62 return SystemConfigIntf::hostName();
63 }
64
65 return SystemConfigIntf::hostName(name);
66}
67
68std::string SystemConfiguration::getHostNameFromSystem() const
69{
70 sdbusplus::message::variant<std::string> name;
Gunnar Mills57d9c502018-09-14 14:42:34 -050071 auto method = bus.new_method_call(HOSTNAMED_SERVICE, HOSTNAMED_SERVICE_PATH,
72 PROPERTY_INTERFACE, METHOD_GET);
Ratan Gupta82e1ef92017-06-15 08:39:15 +053073
74 method.append(HOSTNAMED_INTERFACE, "Hostname");
75
76 auto reply = bus.call(method);
77
78 if (reply)
79 {
80 reply.read(name);
81 }
82 else
83 {
84 log<level::ERR>("Failed to get hostname");
85 report<InternalFailure>();
86 return "";
87 }
William A. Kennington III79e44152018-11-06 16:46:01 -080088 return sdbusplus::message::variant_ns::get<std::string>(name);
Ratan Gupta82e1ef92017-06-15 08:39:15 +053089}
90
Ratan Gupta82e1ef92017-06-15 08:39:15 +053091std::string SystemConfiguration::defaultGateway(std::string gateway)
92{
Nagaraju Gorugantie44acde2017-11-02 05:22:21 -050093 auto gw = SystemConfigIntf::defaultGateway();
94 if (gw == gateway)
Ratan Gupta82e1ef92017-06-15 08:39:15 +053095 {
Nagaraju Gorugantie44acde2017-11-02 05:22:21 -050096 return gw;
Ratan Gupta82e1ef92017-06-15 08:39:15 +053097 }
Nagaraju Gorugantie44acde2017-11-02 05:22:21 -050098
99 if (!isValidIP(AF_INET, gateway))
100 {
101 log<level::ERR>("Not a valid Gateway",
Gunnar Mills57d9c502018-09-14 14:42:34 -0500102 entry("GATEWAY=%s", gateway.c_str()));
103 elog<InvalidArgument>(
104 InvalidArgumentMetadata::ARGUMENT_NAME("GATEWAY"),
105 InvalidArgumentMetadata::ARGUMENT_VALUE(gateway.c_str()));
Nagaraju Gorugantie44acde2017-11-02 05:22:21 -0500106 }
107 gw = SystemConfigIntf::defaultGateway(gateway);
Ratan Gupta82e1ef92017-06-15 08:39:15 +0530108 manager.writeToConfigurationFile();
109 return gw;
110}
111
Gunnar Mills57d9c502018-09-14 14:42:34 -0500112} // namespace network
113} // namespace phosphor