Chris@16: // Chris@16: // ssl/detail/openssl_init.hpp Chris@16: // ~~~~~~~~~~~~~~~~~~~~~~~~~~~ Chris@16: // Chris@101: // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com) Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: Chris@16: #ifndef BOOST_ASIO_SSL_DETAIL_OPENSSL_INIT_HPP Chris@16: #define BOOST_ASIO_SSL_DETAIL_OPENSSL_INIT_HPP Chris@16: Chris@16: #if defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: # pragma once Chris@16: #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace asio { Chris@16: namespace ssl { Chris@16: namespace detail { Chris@16: Chris@16: class openssl_init_base Chris@16: : private noncopyable Chris@16: { Chris@16: protected: Chris@16: // Class that performs the actual initialisation. Chris@16: class do_init; Chris@16: Chris@16: // Helper function to manage a do_init singleton. The static instance of the Chris@16: // openssl_init object ensures that this function is always called before Chris@16: // main, and therefore before any other threads can get started. The do_init Chris@16: // instance must be static in this function to ensure that it gets Chris@16: // initialised before any other global objects try to use it. Chris@16: BOOST_ASIO_DECL static boost::asio::detail::shared_ptr instance(); Chris@16: Chris@16: #if !defined(SSL_OP_NO_COMPRESSION) \ Chris@16: && (OPENSSL_VERSION_NUMBER >= 0x00908000L) Chris@16: // Get an empty stack of compression methods, to be used when disabling Chris@16: // compression. Chris@16: BOOST_ASIO_DECL static STACK_OF(SSL_COMP)* get_null_compression_methods(); Chris@16: #endif // !defined(SSL_OP_NO_COMPRESSION) Chris@16: // && (OPENSSL_VERSION_NUMBER >= 0x00908000L) Chris@16: }; Chris@16: Chris@16: template Chris@16: class openssl_init : private openssl_init_base Chris@16: { Chris@16: public: Chris@16: // Constructor. Chris@16: openssl_init() Chris@16: : ref_(instance()) Chris@16: { Chris@16: using namespace std; // For memmove. Chris@16: Chris@16: // Ensure openssl_init::instance_ is linked in. Chris@16: openssl_init* tmp = &instance_; Chris@16: memmove(&tmp, &tmp, sizeof(openssl_init*)); Chris@16: } Chris@16: Chris@16: // Destructor. Chris@16: ~openssl_init() Chris@16: { Chris@16: } Chris@16: Chris@16: #if !defined(SSL_OP_NO_COMPRESSION) \ Chris@16: && (OPENSSL_VERSION_NUMBER >= 0x00908000L) Chris@16: using openssl_init_base::get_null_compression_methods; Chris@16: #endif // !defined(SSL_OP_NO_COMPRESSION) Chris@16: // && (OPENSSL_VERSION_NUMBER >= 0x00908000L) Chris@16: Chris@16: private: Chris@16: // Instance to force initialisation of openssl at global scope. Chris@16: static openssl_init instance_; Chris@16: Chris@16: // Reference to singleton do_init object to ensure that openssl does not get Chris@16: // cleaned up until the last user has finished with it. Chris@16: boost::asio::detail::shared_ptr ref_; Chris@16: }; Chris@16: Chris@16: template Chris@16: openssl_init openssl_init::instance_; Chris@16: Chris@16: } // namespace detail Chris@16: } // namespace ssl Chris@16: } // namespace asio Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #if defined(BOOST_ASIO_HEADER_ONLY) Chris@16: # include Chris@16: #endif // defined(BOOST_ASIO_HEADER_ONLY) Chris@16: Chris@16: #endif // BOOST_ASIO_SSL_DETAIL_OPENSSL_INIT_HPP