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.

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