threaded-ssl.c 4.2 KB

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