Commit f353b2e3 authored by Paul Lensing's avatar Paul Lensing
Browse files

Only initialize SSL once.

Otherwise multithreaded programs may crash.
parent ad398d5a
Loading
Loading
Loading
Loading
+17 −9
Original line number Diff line number Diff line
@@ -32,18 +32,26 @@ namespace kinetic {

using std::string;

SocketWrapper::SocketWrapper(const std::string& host, int port, bool use_ssl, bool nonblocking)
        : ctx_(NULL), ssl_(NULL), host_(host), port_(port), nonblocking_(nonblocking), fd_(-1) {
    if(!use_ssl) return;

class OpenSSLInitializer{
public:
    OpenSSLInitializer() {
      SSL_library_init();
      OpenSSL_add_all_algorithms();
    }
};
static OpenSSLInitializer init;

SocketWrapper::SocketWrapper(const std::string& host, int port, bool use_ssl, bool nonblocking)
        : ctx_(NULL), ssl_(NULL), host_(host), port_(port), nonblocking_(nonblocking), fd_(-1) {
    if(use_ssl) {
        ctx_ = SSL_CTX_new(SSLv23_client_method());
        ssl_ = SSL_new(ctx_);
    if(!ssl_ || !ctx_)
        if(!ssl_ || !ctx_) {
            throw std::runtime_error("Failed Setting up SSL environment.");
        }
        SSL_set_mode(ssl_, SSL_MODE_AUTO_RETRY);
    }
}

SocketWrapper::~SocketWrapper() {
    if (fd_ == -1) {