C++ SSL关闭,检查错误代码并考虑挂起的数据
我希望确保SSL连接正确关闭。从中,我发现了一个代码片段,用于区分正常关机和短读错误:C++ SSL关闭,检查错误代码并考虑挂起的数据,c++,ssl,boost,openssl,boost-asio,C++,Ssl,Boost,Openssl,Boost Asio,我希望确保SSL连接正确关闭。从中,我发现了一个代码片段,用于区分正常关机和短读错误: // const boost::system::error_code &ec if (ec.category() == asio::error::get_ssl_category() && ec.value() == ERR_PACK(ERR_LIB_SSL, 0, SSL_R_SHORT_READ)) { // -> not a real error, just a n
// const boost::system::error_code &ec
if (ec.category() == asio::error::get_ssl_category() &&
ec.value() == ERR_PACK(ERR_LIB_SSL, 0, SSL_R_SHORT_READ)) {
// -> not a real error, just a normal TLS shutdown
}
鉴于以下代码,上述代码有意义:
在上面的代码中,错误被重新映射,然后我专门检查了这一点。但是在阅读了上面的函数之后,我并不感到安心
似乎两者都是:
(要读取的数据),以及if(BIO_wpending(ext_BIO_))
(协商正确关闭)如果(::SSL\u获取\u关闭(SSL\u)和SSL\u接收\u关闭)==0)
如果
(BIO_wpending(ext_BIO_))
为真,我的错误检查是否会遗漏错误?我不知道这张支票到底在看什么。这有关系吗?这个答案很好地说明了这一点。还可以看到这个问题:因为行为可能已经改变了。。。
const boost::system::error_code& engine::map_error_code(
boost::system::error_code& ec) const
{
// We only want to map the error::eof code.
if (ec != boost::asio::error::eof)
return ec;
// If there's data yet to be read, it's an error.
if (BIO_wpending(ext_bio_))
{
ec = boost::system::error_code(
ERR_PACK(ERR_LIB_SSL, 0, SSL_R_SHORT_READ),
boost::asio::error::get_ssl_category());
return ec;
}
// SSL v2 doesn't provide a protocol-level shutdown, so an eof on the
// underlying transport is passed through.
if (ssl_->version == SSL2_VERSION)
return ec;
// Otherwise, the peer should have negotiated a proper shutdown.
if ((::SSL_get_shutdown(ssl_) & SSL_RECEIVED_SHUTDOWN) == 0)
{
ec = boost::system::error_code(
ERR_PACK(ERR_LIB_SSL, 0, SSL_R_SHORT_READ),
boost::asio::error::get_ssl_category());
}
return ec;
}