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.

282 lines
7.9KB

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