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
函数,它会给你 SSL_ERROR_WANT_READ
或 SSL_ERROR_WANT_WRITE
错误,具体取决于 tcp recv 或 send 何时无法分别完成操作.
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):
这篇关于openssl ssl_connect 永远阻塞——如何设置超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!