2017-08-07 17:01:04 +00:00
|
|
|
#pragma once
|
|
|
|
#include <Poco/Net/IPAddress.h>
|
|
|
|
#include <Poco/Net/SocketAddress.h>
|
|
|
|
#include <memory>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/types.h>
|
2019-06-27 16:28:26 +00:00
|
|
|
#include <Core/Names.h>
|
2019-08-22 03:24:05 +00:00
|
|
|
#include <boost/noncopyable.hpp>
|
2023-08-14 16:57:40 +00:00
|
|
|
#include <Common/LoggingFormatStringHelpers.h>
|
2017-08-07 17:01:04 +00:00
|
|
|
|
|
|
|
|
2023-04-08 04:47:21 +00:00
|
|
|
namespace Poco { class Logger; }
|
|
|
|
|
2017-08-07 17:01:04 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2019-06-27 16:28:26 +00:00
|
|
|
/// A singleton implementing DNS names resolving with optional DNS cache
|
2019-08-22 03:24:05 +00:00
|
|
|
/// The cache is being updated asynchronous in separate thread (see DNSCacheUpdater)
|
2019-06-27 16:28:26 +00:00
|
|
|
/// or it could be updated manually via drop() method.
|
2019-08-22 03:24:05 +00:00
|
|
|
class DNSResolver : private boost::noncopyable
|
2017-08-07 17:01:04 +00:00
|
|
|
{
|
|
|
|
public:
|
2022-03-11 21:47:28 +00:00
|
|
|
using IPAddresses = std::vector<Poco::Net::IPAddress>;
|
2024-02-17 00:46:41 +00:00
|
|
|
using IPAddressesPtr = std::shared_ptr<IPAddresses>;
|
2020-06-05 20:53:46 +00:00
|
|
|
|
2019-08-22 03:24:05 +00:00
|
|
|
static DNSResolver & instance();
|
2017-08-07 17:01:04 +00:00
|
|
|
|
2018-04-19 13:56:14 +00:00
|
|
|
DNSResolver(const DNSResolver &) = delete;
|
2017-08-07 17:01:04 +00:00
|
|
|
|
2020-06-05 20:53:46 +00:00
|
|
|
/// Accepts host names like 'example.com' or '127.0.0.1' or '::1' and resolves its IP
|
2017-08-07 17:01:04 +00:00
|
|
|
Poco::Net::IPAddress resolveHost(const std::string & host);
|
|
|
|
|
2020-06-05 20:53:46 +00:00
|
|
|
/// Accepts host names like 'example.com' or '127.0.0.1' or '::1' and resolves all its IPs
|
|
|
|
IPAddresses resolveHostAll(const std::string & host);
|
|
|
|
|
|
|
|
/// Accepts host names like 'example.com:port' or '127.0.0.1:port' or '[::1]:port' and resolves its IP and port
|
2018-03-29 20:21:01 +00:00
|
|
|
Poco::Net::SocketAddress resolveAddress(const std::string & host_and_port);
|
|
|
|
|
|
|
|
Poco::Net::SocketAddress resolveAddress(const std::string & host, UInt16 port);
|
|
|
|
|
2022-05-16 21:47:07 +00:00
|
|
|
std::vector<Poco::Net::SocketAddress> resolveAddressList(const std::string & host, UInt16 port);
|
|
|
|
|
2022-06-03 01:54:14 +00:00
|
|
|
/// Accepts host IP and resolves its host names
|
2022-08-29 18:11:39 +00:00
|
|
|
std::unordered_set<String> reverseResolve(const Poco::Net::IPAddress & address);
|
2020-06-05 20:53:46 +00:00
|
|
|
|
2018-06-15 13:45:19 +00:00
|
|
|
/// Get this server host name
|
|
|
|
String getHostName();
|
|
|
|
|
2018-03-29 20:21:01 +00:00
|
|
|
/// Disables caching
|
2018-04-19 13:56:14 +00:00
|
|
|
void setDisableCacheFlag(bool is_disabled = true);
|
2017-08-07 17:01:04 +00:00
|
|
|
|
2017-08-31 12:55:19 +00:00
|
|
|
/// Drops all caches
|
2018-04-19 13:56:14 +00:00
|
|
|
void dropCache();
|
2017-08-07 17:01:04 +00:00
|
|
|
|
2022-09-25 12:06:13 +00:00
|
|
|
/// Removes an entry from cache or does nothing
|
|
|
|
void removeHostFromCache(const std::string & host);
|
|
|
|
|
2019-06-27 16:28:26 +00:00
|
|
|
/// Updates all known hosts in cache.
|
2022-04-05 11:00:14 +00:00
|
|
|
/// Returns true if IP of any host has been changed or an element was dropped (too many failures)
|
|
|
|
bool updateCache(UInt32 max_consecutive_failures);
|
2019-06-27 16:28:26 +00:00
|
|
|
|
2018-04-19 13:56:14 +00:00
|
|
|
~DNSResolver();
|
2017-08-07 17:01:04 +00:00
|
|
|
|
2019-06-27 16:28:26 +00:00
|
|
|
private:
|
2022-04-05 11:00:14 +00:00
|
|
|
template <typename UpdateF, typename ElemsT>
|
|
|
|
bool updateCacheImpl(
|
|
|
|
UpdateF && update_func,
|
|
|
|
ElemsT && elems,
|
|
|
|
UInt32 max_consecutive_failures,
|
2023-08-14 16:57:40 +00:00
|
|
|
FormatStringHelper<String> notfound_log_msg,
|
|
|
|
FormatStringHelper<String> dropped_log_msg);
|
2017-08-07 17:01:04 +00:00
|
|
|
|
2018-04-19 13:56:14 +00:00
|
|
|
DNSResolver();
|
2017-08-07 17:01:04 +00:00
|
|
|
|
|
|
|
struct Impl;
|
|
|
|
std::unique_ptr<Impl> impl;
|
2024-01-23 17:04:50 +00:00
|
|
|
LoggerPtr log;
|
2019-06-27 16:28:26 +00:00
|
|
|
|
2020-06-05 20:53:46 +00:00
|
|
|
/// Updates cached value and returns true it has been changed.
|
2019-06-27 16:28:26 +00:00
|
|
|
bool updateHost(const String & host);
|
2020-06-05 20:53:46 +00:00
|
|
|
bool updateAddress(const Poco::Net::IPAddress & address);
|
2019-06-27 16:28:26 +00:00
|
|
|
|
|
|
|
void addToNewHosts(const String & host);
|
2020-06-05 20:53:46 +00:00
|
|
|
void addToNewAddresses(const Poco::Net::IPAddress & address);
|
2017-08-07 17:01:04 +00:00
|
|
|
};
|
|
|
|
|
2017-08-30 21:25:44 +00:00
|
|
|
}
|