Check if connection closed without calling mbedtls_ssl_read (data consumption)?


is there any way to check if a connection was closed without checking the result of mbedtls_ssl_read (e.g. without consuming any data from the stream)?

I’m porting a C++ Wrapper for TLSServer/TLSSocket handling to an ESP32 MCU. Where TLSServer handles dispatching the sockets. Something like that:

if ( FD_ISSET(socket->fd, &readReadyFDSet) ) 
    if ( SSL_peek(socket->ssl, &buf, 1 ) <= 0)

Whereas application code looks like that:

 void App::onDataReady(TLSSocket *socket) {
    socket->readData(this->buffer, BUF_SIZE)

What’s important here is, that socket->readData() is basically just a wrapper for mbedtls_ssl_read and it’s the applications responsibility to manage the buffer.

As Mbed TLS does not have any peek capability I have not found a way to check a closed connection within the TLSServer class without adding an additional read buffer and/or leaving this responsibility to the application code (which in turn would have to signaling the closed socket back to the TLSServer class…).

Is there really no other way to check this?

Best regards

Hi @cryptkeeper
The issue you have referenced sums the status.
Mbed TLS does not have a peek capability, and there is no added value of having it in the TLS stack instead of application layer.
Mbed TLS is not responsible for the socket, and you control the socket layer.
I believe you can check the socket before callign socket->readData() on your application.
Mbed Support