diff options
Diffstat (limited to 'redfish-core/lib/network_protocol.hpp')
-rw-r--r-- | redfish-core/lib/network_protocol.hpp | 311 |
1 files changed, 174 insertions, 137 deletions
diff --git a/redfish-core/lib/network_protocol.hpp b/redfish-core/lib/network_protocol.hpp index ae29df05f6..bb021f5297 100644 --- a/redfish-core/lib/network_protocol.hpp +++ b/redfish-core/lib/network_protocol.hpp @@ -18,24 +18,27 @@ #include "error_messages.hpp" #include "node.hpp" -namespace redfish { - -enum NetworkProtocolUnitStructFields { - NET_PROTO_UNIT_NAME, - NET_PROTO_UNIT_DESC, - NET_PROTO_UNIT_LOAD_STATE, - NET_PROTO_UNIT_ACTIVE_STATE, - NET_PROTO_UNIT_SUB_STATE, - NET_PROTO_UNIT_DEVICE, - NET_PROTO_UNIT_OBJ_PATH, - NET_PROTO_UNIT_ALWAYS_0, - NET_PROTO_UNIT_ALWAYS_EMPTY, - NET_PROTO_UNIT_ALWAYS_ROOT_PATH +namespace redfish +{ + +enum NetworkProtocolUnitStructFields +{ + NET_PROTO_UNIT_NAME, + NET_PROTO_UNIT_DESC, + NET_PROTO_UNIT_LOAD_STATE, + NET_PROTO_UNIT_ACTIVE_STATE, + NET_PROTO_UNIT_SUB_STATE, + NET_PROTO_UNIT_DEVICE, + NET_PROTO_UNIT_OBJ_PATH, + NET_PROTO_UNIT_ALWAYS_0, + NET_PROTO_UNIT_ALWAYS_EMPTY, + NET_PROTO_UNIT_ALWAYS_ROOT_PATH }; -enum NetworkProtocolListenResponseElements { - NET_PROTO_LISTEN_TYPE, - NET_PROTO_LISTEN_STREAM +enum NetworkProtocolListenResponseElements +{ + NET_PROTO_LISTEN_TYPE, + NET_PROTO_LISTEN_STREAM }; /** @@ -46,9 +49,10 @@ using UnitStruct = std::string, sdbusplus::message::object_path, uint32_t, std::string, sdbusplus::message::object_path>; -struct ServiceConfiguration { - const char* serviceName; - const char* socketPath; +struct ServiceConfiguration +{ + const char* serviceName; + const char* socketPath; }; const static boost::container::flat_map<const char*, ServiceConfiguration> @@ -63,128 +67,161 @@ const static boost::container::flat_map<const char*, ServiceConfiguration> {"phosphor-ipmi-net.service", "/org/freedesktop/systemd1/unit/phosphor_2dipmi_2dnet_2esocket"}}}; -class NetworkProtocol : public Node { - public: - NetworkProtocol(CrowApp& app) - : Node(app, "/redfish/v1/Managers/openbmc/NetworkProtocol") { - Node::json["@odata.type"] = - "#ManagerNetworkProtocol.v1_1_0.ManagerNetworkProtocol"; - Node::json["@odata.id"] = "/redfish/v1/Managers/openbmc/NetworkProtocol"; - Node::json["@odata.context"] = - "/redfish/v1/$metadata#ManagerNetworkProtocol.ManagerNetworkProtocol"; - Node::json["Id"] = "NetworkProtocol"; - Node::json["Name"] = "Manager Network Protocol"; - Node::json["Description"] = "Manager Network Service"; - Node::json["Status"]["Health"] = "OK"; - Node::json["Status"]["HealthRollup"] = "OK"; - Node::json["Status"]["State"] = "Enabled"; - - for (auto& protocol : protocolToDBus) { - Node::json[protocol.first]["ProtocolEnabled"] = false; +class NetworkProtocol : public Node +{ + public: + NetworkProtocol(CrowApp& app) : + Node(app, "/redfish/v1/Managers/openbmc/NetworkProtocol") + { + Node::json["@odata.type"] = + "#ManagerNetworkProtocol.v1_1_0.ManagerNetworkProtocol"; + Node::json["@odata.id"] = + "/redfish/v1/Managers/openbmc/NetworkProtocol"; + Node::json["@odata.context"] = + "/redfish/v1/" + "$metadata#ManagerNetworkProtocol.ManagerNetworkProtocol"; + Node::json["Id"] = "NetworkProtocol"; + Node::json["Name"] = "Manager Network Protocol"; + Node::json["Description"] = "Manager Network Service"; + Node::json["Status"]["Health"] = "OK"; + Node::json["Status"]["HealthRollup"] = "OK"; + Node::json["Status"]["State"] = "Enabled"; + + for (auto& protocol : protocolToDBus) + { + Node::json[protocol.first]["ProtocolEnabled"] = false; + } + + entityPrivileges = { + {boost::beast::http::verb::get, {{"Login"}}}, + {boost::beast::http::verb::head, {{"Login"}}}, + {boost::beast::http::verb::patch, {{"ConfigureManager"}}}, + {boost::beast::http::verb::put, {{"ConfigureManager"}}}, + {boost::beast::http::verb::delete_, {{"ConfigureManager"}}}, + {boost::beast::http::verb::post, {{"ConfigureManager"}}}}; } - entityPrivileges = { - {boost::beast::http::verb::get, {{"Login"}}}, - {boost::beast::http::verb::head, {{"Login"}}}, - {boost::beast::http::verb::patch, {{"ConfigureManager"}}}, - {boost::beast::http::verb::put, {{"ConfigureManager"}}}, - {boost::beast::http::verb::delete_, {{"ConfigureManager"}}}, - {boost::beast::http::verb::post, {{"ConfigureManager"}}}}; - } - - private: - void doGet(crow::Response& res, const crow::Request& req, - const std::vector<std::string>& params) override { - std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); - - getData(asyncResp); - } - - std::string getHostName() const { - std::string hostName; - - std::array<char, HOST_NAME_MAX> hostNameCStr; - if (gethostname(hostNameCStr.data(), hostNameCStr.size()) == 0) { - hostName = hostNameCStr.data(); + private: + void doGet(crow::Response& res, const crow::Request& req, + const std::vector<std::string>& params) override + { + std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); + + getData(asyncResp); } - return hostName; - } - - void getData(const std::shared_ptr<AsyncResp>& asyncResp) { - Node::json["HostName"] = getHostName(); - asyncResp->res.jsonValue = Node::json; - - crow::connections::systemBus->async_method_call( - [asyncResp](const boost::system::error_code ec, - const std::vector<UnitStruct>& resp) { - if (ec) { - asyncResp->res.jsonValue = nlohmann::json::object(); - messages::addMessageToErrorJson(asyncResp->res.jsonValue, - messages::internalError()); - asyncResp->res.result( - boost::beast::http::status::internal_server_error); - return; - } - - for (auto& unit : resp) { - for (auto& kv : protocolToDBus) { - if (kv.second.serviceName == - std::get<NET_PROTO_UNIT_NAME>(unit)) { - continue; - } - const char* service = kv.first; - const char* socketPath = kv.second.socketPath; - - asyncResp->res.jsonValue[service]["ProtocolEnabled"] = - std::get<NET_PROTO_UNIT_SUB_STATE>(unit) == "running"; - - crow::connections::systemBus->async_method_call( - [ asyncResp, service{std::string(service)}, socketPath ]( - const boost::system::error_code ec, - const sdbusplus::message::variant<std::vector< - std::tuple<std::string, std::string>>>& resp) { - if (ec) { - messages::addMessageToJson(asyncResp->res.jsonValue, - messages::internalError(), - "/" + service); - return; - } - const std::vector<std::tuple<std::string, std::string>>* - responsePtr = mapbox::getPtr<const std::vector< - std::tuple<std::string, std::string>>>(resp); - if (responsePtr == nullptr || responsePtr->size() < 1) { - return; - } - const std::string& listenStream = - std::get<NET_PROTO_LISTEN_STREAM>((*responsePtr)[0]); - std::size_t lastColonPos = listenStream.rfind(":"); - if (lastColonPos == std::string::npos) { - // Not a port - return; - } - std::string portStr = listenStream.substr(lastColonPos + 1); - char* endPtr = nullptr; - // Use strtol instead of stroi to avoid exceptions - long port = std::strtol(portStr.c_str(), &endPtr, 10); - - if (*endPtr != '\0' || portStr.empty()) { - // Invalid value - asyncResp->res.jsonValue[service]["Port"] = nullptr; - } else { - // Everything OK - asyncResp->res.jsonValue[service]["Port"] = port; + std::string getHostName() const + { + std::string hostName; + + std::array<char, HOST_NAME_MAX> hostNameCStr; + if (gethostname(hostNameCStr.data(), hostNameCStr.size()) == 0) + { + hostName = hostNameCStr.data(); + } + return hostName; + } + + void getData(const std::shared_ptr<AsyncResp>& asyncResp) + { + Node::json["HostName"] = getHostName(); + asyncResp->res.jsonValue = Node::json; + + crow::connections::systemBus->async_method_call( + [asyncResp](const boost::system::error_code ec, + const std::vector<UnitStruct>& resp) { + if (ec) + { + asyncResp->res.jsonValue = nlohmann::json::object(); + messages::addMessageToErrorJson(asyncResp->res.jsonValue, + messages::internalError()); + asyncResp->res.result( + boost::beast::http::status::internal_server_error); + return; + } + + for (auto& unit : resp) + { + for (auto& kv : protocolToDBus) + { + if (kv.second.serviceName == + std::get<NET_PROTO_UNIT_NAME>(unit)) + { + continue; + } + const char* service = kv.first; + const char* socketPath = kv.second.socketPath; + + asyncResp->res.jsonValue[service]["ProtocolEnabled"] = + std::get<NET_PROTO_UNIT_SUB_STATE>(unit) == + "running"; + + crow::connections::systemBus->async_method_call( + [asyncResp, service{std::string(service)}, + socketPath]( + const boost::system::error_code ec, + const sdbusplus::message::variant<std::vector< + std::tuple<std::string, std::string>>>& + resp) { + if (ec) + { + messages::addMessageToJson( + asyncResp->res.jsonValue, + messages::internalError(), + "/" + service); + return; + } + const std::vector<std::tuple< + std::string, std::string>>* responsePtr = + mapbox::getPtr<const std::vector< + std::tuple<std::string, std::string>>>( + resp); + if (responsePtr == nullptr || + responsePtr->size() < 1) + { + return; + } + + const std::string& listenStream = + std::get<NET_PROTO_LISTEN_STREAM>( + (*responsePtr)[0]); + std::size_t lastColonPos = + listenStream.rfind(":"); + if (lastColonPos == std::string::npos) + { + // Not a port + return; + } + std::string portStr = + listenStream.substr(lastColonPos + 1); + char* endPtr = nullptr; + // Use strtol instead of stroi to avoid + // exceptions + long port = + std::strtol(portStr.c_str(), &endPtr, 10); + + if (*endPtr != '\0' || portStr.empty()) + { + // Invalid value + asyncResp->res.jsonValue[service]["Port"] = + nullptr; + } + else + { + // Everything OK + asyncResp->res.jsonValue[service]["Port"] = + port; + } + }, + "org.freedesktop.systemd1", socketPath, + "org.freedesktop.DBus.Properties", "Get", + "org.freedesktop.systemd1.Socket", "Listen"); } - }, - "org.freedesktop.systemd1", socketPath, - "org.freedesktop.DBus.Properties", "Get", - "org.freedesktop.systemd1.Socket", "Listen"); - } - } - }, - "org.freedesktop.systemd1", "/org/freedesktop/systemd1", - "org.freedesktop.systemd1.Manager", "ListUnits"); - } + } + }, + "org.freedesktop.systemd1", "/org/freedesktop/systemd1", + "org.freedesktop.systemd1.Manager", "ListUnits"); + } }; -} // namespace redfish +} // namespace redfish |