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.

296 lines
7.4KB

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