Skip to content

Commit f05e135

Browse files
committed
fix async_connect and host_name_verification
1 parent 536a299 commit f05e135

File tree

2 files changed

+27
-38
lines changed

2 files changed

+27
-38
lines changed

lib/ClientConnection.cc

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@
4141
#include "auth/InitialAuthData.h"
4242
#include "checksum/ChecksumProvider.h"
4343

44+
#ifdef USE_ASIO
45+
#include <asio/connect.hpp>
46+
#include <asio/ssl/host_name_verification.hpp>
47+
#else
48+
#include <boost/asio/connect.hpp>
49+
#include <boost/asio/ssl/host_name_verification.hpp>
50+
#endif
51+
4452
DECLARE_LOG_OBJECT()
4553

4654
using namespace ASIO::ip;
@@ -266,7 +274,7 @@ ClientConnection::ClientConnection(const std::string& logicalAddress, const std:
266274
if (!clientConfiguration.isTlsAllowInsecureConnection() && clientConfiguration.isValidateHostName()) {
267275
LOG_DEBUG("Validating hostname for " << serviceUrl.host() << ":" << serviceUrl.port());
268276
std::string urlHost = isSniProxy_ ? proxyUrl.host() : serviceUrl.host();
269-
tlsSocket_->set_verify_callback(ASIO::ssl::rfc2818_verification(urlHost));
277+
tlsSocket_->set_verify_callback(ASIO::ssl::host_name_verification(urlHost));
270278
}
271279

272280
LOG_DEBUG("TLS SNI Host: " << serviceUrl.host());
@@ -394,7 +402,7 @@ typedef ASIO::detail::socket_option::integer<IPPROTO_TCP, TCP_KEEPIDLE> tcp_keep
394402
* if async_connect without any error, connected_ would be set to true
395403
* at this point the connection is deemed valid to be used by clients of this class
396404
*/
397-
void ClientConnection::handleTcpConnected(const ASIO_ERROR& err, tcp::resolver::iterator endpointIterator) {
405+
void ClientConnection::handleTcpConnected(const ASIO_ERROR& err, const tcp::endpoint& endpoint) {
398406
if (!err) {
399407
std::stringstream cnxStringStream;
400408
try {
@@ -479,35 +487,6 @@ void ClientConnection::handleTcpConnected(const ASIO_ERROR& err, tcp::resolver::
479487
} else {
480488
handleHandshake(ASIO_SUCCESS);
481489
}
482-
} else if (endpointIterator != tcp::resolver::iterator()) {
483-
LOG_WARN(cnxString_ << "Failed to establish connection: " << err.message());
484-
// The connection failed. Try the next endpoint in the list.
485-
ASIO_ERROR closeError;
486-
socket_->close(closeError); // ignore the error of close
487-
if (closeError) {
488-
LOG_WARN(cnxString_ << "Failed to close socket: " << err.message());
489-
}
490-
connectTimeoutTask_->stop();
491-
++endpointIterator;
492-
if (endpointIterator != tcp::resolver::iterator()) {
493-
LOG_DEBUG(cnxString_ << "Connecting to " << endpointIterator->endpoint() << "...");
494-
connectTimeoutTask_->start();
495-
tcp::endpoint endpoint = *endpointIterator;
496-
auto weakSelf = weak_from_this();
497-
socket_->async_connect(endpoint, [weakSelf, endpointIterator](const ASIO_ERROR& err) {
498-
auto self = weakSelf.lock();
499-
if (self) {
500-
self->handleTcpConnected(err, endpointIterator);
501-
}
502-
});
503-
} else {
504-
if (err == ASIO::error::operation_aborted) {
505-
// TCP connect timeout, which is not retryable
506-
close();
507-
} else {
508-
close(ResultRetryable);
509-
}
510-
}
511490
} else {
512491
LOG_ERROR(cnxString_ << "Failed to establish connection: " << err.message());
513492
close(ResultRetryable);
@@ -603,18 +582,18 @@ void ClientConnection::tcpConnectAsync() {
603582
}
604583

605584
LOG_DEBUG(cnxString_ << "Resolving " << service_url.host() << ":" << service_url.port());
606-
tcp::resolver::query query(service_url.host(), std::to_string(service_url.port()));
585+
607586
auto weakSelf = weak_from_this();
608-
resolver_->async_resolve(query,
609-
[weakSelf](const ASIO_ERROR& err, const tcp::resolver::iterator& iterator) {
587+
resolver_->async_resolve(service_url.host(), std::to_string(service_url.port()),
588+
[weakSelf](const ASIO_ERROR& err, tcp::resolver::results_type results) {
610589
auto self = weakSelf.lock();
611590
if (self) {
612-
self->handleResolve(err, iterator);
591+
self->handleResolve(err, results);
613592
}
614593
});
615594
}
616595

617-
void ClientConnection::handleResolve(const ASIO_ERROR& err, const tcp::resolver::iterator& endpointIterator) {
596+
void ClientConnection::handleResolve(ASIO_ERROR err, const tcp::resolver::results_type& results) {
618597
if (err) {
619598
std::string hostUrl = isSniProxy_ ? cnxString_ : proxyServiceUrl_;
620599
LOG_ERROR(hostUrl << "Resolve error: " << err << " : " << err.message());
@@ -642,6 +621,15 @@ void ClientConnection::handleResolve(const ASIO_ERROR& err, const tcp::resolver:
642621
ptr->connectTimeoutTask_->stop();
643622
});
644623

624+
ASIO::async_connect(socket_, results, [weakSelf](const ASIO_ERROR& err, const tcp::endpoint& endpoint) {
625+
auto self = weakSelf.lock();
626+
if (self) {
627+
self->handleTcpConnected(err, endpoint);
628+
}
629+
});
630+
631+
// TODO: use the new resolver results API to iterate over endpoints
632+
/*
645633
LOG_DEBUG(cnxString_ << "Connecting to " << endpointIterator->endpoint() << "...");
646634
connectTimeoutTask_->start();
647635
if (endpointIterator != tcp::resolver::iterator()) {
@@ -658,6 +646,7 @@ void ClientConnection::handleResolve(const ASIO_ERROR& err, const tcp::resolver:
658646
close();
659647
return;
660648
}
649+
*/
661650
}
662651

663652
void ClientConnection::readNextCommand() {

lib/ClientConnection.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ class PULSAR_PUBLIC ClientConnection : public std::enable_shared_from_this<Clien
238238
* although not usable at this point, since this is just tcp connection
239239
* Pulsar - Connect/Connected has yet to happen
240240
*/
241-
void handleTcpConnected(const ASIO_ERROR& err, ASIO::ip::tcp::resolver::iterator endpointIterator);
241+
void handleTcpConnected(const ASIO_ERROR& err, const ASIO::ip::tcp::endpoint& endpoint);
242242

243243
void handleHandshake(const ASIO_ERROR& err);
244244

@@ -261,7 +261,7 @@ class PULSAR_PUBLIC ClientConnection : public std::enable_shared_from_this<Clien
261261

262262
void handlePulsarConnected(const proto::CommandConnected& cmdConnected);
263263

264-
void handleResolve(const ASIO_ERROR& err, const ASIO::ip::tcp::resolver::iterator& endpointIterator);
264+
void handleResolve(ASIO_ERROR err, const ASIO::ip::tcp::resolver::results_type& results);
265265

266266
void handleSend(const ASIO_ERROR& err, const SharedBuffer& cmd);
267267
void handleSendPair(const ASIO_ERROR& err);

0 commit comments

Comments
 (0)