tcp.c 5.5 KB

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