40 PhysicalSocket&& physicalSocket,
41 const std::function<
void(SocketConnection*)>& onDisconnect,
42 const std::string& configuredServer,
43 const SocketAddress& localAddress,
44 const SocketAddress& remoteAddress,
45 const utils::Timeval& readTimeout,
46 const utils::Timeval& writeTimeout,
47 std::size_t readBlockSize,
48 std::size_t writeBlockSize,
49 const utils::Timeval& terminateTimeout)
72 SSL* SocketConnection<PhysicalSocket>::
startSSL(
73 int fd, SSL_CTX* ctx,
const utils::Timeval& sslInitTimeout,
const utils::Timeval& sslShutdownTimeout,
bool closeNotifyIsEOF) {
74 this->sslInitTimeout = sslInitTimeout;
75 this->sslShutdownTimeout = sslShutdownTimeout;
80 SSL_set_ex_data(
ssl, 0,
const_cast<std::string*>(&Super::getConnectionName()));
82 if (SSL_set_fd(
ssl, fd) == 1) {
83 SocketReader::ssl =
ssl;
84 SocketWriter::ssl =
ssl;
85 SocketReader::closeNotifyIsEOF = closeNotifyIsEOF;
86 SocketWriter::closeNotifyIsEOF = closeNotifyIsEOF;
109 bool SocketConnection<PhysicalSocket>::
doSSLHandshake(
const std::function<
void()>& onSuccess,
110 const std::function<
void()>& onTimeout,
111 const std::function<
void(
int)>& onStatus) {
112 if (
ssl !=
nullptr) {
113 if (!SocketReader::isSuspended()) {
114 SocketReader::suspend();
116 if (!SocketWriter::isSuspended()) {
117 SocketWriter::suspend();
120 TLSHandshake::doHandshake(
121 Super::getConnectionName(),
123 [onSuccess,
this]() {
124 SocketReader::span();
130 [onStatus](
int sslErr) {
136 return ssl !=
nullptr;
141 bool resumeSocketReader =
false;
142 bool resumeSocketWriter =
false;
144 if (!SocketReader::isSuspended()) {
145 SocketReader::suspend();
146 resumeSocketReader =
true;
149 if (!SocketWriter::isSuspended()) {
150 SocketWriter::suspend();
151 resumeSocketWriter =
true;
154 TLSShutdown::doShutdown(
155 Super::getConnectionName(),
157 [
this, resumeSocketReader, resumeSocketWriter]() {
158 if (resumeSocketReader) {
159 SocketReader::resume();
161 if (resumeSocketWriter) {
162 SocketWriter::resume();
164 if (SSL_get_shutdown(ssl) == (SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN)) {
165 LOG(DEBUG) << Super::getConnectionName() <<
" SSL/TLS: Close_notify received and sent";
167 LOG(DEBUG) << Super::getConnectionName() <<
" SSL/TLS: Close_notify sent";
169 if (SSL_get_shutdown(ssl) == SSL_SENT_SHUTDOWN && SocketWriter::closeNotifyIsEOF) {
170 LOG(TRACE) << Super::getConnectionName() <<
" SSL/TLS: Close_notify is EOF: setting sslShutdownTimeout to "
171 << sslShutdownTimeout <<
" sec";
172 Super::setTimeout(sslShutdownTimeout);
176 [
this, resumeSocketReader, resumeSocketWriter]() {
177 if (resumeSocketReader) {
178 SocketReader::resume();
180 if (resumeSocketWriter) {
181 SocketWriter::resume();
183 LOG(ERROR) << Super::getConnectionName() <<
" SSL/TLS: Shutdown handshake timed out";
184 Super::doWriteShutdown([
this]() {
185 SocketConnection::close();
188 [
this, resumeSocketReader, resumeSocketWriter](
int sslErr) {
189 if (resumeSocketReader) {
190 SocketReader::resume();
192 if (resumeSocketWriter) {
193 SocketWriter::resume();
195 ssl_log(Super::getConnectionName() +
" SSL/TLS: Shutdown handshake failed", sslErr);
196 Super::doWriteShutdown([
this]() {
197 SocketConnection::close();
205 if ((SSL_get_shutdown(
ssl) & SSL_RECEIVED_SHUTDOWN) != 0) {
206 if ((SSL_get_shutdown(
ssl) & SSL_SENT_SHUTDOWN) != 0) {
207 LOG(DEBUG) << Super::getConnectionName() <<
" SSL/TLS: Close_notify sent and received";
209 SocketWriter::shutdownInProgress =
false;
211 LOG(DEBUG) << Super::getConnectionName() <<
" SSL/TLS: Close_notify received";
216 LOG(ERROR) << Super::getConnectionName() <<
" SSL/TLS: Unexpected EOF error";
218 SocketWriter::shutdownInProgress =
false;
219 SSL_set_shutdown(
ssl, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
SocketConnection(const std::string &instanceName, PhysicalSocket &&physicalSocket, const std::function< void(SocketConnection *)> &onDisconnect, const std::string &configuredServer, const SocketAddress &localAddress, const SocketAddress &remoteAddress, const utils::Timeval &readTimeout, const utils::Timeval &writeTimeout, std::size_t readBlockSize, std::size_t writeBlockSize, const utils::Timeval &terminateTimeout)