lib/net: Pass connection security level to within socket classes

This commit is contained in:
Povilas Kanapickas 2021-11-01 04:50:13 +02:00
parent 5c7d7194d5
commit 57769cffda
5 changed files with 31 additions and 28 deletions

View File

@ -25,11 +25,11 @@
#include "common/DataDirectories.h" #include "common/DataDirectories.h"
#include "base/String.h" #include "base/String.h"
SecureListenSocket::SecureListenSocket( SecureListenSocket::SecureListenSocket(IEventQueue* events, SocketMultiplexer* socketMultiplexer,
IEventQueue* events, IArchNetwork::EAddressFamily family,
SocketMultiplexer* socketMultiplexer, ConnectionSecurityLevel security_level) :
IArchNetwork::EAddressFamily family) : TCPListenSocket(events, socketMultiplexer, family),
TCPListenSocket(events, socketMultiplexer, family) security_level_{security_level}
{ {
} }
@ -38,10 +38,8 @@ SecureListenSocket::accept()
{ {
SecureSocket* socket = NULL; SecureSocket* socket = NULL;
try { try {
socket = new SecureSocket( socket = new SecureSocket(m_events, m_socketMultiplexer,
m_events, ARCH->acceptSocket(m_socket, NULL), security_level_);
m_socketMultiplexer,
ARCH->acceptSocket(m_socket, NULL));
socket->initSsl(true); socket->initSsl(true);
if (socket != NULL) { if (socket != NULL) {

View File

@ -19,6 +19,7 @@
#include "net/TCPListenSocket.h" #include "net/TCPListenSocket.h"
#include "common/stdset.h" #include "common/stdset.h"
#include "ConnectionSecurityLevel.h"
class IEventQueue; class IEventQueue;
class SocketMultiplexer; class SocketMultiplexer;
@ -26,11 +27,13 @@ class IDataSocket;
class SecureListenSocket : public TCPListenSocket { class SecureListenSocket : public TCPListenSocket {
public: public:
SecureListenSocket(IEventQueue* events, SecureListenSocket(IEventQueue* events, SocketMultiplexer* socketMultiplexer,
SocketMultiplexer* socketMultiplexer, IArchNetwork::EAddressFamily family,
IArchNetwork::EAddressFamily family); ConnectionSecurityLevel security_level);
// IListenSocket overrides // IListenSocket overrides
virtual IDataSocket* virtual IDataSocket*
accept(); accept();
private:
ConnectionSecurityLevel security_level_;
}; };

View File

@ -54,25 +54,24 @@ struct Ssl {
SSL* m_ssl; SSL* m_ssl;
}; };
SecureSocket::SecureSocket( SecureSocket::SecureSocket(IEventQueue* events, SocketMultiplexer* socketMultiplexer,
IEventQueue* events, IArchNetwork::EAddressFamily family,
SocketMultiplexer* socketMultiplexer, ConnectionSecurityLevel security_level) :
IArchNetwork::EAddressFamily family) :
TCPSocket(events, socketMultiplexer, family), TCPSocket(events, socketMultiplexer, family),
m_ssl(nullptr), m_ssl(nullptr),
m_secureReady(false), m_secureReady(false),
m_fatal(false) m_fatal(false),
security_level_{security_level}
{ {
} }
SecureSocket::SecureSocket( SecureSocket::SecureSocket(IEventQueue* events, SocketMultiplexer* socketMultiplexer,
IEventQueue* events, ArchSocket socket, ConnectionSecurityLevel security_level) :
SocketMultiplexer* socketMultiplexer,
ArchSocket socket) :
TCPSocket(events, socketMultiplexer, socket), TCPSocket(events, socketMultiplexer, socket),
m_ssl(nullptr), m_ssl(nullptr),
m_secureReady(false), m_secureReady(false),
m_fatal(false) m_fatal(false),
security_level_{security_level}
{ {
} }

View File

@ -17,6 +17,7 @@
#pragma once #pragma once
#include "ConnectionSecurityLevel.h"
#include "net/TCPSocket.h" #include "net/TCPSocket.h"
#include "net/XSocket.h" #include "net/XSocket.h"
#include "io/filesystem.h" #include "io/filesystem.h"
@ -33,10 +34,10 @@ A secure socket using SSL.
*/ */
class SecureSocket : public TCPSocket { class SecureSocket : public TCPSocket {
public: public:
SecureSocket(IEventQueue* events, SocketMultiplexer* socketMultiplexer, IArchNetwork::EAddressFamily family); SecureSocket(IEventQueue* events, SocketMultiplexer* socketMultiplexer,
SecureSocket(IEventQueue* events, IArchNetwork::EAddressFamily family, ConnectionSecurityLevel security_level);
SocketMultiplexer* socketMultiplexer, SecureSocket(IEventQueue* events, SocketMultiplexer* socketMultiplexer,
ArchSocket socket); ArchSocket socket, ConnectionSecurityLevel security_level);
~SecureSocket(); ~SecureSocket();
// ISocket overrides // ISocket overrides
@ -86,4 +87,5 @@ private:
Ssl* m_ssl; Ssl* m_ssl;
bool m_secureReady; bool m_secureReady;
bool m_fatal; bool m_fatal;
ConnectionSecurityLevel security_level_ = ConnectionSecurityLevel::ENCRYPTED;
}; };

View File

@ -44,7 +44,8 @@ IDataSocket* TCPSocketFactory::create(IArchNetwork::EAddressFamily family,
ConnectionSecurityLevel security_level) const ConnectionSecurityLevel security_level) const
{ {
if (security_level != ConnectionSecurityLevel::PLAINTEXT) { if (security_level != ConnectionSecurityLevel::PLAINTEXT) {
SecureSocket* secureSocket = new SecureSocket(m_events, m_socketMultiplexer, family); SecureSocket* secureSocket = new SecureSocket(m_events, m_socketMultiplexer, family,
security_level);
secureSocket->initSsl (false); secureSocket->initSsl (false);
return secureSocket; return secureSocket;
} }
@ -58,7 +59,7 @@ IListenSocket* TCPSocketFactory::createListen(IArchNetwork::EAddressFamily famil
{ {
IListenSocket* socket = NULL; IListenSocket* socket = NULL;
if (security_level != ConnectionSecurityLevel::PLAINTEXT) { if (security_level != ConnectionSecurityLevel::PLAINTEXT) {
socket = new SecureListenSocket(m_events, m_socketMultiplexer, family); socket = new SecureListenSocket(m_events, m_socketMultiplexer, family, security_level);
} }
else { else {
socket = new TCPListenSocket(m_events, m_socketMultiplexer, family); socket = new TCPListenSocket(m_events, m_socketMultiplexer, family);