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.

317 lines
8.2KB

  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, 100);
  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. return fd;
  166. }
  167. int ff_listen_bind(int fd, const struct sockaddr *addr,
  168. socklen_t addrlen, int timeout, URLContext *h)
  169. {
  170. int ret;
  171. int reuse = 1;
  172. struct pollfd lp = { fd, POLLIN, 0 };
  173. if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))) {
  174. av_log(NULL, AV_LOG_WARNING, "setsockopt(SO_REUSEADDR) failed\n");
  175. }
  176. ret = bind(fd, addr, addrlen);
  177. if (ret)
  178. return ff_neterrno();
  179. ret = listen(fd, 1);
  180. if (ret)
  181. return ff_neterrno();
  182. ret = ff_poll_interrupt(&lp, 1, timeout, &h->interrupt_callback);
  183. if (ret < 0)
  184. return ret;
  185. ret = accept(fd, NULL, NULL);
  186. if (ret < 0)
  187. return ff_neterrno();
  188. closesocket(fd);
  189. if (ff_socket_nonblock(ret, 1) < 0)
  190. av_log(NULL, AV_LOG_DEBUG, "ff_socket_nonblock failed\n");
  191. return ret;
  192. }
  193. int ff_listen_connect(int fd, const struct sockaddr *addr,
  194. socklen_t addrlen, int timeout, URLContext *h,
  195. int will_try_next)
  196. {
  197. struct pollfd p = {fd, POLLOUT, 0};
  198. int ret;
  199. socklen_t optlen;
  200. if (ff_socket_nonblock(fd, 1) < 0)
  201. av_log(NULL, AV_LOG_DEBUG, "ff_socket_nonblock failed\n");
  202. while ((ret = connect(fd, addr, addrlen))) {
  203. ret = ff_neterrno();
  204. switch (ret) {
  205. case AVERROR(EINTR):
  206. if (ff_check_interrupt(&h->interrupt_callback))
  207. return AVERROR_EXIT;
  208. continue;
  209. case AVERROR(EINPROGRESS):
  210. case AVERROR(EAGAIN):
  211. ret = ff_poll_interrupt(&p, 1, timeout, &h->interrupt_callback);
  212. if (ret < 0)
  213. return ret;
  214. optlen = sizeof(ret);
  215. if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen))
  216. ret = AVUNERROR(ff_neterrno());
  217. if (ret != 0) {
  218. char errbuf[100];
  219. ret = AVERROR(ret);
  220. av_strerror(ret, errbuf, sizeof(errbuf));
  221. if (will_try_next)
  222. av_log(h, AV_LOG_WARNING,
  223. "Connection to %s failed (%s), trying next address\n",
  224. h->filename, errbuf);
  225. else
  226. av_log(h, AV_LOG_ERROR, "Connection to %s failed: %s\n",
  227. h->filename, errbuf);
  228. }
  229. default:
  230. return ret;
  231. }
  232. }
  233. return ret;
  234. }
  235. static int match_host_pattern(const char *pattern, const char *hostname)
  236. {
  237. int len_p, len_h;
  238. if (!strcmp(pattern, "*"))
  239. return 1;
  240. // Skip a possible *. at the start of the pattern
  241. if (pattern[0] == '*')
  242. pattern++;
  243. if (pattern[0] == '.')
  244. pattern++;
  245. len_p = strlen(pattern);
  246. len_h = strlen(hostname);
  247. if (len_p > len_h)
  248. return 0;
  249. // Simply check if the end of hostname is equal to 'pattern'
  250. if (!strcmp(pattern, &hostname[len_h - len_p])) {
  251. if (len_h == len_p)
  252. return 1; // Exact match
  253. if (hostname[len_h - len_p - 1] == '.')
  254. return 1; // The matched substring is a domain and not just a substring of a domain
  255. }
  256. return 0;
  257. }
  258. int ff_http_match_no_proxy(const char *no_proxy, const char *hostname)
  259. {
  260. char *buf, *start;
  261. int ret = 0;
  262. if (!no_proxy)
  263. return 0;
  264. if (!hostname)
  265. return 0;
  266. buf = av_strdup(no_proxy);
  267. if (!buf)
  268. return 0;
  269. start = buf;
  270. while (start) {
  271. char *sep, *next = NULL;
  272. start += strspn(start, " ,");
  273. sep = start + strcspn(start, " ,");
  274. if (*sep) {
  275. next = sep + 1;
  276. *sep = '\0';
  277. }
  278. if (match_host_pattern(start, hostname)) {
  279. ret = 1;
  280. break;
  281. }
  282. start = next;
  283. }
  284. av_free(buf);
  285. return ret;
  286. }