title: Changing an OpenSSL BIO from blocking to non-blocking mode
body: <p>I have a multithreaded application that makes heavy use of OpenSSL in C. It is designed with the idea that all of its SSL connections are expected to block. Specifically, blocking BIOs. They are all allocated off a single incoming port like this:</p>
  
  <pre><code>ssl = SSL_new(ctx);
  SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
  sock = BIO_new_socket(socket, BIO_CLOSE);
  SSL_set_bio(ssl, sock, sock);
  </code></pre>
  
  <p>As it turns out though, there are a few small parts of the codebase where using non-blocking BIOs would be the best choice. The small parts that would benefit from the non-blocking BIOs have no way of knowing which SSL connections will belong to them. Thus, they always receive blocking BIOs.</p>
  
  <p>The question is, can the blocking BIOs be changed to be non-blocking?</p>
  
  <p>I know that <a href="http://www.openssl.org/docs/crypto/BIO_s_connect.html">BIO_set_nbio</a> can be used to make a BIO non-blocking but the documentation says:</p>
  
  <blockquote>
    <p>The call to BIO_set_nbio() should be made before the connection is established because non blocking I/O is set during the connect process.</p>
  </blockquote>
  
  <p>Another possible option I have thought about would be to copy the BIO and recreate it, while somehow maintaining all of the state. </p>