简体   繁体   English

将 OpenSSL BIO 从阻塞模式更改为非阻塞模式

[英]Changing an OpenSSL BIO from blocking to non-blocking mode

I have a multithreaded application that makes heavy use of OpenSSL in C. It is designed with the idea that all of its SSL connections are expected to block.我有一个多线程应用程序,它在 C 中大量使用 OpenSSL。它的设计理念是它的所有 SSL 连接都应该被阻止。 Specifically, blocking BIOs.具体来说,就是阻止 BIO。 They are all allocated off a single incoming port like this:它们都分配在一个单独的传入端口上,如下所示:

ssl = SSL_new(ctx);
SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
sock = BIO_new_socket(socket, BIO_CLOSE);
SSL_set_bio(ssl, sock, sock);

As it turns out though, there are a few small parts of the codebase where using non-blocking BIOs would be the best choice.但事实证明,在代码库的一些小部分中,使用非阻塞 BIO 将是最佳选择。 The small parts that would benefit from the non-blocking BIOs have no way of knowing which SSL connections will belong to them.受益于非阻塞 BIO 的小部分无法知道哪些 SSL 连接将属于它们。 Thus, they always receive blocking BIOs.因此,它们总是接收阻塞 BIO。

The question is, can the blocking BIOs be changed to be non-blocking?问题是,阻塞的 BIO 可以改成非阻塞的吗?

I know that BIO_set_nbio can be used to make a BIO non-blocking but the documentation says:我知道BIO_set_nbio可用于使 BIO 非阻塞,但文档说:

The call to BIO_set_nbio() should be made before the connection is established because non blocking I/O is set during the connect process.应该在建立连接之前调用 BIO_set_nbio(),因为在连接过程中设置了非阻塞 I/O。

Another possible option I have thought about would be to copy the BIO and recreate it, while somehow maintaining all of the state.我想到的另一个可能的选择是复制 BIO 并重新创建它,同时以某种方式保持所有状态。

I did non-blocking SSL connections in my own "lion" code, but I did not use the BIO functionality in OpenSSL at all.我在自己的“狮子”代码中进行了非阻塞 SSL 连接,但我根本没有使用 OpenSSL 中的 BIO 功能。

Rather, I went for the calls SSL_set_fd(ctx, fd ) and SSL_get_fd(ssl) to handle my own fdsets and calling select .相反,我调用SSL_set_fd(ctx, fd )SSL_get_fd(ssl)来处理我自己的 fdsets 并调用select

The biggest 'gotcha' that took a while to track down was to set SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER and SSL_MODE_ENABLE_PARTIAL_WRITE for it work the way I wanted.需要一段时间才能找到的最大“问题”是设置SSL_MODE_ACCEPT_MOVING_WRITE_BUFFERSSL_MODE_ENABLE_PARTIAL_WRITE以使其按我想要的方式工作。

If you want to read the SSL part of the code, it is here:如果你想阅读代码的 SSL 部分,它在这里:

https://github.com/lundman/lion/blob/master/src/tls.c https://github.com/lundman/lion/blob/master/src/tls.c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM