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.

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