From 524b9991dddf432163cb0f2075c07833e8ab5330 Mon Sep 17 00:00:00 2001 From: Vova Mshanetskiy Date: Mon, 8 Aug 2016 16:46:17 +0300 Subject: [PATCH] Fixed possible exception in async_connection under port scanning (#677) --- .../protocol/http/server/async_connection.hpp | 17 ++++++++++++----- boost/network/protocol/stream_handler.hpp | 15 ++++++++++++--- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/boost/network/protocol/http/server/async_connection.hpp b/boost/network/protocol/http/server/async_connection.hpp index d73dbff9f..2634afb62 100644 --- a/boost/network/protocol/http/server/async_connection.hpp +++ b/boost/network/protocol/http/server/async_connection.hpp @@ -462,11 +462,18 @@ struct async_connection enum state_t { method, uri, version, headers }; void start() { - typename ostringstream::type ip_stream; - ip_stream << socket_.remote_endpoint().address().to_string() << ':' - << socket_.remote_endpoint().port(); - request_.source = ip_stream.str(); - read_more(method); + boost::system::error_code ec; + auto remote_endpoint = socket_.remote_endpoint(ec); + + if (ec) { + error_encountered = in_place(ec); + } else { + typename ostringstream::type ip_stream; + ip_stream << remote_endpoint.address().to_string() << ':' + << remote_endpoint.port(); + request_.source = ip_stream.str(); + read_more(method); + } } void read_more(state_t state) { diff --git a/boost/network/protocol/stream_handler.hpp b/boost/network/protocol/stream_handler.hpp index 7af02f3a1..2231af0cf 100644 --- a/boost/network/protocol/stream_handler.hpp +++ b/boost/network/protocol/stream_handler.hpp @@ -103,12 +103,21 @@ struct stream_handler { } } - tcp_socket::endpoint_type remote_endpoint() const { + tcp_socket::endpoint_type remote_endpoint(boost::system::error_code& ec) const { if (ssl_enabled) { - return ssl_sock_->next_layer().remote_endpoint(); + return ssl_sock_->next_layer().remote_endpoint(ec); } else { - return tcp_sock_->remote_endpoint(); + return tcp_sock_->remote_endpoint(ec); + } + } + + tcp_socket::endpoint_type remote_endpoint() const { + boost::system::error_code ec; + tcp_socket::endpoint_type r = remote_endpoint(ec); + if (ec) { + boost::asio::detail::throw_error(ec, "remote_endpoint"); } + return r; } void shutdown(boost::asio::socket_base::shutdown_type st,