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.

334 lines
8.6KB

  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 "url.h"
  22. #include "libavcodec/internal.h"
  23. #include "libavutil/mem.h"
  24. #if HAVE_THREADS
  25. #if HAVE_PTHREADS
  26. #include <pthread.h>
  27. #else
  28. #include "compat/w32pthreads.h"
  29. #endif
  30. #endif
  31. #if CONFIG_OPENSSL
  32. #include <openssl/ssl.h>
  33. static int openssl_init;
  34. #if HAVE_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 HAVE_THREADS && GNUTLS_VERSION_NUMBER <= 0x020b00
  56. #include <gcrypt.h>
  57. #include <errno.h>
  58. GCRY_THREAD_OPTION_PTHREAD_IMPL;
  59. #endif
  60. #endif
  61. void ff_tls_init(void)
  62. {
  63. avpriv_lock_avformat();
  64. #if CONFIG_OPENSSL
  65. if (!openssl_init) {
  66. SSL_library_init();
  67. SSL_load_error_strings();
  68. #if HAVE_THREADS
  69. if (!CRYPTO_get_locking_callback()) {
  70. int i;
  71. openssl_mutexes = av_malloc(sizeof(pthread_mutex_t) * CRYPTO_num_locks());
  72. for (i = 0; i < CRYPTO_num_locks(); i++)
  73. pthread_mutex_init(&openssl_mutexes[i], NULL);
  74. CRYPTO_set_locking_callback(openssl_lock);
  75. #if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
  76. CRYPTO_set_id_callback(openssl_thread_id);
  77. #endif
  78. }
  79. #endif
  80. }
  81. openssl_init++;
  82. #endif
  83. #if CONFIG_GNUTLS
  84. #if HAVE_THREADS && GNUTLS_VERSION_NUMBER < 0x020b00
  85. if (gcry_control(GCRYCTL_ANY_INITIALIZATION_P) == 0)
  86. gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
  87. #endif
  88. gnutls_global_init();
  89. #endif
  90. avpriv_unlock_avformat();
  91. }
  92. void ff_tls_deinit(void)
  93. {
  94. avpriv_lock_avformat();
  95. #if CONFIG_OPENSSL
  96. openssl_init--;
  97. if (!openssl_init) {
  98. #if HAVE_THREADS
  99. if (CRYPTO_get_locking_callback() == openssl_lock) {
  100. int i;
  101. CRYPTO_set_locking_callback(NULL);
  102. for (i = 0; i < CRYPTO_num_locks(); i++)
  103. pthread_mutex_destroy(&openssl_mutexes[i]);
  104. av_free(openssl_mutexes);
  105. }
  106. #endif
  107. }
  108. #endif
  109. #if CONFIG_GNUTLS
  110. gnutls_global_deinit();
  111. #endif
  112. avpriv_unlock_avformat();
  113. }
  114. int ff_network_inited_globally;
  115. int ff_network_init(void)
  116. {
  117. #if HAVE_WINSOCK2_H
  118. WSADATA wsaData;
  119. #endif
  120. if (!ff_network_inited_globally)
  121. av_log(NULL, AV_LOG_WARNING, "Using network protocols without global "
  122. "network initialization. Please use "
  123. "avformat_network_init(), this will "
  124. "become mandatory later.\n");
  125. #if HAVE_WINSOCK2_H
  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. case WSAEPROTONOSUPPORT:
  155. return AVERROR(EPROTONOSUPPORT);
  156. case WSAETIMEDOUT:
  157. return AVERROR(ETIMEDOUT);
  158. case WSAECONNREFUSED:
  159. return AVERROR(ECONNREFUSED);
  160. case WSAEINPROGRESS:
  161. return AVERROR(EINPROGRESS);
  162. }
  163. return -err;
  164. }
  165. #endif
  166. int ff_is_multicast_address(struct sockaddr *addr)
  167. {
  168. if (addr->sa_family == AF_INET) {
  169. return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
  170. }
  171. #if HAVE_STRUCT_SOCKADDR_IN6
  172. if (addr->sa_family == AF_INET6) {
  173. return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);
  174. }
  175. #endif
  176. return 0;
  177. }
  178. static int ff_poll_interrupt(struct pollfd *p, nfds_t nfds, int timeout,
  179. AVIOInterruptCB *cb)
  180. {
  181. int runs = timeout / POLLING_TIME;
  182. int ret = 0;
  183. do {
  184. if (ff_check_interrupt(cb))
  185. return AVERROR_EXIT;
  186. ret = poll(p, nfds, POLLING_TIME);
  187. if (ret != 0)
  188. break;
  189. } while (timeout < 0 || runs-- > 0);
  190. if (!ret)
  191. return AVERROR(ETIMEDOUT);
  192. if (ret < 0)
  193. return AVERROR(errno);
  194. return ret;
  195. }
  196. int ff_listen_bind(int fd, const struct sockaddr *addr,
  197. socklen_t addrlen, int timeout, URLContext *h)
  198. {
  199. int ret;
  200. int reuse = 1;
  201. struct pollfd lp = { fd, POLLIN, 0 };
  202. setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
  203. ret = bind(fd, addr, addrlen);
  204. if (ret)
  205. return ff_neterrno();
  206. ret = listen(fd, 1);
  207. if (ret)
  208. return ff_neterrno();
  209. ret = ff_poll_interrupt(&lp, 1, timeout, &h->interrupt_callback);
  210. if (ret < 0)
  211. return ret;
  212. ret = accept(fd, NULL, NULL);
  213. if (ret < 0)
  214. return ff_neterrno();
  215. closesocket(fd);
  216. ff_socket_nonblock(ret, 1);
  217. return ret;
  218. }
  219. int ff_listen_connect(int fd, const struct sockaddr *addr,
  220. socklen_t addrlen, int timeout, URLContext *h)
  221. {
  222. struct pollfd p = {fd, POLLOUT, 0};
  223. int ret;
  224. socklen_t optlen;
  225. ff_socket_nonblock(fd, 1);
  226. while ((ret = connect(fd, addr, addrlen))) {
  227. ret = ff_neterrno();
  228. switch (ret) {
  229. case AVERROR(EINTR):
  230. if (ff_check_interrupt(&h->interrupt_callback))
  231. return AVERROR_EXIT;
  232. continue;
  233. case AVERROR(EINPROGRESS):
  234. case AVERROR(EAGAIN):
  235. ret = ff_poll_interrupt(&p, 1, timeout, &h->interrupt_callback);
  236. if (ret < 0)
  237. return ret;
  238. optlen = sizeof(ret);
  239. if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen))
  240. ret = AVUNERROR(ff_neterrno());
  241. if (ret != 0) {
  242. char errbuf[100];
  243. ret = AVERROR(ret);
  244. av_strerror(ret, errbuf, sizeof(errbuf));
  245. av_log(h, AV_LOG_ERROR,
  246. "Connection to %s failed: %s\n",
  247. h->filename, errbuf);
  248. }
  249. default:
  250. return ret;
  251. }
  252. }
  253. return ret;
  254. }
  255. static int match_host_pattern(const char *pattern, const char *hostname)
  256. {
  257. int len_p, len_h;
  258. if (!strcmp(pattern, "*"))
  259. return 1;
  260. // Skip a possible *. at the start of the pattern
  261. if (pattern[0] == '*')
  262. pattern++;
  263. if (pattern[0] == '.')
  264. pattern++;
  265. len_p = strlen(pattern);
  266. len_h = strlen(hostname);
  267. if (len_p > len_h)
  268. return 0;
  269. // Simply check if the end of hostname is equal to 'pattern'
  270. if (!strcmp(pattern, &hostname[len_h - len_p])) {
  271. if (len_h == len_p)
  272. return 1; // Exact match
  273. if (hostname[len_h - len_p - 1] == '.')
  274. return 1; // The matched substring is a domain and not just a substring of a domain
  275. }
  276. return 0;
  277. }
  278. int ff_http_match_no_proxy(const char *no_proxy, const char *hostname)
  279. {
  280. char *buf, *start;
  281. int ret = 0;
  282. if (!no_proxy)
  283. return 0;
  284. if (!hostname)
  285. return 0;
  286. buf = av_strdup(no_proxy);
  287. if (!buf)
  288. return 0;
  289. start = buf;
  290. while (start) {
  291. char *sep, *next = NULL;
  292. start += strspn(start, " ,");
  293. sep = start + strcspn(start, " ,");
  294. if (*sep) {
  295. next = sep + 1;
  296. *sep = '\0';
  297. }
  298. if (match_host_pattern(start, hostname)) {
  299. ret = 1;
  300. break;
  301. }
  302. start = next;
  303. }
  304. av_free(buf);
  305. return ret;
  306. }