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 {};
|
|
|
|
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:
|
|
|
|
void wait();
|
|
|
|
|
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
|
|
|
|
|
|
|
static std::mutex mutex;
|
2022-07-12 17:21:10 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|