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.

234 lines
6.5KB

  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", "set timeout of socket I/O operations", OFFSET(rw_timeout), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D|E },
  46. {"listen_timeout", "set 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. char *endptr = NULL;
  79. s->listen = strtol(buf, &endptr, 10);
  80. /* assume if no digits were found it is a request to enable it */
  81. if (buf == endptr)
  82. s->listen = 1;
  83. }
  84. if (av_find_info_tag(buf, sizeof(buf), "timeout", p)) {
  85. s->rw_timeout = strtol(buf, NULL, 10);
  86. }
  87. if (av_find_info_tag(buf, sizeof(buf), "listen_timeout", p)) {
  88. s->listen_timeout = strtol(buf, NULL, 10);
  89. }
  90. }
  91. if (s->rw_timeout >= 0) {
  92. s->open_timeout =
  93. h->rw_timeout = s->rw_timeout;
  94. }
  95. hints.ai_family = AF_UNSPEC;
  96. hints.ai_socktype = SOCK_STREAM;
  97. snprintf(portstr, sizeof(portstr), "%d", port);
  98. if (s->listen)
  99. hints.ai_flags |= AI_PASSIVE;
  100. if (!hostname[0])
  101. ret = getaddrinfo(NULL, portstr, &hints, &ai);
  102. else
  103. ret = getaddrinfo(hostname, portstr, &hints, &ai);
  104. if (ret) {
  105. av_log(h, AV_LOG_ERROR,
  106. "Failed to resolve hostname %s: %s\n",
  107. hostname, gai_strerror(ret));
  108. return AVERROR(EIO);
  109. }
  110. cur_ai = ai;
  111. restart:
  112. fd = ff_socket(cur_ai->ai_family,
  113. cur_ai->ai_socktype,
  114. cur_ai->ai_protocol);
  115. if (fd < 0) {
  116. ret = ff_neterrno();
  117. goto fail;
  118. }
  119. if (s->listen) {
  120. if ((fd = ff_listen_bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
  121. s->listen_timeout, h)) < 0) {
  122. ret = fd;
  123. goto fail1;
  124. }
  125. } else {
  126. if ((ret = ff_listen_connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
  127. s->open_timeout / 1000, h, !!cur_ai->ai_next)) < 0) {
  128. if (ret == AVERROR_EXIT)
  129. goto fail1;
  130. else
  131. goto fail;
  132. }
  133. }
  134. h->is_streamed = 1;
  135. s->fd = fd;
  136. freeaddrinfo(ai);
  137. return 0;
  138. fail:
  139. if (cur_ai->ai_next) {
  140. /* Retry with the next sockaddr */
  141. cur_ai = cur_ai->ai_next;
  142. if (fd >= 0)
  143. closesocket(fd);
  144. ret = 0;
  145. goto restart;
  146. }
  147. fail1:
  148. if (fd >= 0)
  149. closesocket(fd);
  150. freeaddrinfo(ai);
  151. return ret;
  152. }
  153. static int tcp_read(URLContext *h, uint8_t *buf, int size)
  154. {
  155. TCPContext *s = h->priv_data;
  156. int ret;
  157. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  158. ret = ff_network_wait_fd_timeout(s->fd, 0, h->rw_timeout, &h->interrupt_callback);
  159. if (ret)
  160. return ret;
  161. }
  162. ret = recv(s->fd, buf, size, 0);
  163. return ret < 0 ? ff_neterrno() : ret;
  164. }
  165. static int tcp_write(URLContext *h, const uint8_t *buf, int size)
  166. {
  167. TCPContext *s = h->priv_data;
  168. int ret;
  169. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  170. ret = ff_network_wait_fd_timeout(s->fd, 1, h->rw_timeout, &h->interrupt_callback);
  171. if (ret)
  172. return ret;
  173. }
  174. ret = send(s->fd, buf, size, 0);
  175. return ret < 0 ? ff_neterrno() : ret;
  176. }
  177. static int tcp_shutdown(URLContext *h, int flags)
  178. {
  179. TCPContext *s = h->priv_data;
  180. int how;
  181. if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
  182. how = SHUT_RDWR;
  183. } else if (flags & AVIO_FLAG_WRITE) {
  184. how = SHUT_WR;
  185. } else {
  186. how = SHUT_RD;
  187. }
  188. return shutdown(s->fd, how);
  189. }
  190. static int tcp_close(URLContext *h)
  191. {
  192. TCPContext *s = h->priv_data;
  193. closesocket(s->fd);
  194. return 0;
  195. }
  196. static int tcp_get_file_handle(URLContext *h)
  197. {
  198. TCPContext *s = h->priv_data;
  199. return s->fd;
  200. }
  201. URLProtocol ff_tcp_protocol = {
  202. .name = "tcp",
  203. .url_open = tcp_open,
  204. .url_read = tcp_read,
  205. .url_write = tcp_write,
  206. .url_close = tcp_close,
  207. .url_get_file_handle = tcp_get_file_handle,
  208. .url_shutdown = tcp_shutdown,
  209. .priv_data_size = sizeof(TCPContext),
  210. .priv_data_class = &tcp_context_class,
  211. .flags = URL_PROTOCOL_FLAG_NETWORK,
  212. };