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.

235 lines
6.1KB

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