如果SSL_ERROR_WANT_READ阻止了SSL_read(),我可以调用SSL_write()吗?

时间:2016-06-20 10:39:43

标签: ssl openssl nonblocking

我的程序是基于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_read()被SSL_ERROR_WANT_READ阻止,我必须等待SSL_read()完成,虽然在这种情况下A是可写的,但我无法调用SSL_write()。

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

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

1 个答案:

答案 0 :(得分:0)

你可以试试,但你会得到的只是SSL_WANT_READ

很难理解为什么你甚至在考虑这个问题。