【问题标题】:Set connection timeout for SSL_read in OpenSSL in C++在 C++ 中的 OpenSSL 中为 SSL_read 设置连接超时
【发布时间】:2019-08-31 22:43:06
【问题描述】:

我正在开发一个与 OpenSSL 通信的 linux 应用程序。我目前正在运行一些稳健性测试,其中一个让我很难受。

当我的程序正在下载一个大文件时,我拔掉了以太网电缆,我希望它在例如 30 秒后停止。但它永远不会停止。

我使用 SSL_read,这就是它阻塞的地方:

count = SSL_read(ssl, buffer, BUFSIZE);

是否可以为 SSL_read 设置超时?

我尝试过SSL_CTX_set_timeout(),但它不起作用。我还看到可能可以使用select(),但我不明白如何将它与SSL_read() 一起使用

【问题讨论】:

    标签: c++ ssl timeout


    【解决方案1】:

    您可以像使用普通套接字一样执行此操作。基本上,您在套接字上设置 timeval 并且在 ssl 代码中,当 timeval 中设置的时间过去时,SSL_read 将返回 -1。

    int fd, new_fd;
    struct timeval tv;
    char buffer[1024];
    
    // ...
    
    tv.tv_sec = 5; // 5 seconds
    tv.tv_usec = 0;
    setsockopt(new_fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv);
    
    // ...
    
    // this will return -1 if nothing is received after 5 seconds
    SSL_read(ssl, buffer, sizeof buffer); 
    

    【讨论】:

      猜你喜欢
      • 2013-03-28
      • 2014-08-17
      • 1970-01-01
      • 2017-03-24
      • 2012-06-14
      • 2015-09-16
      • 1970-01-01
      • 2017-03-17
      • 2015-08-14
      相关资源
      最近更新 更多