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.

359 lines
9.0KB

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