1 /* 2 // Copyright (c) 2018 Intel Corporation 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 */ 16 #pragma once 17 18 #include "node.hpp" 19 20 namespace redfish { 21 22 static OperationMap managerNetworkProtocolOpMap = { 23 {crow::HTTPMethod::GET, {{"Login"}}}, 24 {crow::HTTPMethod::HEAD, {{"Login"}}}, 25 {crow::HTTPMethod::PATCH, {{"ConfigureManager"}}}, 26 {crow::HTTPMethod::PUT, {{"ConfigureManager"}}}, 27 {crow::HTTPMethod::DELETE, {{"ConfigureManager"}}}, 28 {crow::HTTPMethod::POST, {{"ConfigureManager"}}}}; 29 30 class NetworkProtocol : public Node { 31 public: 32 NetworkProtocol(CrowApp& app) 33 : Node(app, EntityPrivileges(std::move(managerNetworkProtocolOpMap)), 34 "/redfish/v1/Managers/openbmc/NetworkProtocol") { 35 Node::json["@odata.type"] = 36 "#ManagerNetworkProtocol.v1_1_0.ManagerNetworkProtocol"; 37 Node::json["@odata.id"] = "/redfish/v1/Managers/openbmc/NetworkProtocol"; 38 Node::json["@odata.context"] = 39 "/redfish/v1/$metadata#ManagerNetworkProtocol.ManagerNetworkProtocol"; 40 Node::json["Id"] = "NetworkProtocol"; 41 Node::json["Name"] = "Manager Network Protocol"; 42 Node::json["Description"] = "Manager Network Service"; 43 Node::json["Status"]["Health"] = "OK"; 44 Node::json["Status"]["HealthRollup"] = "OK"; 45 Node::json["Status"]["State"] = "Enabled"; 46 } 47 48 private: 49 void doGet(crow::response& res, const crow::request& req, 50 const std::vector<std::string>& params) override { 51 refreshProtocolsState(); 52 Node::json["HostName"] = getHostName(); 53 res.json_value = Node::json; 54 res.end(); 55 } 56 57 std::string getHostName() const { 58 std::string hostName; 59 60 std::array<char, HOST_NAME_MAX> hostNameCStr; 61 if (gethostname(hostNameCStr.data(), hostNameCStr.size()) == 0) { 62 hostName = hostNameCStr.data(); 63 } 64 return hostName; 65 } 66 67 void refreshProtocolsState() { 68 refreshListeningPorts(); 69 for (auto& kv : portToProtocolMap) { 70 Node::json[kv.second]["Port"] = kv.first; 71 if (listeningPorts.find(kv.first) != listeningPorts.end()) { 72 Node::json[kv.second]["ProtocolEnabled"] = true; 73 } else { 74 Node::json[kv.second]["ProtocolEnabled"] = false; 75 } 76 } 77 } 78 79 void refreshListeningPorts() { 80 listeningPorts.clear(); 81 std::array<char, 128> netstatLine; 82 FILE* p = popen("netstat -tuln | awk '{ print $4 }'", "r"); 83 if (p != nullptr) { 84 while (fgets(netstatLine.data(), netstatLine.size(), p) != nullptr) { 85 auto s = std::string(netstatLine.data()); 86 87 // get port num from strings such as: ".*:.*:.*:port" 88 s.erase(0, s.find_last_of(":") + strlen(":")); 89 90 auto port = atoi(s.c_str()); 91 if (port != 0 && 92 portToProtocolMap.find(port) != portToProtocolMap.end()) { 93 listeningPorts.insert(port); 94 } 95 } 96 } 97 } 98 99 std::map<int, std::string> portToProtocolMap{ 100 {22, "SSH"}, {80, "HTTP"}, {443, "HTTPS"}, {623, "IPMI"}, {1900, "SSDP"}}; 101 102 std::set<int> listeningPorts; 103 }; 104 105 } // namespace redfish 106