gpt4 book ai didi

ssl - 如果 SSL_read() 因 SSL_ERROR_WANT_READ 而被阻止,我可以调用 SSL_write() 吗?

转载 作者:太空宇宙 更新时间:2023-11-03 14:47:17 25 4
gpt4 key购买 nike

我的程序是基于OpenSSL(阻塞模式)开发的,我想将它重构为非阻塞的。它在具有 SSL 对象的线程中调用 SSL_read(),并在具有相同对象的另一个线程中调用 SSL_write()。

我从书"Network Security with OpenSSL"学习了OpenSSL的非阻塞模式, 这种模式有一个例子 (data_transfer)。

for (;;)
{
/* check the readability and writability */
check_availability(A, &can_read_A, &can_write_A, B, &can_read_B, &can_write_B);

/* write_waiton_read_A: SSL_write was blocked for SSL_ERROR_WANT_READ
* write_waiton_write_A: SSL_write was blocked for SSL_ERROR_WANT_WRITE
* read_waiton_write_A: SSL_read was blocked for SSL_ERROR_WANT_WRITE
* read_waiton_read_A: SSL_read was blocked for SSL_ERROR_WANT_READ
*/

/* not in the middle of a write on A */
if (!(write_waiton_read_A || write_waiton_write_A) &&
(A2B_len != BUF_SIZE) &&
/*
* a. A is readable.
* b. the read was blocked for "WANT_WRITE" and A
* is writable now.
*/
(can_read_A || (can_write_A && read_waiton_write_A)))
{
read_waiton_read_A = 0;
read_waiton_write_A = 0;

code = SSL_read(A, A2B + A2B_len, BUF_SIZE - A2B_len);
switch (SSL_get_error(A, code))
{
...
case SSL_ERROR_WANT_READ:
read_waiton_read_A = 1;
break;

case SSL_ERROR_WANT_WRITE:
read_waiton_write_A = 1;
break;
...
}
}

...

/* not in the middle of a read on A */
if (!(read_waiton_write_A || read_waiton_read_A) &&
have_data_B2A &&
/*
* a. A is writable.
* b. the write was blocked for "WANT_READ" and A
* is readable now.
*/
(can_write_A || (can_read_A && write_waiton_read_A)))
{
write_waiton_read_A = 0;
write_waiton_write_A = 0;

code = SSL_write(A, B2A, B2A_len);
switch (SSL_get_error(A, code))
{
...
case SSL_ERROR_WANT_READ:
write_waiton_read_A = 1;
break;

case SSL_ERROR_WANT_WRITE:
write_waiton_write_A = 1;
...
}
}

...
}

我从上面的代码得出结论,如果它因 SSL_ERROR_WANT_READ 而被阻止,我必须等待 SSL_read() 完成,我无法调用 SSL_write(),尽管 A 在这种情况下是可写的。

是吗?我希望我的程序可以同时读取和发送数据,如果 SSL_read() 因 SSL_ERROR_WANT_READ 而被阻止,我可以调用 SSL_write() 吗?

非常感谢任何帮助。非常感谢。

最佳答案

您可以尝试,但您得到的只是再次SSL_WANT_READ

很难理解你为什么要考虑这个。

关于ssl - 如果 SSL_read() 因 SSL_ERROR_WANT_READ 而被阻止,我可以调用 SSL_write() 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37920398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com