opensslthreadlock.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 /* <DESC>
00023  * one way to set the necessary OpenSSL locking callbacks if you want to do
00024  * multi-threaded transfers with HTTPS/FTPS with libcurl built to use OpenSSL.
00025  * </DESC>
00026  */
00027 /*
00028  * This is not a complete stand-alone example.
00029  *
00030  * Author: Jeremy Brown
00031  */
00032 
00033 #include <stdio.h>
00034 #include <pthread.h>
00035 #include <openssl/err.h>
00036 
00037 #define MUTEX_TYPE       pthread_mutex_t
00038 #define MUTEX_SETUP(x)   pthread_mutex_init(&(x), NULL)
00039 #define MUTEX_CLEANUP(x) pthread_mutex_destroy(&(x))
00040 #define MUTEX_LOCK(x)    pthread_mutex_lock(&(x))
00041 #define MUTEX_UNLOCK(x)  pthread_mutex_unlock(&(x))
00042 #define THREAD_ID        pthread_self()
00043 
00044 
00045 void handle_error(const char *file, int lineno, const char *msg)
00046 {
00047   fprintf(stderr, "** %s:%d %s\n", file, lineno, msg);
00048   ERR_print_errors_fp(stderr);
00049   /* exit(-1); */
00050 }
00051 
00052 /* This array will store all of the mutexes available to OpenSSL. */
00053 static MUTEX_TYPE *mutex_buf= NULL;
00054 
00055 static void locking_function(int mode, int n, const char *file, int line)
00056 {
00057   if(mode & CRYPTO_LOCK)
00058     MUTEX_LOCK(mutex_buf[n]);
00059   else
00060     MUTEX_UNLOCK(mutex_buf[n]);
00061 }
00062 
00063 static unsigned long id_function(void)
00064 {
00065   return ((unsigned long)THREAD_ID);
00066 }
00067 
00068 int thread_setup(void)
00069 {
00070   int i;
00071 
00072   mutex_buf = malloc(CRYPTO_num_locks() * sizeof(MUTEX_TYPE));
00073   if(!mutex_buf)
00074     return 0;
00075   for(i = 0;  i < CRYPTO_num_locks();  i++)
00076     MUTEX_SETUP(mutex_buf[i]);
00077   CRYPTO_set_id_callback(id_function);
00078   CRYPTO_set_locking_callback(locking_function);
00079   return 1;
00080 }
00081 
00082 int thread_cleanup(void)
00083 {
00084   int i;
00085 
00086   if(!mutex_buf)
00087     return 0;
00088   CRYPTO_set_id_callback(NULL);
00089   CRYPTO_set_locking_callback(NULL);
00090   for(i = 0;  i < CRYPTO_num_locks();  i++)
00091     MUTEX_CLEANUP(mutex_buf[i]);
00092   free(mutex_buf);
00093   mutex_buf = NULL;
00094   return 1;
00095 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:05