2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
8 * Copyright (C) 2002-2009 OpenVPN Technologies, Inc. <sales@openvpn.net>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program (see the file COPYING included with this
21 * distribution); if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
37 static struct sparse_mutex *ssl_mutex; /* GLOBAL */
40 ssl_pthreads_locking_callback (int mode, int type, char *file, int line)
42 dmsg (D_OPENSSL_LOCK, "SSL LOCK thread=%4lu mode=%s lock=%s %s:%d",
44 (mode & CRYPTO_LOCK) ? "l" : "u",
45 (type & CRYPTO_READ) ? "r" : "w", file, line);
47 if (mode & CRYPTO_LOCK)
48 pthread_mutex_lock (&ssl_mutex[type].mutex);
50 pthread_mutex_unlock (&ssl_mutex[type].mutex);
54 ssl_pthreads_thread_id (void)
58 ret = (unsigned long) pthread_self ();
63 ssl_thread_setup (void)
67 #error L_MSG needs to be initialized as a recursive mutex
69 ssl_mutex = OPENSSL_malloc (CRYPTO_num_locks () * sizeof (struct sparse_mutex));
70 for (i = 0; i < CRYPTO_num_locks (); i++)
71 pthread_mutex_init (&ssl_mutex[i].mutex, NULL);
73 CRYPTO_set_id_callback ((unsigned long (*)(void)) ssl_pthreads_thread_id);
74 CRYPTO_set_locking_callback ((void (*)(int, int, const char*, int)) ssl_pthreads_locking_callback);
78 ssl_thread_cleanup (void)
82 dmsg (D_OPENSSL_LOCK, "SSL LOCK cleanup");
83 CRYPTO_set_locking_callback (NULL);
84 for (i = 0; i < CRYPTO_num_locks (); i++)
85 pthread_mutex_destroy (&ssl_mutex[i].mutex);
86 OPENSSL_free (ssl_mutex);
89 struct sparse_mutex mutex_array[N_MUTEXES]; /* GLOBAL */
90 bool pthread_initialized; /* GLOBAL */
93 openvpn_thread_create (void *(*start_routine) (void *), void* arg)
96 ASSERT (pthread_initialized);
97 ASSERT (!pthread_create (&ret, NULL, start_routine, arg));
98 dmsg (D_THREAD_DEBUG, "CREATE THREAD ID=%lu", (unsigned long)ret);
103 openvpn_thread_join (openvpn_thread_t id)
105 ASSERT (pthread_initialized);
106 pthread_join (id, NULL);
110 openvpn_thread_init ()
114 ASSERT (!pthread_initialized);
116 msg (M_INFO, "PTHREAD support initialized");
118 /* initialize OpenSSL library locking */
119 #if defined(USE_CRYPTO) && defined(USE_SSL)
123 /* initialize static mutexes */
124 for (i = 0; i < N_MUTEXES; i++)
125 ASSERT (!pthread_mutex_init (&mutex_array[i].mutex, NULL));
129 pthread_initialized = true;
133 openvpn_thread_cleanup ()
135 if (pthread_initialized)
139 pthread_initialized = false;
141 /* cleanup OpenSSL library locking */
142 #if defined(USE_CRYPTO) && defined(USE_SSL)
143 ssl_thread_cleanup();
146 /* destroy static mutexes */
147 for (i = 0; i < N_MUTEXES; i++)
148 ASSERT (!pthread_mutex_destroy (&mutex_array[i].mutex));
150 msg_thread_uninit ();
155 static void dummy(void) {}