2011-03-10 05:48:02 -05:00
|
|
|
/***************************************************************************
|
2008-02-03 07:28:48 -05:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2017-08-25 02:15:59 -04:00
|
|
|
* Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2008-02-03 07:28:48 -05:00
|
|
|
*
|
2011-03-10 05:48:02 -05:00
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2016-02-02 18:19:02 -05:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2011-03-10 05:48:02 -05:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
2016-01-04 09:34:05 -05:00
|
|
|
/* <DESC>
|
|
|
|
* Show the required mutex callback setups for GnuTLS and OpenSSL when using
|
|
|
|
* libcurl multi-threaded.
|
|
|
|
* </DESC>
|
|
|
|
*/
|
2011-03-10 05:48:02 -05:00
|
|
|
/* A multi-threaded example that uses pthreads and fetches 4 remote files at
|
2017-08-25 02:15:59 -04:00
|
|
|
* once over HTTPS. The lock callbacks and stuff assume OpenSSL <1.1 or GnuTLS
|
2008-02-23 18:00:24 -05:00
|
|
|
* (libgcrypt) so far.
|
2008-02-03 07:28:48 -05:00
|
|
|
*
|
2008-02-23 18:00:24 -05:00
|
|
|
* OpenSSL docs for this:
|
2016-02-02 22:16:52 -05:00
|
|
|
* https://www.openssl.org/docs/crypto/threads.html
|
2008-02-23 18:00:24 -05:00
|
|
|
* gcrypt docs for this:
|
2016-02-02 22:16:52 -05:00
|
|
|
* https://gnupg.org/documentation/manuals/gcrypt/Multi_002dThreading.html
|
2008-02-03 07:28:48 -05:00
|
|
|
*/
|
|
|
|
|
2008-02-23 18:00:24 -05:00
|
|
|
#define USE_OPENSSL /* or USE_GNUTLS accordingly */
|
|
|
|
|
2008-02-03 07:28:48 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <curl/curl.h>
|
|
|
|
|
2008-04-03 16:28:32 -04:00
|
|
|
#define NUMT 4
|
|
|
|
|
2008-02-03 07:28:48 -05:00
|
|
|
/* we have this global to let the callback get easy access to it */
|
|
|
|
static pthread_mutex_t *lockarray;
|
|
|
|
|
2008-02-23 18:00:24 -05:00
|
|
|
#ifdef USE_OPENSSL
|
|
|
|
#include <openssl/crypto.h>
|
2008-02-03 07:28:48 -05:00
|
|
|
static void lock_callback(int mode, int type, char *file, int line)
|
|
|
|
{
|
|
|
|
(void)file;
|
|
|
|
(void)line;
|
2016-02-11 03:42:38 -05:00
|
|
|
if(mode & CRYPTO_LOCK) {
|
2008-02-03 07:28:48 -05:00
|
|
|
pthread_mutex_lock(&(lockarray[type]));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pthread_mutex_unlock(&(lockarray[type]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned long thread_id(void)
|
|
|
|
{
|
|
|
|
unsigned long ret;
|
|
|
|
|
|
|
|
ret=(unsigned long)pthread_self();
|
2016-02-11 03:42:38 -05:00
|
|
|
return ret;
|
2008-02-03 07:28:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void init_locks(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
lockarray=(pthread_mutex_t *)OPENSSL_malloc(CRYPTO_num_locks() *
|
|
|
|
sizeof(pthread_mutex_t));
|
2016-02-11 03:42:38 -05:00
|
|
|
for(i=0; i<CRYPTO_num_locks(); i++) {
|
|
|
|
pthread_mutex_init(&(lockarray[i]), NULL);
|
2008-02-03 07:28:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
CRYPTO_set_id_callback((unsigned long (*)())thread_id);
|
|
|
|
CRYPTO_set_locking_callback((void (*)())lock_callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kill_locks(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
CRYPTO_set_locking_callback(NULL);
|
2016-02-11 03:42:38 -05:00
|
|
|
for(i=0; i<CRYPTO_num_locks(); i++)
|
2008-02-03 07:28:48 -05:00
|
|
|
pthread_mutex_destroy(&(lockarray[i]));
|
|
|
|
|
|
|
|
OPENSSL_free(lockarray);
|
|
|
|
}
|
2008-02-23 18:00:24 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_GNUTLS
|
|
|
|
#include <gcrypt.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
GCRY_THREAD_OPTION_PTHREAD_IMPL;
|
|
|
|
|
|
|
|
void init_locks(void)
|
|
|
|
{
|
|
|
|
gcry_control(GCRYCTL_SET_THREAD_CBS);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define kill_locks()
|
|
|
|
#endif
|
2008-02-03 07:28:48 -05:00
|
|
|
|
|
|
|
/* List of URLs to fetch.*/
|
2008-04-03 16:28:32 -04:00
|
|
|
const char * const urls[]= {
|
2010-10-05 09:00:19 -04:00
|
|
|
"https://www.example.com/",
|
|
|
|
"https://www2.example.com/",
|
|
|
|
"https://www3.example.com/",
|
|
|
|
"https://www4.example.com/",
|
2008-02-03 07:28:48 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
static void *pull_one_url(void *url)
|
|
|
|
{
|
|
|
|
CURL *curl;
|
|
|
|
|
|
|
|
curl = curl_easy_init();
|
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, url);
|
|
|
|
/* this example doesn't verify the server's certificate, which means we
|
|
|
|
might be downloading stuff from an impostor */
|
2008-05-22 17:20:07 -04:00
|
|
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
|
2008-02-03 07:28:48 -05:00
|
|
|
curl_easy_perform(curl); /* ignores error */
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2008-04-03 16:28:32 -04:00
|
|
|
pthread_t tid[NUMT];
|
2008-02-03 07:28:48 -05:00
|
|
|
int i;
|
|
|
|
int error;
|
|
|
|
(void)argc; /* we don't use any arguments in this example */
|
|
|
|
(void)argv;
|
|
|
|
|
2008-04-03 16:28:32 -04:00
|
|
|
/* Must initialize libcurl before any threads are started */
|
|
|
|
curl_global_init(CURL_GLOBAL_ALL);
|
|
|
|
|
2008-02-03 07:28:48 -05:00
|
|
|
init_locks();
|
|
|
|
|
2008-04-03 16:28:32 -04:00
|
|
|
for(i=0; i< NUMT; i++) {
|
2008-02-03 07:28:48 -05:00
|
|
|
error = pthread_create(&tid[i],
|
|
|
|
NULL, /* default attributes please */
|
|
|
|
pull_one_url,
|
|
|
|
(void *)urls[i]);
|
|
|
|
if(0 != error)
|
|
|
|
fprintf(stderr, "Couldn't run thread number %d, errno %d\n", i, error);
|
|
|
|
else
|
|
|
|
fprintf(stderr, "Thread %d, gets %s\n", i, urls[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now wait for all threads to terminate */
|
2008-04-03 16:28:32 -04:00
|
|
|
for(i=0; i< NUMT; i++) {
|
2008-02-03 07:28:48 -05:00
|
|
|
error = pthread_join(tid[i], NULL);
|
|
|
|
fprintf(stderr, "Thread %d terminated\n", i);
|
|
|
|
}
|
|
|
|
|
|
|
|
kill_locks();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|