/libavformat/network.c

http://github.com/FFmpeg/FFmpeg · C · 586 lines · 485 code · 51 blank · 50 comment · 114 complexity · aa1d4ef453e2f6b235efd9c0f3157299 MD5 · raw file

  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 "tls.h"
  23. #include "url.h"
  24. #include "libavcodec/internal.h"
  25. #include "libavutil/avutil.h"
  26. #include "libavutil/avassert.h"
  27. #include "libavutil/mem.h"
  28. #include "libavutil/time.h"
  29. int ff_tls_init(void)
  30. {
  31. #if CONFIG_TLS_PROTOCOL
  32. #if CONFIG_OPENSSL
  33. int ret;
  34. if ((ret = ff_openssl_init()) < 0)
  35. return ret;
  36. #endif
  37. #if CONFIG_GNUTLS
  38. ff_gnutls_init();
  39. #endif
  40. #endif
  41. return 0;
  42. }
  43. void ff_tls_deinit(void)
  44. {
  45. #if CONFIG_TLS_PROTOCOL
  46. #if CONFIG_OPENSSL
  47. ff_openssl_deinit();
  48. #endif
  49. #if CONFIG_GNUTLS
  50. ff_gnutls_deinit();
  51. #endif
  52. #endif
  53. }
  54. int ff_network_init(void)
  55. {
  56. #if HAVE_WINSOCK2_H
  57. WSADATA wsaData;
  58. if (WSAStartup(MAKEWORD(1,1), &wsaData))
  59. return 0;
  60. #endif
  61. return 1;
  62. }
  63. int ff_network_wait_fd(int fd, int write)
  64. {
  65. int ev = write ? POLLOUT : POLLIN;
  66. struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
  67. int ret;
  68. ret = poll(&p, 1, POLLING_TIME);
  69. return ret < 0 ? ff_neterrno() : p.revents & (ev | POLLERR | POLLHUP) ? 0 : AVERROR(EAGAIN);
  70. }
  71. int ff_network_wait_fd_timeout(int fd, int write, int64_t timeout, AVIOInterruptCB *int_cb)
  72. {
  73. int ret;
  74. int64_t wait_start = 0;
  75. while (1) {
  76. if (ff_check_interrupt(int_cb))
  77. return AVERROR_EXIT;
  78. ret = ff_network_wait_fd(fd, write);
  79. if (ret != AVERROR(EAGAIN))
  80. return ret;
  81. if (timeout > 0) {
  82. if (!wait_start)
  83. wait_start = av_gettime_relative();
  84. else if (av_gettime_relative() - wait_start > timeout)
  85. return AVERROR(ETIMEDOUT);
  86. }
  87. }
  88. }
  89. int ff_network_sleep_interruptible(int64_t timeout, AVIOInterruptCB *int_cb)
  90. {
  91. int64_t wait_start = av_gettime_relative();
  92. while (1) {
  93. int64_t time_left;
  94. if (ff_check_interrupt(int_cb))
  95. return AVERROR_EXIT;
  96. time_left = timeout - (av_gettime_relative() - wait_start);
  97. if (time_left <= 0)
  98. return AVERROR(ETIMEDOUT);
  99. av_usleep(FFMIN(time_left, POLLING_TIME * 1000));
  100. }
  101. }
  102. void ff_network_close(void)
  103. {
  104. #if HAVE_WINSOCK2_H
  105. WSACleanup();
  106. #endif
  107. }
  108. #if HAVE_WINSOCK2_H
  109. int ff_neterrno(void)
  110. {
  111. int err = WSAGetLastError();
  112. switch (err) {
  113. case WSAEWOULDBLOCK:
  114. return AVERROR(EAGAIN);
  115. case WSAEINTR:
  116. return AVERROR(EINTR);
  117. case WSAEPROTONOSUPPORT:
  118. return AVERROR(EPROTONOSUPPORT);
  119. case WSAETIMEDOUT:
  120. return AVERROR(ETIMEDOUT);
  121. case WSAECONNREFUSED:
  122. return AVERROR(ECONNREFUSED);
  123. case WSAEINPROGRESS:
  124. return AVERROR(EINPROGRESS);
  125. }
  126. return -err;
  127. }
  128. #endif
  129. int ff_is_multicast_address(struct sockaddr *addr)
  130. {
  131. if (addr->sa_family == AF_INET) {
  132. return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
  133. }
  134. #if HAVE_STRUCT_SOCKADDR_IN6
  135. if (addr->sa_family == AF_INET6) {
  136. return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);
  137. }
  138. #endif
  139. return 0;
  140. }
  141. static int ff_poll_interrupt(struct pollfd *p, nfds_t nfds, int timeout,
  142. AVIOInterruptCB *cb)
  143. {
  144. int runs = timeout / POLLING_TIME;
  145. int ret = 0;
  146. do {
  147. if (ff_check_interrupt(cb))
  148. return AVERROR_EXIT;
  149. ret = poll(p, nfds, POLLING_TIME);
  150. if (ret != 0) {
  151. if (ret < 0)
  152. ret = ff_neterrno();
  153. if (ret == AVERROR(EINTR))
  154. continue;
  155. break;
  156. }
  157. } while (timeout <= 0 || runs-- > 0);
  158. if (!ret)
  159. return AVERROR(ETIMEDOUT);
  160. return ret;
  161. }
  162. int ff_socket(int af, int type, int proto)
  163. {
  164. int fd;
  165. #ifdef SOCK_CLOEXEC
  166. fd = socket(af, type | SOCK_CLOEXEC, proto);
  167. if (fd == -1 && errno == EINVAL)
  168. #endif
  169. {
  170. fd = socket(af, type, proto);
  171. #if HAVE_FCNTL
  172. if (fd != -1) {
  173. if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
  174. av_log(NULL, AV_LOG_DEBUG, "Failed to set close on exec\n");
  175. }
  176. #endif
  177. }
  178. #ifdef SO_NOSIGPIPE
  179. if (fd != -1) {
  180. if (setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &(int){1}, sizeof(int))) {
  181. av_log(NULL, AV_LOG_WARNING, "setsockopt(SO_NOSIGPIPE) failed\n");
  182. }
  183. }
  184. #endif
  185. return fd;
  186. }
  187. int ff_listen(int fd, const struct sockaddr *addr,
  188. socklen_t addrlen)
  189. {
  190. int ret;
  191. int reuse = 1;
  192. if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))) {
  193. av_log(NULL, AV_LOG_WARNING, "setsockopt(SO_REUSEADDR) failed\n");
  194. }
  195. ret = bind(fd, addr, addrlen);
  196. if (ret)
  197. return ff_neterrno();
  198. ret = listen(fd, 1);
  199. if (ret)
  200. return ff_neterrno();
  201. return ret;
  202. }
  203. int ff_accept(int fd, int timeout, URLContext *h)
  204. {
  205. int ret;
  206. struct pollfd lp = { fd, POLLIN, 0 };
  207. ret = ff_poll_interrupt(&lp, 1, timeout, &h->interrupt_callback);
  208. if (ret < 0)
  209. return ret;
  210. ret = accept(fd, NULL, NULL);
  211. if (ret < 0)
  212. return ff_neterrno();
  213. if (ff_socket_nonblock(ret, 1) < 0)
  214. av_log(h, AV_LOG_DEBUG, "ff_socket_nonblock failed\n");
  215. return ret;
  216. }
  217. int ff_listen_bind(int fd, const struct sockaddr *addr,
  218. socklen_t addrlen, int timeout, URLContext *h)
  219. {
  220. int ret;
  221. if ((ret = ff_listen(fd, addr, addrlen)) < 0)
  222. return ret;
  223. if ((ret = ff_accept(fd, timeout, h)) < 0)
  224. return ret;
  225. closesocket(fd);
  226. return ret;
  227. }
  228. int ff_listen_connect(int fd, const struct sockaddr *addr,
  229. socklen_t addrlen, int timeout, URLContext *h,
  230. int will_try_next)
  231. {
  232. struct pollfd p = {fd, POLLOUT, 0};
  233. int ret;
  234. socklen_t optlen;
  235. if (ff_socket_nonblock(fd, 1) < 0)
  236. av_log(h, AV_LOG_DEBUG, "ff_socket_nonblock failed\n");
  237. while ((ret = connect(fd, addr, addrlen))) {
  238. ret = ff_neterrno();
  239. switch (ret) {
  240. case AVERROR(EINTR):
  241. if (ff_check_interrupt(&h->interrupt_callback))
  242. return AVERROR_EXIT;
  243. continue;
  244. case AVERROR(EINPROGRESS):
  245. case AVERROR(EAGAIN):
  246. ret = ff_poll_interrupt(&p, 1, timeout, &h->interrupt_callback);
  247. if (ret < 0)
  248. return ret;
  249. optlen = sizeof(ret);
  250. if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen))
  251. ret = AVUNERROR(ff_neterrno());
  252. if (ret != 0) {
  253. char errbuf[100];
  254. ret = AVERROR(ret);
  255. av_strerror(ret, errbuf, sizeof(errbuf));
  256. if (will_try_next)
  257. av_log(h, AV_LOG_WARNING,
  258. "Connection to %s failed (%s), trying next address\n",
  259. h->filename, errbuf);
  260. else
  261. av_log(h, AV_LOG_ERROR, "Connection to %s failed: %s\n",
  262. h->filename, errbuf);
  263. }
  264. default:
  265. return ret;
  266. }
  267. }
  268. return ret;
  269. }
  270. static void interleave_addrinfo(struct addrinfo *base)
  271. {
  272. struct addrinfo **next = &base->ai_next;
  273. while (*next) {
  274. struct addrinfo *cur = *next;
  275. // Iterate forward until we find an entry of a different family.
  276. if (cur->ai_family == base->ai_family) {
  277. next = &cur->ai_next;
  278. continue;
  279. }
  280. if (cur == base->ai_next) {
  281. // If the first one following base is of a different family, just
  282. // move base forward one step and continue.
  283. base = cur;
  284. next = &base->ai_next;
  285. continue;
  286. }
  287. // Unchain cur from the rest of the list from its current spot.
  288. *next = cur->ai_next;
  289. // Hook in cur directly after base.
  290. cur->ai_next = base->ai_next;
  291. base->ai_next = cur;
  292. // Restart with a new base. We know that before moving the cur element,
  293. // everything between the previous base and cur had the same family,
  294. // different from cur->ai_family. Therefore, we can keep next pointing
  295. // where it was, and continue from there with base at the one after
  296. // cur.
  297. base = cur->ai_next;
  298. }
  299. }
  300. static void print_address_list(void *ctx, const struct addrinfo *addr,
  301. const char *title)
  302. {
  303. char hostbuf[100], portbuf[20];
  304. av_log(ctx, AV_LOG_DEBUG, "%s:\n", title);
  305. while (addr) {
  306. getnameinfo(addr->ai_addr, addr->ai_addrlen,
  307. hostbuf, sizeof(hostbuf), portbuf, sizeof(portbuf),
  308. NI_NUMERICHOST | NI_NUMERICSERV);
  309. av_log(ctx, AV_LOG_DEBUG, "Address %s port %s\n", hostbuf, portbuf);
  310. addr = addr->ai_next;
  311. }
  312. }
  313. struct ConnectionAttempt {
  314. int fd;
  315. int64_t deadline_us;
  316. struct addrinfo *addr;
  317. };
  318. // Returns < 0 on error, 0 on successfully started connection attempt,
  319. // > 0 for a connection that succeeded already.
  320. static int start_connect_attempt(struct ConnectionAttempt *attempt,
  321. struct addrinfo **ptr, int timeout_ms,
  322. URLContext *h,
  323. void (*customize_fd)(void *, int), void *customize_ctx)
  324. {
  325. struct addrinfo *ai = *ptr;
  326. int ret;
  327. *ptr = ai->ai_next;
  328. attempt->fd = ff_socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
  329. if (attempt->fd < 0)
  330. return ff_neterrno();
  331. attempt->deadline_us = av_gettime_relative() + timeout_ms * 1000;
  332. attempt->addr = ai;
  333. ff_socket_nonblock(attempt->fd, 1);
  334. if (customize_fd)
  335. customize_fd(customize_ctx, attempt->fd);
  336. while ((ret = connect(attempt->fd, ai->ai_addr, ai->ai_addrlen))) {
  337. ret = ff_neterrno();
  338. switch (ret) {
  339. case AVERROR(EINTR):
  340. if (ff_check_interrupt(&h->interrupt_callback)) {
  341. closesocket(attempt->fd);
  342. attempt->fd = -1;
  343. return AVERROR_EXIT;
  344. }
  345. continue;
  346. case AVERROR(EINPROGRESS):
  347. case AVERROR(EAGAIN):
  348. return 0;
  349. default:
  350. closesocket(attempt->fd);
  351. attempt->fd = -1;
  352. return ret;
  353. }
  354. }
  355. return 1;
  356. }
  357. // Try a new connection to another address after 200 ms, as suggested in
  358. // RFC 8305 (or sooner if an earlier attempt fails).
  359. #define NEXT_ATTEMPT_DELAY_MS 200
  360. int ff_connect_parallel(struct addrinfo *addrs, int timeout_ms_per_address,
  361. int parallel, URLContext *h, int *fd,
  362. void (*customize_fd)(void *, int), void *customize_ctx)
  363. {
  364. struct ConnectionAttempt attempts[3];
  365. struct pollfd pfd[3];
  366. int nb_attempts = 0, i, j;
  367. int64_t next_attempt_us = av_gettime_relative(), next_deadline_us;
  368. int last_err = AVERROR(EIO);
  369. socklen_t optlen;
  370. char errbuf[100], hostbuf[100], portbuf[20];
  371. if (parallel > FF_ARRAY_ELEMS(attempts))
  372. parallel = FF_ARRAY_ELEMS(attempts);
  373. print_address_list(h, addrs, "Original list of addresses");
  374. // This mutates the list, but the head of the list is still the same
  375. // element, so the caller, who owns the list, doesn't need to get
  376. // an updated pointer.
  377. interleave_addrinfo(addrs);
  378. print_address_list(h, addrs, "Interleaved list of addresses");
  379. while (nb_attempts > 0 || addrs) {
  380. // Start a new connection attempt, if possible.
  381. if (nb_attempts < parallel && addrs) {
  382. getnameinfo(addrs->ai_addr, addrs->ai_addrlen,
  383. hostbuf, sizeof(hostbuf), portbuf, sizeof(portbuf),
  384. NI_NUMERICHOST | NI_NUMERICSERV);
  385. av_log(h, AV_LOG_VERBOSE, "Starting connection attempt to %s port %s\n",
  386. hostbuf, portbuf);
  387. last_err = start_connect_attempt(&attempts[nb_attempts], &addrs,
  388. timeout_ms_per_address, h,
  389. customize_fd, customize_ctx);
  390. if (last_err < 0) {
  391. av_strerror(last_err, errbuf, sizeof(errbuf));
  392. av_log(h, AV_LOG_VERBOSE, "Connected attempt failed: %s\n",
  393. errbuf);
  394. continue;
  395. }
  396. if (last_err > 0) {
  397. for (i = 0; i < nb_attempts; i++)
  398. closesocket(attempts[i].fd);
  399. *fd = attempts[nb_attempts].fd;
  400. return 0;
  401. }
  402. pfd[nb_attempts].fd = attempts[nb_attempts].fd;
  403. pfd[nb_attempts].events = POLLOUT;
  404. next_attempt_us = av_gettime_relative() + NEXT_ATTEMPT_DELAY_MS * 1000;
  405. nb_attempts++;
  406. }
  407. av_assert0(nb_attempts > 0);
  408. // The connection attempts are sorted from oldest to newest, so the
  409. // first one will have the earliest deadline.
  410. next_deadline_us = attempts[0].deadline_us;
  411. // If we can start another attempt in parallel, wait until that time.
  412. if (nb_attempts < parallel && addrs)
  413. next_deadline_us = FFMIN(next_deadline_us, next_attempt_us);
  414. last_err = ff_poll_interrupt(pfd, nb_attempts,
  415. (next_deadline_us - av_gettime_relative())/1000,
  416. &h->interrupt_callback);
  417. if (last_err < 0 && last_err != AVERROR(ETIMEDOUT))
  418. break;
  419. // Check the status from the poll output.
  420. for (i = 0; i < nb_attempts; i++) {
  421. last_err = 0;
  422. if (pfd[i].revents) {
  423. // Some sort of action for this socket, check its status (either
  424. // a successful connection or an error).
  425. optlen = sizeof(last_err);
  426. if (getsockopt(attempts[i].fd, SOL_SOCKET, SO_ERROR, &last_err, &optlen))
  427. last_err = ff_neterrno();
  428. else if (last_err != 0)
  429. last_err = AVERROR(last_err);
  430. if (last_err == 0) {
  431. // Everything is ok, we seem to have a successful
  432. // connection. Close other sockets and return this one.
  433. for (j = 0; j < nb_attempts; j++)
  434. if (j != i)
  435. closesocket(attempts[j].fd);
  436. *fd = attempts[i].fd;
  437. getnameinfo(attempts[i].addr->ai_addr, attempts[i].addr->ai_addrlen,
  438. hostbuf, sizeof(hostbuf), portbuf, sizeof(portbuf),
  439. NI_NUMERICHOST | NI_NUMERICSERV);
  440. av_log(h, AV_LOG_VERBOSE, "Successfully connected to %s port %s\n",
  441. hostbuf, portbuf);
  442. return 0;
  443. }
  444. }
  445. if (attempts[i].deadline_us < av_gettime_relative() && !last_err)
  446. last_err = AVERROR(ETIMEDOUT);
  447. if (!last_err)
  448. continue;
  449. // Error (or timeout) for this socket; close the socket and remove
  450. // it from the attempts/pfd arrays, to let a new attempt start
  451. // directly.
  452. getnameinfo(attempts[i].addr->ai_addr, attempts[i].addr->ai_addrlen,
  453. hostbuf, sizeof(hostbuf), portbuf, sizeof(portbuf),
  454. NI_NUMERICHOST | NI_NUMERICSERV);
  455. av_strerror(last_err, errbuf, sizeof(errbuf));
  456. av_log(h, AV_LOG_VERBOSE, "Connection attempt to %s port %s "
  457. "failed: %s\n", hostbuf, portbuf, errbuf);
  458. closesocket(attempts[i].fd);
  459. memmove(&attempts[i], &attempts[i + 1],
  460. (nb_attempts - i - 1) * sizeof(*attempts));
  461. memmove(&pfd[i], &pfd[i + 1],
  462. (nb_attempts - i - 1) * sizeof(*pfd));
  463. i--;
  464. nb_attempts--;
  465. }
  466. }
  467. for (i = 0; i < nb_attempts; i++)
  468. closesocket(attempts[i].fd);
  469. if (last_err >= 0)
  470. last_err = AVERROR(ECONNREFUSED);
  471. if (last_err != AVERROR_EXIT) {
  472. av_strerror(last_err, errbuf, sizeof(errbuf));
  473. av_log(h, AV_LOG_ERROR, "Connection to %s failed: %s\n",
  474. h->filename, errbuf);
  475. }
  476. return last_err;
  477. }
  478. static int match_host_pattern(const char *pattern, const char *hostname)
  479. {
  480. int len_p, len_h;
  481. if (!strcmp(pattern, "*"))
  482. return 1;
  483. // Skip a possible *. at the start of the pattern
  484. if (pattern[0] == '*')
  485. pattern++;
  486. if (pattern[0] == '.')
  487. pattern++;
  488. len_p = strlen(pattern);
  489. len_h = strlen(hostname);
  490. if (len_p > len_h)
  491. return 0;
  492. // Simply check if the end of hostname is equal to 'pattern'
  493. if (!strcmp(pattern, &hostname[len_h - len_p])) {
  494. if (len_h == len_p)
  495. return 1; // Exact match
  496. if (hostname[len_h - len_p - 1] == '.')
  497. return 1; // The matched substring is a domain and not just a substring of a domain
  498. }
  499. return 0;
  500. }
  501. int ff_http_match_no_proxy(const char *no_proxy, const char *hostname)
  502. {
  503. char *buf, *start;
  504. int ret = 0;
  505. if (!no_proxy)
  506. return 0;
  507. if (!hostname)
  508. return 0;
  509. buf = av_strdup(no_proxy);
  510. if (!buf)
  511. return 0;
  512. start = buf;
  513. while (start) {
  514. char *sep, *next = NULL;
  515. start += strspn(start, " ,");
  516. sep = start + strcspn(start, " ,");
  517. if (*sep) {
  518. next = sep + 1;
  519. *sep = '\0';
  520. }
  521. if (match_host_pattern(start, hostname)) {
  522. ret = 1;
  523. break;
  524. }
  525. start = next;
  526. }
  527. av_free(buf);
  528. return ret;
  529. }
  530. void ff_log_net_error(void *ctx, int level, const char* prefix)
  531. {
  532. char errbuf[100];
  533. av_strerror(ff_neterrno(), errbuf, sizeof(errbuf));
  534. av_log(ctx, level, "%s: %s\n", prefix, errbuf);
  535. }