【问题标题】:IMAP client using OpenSSL library使用 OpenSSL 库的 IMAP 客户端
【发布时间】:2014-10-20 09:21:35
【问题描述】:

我正在尝试编写一个简单的 SSL IMAP 客户端。为此,我正在使用 OpenSSL 库和 C++(g++ 编译器)。我找到了很多关于 IMAP 的信息,我可以成功地与 IMAP 服务器通信。但我注意到,当没有数据接收时, BIO_read() 会永远阻塞。我还通过查看this 示例(我没有使用证书验证)尝试在非阻塞(?)模式下使用 SSL_read() 并遇到同样的问题。

简化的源代码:

<...>
BIO *bio;
SSL *ssl;
SSL_CTX *ctx;

CRYPTO_malloc_init(); // Initialize malloc, free, etc for OpenSSL's use
SSL_library_init(); // Initialize OpenSSL's SSL libraries
SSL_load_error_strings(); // Load SSL error strings
ERR_load_BIO_strings(); // Load BIO error strings
OpenSSL_add_all_algorithms(); // Load all available encryption algorithms

ctx = SSL_CTX_new(SSLv23_client_method());
bio = BIO_new_ssl_connect(ctx);
BIO_get_ssl(bio, &ssl);
SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);

BIO_set_conn_hostname(bio, "imap.gmail.com:993");
BIO_do_connect(bio);

char tmp[10000];
cout << "Bytes received: " << BIO_read(bio, tmp, 1000) << endl; //Got 68 bytes - full anwer (server welcome string)
cout << "Bytes received: " << BIO_read(bio, tmp, 1000) << endl; //No more data to receive - I expecting 0 (zero), but it blocks forever.
cout << "Bytes received: " << BIO_read(bio, tmp, 1000) << endl;

There is full source code(见第 49 行)


P。 S.:对不起我的英语不好,希望大家能理解。

【问题讨论】:

    标签: openssl imap


    【解决方案1】:

    尝试使用BIO_pending() 确定是否应该执行另一个 BIO_read。

    类似:

    while (bytes_remaining = BIO_pending(bio))
    cout << "Bytes received: " << BIO_read(bio, tmp, bytes_remaining) << endl;
    

    可能对你有用。

    【讨论】:

      猜你喜欢
      • 2019-10-13
      • 2011-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-08
      • 1970-01-01
      • 2013-06-06
      • 1970-01-01
      相关资源
      最近更新 更多