openssl_init.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // ssl/detail/openssl_init.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2021 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_SSL_DETAIL_OPENSSL_INIT_HPP
  11. #define BOOST_ASIO_SSL_DETAIL_OPENSSL_INIT_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #include <cstring>
  17. #include <boost/asio/detail/memory.hpp>
  18. #include <boost/asio/detail/noncopyable.hpp>
  19. #include <boost/asio/ssl/detail/openssl_types.hpp>
  20. #include <boost/asio/detail/push_options.hpp>
  21. namespace boost {
  22. namespace asio {
  23. namespace ssl {
  24. namespace detail {
  25. class openssl_init_base
  26. : private noncopyable
  27. {
  28. protected:
  29. // Class that performs the actual initialisation.
  30. class do_init;
  31. // Helper function to manage a do_init singleton. The static instance of the
  32. // openssl_init object ensures that this function is always called before
  33. // main, and therefore before any other threads can get started. The do_init
  34. // instance must be static in this function to ensure that it gets
  35. // initialised before any other global objects try to use it.
  36. BOOST_ASIO_DECL static boost::asio::detail::shared_ptr<do_init> instance();
  37. #if !defined(SSL_OP_NO_COMPRESSION) \
  38. && (OPENSSL_VERSION_NUMBER >= 0x00908000L)
  39. // Get an empty stack of compression methods, to be used when disabling
  40. // compression.
  41. BOOST_ASIO_DECL static STACK_OF(SSL_COMP)* get_null_compression_methods();
  42. #endif // !defined(SSL_OP_NO_COMPRESSION)
  43. // && (OPENSSL_VERSION_NUMBER >= 0x00908000L)
  44. };
  45. template <bool Do_Init = true>
  46. class openssl_init : private openssl_init_base
  47. {
  48. public:
  49. // Constructor.
  50. openssl_init()
  51. : ref_(instance())
  52. {
  53. using namespace std; // For memmove.
  54. // Ensure openssl_init::instance_ is linked in.
  55. openssl_init* tmp = &instance_;
  56. memmove(&tmp, &tmp, sizeof(openssl_init*));
  57. }
  58. // Destructor.
  59. ~openssl_init()
  60. {
  61. }
  62. #if !defined(SSL_OP_NO_COMPRESSION) \
  63. && (OPENSSL_VERSION_NUMBER >= 0x00908000L)
  64. using openssl_init_base::get_null_compression_methods;
  65. #endif // !defined(SSL_OP_NO_COMPRESSION)
  66. // && (OPENSSL_VERSION_NUMBER >= 0x00908000L)
  67. private:
  68. // Instance to force initialisation of openssl at global scope.
  69. static openssl_init instance_;
  70. // Reference to singleton do_init object to ensure that openssl does not get
  71. // cleaned up until the last user has finished with it.
  72. boost::asio::detail::shared_ptr<do_init> ref_;
  73. };
  74. template <bool Do_Init>
  75. openssl_init<Do_Init> openssl_init<Do_Init>::instance_;
  76. } // namespace detail
  77. } // namespace ssl
  78. } // namespace asio
  79. } // namespace boost
  80. #include <boost/asio/detail/pop_options.hpp>
  81. #if defined(BOOST_ASIO_HEADER_ONLY)
  82. # include <boost/asio/ssl/detail/impl/openssl_init.ipp>
  83. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  84. #endif // BOOST_ASIO_SSL_DETAIL_OPENSSL_INIT_HPP