opensslthreadlock.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*****************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * $Id$
  9. *
  10. * Example source code to show one way to set the necessary OpenSSL locking
  11. * callbacks if you want to do multi-threaded transfers with HTTPS/FTPS with
  12. * libcurl built to use OpenSSL.
  13. *
  14. * This is not a complete stand-alone example.
  15. *
  16. * Author: Jeremy Brown
  17. */
  18. #include <stdio.h>
  19. #include <pthread.h>
  20. #include <openssl/err.h>
  21. #define MUTEX_TYPE pthread_mutex_t
  22. #define MUTEX_SETUP(x) pthread_mutex_init(&(x), NULL)
  23. #define MUTEX_CLEANUP(x) pthread_mutex_destroy(&(x))
  24. #define MUTEX_LOCK(x) pthread_mutex_lock(&(x))
  25. #define MUTEX_UNLOCK(x) pthread_mutex_unlock(&(x))
  26. #define THREAD_ID pthread_self( )
  27. void handle_error(const char *file, int lineno, const char *msg){
  28. fprintf(stderr, "** %s:%d %s\n", file, lineno, msg);
  29. ERR_print_errors_fp(stderr);
  30. /* exit(-1); */
  31. }
  32. /* This array will store all of the mutexes available to OpenSSL. */
  33. static MUTEX_TYPE *mutex_buf= NULL;
  34. static void locking_function(int mode, int n, const char * file, int line)
  35. {
  36. if (mode & CRYPTO_LOCK)
  37. MUTEX_LOCK(mutex_buf[n]);
  38. else
  39. MUTEX_UNLOCK(mutex_buf[n]);
  40. }
  41. static unsigned long id_function(void)
  42. {
  43. return ((unsigned long)THREAD_ID);
  44. }
  45. int thread_setup(void)
  46. {
  47. int i;
  48. mutex_buf = (MUTEX_TYPE *)malloc(CRYPTO_num_locks( ) * sizeof(MUTEX_TYPE));
  49. if (!mutex_buf)
  50. return 0;
  51. for (i = 0; i < CRYPTO_num_locks( ); i++)
  52. MUTEX_SETUP(mutex_buf[i]);
  53. CRYPTO_set_id_callback(id_function);
  54. CRYPTO_set_locking_callback(locking_function);
  55. return 1;
  56. }
  57. int thread_cleanup(void)
  58. {
  59. int i;
  60. if (!mutex_buf)
  61. return 0;
  62. CRYPTO_set_id_callback(NULL);
  63. CRYPTO_set_locking_callback(NULL);
  64. for (i = 0; i < CRYPTO_num_locks( ); i++)
  65. MUTEX_CLEANUP(mutex_buf[i]);
  66. free(mutex_buf);
  67. mutex_buf = NULL;
  68. return 1;
  69. }