openssl ssl_connect永远阻止-如何设置超时时间? [英] openssl ssl_connect blocks forever - how to set timeout?
问题描述
当用openssl编写的服务器未调用SSL_accept时,客户端的SSL_connect会永远阻塞. openssl -SSL_CTX_set_timeout和SSL_SESSION_set_timeout中有一些超时功能,但这些功能对SSL_connect无效.
when server written in openssl is'nt calling SSL_accept, client's SSL_connect blocks forever. There are some timeout functions in openssl -SSL_CTX_set_timeout , SSL_SESSION_set_timeout but these have no effect on SSL_connect.
例如,是否确实没有办法为SSL_connect设置超时时间? ssl服务器有问题,并在进行SSL握手之前进入循环?
Is there really no way of setting timeout for SSL_connect when e.g. ssl server is buggy and goes into loop before doing SSL handshake?
推荐答案
OpenSSL库在处理套接字相关问题方面为您提供了最大的灵活性.在您的情况下,SSL_connect
会阻塞,因为您必须将其与阻塞套接字一起使用.请与非阻塞套接字一起使用,在这种情况下,它将返回-1.如果调用SSL_get_error
函数,该函数将根据tcp recv或send无法分别完成操作的时间而给您SSL_ERROR_WANT_READ
或SSL_ERROR_WANT_WRITE
错误.
The OpenSSL Library gives you the maximum flexibility in terms of handling socket related issues. The SSL_connect
blocks in your case because you must be using it with a blocking socket. Please use it with a non-blocking socket, in which case it will return with a -1. If you call SSL_get_error
function which will give you SSL_ERROR_WANT_READ
or SSL_ERROR_WANT_WRITE
error depending on when the tcp recv or send is unable to complete the operation respectively.
获得SSL_ERROR_WANT_WRITE
/SSL_ERROR_WANT_READ
时,必须通过将套接字传递给适当的fd_set
并超时来调用select函数.如果选择超时,则可以认为您的SSL_connect
已超时.
When SSL_ERROR_WANT_WRITE
/SSL_ERROR_WANT_READ
is obtained, you must call select function by passing the socket to appropriate fd_set
and a timeout. If the select times out, you can consider your SSL_connect
to have timed out.
注意:SSL_SESSION_set_timeout
用于设置链接到SSL恢复的会话超时值.它们与超时连接无关.
Note: The SSL_SESSION_set_timeout
is used for setting session timeout values which are linked to SSL resumption. They have nothing to do with timing out a connection.
以下链接应为您提供帮助(尤其是第二个链接,第6节多路复用I/O"):
The below links should help you (especially the second link, Section 6 which talks about Multiplexed I/O):
- Part 1
- Part 2
这篇关于openssl ssl_connect永远阻止-如何设置超时时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!