当没有互联网连接时,非阻塞BIO_do_connect被阻塞 [英] nonblocking BIO_do_connect blocked when there is no internet connected

查看:381
本文介绍了当没有互联网连接时,非阻塞BIO_do_connect被阻塞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在如下使用Openssl-0.9.8x:

I am using Openssl-0.9.8x as follows:

bio = BIO_new_ssl_connect(ctx);
BIO_get_ssl(bio, & ssl);
SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
BIO_set_nbio(bio, 1);
in_addr_t serverIP =  inet_addr(HTTPS_SERVER_IP);
BIO_set_conn_ip(bio, &serverIP );
BIO_set_conn_port(bio, HTTPS_SERVER_PORT_STR);
while(1) {
    printf("BIO_do_connect start>>>>\n");
    if(BIO_do_connect(bio) <= 0 && BIO_should_retry(bio)) {
        sleep(1);
        printf("BIO_do_connect retry>>>>\n");
    }
    else {
        printf("Connect success.\n");
    }
}

当互联网连接正常时(即可以连接到服务器),它可以正常工作.但是,当互联网连接受到限制(即它无法连接到服务器)时,BIO_do_connect()将在重试一次或多次后被阻止. 输出如下:

It works fine when the internet connection is OK (i.e. it can connect to the server). But, when the internet connection is limited (i.e. it can't connect to the server), the BIO_do_connect() is blocked after one or more times of retry. The output as follows:

BIO_do_connect start>>>>
BIO_do_connect retry>>>>
BIO_do_connect start>>>>
BIO_do_connect retry>>>>
BIO_do_connect start>>>>

最后,它在BIO_do_connect(...)中被阻止了吗?为什么会这样?

Finally, it is blocked in BIO_do_connect(...)? why this happened?

推荐答案

可能是您使用的SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY).

在0.9.8手册页中:

From the 0.9.8 man page:

SSL_MODE_AUTO_RETRY

SSL_MODE_AUTO_RETRY

如果传输阻塞,请不要重试应用程序. 如果在正常操作期间进行了重新协商, SSL_read()或SSL_write()将返回 -1表示需要重试SSL_ERROR_WANT_READ. 在非阻塞环境中,必须准备处理应用程序 不完整的读/写操作. 在阻塞环境中,应用程序并不总是准备好 处理读/写操作返回而没有成功报告的情况.这 标志SSL_MODE_AUTO_RETRY将导致仅读/写操作 握手并成功完成后返回.

Never bother the application with retries if the transport is blocking. If a renegotiation take place during normal operation, a SSL_read() or SSL_write() would return with -1 and indicate the need to retry with SSL_ERROR_WANT_READ. In a non-blocking environment applications must be prepared to handle incomplete read/write operations. In a blocking environment, applications are not always prepared to deal with read/write operations returning without success report. The flag SSL_MODE_AUTO_RETRY will cause read/write operations to only return after the handshake and successful completion.

SSL_MODE_AUTO_RETRY的作用是自动重试将以其他方式返回到应用程序代码的操作(即使在使用阻塞连接时). 想要非阻塞操作时,使用它毫无意义.

The effect of SSL_MODE_AUTO_RETRY is to automatically retry operations that would otherwise return back to application code (even when the using blocking connections). It doesn't make any sense to use it when you want non-blocking operation.

尝试完全删除该行.

顺便说一句0.9.8不在支持范围内,不再接收安全更新.您确实应该升级到最新版本.

By the way 0.9.8 is out of support and is no longer receiving security updates. You really ought to upgrade to a more recent version.

这篇关于当没有互联网连接时,非阻塞BIO_do_connect被阻塞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆