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.

358 lines
9.2KB

  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 <fcntl.h>
  21. #include "network.h"
  22. #include "url.h"
  23. #include "libavcodec/internal.h"
  24. #include "libavutil/mem.h"
  25. #if HAVE_THREADS
  26. #if HAVE_PTHREADS
  27. #include <pthread.h>
  28. #else
  29. #include "compat/w32pthreads.h"
  30. #endif
  31. #endif
  32. #if CONFIG_OPENSSL
  33. #include <openssl/ssl.h>
  34. static int openssl_init;
  35. #if HAVE_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 HAVE_THREADS && GNUTLS_VERSION_NUMBER <= 0x020b00
  57. #include <gcrypt.h>
  58. #include <errno.h>
  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 HAVE_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 HAVE_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 HAVE_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. case WSAEPROTONOSUPPORT:
  156. return AVERROR(EPROTONOSUPPORT);
  157. case WSAETIMEDOUT:
  158. return AVERROR(ETIMEDOUT);
  159. case WSAECONNREFUSED:
  160. return AVERROR(ECONNREFUSED);
  161. case WSAEINPROGRESS:
  162. return AVERROR(EINPROGRESS);
  163. }
  164. return -err;
  165. }
  166. #endif
  167. int ff_is_multicast_address(struct sockaddr *addr)
  168. {
  169. if (addr->sa_family == AF_INET) {
  170. return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
  171. }
  172. #if HAVE_STRUCT_SOCKADDR_IN6
  173. if (addr->sa_family == AF_INET6) {
  174. return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);
  175. }
  176. #endif
  177. return 0;
  178. }
  179. static int ff_poll_interrupt(struct pollfd *p, nfds_t nfds, int timeout,
  180. AVIOInterruptCB *cb)
  181. {
  182. int runs = timeout / POLLING_TIME;
  183. int ret = 0;
  184. do {
  185. if (ff_check_interrupt(cb))
  186. return AVERROR_EXIT;
  187. ret = poll(p, nfds, POLLING_TIME);
  188. if (ret != 0)
  189. break;
  190. } while (timeout < 0 || runs-- > 0);
  191. if (!ret)
  192. return AVERROR(ETIMEDOUT);
  193. if (ret < 0)
  194. return AVERROR(errno);
  195. return ret;
  196. }
  197. int ff_socket(int af, int type, int proto)
  198. {
  199. int fd;
  200. #ifdef SOCK_CLOEXEC
  201. fd = socket(af, type | SOCK_CLOEXEC, proto);
  202. if (fd == -1 && errno == EINVAL)
  203. #endif
  204. {
  205. fd = socket(af, type, proto);
  206. #if HAVE_FCNTL
  207. if (fd != -1)
  208. fcntl(fd, F_SETFD, FD_CLOEXEC);
  209. #endif
  210. }
  211. return fd;
  212. }
  213. int ff_listen_bind(int fd, const struct sockaddr *addr,
  214. socklen_t addrlen, int timeout, URLContext *h)
  215. {
  216. int ret;
  217. int reuse = 1;
  218. struct pollfd lp = { fd, POLLIN, 0 };
  219. setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
  220. ret = bind(fd, addr, addrlen);
  221. if (ret)
  222. return ff_neterrno();
  223. ret = listen(fd, 1);
  224. if (ret)
  225. return ff_neterrno();
  226. ret = ff_poll_interrupt(&lp, 1, timeout, &h->interrupt_callback);
  227. if (ret < 0)
  228. return ret;
  229. ret = accept(fd, NULL, NULL);
  230. if (ret < 0)
  231. return ff_neterrno();
  232. closesocket(fd);
  233. ff_socket_nonblock(ret, 1);
  234. return ret;
  235. }
  236. int ff_listen_connect(int fd, const struct sockaddr *addr,
  237. socklen_t addrlen, int timeout, URLContext *h,
  238. int will_try_next)
  239. {
  240. struct pollfd p = {fd, POLLOUT, 0};
  241. int ret;
  242. socklen_t optlen;
  243. ff_socket_nonblock(fd, 1);
  244. while ((ret = connect(fd, addr, addrlen))) {
  245. ret = ff_neterrno();
  246. switch (ret) {
  247. case AVERROR(EINTR):
  248. if (ff_check_interrupt(&h->interrupt_callback))
  249. return AVERROR_EXIT;
  250. continue;
  251. case AVERROR(EINPROGRESS):
  252. case AVERROR(EAGAIN):
  253. ret = ff_poll_interrupt(&p, 1, timeout, &h->interrupt_callback);
  254. if (ret < 0)
  255. return ret;
  256. optlen = sizeof(ret);
  257. if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen))
  258. ret = AVUNERROR(ff_neterrno());
  259. if (ret != 0) {
  260. char errbuf[100];
  261. ret = AVERROR(ret);
  262. av_strerror(ret, errbuf, sizeof(errbuf));
  263. if (will_try_next)
  264. av_log(h, AV_LOG_WARNING,
  265. "Connection to %s failed (%s), trying next address\n",
  266. h->filename, errbuf);
  267. else
  268. av_log(h, AV_LOG_ERROR, "Connection to %s failed: %s\n",
  269. h->filename, errbuf);
  270. }
  271. default:
  272. return ret;
  273. }
  274. }
  275. return ret;
  276. }
  277. static int match_host_pattern(const char *pattern, const char *hostname)
  278. {
  279. int len_p, len_h;
  280. if (!strcmp(pattern, "*"))
  281. return 1;
  282. // Skip a possible *. at the start of the pattern
  283. if (pattern[0] == '*')
  284. pattern++;
  285. if (pattern[0] == '.')
  286. pattern++;
  287. len_p = strlen(pattern);
  288. len_h = strlen(hostname);
  289. if (len_p > len_h)
  290. return 0;
  291. // Simply check if the end of hostname is equal to 'pattern'
  292. if (!strcmp(pattern, &hostname[len_h - len_p])) {
  293. if (len_h == len_p)
  294. return 1; // Exact match
  295. if (hostname[len_h - len_p - 1] == '.')
  296. return 1; // The matched substring is a domain and not just a substring of a domain
  297. }
  298. return 0;
  299. }
  300. int ff_http_match_no_proxy(const char *no_proxy, const char *hostname)
  301. {
  302. char *buf, *start;
  303. int ret = 0;
  304. if (!no_proxy)
  305. return 0;
  306. if (!hostname)
  307. return 0;
  308. buf = av_strdup(no_proxy);
  309. if (!buf)
  310. return 0;
  311. start = buf;
  312. while (start) {
  313. char *sep, *next = NULL;
  314. start += strspn(start, " ,");
  315. sep = start + strcspn(start, " ,");
  316. if (*sep) {
  317. next = sep + 1;
  318. *sep = '\0';
  319. }
  320. if (match_host_pattern(start, hostname)) {
  321. ret = 1;
  322. break;
  323. }
  324. start = next;
  325. }
  326. av_free(buf);
  327. return ret;
  328. }