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.

336 lines
8.7KB

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