为什么 SSL_read 读取 1,400 字节后 SSL_pending 返回 0?

发布于 2024-12-02 23:08:42 字数 1440 浏览 1 评论 0原文

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;
    }
}
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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慕巷 2024-12-09 23:08:42

我不是 ssl 专家,但这可能是因为没有什么可读的。您正在读取并移动缓冲区(最多需要几毫秒),然后如果此时没有更多内容可读取,则终止。与此同时,您正在处理较慢的网络速度和较低层的解密。那一刻没有什么可归还的,也不是没有可能。

为什么要在那里进行检查?如果您尝试进行多路复用或其他操作,那么以非阻塞方式打开套接字难道不是一种可行的方法吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文