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.

283 lines
8.0KB

  1. /*
  2. * TCP protocol
  3. * Copyright (c) 2002 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avformat.h"
  22. #include "libavutil/parseutils.h"
  23. #include "libavutil/opt.h"
  24. #include "libavutil/time.h"
  25. #include "internal.h"
  26. #include "network.h"
  27. #include "os_support.h"
  28. #include "url.h"
  29. #if HAVE_POLL_H
  30. #include <poll.h>
  31. #endif
  32. typedef struct TCPContext {
  33. const AVClass *class;
  34. int fd;
  35. int listen;
  36. int rw_timeout;
  37. int listen_timeout;
  38. } TCPContext;
  39. #define OFFSET(x) offsetof(TCPContext, x)
  40. #define D AV_OPT_FLAG_DECODING_PARAM
  41. #define E AV_OPT_FLAG_ENCODING_PARAM
  42. static const AVOption options[] = {
  43. {"listen", "listen on port instead of connecting", OFFSET(listen), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, D|E },
  44. {"timeout", "timeout of socket i/o operations", OFFSET(rw_timeout), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, D|E },
  45. {"listen_timeout", "connection awaiting timeout", OFFSET(listen_timeout), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D|E },
  46. {NULL}
  47. };
  48. static const AVClass tcp_context_class = {
  49. .class_name = "tcp",
  50. .item_name = av_default_item_name,
  51. .option = options,
  52. .version = LIBAVUTIL_VERSION_INT,
  53. };
  54. /* return non zero if error */
  55. static int tcp_open(URLContext *h, const char *uri, int flags)
  56. {
  57. struct addrinfo hints = { 0 }, *ai, *cur_ai;
  58. int port, fd = -1;
  59. TCPContext *s = h->priv_data;
  60. const char *p;
  61. char buf[256];
  62. int ret;
  63. socklen_t optlen;
  64. char hostname[1024],proto[1024],path[1024];
  65. char portstr[10];
  66. h->rw_timeout = 5000000;
  67. av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
  68. &port, path, sizeof(path), uri);
  69. if (strcmp(proto, "tcp"))
  70. return AVERROR(EINVAL);
  71. if (port <= 0 || port >= 65536) {
  72. av_log(h, AV_LOG_ERROR, "Port missing in uri\n");
  73. return AVERROR(EINVAL);
  74. }
  75. p = strchr(uri, '?');
  76. if (p) {
  77. if (av_find_info_tag(buf, sizeof(buf), "listen", p))
  78. s->listen = 1;
  79. if (av_find_info_tag(buf, sizeof(buf), "timeout", p)) {
  80. s->rw_timeout = strtol(buf, NULL, 10);
  81. }
  82. if (av_find_info_tag(buf, sizeof(buf), "listen_timeout", p)) {
  83. s->listen_timeout = strtol(buf, NULL, 10);
  84. }
  85. }
  86. h->rw_timeout = s->rw_timeout;
  87. hints.ai_family = AF_UNSPEC;
  88. hints.ai_socktype = SOCK_STREAM;
  89. snprintf(portstr, sizeof(portstr), "%d", port);
  90. if (s->listen)
  91. hints.ai_flags |= AI_PASSIVE;
  92. if (!hostname[0])
  93. ret = getaddrinfo(NULL, portstr, &hints, &ai);
  94. else
  95. ret = getaddrinfo(hostname, portstr, &hints, &ai);
  96. if (ret) {
  97. av_log(h, AV_LOG_ERROR,
  98. "Failed to resolve hostname %s: %s\n",
  99. hostname, gai_strerror(ret));
  100. return AVERROR(EIO);
  101. }
  102. cur_ai = ai;
  103. restart:
  104. ret = AVERROR(EIO);
  105. fd = socket(cur_ai->ai_family, cur_ai->ai_socktype, cur_ai->ai_protocol);
  106. if (fd < 0)
  107. goto fail;
  108. if (s->listen) {
  109. int fd1;
  110. int reuse = 1;
  111. struct pollfd lp = { fd, POLLIN, 0 };
  112. setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
  113. ret = bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
  114. if (ret) {
  115. ret = ff_neterrno();
  116. goto fail1;
  117. }
  118. ret = listen(fd, 1);
  119. if (ret) {
  120. ret = ff_neterrno();
  121. goto fail1;
  122. }
  123. ret = poll(&lp, 1, s->listen_timeout >= 0 ? s->listen_timeout : -1);
  124. if (ret <= 0) {
  125. ret = AVERROR(ETIMEDOUT);
  126. goto fail1;
  127. }
  128. fd1 = accept(fd, NULL, NULL);
  129. if (fd1 < 0) {
  130. ret = ff_neterrno();
  131. goto fail1;
  132. }
  133. closesocket(fd);
  134. fd = fd1;
  135. ff_socket_nonblock(fd, 1);
  136. } else {
  137. redo:
  138. ff_socket_nonblock(fd, 1);
  139. ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
  140. }
  141. if (ret < 0) {
  142. struct pollfd p = {fd, POLLOUT, 0};
  143. int64_t wait_started;
  144. ret = ff_neterrno();
  145. if (ret == AVERROR(EINTR)) {
  146. if (ff_check_interrupt(&h->interrupt_callback)) {
  147. ret = AVERROR_EXIT;
  148. goto fail1;
  149. }
  150. goto redo;
  151. }
  152. if (ret != AVERROR(EINPROGRESS) &&
  153. ret != AVERROR(EAGAIN))
  154. goto fail;
  155. /* wait until we are connected or until abort */
  156. wait_started = av_gettime();
  157. do {
  158. if (ff_check_interrupt(&h->interrupt_callback)) {
  159. ret = AVERROR_EXIT;
  160. goto fail1;
  161. }
  162. ret = poll(&p, 1, 100);
  163. if (ret > 0)
  164. break;
  165. } while (!h->rw_timeout || (av_gettime() - wait_started < h->rw_timeout));
  166. if (ret <= 0) {
  167. ret = AVERROR(ETIMEDOUT);
  168. goto fail;
  169. }
  170. /* test error */
  171. optlen = sizeof(ret);
  172. if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen))
  173. ret = AVUNERROR(ff_neterrno());
  174. if (ret != 0) {
  175. char errbuf[100];
  176. ret = AVERROR(ret);
  177. av_strerror(ret, errbuf, sizeof(errbuf));
  178. av_log(h, AV_LOG_ERROR,
  179. "TCP connection to %s:%d failed: %s\n",
  180. hostname, port, errbuf);
  181. goto fail;
  182. }
  183. }
  184. h->is_streamed = 1;
  185. s->fd = fd;
  186. freeaddrinfo(ai);
  187. return 0;
  188. fail:
  189. if (cur_ai->ai_next) {
  190. /* Retry with the next sockaddr */
  191. cur_ai = cur_ai->ai_next;
  192. if (fd >= 0)
  193. closesocket(fd);
  194. goto restart;
  195. }
  196. fail1:
  197. if (fd >= 0)
  198. closesocket(fd);
  199. freeaddrinfo(ai);
  200. return ret;
  201. }
  202. static int tcp_read(URLContext *h, uint8_t *buf, int size)
  203. {
  204. TCPContext *s = h->priv_data;
  205. int ret;
  206. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  207. ret = ff_network_wait_fd_timeout(s->fd, 0, h->rw_timeout, &h->interrupt_callback);
  208. if (ret)
  209. return ret;
  210. }
  211. ret = recv(s->fd, buf, size, 0);
  212. return ret < 0 ? ff_neterrno() : ret;
  213. }
  214. static int tcp_write(URLContext *h, const uint8_t *buf, int size)
  215. {
  216. TCPContext *s = h->priv_data;
  217. int ret;
  218. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  219. ret = ff_network_wait_fd_timeout(s->fd, 1, h->rw_timeout, &h->interrupt_callback);
  220. if (ret)
  221. return ret;
  222. }
  223. ret = send(s->fd, buf, size, 0);
  224. return ret < 0 ? ff_neterrno() : ret;
  225. }
  226. static int tcp_shutdown(URLContext *h, int flags)
  227. {
  228. TCPContext *s = h->priv_data;
  229. int how;
  230. if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
  231. how = SHUT_RDWR;
  232. } else if (flags & AVIO_FLAG_WRITE) {
  233. how = SHUT_WR;
  234. } else {
  235. how = SHUT_RD;
  236. }
  237. return shutdown(s->fd, how);
  238. }
  239. static int tcp_close(URLContext *h)
  240. {
  241. TCPContext *s = h->priv_data;
  242. closesocket(s->fd);
  243. return 0;
  244. }
  245. static int tcp_get_file_handle(URLContext *h)
  246. {
  247. TCPContext *s = h->priv_data;
  248. return s->fd;
  249. }
  250. URLProtocol ff_tcp_protocol = {
  251. .name = "tcp",
  252. .url_open = tcp_open,
  253. .url_read = tcp_read,
  254. .url_write = tcp_write,
  255. .url_close = tcp_close,
  256. .url_get_file_handle = tcp_get_file_handle,
  257. .url_shutdown = tcp_shutdown,
  258. .priv_data_size = sizeof(TCPContext),
  259. .priv_data_class = &tcp_context_class,
  260. .flags = URL_PROTOCOL_FLAG_NETWORK,
  261. };