From 0536db0ec304c7aa238fb70bcb440f5712a7cd4e Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Thu, 20 May 2021 08:13:17 +0300 Subject: [PATCH] Fix resolving of IPv6 addresses v2: move the check from Cluster to DNSResolver --- src/Common/DNSResolver.cpp | 17 ++++++++++++++--- .../0_stateless/01880_remote_ipv6.reference | 0 tests/queries/0_stateless/01880_remote_ipv6.sql | 8 ++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 tests/queries/0_stateless/01880_remote_ipv6.reference create mode 100644 tests/queries/0_stateless/01880_remote_ipv6.sql diff --git a/src/Common/DNSResolver.cpp b/src/Common/DNSResolver.cpp index f54d3e72df5..8b006bc550d 100644 --- a/src/Common/DNSResolver.cpp +++ b/src/Common/DNSResolver.cpp @@ -87,9 +87,20 @@ static DNSResolver::IPAddresses resolveIPAddressImpl(const std::string & host) { Poco::Net::IPAddress ip; - /// NOTE: Poco::Net::DNS::resolveOne(host) doesn't work for IP addresses like 127.0.0.2 - if (Poco::Net::IPAddress::tryParse(host, ip)) - return DNSResolver::IPAddresses(1, ip); + /// NOTE: + /// - Poco::Net::DNS::resolveOne(host) doesn't work for IP addresses like 127.0.0.2 + /// - Poco::Net::IPAddress::tryParse() expect hex string for IPv6 (w/o brackets) + if (host.starts_with('[')) + { + assert(host.ends_with(']')); + if (Poco::Net::IPAddress::tryParse(host.substr(1, host.size() - 2), ip)) + return DNSResolver::IPAddresses(1, ip); + } + else + { + 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 diff --git a/tests/queries/0_stateless/01880_remote_ipv6.reference b/tests/queries/0_stateless/01880_remote_ipv6.reference new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/queries/0_stateless/01880_remote_ipv6.sql b/tests/queries/0_stateless/01880_remote_ipv6.sql new file mode 100644 index 00000000000..11bdf850ec0 --- /dev/null +++ b/tests/queries/0_stateless/01880_remote_ipv6.sql @@ -0,0 +1,8 @@ +SET connections_with_failover_max_tries=0; + +SELECT * FROM remote('[::1]', system.one) FORMAT Null; +SELECT * FROM remote('[::1]:9000', system.one) FORMAT Null; + +SELECT * FROM remote('[::1', system.one) FORMAT Null; -- { serverError 36 } +SELECT * FROM remote('::1]', system.one) FORMAT Null; -- { serverError 519 } +SELECT * FROM remote('::1', system.one) FORMAT Null; -- { serverError 519 }