SNode.C
Loading...
Searching...
No Matches
core::socket::stream::tls::SocketConnection< PhysicalSocketT, ConfigT > Class Template Referencefinal

#include <SocketConnection.h>

Inheritance diagram for core::socket::stream::tls::SocketConnection< PhysicalSocketT, ConfigT >:
Collaboration diagram for core::socket::stream::tls::SocketConnection< PhysicalSocketT, ConfigT >:

Public Types

using SocketAddress = typename Super::SocketAddress
 

Public Member Functions

 SocketConnection (PhysicalSocket &&physicalSocket, const std::function< void(SocketConnection *)> &onDisconnect, const std::shared_ptr< Config > &config)
 
SSL * getSSL () const
 
- Public Member Functions inherited from core::socket::stream::SocketConnectionT< PhysicalSocketT, core::socket::stream::tls::SocketReader, core::socket::stream::tls::SocketWriter, ConfigT >
 SocketConnectionT ()=delete
 
int getFd () const final
 
void setTimeout (const utils::Timeval &timeout) final
 
const SocketAddressgetLocalAddress () const final
 
const SocketAddressgetRemoteAddress () const final
 
std::size_t readFromPeer (char *chunk, std::size_t chunkLen) final
 
void sendToPeer (const char *chunk, std::size_t chunkLen) final
 
virtual void sendToPeer (const char *chunk, std::size_t chunkLen)=0
 
void sendToPeer (const std::string &data)
 
bool streamToPeer (core::pipe::Source *source) final
 
void streamEof () final
 
void shutdownRead () final
 
void shutdownWrite (bool forceClose) final
 
void close () final
 
ConfiggetConfig () const
 
std::size_t getTotalSent () const override
 
std::size_t getTotalQueued () const override
 
std::size_t getTotalRead () const override
 
std::size_t getTotalProcessed () const override
 
- Public Member Functions inherited from core::socket::stream::SocketConnection
 SocketConnection (int fd, const net::config::ConfigInstance *config)
 
 SocketConnection (const SocketConnection &)=delete
 
void sendToPeer (const std::string &data)
 
void sentToPeer (const std::vector< uint8_t > &data)
 
void sentToPeer (const std::vector< char > &data)
 
const std::string & getInstanceName () const
 
const std::string & getConnectionName () const
 
SocketContextgetSocketContext () const
 
std::string getOnlineSince () const
 
std::string getOnlineDuration () const
 
const net::config::ConfigInstancegetConfig () const
 

Private Types

using Super = core::socket::stream::SocketConnectionT< PhysicalSocketT, core::socket::stream::tls::SocketReader, core::socket::stream::tls::SocketWriter, ConfigT >
 
using Config = ConfigT
 
using PhysicalSocket = PhysicalSocketT
 
using SocketReader = typename Super::SocketReader
 
using SocketWriter = typename Super::SocketWriter
 

Private Member Functions

SSL * startSSL (int fd, SSL_CTX *ctx, const utils::Timeval &sslInitTimeout, const utils::Timeval &sslShutdownTimeout, bool closeNotifyIsEOF)
 
void stopSSL ()
 
bool doSSLHandshake (const std::function< void()> &onSuccess, const std::function< void()> &onTimeout, const std::function< void(int)> &onStatus) final
 
void doSSLShutdown ()
 
void onReadShutdown () final
 
void doWriteShutdown (const std::function< void()> &onShutdown) final
 

Private Attributes

SSL * ssl = nullptr
 
utils::Timeval sslInitTimeout
 
utils::Timeval sslShutdownTimeout
 

Friends

template<typename PhysicalSocket , typename Config >
class SocketAcceptor
 
template<typename PhysicalSocket , typename Config >
class SocketConnector
 

Additional Inherited Members

- Protected Types inherited from core::socket::stream::SocketConnectionT< PhysicalSocketT, core::socket::stream::tls::SocketReader, core::socket::stream::tls::SocketWriter, ConfigT >
using Super = core::socket::stream::SocketConnection
 
using Config = ConfigT
 
using PhysicalSocket = PhysicalSocketT
 
using SocketReader = core::socket::stream::tls::SocketReader
 
using SocketWriter = core::socket::stream::tls::SocketWriter
 
using SocketAddress = typename PhysicalSocket::SocketAddress
 
- Protected Member Functions inherited from core::socket::stream::SocketConnectionT< PhysicalSocketT, core::socket::stream::tls::SocketReader, core::socket::stream::tls::SocketWriter, ConfigT >
 SocketConnectionT (PhysicalSocket &&physicalSocket, const std::function< void()> &onDisconnectm, const std::shared_ptr< Config > &config)
 
 ~SocketConnectionT () override
 
void doWriteShutdown (const std::function< void()> &onShutdown) override
 
void onWriteError (int errnum)
 
void onReadError (int errnum)
 
- Protected Member Functions inherited from core::socket::stream::SocketConnection
virtual ~SocketConnection ()
 
void setSocketContext (SocketContext *socketContext)
 
void connectSocketContext (const std::shared_ptr< SocketContextFactory > &socketContextFactory)
 
- Protected Member Functions inherited from core::socket::stream::SocketReader
 SocketReader (const std::string &instanceName, const std::function< void(int)> &onStatus, const utils::Timeval &timeout, std::size_t blockSize, const utils::Timeval &terminateTimeout)
 
std::size_t getTotalRead () const
 
std::size_t getTotalProcessed () const
 
void setBlockSize (std::size_t readBlockSize)
 
std::size_t readFromPeer (char *chunk, std::size_t chunkLen)
 
void shutdownRead ()
 
 SocketReader ()=delete
 
- Protected Member Functions inherited from core::eventreceiver::ReadEventReceiver
 ReadEventReceiver (const std::string &name, const utils::Timeval &timeout)
 
- Protected Member Functions inherited from core::DescriptorEventReceiver
bool enable (int fd)
 
void disable ()
 
void suspend ()
 
void resume ()
 
 DescriptorEventReceiver (const std::string &name, DescriptorEventPublisher &descriptorEventPublisher, const utils::Timeval &timeout=TIMEOUT::DISABLE)
 
int getRegisteredFd () const
 
bool isEnabled () const
 
bool isSuspended () const
 
void setTimeout (const utils::Timeval &timeout)
 
utils::Timeval getTimeout (const utils::Timeval &currentTime) const
 
void checkTimeout (const utils::Timeval &currentTime)
 
- Protected Member Functions inherited from core::Observer
void observed ()
 
void unObserved ()
 
 Observer ()=default
 
 Observer (Observer &)=delete
 
 Observer (Observer &&)=delete
 
virtual ~Observer ()
 
- Protected Member Functions inherited from core::EventReceiver
virtual ~EventReceiver ()=default
 
 EventReceiver (const std::string &name)
 
 EventReceiver (EventReceiver &)=delete
 
 EventReceiver (EventReceiver &&)=delete
 
EventReceiveroperator= (EventReceiver &)=delete
 
EventReceiveroperator= (EventReceiver &&)=delete
 
virtual void destruct ()
 
void span ()
 
void relax ()
 
const std::string & getName () const
 
- Protected Member Functions inherited from core::socket::stream::SocketWriter
 SocketWriter (const std::string &instanceName, const std::function< void(int)> &onStatus, const utils::Timeval &timeout, std::size_t blockSize, const utils::Timeval &terminateTimeout)
 
std::size_t getTotalSent () const
 
std::size_t getTotalQueued () const
 
void setBlockSize (std::size_t writeBlockSize)
 
void sendToPeer (const char *chunk, std::size_t chunkLen)
 
bool streamToPeer (core::pipe::Source *source)
 
void streamEof ()
 
void shutdownWrite (const std::function< void()> &onShutdown)
 
 SocketWriter ()=delete
 
- Protected Member Functions inherited from core::eventreceiver::WriteEventReceiver
 WriteEventReceiver (const std::string &name, const utils::Timeval &timeout)
 
- Static Protected Member Functions inherited from core::EventReceiver
static void atNextTick (const std::function< void(void)> &callBack)
 
- Protected Attributes inherited from core::socket::stream::SocketConnection
core::socket::stream::SocketContextsocketContext = nullptr
 
std::string instanceName
 
std::string connectionName
 
std::chrono::time_point< std::chrono::system_clock > onlineSinceTimePoint
 
- Protected Attributes inherited from core::socket::stream::tls::SocketReader
SSL * ssl = nullptr
 
bool closeNotifyIsEOF = true
 
- Protected Attributes inherited from core::socket::stream::SocketReader
utils::Timeval terminateTimeout
 
- Protected Attributes inherited from core::socket::stream::tls::SocketWriter
SSL * ssl = nullptr
 
bool closeNotifyIsEOF = true
 
- Protected Attributes inherited from core::socket::stream::SocketWriter
bool markShutdown = false
 
std::function< void()> onShutdown
 
std::vector< char > writePuffer
 
bool shutdownInProgress = false
 
utils::Timeval terminateTimeout
 

Detailed Description

template<typename PhysicalSocketT, typename ConfigT>
class core::socket::stream::tls::SocketConnection< PhysicalSocketT, ConfigT >

Definition at line 60 of file SocketConnection.h.

Member Typedef Documentation

◆ Config

template<typename PhysicalSocketT , typename ConfigT >
using core::socket::stream::tls::SocketConnection< PhysicalSocketT, ConfigT >::Config = ConfigT
private

Definition at line 69 of file SocketConnection.h.

◆ PhysicalSocket

template<typename PhysicalSocketT , typename ConfigT >
using core::socket::stream::tls::SocketConnection< PhysicalSocketT, ConfigT >::PhysicalSocket = PhysicalSocketT
private

Definition at line 70 of file SocketConnection.h.

◆ SocketAddress

template<typename PhysicalSocketT , typename ConfigT >
using core::socket::stream::tls::SocketConnection< PhysicalSocketT, ConfigT >::SocketAddress = typename Super::SocketAddress

Definition at line 75 of file SocketConnection.h.

◆ SocketReader

template<typename PhysicalSocketT , typename ConfigT >
using core::socket::stream::tls::SocketConnection< PhysicalSocketT, ConfigT >::SocketReader = typename Super::SocketReader
private

Definition at line 71 of file SocketConnection.h.

◆ SocketWriter

template<typename PhysicalSocketT , typename ConfigT >
using core::socket::stream::tls::SocketConnection< PhysicalSocketT, ConfigT >::SocketWriter = typename Super::SocketWriter
private

Definition at line 72 of file SocketConnection.h.

◆ Super

template<typename PhysicalSocketT , typename ConfigT >
using core::socket::stream::tls::SocketConnection< PhysicalSocketT, ConfigT >::Super = core::socket::stream:: SocketConnectionT<PhysicalSocketT, core::socket::stream::tls::SocketReader, core::socket::stream::tls::SocketWriter, ConfigT>
private

Definition at line 66 of file SocketConnection.h.

Constructor & Destructor Documentation

◆ SocketConnection()

template<typename PhysicalSocket , typename Config >
core::socket::stream::tls::SocketConnection< PhysicalSocket, Config >::SocketConnection ( PhysicalSocket &&  physicalSocket,
const std::function< void(SocketConnection< PhysicalSocketT, ConfigT > *)> &  onDisconnect,
const std::shared_ptr< Config > &  config 
)

Member Function Documentation

◆ doSSLHandshake()

template<typename PhysicalSocket , typename Config >
bool core::socket::stream::tls::SocketConnection< PhysicalSocket, Config >::doSSLHandshake ( const std::function< void()> &  onSuccess,
const std::function< void()> &  onTimeout,
const std::function< void(int)> &  onStatus 
)
finalprivatevirtual

Implements core::socket::stream::tls::SocketReader.

Definition at line 115 of file SocketConnection.hpp.

117 {
118 if (ssl != nullptr) {
121 }
124 }
125
128 ssl,
129 [onSuccess, this]() { // onSuccess
131 onSuccess();
132 },
133 [onTimeout]() { // onTimeout
134 onTimeout();
135 },
136 [onStatus](int sslErr) { // onStatus
137 onStatus(sslErr);
138 },
140 }
141
142 return ssl != nullptr;
143 }
const std::string & getConnectionName() const
std::function< void(int)> onStatus
static void doHandshake(const std::string &instanceName, SSL *ssl, const std::function< void(void)> &onSuccess, const std::function< void(void)> &onTimeout, const std::function< void(int)> &onStatus, const utils::Timeval &timeout)

References core::socket::stream::tls::TLSHandshake::doHandshake(), core::socket::stream::tls::SocketConnection< PhysicalSocketT, ConfigT >::ssl, and core::socket::stream::tls::SocketConnection< PhysicalSocketT, ConfigT >::sslInitTimeout.

Here is the call graph for this function:

◆ doSSLShutdown()

template<typename PhysicalSocket , typename Config >
void core::socket::stream::tls::SocketConnection< PhysicalSocket, Config >::doSSLShutdown ( )
private

Definition at line 146 of file SocketConnection.hpp.

146 {
147 bool resumeSocketReader = false;
148 bool resumeSocketWriter = false;
149
152 resumeSocketReader = true;
153 }
154
157 resumeSocketWriter = true;
158 }
159
162 ssl,
163 [this, resumeSocketReader, resumeSocketWriter]() { // onSuccess
164 if (resumeSocketReader) {
166 }
167 if (resumeSocketWriter) {
169 }
170 if (SSL_get_shutdown(ssl) == (SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN)) {
171 LOG(DEBUG) << Super::getConnectionName() << " SSL/TLS: Close_notify received and sent";
172 } else {
173 LOG(DEBUG) << Super::getConnectionName() << " SSL/TLS: Close_notify sent";
174
175 if (SSL_get_shutdown(ssl) == SSL_SENT_SHUTDOWN && SocketWriter::closeNotifyIsEOF) {
176 LOG(TRACE) << Super::getConnectionName() << " SSL/TLS: Close_notify is EOF: setting sslShutdownTimeout to "
177 << sslShutdownTimeout << " sec";
179 }
180 }
181 },
182 [this, resumeSocketReader, resumeSocketWriter]() { // onTimeout
183 if (resumeSocketReader) {
185 }
186 if (resumeSocketWriter) {
188 }
189 LOG(ERROR) << Super::getConnectionName() << " SSL/TLS: Shutdown handshake timed out";
190 Super::doWriteShutdown([this]() {
192 });
193 },
194 [this, resumeSocketReader, resumeSocketWriter](int sslErr) { // onStatus
195 if (resumeSocketReader) {
197 }
198 if (resumeSocketWriter) {
200 }
201 ssl_log(Super::getConnectionName() + " SSL/TLS: Shutdown handshake failed", sslErr);
202 Super::doWriteShutdown([this]() {
204 });
205 },
207 }
static void doShutdown(const std::string &instanceName, SSL *ssl, const std::function< void(void)> &onSuccess, const std::function< void(void)> &onTimeout, const std::function< void(int)> &onStatus, const utils::Timeval &timeout)
void ssl_log(const std::string &message, int sslErr)

References core::socket::stream::tls::TLSShutdown::doShutdown(), core::socket::stream::tls::SocketConnection< PhysicalSocketT, ConfigT >::ssl, and core::socket::stream::tls::SocketConnection< PhysicalSocketT, ConfigT >::sslShutdownTimeout.

Referenced by core::socket::stream::tls::SocketConnection< PhysicalSocketT, ConfigT >::doWriteShutdown(), and core::socket::stream::tls::SocketConnection< PhysicalSocketT, ConfigT >::onReadShutdown().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ doWriteShutdown()

template<typename PhysicalSocket , typename Config >
void core::socket::stream::tls::SocketConnection< PhysicalSocket, Config >::doWriteShutdown ( const std::function< void()> &  onShutdown)
finalprivatevirtual

Implements core::socket::stream::SocketWriter.

Definition at line 230 of file SocketConnection.hpp.

230 {
231 if ((SSL_get_shutdown(ssl) & SSL_SENT_SHUTDOWN) == 0) {
232 LOG(DEBUG) << Super::getConnectionName() << " SSL/TLS: Send close_notify";
233
235 } else {
237 }
238 }
std::function< void()> onShutdown

References core::socket::stream::tls::SocketConnection< PhysicalSocketT, ConfigT >::doSSLShutdown(), and core::socket::stream::tls::SocketConnection< PhysicalSocketT, ConfigT >::ssl.

Here is the call graph for this function:

◆ getSSL()

template<typename PhysicalSocket , typename Config >
SSL * core::socket::stream::tls::SocketConnection< PhysicalSocket, Config >::getSSL ( ) const

Definition at line 73 of file SocketConnection.hpp.

73 {
74 return ssl;
75 }

References core::socket::stream::tls::SocketConnection< PhysicalSocketT, ConfigT >::ssl.

Referenced by tls::getClient().

Here is the caller graph for this function:

◆ onReadShutdown()

template<typename PhysicalSocket , typename Config >
void core::socket::stream::tls::SocketConnection< PhysicalSocket, Config >::onReadShutdown ( )
finalprivatevirtual

Implements core::socket::stream::tls::SocketReader.

Definition at line 210 of file SocketConnection.hpp.

210 {
211 if ((SSL_get_shutdown(ssl) & SSL_RECEIVED_SHUTDOWN) != 0) {
212 if ((SSL_get_shutdown(ssl) & SSL_SENT_SHUTDOWN) != 0) {
213 LOG(DEBUG) << Super::getConnectionName() << " SSL/TLS: Close_notify sent and received";
214
216 } else {
217 LOG(DEBUG) << Super::getConnectionName() << " SSL/TLS: Close_notify received";
218
220 }
221 } else {
222 LOG(ERROR) << Super::getConnectionName() << " SSL/TLS: Unexpected EOF error";
223
225 SSL_set_shutdown(ssl, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
226 }
227 }

References core::socket::stream::tls::SocketConnection< PhysicalSocketT, ConfigT >::doSSLShutdown(), and core::socket::stream::tls::SocketConnection< PhysicalSocketT, ConfigT >::ssl.

Here is the call graph for this function:

◆ startSSL()

template<typename PhysicalSocket , typename Config >
SSL * core::socket::stream::tls::SocketConnection< PhysicalSocket, Config >::startSSL ( int  fd,
SSL_CTX *  ctx,
const utils::Timeval sslInitTimeout,
const utils::Timeval sslShutdownTimeout,
bool  closeNotifyIsEOF 
)
private

Definition at line 78 of file SocketConnection.hpp.

79 {
80 this->sslInitTimeout = sslInitTimeout;
81 this->sslShutdownTimeout = sslShutdownTimeout;
82 if (ctx != nullptr) {
83 ssl = SSL_new(ctx);
84
85 if (ssl != nullptr) {
86 SSL_set_ex_data(ssl, 0, const_cast<std::string*>(&Super::getConnectionName()));
87
88 if (SSL_set_fd(ssl, fd) == 1) {
93 } else {
94 SSL_free(ssl);
95 ssl = nullptr;
96 }
97 }
98 }
99
100 return ssl;
101 }

References utils::Timeval::operator=(), core::socket::stream::tls::SocketConnection< PhysicalSocketT, ConfigT >::ssl, core::socket::stream::tls::SocketConnection< PhysicalSocketT, ConfigT >::sslInitTimeout, and core::socket::stream::tls::SocketConnection< PhysicalSocketT, ConfigT >::sslShutdownTimeout.

Here is the call graph for this function:

◆ stopSSL()

template<typename PhysicalSocket , typename Config >
void core::socket::stream::tls::SocketConnection< PhysicalSocket, Config >::stopSSL ( )
private

Definition at line 104 of file SocketConnection.hpp.

104 {
105 if (ssl != nullptr) {
106 SSL_free(ssl);
107
108 ssl = nullptr;
109 SocketReader::ssl = nullptr;
110 SocketWriter::ssl = nullptr;
111 }
112 }

References core::socket::stream::tls::SocketConnection< PhysicalSocketT, ConfigT >::ssl.

Friends And Related Symbol Documentation

◆ SocketAcceptor

template<typename PhysicalSocketT , typename ConfigT >
template<typename PhysicalSocket , typename Config >
friend class SocketAcceptor
friend

Definition at line 105 of file SocketConnection.h.

◆ SocketConnector

template<typename PhysicalSocketT , typename ConfigT >
template<typename PhysicalSocket , typename Config >
friend class SocketConnector
friend

Definition at line 108 of file SocketConnection.h.

Member Data Documentation

◆ ssl

◆ sslInitTimeout

◆ sslShutdownTimeout

template<typename PhysicalSocketT , typename ConfigT >
utils::Timeval core::socket::stream::tls::SocketConnection< PhysicalSocketT, ConfigT >::sslShutdownTimeout
private

The documentation for this class was generated from the following files: