|
7 | 7 | #include "base/logger.hpp"
|
8 | 8 | #include "base/configuration.hpp"
|
9 | 9 | #include "base/convert.hpp"
|
| 10 | +#include "base/defer.hpp" |
| 11 | +#include "base/io-engine.hpp" |
10 | 12 | #include <boost/asio/ssl/context.hpp>
|
11 | 13 | #include <boost/asio/ssl/verify_context.hpp>
|
12 | 14 | #include <boost/asio/ssl/verify_mode.hpp>
|
@@ -103,3 +105,65 @@ void UnbufferedAsioTlsStream::BeforeHandshake(handshake_type type)
|
103 | 105 | }
|
104 | 106 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
|
105 | 107 | }
|
| 108 | + |
| 109 | +/** |
| 110 | + * Forcefully close the connection, typically (details are up to the operating system) using a TCP RST. |
| 111 | + */ |
| 112 | +void AsioTlsStream::ForceDisconnect() |
| 113 | +{ |
| 114 | + if (!lowest_layer().is_open()) { |
| 115 | + // Already disconnected, nothing to do. |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + boost::system::error_code ec; |
| 120 | + |
| 121 | + // Close the socket. In case the connection wasn't shut down cleanly by GracefulDisconnect(), the operating system |
| 122 | + // will typically terminate the connection with a TCP RST. Otherwise, this just releases the file descriptor. |
| 123 | + lowest_layer().close(ec); |
| 124 | +} |
| 125 | + |
| 126 | +/** |
| 127 | + * Try to cleanly shut down the connection. This involves sending a TLS close_notify shutdown alert and terminating the |
| 128 | + * underlying TCP connection. Sending these additional messages can block, hence the method takes a yield context and |
| 129 | + * internally implements a timeout of 10 seconds for the operation after which the connection is forcefully terminated |
| 130 | + * using ForceDisconnect(). |
| 131 | + * |
| 132 | + * @param strand Asio strand used for other operations on this connection. |
| 133 | + * @param yc Yield context for Asio coroutines |
| 134 | + */ |
| 135 | +void AsioTlsStream::GracefulDisconnect(boost::asio::io_context::strand& strand, boost::asio::yield_context& yc) |
| 136 | +{ |
| 137 | + if (!lowest_layer().is_open()) { |
| 138 | + // Already disconnected, nothing to do. |
| 139 | + return; |
| 140 | + } |
| 141 | + |
| 142 | + { |
| 143 | + Timeout::Ptr shutdownTimeout(new Timeout(strand.context(), strand, boost::posix_time::seconds(10), |
| 144 | + [this](boost::asio::yield_context yc) { |
| 145 | + // Forcefully terminate the connection if async_shutdown() blocked more than 10 seconds. |
| 146 | + ForceDisconnect(); |
| 147 | + } |
| 148 | + )); |
| 149 | + Defer cancelTimeout ([&shutdownTimeout]() { |
| 150 | + shutdownTimeout->Cancel(); |
| 151 | + }); |
| 152 | + |
| 153 | + // Close the TLS connection, effectively uses SSL_shutdown() to send a close_notify shutdown alert to the peer. |
| 154 | + boost::system::error_code ec; |
| 155 | + next_layer().async_shutdown(yc[ec]); |
| 156 | + } |
| 157 | + |
| 158 | + if (!lowest_layer().is_open()) { |
| 159 | + // Connection got closed in the meantime, most likely by the timeout, so nothing more to do. |
| 160 | + return; |
| 161 | + } |
| 162 | + |
| 163 | + // Shut down the TCP connection. |
| 164 | + boost::system::error_code ec; |
| 165 | + lowest_layer().shutdown(lowest_layer_type::shutdown_both, ec); |
| 166 | + |
| 167 | + // Clean up the connection (closes the file descriptor). |
| 168 | + ForceDisconnect(); |
| 169 | +} |
0 commit comments