1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
9 * Example source code to show one way to set the necessary OpenSSL locking
10 * callbacks if you want to do multi-threaded transfers with HTTPS/FTPS with
11 * libcurl built to use OpenSSL.
13 * This is not a complete stand-alone example.
15 * Author: Jeremy Brown
21 #include <openssl/err.h>
23 #define MUTEX_TYPE pthread_mutex_t
24 #define MUTEX_SETUP(x) pthread_mutex_init(&(x), NULL)
25 #define MUTEX_CLEANUP(x) pthread_mutex_destroy(&(x))
26 #define MUTEX_LOCK(x) pthread_mutex_lock(&(x))
27 #define MUTEX_UNLOCK(x) pthread_mutex_unlock(&(x))
28 #define THREAD_ID pthread_self( )
31 void handle_error(const char *file, int lineno, const char *msg){
32 fprintf(stderr, "** %s:%d %s\n", file, lineno, msg);
33 ERR_print_errors_fp(stderr);
37 /* This array will store all of the mutexes available to OpenSSL. */
38 static MUTEX_TYPE *mutex_buf= NULL;
41 static void locking_function(int mode, int n, const char * file, int line)
43 if (mode & CRYPTO_LOCK)
44 MUTEX_LOCK(mutex_buf[n]);
46 MUTEX_UNLOCK(mutex_buf[n]);
49 static unsigned long id_function(void)
51 return ((unsigned long)THREAD_ID);
54 int thread_setup(void)
58 mutex_buf = malloc(CRYPTO_num_locks( ) * sizeof(MUTEX_TYPE));
61 for (i = 0; i < CRYPTO_num_locks( ); i++)
62 MUTEX_SETUP(mutex_buf[i]);
63 CRYPTO_set_id_callback(id_function);
64 CRYPTO_set_locking_callback(locking_function);
68 int thread_cleanup(void)
74 CRYPTO_set_id_callback(NULL);
75 CRYPTO_set_locking_callback(NULL);
76 for (i = 0; i < CRYPTO_num_locks( ); i++)
77 MUTEX_CLEANUP(mutex_buf[i]);