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.

310 lines
9.4KB

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