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.

205 lines
5.1KB

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