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.

303 lines
9.1KB

  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. #if HAVE_STRUCT_SOCKADDR_IN6
  118. // workaround for IOS9 getaddrinfo in IPv6 only network use hardcode IPv4 address can not resolve port number.
  119. if (cur_ai->ai_family == AF_INET6){
  120. struct sockaddr_in6 * sockaddr_v6 = (struct sockaddr_in6 *)cur_ai->ai_addr;
  121. if (!sockaddr_v6->sin6_port){
  122. sockaddr_v6->sin6_port = htons(port);
  123. }
  124. }
  125. #endif
  126. fd = ff_socket(cur_ai->ai_family,
  127. cur_ai->ai_socktype,
  128. cur_ai->ai_protocol);
  129. if (fd < 0) {
  130. ret = ff_neterrno();
  131. goto fail;
  132. }
  133. /* Set the socket's send or receive buffer sizes, if specified.
  134. If unspecified or setting fails, system default is used. */
  135. if (s->recv_buffer_size > 0) {
  136. setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &s->recv_buffer_size, sizeof (s->recv_buffer_size));
  137. }
  138. if (s->send_buffer_size > 0) {
  139. setsockopt (fd, SOL_SOCKET, SO_SNDBUF, &s->send_buffer_size, sizeof (s->send_buffer_size));
  140. }
  141. if (s->listen == 2) {
  142. // multi-client
  143. if ((ret = ff_listen(fd, cur_ai->ai_addr, cur_ai->ai_addrlen)) < 0)
  144. goto fail1;
  145. } else if (s->listen == 1) {
  146. // single client
  147. if ((ret = ff_listen_bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
  148. s->listen_timeout, h)) < 0)
  149. goto fail1;
  150. // Socket descriptor already closed here. Safe to overwrite to client one.
  151. fd = ret;
  152. } else {
  153. if ((ret = ff_listen_connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
  154. s->open_timeout / 1000, h, !!cur_ai->ai_next)) < 0) {
  155. if (ret == AVERROR_EXIT)
  156. goto fail1;
  157. else
  158. goto fail;
  159. }
  160. }
  161. h->is_streamed = 1;
  162. s->fd = fd;
  163. freeaddrinfo(ai);
  164. return 0;
  165. fail:
  166. if (cur_ai->ai_next) {
  167. /* Retry with the next sockaddr */
  168. cur_ai = cur_ai->ai_next;
  169. if (fd >= 0)
  170. closesocket(fd);
  171. ret = 0;
  172. goto restart;
  173. }
  174. fail1:
  175. if (fd >= 0)
  176. closesocket(fd);
  177. freeaddrinfo(ai);
  178. return ret;
  179. }
  180. static int tcp_accept(URLContext *s, URLContext **c)
  181. {
  182. TCPContext *sc = s->priv_data;
  183. TCPContext *cc;
  184. int ret;
  185. av_assert0(sc->listen);
  186. if ((ret = ffurl_alloc(c, s->filename, s->flags, &s->interrupt_callback)) < 0)
  187. return ret;
  188. cc = (*c)->priv_data;
  189. ret = ff_accept(sc->fd, sc->listen_timeout, s);
  190. if (ret < 0)
  191. return ff_neterrno();
  192. cc->fd = ret;
  193. return 0;
  194. }
  195. static int tcp_read(URLContext *h, uint8_t *buf, int size)
  196. {
  197. TCPContext *s = h->priv_data;
  198. int ret;
  199. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  200. ret = ff_network_wait_fd_timeout(s->fd, 0, h->rw_timeout, &h->interrupt_callback);
  201. if (ret)
  202. return ret;
  203. }
  204. ret = recv(s->fd, buf, size, 0);
  205. return ret < 0 ? ff_neterrno() : ret;
  206. }
  207. static int tcp_write(URLContext *h, const uint8_t *buf, int size)
  208. {
  209. TCPContext *s = h->priv_data;
  210. int ret;
  211. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  212. ret = ff_network_wait_fd_timeout(s->fd, 1, h->rw_timeout, &h->interrupt_callback);
  213. if (ret)
  214. return ret;
  215. }
  216. ret = send(s->fd, buf, size, MSG_NOSIGNAL);
  217. return ret < 0 ? ff_neterrno() : ret;
  218. }
  219. static int tcp_shutdown(URLContext *h, int flags)
  220. {
  221. TCPContext *s = h->priv_data;
  222. int how;
  223. if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
  224. how = SHUT_RDWR;
  225. } else if (flags & AVIO_FLAG_WRITE) {
  226. how = SHUT_WR;
  227. } else {
  228. how = SHUT_RD;
  229. }
  230. return shutdown(s->fd, how);
  231. }
  232. static int tcp_close(URLContext *h)
  233. {
  234. TCPContext *s = h->priv_data;
  235. closesocket(s->fd);
  236. return 0;
  237. }
  238. static int tcp_get_file_handle(URLContext *h)
  239. {
  240. TCPContext *s = h->priv_data;
  241. return s->fd;
  242. }
  243. static int tcp_get_window_size(URLContext *h)
  244. {
  245. TCPContext *s = h->priv_data;
  246. int avail;
  247. int avail_len = sizeof(avail);
  248. #if HAVE_WINSOCK2_H
  249. /* SO_RCVBUF with winsock only reports the actual TCP window size when
  250. auto-tuning has been disabled via setting SO_RCVBUF */
  251. if (s->recv_buffer_size < 0) {
  252. return AVERROR(ENOSYS);
  253. }
  254. #endif
  255. if (getsockopt(s->fd, SOL_SOCKET, SO_RCVBUF, &avail, &avail_len)) {
  256. return ff_neterrno();
  257. }
  258. return avail;
  259. }
  260. const URLProtocol ff_tcp_protocol = {
  261. .name = "tcp",
  262. .url_open = tcp_open,
  263. .url_accept = tcp_accept,
  264. .url_read = tcp_read,
  265. .url_write = tcp_write,
  266. .url_close = tcp_close,
  267. .url_get_file_handle = tcp_get_file_handle,
  268. .url_get_short_seek = tcp_get_window_size,
  269. .url_shutdown = tcp_shutdown,
  270. .priv_data_size = sizeof(TCPContext),
  271. .flags = URL_PROTOCOL_FLAG_NETWORK,
  272. .priv_data_class = &tcp_class,
  273. };