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.

321 lines
8.3KB

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