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.

361 lines
9.3KB

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