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.

218 lines
5.6KB

  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() == FF_NETERROR(EINTR)) {
  69. if (url_interrupt_cb())
  70. goto fail1;
  71. goto redo;
  72. }
  73. if (ff_neterrno() != FF_NETERROR(EINPROGRESS) &&
  74. ff_neterrno() != FF_NETERROR(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);
  128. }
  129. static int tcp_read(URLContext *h, uint8_t *buf, int size)
  130. {
  131. TCPContext *s = h->priv_data;
  132. int len, ret;
  133. for (;;) {
  134. if (url_interrupt_cb())
  135. return AVERROR(EINTR);
  136. ret = tcp_wait_fd(s->fd, 0);
  137. if (ret > 0) {
  138. len = recv(s->fd, buf, size, 0);
  139. if (len < 0) {
  140. if (ff_neterrno() != FF_NETERROR(EINTR) &&
  141. ff_neterrno() != FF_NETERROR(EAGAIN))
  142. return ff_neterrno();
  143. } else return len;
  144. } else if (ret < 0) {
  145. if (ret == FF_NETERROR(EINTR))
  146. continue;
  147. return ret;
  148. }
  149. }
  150. }
  151. static int tcp_write(URLContext *h, const uint8_t *buf, int size)
  152. {
  153. TCPContext *s = h->priv_data;
  154. int ret, size1, len;
  155. size1 = size;
  156. while (size > 0) {
  157. if (url_interrupt_cb())
  158. return AVERROR(EINTR);
  159. ret = tcp_wait_fd(s->fd, 1);
  160. if (ret > 0) {
  161. len = send(s->fd, buf, size, 0);
  162. if (len < 0) {
  163. if (ff_neterrno() != FF_NETERROR(EINTR) &&
  164. ff_neterrno() != FF_NETERROR(EAGAIN))
  165. return ff_neterrno();
  166. continue;
  167. }
  168. size -= len;
  169. buf += len;
  170. } else if (ret < 0) {
  171. if (ret == FF_NETERROR(EINTR))
  172. continue;
  173. return ret;
  174. }
  175. }
  176. return size1 - size;
  177. }
  178. static int tcp_close(URLContext *h)
  179. {
  180. TCPContext *s = h->priv_data;
  181. closesocket(s->fd);
  182. av_free(s);
  183. return 0;
  184. }
  185. static int tcp_get_file_handle(URLContext *h)
  186. {
  187. TCPContext *s = h->priv_data;
  188. return s->fd;
  189. }
  190. URLProtocol ff_tcp_protocol = {
  191. "tcp",
  192. tcp_open,
  193. tcp_read,
  194. tcp_write,
  195. NULL, /* seek */
  196. tcp_close,
  197. .url_get_file_handle = tcp_get_file_handle,
  198. };