blob: a957ded8a25f9572a8f81e6267780e7acc698feb [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;
26
27using SystemConfigIntf =
28 sdbusplus::xyz::openbmc_project::Network::server::SystemConfiguration;
29
30SystemConfiguration::SystemConfiguration(sdbusplus::bus::bus& bus,
31 const std::string& objPath,
32 Manager& parent) :
33 Iface(bus, objPath.c_str(), true),
34 bus(bus),
35 manager(parent)
36{
37 auto name = getHostNameFromSystem();
38 route::Table routingTable;
39
40 SystemConfigIntf::hostName(name);
41 SystemConfigIntf::defaultGateway(routingTable.getDefaultGateway());
42
43 this->emit_object_added();
44}
45
46std::string SystemConfiguration::hostName(std::string name)
47{
48 if (SystemConfigIntf::hostName() == name)
49 {
50 return name;
51 }
52 auto method = bus.new_method_call(
53 HOSTNAMED_SERVICE,
54 HOSTNAMED_SERVICE_PATH,
55 HOSTNAMED_INTERFACE,
56 METHOD_SET);
57
58 method.append(name, true);
59
60 if (!bus.call(method))
61 {
62 log<level::ERR>("Failed to set the hostname");
63 report<InternalFailure>();
64 return SystemConfigIntf::hostName();
65 }
66
67 return SystemConfigIntf::hostName(name);
68}
69
70std::string SystemConfiguration::getHostNameFromSystem() const
71{
72 sdbusplus::message::variant<std::string> name;
73 auto method = bus.new_method_call(
74 HOSTNAMED_SERVICE,
75 HOSTNAMED_SERVICE_PATH,
76 PROPERTY_INTERFACE,
77 METHOD_GET);
78
79 method.append(HOSTNAMED_INTERFACE, "Hostname");
80
81 auto reply = bus.call(method);
82
83 if (reply)
84 {
85 reply.read(name);
86 }
87 else
88 {
89 log<level::ERR>("Failed to get hostname");
90 report<InternalFailure>();
91 return "";
92 }
93 return name.get<std::string>();
94}
95
96
97std::string SystemConfiguration::defaultGateway(std::string gateway)
98{
Nagaraju Gorugantie44acde2017-11-02 05:22:21 -050099 auto gw = SystemConfigIntf::defaultGateway();
100 if (gw == gateway)
Ratan Gupta82e1ef92017-06-15 08:39:15 +0530101 {
Nagaraju Gorugantie44acde2017-11-02 05:22:21 -0500102 return gw;
Ratan Gupta82e1ef92017-06-15 08:39:15 +0530103 }
Nagaraju Gorugantie44acde2017-11-02 05:22:21 -0500104
105 if (!isValidIP(AF_INET, gateway))
106 {
107 log<level::ERR>("Not a valid Gateway",
108 entry("GATEWAY=%s", gateway.c_str()));
109 return gw;
110 }
111 gw = SystemConfigIntf::defaultGateway(gateway);
Ratan Gupta82e1ef92017-06-15 08:39:15 +0530112 manager.writeToConfigurationFile();
113 return gw;
114}
115
116}// namespace network
117}// namespace phosphor