reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
reTurn::AsyncTlsSocketBase Class Reference

#include <AsyncTlsSocketBase.hxx>

Inheritance diagram for reTurn::AsyncTlsSocketBase:
[legend]

Public Member Functions

 AsyncTlsSocketBase (asio::io_context &ioService, asio::ssl::context &context, bool validateServerCertificateHostname)
 
unsigned int getSocketDescriptor () override
 
asio::error_code bind (const asio::ip::address &address, unsigned short port) override
 Note: The following API's are thread safe and queue the request to be handled by the ioService thread.
 
void connect (const std::string &address, unsigned short port) override
 
void doHandshake (unsigned int timeoutSeconds=0)
 Starts the server side handshake.
 
void transportReceive () override
 
void transportFramedReceive () override
 
void transportSend (const StunTuple &destination, std::vector< asio::const_buffer > &buffers) override
 
void transportClose () override
 
asio::ip::address getSenderEndpointAddress () override
 
unsigned short getSenderEndpointPort () override
 
- Public Member Functions inherited from reTurn::AsyncSocketBase
 AsyncSocketBase (asio::io_context &ioService)
 
virtual ~AsyncSocketBase ()
 
virtual void registerAsyncSocketBaseHandler (AsyncSocketBaseHandler *handler)
 
virtual void send (const StunTuple &destination, const std::shared_ptr< DataBuffer > &data)
 Note: destination is ignored for TCP and TLS connections.
 
virtual void send (const StunTuple &destination, unsigned short channel, const std::shared_ptr< DataBuffer > &data)
 
virtual void receive ()
 Overlapped calls to receive functions have no effect.
 
virtual void framedReceive ()
 
virtual void close ()
 
bool isConnected () const noexcept
 
asio::ip::address & getConnectedAddress () noexcept
 
unsigned short getConnectedPort () const noexcept
 
virtual void setOnBeforeSocketClosedFp (BeforeClosedHandler fp)
 
virtual void doSend (const StunTuple &destination, unsigned short channel, const std::shared_ptr< DataBuffer > &data, const std::size_t bufferStartPos=0)
 Use these if you already operating within the ioService thread.
 
virtual void doSend (const StunTuple &destination, const std::shared_ptr< DataBuffer > &data, const std::size_t bufferStartPos=0)
 
virtual void doReceive ()
 
virtual void doFramedReceive ()
 
virtual void onConnectSuccess ()
 Class override callbacks.
 
virtual void onConnectFailure (const asio::error_code &e)
 
virtual void onReceiveSuccess (const asio::ip::address &address, unsigned short port, const std::shared_ptr< DataBuffer > &data)=0
 
virtual void onReceiveFailure (const asio::error_code &e)=0
 
virtual void onSendSuccess ()=0
 
virtual void onSendFailure (const asio::error_code &e)=0
 
virtual void start ()
 
virtual void stop ()
 
virtual void handleUdpResolve (const asio::error_code &ec, const asio::ip::udp::resolver::results_type &endpoints)
 

Static Public Member Functions

static bool setupClientSsl (SSL *ssl, const std::string &hostname, bool validateHostname)
 Sets up a client SSL to connect to hostname (a host name or IP address): SNI for a host name, and - when validateHostname - has OpenSSL check during the handshake that the server certificate is for hostname, so the handshake fails if it isn't.
 
static void logClientHandshakeResult (SSL *ssl, const std::string &hostname, const asio::error_code &ec)
 Logs a client handshake's outcome: the TLS version and cipher, or why the server certificate was rejected.
 
- Static Public Member Functions inherited from reTurn::AsyncSocketBase
static std::shared_ptr< DataBuffer > allocateBuffer (size_t size)
 Utility API.
 
static size_t channelDataPadding (size_t length)
 Over TCP and TLS, a TURN ChannelData message is padded to a multiple of 4 bytes, the padding not counted in its length (RFC 5766 11.5, RFC 8656 12.5): the padding after one of length bytes.
 
static void setExclusiveAddressUse (asio::ip::udp::socket &socket)
 For a UDP socket, between open() and bind().
 

Protected Member Functions

void handleReadHeader (const asio::error_code &e) override
 
void handleServerHandshake (const asio::error_code &e) override
 
void handleTcpResolve (const asio::error_code &ec, const asio::ip::tcp::resolver::results_type &endpoints) override
 
void handleConnect (const asio::error_code &ec, const asio::ip::tcp::endpoint &endpoint) override
 
void handleClientHandshake (const asio::error_code &ec, const asio::ip::tcp::endpoint &endpoint) override
 
virtual void onServerHandshakeSuccess ()
 
virtual void onServerHandshakeFailure (const asio::error_code &e)
 
- Protected Member Functions inherited from reTurn::AsyncSocketBase
virtual void handleSend (const asio::error_code &e)
 Handle completion of a sendData operation.
 
virtual void handleReceive (const asio::error_code &e, size_t bytesTransferred)
 
asio::error_code openAndBind (asio::ip::tcp::socket &socket)
 For TCP and TLS client sockets.
 
void asyncConnect (asio::ip::tcp::socket &socket, const asio::ip::tcp::resolver::results_type &endpoints)
 
void startIdleTimer (unsigned int timeoutSeconds, std::function< bool()> inUse)
 For server connections (TcpConnectionIdleTimeout): the connection is closed, with close(), once nothing has been received on it for timeoutSeconds while inUse() is false - eg.
 
void noteReceived ()
 
void stopIdleTimer ()
 

Protected Attributes

asio::ssl::stream< asio::ip::tcp::socket > mSocket
 
asio::ip::tcp::resolver mResolver
 
- Protected Attributes inherited from reTurn::AsyncSocketBase
asio::ip::tcp::endpoint mLocalTcpEndpoint
 
asio::io_context & mIOService
 The io_context used to perform asynchronous operations.
 
std::shared_ptr< DataBuffer > mReceiveBuffer
 Receive Buffer and state.
 
bool mReceiving
 
asio::ip::address mConnectedAddress
 Connected Info and State.
 
unsigned short mConnectedPort
 
bool mConnected
 
AsyncSocketBaseHandler * mAsyncSocketBaseHandler
 Handlers.
 
BeforeClosedHandler mOnBeforeSocketCloseFp
 Provides an opportunity for the app to clean up, e.g., QoS-related data or resources just before the socket is closed.
 

Additional Inherited Members

- Public Types inherited from reTurn::AsyncSocketBase
using BeforeClosedHandler = std::function< void(unsigned int)>
 

Detailed Description

Definition at line 17 of file AsyncTlsSocketBase.hxx.

Constructor & Destructor Documentation

◆ AsyncTlsSocketBase()

reTurn::AsyncTlsSocketBase::AsyncTlsSocketBase ( asio::io_context &  ioService,
asio::ssl::context &  context,
bool  validateServerCertificateHostname 
)

Definition at line 26 of file AsyncTlsSocketBase.cxx.

Member Function Documentation

◆ bind()

asio::error_code reTurn::AsyncTlsSocketBase::bind ( const asio::ip::address &  address,
unsigned short  port 
)
overridevirtual

Note: The following API's are thread safe and queue the request to be handled by the ioService thread.

Implements reTurn::AsyncSocketBase.

Definition at line 43 of file AsyncTlsSocketBase.cxx.

◆ connect()

void reTurn::AsyncTlsSocketBase::connect ( const std::string &  address,
unsigned short  port 
)
overridevirtual

Implements reTurn::AsyncSocketBase.

Definition at line 51 of file AsyncTlsSocketBase.cxx.

◆ doHandshake()

void reTurn::AsyncTlsSocketBase::doHandshake ( unsigned int  timeoutSeconds = 0)

Starts the server side handshake.

If timeoutSeconds isn't 0, the connection is closed if the handshake hasn't completed by then.

Definition at line 179 of file AsyncTlsSocketBase.cxx.

◆ getSenderEndpointAddress()

asio::ip::address reTurn::AsyncTlsSocketBase::getSenderEndpointAddress ( )
overridevirtual

Implements reTurn::AsyncSocketBase.

Definition at line 220 of file AsyncTlsSocketBase.cxx.

◆ getSenderEndpointPort()

unsigned short reTurn::AsyncTlsSocketBase::getSenderEndpointPort ( )
overridevirtual

Implements reTurn::AsyncSocketBase.

Definition at line 226 of file AsyncTlsSocketBase.cxx.

◆ getSocketDescriptor()

unsigned int reTurn::AsyncTlsSocketBase::getSocketDescriptor ( )
overridevirtual

Implements reTurn::AsyncSocketBase.

Reimplemented in reTurn::TurnAsyncTlsSocket.

Definition at line 37 of file AsyncTlsSocketBase.cxx.

◆ handleClientHandshake()

void reTurn::AsyncTlsSocketBase::handleClientHandshake ( const asio::error_code &  ec,
const asio::ip::tcp::endpoint &  endpoint 
)
overrideprotectedvirtual

Reimplemented from reTurn::AsyncSocketBase.

Definition at line 159 of file AsyncTlsSocketBase.cxx.

◆ handleConnect()

void reTurn::AsyncTlsSocketBase::handleConnect ( const asio::error_code &  ec,
const asio::ip::tcp::endpoint &  endpoint 
)
overrideprotectedvirtual

Reimplemented from reTurn::AsyncSocketBase.

Definition at line 136 of file AsyncTlsSocketBase.cxx.

◆ handleReadHeader()

void reTurn::AsyncTlsSocketBase::handleReadHeader ( const asio::error_code &  e)
overrideprotectedvirtual

Reimplemented from reTurn::AsyncSocketBase.

Definition at line 268 of file AsyncTlsSocketBase.cxx.

◆ handleServerHandshake()

void reTurn::AsyncTlsSocketBase::handleServerHandshake ( const asio::error_code &  e)
overrideprotectedvirtual

Reimplemented from reTurn::AsyncSocketBase.

Definition at line 201 of file AsyncTlsSocketBase.cxx.

◆ handleTcpResolve()

void reTurn::AsyncTlsSocketBase::handleTcpResolve ( const asio::error_code &  ec,
const asio::ip::tcp::resolver::results_type &  endpoints 
)
overrideprotectedvirtual

Reimplemented from reTurn::AsyncSocketBase.

Definition at line 65 of file AsyncTlsSocketBase.cxx.

◆ logClientHandshakeResult()

void reTurn::AsyncTlsSocketBase::logClientHandshakeResult ( SSL *  ssl,
const std::string &  hostname,
const asio::error_code &  ec 
)
static

Logs a client handshake's outcome: the TLS version and cipher, or why the server certificate was rejected.

Definition at line 119 of file AsyncTlsSocketBase.cxx.

◆ onServerHandshakeFailure()

virtual void reTurn::AsyncTlsSocketBase::onServerHandshakeFailure ( const asio::error_code &  e)
inlineprotectedvirtual

Reimplemented in reTurn::TlsConnection.

Definition at line 58 of file AsyncTlsSocketBase.hxx.

◆ onServerHandshakeSuccess()

virtual void reTurn::AsyncTlsSocketBase::onServerHandshakeSuccess ( )
inlineprotectedvirtual

Reimplemented in reTurn::TlsConnection.

Definition at line 57 of file AsyncTlsSocketBase.hxx.

◆ setupClientSsl()

bool reTurn::AsyncTlsSocketBase::setupClientSsl ( SSL *  ssl,
const std::string &  hostname,
bool  validateHostname 
)
static

Sets up a client SSL to connect to hostname (a host name or IP address): SNI for a host name, and - when validateHostname - has OpenSSL check during the handshake that the server certificate is for hostname, so the handshake fails if it isn't.

The context's verify mode must include verify_peer for that check to apply. Returns false if hostname can't be checked against.

Definition at line 82 of file AsyncTlsSocketBase.cxx.

◆ transportClose()

void reTurn::AsyncTlsSocketBase::transportClose ( )
overridevirtual

Implements reTurn::AsyncSocketBase.

Definition at line 255 of file AsyncTlsSocketBase.cxx.

◆ transportFramedReceive()

void reTurn::AsyncTlsSocketBase::transportFramedReceive ( )
overridevirtual

Implements reTurn::AsyncSocketBase.

Definition at line 248 of file AsyncTlsSocketBase.cxx.

◆ transportReceive()

void reTurn::AsyncTlsSocketBase::transportReceive ( )
overridevirtual

Implements reTurn::AsyncSocketBase.

Definition at line 240 of file AsyncTlsSocketBase.cxx.

◆ transportSend()

void reTurn::AsyncTlsSocketBase::transportSend ( const StunTuple &  destination,
std::vector< asio::const_buffer > &  buffers 
)
overridevirtual

Implements reTurn::AsyncSocketBase.

Definition at line 232 of file AsyncTlsSocketBase.cxx.

Member Data Documentation

◆ mResolver

asio::ip::tcp::resolver reTurn::AsyncTlsSocketBase::mResolver
protected

Definition at line 61 of file AsyncTlsSocketBase.hxx.

◆ mSocket

asio::ssl::stream<asio::ip::tcp::socket> reTurn::AsyncTlsSocketBase::mSocket
protected

Definition at line 60 of file AsyncTlsSocketBase.hxx.


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