#pragma once #include #include #include #include "DNSPTRResolver.h" using ares_channel = struct ares_channeldata *; namespace DB { /* * Implements reverse DNS resolution using c-ares lib. System reverse DNS resolution via * gethostbyaddr or getnameinfo does not work reliably because in some systems * it returns all PTR records for a given IP and in others it returns only one. * */ class CaresPTRResolver : public DNSPTRResolver { friend class DNSPTRResolverProvider; /* * Allow only DNSPTRProvider to instantiate this class * */ struct provider_token {}; public: explicit CaresPTRResolver(provider_token); ~CaresPTRResolver() override; std::unordered_set resolve(const std::string & ip) override; std::unordered_set resolve_v6(const std::string & ip) override; private: void wait(); void resolve(const std::string & ip, std::unordered_set & response); void resolve_v6(const std::string & ip, std::unordered_set & response); std::span get_readable_sockets(int * sockets, pollfd * pollfd); int64_t calculate_timeout(); void process_possible_timeout(); void process_readable_sockets(std::span readable_sockets); ares_channel channel; static std::mutex mutex; }; }