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.

184 lines
4.7KB

  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. #define THREADS (HAVE_PTHREADS || (defined(WIN32) && !defined(__MINGW32CE__)))
  24. #if THREADS
  25. #if HAVE_PTHREADS
  26. #include <pthread.h>
  27. #else
  28. #include "libavcodec/w32pthreads.h"
  29. #endif
  30. #endif
  31. #if CONFIG_OPENSSL
  32. #include <openssl/ssl.h>
  33. static int openssl_init;
  34. #if THREADS
  35. #include <openssl/crypto.h>
  36. #include "libavutil/avutil.h"
  37. pthread_mutex_t *openssl_mutexes;
  38. static void openssl_lock(int mode, int type, const char *file, int line)
  39. {
  40. if (mode & CRYPTO_LOCK)
  41. pthread_mutex_lock(&openssl_mutexes[type]);
  42. else
  43. pthread_mutex_unlock(&openssl_mutexes[type]);
  44. }
  45. #if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
  46. static unsigned long openssl_thread_id(void)
  47. {
  48. return (intptr_t) pthread_self();
  49. }
  50. #endif
  51. #endif
  52. #endif
  53. #if CONFIG_GNUTLS
  54. #include <gnutls/gnutls.h>
  55. #if THREADS && GNUTLS_VERSION_NUMBER <= 0x020b00
  56. #include <gcrypt.h>
  57. #include <errno.h>
  58. #undef malloc
  59. #undef free
  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 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 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 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 (!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. WSADATA wsaData;
  126. if (WSAStartup(MAKEWORD(1,1), &wsaData))
  127. return 0;
  128. #endif
  129. return 1;
  130. }
  131. int ff_network_wait_fd(int fd, int write)
  132. {
  133. int ev = write ? POLLOUT : POLLIN;
  134. struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
  135. int ret;
  136. ret = poll(&p, 1, 100);
  137. return ret < 0 ? ff_neterrno() : p.revents & (ev | POLLERR | POLLHUP) ? 0 : AVERROR(EAGAIN);
  138. }
  139. void ff_network_close(void)
  140. {
  141. #if HAVE_WINSOCK2_H
  142. WSACleanup();
  143. #endif
  144. }
  145. #if HAVE_WINSOCK2_H
  146. int ff_neterrno(void)
  147. {
  148. int err = WSAGetLastError();
  149. switch (err) {
  150. case WSAEWOULDBLOCK:
  151. return AVERROR(EAGAIN);
  152. case WSAEINTR:
  153. return AVERROR(EINTR);
  154. }
  155. return -err;
  156. }
  157. #endif
  158. int ff_is_multicast_address(struct sockaddr *addr)
  159. {
  160. if (addr->sa_family == AF_INET) {
  161. return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
  162. }
  163. #if HAVE_STRUCT_SOCKADDR_IN6
  164. if (addr->sa_family == AF_INET6) {
  165. return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);
  166. }
  167. #endif
  168. return 0;
  169. }