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.

285 lines
7.2KB

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