2018-04-19 13:56:14 +00:00
|
|
|
#include "DNSResolver.h"
|
2019-06-28 18:06:38 +00:00
|
|
|
#include <common/SimpleCache.h>
|
2017-08-31 12:55:19 +00:00
|
|
|
#include <Common/Exception.h>
|
2019-07-04 10:38:44 +00:00
|
|
|
#include <Common/ProfileEvents.h>
|
2019-06-27 16:28:26 +00:00
|
|
|
#include <Core/Names.h>
|
2017-08-07 17:01:04 +00:00
|
|
|
#include <Core/Types.h>
|
2020-06-05 20:53:46 +00:00
|
|
|
#include <Poco/Net/IPAddress.h>
|
2017-08-31 12:55:19 +00:00
|
|
|
#include <Poco/Net/DNS.h>
|
2017-08-28 18:39:57 +00:00
|
|
|
#include <Poco/Net/NetException.h>
|
2017-08-31 12:55:19 +00:00
|
|
|
#include <Poco/NumberParser.h>
|
|
|
|
#include <arpa/inet.h>
|
2018-03-29 20:21:01 +00:00
|
|
|
#include <atomic>
|
2018-06-15 13:45:19 +00:00
|
|
|
#include <optional>
|
2020-06-05 20:53:46 +00:00
|
|
|
#include <string_view>
|
2017-08-07 17:01:04 +00:00
|
|
|
|
2019-07-04 10:38:44 +00:00
|
|
|
namespace ProfileEvents
|
|
|
|
{
|
2020-03-13 19:00:00 +00:00
|
|
|
extern Event DNSError;
|
2019-07-04 10:38:44 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 20:53:46 +00:00
|
|
|
namespace std
|
|
|
|
{
|
|
|
|
template<> struct hash<Poco::Net::IPAddress>
|
|
|
|
{
|
|
|
|
size_t operator()(const Poco::Net::IPAddress & address) const noexcept
|
|
|
|
{
|
|
|
|
std::string_view addr(static_cast<const char *>(address.addr()), address.length());
|
|
|
|
std::hash<std::string_view> hash_impl;
|
|
|
|
return hash_impl(addr);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2017-08-07 17:01:04 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-09-01 18:36:03 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int BAD_ARGUMENTS;
|
2020-06-05 20:53:46 +00:00
|
|
|
extern const int DNS_ERROR;
|
2017-09-01 18:36:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-31 12:55:19 +00:00
|
|
|
/// Slightly altered implementation from https://github.com/pocoproject/poco/blob/poco-1.6.1/Net/src/SocketAddress.cpp#L86
|
|
|
|
static void splitHostAndPort(const std::string & host_and_port, std::string & out_host, UInt16 & out_port)
|
|
|
|
{
|
|
|
|
String port_str;
|
|
|
|
out_host.clear();
|
|
|
|
|
|
|
|
auto it = host_and_port.begin();
|
|
|
|
auto end = host_and_port.end();
|
|
|
|
|
|
|
|
if (*it == '[') /// Try parse case '[<IPv6 or something else>]:<port>'
|
|
|
|
{
|
|
|
|
++it;
|
|
|
|
while (it != end && *it != ']')
|
|
|
|
out_host += *it++;
|
|
|
|
if (it == end)
|
|
|
|
throw Exception("Malformed IPv6 address", ErrorCodes::BAD_ARGUMENTS);
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
else /// Case '<IPv4 or domain name or something else>:<port>'
|
|
|
|
{
|
|
|
|
while (it != end && *it != ':')
|
|
|
|
out_host += *it++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (it != end && *it == ':')
|
|
|
|
{
|
|
|
|
++it;
|
|
|
|
while (it != end)
|
|
|
|
port_str += *it++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw Exception("Missing port number", ErrorCodes::BAD_ARGUMENTS);
|
|
|
|
|
|
|
|
unsigned port;
|
|
|
|
if (Poco::NumberParser::tryParseUnsigned(port_str, port) && port <= 0xFFFF)
|
|
|
|
{
|
|
|
|
out_port = static_cast<UInt16>(port);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct servent * se = getservbyname(port_str.c_str(), nullptr);
|
|
|
|
if (se)
|
2017-09-06 02:42:17 +00:00
|
|
|
out_port = ntohs(static_cast<UInt16>(se->s_port));
|
2017-08-31 12:55:19 +00:00
|
|
|
else
|
|
|
|
throw Exception("Service not found", ErrorCodes::BAD_ARGUMENTS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-05 20:53:46 +00:00
|
|
|
static DNSResolver::IPAddresses resolveIPAddressImpl(const std::string & host)
|
2017-08-07 17:01:04 +00:00
|
|
|
{
|
2020-06-05 20:53:46 +00:00
|
|
|
Poco::Net::IPAddress ip;
|
|
|
|
|
2017-08-28 18:39:57 +00:00
|
|
|
/// NOTE: Poco::Net::DNS::resolveOne(host) doesn't work for IP addresses like 127.0.0.2
|
2020-06-05 20:53:46 +00:00
|
|
|
if (Poco::Net::IPAddress::tryParse(host, ip))
|
|
|
|
return DNSResolver::IPAddresses(1, ip);
|
|
|
|
|
|
|
|
/// Family: AF_UNSPEC
|
|
|
|
/// AI_ALL is required for checking if client is allowed to connect from an address
|
|
|
|
auto flags = Poco::Net::DNS::DNS_HINT_AI_V4MAPPED | Poco::Net::DNS::DNS_HINT_AI_ALL;
|
|
|
|
/// Do not resolve IPv6 (or IPv4) if no local IPv6 (or IPv4) addresses are configured.
|
|
|
|
/// It should not affect client address checking, since client cannot connect from IPv6 address
|
|
|
|
/// if server has no IPv6 addresses.
|
|
|
|
flags |= Poco::Net::DNS::DNS_HINT_AI_ADDRCONFIG;
|
2020-06-06 21:48:38 +00:00
|
|
|
#if defined(ARCADIA_BUILD)
|
|
|
|
auto addresses = Poco::Net::DNS::hostByName(host, &Poco::Net::DNS::DEFAULT_DNS_TIMEOUT, flags).addresses();
|
|
|
|
#else
|
2020-06-05 20:53:46 +00:00
|
|
|
auto addresses = Poco::Net::DNS::hostByName(host, flags).addresses();
|
2020-06-06 21:48:38 +00:00
|
|
|
#endif
|
2020-06-05 20:53:46 +00:00
|
|
|
if (addresses.empty())
|
|
|
|
throw Exception("Not found address of host: " + host, ErrorCodes::DNS_ERROR);
|
|
|
|
|
|
|
|
return addresses;
|
|
|
|
}
|
|
|
|
|
|
|
|
static String reverseResolveImpl(const Poco::Net::IPAddress & address)
|
|
|
|
{
|
|
|
|
Poco::Net::SocketAddress sock_addr(address, 0);
|
|
|
|
|
|
|
|
/// Resolve by hand, because Poco::Net::DNS::hostByAddress(...) does getaddrinfo(...) after getnameinfo(...)
|
|
|
|
char host[1024];
|
|
|
|
int err = getnameinfo(sock_addr.addr(), sock_addr.length(), host, sizeof(host), nullptr, 0, NI_NAMEREQD);
|
|
|
|
if (err)
|
|
|
|
throw Exception("Cannot getnameinfo(" + address.toString() + "): " + gai_strerror(err), ErrorCodes::DNS_ERROR);
|
|
|
|
return host;
|
2017-08-07 17:01:04 +00:00
|
|
|
}
|
|
|
|
|
2018-04-19 13:56:14 +00:00
|
|
|
struct DNSResolver::Impl
|
2017-08-07 17:01:04 +00:00
|
|
|
{
|
|
|
|
SimpleCache<decltype(resolveIPAddressImpl), &resolveIPAddressImpl> cache_host;
|
2020-06-05 20:53:46 +00:00
|
|
|
SimpleCache<decltype(reverseResolveImpl), &reverseResolveImpl> cache_address;
|
2018-03-29 20:21:01 +00:00
|
|
|
|
2019-06-27 16:28:26 +00:00
|
|
|
std::mutex drop_mutex;
|
|
|
|
std::mutex update_mutex;
|
|
|
|
|
2018-06-15 13:45:19 +00:00
|
|
|
/// Cached server host name
|
|
|
|
std::optional<String> host_name;
|
|
|
|
|
2019-06-27 16:28:26 +00:00
|
|
|
/// Store hosts, which was asked to resolve from last update of DNS cache.
|
|
|
|
NameSet new_hosts;
|
2020-06-05 20:53:46 +00:00
|
|
|
std::unordered_set<Poco::Net::IPAddress> new_addresses;
|
2019-06-27 16:28:26 +00:00
|
|
|
|
|
|
|
/// Store all hosts, which was whenever asked to resolve
|
|
|
|
NameSet known_hosts;
|
2020-06-05 20:53:46 +00:00
|
|
|
std::unordered_set<Poco::Net::IPAddress> known_addresses;
|
2019-06-27 16:28:26 +00:00
|
|
|
|
2018-03-29 20:21:01 +00:00
|
|
|
/// If disabled, will not make cache lookups, will resolve addresses manually on each call
|
2018-04-19 13:56:14 +00:00
|
|
|
std::atomic<bool> disable_cache{false};
|
2017-08-07 17:01:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-06-05 20:53:46 +00:00
|
|
|
DNSResolver::DNSResolver() : impl(std::make_unique<DNSResolver::Impl>()), log(&Poco::Logger::get("DNSResolver")) {}
|
2017-08-07 17:01:04 +00:00
|
|
|
|
2018-04-19 13:56:14 +00:00
|
|
|
Poco::Net::IPAddress DNSResolver::resolveHost(const std::string & host)
|
2020-06-05 20:53:46 +00:00
|
|
|
{
|
|
|
|
return resolveHostAll(host).front();
|
|
|
|
}
|
|
|
|
|
|
|
|
DNSResolver::IPAddresses DNSResolver::resolveHostAll(const std::string & host)
|
2017-08-07 17:01:04 +00:00
|
|
|
{
|
2019-06-27 16:28:26 +00:00
|
|
|
if (impl->disable_cache)
|
|
|
|
return resolveIPAddressImpl(host);
|
|
|
|
|
|
|
|
addToNewHosts(host);
|
|
|
|
return impl->cache_host(host);
|
2017-08-07 17:01:04 +00:00
|
|
|
}
|
|
|
|
|
2018-04-19 13:56:14 +00:00
|
|
|
Poco::Net::SocketAddress DNSResolver::resolveAddress(const std::string & host_and_port)
|
2017-08-07 17:01:04 +00:00
|
|
|
{
|
2019-06-27 16:28:26 +00:00
|
|
|
if (impl->disable_cache)
|
|
|
|
return Poco::Net::SocketAddress(host_and_port);
|
|
|
|
|
2017-08-31 12:55:19 +00:00
|
|
|
String host;
|
|
|
|
UInt16 port;
|
|
|
|
splitHostAndPort(host_and_port, host, port);
|
|
|
|
|
2019-06-27 16:28:26 +00:00
|
|
|
addToNewHosts(host);
|
2020-06-05 20:53:46 +00:00
|
|
|
return Poco::Net::SocketAddress(impl->cache_host(host).front(), port);
|
2018-03-29 20:21:01 +00:00
|
|
|
}
|
|
|
|
|
2018-04-19 13:56:14 +00:00
|
|
|
Poco::Net::SocketAddress DNSResolver::resolveAddress(const std::string & host, UInt16 port)
|
2018-03-29 20:21:01 +00:00
|
|
|
{
|
2019-06-27 16:28:26 +00:00
|
|
|
if (impl->disable_cache)
|
|
|
|
return Poco::Net::SocketAddress(host, port);
|
|
|
|
|
|
|
|
addToNewHosts(host);
|
2020-06-05 20:53:46 +00:00
|
|
|
return Poco::Net::SocketAddress(impl->cache_host(host).front(), port);
|
|
|
|
}
|
|
|
|
|
|
|
|
String DNSResolver::reverseResolve(const Poco::Net::IPAddress & address)
|
|
|
|
{
|
|
|
|
if (impl->disable_cache)
|
|
|
|
return reverseResolveImpl(address);
|
|
|
|
|
|
|
|
addToNewAddresses(address);
|
|
|
|
return impl->cache_address(address);
|
2017-08-07 17:01:04 +00:00
|
|
|
}
|
|
|
|
|
2018-04-19 13:56:14 +00:00
|
|
|
void DNSResolver::dropCache()
|
2017-08-07 17:01:04 +00:00
|
|
|
{
|
2017-08-31 12:55:19 +00:00
|
|
|
impl->cache_host.drop();
|
2020-06-05 20:53:46 +00:00
|
|
|
impl->cache_address.drop();
|
2018-06-15 13:45:19 +00:00
|
|
|
|
2019-06-27 16:28:26 +00:00
|
|
|
std::scoped_lock lock(impl->update_mutex, impl->drop_mutex);
|
|
|
|
|
|
|
|
impl->known_hosts.clear();
|
2020-06-05 20:53:46 +00:00
|
|
|
impl->known_addresses.clear();
|
2019-06-27 16:28:26 +00:00
|
|
|
impl->new_hosts.clear();
|
2020-06-05 20:53:46 +00:00
|
|
|
impl->new_addresses.clear();
|
2018-06-15 13:45:19 +00:00
|
|
|
impl->host_name.reset();
|
2017-08-07 17:01:04 +00:00
|
|
|
}
|
|
|
|
|
2018-04-19 13:56:14 +00:00
|
|
|
void DNSResolver::setDisableCacheFlag(bool is_disabled)
|
2018-03-29 20:21:01 +00:00
|
|
|
{
|
2018-04-19 13:56:14 +00:00
|
|
|
impl->disable_cache = is_disabled;
|
2018-03-29 20:21:01 +00:00
|
|
|
}
|
|
|
|
|
2018-06-15 13:45:19 +00:00
|
|
|
String DNSResolver::getHostName()
|
|
|
|
{
|
|
|
|
if (impl->disable_cache)
|
|
|
|
return Poco::Net::DNS::hostName();
|
|
|
|
|
2019-06-27 16:28:26 +00:00
|
|
|
std::lock_guard lock(impl->drop_mutex);
|
2018-06-15 13:45:19 +00:00
|
|
|
|
|
|
|
if (!impl->host_name.has_value())
|
|
|
|
impl->host_name.emplace(Poco::Net::DNS::hostName());
|
|
|
|
|
|
|
|
return *impl->host_name;
|
|
|
|
}
|
|
|
|
|
2020-06-05 20:53:46 +00:00
|
|
|
static const String & cacheElemToString(const String & str) { return str; }
|
|
|
|
static String cacheElemToString(const Poco::Net::IPAddress & addr) { return addr.toString(); }
|
2019-06-27 16:28:26 +00:00
|
|
|
|
2020-06-05 20:53:46 +00:00
|
|
|
template<typename UpdateF, typename ElemsT>
|
2020-06-06 21:48:38 +00:00
|
|
|
bool DNSResolver::updateCacheImpl(UpdateF && update_func, ElemsT && elems, const String & log_msg)
|
2020-06-05 20:53:46 +00:00
|
|
|
{
|
2019-06-27 16:28:26 +00:00
|
|
|
bool updated = false;
|
2020-06-05 20:53:46 +00:00
|
|
|
String lost_elems;
|
|
|
|
for (const auto & elem : elems)
|
2019-06-27 16:28:26 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-06-05 20:53:46 +00:00
|
|
|
updated |= (this->*update_func)(elem);
|
2019-06-27 16:28:26 +00:00
|
|
|
}
|
2019-07-04 10:38:44 +00:00
|
|
|
catch (const Poco::Net::NetException &)
|
|
|
|
{
|
2020-03-13 19:00:00 +00:00
|
|
|
ProfileEvents::increment(ProfileEvents::DNSError);
|
2019-07-04 10:38:44 +00:00
|
|
|
|
2020-06-05 20:53:46 +00:00
|
|
|
if (!lost_elems.empty())
|
|
|
|
lost_elems += ", ";
|
|
|
|
lost_elems += cacheElemToString(elem);
|
2019-07-04 10:38:44 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
tryLogCurrentException(__PRETTY_FUNCTION__);
|
|
|
|
}
|
2019-06-27 16:28:26 +00:00
|
|
|
}
|
2019-07-04 10:38:44 +00:00
|
|
|
|
2020-06-05 20:53:46 +00:00
|
|
|
if (!lost_elems.empty())
|
|
|
|
LOG_INFO(log, log_msg, lost_elems);
|
2019-07-04 10:38:44 +00:00
|
|
|
|
2019-06-27 16:28:26 +00:00
|
|
|
return updated;
|
|
|
|
}
|
|
|
|
|
2020-06-05 20:53:46 +00:00
|
|
|
bool DNSResolver::updateCache()
|
|
|
|
{
|
|
|
|
LOG_DEBUG(log, "Updating DNS cache");
|
|
|
|
|
|
|
|
{
|
2020-07-08 14:39:48 +00:00
|
|
|
String updated_host_name = Poco::Net::DNS::hostName();
|
|
|
|
|
2020-06-05 20:53:46 +00:00
|
|
|
std::lock_guard lock(impl->drop_mutex);
|
|
|
|
|
2020-06-07 00:18:54 +00:00
|
|
|
for (const auto & host : impl->new_hosts)
|
2020-06-07 15:17:23 +00:00
|
|
|
impl->known_hosts.insert(host);
|
2020-06-05 20:53:46 +00:00
|
|
|
impl->new_hosts.clear();
|
|
|
|
|
2020-06-07 00:18:54 +00:00
|
|
|
for (const auto & address : impl->new_addresses)
|
2020-06-07 15:17:23 +00:00
|
|
|
impl->known_addresses.insert(address);
|
2020-06-05 20:53:46 +00:00
|
|
|
impl->new_addresses.clear();
|
|
|
|
|
2020-07-08 14:39:48 +00:00
|
|
|
impl->host_name.emplace(updated_host_name);
|
2020-06-05 20:53:46 +00:00
|
|
|
}
|
|
|
|
|
2020-08-08 00:47:03 +00:00
|
|
|
/// FIXME Updating may take a long time because we cannot manage timeouts of getaddrinfo(...) and getnameinfo(...).
|
2020-06-05 20:53:46 +00:00
|
|
|
/// DROP DNS CACHE will wait on update_mutex (possibly while holding drop_mutex)
|
|
|
|
std::lock_guard lock(impl->update_mutex);
|
|
|
|
|
|
|
|
bool hosts_updated = updateCacheImpl(&DNSResolver::updateHost, impl->known_hosts, "Cached hosts not found: {}");
|
|
|
|
updateCacheImpl(&DNSResolver::updateAddress, impl->known_addresses, "Cached addresses not found: {}");
|
|
|
|
|
|
|
|
LOG_DEBUG(log, "Updated DNS cache");
|
|
|
|
return hosts_updated;
|
|
|
|
}
|
|
|
|
|
2019-06-27 16:28:26 +00:00
|
|
|
bool DNSResolver::updateHost(const String & host)
|
|
|
|
{
|
|
|
|
/// Usage of updateHost implies that host is already in cache and there is no extra computations
|
|
|
|
auto old_value = impl->cache_host(host);
|
|
|
|
impl->cache_host.update(host);
|
|
|
|
return old_value != impl->cache_host(host);
|
|
|
|
}
|
|
|
|
|
2020-06-05 20:53:46 +00:00
|
|
|
bool DNSResolver::updateAddress(const Poco::Net::IPAddress & address)
|
|
|
|
{
|
|
|
|
auto old_value = impl->cache_address(address);
|
|
|
|
impl->cache_address.update(address);
|
|
|
|
return old_value == impl->cache_address(address);
|
|
|
|
}
|
|
|
|
|
2019-06-27 16:28:26 +00:00
|
|
|
void DNSResolver::addToNewHosts(const String & host)
|
|
|
|
{
|
|
|
|
std::lock_guard lock(impl->drop_mutex);
|
|
|
|
impl->new_hosts.insert(host);
|
|
|
|
}
|
|
|
|
|
2020-06-05 20:53:46 +00:00
|
|
|
void DNSResolver::addToNewAddresses(const Poco::Net::IPAddress & address)
|
|
|
|
{
|
|
|
|
std::lock_guard lock(impl->drop_mutex);
|
|
|
|
impl->new_addresses.insert(address);
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:56:14 +00:00
|
|
|
DNSResolver::~DNSResolver() = default;
|
2017-08-07 17:01:04 +00:00
|
|
|
|
2019-08-22 03:24:05 +00:00
|
|
|
DNSResolver & DNSResolver::instance()
|
|
|
|
{
|
|
|
|
static DNSResolver ret;
|
|
|
|
return ret;
|
|
|
|
}
|
2017-08-07 17:01:04 +00:00
|
|
|
|
|
|
|
}
|