2022-07-12 17:21:10 +00:00
|
|
|
#pragma once
|
|
|
|
|
2022-10-12 16:56:00 +00:00
|
|
|
#include <span>
|
|
|
|
#include <poll.h>
|
2022-10-21 17:20:49 +00:00
|
|
|
#include <mutex>
|
2022-07-12 17:21:10 +00:00
|
|
|
#include "DNSPTRResolver.h"
|
|
|
|
|
|
|
|
using ares_channel = struct ares_channeldata *;
|
|
|
|
|
2022-07-13 12:40:56 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
2022-07-12 17:21:10 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Implements reverse DNS resolution using c-ares lib. System reverse DNS resolution via
|
2022-07-12 22:27:44 +00:00
|
|
|
* gethostbyaddr or getnameinfo does not work reliably because in some systems
|
2022-07-12 17:21:10 +00:00
|
|
|
* it returns all PTR records for a given IP and in others it returns only one.
|
|
|
|
* */
|
2022-07-13 12:40:56 +00:00
|
|
|
class CaresPTRResolver : public DNSPTRResolver
|
|
|
|
{
|
2022-07-12 17:21:10 +00:00
|
|
|
friend class DNSPTRResolverProvider;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allow only DNSPTRProvider to instantiate this class
|
|
|
|
* */
|
|
|
|
struct provider_token {};
|
2023-01-26 16:54:12 +00:00
|
|
|
|
|
|
|
static constexpr auto C_ARES_POLL_EVENTS = POLLRDNORM | POLLIN;
|
|
|
|
|
2022-07-12 17:21:10 +00:00
|
|
|
public:
|
2022-07-13 12:40:56 +00:00
|
|
|
explicit CaresPTRResolver(provider_token);
|
2022-07-12 17:21:10 +00:00
|
|
|
~CaresPTRResolver() override;
|
|
|
|
|
2022-08-29 18:11:39 +00:00
|
|
|
std::unordered_set<std::string> resolve(const std::string & ip) override;
|
2022-07-12 17:21:10 +00:00
|
|
|
|
2022-08-29 18:11:39 +00:00
|
|
|
std::unordered_set<std::string> resolve_v6(const std::string & ip) override;
|
2022-07-12 17:21:10 +00:00
|
|
|
|
|
|
|
private:
|
2023-01-26 16:54:12 +00:00
|
|
|
bool wait_and_process();
|
2023-01-25 22:07:58 +00:00
|
|
|
|
|
|
|
void cancel_requests();
|
2022-07-12 17:21:10 +00:00
|
|
|
|
2022-08-29 18:11:39 +00:00
|
|
|
void resolve(const std::string & ip, std::unordered_set<std::string> & response);
|
2022-07-12 17:21:10 +00:00
|
|
|
|
2022-08-29 18:11:39 +00:00
|
|
|
void resolve_v6(const std::string & ip, std::unordered_set<std::string> & response);
|
2022-07-12 17:21:10 +00:00
|
|
|
|
2022-10-12 16:56:00 +00:00
|
|
|
std::span<pollfd> get_readable_sockets(int * sockets, pollfd * pollfd);
|
|
|
|
|
|
|
|
int64_t calculate_timeout();
|
|
|
|
|
|
|
|
void process_possible_timeout();
|
2022-10-10 21:54:35 +00:00
|
|
|
|
2022-10-12 16:56:00 +00:00
|
|
|
void process_readable_sockets(std::span<pollfd> readable_sockets);
|
|
|
|
|
|
|
|
ares_channel channel;
|
2022-10-21 17:20:49 +00:00
|
|
|
|
2023-01-25 22:09:35 +00:00
|
|
|
static std::mutex mutex;
|
2022-07-12 17:21:10 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|