113 std::make_shared<cBlockingSslClientSocketConnectCallbacks>(*
this),
114 std::make_shared<cBlockingSslClientSocketLinkCallbacks>(*
this))
135 ret =
m_Ssl.Initialize(
true);
140 m_LastErrorText = fmt::format(FMT_STRING(
"SSL initialization failed: -0x{:x}"), -ret);
150 ret =
m_Ssl.Handshake();
153 m_LastErrorText = fmt::format(FMT_STRING(
"SSL handshake failed: -0x{:x}"), -ret);
172 "SSL: Trying to set multiple expected peer names, only the last one will be used. %s overwriting the previous %s",
191 LOGWARNING(
"SSL: Trying to set multiple configurations, only the last one will be used.");
210 const char * Data =
static_cast<const char *
>(a_Data);
211 size_t NumBytes = a_NumBytes;
214 int res =
m_Ssl.WritePlain(Data, a_NumBytes);
217 ASSERT(res != MBEDTLS_ERR_SSL_WANT_READ);
218 ASSERT(res != MBEDTLS_ERR_SSL_WANT_WRITE);
219 m_LastErrorText = fmt::format(FMT_STRING(
"Data cannot be written to SSL context: -0x{:x}"), -res);
225 NumBytes -=
static_cast<size_t>(res);
241 int res =
m_Ssl.ReadPlain(a_Data, a_MaxBytes);
244 m_LastErrorText = fmt::format(FMT_STRING(
"Data cannot be read from SSL context: -0x{:x}"), -res);
266 if (socket !=
nullptr)
298 return static_cast<int>(NumToCopy);
308 if (Socket ==
nullptr)
312 if (!Socket->Send(a_Buffer, a_NumBytes))
317 return static_cast<int>(a_NumBytes);
336 LOG(
"Cannot connect to %s: \"%s\"",
m_ServerName.c_str(), a_ErrorMsg.c_str());
void LOGWARNING(std::string_view a_Format, const Args &... args)
void LOG(std::string_view a_Format, const Args &... args)
#define MBEDTLS_ERR_NET_RECV_FAILED
Reading information from the socket failed.
#define MBEDTLS_ERR_NET_SEND_FAILED
Sending information through the socket failed.
std::shared_ptr< cTCPLink > cTCPLinkPtr
virtual void OnConnected(cTCPLink &a_Link) override
Called when the Connect call succeeds.
cBlockingSslClientSocketConnectCallbacks(cBlockingSslClientSocket &a_Socket)
cBlockingSslClientSocket & m_Socket
The socket object that is using this instance of the callbacks.
virtual void OnError(int a_ErrorCode, const AString &a_ErrorMsg) override
Called when the Connect call fails.
cBlockingSslClientSocket & m_Socket
virtual void OnRemoteClosed(void) override
Called when the remote end closes the connection.
virtual void OnLinkCreated(cTCPLinkPtr a_Link) override
Called when the cTCPLink for the connection is created.
virtual void OnError(int a_ErrorCode, const AString &a_ErrorMsg) override
Called when an error is detected on the connection.
virtual void OnReceivedData(const char *a_Data, size_t a_Length) override
Called when there's data incoming from the remote peer.
cBlockingSslClientSocketLinkCallbacks(cBlockingSslClientSocket &a_Socket)
cCallbackSslContext m_Ssl
The SSL context used for the socket.
void Disconnect(void)
Disconnects the connection gracefully, if possible.
bool Connect(const AString &a_ServerName, UInt16 a_Port)
Connects to the specified server and performs SSL handshake.
cTCPLinkPtr m_Socket
The underlying socket to the SSL server.
void SetSslConfig(std::shared_ptr< const cSslConfig > a_Config)
Set the config to be used by the SSL context.
void OnConnected(void)
Called when the connection is established successfully.
AString m_ServerName
The hostname to which the socket is connecting (stored for error reporting).
cBlockingSslClientSocket(void)
int Receive(void *a_Data, size_t a_MaxBytes)
Receives data from the connection.
virtual int ReceiveEncrypted(unsigned char *a_Buffer, size_t a_NumBytes) override
Called when mbedTLS wants to read encrypted data from the SSL peer.
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.
void SetExpectedPeerName(AString a_ExpectedPeerName)
Sets the Expected peer name.
std::atomic< bool > m_IsConnected
Set to true if the connection established successfully.
void OnConnectError(const AString &a_ErrorMsg)
Called when an error occurs while connecting the socket.
std::shared_ptr< const cSslConfig > m_Config
The configuration to be used by the SSL context.
cEvent m_Event
The object used to signal state changes in the socket (the cause of the blocking).
bool Send(const void *a_Data, size_t a_NumBytes)
Sends the specified data over the connection.
AString m_ExpectedPeerName
The expected SSL peer's name, if we are to verify the cert strictly.
cCriticalSection m_CSIncomingData
Protects m_IncomingData against multithreaded access.
AString m_IncomingData
Buffer for the data incoming on the network socket.
void OnReceivedData(const char *a_Data, size_t a_Size)
Called when there's incoming data from the socket.
void OnDisconnected(void)
Called when the link is disconnected, either gracefully or by an error.
void SetLink(cTCPLinkPtr a_Link)
Called when the link for the connection is created.
AString m_LastErrorText
Text of the last error that has occurred.
RAII for cCriticalSection - locks the CS on creation, unlocks on destruction.
Temporary RAII unlock for a cCSLock.
void Wait(void)
Waits until the event has been set.
void Set(void)
Sets the event - releases one thread that has been waiting in Wait().
Interface that provides the methods available on a single TCP connection.
static bool Connect(const AString &a_Host, UInt16 a_Port, cConnectCallbacksPtr a_ConnectCallbacks, cTCPLink::cCallbacksPtr a_LinkCallbacks)
Queues a TCP connection to be made to the specified host.
Callbacks used for connecting to other servers as a client.