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.

210 lines
5.5KB

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