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.

193 lines
4.9KB

  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. struct pollfd p = {fd, POLLOUT, 0};
  68. if (ff_neterrno() == AVERROR(EINTR)) {
  69. if (url_interrupt_cb())
  70. goto fail1;
  71. goto redo;
  72. }
  73. if (ff_neterrno() != AVERROR(EINPROGRESS) &&
  74. ff_neterrno() != AVERROR(EAGAIN))
  75. goto fail;
  76. /* wait until we are connected or until abort */
  77. for(;;) {
  78. if (url_interrupt_cb()) {
  79. ret = AVERROR(EINTR);
  80. goto fail1;
  81. }
  82. ret = poll(&p, 1, 100);
  83. if (ret > 0)
  84. break;
  85. }
  86. /* test error */
  87. optlen = sizeof(ret);
  88. getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
  89. if (ret != 0) {
  90. av_log(NULL, AV_LOG_ERROR,
  91. "TCP connection to %s:%d failed: %s\n",
  92. hostname, port, strerror(ret));
  93. goto fail;
  94. }
  95. }
  96. s = av_malloc(sizeof(TCPContext));
  97. if (!s) {
  98. freeaddrinfo(ai);
  99. return AVERROR(ENOMEM);
  100. }
  101. h->priv_data = s;
  102. h->is_streamed = 1;
  103. s->fd = fd;
  104. freeaddrinfo(ai);
  105. return 0;
  106. fail:
  107. if (cur_ai->ai_next) {
  108. /* Retry with the next sockaddr */
  109. cur_ai = cur_ai->ai_next;
  110. if (fd >= 0)
  111. closesocket(fd);
  112. goto restart;
  113. }
  114. ret = AVERROR(EIO);
  115. fail1:
  116. if (fd >= 0)
  117. closesocket(fd);
  118. freeaddrinfo(ai);
  119. return ret;
  120. }
  121. static int tcp_wait_fd(int fd, int write)
  122. {
  123. int ev = write ? POLLOUT : POLLIN;
  124. struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
  125. int ret;
  126. ret = poll(&p, 1, 100);
  127. return ret < 0 ? ff_neterrno() : p.revents & ev ? 0 : AVERROR(EAGAIN);
  128. }
  129. static int tcp_read(URLContext *h, uint8_t *buf, int size)
  130. {
  131. TCPContext *s = h->priv_data;
  132. int ret;
  133. if (!(h->flags & URL_FLAG_NONBLOCK)) {
  134. ret = tcp_wait_fd(s->fd, 0);
  135. if (ret < 0)
  136. return ret;
  137. }
  138. ret = recv(s->fd, buf, size, 0);
  139. return ret < 0 ? ff_neterrno() : ret;
  140. }
  141. static int tcp_write(URLContext *h, const uint8_t *buf, int size)
  142. {
  143. TCPContext *s = h->priv_data;
  144. int ret;
  145. if (!(h->flags & URL_FLAG_NONBLOCK)) {
  146. ret = tcp_wait_fd(s->fd, 1);
  147. if (ret < 0)
  148. return ret;
  149. }
  150. ret = send(s->fd, buf, size, 0);
  151. return ret < 0 ? ff_neterrno() : ret;
  152. }
  153. static int tcp_close(URLContext *h)
  154. {
  155. TCPContext *s = h->priv_data;
  156. closesocket(s->fd);
  157. av_free(s);
  158. return 0;
  159. }
  160. static int tcp_get_file_handle(URLContext *h)
  161. {
  162. TCPContext *s = h->priv_data;
  163. return s->fd;
  164. }
  165. URLProtocol ff_tcp_protocol = {
  166. "tcp",
  167. tcp_open,
  168. tcp_read,
  169. tcp_write,
  170. NULL, /* seek */
  171. tcp_close,
  172. .url_get_file_handle = tcp_get_file_handle,
  173. };