Ratan Gupta | 6347619 | 2018-04-19 16:55:32 +0530 | [diff] [blame] | 1 | #include "snmp_util.hpp" |
Patrick Williams | 1334b7b | 2021-02-22 17:15:12 -0600 | [diff] [blame] | 2 | |
Ratan Gupta | 6347619 | 2018-04-19 16:55:32 +0530 | [diff] [blame] | 3 | #include "xyz/openbmc_project/Common/error.hpp" |
| 4 | |
Ratan Gupta | 213517b | 2018-04-28 13:41:09 +0530 | [diff] [blame] | 5 | #include <arpa/inet.h> |
Patrick Williams | 1334b7b | 2021-02-22 17:15:12 -0600 | [diff] [blame] | 6 | #include <netdb.h> |
Ratan Gupta | 213517b | 2018-04-28 13:41:09 +0530 | [diff] [blame] | 7 | |
Ratan Gupta | 6347619 | 2018-04-19 16:55:32 +0530 | [diff] [blame] | 8 | #include <phosphor-logging/elog-errors.hpp> |
George Liu | 4caedfb | 2022-05-10 16:26:53 +0800 | [diff] [blame] | 9 | #include <phosphor-logging/lg2.hpp> |
Ratan Gupta | 6347619 | 2018-04-19 16:55:32 +0530 | [diff] [blame] | 10 | |
Ratan Gupta | 6347619 | 2018-04-19 16:55:32 +0530 | [diff] [blame] | 11 | #include <string> |
| 12 | |
| 13 | namespace phosphor |
| 14 | { |
| 15 | |
| 16 | using namespace phosphor::logging; |
| 17 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 18 | |
Patrick Williams | 87d3edd | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 19 | ObjectValueTree getManagedObjects(sdbusplus::bus_t& bus, |
Ratan Gupta | 6347619 | 2018-04-19 16:55:32 +0530 | [diff] [blame] | 20 | const std::string& service, |
| 21 | const std::string& objPath) |
| 22 | { |
| 23 | ObjectValueTree interfaces; |
| 24 | |
| 25 | auto method = bus.new_method_call(service.c_str(), objPath.c_str(), |
| 26 | "org.freedesktop.DBus.ObjectManager", |
| 27 | "GetManagedObjects"); |
| 28 | |
George Liu | e65b0a7 | 2022-05-10 16:47:27 +0800 | [diff] [blame] | 29 | try |
| 30 | { |
| 31 | auto reply = bus.call(method); |
| 32 | reply.read(interfaces); |
| 33 | } |
Patrick Williams | 87d3edd | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 34 | catch (const sdbusplus::exception_t& e) |
Ratan Gupta | 6347619 | 2018-04-19 16:55:32 +0530 | [diff] [blame] | 35 | { |
George Liu | 4caedfb | 2022-05-10 16:26:53 +0800 | [diff] [blame] | 36 | lg2::error("Failed to get managed objects: {PATH}", "PATH", objPath); |
Ratan Gupta | 6347619 | 2018-04-19 16:55:32 +0530 | [diff] [blame] | 37 | elog<InternalFailure>(); |
| 38 | } |
| 39 | |
Ratan Gupta | 6347619 | 2018-04-19 16:55:32 +0530 | [diff] [blame] | 40 | return interfaces; |
| 41 | } |
| 42 | |
| 43 | namespace network |
| 44 | { |
| 45 | |
| 46 | std::string resolveAddress(const std::string& address) |
| 47 | { |
Ratan Gupta | 34d129a | 2021-12-04 21:04:51 +0530 | [diff] [blame] | 48 | addrinfo hints{}; |
Ratan Gupta | 6347619 | 2018-04-19 16:55:32 +0530 | [diff] [blame] | 49 | addrinfo* addr = nullptr; |
| 50 | |
| 51 | hints.ai_family = AF_UNSPEC; |
| 52 | hints.ai_socktype = SOCK_STREAM; |
| 53 | hints.ai_flags |= AI_CANONNAME; |
| 54 | |
| 55 | auto result = getaddrinfo(address.c_str(), NULL, &hints, &addr); |
| 56 | if (result) |
| 57 | { |
George Liu | 4caedfb | 2022-05-10 16:26:53 +0800 | [diff] [blame] | 58 | lg2::error("getaddrinfo failed {ADDRESS}: {RC}", "ADDRESS", address, |
| 59 | "RC", result); |
Ratan Gupta | 6347619 | 2018-04-19 16:55:32 +0530 | [diff] [blame] | 60 | elog<InternalFailure>(); |
| 61 | } |
| 62 | |
| 63 | AddrPtr addrPtr{addr}; |
| 64 | addr = nullptr; |
| 65 | |
| 66 | char ipaddress[INET6_ADDRSTRLEN]{0}; |
| 67 | result = getnameinfo(addrPtr->ai_addr, addrPtr->ai_addrlen, ipaddress, |
| 68 | sizeof(ipaddress), NULL, 0, NI_NUMERICHOST); |
| 69 | if (result) |
| 70 | { |
George Liu | 4caedfb | 2022-05-10 16:26:53 +0800 | [diff] [blame] | 71 | lg2::error("getnameinfo failed {ADDRESS}: {RC}", "ADDRESS", address, |
| 72 | "RC", result); |
Ratan Gupta | 6347619 | 2018-04-19 16:55:32 +0530 | [diff] [blame] | 73 | elog<InternalFailure>(); |
| 74 | } |
| 75 | |
Chicago Duan | 7350c77 | 2021-12-03 16:54:17 +0800 | [diff] [blame] | 76 | unsigned char buf[sizeof(struct in6_addr)]; |
| 77 | int isValid = inet_pton(AF_INET, ipaddress, buf); |
| 78 | if (isValid < 0) |
| 79 | { |
George Liu | 4caedfb | 2022-05-10 16:26:53 +0800 | [diff] [blame] | 80 | lg2::error("Invalid address {ADDRESS}: {RC}", "ADDRESS", address, "RC", |
| 81 | isValid); |
Chicago Duan | 7350c77 | 2021-12-03 16:54:17 +0800 | [diff] [blame] | 82 | elog<InternalFailure>(); |
| 83 | } |
| 84 | if (isValid == 0) |
| 85 | { |
| 86 | int isValid6 = inet_pton(AF_INET6, ipaddress, buf); |
| 87 | if (isValid6 < 1) |
| 88 | { |
George Liu | 4caedfb | 2022-05-10 16:26:53 +0800 | [diff] [blame] | 89 | lg2::error("Invalid address {ADDRESS}: {RC}", "ADDRESS", address, |
| 90 | "RC", isValid); |
Chicago Duan | 7350c77 | 2021-12-03 16:54:17 +0800 | [diff] [blame] | 91 | elog<InternalFailure>(); |
| 92 | } |
| 93 | } |
| 94 | |
Ratan Gupta | 6347619 | 2018-04-19 16:55:32 +0530 | [diff] [blame] | 95 | return ipaddress; |
| 96 | } |
| 97 | |
| 98 | namespace snmp |
| 99 | { |
| 100 | |
| 101 | static constexpr auto busName = "xyz.openbmc_project.Network.SNMP"; |
| 102 | static constexpr auto root = "/xyz/openbmc_project/network/snmp/manager"; |
| 103 | static constexpr auto clientIntf = "xyz.openbmc_project.Network.Client"; |
| 104 | |
| 105 | /** @brief Gets the sdbus object for this process. |
| 106 | * @return the bus object. |
| 107 | */ |
| 108 | static auto& getBus() |
| 109 | { |
| 110 | static auto bus = sdbusplus::bus::new_default(); |
| 111 | return bus; |
| 112 | } |
| 113 | |
| 114 | std::vector<std::string> getManagers() |
| 115 | { |
| 116 | std::vector<std::string> managers; |
| 117 | auto& bus = getBus(); |
| 118 | auto objTree = phosphor::getManagedObjects(bus, busName, root); |
| 119 | for (const auto& objIter : objTree) |
| 120 | { |
| 121 | try |
| 122 | { |
| 123 | auto& intfMap = objIter.second; |
| 124 | auto& snmpClientProps = intfMap.at(clientIntf); |
William A. Kennington III | bbee5d0 | 2018-11-06 15:52:22 -0800 | [diff] [blame] | 125 | auto& address = |
Patrick Williams | 7d4bd22 | 2020-05-13 11:05:10 -0500 | [diff] [blame] | 126 | std::get<std::string>(snmpClientProps.at("Address")); |
| 127 | auto& port = std::get<uint16_t>(snmpClientProps.at("Port")); |
Ratan Gupta | 6347619 | 2018-04-19 16:55:32 +0530 | [diff] [blame] | 128 | auto ipaddress = phosphor::network::resolveAddress(address); |
| 129 | auto mgr = std::move(ipaddress); |
| 130 | if (port > 0) |
| 131 | { |
| 132 | mgr += ":"; |
| 133 | mgr += std::to_string(port); |
| 134 | } |
| 135 | managers.push_back(mgr); |
| 136 | } |
| 137 | catch (const std::exception& e) |
| 138 | { |
George Liu | 4caedfb | 2022-05-10 16:26:53 +0800 | [diff] [blame] | 139 | lg2::error("Invalid address: {ERROR}", "ERROR", e); |
Ratan Gupta | 6347619 | 2018-04-19 16:55:32 +0530 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | return managers; |
| 143 | } |
| 144 | |
| 145 | } // namespace snmp |
| 146 | } // namespace network |
| 147 | } // namespace phosphor |