You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
4.9KB

  1. /*
  2. * Copyright (c) 2007 The Libav Project
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "network.h"
  21. #include "libavcodec/internal.h"
  22. #include "libavutil/mem.h"
  23. #if HAVE_THREADS
  24. #if HAVE_PTHREADS
  25. #include <pthread.h>
  26. #else
  27. #include "libavcodec/w32pthreads.h"
  28. #endif
  29. #endif
  30. #if CONFIG_OPENSSL
  31. #include <openssl/ssl.h>
  32. static int openssl_init;
  33. #if HAVE_THREADS
  34. #include <openssl/crypto.h>
  35. #include "libavutil/avutil.h"
  36. pthread_mutex_t *openssl_mutexes;
  37. static void openssl_lock(int mode, int type, const char *file, int line)
  38. {
  39. if (mode & CRYPTO_LOCK)
  40. pthread_mutex_lock(&openssl_mutexes[type]);
  41. else
  42. pthread_mutex_unlock(&openssl_mutexes[type]);
  43. }
  44. #if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
  45. static unsigned long openssl_thread_id(void)
  46. {
  47. return (intptr_t) pthread_self();
  48. }
  49. #endif
  50. #endif
  51. #endif
  52. #if CONFIG_GNUTLS
  53. #include <gnutls/gnutls.h>
  54. #if HAVE_THREADS && GNUTLS_VERSION_NUMBER <= 0x020b00
  55. #include <gcrypt.h>
  56. #include <errno.h>
  57. GCRY_THREAD_OPTION_PTHREAD_IMPL;
  58. #endif
  59. #endif
  60. void ff_tls_init(void)
  61. {
  62. avpriv_lock_avformat();
  63. #if CONFIG_OPENSSL
  64. if (!openssl_init) {
  65. SSL_library_init();
  66. SSL_load_error_strings();
  67. #if HAVE_THREADS
  68. if (!CRYPTO_get_locking_callback()) {
  69. int i;
  70. openssl_mutexes = av_malloc(sizeof(pthread_mutex_t) * CRYPTO_num_locks());
  71. for (i = 0; i < CRYPTO_num_locks(); i++)
  72. pthread_mutex_init(&openssl_mutexes[i], NULL);
  73. CRYPTO_set_locking_callback(openssl_lock);
  74. #if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
  75. CRYPTO_set_id_callback(openssl_thread_id);
  76. #endif
  77. }
  78. #endif
  79. }
  80. openssl_init++;
  81. #endif
  82. #if CONFIG_GNUTLS
  83. #if HAVE_THREADS && GNUTLS_VERSION_NUMBER < 0x020b00
  84. if (gcry_control(GCRYCTL_ANY_INITIALIZATION_P) == 0)
  85. gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
  86. #endif
  87. gnutls_global_init();
  88. #endif
  89. avpriv_unlock_avformat();
  90. }
  91. void ff_tls_deinit(void)
  92. {
  93. avpriv_lock_avformat();
  94. #if CONFIG_OPENSSL
  95. openssl_init--;
  96. if (!openssl_init) {
  97. #if HAVE_THREADS
  98. if (CRYPTO_get_locking_callback() == openssl_lock) {
  99. int i;
  100. CRYPTO_set_locking_callback(NULL);
  101. for (i = 0; i < CRYPTO_num_locks(); i++)
  102. pthread_mutex_destroy(&openssl_mutexes[i]);
  103. av_free(openssl_mutexes);
  104. }
  105. #endif
  106. }
  107. #endif
  108. #if CONFIG_GNUTLS
  109. gnutls_global_deinit();
  110. #endif
  111. avpriv_unlock_avformat();
  112. }
  113. int ff_network_inited_globally;
  114. int ff_network_init(void)
  115. {
  116. #if HAVE_WINSOCK2_H
  117. WSADATA wsaData;
  118. #endif
  119. if (!ff_network_inited_globally)
  120. av_log(NULL, AV_LOG_WARNING, "Using network protocols without global "
  121. "network initialization. Please use "
  122. "avformat_network_init(), this will "
  123. "become mandatory later.\n");
  124. #if HAVE_WINSOCK2_H
  125. if (WSAStartup(MAKEWORD(1,1), &wsaData))
  126. return 0;
  127. #endif
  128. return 1;
  129. }
  130. int ff_network_wait_fd(int fd, int write)
  131. {
  132. int ev = write ? POLLOUT : POLLIN;
  133. struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
  134. int ret;
  135. ret = poll(&p, 1, 100);
  136. return ret < 0 ? ff_neterrno() : p.revents & (ev | POLLERR | POLLHUP) ? 0 : AVERROR(EAGAIN);
  137. }
  138. void ff_network_close(void)
  139. {
  140. #if HAVE_WINSOCK2_H
  141. WSACleanup();
  142. #endif
  143. }
  144. #if HAVE_WINSOCK2_H
  145. int ff_neterrno(void)
  146. {
  147. int err = WSAGetLastError();
  148. switch (err) {
  149. case WSAEWOULDBLOCK:
  150. return AVERROR(EAGAIN);
  151. case WSAEINTR:
  152. return AVERROR(EINTR);
  153. case WSAEPROTONOSUPPORT:
  154. return AVERROR(EPROTONOSUPPORT);
  155. case WSAETIMEDOUT:
  156. return AVERROR(ETIMEDOUT);
  157. case WSAECONNREFUSED:
  158. return AVERROR(ECONNREFUSED);
  159. case WSAEINPROGRESS:
  160. return AVERROR(EINPROGRESS);
  161. }
  162. return -err;
  163. }
  164. #endif
  165. int ff_is_multicast_address(struct sockaddr *addr)
  166. {
  167. if (addr->sa_family == AF_INET) {
  168. return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
  169. }
  170. #if HAVE_STRUCT_SOCKADDR_IN6
  171. if (addr->sa_family == AF_INET6) {
  172. return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);
  173. }
  174. #endif
  175. return 0;
  176. }