network.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 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. void ff_network_close(void)
  142. {
  143. #if HAVE_WINSOCK2_H
  144. WSACleanup();
  145. #endif
  146. }
  147. #if HAVE_WINSOCK2_H
  148. int ff_neterrno(void)
  149. {
  150. int err = WSAGetLastError();
  151. switch (err) {
  152. case WSAEWOULDBLOCK:
  153. return AVERROR(EAGAIN);
  154. case WSAEINTR:
  155. return AVERROR(EINTR);
  156. }
  157. return -err;
  158. }
  159. #endif
  160. int ff_is_multicast_address(struct sockaddr *addr)
  161. {
  162. if (addr->sa_family == AF_INET) {
  163. return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
  164. }
  165. #if HAVE_STRUCT_SOCKADDR_IN6
  166. if (addr->sa_family == AF_INET6) {
  167. return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);
  168. }
  169. #endif
  170. return 0;
  171. }