network.c 5.0 KB

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