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.

227 lines
6.2KB

  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 = socket(cur_ai->ai_family, cur_ai->ai_socktype, cur_ai->ai_protocol);
  108. if (fd < 0) {
  109. ret = ff_neterrno();
  110. goto fail;
  111. }
  112. if (s->listen) {
  113. if ((fd = ff_listen_bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
  114. s->listen_timeout, h)) < 0) {
  115. ret = fd;
  116. goto fail1;
  117. }
  118. } else {
  119. if ((ret = ff_listen_connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
  120. s->open_timeout / 1000, h)) < 0) {
  121. if (ret == AVERROR_EXIT)
  122. goto fail1;
  123. else
  124. goto fail;
  125. }
  126. }
  127. h->is_streamed = 1;
  128. s->fd = fd;
  129. freeaddrinfo(ai);
  130. return 0;
  131. fail:
  132. if (cur_ai->ai_next) {
  133. /* Retry with the next sockaddr */
  134. cur_ai = cur_ai->ai_next;
  135. if (fd >= 0)
  136. closesocket(fd);
  137. ret = 0;
  138. goto restart;
  139. }
  140. fail1:
  141. if (fd >= 0)
  142. closesocket(fd);
  143. freeaddrinfo(ai);
  144. return ret;
  145. }
  146. static int tcp_read(URLContext *h, uint8_t *buf, int size)
  147. {
  148. TCPContext *s = h->priv_data;
  149. int ret;
  150. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  151. ret = ff_network_wait_fd_timeout(s->fd, 0, h->rw_timeout, &h->interrupt_callback);
  152. if (ret)
  153. return ret;
  154. }
  155. ret = recv(s->fd, buf, size, 0);
  156. return ret < 0 ? ff_neterrno() : ret;
  157. }
  158. static int tcp_write(URLContext *h, const uint8_t *buf, int size)
  159. {
  160. TCPContext *s = h->priv_data;
  161. int ret;
  162. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  163. ret = ff_network_wait_fd_timeout(s->fd, 1, h->rw_timeout, &h->interrupt_callback);
  164. if (ret)
  165. return ret;
  166. }
  167. ret = send(s->fd, buf, size, 0);
  168. return ret < 0 ? ff_neterrno() : ret;
  169. }
  170. static int tcp_shutdown(URLContext *h, int flags)
  171. {
  172. TCPContext *s = h->priv_data;
  173. int how;
  174. if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
  175. how = SHUT_RDWR;
  176. } else if (flags & AVIO_FLAG_WRITE) {
  177. how = SHUT_WR;
  178. } else {
  179. how = SHUT_RD;
  180. }
  181. return shutdown(s->fd, how);
  182. }
  183. static int tcp_close(URLContext *h)
  184. {
  185. TCPContext *s = h->priv_data;
  186. closesocket(s->fd);
  187. return 0;
  188. }
  189. static int tcp_get_file_handle(URLContext *h)
  190. {
  191. TCPContext *s = h->priv_data;
  192. return s->fd;
  193. }
  194. URLProtocol ff_tcp_protocol = {
  195. .name = "tcp",
  196. .url_open = tcp_open,
  197. .url_read = tcp_read,
  198. .url_write = tcp_write,
  199. .url_close = tcp_close,
  200. .url_get_file_handle = tcp_get_file_handle,
  201. .url_shutdown = tcp_shutdown,
  202. .priv_data_size = sizeof(TCPContext),
  203. .priv_data_class = &tcp_context_class,
  204. .flags = URL_PROTOCOL_FLAG_NETWORK,
  205. };