Sinder230
Sinder230

Reputation: 33

OpenSSL Non-Blocking Socket SSL_read() unpredictable

I'm trying to make a non-blocking socket w/OpenSSL so I can loop over SSL_read() until there's no more data left, then break the loop. After a lot of work, I finally got the connection working, but now for the first thousand+ iterations SSL_read() will return -1. After, it will give me the actual data on the socket. SSL_read() also is not returning an accurate number of bytes read, it ALWAYS says -1 even when it's reading the correct bytes. I was able to get it working perfectly w/blocking sockets but non blocking seems problematic...

char *sslRead (connection *c)
{
const int readSize = 1024;
char *rc = NULL;
int r;
int received = -1, count = 0;
int TotalReceived = 0, ReallocSize = 0;
char buffer[1024];

if (c)
{
    while (1)
    {

        received = SSL_read (c->sslHandle, buffer, readSize);

    buffer[received] = '\0';

        TotalReceived += received;
        printf("Buffsize - %i - %s \n", received, buffer);


        if (received <= 0)
        {

            // this line added per advice of Eric Tsui but does not
            // change behaviour 
            received = SSL_read (c->sslHandle, buffer, readSize);



            //printf(" received equal to or less than 0\n")
            switch (SSL_get_error(c->sslHandle, received))
            {
                case SSL_ERROR_NONE:
                {
                        printf("SSL_ERROR_NONE %i\n", count);
                        //if (received != -1)
                        //  goto END;
                        break;
                }   
                case SSL_ERROR_ZERO_RETURN: 
                {
                        printf("SSL_ERROR_ZERO_RETURN %i\n", count);
                        goto END;
                        break;
                }   
                case SSL_ERROR_WANT_READ: 
                {
                        printf("SSL_ERROR_WANT_READ %i\n", count);

                        break;
                }
                case SSL_ERROR_WANT_WRITE: 
                {
                        printf("SSL_ERROR_WANT_WRITE %i\n", count);
                        goto END;
                        //break;
                }
                default:
                {
                        printf("error %i\n", received); 
                        break;
                }
            }     


            count++;
        }
    }
}
END:
return rc;
}

If you've done this before, I'd really appreciate if you could tell me what's wrong with this code. Thanks.

Upvotes: 3

Views: 15715

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 596352

Your loop is calling SSL_read() too many times, and your error handling is not breaking the loop when there really is no more data to read (the SSL_ERROR_WANT_READ condition). Try something more like this instead:

char *sslRead (connection *c)
{
    const int readSize = 1024;
    char *rc = NULL;
    int received, count = 0;
    int TotalReceived = 0;
    fd_set fds;
    struct timeval timeout;
    char buffer[1024];

    if (c)
    {
        while (1)
        {
            received = SSL_read (c->sslHandle, buffer, readSize);
            if (received > 0)
            {
                TotalReceived += received;
                printf("Buffsize - %i - %.*s \n", received, received, buffer);
            }
            else
            {
                count++;

                //printf(" received equal to or less than 0\n")
                int err = SSL_get_error(c->sslHandle, received);
                switch (err)
                {
                    case SSL_ERROR_NONE:
                    {
                        // no real error, just try again...
                        printf("SSL_ERROR_NONE %i\n", count);
                        continue;
                    }   

                    case SSL_ERROR_ZERO_RETURN: 
                    {
                        // peer disconnected...
                        printf("SSL_ERROR_ZERO_RETURN %i\n", count);
                        break;
                    }   

                    case SSL_ERROR_WANT_READ: 
                    {
                        // no data available right now, wait a few seconds in case new data arrives...
                        printf("SSL_ERROR_WANT_READ %i\n", count);

                        int sock = SSL_get_rfd(c->sslHandle);
                        FD_ZERO(&fds);
                        FD_SET(sock, &fds);

                        timeout.tv_sec = 5;
                        timeou.tv_nsec = 0;

                        err = select(sock+1, &fds, NULL, NULL, &timeout);
                        if (err > 0)
                            continue; // more data to read...

                        if (err == 0) {
                            // timeout...
                        } else {
                            // error...
                        }

                        break;
                    }

                    case SSL_ERROR_WANT_WRITE: 
                    {
                        // socket not writable right now, wait a few seconds and try again...
                        printf("SSL_ERROR_WANT_WRITE %i\n", count);

                        int sock = SSL_get_wfd(c->sslHandle);
                        FD_ZERO(&fds);
                        FD_SET(sock, &fds);

                        timeout.tv_sec = 5;
                        timeou.tv_nsec = 0;

                        err = select(sock+1, NULL, &fds, NULL, &timeout);
                        if (err > 0)
                            continue; // can write more data now...

                        if (err == 0) {
                            // timeout...
                        } else {
                            // error...
                        }

                        break;
                    }

                    default:
                    {
                        printf("error %i:%i\n", received, err); 
                        break;
                    }
                }     

                break;
            }
        }
    }

    return rc;
}

Upvotes: 11

Related Questions