[openssl-dev] OpenSSL Non blocking SSL_read() ?

(( \/\/|||"'""/'")) ((\"""" )) (( ))\\\"\\"\ simplesec2012 at gmail.com
Tue Jun 23 01:51:48 UTC 2015


Currently in my OpenSSL client, when I am reading data from the ssl socket,
my client will always call SSL_read() until the terminator is found to be
as the last bytes of the received data. This works well but I don't think
it's the proper way.

while (1)
 {
            if (!rc)
            {
                rc = malloc (readSize + 1);

            }
            else
            {
                ReallocSize = (count + 1) * (readSize + 1);
                rc = realloc (rc, ReallocSize);
            }

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

            if (received <= 0)
            {
                printf(" received equal to or less than 0\n");
                switch (SSL_get_error(c->sslHandle, r))
                {
                case SSL_ERROR_NONE:
                    printf("SSL_ERROR_NONE\n");
                    break;
                case SSL_ERROR_ZERO_RETURN:
                    printf("SSL_ERROR_ZERO_RETURN\n");
                    break;
                case SSL_ERROR_WANT_READ:
                    printf("SSL_ERROR_WANT_READ\n");
                    break;
                default:
                    printf("error happens %i\n", r);
                }
                break;
            }

          if (TerminatorFound)
              break;

            count++;
        }

Here is how I am establishing the connection:

c->socket = tcpConnect ();
  if (c->socket)
    {
      // Register the error strings for libcrypto & libssl
      SSL_load_error_strings ();
      // Register the available ciphers and digests
      SSL_library_init ();

      // New context saying we are a client, and using SSL 2 or 3
      c->sslContext = SSL_CTX_new (SSLv23_client_method ());
      if (c->sslContext == NULL)
        ERR_print_errors_fp (stderr);

      // Create an SSL struct for the connection
      c->sslHandle = SSL_new (c->sslContext);
      if (c->sslHandle == NULL)
        ERR_print_errors_fp (stderr);

      // Connect the SSL struct to our connection
      if (!SSL_set_fd (c->sslHandle, c->socket))
        ERR_print_errors_fp (stderr);

      // Initiate SSL handshake
      if (SSL_connect (c->sslHandle) != 1)
        ERR_print_errors_fp (stderr);
    }
  else
    {
      perror ("Connect failed");
    }

Also, SSL_pending() always returns 0, even if there is data to be read from
the socket.

My goal is to know if there is data to be read without resorting to a
terminating character.

If there is no data, then the call to SSL_read will hang. What function
must I call so that SSL_read will return and not hang? This way I can tell
if data is there without having to look in the response for a terminating
character.

Thank you for your help.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mta.openssl.org/pipermail/openssl-dev/attachments/20150622/82b07f99/attachment.html>


More information about the openssl-dev mailing list