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.

193 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. #define THREADS (HAVE_PTHREADS || (defined(WIN32) && !defined(__MINGW32CE__)))
  23. #if 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 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 THREADS && GNUTLS_VERSION_NUMBER <= 0x020b00
  55. #include <gcrypt.h>
  56. #include <errno.h>
  57. #undef malloc
  58. #undef free
  59. GCRY_THREAD_OPTION_PTHREAD_IMPL;
  60. #endif
  61. #endif
  62. void ff_tls_init(void)
  63. {
  64. avpriv_lock_avformat();
  65. #if CONFIG_OPENSSL
  66. if (!openssl_init) {
  67. SSL_library_init();
  68. SSL_load_error_strings();
  69. #if THREADS
  70. if (!CRYPTO_get_locking_callback()) {
  71. int i;
  72. openssl_mutexes = av_malloc(sizeof(pthread_mutex_t) * CRYPTO_num_locks());
  73. for (i = 0; i < CRYPTO_num_locks(); i++)
  74. pthread_mutex_init(&openssl_mutexes[i], NULL);
  75. CRYPTO_set_locking_callback(openssl_lock);
  76. #if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
  77. CRYPTO_set_id_callback(openssl_thread_id);
  78. #endif
  79. }
  80. #endif
  81. }
  82. openssl_init++;
  83. #endif
  84. #if CONFIG_GNUTLS
  85. #if THREADS && GNUTLS_VERSION_NUMBER < 0x020b00
  86. if (gcry_control(GCRYCTL_ANY_INITIALIZATION_P) == 0)
  87. gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
  88. #endif
  89. gnutls_global_init();
  90. #endif
  91. avpriv_unlock_avformat();
  92. }
  93. void ff_tls_deinit(void)
  94. {
  95. avpriv_lock_avformat();
  96. #if CONFIG_OPENSSL
  97. openssl_init--;
  98. if (!openssl_init) {
  99. #if THREADS
  100. if (CRYPTO_get_locking_callback() == openssl_lock) {
  101. int i;
  102. CRYPTO_set_locking_callback(NULL);
  103. for (i = 0; i < CRYPTO_num_locks(); i++)
  104. pthread_mutex_destroy(&openssl_mutexes[i]);
  105. av_free(openssl_mutexes);
  106. }
  107. #endif
  108. }
  109. #endif
  110. #if CONFIG_GNUTLS
  111. gnutls_global_deinit();
  112. #endif
  113. avpriv_unlock_avformat();
  114. }
  115. int ff_network_inited_globally;
  116. int ff_network_init(void)
  117. {
  118. #if HAVE_WINSOCK2_H
  119. WSADATA wsaData;
  120. #endif
  121. if (!ff_network_inited_globally)
  122. av_log(NULL, AV_LOG_WARNING, "Using network protocols without global "
  123. "network initialization. Please use "
  124. "avformat_network_init(), this will "
  125. "become mandatory later.\n");
  126. #if HAVE_WINSOCK2_H
  127. if (WSAStartup(MAKEWORD(1,1), &wsaData))
  128. return 0;
  129. #endif
  130. return 1;
  131. }
  132. int ff_network_wait_fd(int fd, int write)
  133. {
  134. int ev = write ? POLLOUT : POLLIN;
  135. struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
  136. int ret;
  137. ret = poll(&p, 1, 100);
  138. return ret < 0 ? ff_neterrno() : p.revents & (ev | POLLERR | POLLHUP) ? 0 : AVERROR(EAGAIN);
  139. }
  140. void ff_network_close(void)
  141. {
  142. #if HAVE_WINSOCK2_H
  143. WSACleanup();
  144. #endif
  145. }
  146. #if HAVE_WINSOCK2_H
  147. int ff_neterrno(void)
  148. {
  149. int err = WSAGetLastError();
  150. switch (err) {
  151. case WSAEWOULDBLOCK:
  152. return AVERROR(EAGAIN);
  153. case WSAEINTR:
  154. return AVERROR(EINTR);
  155. case WSAEPROTONOSUPPORT:
  156. return AVERROR(EPROTONOSUPPORT);
  157. case WSAETIMEDOUT:
  158. return AVERROR(ETIMEDOUT);
  159. case WSAECONNREFUSED:
  160. return AVERROR(ECONNREFUSED);
  161. case WSAEINPROGRESS:
  162. return AVERROR(EINPROGRESS);
  163. }
  164. return -err;
  165. }
  166. #endif
  167. int ff_is_multicast_address(struct sockaddr *addr)
  168. {
  169. if (addr->sa_family == AF_INET) {
  170. return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
  171. }
  172. #if HAVE_STRUCT_SOCKADDR_IN6
  173. if (addr->sa_family == AF_INET6) {
  174. return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);
  175. }
  176. #endif
  177. return 0;
  178. }