If hostname resolution fails, do not try all possible ports

This commit is contained in:
Florent Le Coz
2015-07-09 15:30:24 +02:00
parent ac6a74595c
commit ece4b4969b
3 changed files with 13 additions and 3 deletions
+6 -3
View File
@@ -40,15 +40,16 @@ TCPSocketHandler::TCPSocketHandler(std::shared_ptr<Poller> poller):
SocketHandler(poller, -1), SocketHandler(poller, -1),
use_tls(false), use_tls(false),
connected(false), connected(false),
connecting(false) connecting(false),
#ifdef CARES_FOUND #ifdef CARES_FOUND
,resolving(false), resolving(false),
resolved(false), resolved(false),
resolved4(false), resolved4(false),
resolved6(false), resolved6(false),
cares_addrinfo(nullptr), cares_addrinfo(nullptr),
cares_error() cares_error(),
#endif #endif
hostname_resolution_failed(false)
{} {}
TCPSocketHandler::~TCPSocketHandler() TCPSocketHandler::~TCPSocketHandler()
@@ -109,6 +110,7 @@ void TCPSocketHandler::connect(const std::string& address, const std::string& po
addr_res = this->cares_addrinfo; addr_res = this->cares_addrinfo;
if (!addr_res) if (!addr_res)
{ {
this->hostname_resolution_failed = true;
const auto msg = this->cares_error; const auto msg = this->cares_error;
this->close(); this->close();
this->on_connection_failed(msg); this->on_connection_failed(msg);
@@ -129,6 +131,7 @@ void TCPSocketHandler::connect(const std::string& address, const std::string& po
if (res != 0) if (res != 0)
{ {
log_warning("getaddrinfo failed: "s + gai_strerror(res)); log_warning("getaddrinfo failed: "s + gai_strerror(res));
this->hostname_resolution_failed = true;
this->close(); this->close();
this->on_connection_failed(gai_strerror(res)); this->on_connection_failed(gai_strerror(res));
return ; return ;
@@ -256,6 +256,8 @@ protected:
std::string cares_error; std::string cares_error;
#endif // CARES_FOUND #endif // CARES_FOUND
bool hostname_resolution_failed;
private: private:
TCPSocketHandler(const TCPSocketHandler&) = delete; TCPSocketHandler(const TCPSocketHandler&) = delete;
TCPSocketHandler(TCPSocketHandler&&) = delete; TCPSocketHandler(TCPSocketHandler&&) = delete;
+5
View File
@@ -72,6 +72,11 @@ void IrcClient::on_connection_failed(const std::string& reason)
{ {
this->bridge->send_xmpp_message(this->hostname, "", this->bridge->send_xmpp_message(this->hostname, "",
"Connection failed: "s + reason); "Connection failed: "s + reason);
if (this->hostname_resolution_failed)
while (!this->ports_to_try.empty())
this->ports_to_try.pop();
if (this->ports_to_try.empty()) if (this->ports_to_try.empty())
{ {
// Send an error message for all room that the user wanted to join // Send an error message for all room that the user wanted to join