tcp.c 6.9 KB

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