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.1KB

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