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