Cuberite
A lightweight, fast and extensible game server for Minecraft
Public Member Functions | Protected Member Functions | Protected Attributes | Friends | List of all members
cBlockingSslClientSocket Class Reference

#include <BlockingSslClientSocket.h>

Inheritance diagram for cBlockingSslClientSocket:
Inheritance graph
[legend]
Collaboration diagram for cBlockingSslClientSocket:
Collaboration graph
[legend]

Public Member Functions

 cBlockingSslClientSocket (void)
 
bool Connect (const AString &a_ServerName, UInt16 a_Port)
 Connects to the specified server and performs SSL handshake. More...
 
void Disconnect (void)
 Disconnects the connection gracefully, if possible. More...
 
const AStringGetLastErrorText (void) const
 Returns the text of the last error that has occurred in this instance. More...
 
int Receive (void *a_Data, size_t a_MaxBytes)
 Receives data from the connection. More...
 
bool Send (const void *a_Data, size_t a_NumBytes)
 Sends the specified data over the connection. More...
 
void SetExpectedPeerName (AString a_ExpectedPeerName)
 Sets the Expected peer name. More...
 
void SetSslConfig (std::shared_ptr< const cSslConfig > a_Config)
 Set the config to be used by the SSL context. More...
 
virtual ~cBlockingSslClientSocket (void) override
 

Protected Member Functions

void OnConnected (void)
 Called when the connection is established successfully. More...
 
void OnConnectError (const AString &a_ErrorMsg)
 Called when an error occurs while connecting the socket. More...
 
void OnDisconnected (void)
 Called when the link is disconnected, either gracefully or by an error. More...
 
void OnReceivedData (const char *a_Data, size_t a_Size)
 Called when there's incoming data from the socket. More...
 
virtual int ReceiveEncrypted (unsigned char *a_Buffer, size_t a_NumBytes) override
 Called when mbedTLS wants to read encrypted data from the SSL peer. More...
 
virtual int SendEncrypted (const unsigned char *a_Buffer, size_t a_NumBytes) override
 Called when mbedTLS wants to write encrypted data to the SSL peer. More...
 
void SetLink (cTCPLinkPtr a_Link)
 Called when the link for the connection is created. More...
 
- Protected Member Functions inherited from cCallbackSslContext::cDataCallbacks
virtual ~cDataCallbacks ()
 

Protected Attributes

std::shared_ptr< const cSslConfigm_Config
 The configuration to be used by the SSL context. More...
 
cCriticalSection m_CSIncomingData
 Protects m_IncomingData against multithreaded access. More...
 
cEvent m_Event
 The object used to signal state changes in the socket (the cause of the blocking). More...
 
AString m_ExpectedPeerName
 The expected SSL peer's name, if we are to verify the cert strictly. More...
 
AString m_IncomingData
 Buffer for the data incoming on the network socket. More...
 
std::atomic< bool > m_IsConnected
 Set to true if the connection established successfully. More...
 
AString m_LastErrorText
 Text of the last error that has occurred. More...
 
AString m_ServerName
 The hostname to which the socket is connecting (stored for error reporting). More...
 
cTCPLinkPtr m_Socket
 The underlying socket to the SSL server. More...
 
cCallbackSslContext m_Ssl
 The SSL context used for the socket. More...
 

Friends

class cBlockingSslClientSocketConnectCallbacks
 
class cBlockingSslClientSocketLinkCallbacks
 

Detailed Description

Definition at line 19 of file BlockingSslClientSocket.h.

Constructor & Destructor Documentation

◆ cBlockingSslClientSocket()

cBlockingSslClientSocket::cBlockingSslClientSocket ( void  )

Definition at line 89 of file BlockingSslClientSocket.cpp.

◆ ~cBlockingSslClientSocket()

virtual cBlockingSslClientSocket::~cBlockingSslClientSocket ( void  )
inlineoverridevirtual

Definition at line 25 of file BlockingSslClientSocket.h.

Member Function Documentation

◆ Connect()

bool cBlockingSslClientSocket::Connect ( const AString a_ServerName,
UInt16  a_Port 
)

Connects to the specified server and performs SSL handshake.

Returns true if successful, false on failure. Sets internal error text on failure.

Definition at line 100 of file BlockingSslClientSocket.cpp.

◆ Disconnect()

void cBlockingSslClientSocket::Disconnect ( void  )

Disconnects the connection gracefully, if possible.

Note that this also frees the internal SSL context, so all the certificates etc. are lost.

Definition at line 253 of file BlockingSslClientSocket.cpp.

◆ GetLastErrorText()

const AString& cBlockingSslClientSocket::GetLastErrorText ( void  ) const
inline

Returns the text of the last error that has occurred in this instance.

Definition at line 59 of file BlockingSslClientSocket.h.

◆ OnConnected()

void cBlockingSslClientSocket::OnConnected ( void  )
protected

Called when the connection is established successfully.

Definition at line 324 of file BlockingSslClientSocket.cpp.

◆ OnConnectError()

void cBlockingSslClientSocket::OnConnectError ( const AString a_ErrorMsg)
protected

Called when an error occurs while connecting the socket.

Definition at line 334 of file BlockingSslClientSocket.cpp.

◆ OnDisconnected()

void cBlockingSslClientSocket::OnDisconnected ( void  )
protected

Called when the link is disconnected, either gracefully or by an error.

Definition at line 366 of file BlockingSslClientSocket.cpp.

◆ OnReceivedData()

void cBlockingSslClientSocket::OnReceivedData ( const char *  a_Data,
size_t  a_Size 
)
protected

Called when there's incoming data from the socket.

Definition at line 344 of file BlockingSslClientSocket.cpp.

◆ Receive()

int cBlockingSslClientSocket::Receive ( void *  a_Data,
size_t  a_MaxBytes 
)

Receives data from the connection.

Blocks until there is any data available, then returns as much as possible. Returns the number of bytes actually received, negative number on failure. Sets the internal error text on failure.

Definition at line 238 of file BlockingSslClientSocket.cpp.

◆ ReceiveEncrypted()

int cBlockingSslClientSocket::ReceiveEncrypted ( unsigned char *  a_Buffer,
size_t  a_NumBytes 
)
overrideprotectedvirtual

Called when mbedTLS wants to read encrypted data from the SSL peer.

The returned value is the number of bytes received, or a mbedTLS error on failure. The implementation can return MBEDTLS_ERR_SSL_WANT_READ or MBEDTLS_ERR_SSL_WANT_WRITE to indicate that there's currently no more data and that there might be more data in the future. In such cases the SSL operation that invoked this call will terminate with the same return value, so that the owner is notified of this condition and can potentially restart the operation later on.

Implements cCallbackSslContext::cDataCallbacks.

Definition at line 278 of file BlockingSslClientSocket.cpp.

◆ Send()

bool cBlockingSslClientSocket::Send ( const void *  a_Data,
size_t  a_NumBytes 
)

Sends the specified data over the connection.

Returns true if successful, false on failure. Sets the internal error text on failure.

Definition at line 201 of file BlockingSslClientSocket.cpp.

◆ SendEncrypted()

int cBlockingSslClientSocket::SendEncrypted ( const unsigned char *  a_Buffer,
size_t  a_NumBytes 
)
overrideprotectedvirtual

Called when mbedTLS wants to write encrypted data to the SSL peer.

The returned value is the number of bytes sent, or a mbedTLS error on failure. The implementation can return MBEDTLS_ERR_SSL_WANT_READ or MBEDTLS_ERR_SSL_WANT_WRITE to indicate that there's currently no more data and that there might be more data in the future. In such cases the SSL operation that invoked this call will terminate with the same return value, so that the owner is notified of this condition and can potentially restart the operation later on.

Implements cCallbackSslContext::cDataCallbacks.

Definition at line 305 of file BlockingSslClientSocket.cpp.

◆ SetExpectedPeerName()

void cBlockingSslClientSocket::SetExpectedPeerName ( AString  a_ExpectedPeerName)

Sets the Expected peer name.

Needs to be used before calling Connect().

Parameters
a_ExpectedPeerNameName that we expect to receive in the SSL peer's cert; verification will fail if the presented name is different (possible MITM).

Definition at line 164 of file BlockingSslClientSocket.cpp.

◆ SetLink()

void cBlockingSslClientSocket::SetLink ( cTCPLinkPtr  a_Link)
protected

Called when the link for the connection is created.

Definition at line 357 of file BlockingSslClientSocket.cpp.

◆ SetSslConfig()

void cBlockingSslClientSocket::SetSslConfig ( std::shared_ptr< const cSslConfig a_Config)

Set the config to be used by the SSL context.

Config must not be modified after calling connect.

Definition at line 184 of file BlockingSslClientSocket.cpp.

Friends And Related Function Documentation

◆ cBlockingSslClientSocketConnectCallbacks

Definition at line 62 of file BlockingSslClientSocket.h.

◆ cBlockingSslClientSocketLinkCallbacks

Definition at line 63 of file BlockingSslClientSocket.h.

Member Data Documentation

◆ m_Config

std::shared_ptr<const cSslConfig> cBlockingSslClientSocket::m_Config
protected

The configuration to be used by the SSL context.

Set by SetSslConfig().

Definition at line 75 of file BlockingSslClientSocket.h.

◆ m_CSIncomingData

cCriticalSection cBlockingSslClientSocket::m_CSIncomingData
protected

Protects m_IncomingData against multithreaded access.

Definition at line 90 of file BlockingSslClientSocket.h.

◆ m_Event

cEvent cBlockingSslClientSocket::m_Event
protected

The object used to signal state changes in the socket (the cause of the blocking).

Definition at line 72 of file BlockingSslClientSocket.h.

◆ m_ExpectedPeerName

AString cBlockingSslClientSocket::m_ExpectedPeerName
protected

The expected SSL peer's name, if we are to verify the cert strictly.

Set by SetExpectedPeerName().

Definition at line 78 of file BlockingSslClientSocket.h.

◆ m_IncomingData

AString cBlockingSslClientSocket::m_IncomingData
protected

Buffer for the data incoming on the network socket.

Protected by m_CSIncomingData.

Definition at line 94 of file BlockingSslClientSocket.h.

◆ m_IsConnected

std::atomic<bool> cBlockingSslClientSocket::m_IsConnected
protected

Set to true if the connection established successfully.

Definition at line 87 of file BlockingSslClientSocket.h.

◆ m_LastErrorText

AString cBlockingSslClientSocket::m_LastErrorText
protected

Text of the last error that has occurred.

Definition at line 84 of file BlockingSslClientSocket.h.

◆ m_ServerName

AString cBlockingSslClientSocket::m_ServerName
protected

The hostname to which the socket is connecting (stored for error reporting).

Definition at line 81 of file BlockingSslClientSocket.h.

◆ m_Socket

cTCPLinkPtr cBlockingSslClientSocket::m_Socket
protected

The underlying socket to the SSL server.

Definition at line 69 of file BlockingSslClientSocket.h.

◆ m_Ssl

cCallbackSslContext cBlockingSslClientSocket::m_Ssl
protected

The SSL context used for the socket.

Definition at line 66 of file BlockingSslClientSocket.h.


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