Ed Tanous | 40e9b92 | 2024-09-10 13:50:16 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright OpenBMC Authors |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 3 | #pragma once |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 4 | #include "dbus_singleton.hpp" |
| 5 | #include "logging.hpp" |
| 6 | |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame^] | 7 | #include <sys/socket.h> |
| 8 | |
| 9 | #include <boost/asio/io_context.hpp> |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 10 | #include <boost/asio/ip/address.hpp> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame^] | 11 | #include <boost/asio/ip/address_v4.hpp> |
| 12 | #include <boost/asio/ip/address_v6.hpp> |
Carson Labrado | 7e8890c | 2022-11-23 23:58:01 +0000 | [diff] [blame] | 13 | #include <boost/asio/ip/tcp.hpp> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame^] | 14 | #include <boost/system/errc.hpp> |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 15 | |
| 16 | #include <charconv> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame^] | 17 | #include <cstdint> |
| 18 | #include <string> |
| 19 | #include <string_view> |
| 20 | #include <system_error> |
| 21 | #include <tuple> |
| 22 | #include <vector> |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 23 | |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 24 | namespace async_resolve |
| 25 | { |
| 26 | |
Ed Tanous | e1452be | 2021-10-04 17:03:52 -0700 | [diff] [blame] | 27 | inline bool endpointFromResolveTuple(const std::vector<uint8_t>& ipAddress, |
| 28 | boost::asio::ip::tcp::endpoint& endpoint) |
| 29 | { |
| 30 | if (ipAddress.size() == 4) // ipv4 address |
| 31 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 32 | BMCWEB_LOG_DEBUG("ipv4 address"); |
Ed Tanous | e1452be | 2021-10-04 17:03:52 -0700 | [diff] [blame] | 33 | boost::asio::ip::address_v4 ipv4Addr( |
| 34 | {ipAddress[0], ipAddress[1], ipAddress[2], ipAddress[3]}); |
| 35 | endpoint.address(ipv4Addr); |
| 36 | } |
| 37 | else if (ipAddress.size() == 16) // ipv6 address |
| 38 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 39 | BMCWEB_LOG_DEBUG("ipv6 address"); |
Ed Tanous | e1452be | 2021-10-04 17:03:52 -0700 | [diff] [blame] | 40 | boost::asio::ip::address_v6 ipv6Addr( |
| 41 | {ipAddress[0], ipAddress[1], ipAddress[2], ipAddress[3], |
| 42 | ipAddress[4], ipAddress[5], ipAddress[6], ipAddress[7], |
| 43 | ipAddress[8], ipAddress[9], ipAddress[10], ipAddress[11], |
| 44 | ipAddress[12], ipAddress[13], ipAddress[14], ipAddress[15]}); |
| 45 | endpoint.address(ipv6Addr); |
| 46 | } |
| 47 | else |
| 48 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 49 | BMCWEB_LOG_ERROR("Resolve failed to fetch the IP address"); |
Ed Tanous | e1452be | 2021-10-04 17:03:52 -0700 | [diff] [blame] | 50 | return false; |
| 51 | } |
| 52 | return true; |
| 53 | } |
| 54 | |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 55 | class Resolver |
| 56 | { |
| 57 | public: |
Ed Tanous | f8ca6d7 | 2022-06-28 12:12:03 -0700 | [diff] [blame] | 58 | // unused io param used to keep interface identical to |
| 59 | // boost::asio::tcp:::resolver |
| 60 | explicit Resolver(boost::asio::io_context& /*io*/) {} |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 61 | |
| 62 | ~Resolver() = default; |
| 63 | |
Ed Tanous | ecd6a3a | 2022-01-07 09:18:40 -0800 | [diff] [blame] | 64 | Resolver(const Resolver&) = delete; |
| 65 | Resolver(Resolver&&) = delete; |
| 66 | Resolver& operator=(const Resolver&) = delete; |
| 67 | Resolver& operator=(Resolver&&) = delete; |
| 68 | |
Ed Tanous | f8ca6d7 | 2022-06-28 12:12:03 -0700 | [diff] [blame] | 69 | using results_type = std::vector<boost::asio::ip::tcp::endpoint>; |
| 70 | |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 71 | template <typename ResolveHandler> |
Ed Tanous | f8ca6d7 | 2022-06-28 12:12:03 -0700 | [diff] [blame] | 72 | // This function is kept using snake case so that it is interoperable with |
| 73 | // boost::asio::ip::tcp::resolver |
| 74 | // NOLINTNEXTLINE(readability-identifier-naming) |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 75 | void async_resolve(std::string_view host, std::string_view port, |
Ed Tanous | f8ca6d7 | 2022-06-28 12:12:03 -0700 | [diff] [blame] | 76 | ResolveHandler&& handler) |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 77 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 78 | BMCWEB_LOG_DEBUG("Trying to resolve: {}:{}", host, port); |
Ed Tanous | f8ca6d7 | 2022-06-28 12:12:03 -0700 | [diff] [blame] | 79 | |
| 80 | uint16_t portNum = 0; |
| 81 | |
| 82 | auto it = std::from_chars(&*port.begin(), &*port.end(), portNum); |
| 83 | if (it.ec != std::errc()) |
| 84 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 85 | BMCWEB_LOG_ERROR("Failed to get the Port"); |
Ed Tanous | f8ca6d7 | 2022-06-28 12:12:03 -0700 | [diff] [blame] | 86 | handler(std::make_error_code(std::errc::invalid_argument), |
| 87 | results_type{}); |
| 88 | |
| 89 | return; |
| 90 | } |
| 91 | |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 92 | uint64_t flag = 0; |
| 93 | crow::connections::systemBus->async_method_call( |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 94 | [host{std::string(host)}, portNum, |
Ed Tanous | 8cb2c02 | 2024-03-27 16:31:46 -0700 | [diff] [blame] | 95 | handler = std::forward<ResolveHandler>(handler)]( |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 96 | const boost::system::error_code& ec, |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 97 | const std::vector< |
| 98 | std::tuple<int32_t, int32_t, std::vector<uint8_t>>>& resp, |
| 99 | const std::string& hostName, const uint64_t flagNum) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 100 | results_type endpointList; |
| 101 | if (ec) |
Ed Tanous | e1452be | 2021-10-04 17:03:52 -0700 | [diff] [blame] | 102 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 103 | BMCWEB_LOG_ERROR("Resolve failed: {}", ec.message()); |
| 104 | handler(ec, endpointList); |
| 105 | return; |
Ed Tanous | e1452be | 2021-10-04 17:03:52 -0700 | [diff] [blame] | 106 | } |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 107 | BMCWEB_LOG_DEBUG("ResolveHostname returned: {}:{}", hostName, |
| 108 | flagNum); |
| 109 | // Extract the IP address from the response |
| 110 | for (const std::tuple<int32_t, int32_t, std::vector<uint8_t>>& |
| 111 | resolveList : resp) |
| 112 | { |
| 113 | boost::asio::ip::tcp::endpoint endpoint; |
| 114 | endpoint.port(portNum); |
| 115 | if (!endpointFromResolveTuple(std::get<2>(resolveList), |
| 116 | endpoint)) |
| 117 | { |
| 118 | boost::system::error_code ecErr = make_error_code( |
| 119 | boost::system::errc::address_not_available); |
| 120 | handler(ecErr, endpointList); |
| 121 | } |
| 122 | BMCWEB_LOG_DEBUG("resolved endpoint is : {}", |
| 123 | endpoint.address().to_string()); |
| 124 | endpointList.push_back(endpoint); |
| 125 | } |
| 126 | // All the resolved data is filled in the endpointList |
| 127 | handler(ec, endpointList); |
| 128 | }, |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 129 | "org.freedesktop.resolve1", "/org/freedesktop/resolve1", |
| 130 | "org.freedesktop.resolve1.Manager", "ResolveHostname", 0, host, |
| 131 | AF_UNSPEC, flag); |
| 132 | } |
| 133 | }; |
| 134 | |
| 135 | } // namespace async_resolve |