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.

254 lines
6.8KB

  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/parseutils.h"
  23. #include "internal.h"
  24. #include "network.h"
  25. #include "os_support.h"
  26. #include "url.h"
  27. #if HAVE_POLL_H
  28. #include <poll.h>
  29. #endif
  30. typedef struct TCPContext {
  31. int fd;
  32. } TCPContext;
  33. /* return non zero if error */
  34. static int tcp_open(URLContext *h, const char *uri, int flags)
  35. {
  36. struct addrinfo hints = { 0 }, *ai, *cur_ai;
  37. int port, fd = -1;
  38. TCPContext *s = h->priv_data;
  39. int listen_socket = 0;
  40. const char *p;
  41. char buf[256];
  42. int ret;
  43. socklen_t optlen;
  44. int timeout = 50, listen_timeout = -1;
  45. char hostname[1024],proto[1024],path[1024];
  46. char portstr[10];
  47. av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
  48. &port, path, sizeof(path), uri);
  49. if (strcmp(proto,"tcp") || port <= 0 || port >= 65536)
  50. return AVERROR(EINVAL);
  51. p = strchr(uri, '?');
  52. if (p) {
  53. if (av_find_info_tag(buf, sizeof(buf), "listen", p))
  54. listen_socket = 1;
  55. if (av_find_info_tag(buf, sizeof(buf), "timeout", p)) {
  56. timeout = strtol(buf, NULL, 10);
  57. }
  58. if (av_find_info_tag(buf, sizeof(buf), "listen_timeout", p)) {
  59. listen_timeout = strtol(buf, NULL, 10);
  60. }
  61. }
  62. hints.ai_family = AF_UNSPEC;
  63. hints.ai_socktype = SOCK_STREAM;
  64. snprintf(portstr, sizeof(portstr), "%d", port);
  65. if (listen_socket)
  66. hints.ai_flags |= AI_PASSIVE;
  67. if (!hostname[0])
  68. ret = getaddrinfo(NULL, portstr, &hints, &ai);
  69. else
  70. ret = getaddrinfo(hostname, portstr, &hints, &ai);
  71. if (ret) {
  72. av_log(h, AV_LOG_ERROR,
  73. "Failed to resolve hostname %s: %s\n",
  74. hostname, gai_strerror(ret));
  75. return AVERROR(EIO);
  76. }
  77. cur_ai = ai;
  78. restart:
  79. ret = AVERROR(EIO);
  80. fd = socket(cur_ai->ai_family, cur_ai->ai_socktype, cur_ai->ai_protocol);
  81. if (fd < 0)
  82. goto fail;
  83. if (listen_socket) {
  84. int fd1;
  85. int reuse = 1;
  86. struct pollfd lp = { fd, POLLIN, 0 };
  87. setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
  88. ret = bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
  89. if (ret) {
  90. ret = ff_neterrno();
  91. goto fail1;
  92. }
  93. ret = listen(fd, 1);
  94. if (ret) {
  95. ret = ff_neterrno();
  96. goto fail1;
  97. }
  98. ret = poll(&lp, 1, listen_timeout >= 0 ? listen_timeout : -1);
  99. if (ret <= 0) {
  100. ret = AVERROR(ETIMEDOUT);
  101. goto fail1;
  102. }
  103. fd1 = accept(fd, NULL, NULL);
  104. if (fd1 < 0) {
  105. ret = ff_neterrno();
  106. goto fail1;
  107. }
  108. closesocket(fd);
  109. fd = fd1;
  110. ff_socket_nonblock(fd, 1);
  111. } else {
  112. redo:
  113. ff_socket_nonblock(fd, 1);
  114. ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
  115. }
  116. if (ret < 0) {
  117. struct pollfd p = {fd, POLLOUT, 0};
  118. ret = ff_neterrno();
  119. if (ret == AVERROR(EINTR)) {
  120. if (ff_check_interrupt(&h->interrupt_callback)) {
  121. ret = AVERROR_EXIT;
  122. goto fail1;
  123. }
  124. goto redo;
  125. }
  126. if (ret != AVERROR(EINPROGRESS) &&
  127. ret != AVERROR(EAGAIN))
  128. goto fail;
  129. /* wait until we are connected or until abort */
  130. while(timeout--) {
  131. if (ff_check_interrupt(&h->interrupt_callback)) {
  132. ret = AVERROR_EXIT;
  133. goto fail1;
  134. }
  135. ret = poll(&p, 1, 100);
  136. if (ret > 0)
  137. break;
  138. }
  139. if (ret <= 0) {
  140. ret = AVERROR(ETIMEDOUT);
  141. goto fail;
  142. }
  143. /* test error */
  144. optlen = sizeof(ret);
  145. if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen))
  146. ret = AVUNERROR(ff_neterrno());
  147. if (ret != 0) {
  148. char errbuf[100];
  149. ret = AVERROR(ret);
  150. av_strerror(ret, errbuf, sizeof(errbuf));
  151. av_log(h, AV_LOG_ERROR,
  152. "TCP connection to %s:%d failed: %s\n",
  153. hostname, port, errbuf);
  154. goto fail;
  155. }
  156. }
  157. h->is_streamed = 1;
  158. s->fd = fd;
  159. freeaddrinfo(ai);
  160. return 0;
  161. fail:
  162. if (cur_ai->ai_next) {
  163. /* Retry with the next sockaddr */
  164. cur_ai = cur_ai->ai_next;
  165. if (fd >= 0)
  166. closesocket(fd);
  167. goto restart;
  168. }
  169. fail1:
  170. if (fd >= 0)
  171. closesocket(fd);
  172. freeaddrinfo(ai);
  173. return ret;
  174. }
  175. static int tcp_read(URLContext *h, uint8_t *buf, int size)
  176. {
  177. TCPContext *s = h->priv_data;
  178. int ret;
  179. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  180. ret = ff_network_wait_fd(s->fd, 0);
  181. if (ret < 0)
  182. return ret;
  183. }
  184. ret = recv(s->fd, buf, size, 0);
  185. return ret < 0 ? ff_neterrno() : ret;
  186. }
  187. static int tcp_write(URLContext *h, const uint8_t *buf, int size)
  188. {
  189. TCPContext *s = h->priv_data;
  190. int ret;
  191. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  192. ret = ff_network_wait_fd(s->fd, 1);
  193. if (ret < 0)
  194. return ret;
  195. }
  196. ret = send(s->fd, buf, size, 0);
  197. return ret < 0 ? ff_neterrno() : ret;
  198. }
  199. static int tcp_shutdown(URLContext *h, int flags)
  200. {
  201. TCPContext *s = h->priv_data;
  202. int how;
  203. if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
  204. how = SHUT_RDWR;
  205. } else if (flags & AVIO_FLAG_WRITE) {
  206. how = SHUT_WR;
  207. } else {
  208. how = SHUT_RD;
  209. }
  210. return shutdown(s->fd, how);
  211. }
  212. static int tcp_close(URLContext *h)
  213. {
  214. TCPContext *s = h->priv_data;
  215. closesocket(s->fd);
  216. return 0;
  217. }
  218. static int tcp_get_file_handle(URLContext *h)
  219. {
  220. TCPContext *s = h->priv_data;
  221. return s->fd;
  222. }
  223. URLProtocol ff_tcp_protocol = {
  224. .name = "tcp",
  225. .url_open = tcp_open,
  226. .url_read = tcp_read,
  227. .url_write = tcp_write,
  228. .url_close = tcp_close,
  229. .url_get_file_handle = tcp_get_file_handle,
  230. .url_shutdown = tcp_shutdown,
  231. .priv_data_size = sizeof(TCPContext),
  232. .flags = URL_PROTOCOL_FLAG_NETWORK,
  233. };