简体   繁体   中英

Why does SSL_pending return 0 after SSL_read reads 1,400 bytes?

while(1)
{
    read_blocked_on_write=0;

    const int buff_len = 1024;
    char buff[buff_len];

    iResult = SSL_read(ssl, buff, buff_len);

    int ssl_err = SSL_get_error(ssl, iResult);
    if(ssl_err == SSL_ERROR_NONE)
    {
        if(offset + iResult > recvbuflen - 1)
        {
            FD_ZERO(&fdread);
            FD_ZERO(&fdwrite);
            MessageBox(hwnd, TEXT("ERROR"), TEXT("Not enough memory!"), MB_OK | MB_ICONERROR);
            return 1;
        }
        memcpy(recvbuf + offset, buff, iResult);
        offset += iResult;
        if(SSL_pending(ssl))
        {
            continue;
        }
        else
        {
            bFinish = true;
            break;
        }
    }
    else if(ssl_err == SSL_ERROR_ZERO_RETURN)
    {
        bFinish = true;
        break;
    }
    else if(ssl_err == SSL_ERROR_WANT_READ)
    {
        break;
    }
    else if(ssl_err == SSL_ERROR_WANT_WRITE)
    {
        /* We get a WANT_WRITE if we're
        trying to rehandshake and we block on
        a write during that rehandshake.

        We need to wait on the socket to be 
        writeable but reinitiate the read
        when it is */
        read_blocked_on_write=1;
        break;
    }
    else
    {
        FD_ZERO(&fdread);
        FD_ZERO(&fdwrite);
        MessageBox(hwnd, TEXT("ERROR"), TEXT("SSL problem!"), MB_OK | MB_ICONERROR);
        return 1;
    }
}

I'm no ssl expert but it's likely because there is nothing to read. You are reading and moving a buffer (which takes milliseconds at most) and then terminating if there is nothing more to read at that instant. Meanwhile you are dealing with the much slower network speeds and decryption at the lower layer. It's not at all improbable that there is nothing to be returned at that moment.

Why have that check there at all? Wouldn't alternatively opening the socket as non-blocking be the way to go if you are trying to multiplex or whatever?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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