Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | #include <boost/asio/ip/address.hpp> |
| 3 | #include <boost/asio/ip/basic_endpoint.hpp> |
Carson Labrado | 7e8890c | 2022-11-23 23:58:01 +0000 | [diff] [blame] | 4 | #include <boost/asio/ip/tcp.hpp> |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 5 | #include <sdbusplus/message.hpp> |
| 6 | |
| 7 | #include <charconv> |
| 8 | #include <iostream> |
| 9 | #include <memory> |
| 10 | |
| 11 | namespace crow |
| 12 | { |
| 13 | |
| 14 | namespace async_resolve |
| 15 | { |
| 16 | |
| 17 | class Resolver |
| 18 | { |
| 19 | public: |
| 20 | Resolver() = default; |
| 21 | |
| 22 | ~Resolver() = default; |
| 23 | |
Ed Tanous | ecd6a3a | 2022-01-07 09:18:40 -0800 | [diff] [blame] | 24 | Resolver(const Resolver&) = delete; |
| 25 | Resolver(Resolver&&) = delete; |
| 26 | Resolver& operator=(const Resolver&) = delete; |
| 27 | Resolver& operator=(Resolver&&) = delete; |
| 28 | |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 29 | template <typename ResolveHandler> |
Ed Tanous | eb1c47d | 2022-02-09 11:47:27 -0800 | [diff] [blame] | 30 | void asyncResolve(const std::string& host, uint16_t port, |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 31 | ResolveHandler&& handler) |
| 32 | { |
| 33 | BMCWEB_LOG_DEBUG << "Trying to resolve: " << host << ":" << port; |
| 34 | uint64_t flag = 0; |
| 35 | crow::connections::systemBus->async_method_call( |
Ed Tanous | f94c4ec | 2022-01-06 12:44:41 -0800 | [diff] [blame] | 36 | [host, port, handler{std::forward<ResolveHandler>(handler)}]( |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 37 | const boost::system::error_code ec, |
| 38 | const std::vector< |
| 39 | std::tuple<int32_t, int32_t, std::vector<uint8_t>>>& resp, |
| 40 | const std::string& hostName, const uint64_t flagNum) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 41 | std::vector<boost::asio::ip::tcp::endpoint> endpointList; |
| 42 | if (ec) |
| 43 | { |
| 44 | BMCWEB_LOG_ERROR << "Resolve failed: " << ec.message(); |
| 45 | handler(ec, endpointList); |
| 46 | return; |
| 47 | } |
| 48 | BMCWEB_LOG_DEBUG << "ResolveHostname returned: " << hostName << ":" |
| 49 | << flagNum; |
| 50 | // Extract the IP address from the response |
| 51 | for (auto resolveList : resp) |
| 52 | { |
| 53 | std::vector<uint8_t> ipAddress = std::get<2>(resolveList); |
| 54 | boost::asio::ip::tcp::endpoint endpoint; |
| 55 | if (ipAddress.size() == 4) // ipv4 address |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 56 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 57 | BMCWEB_LOG_DEBUG << "ipv4 address"; |
| 58 | boost::asio::ip::address_v4 ipv4Addr( |
| 59 | {ipAddress[0], ipAddress[1], ipAddress[2], |
| 60 | ipAddress[3]}); |
| 61 | endpoint.address(ipv4Addr); |
| 62 | } |
| 63 | else if (ipAddress.size() == 16) // ipv6 address |
| 64 | { |
| 65 | BMCWEB_LOG_DEBUG << "ipv6 address"; |
| 66 | boost::asio::ip::address_v6 ipv6Addr( |
| 67 | {ipAddress[0], ipAddress[1], ipAddress[2], ipAddress[3], |
| 68 | ipAddress[4], ipAddress[5], ipAddress[6], ipAddress[7], |
| 69 | ipAddress[8], ipAddress[9], ipAddress[10], |
| 70 | ipAddress[11], ipAddress[12], ipAddress[13], |
| 71 | ipAddress[14], ipAddress[15]}); |
| 72 | endpoint.address(ipv6Addr); |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | BMCWEB_LOG_ERROR |
| 77 | << "Resolve failed to fetch the IP address"; |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 78 | handler(ec, endpointList); |
| 79 | return; |
| 80 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 81 | endpoint.port(port); |
| 82 | BMCWEB_LOG_DEBUG << "resolved endpoint is : " << endpoint; |
| 83 | endpointList.push_back(endpoint); |
| 84 | } |
| 85 | // All the resolved data is filled in the endpointList |
| 86 | handler(ec, endpointList); |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 87 | }, |
| 88 | "org.freedesktop.resolve1", "/org/freedesktop/resolve1", |
| 89 | "org.freedesktop.resolve1.Manager", "ResolveHostname", 0, host, |
| 90 | AF_UNSPEC, flag); |
| 91 | } |
| 92 | }; |
| 93 | |
| 94 | } // namespace async_resolve |
| 95 | } // namespace crow |