tcp.c 4.9 KB

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