Skip to content

Commit 3838d66

Browse files
committed
improved error handling, specifically socket timeouts
1 parent 8b456cd commit 3838d66

1 file changed

Lines changed: 41 additions & 12 deletions

File tree

NetSSL_OpenSSL/src/SecureSocketImpl.cpp

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// SecureSocketImpl.cpp
33
//
4-
// $Id: //poco/1.4/NetSSL_OpenSSL/src/SecureSocketImpl.cpp#5 $
4+
// $Id: //poco/1.4/NetSSL_OpenSSL/src/SecureSocketImpl.cpp#11 $
55
//
66
// Library: NetSSL_OpenSSL
77
// Package: SSLSockets
@@ -146,7 +146,7 @@ void SecureSocketImpl::connect(const SocketAddress& address, const Poco::Timespa
146146
_pSocket->setSendTimeout(timeout);
147147
connectSSL(performHandshake);
148148
_pSocket->setReceiveTimeout(receiveTimeout);
149-
_pSocket->setSendTimeout(sendTimeout);
149+
_pSocket->setSendTimeout(sendTimeout);
150150
}
151151

152152

@@ -155,6 +155,7 @@ void SecureSocketImpl::connectNB(const SocketAddress& address)
155155
if (_pSSL) reset();
156156

157157
poco_assert (!_pSSL);
158+
158159
_pSocket->connectNB(address);
159160
connectSSL(false);
160161
}
@@ -231,12 +232,12 @@ void SecureSocketImpl::listen(int backlog)
231232
void SecureSocketImpl::shutdown()
232233
{
233234
if (_pSSL)
234-
{
235-
// Don't shut down the socket more than once.
236-
int shutdownState = SSL_get_shutdown(_pSSL);
237-
bool shutdownSent = (shutdownState & SSL_SENT_SHUTDOWN) == SSL_SENT_SHUTDOWN;
238-
if (!shutdownSent)
239-
{
235+
{
236+
// Don't shut down the socket more than once.
237+
int shutdownState = SSL_get_shutdown(_pSSL);
238+
bool shutdownSent = (shutdownState & SSL_SENT_SHUTDOWN) == SSL_SENT_SHUTDOWN;
239+
if (!shutdownSent)
240+
{
240241
// A proper clean shutdown would require us to
241242
// retry the shutdown if we get a zero return
242243
// value, until SSL_shutdown() returns 1.
@@ -246,15 +247,24 @@ void SecureSocketImpl::shutdown()
246247
// done with it.
247248
int rc = SSL_shutdown(_pSSL);
248249
if (rc < 0) handleError(rc);
249-
if (_pSocket->getBlocking()) _pSocket->shutdown();
250+
if (_pSocket->getBlocking())
251+
{
252+
_pSocket->shutdown();
253+
}
250254
}
251255
}
252256
}
253257

254258

255259
void SecureSocketImpl::close()
256260
{
257-
try { shutdown(); } catch (...) { }
261+
try
262+
{
263+
shutdown();
264+
}
265+
catch (...)
266+
{
267+
}
258268
_pSocket->close();
259269
}
260270

@@ -403,12 +413,21 @@ int SecureSocketImpl::handleError(int rc)
403413
{
404414
if (rc > 0) return rc;
405415

406-
int sslError = SSL_get_error(_pSSL, rc);
416+
int sslError = SSL_get_error(_pSSL, rc);
417+
int error = SocketImpl::lastError();
418+
407419
switch (sslError)
408420
{
409421
case SSL_ERROR_ZERO_RETURN:
410422
return 0;
411423
case SSL_ERROR_WANT_READ:
424+
if (_pSocket->getBlocking() && error != 0)
425+
{
426+
if (error == POCO_EAGAIN)
427+
throw TimeoutException(error);
428+
else
429+
SocketImpl::error(error);
430+
}
412431
return SecureStreamSocket::ERR_SSL_WANT_READ;
413432
case SSL_ERROR_WANT_WRITE:
414433
return SecureStreamSocket::ERR_SSL_WANT_WRITE;
@@ -418,12 +437,22 @@ int SecureSocketImpl::handleError(int rc)
418437
// these should not occur
419438
poco_bugcheck();
420439
return rc;
440+
case SSL_ERROR_SYSCALL:
441+
if (error != 0)
442+
{
443+
if (_pSocket->getBlocking() && error == POCO_EAGAIN)
444+
throw TimeoutException(error);
445+
else
446+
SocketImpl::error(error);
447+
return rc;
448+
}
449+
// fallthrough
421450
default:
422451
{
423452
long lastError = ERR_get_error();
424453
if (lastError == 0)
425454
{
426-
if (rc == 0)
455+
if (rc == 0 || rc == -1)
427456
{
428457
throw SSLConnectionUnexpectedlyClosedException();
429458
}

0 commit comments

Comments
 (0)