blob: 6f86c7f7064aaa4a2eeb91adfd42eefa642b6a11 [file] [log] [blame]
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001/***************************************************************************
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
Alex Deymo8f1a2142016-06-28 14:49:26 -07008 * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
Lucas Eckels9bd90e62012-08-06 15:07:02 -07009 *
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070010 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
Alex Deymo8f1a2142016-06-28 14:49:26 -070012 * are also available at https://curl.haxx.se/docs/copyright.html.
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070013 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ***************************************************************************/
Alex Deymo8f1a2142016-06-28 14:49:26 -070022/* <DESC>
23 * one way to set the necessary OpenSSL locking callbacks if you want to do
24 * multi-threaded transfers with HTTPS/FTPS with libcurl built to use OpenSSL.
25 * </DESC>
26 */
27/*
Lucas Eckels9bd90e62012-08-06 15:07:02 -070028 * This is not a complete stand-alone example.
29 *
30 * Author: Jeremy Brown
31 */
32
Lucas Eckels9bd90e62012-08-06 15:07:02 -070033#include <stdio.h>
34#include <pthread.h>
35#include <openssl/err.h>
36
37#define MUTEX_TYPE pthread_mutex_t
38#define MUTEX_SETUP(x) pthread_mutex_init(&(x), NULL)
39#define MUTEX_CLEANUP(x) pthread_mutex_destroy(&(x))
40#define MUTEX_LOCK(x) pthread_mutex_lock(&(x))
41#define MUTEX_UNLOCK(x) pthread_mutex_unlock(&(x))
Alex Deymo8f1a2142016-06-28 14:49:26 -070042#define THREAD_ID pthread_self()
Lucas Eckels9bd90e62012-08-06 15:07:02 -070043
44
Alex Deymo8f1a2142016-06-28 14:49:26 -070045void handle_error(const char *file, int lineno, const char *msg)
46{
47 fprintf(stderr, "** %s:%d %s\n", file, lineno, msg);
48 ERR_print_errors_fp(stderr);
49 /* exit(-1); */
50}
Lucas Eckels9bd90e62012-08-06 15:07:02 -070051
52/* This array will store all of the mutexes available to OpenSSL. */
53static MUTEX_TYPE *mutex_buf= NULL;
54
Elliott Hughescee03382017-06-23 12:17:18 -070055static void locking_function(int mode, int n, const char *file, int line)
Lucas Eckels9bd90e62012-08-06 15:07:02 -070056{
Alex Deymo8f1a2142016-06-28 14:49:26 -070057 if(mode & CRYPTO_LOCK)
Lucas Eckels9bd90e62012-08-06 15:07:02 -070058 MUTEX_LOCK(mutex_buf[n]);
59 else
60 MUTEX_UNLOCK(mutex_buf[n]);
61}
62
63static unsigned long id_function(void)
64{
65 return ((unsigned long)THREAD_ID);
66}
67
68int thread_setup(void)
69{
70 int i;
71
Alex Deymo8f1a2142016-06-28 14:49:26 -070072 mutex_buf = malloc(CRYPTO_num_locks() * sizeof(MUTEX_TYPE));
73 if(!mutex_buf)
Lucas Eckels9bd90e62012-08-06 15:07:02 -070074 return 0;
Alex Deymo8f1a2142016-06-28 14:49:26 -070075 for(i = 0; i < CRYPTO_num_locks(); i++)
Lucas Eckels9bd90e62012-08-06 15:07:02 -070076 MUTEX_SETUP(mutex_buf[i]);
77 CRYPTO_set_id_callback(id_function);
78 CRYPTO_set_locking_callback(locking_function);
79 return 1;
80}
81
82int thread_cleanup(void)
83{
84 int i;
85
Alex Deymo8f1a2142016-06-28 14:49:26 -070086 if(!mutex_buf)
Lucas Eckels9bd90e62012-08-06 15:07:02 -070087 return 0;
88 CRYPTO_set_id_callback(NULL);
89 CRYPTO_set_locking_callback(NULL);
Alex Deymo8f1a2142016-06-28 14:49:26 -070090 for(i = 0; i < CRYPTO_num_locks(); i++)
Lucas Eckels9bd90e62012-08-06 15:07:02 -070091 MUTEX_CLEANUP(mutex_buf[i]);
92 free(mutex_buf);
93 mutex_buf = NULL;
94 return 1;
95}