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.

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