threaded-ssl.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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.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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. /* <DESC>
  25. * Show the required mutex callback setups for GnuTLS and OpenSSL when using
  26. * libcurl multi-threaded.
  27. * </DESC>
  28. */
  29. /* A multi-threaded example that uses pthreads and fetches 4 remote files at
  30. * once over HTTPS.
  31. *
  32. * Recent versions of OpenSSL and GnuTLS are thread safe by design, assuming
  33. * support for the underlying OS threading API is built-in. Older revisions
  34. * of this example demonstrated locking callbacks for the SSL library, which
  35. * are no longer necessary. An older revision with callbacks can be found at
  36. * https://github.com/curl/curl/blob/curl-7_88_1/docs/examples/threaded-ssl.c
  37. */
  38. #define USE_OPENSSL /* or USE_GNUTLS accordingly */
  39. #include <stdio.h>
  40. #include <pthread.h>
  41. #include <curl/curl.h>
  42. #define NUMT 4
  43. /* List of URLs to fetch.*/
  44. const char * const urls[]= {
  45. "https://www.example.com/",
  46. "https://www2.example.com/",
  47. "https://www3.example.com/",
  48. "https://www4.example.com/",
  49. };
  50. static void *pull_one_url(void *url)
  51. {
  52. CURL *curl;
  53. curl = curl_easy_init();
  54. curl_easy_setopt(curl, CURLOPT_URL, url);
  55. /* this example does not verify the server's certificate, which means we
  56. might be downloading stuff from an impostor */
  57. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
  58. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
  59. curl_easy_perform(curl); /* ignores error */
  60. curl_easy_cleanup(curl);
  61. return NULL;
  62. }
  63. int main(int argc, char **argv)
  64. {
  65. pthread_t tid[NUMT];
  66. int i;
  67. (void)argc; /* we do not use any arguments in this example */
  68. (void)argv;
  69. /* Must initialize libcurl before any threads are started */
  70. curl_global_init(CURL_GLOBAL_ALL);
  71. for(i = 0; i < NUMT; i++) {
  72. int error = pthread_create(&tid[i],
  73. NULL, /* default attributes please */
  74. pull_one_url,
  75. (void *)urls[i]);
  76. if(0 != error)
  77. fprintf(stderr, "Couldn't run thread number %d, errno %d\n", i, error);
  78. else
  79. fprintf(stderr, "Thread %d, gets %s\n", i, urls[i]);
  80. }
  81. /* now wait for all threads to terminate */
  82. for(i = 0; i < NUMT; i++) {
  83. pthread_join(tid[i], NULL);
  84. fprintf(stderr, "Thread %d terminated\n", i);
  85. }
  86. return 0;
  87. }