Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:01

0001 //
0002 // ssl/detail/openssl_init.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 
0011 #ifndef BOOST_ASIO_SSL_DETAIL_OPENSSL_INIT_HPP
0012 #define BOOST_ASIO_SSL_DETAIL_OPENSSL_INIT_HPP
0013 
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0017 
0018 #include <boost/asio/detail/config.hpp>
0019 #include <cstring>
0020 #include <boost/asio/detail/memory.hpp>
0021 #include <boost/asio/detail/noncopyable.hpp>
0022 #include <boost/asio/ssl/detail/openssl_types.hpp>
0023 
0024 #include <boost/asio/detail/push_options.hpp>
0025 
0026 namespace boost {
0027 namespace asio {
0028 namespace ssl {
0029 namespace detail {
0030 
0031 class openssl_init_base
0032   : private noncopyable
0033 {
0034 protected:
0035   // Class that performs the actual initialisation.
0036   class do_init;
0037 
0038   // Helper function to manage a do_init singleton. The static instance of the
0039   // openssl_init object ensures that this function is always called before
0040   // main, and therefore before any other threads can get started. The do_init
0041   // instance must be static in this function to ensure that it gets
0042   // initialised before any other global objects try to use it.
0043   BOOST_ASIO_DECL static boost::asio::detail::shared_ptr<do_init> instance();
0044 
0045 #if !defined(SSL_OP_NO_COMPRESSION) \
0046   && (OPENSSL_VERSION_NUMBER >= 0x00908000L)
0047   // Get an empty stack of compression methods, to be used when disabling
0048   // compression.
0049   BOOST_ASIO_DECL static STACK_OF(SSL_COMP)* get_null_compression_methods();
0050 #endif // !defined(SSL_OP_NO_COMPRESSION)
0051        // && (OPENSSL_VERSION_NUMBER >= 0x00908000L)
0052 };
0053 
0054 template <bool Do_Init = true>
0055 class openssl_init : private openssl_init_base
0056 {
0057 public:
0058   // Constructor.
0059   openssl_init()
0060     : ref_(instance())
0061   {
0062     using namespace std; // For memmove.
0063 
0064     // Ensure openssl_init::instance_ is linked in.
0065     openssl_init* tmp = &instance_;
0066     memmove(&tmp, &tmp, sizeof(openssl_init*));
0067   }
0068 
0069   // Destructor.
0070   ~openssl_init()
0071   {
0072   }
0073 
0074 #if !defined(SSL_OP_NO_COMPRESSION) \
0075   && (OPENSSL_VERSION_NUMBER >= 0x00908000L)
0076   using openssl_init_base::get_null_compression_methods;
0077 #endif // !defined(SSL_OP_NO_COMPRESSION)
0078        // && (OPENSSL_VERSION_NUMBER >= 0x00908000L)
0079 
0080 private:
0081   // Instance to force initialisation of openssl at global scope.
0082   static openssl_init instance_;
0083 
0084   // Reference to singleton do_init object to ensure that openssl does not get
0085   // cleaned up until the last user has finished with it.
0086   boost::asio::detail::shared_ptr<do_init> ref_;
0087 };
0088 
0089 template <bool Do_Init>
0090 openssl_init<Do_Init> openssl_init<Do_Init>::instance_;
0091 
0092 } // namespace detail
0093 } // namespace ssl
0094 } // namespace asio
0095 } // namespace boost
0096 
0097 #include <boost/asio/detail/pop_options.hpp>
0098 
0099 #if defined(BOOST_ASIO_HEADER_ONLY)
0100 # include <boost/asio/ssl/detail/impl/openssl_init.ipp>
0101 #endif // defined(BOOST_ASIO_HEADER_ONLY)
0102 
0103 #endif // BOOST_ASIO_SSL_DETAIL_OPENSSL_INIT_HPP