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.

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