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.

392 lines
10KB

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