Skip to content

Commit b035ad7

Browse files
committed
Fix error with IPv6 in host param and impove HostPort reading from input stream
1 parent 3cb4337 commit b035ad7

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

programs/client/Client.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -482,10 +482,6 @@ catch (...)
482482

483483
void Client::connect()
484484
{
485-
for (auto host_port : hosts_ports)
486-
{
487-
DB::DNSResolver::instance().resolveHost(host_port.host);
488-
}
489485
UInt16 default_port = ConnectionParameters::getPortFromConfig(config());
490486
connection_parameters = ConnectionParameters(config(), hosts_ports[0].host,
491487
hosts_ports[0].port.value_or(default_port));

src/Client/ClientBase.h

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <Common/InterruptListener.h>
66
#include <Common/ShellCommand.h>
77
#include <Common/Stopwatch.h>
8+
#include <Common/DNSResolver.h>
89
#include <Core/ExternalTable.h>
910
#include <Poco/Util/Application.h>
1011
#include <Interpreters/Context.h>
@@ -239,20 +240,27 @@ class ClientBase : public Poco::Util::Application, public IHints<2, ClientBase>
239240
struct HostPort
240241
{
241242
String host;
242-
std::optional<int> port{};
243+
std::optional<UInt16> port{};
243244
friend std::istream & operator>>(std::istream & in, HostPort & hostPort)
244245
{
245246
String host_with_port;
246-
String delimiter = ":";
247247
in >> host_with_port;
248-
size_t delimiter_pos = host_with_port.find(delimiter);
249-
if (delimiter_pos != String::npos)
248+
DB::DNSResolver & resolver = DB::DNSResolver::instance();
249+
try
250250
{
251-
hostPort.host = host_with_port.substr(0, delimiter_pos);
252-
hostPort.port = boost::lexical_cast<uint>(host_with_port.substr(delimiter_pos + 1, host_with_port.length()));
251+
Poco::Net::SocketAddress address = resolver.resolveAddress(host_with_port);
252+
hostPort.host = address.host().toString();
253+
hostPort.port = address.port();
254+
}
255+
catch (const Exception & e)
256+
{
257+
if (e.message() == "Missing port number") {
258+
hostPort.host = resolver.resolveHost(host_with_port).toString();
259+
hostPort.port = std::nullopt;
260+
return in;
261+
}
262+
throw;
253263
}
254-
else
255-
hostPort.host = host_with_port;
256264
return in;
257265
}
258266
};

0 commit comments

Comments
 (0)