blob: 8a5cc4e7fe145c777ae6d2be0182996a6caf2fed [file] [log] [blame]
Sunitha Harish29a82b02021-02-18 15:54:16 +05301#pragma once
2#include <boost/asio/ip/address.hpp>
3#include <boost/asio/ip/basic_endpoint.hpp>
4#include <sdbusplus/message.hpp>
5
6#include <charconv>
7#include <iostream>
8#include <memory>
9
10namespace crow
11{
12
13namespace async_resolve
14{
15
16class Resolver
17{
18 public:
19 Resolver() = default;
20
21 ~Resolver() = default;
22
Ed Tanousecd6a3a2022-01-07 09:18:40 -080023 Resolver(const Resolver&) = delete;
24 Resolver(Resolver&&) = delete;
25 Resolver& operator=(const Resolver&) = delete;
26 Resolver& operator=(Resolver&&) = delete;
27
Sunitha Harish29a82b02021-02-18 15:54:16 +053028 template <typename ResolveHandler>
Ed Tanouseb1c47d2022-02-09 11:47:27 -080029 void asyncResolve(const std::string& host, uint16_t port,
Sunitha Harish29a82b02021-02-18 15:54:16 +053030 ResolveHandler&& handler)
31 {
32 BMCWEB_LOG_DEBUG << "Trying to resolve: " << host << ":" << port;
33 uint64_t flag = 0;
34 crow::connections::systemBus->async_method_call(
Ed Tanousf94c4ec2022-01-06 12:44:41 -080035 [host, port, handler{std::forward<ResolveHandler>(handler)}](
Sunitha Harish29a82b02021-02-18 15:54:16 +053036 const boost::system::error_code ec,
37 const std::vector<
38 std::tuple<int32_t, int32_t, std::vector<uint8_t>>>& resp,
39 const std::string& hostName, const uint64_t flagNum) {
Ed Tanous002d39b2022-05-31 08:59:27 -070040 std::vector<boost::asio::ip::tcp::endpoint> endpointList;
41 if (ec)
42 {
43 BMCWEB_LOG_ERROR << "Resolve failed: " << ec.message();
44 handler(ec, endpointList);
45 return;
46 }
47 BMCWEB_LOG_DEBUG << "ResolveHostname returned: " << hostName << ":"
48 << flagNum;
49 // Extract the IP address from the response
50 for (auto resolveList : resp)
51 {
52 std::vector<uint8_t> ipAddress = std::get<2>(resolveList);
53 boost::asio::ip::tcp::endpoint endpoint;
54 if (ipAddress.size() == 4) // ipv4 address
Sunitha Harish29a82b02021-02-18 15:54:16 +053055 {
Ed Tanous002d39b2022-05-31 08:59:27 -070056 BMCWEB_LOG_DEBUG << "ipv4 address";
57 boost::asio::ip::address_v4 ipv4Addr(
58 {ipAddress[0], ipAddress[1], ipAddress[2],
59 ipAddress[3]});
60 endpoint.address(ipv4Addr);
61 }
62 else if (ipAddress.size() == 16) // ipv6 address
63 {
64 BMCWEB_LOG_DEBUG << "ipv6 address";
65 boost::asio::ip::address_v6 ipv6Addr(
66 {ipAddress[0], ipAddress[1], ipAddress[2], ipAddress[3],
67 ipAddress[4], ipAddress[5], ipAddress[6], ipAddress[7],
68 ipAddress[8], ipAddress[9], ipAddress[10],
69 ipAddress[11], ipAddress[12], ipAddress[13],
70 ipAddress[14], ipAddress[15]});
71 endpoint.address(ipv6Addr);
72 }
73 else
74 {
75 BMCWEB_LOG_ERROR
76 << "Resolve failed to fetch the IP address";
Sunitha Harish29a82b02021-02-18 15:54:16 +053077 handler(ec, endpointList);
78 return;
79 }
Ed Tanous002d39b2022-05-31 08:59:27 -070080 endpoint.port(port);
81 BMCWEB_LOG_DEBUG << "resolved endpoint is : " << endpoint;
82 endpointList.push_back(endpoint);
83 }
84 // All the resolved data is filled in the endpointList
85 handler(ec, endpointList);
Sunitha Harish29a82b02021-02-18 15:54:16 +053086 },
87 "org.freedesktop.resolve1", "/org/freedesktop/resolve1",
88 "org.freedesktop.resolve1.Manager", "ResolveHostname", 0, host,
89 AF_UNSPEC, flag);
90 }
91};
92
93} // namespace async_resolve
94} // namespace crow