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.

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