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