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*
ServerApp::openClientListener(const NetworkAddress& address)
{
auto security_level = ConnectionSecurityLevel::PLAINTEXT;
if (args().m_enableCrypto) {
security_level = ConnectionSecurityLevel::ENCRYPTED;
}
ClientListener* listen = new ClientListener(
address,
new TCPSocketFactory(m_events, getSocketMultiplexer()),
m_events,
args().m_enableCrypto);
m_events, security_level);
m_events->adoptHandler(
m_events->forClientListener().connected(), listen,

View File

@ -127,6 +127,11 @@ Client::connect()
return;
}
auto security_level = ConnectionSecurityLevel::PLAINTEXT;
if (m_useSecureNetwork) {
security_level = ConnectionSecurityLevel::ENCRYPTED;
}
try {
// resolve the server hostname. do this every time we connect
// in case we couldn't resolve the address earlier or the address
@ -145,9 +150,8 @@ Client::connect()
}
// create the socket
IDataSocket* socket = m_socketFactory->create(
ARCH->getAddrFamily(m_serverAddress.getAddress()),
m_useSecureNetwork);
IDataSocket* socket = m_socketFactory->create(ARCH->getAddrFamily(m_serverAddress.getAddress()),
security_level);
m_socket = dynamic_cast<TCPSocket*>(socket);
// 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 "arch/IArchNetwork.h"
#include "net/ConnectionSecurityLevel.h"
class IDataSocket;
class IListenSocket;
@ -35,14 +36,12 @@ public:
//@{
//! Create data socket
virtual IDataSocket* create(
IArchNetwork::EAddressFamily family,
bool secure) const = 0;
virtual IDataSocket* create(IArchNetwork::EAddressFamily family,
ConnectionSecurityLevel security_level) const = 0;
//! Create listen socket
virtual IListenSocket* createListen(
IArchNetwork::EAddressFamily family,
bool secure) const = 0;
virtual IListenSocket* createListen(IArchNetwork::EAddressFamily family,
ConnectionSecurityLevel security_level) const = 0;
//@}
};

View File

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

View File

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

View File

@ -36,18 +36,17 @@
ClientListener::ClientListener(const NetworkAddress& address,
ISocketFactory* socketFactory,
IEventQueue* events,
bool enableCrypto) :
ConnectionSecurityLevel security_level) :
m_socketFactory(socketFactory),
m_server(NULL),
m_events(events),
m_useSecureNetwork(enableCrypto)
security_level_{security_level}
{
assert(m_socketFactory != NULL);
try {
m_listen = m_socketFactory->createListen(
ARCH->getAddrFamily(address.getAddress()),
m_useSecureNetwork);
m_listen = m_socketFactory->createListen(ARCH->getAddrFamily(address.getAddress()),
security_level);
// setup event handler
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
// 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(),
socket->getEventTarget()));
}

View File

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

View File

@ -115,7 +115,8 @@ TEST_F(NetworkTests, sendToClient_mockData)
// server
SocketMultiplexer 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<MockPrimaryClient> primaryClient;
NiceMock<MockConfig> serverConfig;
@ -173,7 +174,8 @@ TEST_F(NetworkTests, sendToClient_mockFile)
// server
SocketMultiplexer 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<MockPrimaryClient> primaryClient;
NiceMock<MockConfig> serverConfig;
@ -230,7 +232,8 @@ TEST_F(NetworkTests, sendToServer_mockData)
// server
SocketMultiplexer 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<MockPrimaryClient> primaryClient;
NiceMock<MockConfig> serverConfig;
@ -287,7 +290,8 @@ TEST_F(NetworkTests, sendToServer_mockFile)
// server
SocketMultiplexer 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<MockPrimaryClient> primaryClient;
NiceMock<MockConfig> serverConfig;