summaryrefslogtreecommitdiffstats
path: root/redfish-core/lib/network_protocol.hpp
blob: 438de41efcd0e3a84ed4225f8b53cd5f3b900ccb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
// Copyright (c) 2018 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/
#pragma once

#include "node.hpp"

namespace redfish {

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";

    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 {
    refreshProtocolsState();
    Node::json["HostName"] = getHostName();
    res.json_value = Node::json;
    res.end();
  }

  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 refreshProtocolsState() {
    refreshListeningPorts();
    for (auto& kv : portToProtocolMap) {
      Node::json[kv.second]["Port"] = kv.first;
      if (listeningPorts.find(kv.first) != listeningPorts.end()) {
        Node::json[kv.second]["ProtocolEnabled"] = true;
      } else {
        Node::json[kv.second]["ProtocolEnabled"] = false;
      }
    }
  }

  void refreshListeningPorts() {
    listeningPorts.clear();
    std::array<char, 128> netstatLine;
    FILE* p = popen("netstat -tuln | awk '{ print $4 }'", "r");
    if (p != nullptr) {
      while (fgets(netstatLine.data(), netstatLine.size(), p) != nullptr) {
        auto s = std::string(netstatLine.data());

        // get port num from strings such as: ".*:.*:.*:port"
        s.erase(0, s.find_last_of(":") + strlen(":"));

        auto port = atoi(s.c_str());
        if (port != 0 &&
            portToProtocolMap.find(port) != portToProtocolMap.end()) {
          listeningPorts.insert(port);
        }
      }
    }
  }

  boost::container::flat_map<int, std::string> portToProtocolMap{
      {22, "SSH"}, {80, "HTTP"}, {443, "HTTPS"}, {623, "IPMI"}, {1900, "SSDP"}};

  boost::container::flat_set<int> listeningPorts;
};

}  // namespace redfish
OpenPOWER on IntegriCloud