2017-08-07 17:01:04 +00:00
|
|
|
#pragma once
|
|
|
|
#include <Poco/Net/IPAddress.h>
|
|
|
|
#include <Poco/Net/SocketAddress.h>
|
|
|
|
#include <memory>
|
2018-03-29 20:21:01 +00:00
|
|
|
#include <Core/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>
|
2020-06-05 20:53:46 +00:00
|
|
|
#include <common/logger_useful.h>
|
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:
|
2020-06-05 20:53:46 +00:00
|
|
|
typedef std::vector<Poco::Net::IPAddress> IPAddresses;
|
|
|
|
|
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);
|
|
|
|
|
2020-06-05 20:53:46 +00:00
|
|
|
/// Accepts host IP and resolves its host name
|
|
|
|
String reverseResolve(const Poco::Net::IPAddress & address);
|
|
|
|
|
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
|
|
|
|
2019-06-27 16:28:26 +00:00
|
|
|
/// Updates all known hosts in cache.
|
|
|
|
/// Returns true if IP of any host has been changed.
|
|
|
|
bool updateCache();
|
|
|
|
|
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:
|
2020-06-05 20:53:46 +00:00
|
|
|
template<typename UpdateF, typename ElemsT>
|
2020-06-06 21:48:38 +00:00
|
|
|
bool updateCacheImpl(UpdateF && update_func, ElemsT && elems, const String & 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;
|
2020-06-05 20:53:46 +00:00
|
|
|
Poco::Logger * 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
|
|
|
}
|