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.

241 lines
6.3KB

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