aboutsummaryrefslogtreecommitdiff
path: root/contrib/epee/src/net_ssl.cpp
diff options
context:
space:
mode:
authorxiphon <xiphon@protonmail.com>2019-09-17 22:19:48 +0000
committerxiphon <xiphon@protonmail.com>2019-09-17 22:21:16 +0000
commit4371791977a18653da16a99f41de46cee5fa337b (patch)
treec695c99378d59132102c4526120aed34fe6be844 /contrib/epee/src/net_ssl.cpp
parentMerge pull request #5876 (diff)
downloadmonero-4371791977a18653da16a99f41de46cee5fa337b.tar.xz
epee: implement handshake timeout for SSL connections
Diffstat (limited to '')
-rw-r--r--contrib/epee/src/net_ssl.cpp27
1 files changed, 24 insertions, 3 deletions
diff --git a/contrib/epee/src/net_ssl.cpp b/contrib/epee/src/net_ssl.cpp
index 7d48d2a64..c7dca1914 100644
--- a/contrib/epee/src/net_ssl.cpp
+++ b/contrib/epee/src/net_ssl.cpp
@@ -28,9 +28,11 @@
#include <string.h>
#include <boost/asio/ssl.hpp>
+#include <boost/lambda/lambda.hpp>
#include <openssl/ssl.h>
#include <openssl/pem.h>
#include "misc_log_ex.h"
+#include "net/net_helper.h"
#include "net/net_ssl.h"
#undef MONERO_DEFAULT_LOG_CATEGORY
@@ -456,7 +458,11 @@ bool ssl_options_t::has_fingerprint(boost::asio::ssl::verify_context &ctx) const
return false;
}
-bool ssl_options_t::handshake(boost::asio::ssl::stream<boost::asio::ip::tcp::socket> &socket, boost::asio::ssl::stream_base::handshake_type type, const std::string& host) const
+bool ssl_options_t::handshake(
+ boost::asio::ssl::stream<boost::asio::ip::tcp::socket> &socket,
+ boost::asio::ssl::stream_base::handshake_type type,
+ const std::string& host,
+ std::chrono::milliseconds timeout) const
{
socket.next_layer().set_option(boost::asio::ip::tcp::no_delay(true));
@@ -502,8 +508,23 @@ bool ssl_options_t::handshake(boost::asio::ssl::stream<boost::asio::ip::tcp::soc
});
}
- boost::system::error_code ec;
- socket.handshake(type, ec);
+ auto& io_service = GET_IO_SERVICE(socket);
+ boost::asio::steady_timer deadline(io_service, timeout);
+ deadline.async_wait([&socket](const boost::system::error_code& error) {
+ if (error != boost::asio::error::operation_aborted)
+ {
+ socket.next_layer().close();
+ }
+ });
+
+ boost::system::error_code ec = boost::asio::error::would_block;
+ socket.async_handshake(type, boost::lambda::var(ec) = boost::lambda::_1);
+ while (ec == boost::asio::error::would_block)
+ {
+ io_service.reset();
+ io_service.run_one();
+ }
+
if (ec)
{
MERROR("SSL handshake failed, connection dropped: " << ec.message());