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.

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