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.

279 lines
8.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/avassert.h"
  23. #include "libavutil/parseutils.h"
  24. #include "libavutil/opt.h"
  25. #include "libavutil/time.h"
  26. #include "internal.h"
  27. #include "network.h"
  28. #include "os_support.h"
  29. #include "url.h"
  30. #if HAVE_POLL_H
  31. #include <poll.h>
  32. #endif
  33. typedef struct TCPContext {
  34. const AVClass *class;
  35. int fd;
  36. int listen;
  37. int open_timeout;
  38. int rw_timeout;
  39. int listen_timeout;
  40. int recv_buffer_size;
  41. int send_buffer_size;
  42. } TCPContext;
  43. #define OFFSET(x) offsetof(TCPContext, x)
  44. #define D AV_OPT_FLAG_DECODING_PARAM
  45. #define E AV_OPT_FLAG_ENCODING_PARAM
  46. static const AVOption options[] = {
  47. { "listen", "Listen for incoming connections", OFFSET(listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, .flags = D|E },
  48. { "timeout", "set timeout (in microseconds) of socket I/O operations", OFFSET(rw_timeout), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
  49. { "listen_timeout", "Connection awaiting timeout (in milliseconds)", OFFSET(listen_timeout), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
  50. { "send_buffer_size", "Socket send buffer size (in bytes)", OFFSET(send_buffer_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
  51. { "recv_buffer_size", "Socket receive buffer size (in bytes)", OFFSET(recv_buffer_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
  52. { NULL }
  53. };
  54. static const AVClass tcp_class = {
  55. .class_name = "tcp",
  56. .item_name = av_default_item_name,
  57. .option = options,
  58. .version = LIBAVUTIL_VERSION_INT,
  59. };
  60. /* return non zero if error */
  61. static int tcp_open(URLContext *h, const char *uri, int flags)
  62. {
  63. struct addrinfo hints = { 0 }, *ai, *cur_ai;
  64. int port, fd = -1;
  65. TCPContext *s = h->priv_data;
  66. const char *p;
  67. char buf[256];
  68. int ret;
  69. char hostname[1024],proto[1024],path[1024];
  70. char portstr[10];
  71. s->open_timeout = 5000000;
  72. av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
  73. &port, path, sizeof(path), uri);
  74. if (strcmp(proto, "tcp"))
  75. return AVERROR(EINVAL);
  76. if (port <= 0 || port >= 65536) {
  77. av_log(h, AV_LOG_ERROR, "Port missing in uri\n");
  78. return AVERROR(EINVAL);
  79. }
  80. p = strchr(uri, '?');
  81. if (p) {
  82. if (av_find_info_tag(buf, sizeof(buf), "listen", p)) {
  83. char *endptr = NULL;
  84. s->listen = strtol(buf, &endptr, 10);
  85. /* assume if no digits were found it is a request to enable it */
  86. if (buf == endptr)
  87. s->listen = 1;
  88. }
  89. if (av_find_info_tag(buf, sizeof(buf), "timeout", p)) {
  90. s->rw_timeout = strtol(buf, NULL, 10);
  91. }
  92. if (av_find_info_tag(buf, sizeof(buf), "listen_timeout", p)) {
  93. s->listen_timeout = strtol(buf, NULL, 10);
  94. }
  95. }
  96. if (s->rw_timeout >= 0) {
  97. s->open_timeout =
  98. h->rw_timeout = s->rw_timeout;
  99. }
  100. hints.ai_family = AF_UNSPEC;
  101. hints.ai_socktype = SOCK_STREAM;
  102. snprintf(portstr, sizeof(portstr), "%d", port);
  103. if (s->listen)
  104. hints.ai_flags |= AI_PASSIVE;
  105. if (!hostname[0])
  106. ret = getaddrinfo(NULL, portstr, &hints, &ai);
  107. else
  108. ret = getaddrinfo(hostname, portstr, &hints, &ai);
  109. if (ret) {
  110. av_log(h, AV_LOG_ERROR,
  111. "Failed to resolve hostname %s: %s\n",
  112. hostname, gai_strerror(ret));
  113. return AVERROR(EIO);
  114. }
  115. cur_ai = ai;
  116. restart:
  117. // workaround for IOS9 getaddrinfo in IPv6 only network use hardcode IPv4 address can not resolve port number.
  118. if (cur_ai->ai_family == AF_INET6){
  119. struct sockaddr_in6 * sockaddr_v6 = (struct sockaddr_in6 *)cur_ai->ai_addr;
  120. if (!sockaddr_v6->sin6_port){
  121. sockaddr_v6->sin6_port = htons(port);
  122. }
  123. }
  124. fd = ff_socket(cur_ai->ai_family,
  125. cur_ai->ai_socktype,
  126. cur_ai->ai_protocol);
  127. if (fd < 0) {
  128. ret = ff_neterrno();
  129. goto fail;
  130. }
  131. if (s->listen == 2) {
  132. // multi-client
  133. if ((ret = ff_listen(fd, cur_ai->ai_addr, cur_ai->ai_addrlen)) < 0)
  134. goto fail1;
  135. } else if (s->listen == 1) {
  136. // single client
  137. if ((ret = ff_listen_bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
  138. s->listen_timeout, h)) < 0)
  139. goto fail1;
  140. // Socket descriptor already closed here. Safe to overwrite to client one.
  141. fd = ret;
  142. } else {
  143. if ((ret = ff_listen_connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
  144. s->open_timeout / 1000, h, !!cur_ai->ai_next)) < 0) {
  145. if (ret == AVERROR_EXIT)
  146. goto fail1;
  147. else
  148. goto fail;
  149. }
  150. }
  151. h->is_streamed = 1;
  152. s->fd = fd;
  153. /* Set the socket's send or receive buffer sizes, if specified.
  154. If unspecified or setting fails, system default is used. */
  155. if (s->recv_buffer_size > 0) {
  156. setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &s->recv_buffer_size, sizeof (s->recv_buffer_size));
  157. }
  158. if (s->send_buffer_size > 0) {
  159. setsockopt (fd, SOL_SOCKET, SO_SNDBUF, &s->send_buffer_size, sizeof (s->send_buffer_size));
  160. }
  161. freeaddrinfo(ai);
  162. return 0;
  163. fail:
  164. if (cur_ai->ai_next) {
  165. /* Retry with the next sockaddr */
  166. cur_ai = cur_ai->ai_next;
  167. if (fd >= 0)
  168. closesocket(fd);
  169. ret = 0;
  170. goto restart;
  171. }
  172. fail1:
  173. if (fd >= 0)
  174. closesocket(fd);
  175. freeaddrinfo(ai);
  176. return ret;
  177. }
  178. static int tcp_accept(URLContext *s, URLContext **c)
  179. {
  180. TCPContext *sc = s->priv_data;
  181. TCPContext *cc;
  182. int ret;
  183. av_assert0(sc->listen);
  184. if ((ret = ffurl_alloc(c, s->filename, s->flags, &s->interrupt_callback)) < 0)
  185. return ret;
  186. cc = (*c)->priv_data;
  187. ret = ff_accept(sc->fd, sc->listen_timeout, s);
  188. if (ret < 0)
  189. return ff_neterrno();
  190. cc->fd = ret;
  191. return 0;
  192. }
  193. static int tcp_read(URLContext *h, uint8_t *buf, int size)
  194. {
  195. TCPContext *s = h->priv_data;
  196. int ret;
  197. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  198. ret = ff_network_wait_fd_timeout(s->fd, 0, h->rw_timeout, &h->interrupt_callback);
  199. if (ret)
  200. return ret;
  201. }
  202. ret = recv(s->fd, buf, size, 0);
  203. return ret < 0 ? ff_neterrno() : ret;
  204. }
  205. static int tcp_write(URLContext *h, const uint8_t *buf, int size)
  206. {
  207. TCPContext *s = h->priv_data;
  208. int ret;
  209. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  210. ret = ff_network_wait_fd_timeout(s->fd, 1, h->rw_timeout, &h->interrupt_callback);
  211. if (ret)
  212. return ret;
  213. }
  214. ret = send(s->fd, buf, size, MSG_NOSIGNAL);
  215. return ret < 0 ? ff_neterrno() : ret;
  216. }
  217. static int tcp_shutdown(URLContext *h, int flags)
  218. {
  219. TCPContext *s = h->priv_data;
  220. int how;
  221. if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
  222. how = SHUT_RDWR;
  223. } else if (flags & AVIO_FLAG_WRITE) {
  224. how = SHUT_WR;
  225. } else {
  226. how = SHUT_RD;
  227. }
  228. return shutdown(s->fd, how);
  229. }
  230. static int tcp_close(URLContext *h)
  231. {
  232. TCPContext *s = h->priv_data;
  233. closesocket(s->fd);
  234. return 0;
  235. }
  236. static int tcp_get_file_handle(URLContext *h)
  237. {
  238. TCPContext *s = h->priv_data;
  239. return s->fd;
  240. }
  241. const URLProtocol ff_tcp_protocol = {
  242. .name = "tcp",
  243. .url_open = tcp_open,
  244. .url_accept = tcp_accept,
  245. .url_read = tcp_read,
  246. .url_write = tcp_write,
  247. .url_close = tcp_close,
  248. .url_get_file_handle = tcp_get_file_handle,
  249. .url_shutdown = tcp_shutdown,
  250. .priv_data_size = sizeof(TCPContext),
  251. .flags = URL_PROTOCOL_FLAG_NETWORK,
  252. .priv_data_class = &tcp_class,
  253. };