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.

242 lines
6.4KB

  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 "internal.h"
  24. #include "network.h"
  25. #include "os_support.h"
  26. #include "url.h"
  27. #if HAVE_POLL_H
  28. #include <poll.h>
  29. #endif
  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 = { 0 }, *ai, *cur_ai;
  37. int port, fd = -1;
  38. TCPContext *s = h->priv_data;
  39. int listen_socket = 0;
  40. const char *p;
  41. char buf[256];
  42. int ret;
  43. socklen_t optlen;
  44. int timeout = 100;
  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. if (av_find_info_tag(buf, sizeof(buf), "timeout", p)) {
  56. timeout = strtol(buf, NULL, 10);
  57. }
  58. }
  59. hints.ai_family = AF_UNSPEC;
  60. hints.ai_socktype = SOCK_STREAM;
  61. snprintf(portstr, sizeof(portstr), "%d", port);
  62. if (listen_socket)
  63. hints.ai_flags |= AI_PASSIVE;
  64. ret = getaddrinfo(hostname, portstr, &hints, &ai);
  65. if (ret) {
  66. av_log(h, AV_LOG_ERROR,
  67. "Failed to resolve hostname %s: %s\n",
  68. hostname, gai_strerror(ret));
  69. return AVERROR(EIO);
  70. }
  71. cur_ai = ai;
  72. restart:
  73. ret = AVERROR(EIO);
  74. fd = socket(cur_ai->ai_family, cur_ai->ai_socktype, cur_ai->ai_protocol);
  75. if (fd < 0)
  76. goto fail;
  77. if (listen_socket) {
  78. int fd1;
  79. int reuse = 1;
  80. setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
  81. ret = bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
  82. if (ret) {
  83. ret = ff_neterrno();
  84. goto fail1;
  85. }
  86. ret = listen(fd, 1);
  87. if (ret) {
  88. ret = ff_neterrno();
  89. goto fail1;
  90. }
  91. fd1 = accept(fd, NULL, NULL);
  92. if (fd1 < 0) {
  93. ret = ff_neterrno();
  94. goto fail1;
  95. }
  96. closesocket(fd);
  97. fd = fd1;
  98. ff_socket_nonblock(fd, 1);
  99. } else {
  100. redo:
  101. ff_socket_nonblock(fd, 1);
  102. ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
  103. }
  104. if (ret < 0) {
  105. struct pollfd p = {fd, POLLOUT, 0};
  106. ret = ff_neterrno();
  107. if (ret == AVERROR(EINTR)) {
  108. if (ff_check_interrupt(&h->interrupt_callback)) {
  109. ret = AVERROR_EXIT;
  110. goto fail1;
  111. }
  112. goto redo;
  113. }
  114. if (ret != AVERROR(EINPROGRESS) &&
  115. ret != AVERROR(EAGAIN))
  116. goto fail;
  117. /* wait until we are connected or until abort */
  118. while(timeout--) {
  119. if (ff_check_interrupt(&h->interrupt_callback)) {
  120. ret = AVERROR_EXIT;
  121. goto fail1;
  122. }
  123. ret = poll(&p, 1, 100);
  124. if (ret > 0)
  125. break;
  126. }
  127. if (ret <= 0) {
  128. ret = AVERROR(ETIMEDOUT);
  129. goto fail;
  130. }
  131. /* test error */
  132. optlen = sizeof(ret);
  133. if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen))
  134. ret = AVUNERROR(ff_neterrno());
  135. if (ret != 0) {
  136. char errbuf[100];
  137. ret = AVERROR(ret);
  138. av_strerror(ret, errbuf, sizeof(errbuf));
  139. av_log(h, AV_LOG_ERROR,
  140. "TCP connection to %s:%d failed: %s\n",
  141. hostname, port, errbuf);
  142. goto fail;
  143. }
  144. }
  145. h->is_streamed = 1;
  146. s->fd = fd;
  147. freeaddrinfo(ai);
  148. return 0;
  149. fail:
  150. if (cur_ai->ai_next) {
  151. /* Retry with the next sockaddr */
  152. cur_ai = cur_ai->ai_next;
  153. if (fd >= 0)
  154. closesocket(fd);
  155. goto restart;
  156. }
  157. fail1:
  158. if (fd >= 0)
  159. closesocket(fd);
  160. freeaddrinfo(ai);
  161. return ret;
  162. }
  163. static int tcp_read(URLContext *h, uint8_t *buf, int size)
  164. {
  165. TCPContext *s = h->priv_data;
  166. int ret;
  167. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  168. ret = ff_network_wait_fd(s->fd, 0);
  169. if (ret < 0)
  170. return ret;
  171. }
  172. ret = recv(s->fd, buf, size, 0);
  173. return ret < 0 ? ff_neterrno() : ret;
  174. }
  175. static int tcp_write(URLContext *h, const uint8_t *buf, int size)
  176. {
  177. TCPContext *s = h->priv_data;
  178. int ret;
  179. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  180. ret = ff_network_wait_fd(s->fd, 1);
  181. if (ret < 0)
  182. return ret;
  183. }
  184. ret = send(s->fd, buf, size, 0);
  185. return ret < 0 ? ff_neterrno() : ret;
  186. }
  187. static int tcp_shutdown(URLContext *h, int flags)
  188. {
  189. TCPContext *s = h->priv_data;
  190. int how;
  191. if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
  192. how = SHUT_RDWR;
  193. } else if (flags & AVIO_FLAG_WRITE) {
  194. how = SHUT_WR;
  195. } else {
  196. how = SHUT_RD;
  197. }
  198. return shutdown(s->fd, how);
  199. }
  200. static int tcp_close(URLContext *h)
  201. {
  202. TCPContext *s = h->priv_data;
  203. closesocket(s->fd);
  204. return 0;
  205. }
  206. static int tcp_get_file_handle(URLContext *h)
  207. {
  208. TCPContext *s = h->priv_data;
  209. return s->fd;
  210. }
  211. URLProtocol ff_tcp_protocol = {
  212. .name = "tcp",
  213. .url_open = tcp_open,
  214. .url_read = tcp_read,
  215. .url_write = tcp_write,
  216. .url_close = tcp_close,
  217. .url_get_file_handle = tcp_get_file_handle,
  218. .url_shutdown = tcp_shutdown,
  219. .priv_data_size = sizeof(TCPContext),
  220. .flags = URL_PROTOCOL_FLAG_NETWORK,
  221. };