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.

215 lines
5.5KB

  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 "libavutil/avutil.h"
  21. #include "network.h"
  22. #include "libavcodec/internal.h"
  23. #include "libavutil/mem.h"
  24. #include "url.h"
  25. #include "libavutil/time.h"
  26. #if HAVE_THREADS
  27. #if HAVE_PTHREADS
  28. #include <pthread.h>
  29. #elif HAVE_OS2THREADS
  30. #include "libavcodec/os2threads.h"
  31. #else
  32. #include "libavcodec/w32pthreads.h"
  33. #endif
  34. #endif
  35. #if CONFIG_OPENSSL
  36. #include <openssl/ssl.h>
  37. static int openssl_init;
  38. #if HAVE_THREADS
  39. #include <openssl/crypto.h>
  40. #include "libavutil/avutil.h"
  41. pthread_mutex_t *openssl_mutexes;
  42. static void openssl_lock(int mode, int type, const char *file, int line)
  43. {
  44. if (mode & CRYPTO_LOCK)
  45. pthread_mutex_lock(&openssl_mutexes[type]);
  46. else
  47. pthread_mutex_unlock(&openssl_mutexes[type]);
  48. }
  49. #if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
  50. static unsigned long openssl_thread_id(void)
  51. {
  52. return (intptr_t) pthread_self();
  53. }
  54. #endif
  55. #endif
  56. #endif
  57. #if CONFIG_GNUTLS
  58. #include <gnutls/gnutls.h>
  59. #if HAVE_THREADS && GNUTLS_VERSION_NUMBER <= 0x020b00
  60. #include <gcrypt.h>
  61. #include <errno.h>
  62. GCRY_THREAD_OPTION_PTHREAD_IMPL;
  63. #endif
  64. #endif
  65. void ff_tls_init(void)
  66. {
  67. avpriv_lock_avformat();
  68. #if CONFIG_OPENSSL
  69. if (!openssl_init) {
  70. SSL_library_init();
  71. SSL_load_error_strings();
  72. #if HAVE_THREADS
  73. if (!CRYPTO_get_locking_callback()) {
  74. int i;
  75. openssl_mutexes = av_malloc(sizeof(pthread_mutex_t) * CRYPTO_num_locks());
  76. for (i = 0; i < CRYPTO_num_locks(); i++)
  77. pthread_mutex_init(&openssl_mutexes[i], NULL);
  78. CRYPTO_set_locking_callback(openssl_lock);
  79. #if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
  80. CRYPTO_set_id_callback(openssl_thread_id);
  81. #endif
  82. }
  83. #endif
  84. }
  85. openssl_init++;
  86. #endif
  87. #if CONFIG_GNUTLS
  88. #if HAVE_THREADS && GNUTLS_VERSION_NUMBER < 0x020b00
  89. if (gcry_control(GCRYCTL_ANY_INITIALIZATION_P) == 0)
  90. gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
  91. #endif
  92. gnutls_global_init();
  93. #endif
  94. avpriv_unlock_avformat();
  95. }
  96. void ff_tls_deinit(void)
  97. {
  98. avpriv_lock_avformat();
  99. #if CONFIG_OPENSSL
  100. openssl_init--;
  101. if (!openssl_init) {
  102. #if HAVE_THREADS
  103. if (CRYPTO_get_locking_callback() == openssl_lock) {
  104. int i;
  105. CRYPTO_set_locking_callback(NULL);
  106. for (i = 0; i < CRYPTO_num_locks(); i++)
  107. pthread_mutex_destroy(&openssl_mutexes[i]);
  108. av_free(openssl_mutexes);
  109. }
  110. #endif
  111. }
  112. #endif
  113. #if CONFIG_GNUTLS
  114. gnutls_global_deinit();
  115. #endif
  116. avpriv_unlock_avformat();
  117. }
  118. int ff_network_inited_globally;
  119. int ff_network_init(void)
  120. {
  121. #if HAVE_WINSOCK2_H
  122. WSADATA wsaData;
  123. #endif
  124. if (!ff_network_inited_globally)
  125. av_log(NULL, AV_LOG_WARNING, "Using network protocols without global "
  126. "network initialization. Please use "
  127. "avformat_network_init(), this will "
  128. "become mandatory later.\n");
  129. #if HAVE_WINSOCK2_H
  130. if (WSAStartup(MAKEWORD(1,1), &wsaData))
  131. return 0;
  132. #endif
  133. return 1;
  134. }
  135. int ff_network_wait_fd(int fd, int write)
  136. {
  137. int ev = write ? POLLOUT : POLLIN;
  138. struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
  139. int ret;
  140. ret = poll(&p, 1, 100);
  141. return ret < 0 ? ff_neterrno() : p.revents & (ev | POLLERR | POLLHUP) ? 0 : AVERROR(EAGAIN);
  142. }
  143. int ff_network_wait_fd_timeout(int fd, int write, int64_t timeout, AVIOInterruptCB *int_cb)
  144. {
  145. int ret;
  146. int64_t wait_start = 0;
  147. while (1) {
  148. ret = ff_network_wait_fd(fd, write);
  149. if (ret != AVERROR(EAGAIN))
  150. return ret;
  151. if (ff_check_interrupt(int_cb))
  152. return AVERROR_EXIT;
  153. if (timeout) {
  154. if (!wait_start)
  155. wait_start = av_gettime();
  156. else if (av_gettime() - wait_start > timeout)
  157. return AVERROR(ETIMEDOUT);
  158. }
  159. }
  160. }
  161. void ff_network_close(void)
  162. {
  163. #if HAVE_WINSOCK2_H
  164. WSACleanup();
  165. #endif
  166. }
  167. #if HAVE_WINSOCK2_H
  168. int ff_neterrno(void)
  169. {
  170. int err = WSAGetLastError();
  171. switch (err) {
  172. case WSAEWOULDBLOCK:
  173. return AVERROR(EAGAIN);
  174. case WSAEINTR:
  175. return AVERROR(EINTR);
  176. case WSAEPROTONOSUPPORT:
  177. return AVERROR(EPROTONOSUPPORT);
  178. case WSAETIMEDOUT:
  179. return AVERROR(ETIMEDOUT);
  180. case WSAECONNREFUSED:
  181. return AVERROR(ECONNREFUSED);
  182. case WSAEINPROGRESS:
  183. return AVERROR(EINPROGRESS);
  184. }
  185. return -err;
  186. }
  187. #endif
  188. int ff_is_multicast_address(struct sockaddr *addr)
  189. {
  190. if (addr->sa_family == AF_INET) {
  191. return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
  192. }
  193. #if HAVE_STRUCT_SOCKADDR_IN6
  194. if (addr->sa_family == AF_INET6) {
  195. return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);
  196. }
  197. #endif
  198. return 0;
  199. }