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.

327 lines
10KB

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