5
// Copyright (c) 2005 Voipster / Indrek dot Juhani at voipster dot com
6
// Copyright (c) 2005-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
8
// Distributed under the Boost Software License, Version 1.0. (See accompanying
9
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
12
#ifndef ASIO_SSL_DETAIL_OPENSSL_INIT_HPP
13
#define ASIO_SSL_DETAIL_OPENSSL_INIT_HPP
15
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
17
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
19
#include "asio/detail/push_options.hpp"
21
#include "asio/detail/push_options.hpp"
23
#include <boost/assert.hpp>
24
#include <boost/config.hpp>
25
#include <boost/shared_ptr.hpp>
26
#include "asio/detail/pop_options.hpp"
28
#include "asio/detail/mutex.hpp"
29
#include "asio/detail/tss_ptr.hpp"
30
#include "asio/ssl/detail/openssl_types.hpp"
36
template <bool Do_Init = true>
38
: private boost::noncopyable
41
// Structure to perform the actual initialisation.
50
::SSL_load_error_strings();
51
::OpenSSL_add_ssl_algorithms();
53
mutexes_.resize(::CRYPTO_num_locks());
54
for (size_t i = 0; i < mutexes_.size(); ++i)
55
mutexes_[i].reset(new asio::detail::mutex);
56
::CRYPTO_set_locking_callback(&do_init::openssl_locking_func);
57
::CRYPTO_set_id_callback(&do_init::openssl_id_func);
65
::CRYPTO_set_id_callback(0);
66
::CRYPTO_set_locking_callback(0);
68
::ERR_remove_state(0);
70
::CRYPTO_cleanup_all_ex_data();
71
::CONF_modules_unload(1);
76
// Helper function to manage a do_init singleton. The static instance of the
77
// openssl_init object ensures that this function is always called before
78
// main, and therefore before any other threads can get started. The do_init
79
// instance must be static in this function to ensure that it gets
80
// initialised before any other global objects try to use it.
81
static boost::shared_ptr<do_init> instance()
83
static boost::shared_ptr<do_init> init(new do_init);
88
static unsigned long openssl_id_func()
90
#if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
91
return ::GetCurrentThreadId();
92
#else // defined(BOOST_WINDOWS) || defined(__CYGWIN__)
93
void* id = instance()->thread_id_;
95
instance()->thread_id_ = id = &id; // Ugh.
96
BOOST_ASSERT(sizeof(unsigned long) >= sizeof(void*));
97
return reinterpret_cast<unsigned long>(id);
98
#endif // defined(BOOST_WINDOWS) || defined(__CYGWIN__)
101
static void openssl_locking_func(int mode, int n,
102
const char *file, int line)
104
if (mode & CRYPTO_LOCK)
105
instance()->mutexes_[n]->lock();
107
instance()->mutexes_[n]->unlock();
110
// Mutexes to be used in locking callbacks.
111
std::vector<boost::shared_ptr<asio::detail::mutex> > mutexes_;
113
#if !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
114
// The thread identifiers to be used by openssl.
115
asio::detail::tss_ptr<void> thread_id_;
116
#endif // !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
122
: ref_(do_init::instance())
124
while (&instance_ == 0); // Ensure openssl_init::instance_ is linked in.
133
// Instance to force initialisation of openssl at global scope.
134
static openssl_init instance_;
136
// Reference to singleton do_init object to ensure that openssl does not get
137
// cleaned up until the last user has finished with it.
138
boost::shared_ptr<do_init> ref_;
141
template <bool Do_Init>
142
openssl_init<Do_Init> openssl_init<Do_Init>::instance_;
144
} // namespace detail
148
#include "asio/detail/pop_options.hpp"
150
#endif // ASIO_SSL_DETAIL_OPENSSL_INIT_HPP