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.

201 lines
5.3KB

  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. int timeout = 100, listen_timeout = -1;
  44. char hostname[1024],proto[1024],path[1024];
  45. char portstr[10];
  46. av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
  47. &port, path, sizeof(path), uri);
  48. if (strcmp(proto, "tcp"))
  49. return AVERROR(EINVAL);
  50. if (port <= 0 || port >= 65536) {
  51. av_log(h, AV_LOG_ERROR, "Port missing in uri\n");
  52. return AVERROR(EINVAL);
  53. }
  54. p = strchr(uri, '?');
  55. if (p) {
  56. if (av_find_info_tag(buf, sizeof(buf), "listen", p))
  57. listen_socket = 1;
  58. if (av_find_info_tag(buf, sizeof(buf), "timeout", p)) {
  59. timeout = strtol(buf, NULL, 10);
  60. }
  61. if (av_find_info_tag(buf, sizeof(buf), "listen_timeout", p)) {
  62. listen_timeout = strtol(buf, NULL, 10);
  63. }
  64. }
  65. hints.ai_family = AF_UNSPEC;
  66. hints.ai_socktype = SOCK_STREAM;
  67. snprintf(portstr, sizeof(portstr), "%d", port);
  68. if (listen_socket)
  69. hints.ai_flags |= AI_PASSIVE;
  70. if (!hostname[0])
  71. ret = getaddrinfo(NULL, portstr, &hints, &ai);
  72. else
  73. ret = getaddrinfo(hostname, portstr, &hints, &ai);
  74. if (ret) {
  75. av_log(h, AV_LOG_ERROR,
  76. "Failed to resolve hostname %s: %s\n",
  77. hostname, gai_strerror(ret));
  78. return AVERROR(EIO);
  79. }
  80. cur_ai = ai;
  81. restart:
  82. fd = ff_socket(cur_ai->ai_family,
  83. cur_ai->ai_socktype,
  84. cur_ai->ai_protocol);
  85. if (fd < 0) {
  86. ret = ff_neterrno();
  87. goto fail;
  88. }
  89. if (listen_socket) {
  90. if ((fd = ff_listen_bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
  91. listen_timeout, h)) < 0) {
  92. ret = fd;
  93. goto fail1;
  94. }
  95. } else {
  96. if ((ret = ff_listen_connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
  97. timeout * 100, h, !!cur_ai->ai_next)) < 0) {
  98. if (ret == AVERROR_EXIT)
  99. goto fail1;
  100. else
  101. goto fail;
  102. }
  103. }
  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. ret = 0;
  115. goto restart;
  116. }
  117. fail1:
  118. if (fd >= 0)
  119. closesocket(fd);
  120. freeaddrinfo(ai);
  121. return ret;
  122. }
  123. static int tcp_read(URLContext *h, uint8_t *buf, int size)
  124. {
  125. TCPContext *s = h->priv_data;
  126. int ret;
  127. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  128. ret = ff_network_wait_fd(s->fd, 0);
  129. if (ret < 0)
  130. return ret;
  131. }
  132. ret = recv(s->fd, buf, size, 0);
  133. return ret < 0 ? ff_neterrno() : ret;
  134. }
  135. static int tcp_write(URLContext *h, const uint8_t *buf, int size)
  136. {
  137. TCPContext *s = h->priv_data;
  138. int ret;
  139. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  140. ret = ff_network_wait_fd(s->fd, 1);
  141. if (ret < 0)
  142. return ret;
  143. }
  144. ret = send(s->fd, buf, size, 0);
  145. return ret < 0 ? ff_neterrno() : ret;
  146. }
  147. static int tcp_shutdown(URLContext *h, int flags)
  148. {
  149. TCPContext *s = h->priv_data;
  150. int how;
  151. if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
  152. how = SHUT_RDWR;
  153. } else if (flags & AVIO_FLAG_WRITE) {
  154. how = SHUT_WR;
  155. } else {
  156. how = SHUT_RD;
  157. }
  158. return shutdown(s->fd, how);
  159. }
  160. static int tcp_close(URLContext *h)
  161. {
  162. TCPContext *s = h->priv_data;
  163. closesocket(s->fd);
  164. return 0;
  165. }
  166. static int tcp_get_file_handle(URLContext *h)
  167. {
  168. TCPContext *s = h->priv_data;
  169. return s->fd;
  170. }
  171. URLProtocol ff_tcp_protocol = {
  172. .name = "tcp",
  173. .url_open = tcp_open,
  174. .url_read = tcp_read,
  175. .url_write = tcp_write,
  176. .url_close = tcp_close,
  177. .url_get_file_handle = tcp_get_file_handle,
  178. .url_shutdown = tcp_shutdown,
  179. .priv_data_size = sizeof(TCPContext),
  180. .flags = URL_PROTOCOL_FLAG_NETWORK,
  181. };