ssl connection fixes

This commit is contained in:
Emiel Bruijntjes 2018-03-08 13:36:56 +01:00
parent 872d4e9a11
commit e39ca5b012
4 changed files with 53 additions and 38 deletions

View File

@ -14,6 +14,7 @@
#include <amqpcpp.h>
#include <amqpcpp/libev.h>
#include <openssl/ssl.h>
#include <openssl/opensslv.h>
/**
* Custom handler
@ -66,7 +67,11 @@ int main()
MyHandler handler(loop);
// init the SSL library
// SSL_library_init();
#if OPENSSL_VERSION_NUMBER < 0x10100000L
SSL_library_init();
#else
OPENSSL_init_ssl(0, NULL);
#endif
// make a connection
AMQP::Address address("amqp://guest:guest@localhost/");

View File

@ -139,11 +139,14 @@ private:
}
else if (_closed)
{
// start the state that closes the connection
auto *nextstate = new SslShutdown(_connection, _socket, std::move(_ssl), _finalized, _handler);
// we forget the current socket to prevent that it gets destructed
_socket = -1;
// start the state that closes the connection
return new SslShutdown(_connection, _socket, std::move(_ssl), _finalized, _handler);
// report the next state
return nextstate;
}
else
{

View File

@ -217,15 +217,15 @@ public:
auto error = OpenSSL::SSL_get_error(_ssl, result);
// check the error
switch (error)
{
// if openssl reports that socket readability or writability is needed,
// we wait for that until this situation is reached
case SSL_ERROR_WANT_READ: wait.readable(); break;
case SSL_ERROR_WANT_WRITE: wait.active(); break;
switch (error) {
// something is wrong, we proceed to the next state
default: return reportError(monitor);
// if openssl reports that socket readability or writability is needed,
// we wait for that until this situation is reached
case SSL_ERROR_WANT_READ: wait.readable(); break;
case SSL_ERROR_WANT_WRITE: wait.active(); break;
// something is wrong, we proceed to the next state
default: return reportError(monitor);
}
}
}

View File

@ -42,6 +42,28 @@ private:
bool _finalized;
/**
* Close the socket
* @return bool
*/
bool close()
{
// skip if already closed
if (_socket < 0) return false;
// we're no longer interested in events
_handler->monitor(_connection, _socket, 0);
// close the socket
::close(_socket);
// forget the socket
_socket = -1;
// done
return true;
}
/**
* Report an error
* @param monitor object to check if connection still exists
@ -50,10 +72,7 @@ private:
TcpState *reporterror(const Monitor &monitor)
{
// close the socket
close(_socket);
// forget the socket
_socket = -1;
close();
// if we have already told user space that connection is gone
if (_finalized) return new TcpClosed(this);
@ -76,14 +95,8 @@ private:
*/
TcpState *proceed(const Monitor &monitor)
{
// we're no longer interested in events
_handler->monitor(_connection, _socket, 0);
// close the socket
close(_socket);
// forget the socket
_socket = -1;
close();
// if we have already told user space that connection is gone
if (_finalized) return new TcpClosed(this);
@ -143,8 +156,8 @@ public:
_socket(socket),
_finalized(finalized)
{
// tell the handler to monitor the socket if there is an out
_handler->monitor(_connection, _socket, readable);
// wait until the socket is accessible
_handler->monitor(_connection, _socket, readable | writable);
}
/**
@ -152,14 +165,8 @@ public:
*/
virtual ~SslShutdown() noexcept
{
// skip if socket is already gond
if (_socket < 0) return;
// we no longer have to monitor the socket
_handler->monitor(_connection, _socket, 0);
// close the socket
close(_socket);
close();
}
/**