【问题标题】:C - BIO_get_fd() always fails to get a valid FDC - BIO_get_fd() 总是无法获得有效的 FD
【发布时间】:2013-05-20 14:53:54
【问题描述】:

为什么 BIO_get_fd() 总是无法在下面这段代码中获取到有效的 FD?

SSL *pSsl = NULL;

int nRet;
int fdSocket;
fd_set connectionfds;
struct timeval timeout;

BIO_get_ssl(pBio, &pSsl);
if (!pSsl)
    // failed to get SSL pointer of BIO.

SSL_set_mode(pSsl, SSL_MODE_AUTO_RETRY);

BIO_set_conn_hostname(pBio, "HostName");
BIO_set_conn_port(pBio, "PortNumber");

BIO_set_nbio(pBio, 1);

nRet = BIO_do_connect(pBio);

if ((nRet <= 0) && !BIO_should_retry(pBio))
    // failed to establish connection.

if (BIO_get_fd(pBio, &fdSocket) <= 0)
    // failed to get fd but why?

if (nRet <= 0)
{
    FD_ZERO(&connectionfds);
    FD_SET(fdSocket, &connectionfds);

    timeout.tv_usec = 0;
    timeout.tv_sec = 10;

    nRet = select(fdSocket + 1, NULL, &connectionfds, NULL, &timeout);
    if (nRet == 0)
        // timeout has occurred.
}
  • 我的 BIO 对象是通过调用 BIO *BIO_new_ssl_connect(SSL_CTX *ctx) 预先正确创建的
  • 我的 OpenSSL 版本是 v1.0.1e
  • 我在 Ubuntu for ARM 上进行交叉编译

【问题讨论】:

    标签: c ubuntu openssl cross-compiling


    【解决方案1】:

    是我太笨了。 BIO_get_fd() 返回 0 并且 0 是有效的文件描述符!

    BIO_get_fd() 返回套接字,如果 BIO 尚未初始化,则返回 -1。

    所以这一行:

    if (BIO_get_fd(pBio, &fdSocket) <= 0)
    

    应该是

    if (BIO_get_fd(pBio, &fdSocket) < 0)
    

    【讨论】:

      猜你喜欢
      • 2016-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-19
      • 2020-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多