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.

229 lines
6.3KB

  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 open_timeout;
  37. int rw_timeout;
  38. int listen_timeout;
  39. } TCPContext;
  40. #define OFFSET(x) offsetof(TCPContext, x)
  41. #define D AV_OPT_FLAG_DECODING_PARAM
  42. #define E AV_OPT_FLAG_ENCODING_PARAM
  43. static const AVOption options[] = {
  44. {"listen", "listen on port instead of connecting", OFFSET(listen), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, D|E },
  45. {"timeout", "timeout of socket i/o operations", OFFSET(rw_timeout), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D|E },
  46. {"listen_timeout", "connection awaiting timeout", OFFSET(listen_timeout), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D|E },
  47. {NULL}
  48. };
  49. static const AVClass tcp_context_class = {
  50. .class_name = "tcp",
  51. .item_name = av_default_item_name,
  52. .option = options,
  53. .version = LIBAVUTIL_VERSION_INT,
  54. };
  55. /* return non zero if error */
  56. static int tcp_open(URLContext *h, const char *uri, int flags)
  57. {
  58. struct addrinfo hints = { 0 }, *ai, *cur_ai;
  59. int port, fd = -1;
  60. TCPContext *s = h->priv_data;
  61. const char *p;
  62. char buf[256];
  63. int ret;
  64. char hostname[1024],proto[1024],path[1024];
  65. char portstr[10];
  66. s->open_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. if (s->rw_timeout >= 0) {
  87. s->open_timeout =
  88. h->rw_timeout = s->rw_timeout;
  89. }
  90. hints.ai_family = AF_UNSPEC;
  91. hints.ai_socktype = SOCK_STREAM;
  92. snprintf(portstr, sizeof(portstr), "%d", port);
  93. if (s->listen)
  94. hints.ai_flags |= AI_PASSIVE;
  95. if (!hostname[0])
  96. ret = getaddrinfo(NULL, portstr, &hints, &ai);
  97. else
  98. ret = getaddrinfo(hostname, portstr, &hints, &ai);
  99. if (ret) {
  100. av_log(h, AV_LOG_ERROR,
  101. "Failed to resolve hostname %s: %s\n",
  102. hostname, gai_strerror(ret));
  103. return AVERROR(EIO);
  104. }
  105. cur_ai = ai;
  106. restart:
  107. fd = ff_socket(cur_ai->ai_family,
  108. cur_ai->ai_socktype,
  109. cur_ai->ai_protocol);
  110. if (fd < 0) {
  111. ret = ff_neterrno();
  112. goto fail;
  113. }
  114. if (s->listen) {
  115. if ((fd = ff_listen_bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
  116. s->listen_timeout, h)) < 0) {
  117. ret = fd;
  118. goto fail1;
  119. }
  120. } else {
  121. if ((ret = ff_listen_connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
  122. s->open_timeout / 1000, h, !!cur_ai->ai_next)) < 0) {
  123. if (ret == AVERROR_EXIT)
  124. goto fail1;
  125. else
  126. goto fail;
  127. }
  128. }
  129. h->is_streamed = 1;
  130. s->fd = fd;
  131. freeaddrinfo(ai);
  132. return 0;
  133. fail:
  134. if (cur_ai->ai_next) {
  135. /* Retry with the next sockaddr */
  136. cur_ai = cur_ai->ai_next;
  137. if (fd >= 0)
  138. closesocket(fd);
  139. ret = 0;
  140. goto restart;
  141. }
  142. fail1:
  143. if (fd >= 0)
  144. closesocket(fd);
  145. freeaddrinfo(ai);
  146. return ret;
  147. }
  148. static int tcp_read(URLContext *h, uint8_t *buf, int size)
  149. {
  150. TCPContext *s = h->priv_data;
  151. int ret;
  152. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  153. ret = ff_network_wait_fd_timeout(s->fd, 0, h->rw_timeout, &h->interrupt_callback);
  154. if (ret)
  155. return ret;
  156. }
  157. ret = recv(s->fd, buf, size, 0);
  158. return ret < 0 ? ff_neterrno() : ret;
  159. }
  160. static int tcp_write(URLContext *h, const uint8_t *buf, int size)
  161. {
  162. TCPContext *s = h->priv_data;
  163. int ret;
  164. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  165. ret = ff_network_wait_fd_timeout(s->fd, 1, h->rw_timeout, &h->interrupt_callback);
  166. if (ret)
  167. return ret;
  168. }
  169. ret = send(s->fd, buf, size, 0);
  170. return ret < 0 ? ff_neterrno() : ret;
  171. }
  172. static int tcp_shutdown(URLContext *h, int flags)
  173. {
  174. TCPContext *s = h->priv_data;
  175. int how;
  176. if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
  177. how = SHUT_RDWR;
  178. } else if (flags & AVIO_FLAG_WRITE) {
  179. how = SHUT_WR;
  180. } else {
  181. how = SHUT_RD;
  182. }
  183. return shutdown(s->fd, how);
  184. }
  185. static int tcp_close(URLContext *h)
  186. {
  187. TCPContext *s = h->priv_data;
  188. closesocket(s->fd);
  189. return 0;
  190. }
  191. static int tcp_get_file_handle(URLContext *h)
  192. {
  193. TCPContext *s = h->priv_data;
  194. return s->fd;
  195. }
  196. URLProtocol ff_tcp_protocol = {
  197. .name = "tcp",
  198. .url_open = tcp_open,
  199. .url_read = tcp_read,
  200. .url_write = tcp_write,
  201. .url_close = tcp_close,
  202. .url_get_file_handle = tcp_get_file_handle,
  203. .url_shutdown = tcp_shutdown,
  204. .priv_data_size = sizeof(TCPContext),
  205. .priv_data_class = &tcp_context_class,
  206. .flags = URL_PROTOCOL_FLAG_NETWORK,
  207. };