Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

ssl - openssl ssl_connect blocks forever - how to set timeout?

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.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.

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.

The below links should help you (especially the second link, Section 6 which talks about Multiplexed I/O):


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...