lib/net: Use enum for connection security level instead of boolean

This commit is contained in:
Povilas Kanapickas 2021-11-01 04:50:12 +02:00
parent 82b8fa905e
commit 5c7d7194d5
9 changed files with 72 additions and 38 deletions

View File

@ -655,11 +655,15 @@ ServerApp::handleResume(const Event&, void*)
ClientListener* ClientListener*
ServerApp::openClientListener(const NetworkAddress& address) ServerApp::openClientListener(const NetworkAddress& address)
{ {
auto security_level = ConnectionSecurityLevel::PLAINTEXT;
if (args().m_enableCrypto) {
security_level = ConnectionSecurityLevel::ENCRYPTED;
}
ClientListener* listen = new ClientListener( ClientListener* listen = new ClientListener(
address, address,
new TCPSocketFactory(m_events, getSocketMultiplexer()), new TCPSocketFactory(m_events, getSocketMultiplexer()),
m_events, m_events, security_level);
args().m_enableCrypto);
m_events->adoptHandler( m_events->adoptHandler(
m_events->forClientListener().connected(), listen, m_events->forClientListener().connected(), listen,

View File

@ -127,6 +127,11 @@ Client::connect()
return; return;
} }
auto security_level = ConnectionSecurityLevel::PLAINTEXT;
if (m_useSecureNetwork) {
security_level = ConnectionSecurityLevel::ENCRYPTED;
}
try { try {
// resolve the server hostname. do this every time we connect // resolve the server hostname. do this every time we connect
// in case we couldn't resolve the address earlier or the address // in case we couldn't resolve the address earlier or the address
@ -145,9 +150,8 @@ Client::connect()
} }
// create the socket // create the socket
IDataSocket* socket = m_socketFactory->create( IDataSocket* socket = m_socketFactory->create(ARCH->getAddrFamily(m_serverAddress.getAddress()),
ARCH->getAddrFamily(m_serverAddress.getAddress()), security_level);
m_useSecureNetwork);
m_socket = dynamic_cast<TCPSocket*>(socket); m_socket = dynamic_cast<TCPSocket*>(socket);
// filter socket messages, including a packetizing filter // filter socket messages, including a packetizing filter

View File

@ -0,0 +1,26 @@
/*
barrier -- mouse and keyboard sharing utility
Copyright (C) Barrier contributors
This package is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
found in the file LICENSE that should have accompanied this file.
This package is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef BARRIER_LIB_NET_CONNECTION_SECURITY_LEVEL_H
#define BARRIER_LIB_NET_CONNECTION_SECURITY_LEVEL_H
enum class ConnectionSecurityLevel {
PLAINTEXT,
ENCRYPTED,
};
#endif // BARRIER_LIB_NET_CONNECTION_SECURITY_LEVEL_H

View File

@ -20,6 +20,7 @@
#include "common/IInterface.h" #include "common/IInterface.h"
#include "arch/IArchNetwork.h" #include "arch/IArchNetwork.h"
#include "net/ConnectionSecurityLevel.h"
class IDataSocket; class IDataSocket;
class IListenSocket; class IListenSocket;
@ -35,14 +36,12 @@ public:
//@{ //@{
//! Create data socket //! Create data socket
virtual IDataSocket* create( virtual IDataSocket* create(IArchNetwork::EAddressFamily family,
IArchNetwork::EAddressFamily family, ConnectionSecurityLevel security_level) const = 0;
bool secure) const = 0;
//! Create listen socket //! Create listen socket
virtual IListenSocket* createListen( virtual IListenSocket* createListen(IArchNetwork::EAddressFamily family,
IArchNetwork::EAddressFamily family, ConnectionSecurityLevel security_level) const = 0;
bool secure) const = 0;
//@} //@}
}; };

View File

@ -40,10 +40,10 @@ TCPSocketFactory::~TCPSocketFactory()
// do nothing // do nothing
} }
IDataSocket* IDataSocket* TCPSocketFactory::create(IArchNetwork::EAddressFamily family,
TCPSocketFactory::create(IArchNetwork::EAddressFamily family, bool secure) const ConnectionSecurityLevel security_level) const
{ {
if (secure) { if (security_level != ConnectionSecurityLevel::PLAINTEXT) {
SecureSocket* secureSocket = new SecureSocket(m_events, m_socketMultiplexer, family); SecureSocket* secureSocket = new SecureSocket(m_events, m_socketMultiplexer, family);
secureSocket->initSsl (false); secureSocket->initSsl (false);
return secureSocket; return secureSocket;
@ -53,11 +53,11 @@ TCPSocketFactory::create(IArchNetwork::EAddressFamily family, bool secure) const
} }
} }
IListenSocket* IListenSocket* TCPSocketFactory::createListen(IArchNetwork::EAddressFamily family,
TCPSocketFactory::createListen(IArchNetwork::EAddressFamily family, bool secure) const ConnectionSecurityLevel security_level) const
{ {
IListenSocket* socket = NULL; IListenSocket* socket = NULL;
if (secure) { if (security_level != ConnectionSecurityLevel::PLAINTEXT) {
socket = new SecureListenSocket(m_events, m_socketMultiplexer, family); socket = new SecureListenSocket(m_events, m_socketMultiplexer, family);
} }
else { else {

View File

@ -31,12 +31,11 @@ public:
virtual ~TCPSocketFactory(); virtual ~TCPSocketFactory();
// ISocketFactory overrides // ISocketFactory overrides
virtual IDataSocket* create( virtual IDataSocket* create(IArchNetwork::EAddressFamily family,
IArchNetwork::EAddressFamily family, ConnectionSecurityLevel security_level) const;
bool secure) const;
virtual IListenSocket* createListen( virtual IListenSocket* createListen(IArchNetwork::EAddressFamily family,
IArchNetwork::EAddressFamily family, ConnectionSecurityLevel security_level) const;
bool secure) const;
private: private:
IEventQueue* m_events; IEventQueue* m_events;

View File

@ -36,18 +36,17 @@
ClientListener::ClientListener(const NetworkAddress& address, ClientListener::ClientListener(const NetworkAddress& address,
ISocketFactory* socketFactory, ISocketFactory* socketFactory,
IEventQueue* events, IEventQueue* events,
bool enableCrypto) : ConnectionSecurityLevel security_level) :
m_socketFactory(socketFactory), m_socketFactory(socketFactory),
m_server(NULL), m_server(NULL),
m_events(events), m_events(events),
m_useSecureNetwork(enableCrypto) security_level_{security_level}
{ {
assert(m_socketFactory != NULL); assert(m_socketFactory != NULL);
try { try {
m_listen = m_socketFactory->createListen( m_listen = m_socketFactory->createListen(ARCH->getAddrFamily(address.getAddress()),
ARCH->getAddrFamily(address.getAddress()), security_level);
m_useSecureNetwork);
// setup event handler // setup event handler
m_events->adoptHandler(m_events->forIListenSocket().connecting(), m_events->adoptHandler(m_events->forIListenSocket().connecting(),
@ -140,7 +139,7 @@ ClientListener::handleClientConnecting(const Event&, void*)
// When using non SSL, server accepts clients immediately, while SSL // When using non SSL, server accepts clients immediately, while SSL
// has to call secure accept which may require retry // has to call secure accept which may require retry
if (!m_useSecureNetwork) { if (security_level_ == ConnectionSecurityLevel::PLAINTEXT) {
m_events->addEvent(Event(m_events->forClientListener().accepted(), m_events->addEvent(Event(m_events->forClientListener().accepted(),
socket->getEventTarget())); socket->getEventTarget()));
} }

View File

@ -23,6 +23,7 @@
#include "base/Event.h" #include "base/Event.h"
#include "common/stddeque.h" #include "common/stddeque.h"
#include "common/stdset.h" #include "common/stdset.h"
#include "net/ConnectionSecurityLevel.h"
class ClientProxy; class ClientProxy;
class ClientProxyUnknown; class ClientProxyUnknown;
@ -36,10 +37,8 @@ class IDataSocket;
class ClientListener { class ClientListener {
public: public:
// The factories are adopted. // The factories are adopted.
ClientListener(const NetworkAddress&, ClientListener(const NetworkAddress&, ISocketFactory*, IEventQueue* events,
ISocketFactory*, ConnectionSecurityLevel security_level);
IEventQueue* events,
bool enableCrypto);
~ClientListener(); ~ClientListener();
//! @name manipulators //! @name manipulators
@ -86,6 +85,6 @@ private:
WaitingClients m_waitingClients; WaitingClients m_waitingClients;
Server* m_server; Server* m_server;
IEventQueue* m_events; IEventQueue* m_events;
bool m_useSecureNetwork; ConnectionSecurityLevel security_level_;
ClientSockets m_clientSockets; ClientSockets m_clientSockets;
}; };

View File

@ -115,7 +115,8 @@ TEST_F(NetworkTests, sendToClient_mockData)
// server // server
SocketMultiplexer serverSocketMultiplexer; SocketMultiplexer serverSocketMultiplexer;
TCPSocketFactory* serverSocketFactory = new TCPSocketFactory(&m_events, &serverSocketMultiplexer); TCPSocketFactory* serverSocketFactory = new TCPSocketFactory(&m_events, &serverSocketMultiplexer);
ClientListener listener(serverAddress, serverSocketFactory, &m_events, false); ClientListener listener(serverAddress, serverSocketFactory, &m_events,
ConnectionSecurityLevel::PLAINTEXT);
NiceMock<MockScreen> serverScreen; NiceMock<MockScreen> serverScreen;
NiceMock<MockPrimaryClient> primaryClient; NiceMock<MockPrimaryClient> primaryClient;
NiceMock<MockConfig> serverConfig; NiceMock<MockConfig> serverConfig;
@ -173,7 +174,8 @@ TEST_F(NetworkTests, sendToClient_mockFile)
// server // server
SocketMultiplexer serverSocketMultiplexer; SocketMultiplexer serverSocketMultiplexer;
TCPSocketFactory* serverSocketFactory = new TCPSocketFactory(&m_events, &serverSocketMultiplexer); TCPSocketFactory* serverSocketFactory = new TCPSocketFactory(&m_events, &serverSocketMultiplexer);
ClientListener listener(serverAddress, serverSocketFactory, &m_events, false); ClientListener listener(serverAddress, serverSocketFactory, &m_events,
ConnectionSecurityLevel::PLAINTEXT);
NiceMock<MockScreen> serverScreen; NiceMock<MockScreen> serverScreen;
NiceMock<MockPrimaryClient> primaryClient; NiceMock<MockPrimaryClient> primaryClient;
NiceMock<MockConfig> serverConfig; NiceMock<MockConfig> serverConfig;
@ -230,7 +232,8 @@ TEST_F(NetworkTests, sendToServer_mockData)
// server // server
SocketMultiplexer serverSocketMultiplexer; SocketMultiplexer serverSocketMultiplexer;
TCPSocketFactory* serverSocketFactory = new TCPSocketFactory(&m_events, &serverSocketMultiplexer); TCPSocketFactory* serverSocketFactory = new TCPSocketFactory(&m_events, &serverSocketMultiplexer);
ClientListener listener(serverAddress, serverSocketFactory, &m_events, false); ClientListener listener(serverAddress, serverSocketFactory, &m_events,
ConnectionSecurityLevel::PLAINTEXT);
NiceMock<MockScreen> serverScreen; NiceMock<MockScreen> serverScreen;
NiceMock<MockPrimaryClient> primaryClient; NiceMock<MockPrimaryClient> primaryClient;
NiceMock<MockConfig> serverConfig; NiceMock<MockConfig> serverConfig;
@ -287,7 +290,8 @@ TEST_F(NetworkTests, sendToServer_mockFile)
// server // server
SocketMultiplexer serverSocketMultiplexer; SocketMultiplexer serverSocketMultiplexer;
TCPSocketFactory* serverSocketFactory = new TCPSocketFactory(&m_events, &serverSocketMultiplexer); TCPSocketFactory* serverSocketFactory = new TCPSocketFactory(&m_events, &serverSocketMultiplexer);
ClientListener listener(serverAddress, serverSocketFactory, &m_events, false); ClientListener listener(serverAddress, serverSocketFactory, &m_events,
ConnectionSecurityLevel::PLAINTEXT);
NiceMock<MockScreen> serverScreen; NiceMock<MockScreen> serverScreen;
NiceMock<MockPrimaryClient> primaryClient; NiceMock<MockPrimaryClient> primaryClient;
NiceMock<MockConfig> serverConfig; NiceMock<MockConfig> serverConfig;