1 (edited by muyouyuwan 2017-08-16 22:29:36)

Topic: What wolfSSL_recv will return when the network is broken

I have a server and client connected though wifi.

server receive code:
    int32_t recv_len = 0;
    char errbuffer[WOLFSSL_MAX_ERROR_SZ];
    unsigned int err;
    unsigned int rc = 0;
    do
    {
        recv_len = wolfSSL_recv(ssl, buffer, length, flags);
        Tr_Wrn("wolfSSL_read->recv_len:%d", recv_len);
        if (recv_len < 0)
        {
            err = wolfSSL_get_error(ssl, 0);
            Tr_Err("err = %d, %s\n", err, wolfSSL_ERR_error_string(err, errbuffer));
            if ((err == SSL_ERROR_WANT_READ) || (err == SSL_ERROR_WANT_WRITE))
            {
                Tr_Wrn("err SSL_ERROR_WANT_READ / SSL_ERROR_WANT_WRITE");
            }
            else
            {
                return -1;
            }
        }
        else if (recv_len == 0 )
        {
            Tr_Wrn("peer socket closed!!!");
            return 0;
        }
        else
        {
            rc += recv_len;
            buffer += recv_len;
        }
    } while((recv_len < 0) && ((err == SSL_ERROR_WANT_READ) || (err == SSL_ERROR_WANT_WRITE)));


The flags is set MSG_DONTWAIT. When server is receiving a big data from client, the wifi is broken suddenly.
Print log is:
viwi: 26037048-[viwi soc]-W-00:13:23.800-SSLSocket.cpp-79-wolfSSL_read->recv_len:-1
viwi: 26037049-[viwi soc]-E-00:13:23.800-SSLSocket.cpp-83-err = 2, non-blocking socket wants data to be read
viwi: 26037050-[viwi soc]-W-00:13:23.800-SSLSocket.cpp-86-err SSL_ERROR_WANT_READ / SSL_ERROR_WANT_WRITE
viwi: 26037051-[viwi soc]-W-00:13:23.801-SSLSocket.cpp-79-wolfSSL_read->recv_len:-1
viwi: 26037052-[viwi soc]-E-00:13:23.801-SSLSocket.cpp-83-err = 2, non-blocking socket wants data to be read
viwi: 26037053-[viwi soc]-W-00:13:23.801-SSLSocket.cpp-86-err SSL_ERROR_WANT_READ / SSL_ERROR_WANT_WRITE
viwi: 26037054-[viwi soc]-W-00:13:23.801-SSLSocket.cpp-79-wolfSSL_read->recv_len:-1
viwi: 26037055-[viwi soc]-E-00:13:23.801-SSLSocket.cpp-83-err = 2, non-blocking socket wants data to be read
viwi: 26037056-[viwi soc]-W-00:13:23.801-SSLSocket.cpp-86-err SSL_ERROR_WANT_READ / SSL_ERROR_WANT_WRITE
viwi: 26037057-[viwi soc]-W-00:13:23.801-SSLSocket.cpp-79-wolfSSL_read->recv_len:-1
viwi: 26037058-[viwi soc]-E-00:13:23.801-SSLSocket.cpp-83-err = 2, non-blocking socket wants data to be read
viwi: 26037059-[viwi soc]-W-00:13:23.801-SSLSocket.cpp-86-err SSL_ERROR_WANT_READ / SSL_ERROR_WANT_WRITE
viwi: 26037060-[viwi soc]-W-00:13:23.801-SSLSocket.cpp-79-wolfSSL_read->recv_len:-1
viwi: 26037061-[viwi soc]-E-00:13:23.801-SSLSocket.cpp-83-err = 2, non-blocking socket wants data to be read
viwi: 26037062-[viwi soc]-W-00:13:23.801-SSLSocket.cpp-86-err SSL_ERROR_WANT_READ / SSL_ERROR_WANT_WRITE
viwi: 26037063-[viwi soc]-W-00:13:23.801-SSLSocket.cpp-79-wolfSSL_read->recv_len:-1
viwi: 26037064-[viwi soc]-E-00:13:23.801-SSLSocket.cpp-83-err = 2, non-blocking socket wants data to be read
viwi: 26037065-[viwi soc]-W-00:13:23.801-SSLSocket.cpp-86-err SSL_ERROR_WANT_READ / SSL_ERROR_WANT_WRITE
viwi: 26037066-[viwi soc]-W-00:13:23.801-SSLSocket.cpp-79-wolfSSL_read->recv_len:-1
viwi: 26037067-[viwi soc]-E-00:13:23.801-SSLSocket.cpp-83-err = 2, non-blocking socket wants data to be read
viwi: 26037068-[viwi soc]-W-00:13:23.801-SSLSocket.cpp-86-err SSL_ERROR_WANT_READ / SSL_ERROR_WANT_WRITE
viwi: 26037069-[viwi soc]-W-00:13:23.801-SSLSocket.cpp-79-wolfSSL_read->recv_len:-1
viwi: 26037070-[viwi soc]-E-00:13:23.801-SSLSocket.cpp-83-err = 2, non-blocking socket wants data to be read
viwi: 26037071-[viwi soc]-W-00:13:23.801-SSLSocket.cpp-86-err SSL_ERROR_WANT_READ / SSL_ERROR_WANT_WRITE
viwi: 26037072-[viwi soc]-W-00:13:23.801-SSLSocket.cpp-79-wolfSSL_read->recv_len:-1
viwi: 26037073-[viwi soc]-E-00:13:23.801-SSLSocket.cpp-83-err = 2, non-blocking socket wants data to be read
viwi: 26037074-[viwi soc]-W-00:13:23.801-SSLSocket.cpp-86-err SSL_ERROR_WANT_READ / SSL_ERROR_WANT_WRITE
viwi: 26037075-[viwi soc]-W-00:13:23.801-SSLSocket.cpp-79-wolfSSL_read->recv_len:-1
viwi: 26037076-[viwi soc]-E-00:13:23.801-SSLSocket.cpp-83-err = 2, non-blocking socket wants data to be read
viwi: 26037077-[viwi soc]-W-00:13:23.801-SSLSocket.cpp-86-err SSL_ERROR_WANT_READ / SSL_ERROR_WANT_WRITE
viwi: 26037078-[viwi soc]-W-00:13:23.801-SSLSocket.cpp-79-wolfSSL_read->recv_len:-1
viwi: 26037079-[viwi soc]-E-00:13:23.801-SSLSocket.cpp-83-err = 2, non-blocking socket wants data to be read
viwi: 26037080-[viwi soc]-W-00:13:23.801-SSLSocket.cpp-86-err SSL_ERROR_WANT_READ / SSL_ERROR_WANT_WRITE


And will not stop printing this log.

So I want to know how can i know the connecting is broken and stop receiving.

Share

Re: What wolfSSL_recv will return when the network is broken

Hi muyouyuwan,

When using non-blocking sockets with wolfSSL, wolfSSL returns SSL_ERROR_WANT_READ when there is no data available to be read on the socket, or SSL_ERROR_WANT_WRITE when data is not able to be written to the socket at the given time.

In either of these cases, the application needs to call the wolfSSL API (in your case wolfSSL_recv()) when data is available.  Your application is expected to know when data is available on the socket, or poll the wolfSSL API periodically.  Oftentimes, applications will call select() on the socket to wait for data to become available for reading.

Best Regards,
Chris

Re: What wolfSSL_recv will return when the network is broken

Hi Chris,
Thanks for your reply. But I want to know how can I judge if the network is abort from the return value?

Share

Re: What wolfSSL_recv will return when the network is broken

wolfSSL_get_error() should return SOCKET_PEER_CLOSED_E in the case where the peer has reset or closed the socket.

Best Regards,
Chris

Re: What wolfSSL_recv will return when the network is broken

Hi Chris,
I got the err is SSL_ERROR_WANT_READ but not SOCKET_PEER_CLOSED_E.
I know wolfSSL_recv() should return 0 if peer has reset or closed the socket.
But this issue is the wifi is abort suddenly before peer can closed the socket.

Share

Re: What wolfSSL_recv will return when the network is broken

Internally wolfSSL uses the EmbedSend() and EmbedReceive() callback functions in ./src/io.c to send and receive data.  This is where we call the normal BSD send/recv functions by default.  It may help to take a look here and see what the normal socket function is receiving to better understand wolfSSL's error codes as well.

In EmbedReceive() for example, if recv() has set errno to SOCKET_EWOULDBLOCK, SOCKET_EAGAIN, or SOCKET_ECONNREFUSED, we return WOLFSSL_CBIO_ERR_WANT_READ which gets propogated back up to the application layer as SSL_ERROR_WANT_READ from wolfSSL_get_error().

Best Regards,
Chris