From c000b886c2955f90b502b7cb62c2f261d0a18650 Mon Sep 17 00:00:00 2001 From: SerhiiGadzhilov <71632867+SerhiiGadzhilov@users.noreply.github.com> Date: Mon, 24 May 2021 11:21:32 +0300 Subject: [PATCH] SYNERGY-1003 Connection processing for name with several ips (#7013) * SYNERGY-1003 Connection processing for name with several ips * Update ChangeLog * SYNERGY-1003 Fix compilation * SYNERGY-1003 Fix code smells --- ChangeLog | 1 + src/lib/base/EventTypes.cpp | 1 + src/lib/base/EventTypes.h | 19 ++++++++------- src/lib/client/Client.cpp | 32 ++++++++++++++++++++----- src/lib/client/Client.h | 8 +++++++ src/lib/client/ServerProxy.cpp | 8 +++---- src/lib/synergy/ClientApp.cpp | 43 ++++++++++++++++++++++------------ src/lib/synergy/ClientApp.h | 1 + 8 files changed, 80 insertions(+), 33 deletions(-) diff --git a/ChangeLog b/ChangeLog index ffc49de51..43ae93435 100644 --- a/ChangeLog +++ b/ChangeLog @@ -19,6 +19,7 @@ Bug fixes: - #7009 Clipboard sharing doesn't work on Linux system - #7010 Fix Ubuntu 16.04 pipeline in Azure - #7011 Fix language synchronisation checkbox spelling +- #7013 Wrong connection pop up appears if the user connects using a domain name which is assigned to list of IP's Enhancements: - #6954 Move language selection to advanced section diff --git a/src/lib/base/EventTypes.cpp b/src/lib/base/EventTypes.cpp index 6f1370fe3..7551939c8 100644 --- a/src/lib/base/EventTypes.cpp +++ b/src/lib/base/EventTypes.cpp @@ -45,6 +45,7 @@ EventTypes::setEvents(IEventQueue* events) REGISTER_EVENT(Client, connected) REGISTER_EVENT(Client, connectionFailed) +REGISTER_EVENT(Client, connectionRefused) REGISTER_EVENT(Client, disconnected) // diff --git a/src/lib/base/EventTypes.h b/src/lib/base/EventTypes.h index 1d0b61e8f..e9de70164 100644 --- a/src/lib/base/EventTypes.h +++ b/src/lib/base/EventTypes.h @@ -42,11 +42,6 @@ type_##Events::name_() \ class ClientEvents : public EventTypes { public: - ClientEvents() : - m_connected(Event::kUnknown), - m_connectionFailed(Event::kUnknown), - m_disconnected(Event::kUnknown) { } - //! @name accessors //@{ @@ -64,6 +59,13 @@ public: */ Event::Type connectionFailed(); + //! Get connection refused event type + /*! + Returns the connection refused event type. This is sent when the + server refuses for some reason. The event data is a FailInfo*. + */ + Event::Type connectionRefused(); + //! Get disconnected event type /*! Returns the disconnected event type. This is sent when the client @@ -75,9 +77,10 @@ public: //@} private: - Event::Type m_connected; - Event::Type m_connectionFailed; - Event::Type m_disconnected; + Event::Type m_connected = Event::kUnknown; + Event::Type m_connectionFailed = Event::kUnknown; + Event::Type m_connectionRefused = Event::kUnknown; + Event::Type m_disconnected = Event::kUnknown; }; class IStreamEvents : public EventTypes { diff --git a/src/lib/client/Client.cpp b/src/lib/client/Client.cpp index 69ebff7bc..a3bcff046 100644 --- a/src/lib/client/Client.cpp +++ b/src/lib/client/Client.cpp @@ -176,12 +176,9 @@ Client::connect(size_t addressIndex) void Client::disconnect(const char* msg) { - m_connectOnResume = false; - cleanupTimer(); - cleanupScreen(); - cleanupConnecting(); - cleanupConnection(); - if (msg != NULL) { + cleanup(); + + if (msg) { sendConnectionFailedEvent(msg); } else { @@ -189,6 +186,19 @@ Client::disconnect(const char* msg) } } +void +Client::refuseConnection(const char* msg) +{ + cleanup(); + + if (msg) { + auto info = new FailInfo(msg); + info->m_retry = true; + Event event(m_events->forClient().connectionRefused(), getEventTarget(), info, Event::kDontFreeData); + m_events->addEvent(event); + } +} + void Client::handshakeComplete() { @@ -540,6 +550,16 @@ Client::setupTimer() &Client::handleConnectTimeout)); } +void +Client::cleanup() +{ + m_connectOnResume = false; + cleanupTimer(); + cleanupScreen(); + cleanupConnecting(); + cleanupConnection(); +} + void Client::cleanupConnecting() { diff --git a/src/lib/client/Client.h b/src/lib/client/Client.h index 0450560c9..a8bf83772 100644 --- a/src/lib/client/Client.h +++ b/src/lib/client/Client.h @@ -83,6 +83,13 @@ public: */ void disconnect(const char* msg); + //! Refuse connection + /*! + Disconnects from the server with an optional error message. + Unlike disconnect this function doesn't try to use other ip addresses + */ + void refuseConnection(const char* msg); + //! Notify of handshake complete /*! Notifies the client that the connection handshake has completed. @@ -180,6 +187,7 @@ private: void setupConnection(); void setupScreen(); void setupTimer(); + void cleanup(); void cleanupConnecting(); void cleanupConnection(); void cleanupScreen(); diff --git a/src/lib/client/ServerProxy.cpp b/src/lib/client/ServerProxy.cpp index ae5d9fc5f..177baa6d5 100644 --- a/src/lib/client/ServerProxy.cpp +++ b/src/lib/client/ServerProxy.cpp @@ -188,25 +188,25 @@ ServerProxy::parseHandshakeMessage(const UInt8* code) ProtocolUtil::readf(m_stream, kMsgEIncompatible + 4, &major, &minor); LOG((CLOG_ERR "server has incompatible version %d.%d", major, minor)); - m_client->disconnect("server has incompatible version"); + m_client->refuseConnection("server has incompatible version"); return kDisconnect; } else if (memcmp(code, kMsgEBusy, 4) == 0) { LOG((CLOG_ERR "server already has a connected client with name \"%s\"", m_client->getName().c_str())); - m_client->disconnect("server already has a connected client with our name"); + m_client->refuseConnection("server already has a connected client with our name"); return kDisconnect; } else if (memcmp(code, kMsgEUnknown, 4) == 0) { LOG((CLOG_ERR "server refused client with name \"%s\"", m_client->getName().c_str())); - m_client->disconnect("server refused client with our name"); + m_client->refuseConnection("server refused client with our name"); return kDisconnect; } else if (memcmp(code, kMsgEBad, 4) == 0) { LOG((CLOG_ERR "server disconnected due to a protocol error")); - m_client->disconnect("server reported a protocol error"); + m_client->refuseConnection("server reported a protocol error"); return kDisconnect; } else { diff --git a/src/lib/synergy/ClientApp.cpp b/src/lib/synergy/ClientApp.cpp index af60d8479..57857c3c5 100644 --- a/src/lib/synergy/ClientApp.cpp +++ b/src/lib/synergy/ClientApp.cpp @@ -57,6 +57,7 @@ #include "platform/OSXDragSimulator.h" #endif +#include #include #include @@ -301,11 +302,9 @@ ClientApp::handleClientConnected(const Event&, void*) void ClientApp::handleClientFailed(const Event& e, void*) { - Client::FailInfo* info = - static_cast(e.getData()); + if ( (++m_lastServerAddressIndex) < m_client->getLastResolvedAddressesCount()) { + std::unique_ptr info(static_cast(e.getData())); - if (m_lastServerAddressIndex + 1 < m_client->getLastResolvedAddressesCount()) { - m_lastServerAddressIndex++; updateStatus(String("Failed to connect to server: ") + info->m_what + " Trying next address..."); LOG((CLOG_NOTE "Failed to connect to server: %s. Trying next address...", info->m_what.c_str())); if (!m_suspended) { @@ -314,19 +313,27 @@ ClientApp::handleClientFailed(const Event& e, void*) } else { m_lastServerAddressIndex = 0; - updateStatus(String("Failed to connect to server: ") + info->m_what); - if (!args().m_restartable || !info->m_retry) { - LOG((CLOG_ERR "failed to connect to server: %s", info->m_what.c_str())); - m_events->addEvent(Event(Event::kQuit)); - } - else { - LOG((CLOG_WARN "failed to connect to server: %s", info->m_what.c_str())); - if (!m_suspended) { - scheduleClientRestart(nextRestartTimeout()); - } + handleClientRefused(e, nullptr); + } + +} + +void +ClientApp::handleClientRefused(const Event& e, void*) +{ + std::unique_ptr info(static_cast(e.getData())); + + updateStatus(String("Failed to connect to server: ") + info->m_what); + if (!args().m_restartable || !info->m_retry) { + LOG((CLOG_ERR "failed to connect to server: %s", info->m_what.c_str())); + m_events->addEvent(Event(Event::kQuit)); + } + else { + LOG((CLOG_WARN "failed to connect to server: %s", info->m_what.c_str())); + if (!m_suspended) { + scheduleClientRestart(nextRestartTimeout()); } } - delete info; } @@ -366,6 +373,11 @@ ClientApp::openClient(const String& name, const NetworkAddress& address, client->getEventTarget(), new TMethodEventJob(this, &ClientApp::handleClientFailed)); + m_events->adoptHandler( + m_events->forClient().connectionRefused(), + client->getEventTarget(), + new TMethodEventJob(this, &ClientApp::handleClientRefused)); + m_events->adoptHandler( m_events->forClient().disconnected(), client->getEventTarget(), @@ -389,6 +401,7 @@ ClientApp::closeClient(Client* client) m_events->removeHandler(m_events->forClient().connected(), client); m_events->removeHandler(m_events->forClient().connectionFailed(), client); + m_events->removeHandler(m_events->forClient().connectionRefused(), client); m_events->removeHandler(m_events->forClient().disconnected(), client); delete client; } diff --git a/src/lib/synergy/ClientApp.h b/src/lib/synergy/ClientApp.h index 38d2497f8..56bbd3ad7 100644 --- a/src/lib/synergy/ClientApp.h +++ b/src/lib/synergy/ClientApp.h @@ -67,6 +67,7 @@ public: void scheduleClientRestart(double retryTime); void handleClientConnected(const Event&, void*); void handleClientFailed(const Event& e, void*); + void handleClientRefused(const Event& e, void*); void handleClientDisconnected(const Event&, void*); Client* openClient(const String& name, const NetworkAddress& address, synergy::Screen* screen);