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.

385 lines
9.9KB

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