static int on_io_recv()

in adapters/tlsio_wolfssl.c [382:456]


static int on_io_recv(WOLFSSL *ssl, char *buf, int sz, void *context)
{
    int result;
    if (context != NULL)
    {
        TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)context;
        unsigned char* new_socket_io_read_bytes;
        size_t socket_reads = 0;

        AZURE_UNREFERENCED_PARAMETER(ssl);
        while (tls_io_instance->socket_io_read_byte_count == 0 && socket_reads < SOCKET_READ_LIMIT)
        {
            xio_dowork(tls_io_instance->socket_io);
            if (tls_io_instance->tlsio_state != TLSIO_STATE_IN_HANDSHAKE)
            {
                break;
            }
            socket_reads++;
        }

        result = tls_io_instance->socket_io_read_byte_count;
        if (result > sz)
        {
            result = sz;
        }

        if (result > 0)
        {
            size_t read_byte_count = safe_subtract_size_t(tls_io_instance->socket_io_read_byte_count, (size_t)result);
            (void)memcpy(buf, tls_io_instance->socket_io_read_bytes, result);
            (void)memmove(tls_io_instance->socket_io_read_bytes, tls_io_instance->socket_io_read_bytes + result, read_byte_count);

            tls_io_instance->socket_io_read_byte_count = read_byte_count;
            if (tls_io_instance->socket_io_read_byte_count > 0)
            {
                new_socket_io_read_bytes = (unsigned char*)realloc(tls_io_instance->socket_io_read_bytes, tls_io_instance->socket_io_read_byte_count);
                if (new_socket_io_read_bytes != NULL)
                {
                    tls_io_instance->socket_io_read_bytes = new_socket_io_read_bytes;
                }
            }
            else
            {
                free(tls_io_instance->socket_io_read_bytes);
                tls_io_instance->socket_io_read_bytes = NULL;
            }
        }

        if (tls_io_instance->tlsio_state == TLSIO_STATE_ERROR)
        {
            result = WOLFSSL_CBIO_ERR_GENERAL;
        }
        else if ( (result == 0) && (tls_io_instance->tlsio_state == TLSIO_STATE_OPEN))
        {
#ifdef HAVE_SECURE_RENEGOTIATION
            if (wolfSSL_SSL_renegotiate_pending(tls_io_instance->ssl) == 0) // SERVER_HELLODONE_COMPLETE
            {
                result = WOLFSSL_CBIO_ERR_WANT_READ;
            }
            // If Server Hello not complete during renegotiation, do not return error.
#else
            result = WOLFSSL_CBIO_ERR_WANT_READ;
#endif
        }
        else if ((result == 0) && (tls_io_instance->tlsio_state == TLSIO_STATE_CLOSING || tls_io_instance->tlsio_state == TLSIO_STATE_NOT_OPEN))
        {
            result = WOLFSSL_CBIO_ERR_CONN_CLOSE;
        }
    }
    else
    {
        result = WOLFSSL_CBIO_ERR_GENERAL;
    }
    return result;
}