network.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. }
  156. return -err;
  157. }
  158. #endif
  159. int ff_is_multicast_address(struct sockaddr *addr)
  160. {
  161. if (addr->sa_family == AF_INET) {
  162. return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
  163. }
  164. #if HAVE_STRUCT_SOCKADDR_IN6
  165. if (addr->sa_family == AF_INET6) {
  166. return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);
  167. }
  168. #endif
  169. return 0;
  170. }