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 class NetworkProtocol : public Node {
23  public:
24   NetworkProtocol(CrowApp& app)
25       : Node(app,
26              "/redfish/v1/Managers/openbmc/NetworkProtocol") {
27     Node::json["@odata.type"] =
28         "#ManagerNetworkProtocol.v1_1_0.ManagerNetworkProtocol";
29     Node::json["@odata.id"] = "/redfish/v1/Managers/openbmc/NetworkProtocol";
30     Node::json["@odata.context"] =
31         "/redfish/v1/$metadata#ManagerNetworkProtocol.ManagerNetworkProtocol";
32     Node::json["Id"] = "NetworkProtocol";
33     Node::json["Name"] = "Manager Network Protocol";
34     Node::json["Description"] = "Manager Network Service";
35     Node::json["Status"]["Health"] = "OK";
36     Node::json["Status"]["HealthRollup"] = "OK";
37     Node::json["Status"]["State"] = "Enabled";
38 
39     entityPrivileges = {{crow::HTTPMethod::GET, {{"Login"}}},
40                         {crow::HTTPMethod::HEAD, {{"Login"}}},
41                         {crow::HTTPMethod::PATCH, {{"ConfigureManager"}}},
42                         {crow::HTTPMethod::PUT, {{"ConfigureManager"}}},
43                         {crow::HTTPMethod::DELETE, {{"ConfigureManager"}}},
44                         {crow::HTTPMethod::POST, {{"ConfigureManager"}}}};
45   }
46 
47  private:
48   void doGet(crow::response& res, const crow::request& req,
49              const std::vector<std::string>& params) override {
50     refreshProtocolsState();
51     Node::json["HostName"] = getHostName();
52     res.json_value = Node::json;
53     res.end();
54   }
55 
56   std::string getHostName() const {
57     std::string hostName;
58 
59     std::array<char, HOST_NAME_MAX> hostNameCStr;
60     if (gethostname(hostNameCStr.data(), hostNameCStr.size()) == 0) {
61       hostName = hostNameCStr.data();
62     }
63     return hostName;
64   }
65 
66   void refreshProtocolsState() {
67     refreshListeningPorts();
68     for (auto& kv : portToProtocolMap) {
69       Node::json[kv.second]["Port"] = kv.first;
70       if (listeningPorts.find(kv.first) != listeningPorts.end()) {
71         Node::json[kv.second]["ProtocolEnabled"] = true;
72       } else {
73         Node::json[kv.second]["ProtocolEnabled"] = false;
74       }
75     }
76   }
77 
78   void refreshListeningPorts() {
79     listeningPorts.clear();
80     std::array<char, 128> netstatLine;
81     FILE* p = popen("netstat -tuln | awk '{ print $4 }'", "r");
82     if (p != nullptr) {
83       while (fgets(netstatLine.data(), netstatLine.size(), p) != nullptr) {
84         auto s = std::string(netstatLine.data());
85 
86         // get port num from strings such as: ".*:.*:.*:port"
87         s.erase(0, s.find_last_of(":") + strlen(":"));
88 
89         auto port = atoi(s.c_str());
90         if (port != 0 &&
91             portToProtocolMap.find(port) != portToProtocolMap.end()) {
92           listeningPorts.insert(port);
93         }
94       }
95     }
96   }
97 
98   std::map<int, std::string> portToProtocolMap{
99       {22, "SSH"}, {80, "HTTP"}, {443, "HTTPS"}, {623, "IPMI"}, {1900, "SSDP"}};
100 
101   std::set<int> listeningPorts;
102 };
103 
104 }  // namespace redfish
105