1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
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 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24 /* <DESC>
25 * Show the required mutex callback setups for GnuTLS and OpenSSL when using
26 * libcurl multi-threaded.
27 * </DESC>
28 */
29 /* A multi-threaded example that uses pthreads and fetches 4 remote files at
30 * once over HTTPS. The lock callbacks and stuff assume OpenSSL <1.1 or GnuTLS
31 * (libgcrypt) so far.
32 *
33 * OpenSSL docs for this:
34 * https://www.openssl.org/docs/man1.0.2/man3/CRYPTO_num_locks.html
35 * gcrypt docs for this:
36 * https://gnupg.org/documentation/manuals/gcrypt/Multi_002dThreading.html
37 */
38
39 #define USE_OPENSSL /* or USE_GNUTLS accordingly */
40
41 #include <stdio.h>
42 #include <pthread.h>
43 #include <curl/curl.h>
44
45 #define NUMT 4
46
47 /* we have this global to let the callback get easy access to it */
48 static pthread_mutex_t *lockarray;
49
50 #ifdef USE_OPENSSL
51 #include <openssl/crypto.h>
lock_callback(int mode,int type,char * file,int line)52 static void lock_callback(int mode, int type, char *file, int line)
53 {
54 (void)file;
55 (void)line;
56 if(mode & CRYPTO_LOCK) {
57 pthread_mutex_lock(&(lockarray[type]));
58 }
59 else {
60 pthread_mutex_unlock(&(lockarray[type]));
61 }
62 }
63
thread_id(void)64 static unsigned long thread_id(void)
65 {
66 unsigned long ret;
67
68 ret = (unsigned long)pthread_self();
69 return ret;
70 }
71
init_locks(void)72 static void init_locks(void)
73 {
74 int i;
75
76 lockarray = (pthread_mutex_t *)OPENSSL_malloc(CRYPTO_num_locks() *
77 sizeof(pthread_mutex_t));
78 for(i = 0; i<CRYPTO_num_locks(); i++) {
79 pthread_mutex_init(&(lockarray[i]), NULL);
80 }
81
82 CRYPTO_set_id_callback((unsigned long (*)())thread_id);
83 CRYPTO_set_locking_callback((void (*)())lock_callback);
84 }
85
kill_locks(void)86 static void kill_locks(void)
87 {
88 int i;
89
90 CRYPTO_set_locking_callback(NULL);
91 for(i = 0; i<CRYPTO_num_locks(); i++)
92 pthread_mutex_destroy(&(lockarray[i]));
93
94 OPENSSL_free(lockarray);
95 }
96 #endif
97
98 #ifdef USE_GNUTLS
99 #include <gcrypt.h>
100 #include <errno.h>
101
102 GCRY_THREAD_OPTION_PTHREAD_IMPL;
103
init_locks(void)104 void init_locks(void)
105 {
106 gcry_control(GCRYCTL_SET_THREAD_CBS);
107 }
108
109 #define kill_locks()
110 #endif
111
112 /* List of URLs to fetch.*/
113 const char * const urls[]= {
114 "https://www.example.com/",
115 "https://www2.example.com/",
116 "https://www3.example.com/",
117 "https://www4.example.com/",
118 };
119
pull_one_url(void * url)120 static void *pull_one_url(void *url)
121 {
122 CURL *curl;
123
124 curl = curl_easy_init();
125 curl_easy_setopt(curl, CURLOPT_URL, url);
126 /* this example does not verify the server's certificate, which means we
127 might be downloading stuff from an impostor */
128 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
129 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
130 curl_easy_perform(curl); /* ignores error */
131 curl_easy_cleanup(curl);
132
133 return NULL;
134 }
135
main(int argc,char ** argv)136 int main(int argc, char **argv)
137 {
138 pthread_t tid[NUMT];
139 int i;
140 (void)argc; /* we do not use any arguments in this example */
141 (void)argv;
142
143 /* Must initialize libcurl before any threads are started */
144 curl_global_init(CURL_GLOBAL_ALL);
145
146 init_locks();
147
148 for(i = 0; i< NUMT; i++) {
149 int error = pthread_create(&tid[i],
150 NULL, /* default attributes please */
151 pull_one_url,
152 (void *)urls[i]);
153 if(0 != error)
154 fprintf(stderr, "Couldn't run thread number %d, errno %d\n", i, error);
155 else
156 fprintf(stderr, "Thread %d, gets %s\n", i, urls[i]);
157 }
158
159 /* now wait for all threads to terminate */
160 for(i = 0; i< NUMT; i++) {
161 pthread_join(tid[i], NULL);
162 fprintf(stderr, "Thread %d terminated\n", i);
163 }
164
165 kill_locks();
166
167 return 0;
168 }
169