added error in case openssl is missing

This commit is contained in:
Emiel Bruijntjes 2018-03-06 18:07:34 +01:00
parent 25d5410b13
commit 0ca9bc9dad
4 changed files with 20 additions and 8 deletions

View File

@ -4,7 +4,7 @@
* Test program to check AMQP functionality based on LibEV * Test program to check AMQP functionality based on LibEV
* *
* @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
* @copyright 2015 - 2017 Copernica BV * @copyright 2015 - 2018 Copernica BV
*/ */
/** /**
@ -13,6 +13,7 @@
#include <ev.h> #include <ev.h>
#include <amqpcpp.h> #include <amqpcpp.h>
#include <amqpcpp/libev.h> #include <amqpcpp/libev.h>
#include <openssl/ssl.h>
/** /**
* Custom handler * Custom handler
@ -65,7 +66,7 @@ int main()
MyHandler handler(loop); MyHandler handler(loop);
// init the SSL library // init the SSL library
SSL_library_init(); // SSL_library_init();
// make a connection // make a connection
AMQP::Address address("amqps://guest:guest@localhost/"); AMQP::Address address("amqps://guest:guest@localhost/");

View File

@ -22,10 +22,16 @@
/** /**
* Begin of namespace * Begin of namespace
*/ */
namespace Copernica { namespace OpenSSL { namespace AMQP { namespace OpenSSL {
/** /**
* List of all methods that we need * Function to check if openssl is loaded
* @return bool
*/
bool valid();
/**
* List of all wrapper methods that are in use inside AMQP-CPP
*/ */
SSL_CTX *SSL_CTX_new(const SSL_METHOD *method); SSL_CTX *SSL_CTX_new(const SSL_METHOD *method);
SSL *SSL_new(SSL_CTX *ctx); SSL *SSL_new(SSL_CTX *ctx);

View File

@ -158,7 +158,7 @@ public:
if (fd != _socket) return this; if (fd != _socket) return this;
// start the ssl handshake // start the ssl handshake
int result = SSL_do_handshake(_ssl); int result = OpenSSL::SSL_do_handshake(_ssl);
// if the connection succeeds, we can move to the ssl-connected state // if the connection succeeds, we can move to the ssl-connected state
if (result == 1) return nextstate(new SslConnected(_connection, _socket, _ssl, std::move(_out), _handler)); if (result == 1) return nextstate(new SslConnected(_connection, _socket, _ssl, std::move(_out), _handler));

View File

@ -20,7 +20,8 @@
#include "tcpstate.h" #include "tcpstate.h"
#include "tcpclosed.h" #include "tcpclosed.h"
#include "tcpconnected.h" #include "tcpconnected.h"
//#include "sslhandshake.h" #include "openssl.h"
#include "sslhandshake.h"
#include <thread> #include <thread>
/** /**
@ -91,6 +92,9 @@ private:
// prevent exceptions // prevent exceptions
try try
{ {
// check if we support openssl in the first place
if (!OpenSSL::valid()) throw std::runtime_error("Secure connection cannot be established: the application has no access to openssl");
// get address info // get address info
AddressInfo addresses(_hostname.data(), _port); AddressInfo addresses(_hostname.data(), _port);
@ -190,7 +194,8 @@ public:
if (_socket >= 0) if (_socket >= 0)
{ {
// if we need a secure connection, we move to the tls handshake // if we need a secure connection, we move to the tls handshake
//if (_secure) return new SslHandshake(_connection, _socket, _hostname, std::move(_buffer), _handler); // @todo catch exception
if (_secure) return new SslHandshake(_connection, _socket, _hostname, std::move(_buffer), _handler);
// otherwise we have a valid regular tcp connection // otherwise we have a valid regular tcp connection
return new TcpConnected(_connection, _socket, std::move(_buffer), _handler); return new TcpConnected(_connection, _socket, std::move(_buffer), _handler);