Question

I have a non blocking Socket and I'm using openssl for creating a ssl connection. My problem is the way SSL_connect works. It Returns SSL_ERROR_WANT_READ or SSL_ERROR_WANT_WRITE in these cases SSL_connect has to be called again. Now this is actually working. The issue I have is, that the select() which should make the program wait for the socket to be ready is not working. This results in over 2000 calls to SSL_connect which is not nice.

Here is my code for this Part of the Program

 int test = 0;
        while (test != 1) {
            test = SSL_connect(sslHandle);
            if (test != 1) { //test again or we would quit if we made a connection
                if (SSL_get_error(sslHandle, test) != SSL_ERROR_WANT_READ && SSL_get_error(sslHandle, test) != SSL_ERROR_WANT_WRITE) {
                    //error happend
                    return -1;
                }
                FD_ZERO(&socketSet); //reset socketSet
                FD_SET(socket, &socketSet); //init socketSet with our socket
                if (test == SSL_ERROR_WANT_READ) {

                    result = select(socket + 1, &socketSet, NULL, NULL, &timeoutCopy); //wait for the socket to be readable
                    if (result == 0) {
                        //timeout
                        return -1; 
                    } else if (result == -1) {
                        //error
                        return -1;
                    }


                } else {

                    result = select(socket + 1, NULL, &socketSet, NULL, &timeoutCopy); //wait for the socket to be writable
                    if (result == 0) {
                       //timeout
                        return -1; 
                    } else if (result == -1) {
                       //error
                        return -1; 
                    }

                }

            }
        }

I Removed all the error handling stuff and logs here so it is better readable. timeoutCopy is defined elsewhere. I'm using the same kind of selects for other parts of the program there it is working just fine. As I said the program is working, but just not waiting at the select. I could just sleep, but that is not a nice solution. Thanks in advance!

Was it helpful?

Solution

Ok I'm a dummy. Issue is in line

 if (test == SSL_ERROR_WANT_READ) 

This has to be

if (SSL_get_error(sslHandle, sslConnectResult) == SSL_ERROR_WANT_READ) 

Than the code works just fine. It iterates 4 to 5 times and than has the connection.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top