blob: 2d9899d1a4759c17055a8d40b6b0dad8f3d28ccd [file] [log] [blame]
Sunitha Harish29a82b02021-02-18 15:54:16 +05301#pragma once
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08002#include "dbus_singleton.hpp"
3#include "logging.hpp"
4
Sunitha Harish29a82b02021-02-18 15:54:16 +05305#include <boost/asio/ip/address.hpp>
6#include <boost/asio/ip/basic_endpoint.hpp>
Carson Labrado7e8890c2022-11-23 23:58:01 +00007#include <boost/asio/ip/tcp.hpp>
Sunitha Harish29a82b02021-02-18 15:54:16 +05308#include <sdbusplus/message.hpp>
9
10#include <charconv>
11#include <iostream>
12#include <memory>
13
Sunitha Harish29a82b02021-02-18 15:54:16 +053014namespace async_resolve
15{
16
Ed Tanouse1452be2021-10-04 17:03:52 -070017inline bool endpointFromResolveTuple(const std::vector<uint8_t>& ipAddress,
18 boost::asio::ip::tcp::endpoint& endpoint)
19{
20 if (ipAddress.size() == 4) // ipv4 address
21 {
Ed Tanous62598e32023-07-17 17:06:25 -070022 BMCWEB_LOG_DEBUG("ipv4 address");
Ed Tanouse1452be2021-10-04 17:03:52 -070023 boost::asio::ip::address_v4 ipv4Addr(
24 {ipAddress[0], ipAddress[1], ipAddress[2], ipAddress[3]});
25 endpoint.address(ipv4Addr);
26 }
27 else if (ipAddress.size() == 16) // ipv6 address
28 {
Ed Tanous62598e32023-07-17 17:06:25 -070029 BMCWEB_LOG_DEBUG("ipv6 address");
Ed Tanouse1452be2021-10-04 17:03:52 -070030 boost::asio::ip::address_v6 ipv6Addr(
31 {ipAddress[0], ipAddress[1], ipAddress[2], ipAddress[3],
32 ipAddress[4], ipAddress[5], ipAddress[6], ipAddress[7],
33 ipAddress[8], ipAddress[9], ipAddress[10], ipAddress[11],
34 ipAddress[12], ipAddress[13], ipAddress[14], ipAddress[15]});
35 endpoint.address(ipv6Addr);
36 }
37 else
38 {
Ed Tanous62598e32023-07-17 17:06:25 -070039 BMCWEB_LOG_ERROR("Resolve failed to fetch the IP address");
Ed Tanouse1452be2021-10-04 17:03:52 -070040 return false;
41 }
42 return true;
43}
44
Sunitha Harish29a82b02021-02-18 15:54:16 +053045class Resolver
46{
47 public:
Ed Tanousf8ca6d72022-06-28 12:12:03 -070048 // unused io param used to keep interface identical to
49 // boost::asio::tcp:::resolver
50 explicit Resolver(boost::asio::io_context& /*io*/) {}
Sunitha Harish29a82b02021-02-18 15:54:16 +053051
52 ~Resolver() = default;
53
Ed Tanousecd6a3a2022-01-07 09:18:40 -080054 Resolver(const Resolver&) = delete;
55 Resolver(Resolver&&) = delete;
56 Resolver& operator=(const Resolver&) = delete;
57 Resolver& operator=(Resolver&&) = delete;
58
Ed Tanousf8ca6d72022-06-28 12:12:03 -070059 using results_type = std::vector<boost::asio::ip::tcp::endpoint>;
60
Sunitha Harish29a82b02021-02-18 15:54:16 +053061 template <typename ResolveHandler>
Ed Tanousf8ca6d72022-06-28 12:12:03 -070062 // This function is kept using snake case so that it is interoperable with
63 // boost::asio::ip::tcp::resolver
64 // NOLINTNEXTLINE(readability-identifier-naming)
Ed Tanousa716aa72023-08-01 11:35:53 -070065 void async_resolve(std::string_view host, std::string_view port,
Ed Tanousf8ca6d72022-06-28 12:12:03 -070066 ResolveHandler&& handler)
Sunitha Harish29a82b02021-02-18 15:54:16 +053067 {
Ed Tanous62598e32023-07-17 17:06:25 -070068 BMCWEB_LOG_DEBUG("Trying to resolve: {}:{}", host, port);
Ed Tanousf8ca6d72022-06-28 12:12:03 -070069
70 uint16_t portNum = 0;
71
72 auto it = std::from_chars(&*port.begin(), &*port.end(), portNum);
73 if (it.ec != std::errc())
74 {
Ed Tanous62598e32023-07-17 17:06:25 -070075 BMCWEB_LOG_ERROR("Failed to get the Port");
Ed Tanousf8ca6d72022-06-28 12:12:03 -070076 handler(std::make_error_code(std::errc::invalid_argument),
77 results_type{});
78
79 return;
80 }
81
Sunitha Harish29a82b02021-02-18 15:54:16 +053082 uint64_t flag = 0;
83 crow::connections::systemBus->async_method_call(
Ed Tanousa716aa72023-08-01 11:35:53 -070084 [host{std::string(host)}, portNum,
Ed Tanous8cb2c022024-03-27 16:31:46 -070085 handler = std::forward<ResolveHandler>(handler)](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -080086 const boost::system::error_code& ec,
Sunitha Harish29a82b02021-02-18 15:54:16 +053087 const std::vector<
88 std::tuple<int32_t, int32_t, std::vector<uint8_t>>>& resp,
89 const std::string& hostName, const uint64_t flagNum) {
Ed Tanousf8ca6d72022-06-28 12:12:03 -070090 results_type endpointList;
Ed Tanous002d39b2022-05-31 08:59:27 -070091 if (ec)
92 {
Ed Tanous62598e32023-07-17 17:06:25 -070093 BMCWEB_LOG_ERROR("Resolve failed: {}", ec.message());
Ed Tanous002d39b2022-05-31 08:59:27 -070094 handler(ec, endpointList);
95 return;
96 }
Ed Tanous62598e32023-07-17 17:06:25 -070097 BMCWEB_LOG_DEBUG("ResolveHostname returned: {}:{}", hostName,
98 flagNum);
Ed Tanous002d39b2022-05-31 08:59:27 -070099 // Extract the IP address from the response
Ed Tanousf8ca6d72022-06-28 12:12:03 -0700100 for (const std::tuple<int32_t, int32_t, std::vector<uint8_t>>&
101 resolveList : resp)
Ed Tanous002d39b2022-05-31 08:59:27 -0700102 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700103 boost::asio::ip::tcp::endpoint endpoint;
Ed Tanousf8ca6d72022-06-28 12:12:03 -0700104 endpoint.port(portNum);
Ed Tanouse1452be2021-10-04 17:03:52 -0700105 if (!endpointFromResolveTuple(std::get<2>(resolveList),
106 endpoint))
107 {
108 boost::system::error_code ecErr = make_error_code(
109 boost::system::errc::address_not_available);
110 handler(ecErr, endpointList);
111 }
Ed Tanous62598e32023-07-17 17:06:25 -0700112 BMCWEB_LOG_DEBUG("resolved endpoint is : {}",
113 endpoint.address().to_string());
Ed Tanous002d39b2022-05-31 08:59:27 -0700114 endpointList.push_back(endpoint);
115 }
116 // All the resolved data is filled in the endpointList
117 handler(ec, endpointList);
Patrick Williams5a39f772023-10-20 11:20:21 -0500118 },
Sunitha Harish29a82b02021-02-18 15:54:16 +0530119 "org.freedesktop.resolve1", "/org/freedesktop/resolve1",
120 "org.freedesktop.resolve1.Manager", "ResolveHostname", 0, host,
121 AF_UNSPEC, flag);
122 }
123};
124
125} // namespace async_resolve