Using OpenSSL without threads
Whenever I bash into OpenSSL on Windows or Mac I always make my own memory BIOs, and link them up to the platforms message based (asynchronous non blocking) socket implementation. (WSAAsyncSelect on windows: CFSocket on Mac)
Secure programming with the OpenSSL API hosted on ibm.com seems to be the best reference on implementing OpenSSL - but it implements a very simple blocking connection.
Is there a standard way to setup and use OpenSSL with non blocking sockets - 开发者_开发技巧such that calls to SSL_read will not block if there is no data for example?
SSL_read()
(and the other SSL functions) work fine if the underlying socket is set non-blocking. If insufficient data is available, it will return a value less than zero; SSL_get_error()
called on the return value will return SSL_ERROR_WANT_READ
or SSL_ERROR_WANT_WRITE
, indicating what SSL is waiting for.
Using BIO_set_nbio
with either BIO_new_socket
or BIO_new_connect/accept
is probably less code than creating memory BIOs. Not sure if there's anything more standard than that. The docs explain this in more detail:
http://www.openssl.org/docs/crypto/BIO_s_connect.html
精彩评论