threaded-ssl.c 4.3 KB

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