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.

213 lines
5.4KB

  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, *ai, *cur_ai;
  39. int port, fd = -1;
  40. TCPContext *s = NULL;
  41. int listen_socket = 0;
  42. const char *p;
  43. char buf[256];
  44. int ret;
  45. socklen_t optlen;
  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. }
  57. memset(&hints, 0, sizeof(hints));
  58. hints.ai_family = AF_UNSPEC;
  59. hints.ai_socktype = SOCK_STREAM;
  60. snprintf(portstr, sizeof(portstr), "%d", port);
  61. ret = getaddrinfo(hostname, portstr, &hints, &ai);
  62. if (ret) {
  63. av_log(NULL, AV_LOG_ERROR,
  64. "Failed to resolve hostname %s: %s\n",
  65. hostname, gai_strerror(ret));
  66. return AVERROR(EIO);
  67. }
  68. cur_ai = ai;
  69. restart:
  70. fd = socket(cur_ai->ai_family, cur_ai->ai_socktype, cur_ai->ai_protocol);
  71. if (fd < 0)
  72. goto fail;
  73. if (listen_socket) {
  74. int fd1;
  75. ret = bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
  76. listen(fd, 1);
  77. fd1 = accept(fd, NULL, NULL);
  78. closesocket(fd);
  79. fd = fd1;
  80. } else {
  81. redo:
  82. ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
  83. }
  84. ff_socket_nonblock(fd, 1);
  85. if (ret < 0) {
  86. int timeout=50;
  87. struct pollfd p = {fd, POLLOUT, 0};
  88. if (ff_neterrno() == AVERROR(EINTR)) {
  89. if (url_interrupt_cb()) {
  90. ret = AVERROR_EXIT;
  91. goto fail1;
  92. }
  93. goto redo;
  94. }
  95. if (ff_neterrno() != AVERROR(EINPROGRESS) &&
  96. ff_neterrno() != AVERROR(EAGAIN))
  97. goto fail;
  98. /* wait until we are connected or until abort */
  99. for(;;) {
  100. if (url_interrupt_cb()) {
  101. ret = AVERROR_EXIT;
  102. goto fail1;
  103. }
  104. ret = poll(&p, 1, 100);
  105. if (ret > 0)
  106. break;
  107. if(!--timeout){
  108. av_log(NULL, AV_LOG_ERROR,
  109. "TCP open %s:%d timeout\n",
  110. hostname, port);
  111. goto fail;
  112. }
  113. }
  114. /* test error */
  115. optlen = sizeof(ret);
  116. getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
  117. if (ret != 0) {
  118. av_log(NULL, AV_LOG_ERROR,
  119. "TCP connection to %s:%d failed: %s\n",
  120. hostname, port, strerror(ret));
  121. goto fail;
  122. }
  123. }
  124. s = av_malloc(sizeof(TCPContext));
  125. if (!s) {
  126. freeaddrinfo(ai);
  127. return AVERROR(ENOMEM);
  128. }
  129. h->priv_data = s;
  130. h->is_streamed = 1;
  131. s->fd = fd;
  132. freeaddrinfo(ai);
  133. return 0;
  134. fail:
  135. if (cur_ai->ai_next) {
  136. /* Retry with the next sockaddr */
  137. cur_ai = cur_ai->ai_next;
  138. if (fd >= 0)
  139. closesocket(fd);
  140. goto restart;
  141. }
  142. ret = AVERROR(EIO);
  143. fail1:
  144. if (fd >= 0)
  145. closesocket(fd);
  146. freeaddrinfo(ai);
  147. return ret;
  148. }
  149. static int tcp_read(URLContext *h, uint8_t *buf, int size)
  150. {
  151. TCPContext *s = h->priv_data;
  152. int ret;
  153. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  154. ret = ff_network_wait_fd(s->fd, 0);
  155. if (ret < 0)
  156. return ret;
  157. }
  158. ret = recv(s->fd, buf, size, 0);
  159. return ret < 0 ? ff_neterrno() : ret;
  160. }
  161. static int tcp_write(URLContext *h, const uint8_t *buf, int size)
  162. {
  163. TCPContext *s = h->priv_data;
  164. int ret;
  165. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  166. ret = ff_network_wait_fd(s->fd, 1);
  167. if (ret < 0)
  168. return ret;
  169. }
  170. ret = send(s->fd, buf, size, 0);
  171. return ret < 0 ? ff_neterrno() : ret;
  172. }
  173. static int tcp_close(URLContext *h)
  174. {
  175. TCPContext *s = h->priv_data;
  176. closesocket(s->fd);
  177. av_free(s);
  178. return 0;
  179. }
  180. static int tcp_get_file_handle(URLContext *h)
  181. {
  182. TCPContext *s = h->priv_data;
  183. return s->fd;
  184. }
  185. URLProtocol ff_tcp_protocol = {
  186. "tcp",
  187. tcp_open,
  188. tcp_read,
  189. tcp_write,
  190. NULL, /* seek */
  191. tcp_close,
  192. .url_get_file_handle = tcp_get_file_handle,
  193. };