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.

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