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.

289 lines
7.3KB

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