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