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.

199 lines
5.2KB

  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 = socket(cur_ai->ai_family, cur_ai->ai_socktype, cur_ai->ai_protocol);
  83. if (fd < 0) {
  84. ret = ff_neterrno();
  85. goto fail;
  86. }
  87. if (listen_socket) {
  88. if ((fd = ff_listen_bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
  89. listen_timeout, h)) < 0) {
  90. ret = fd;
  91. goto fail1;
  92. }
  93. } else {
  94. if ((ret = ff_listen_connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
  95. timeout * 100, h)) < 0) {
  96. if (ret == AVERROR_EXIT)
  97. goto fail1;
  98. else
  99. goto fail;
  100. }
  101. }
  102. h->is_streamed = 1;
  103. s->fd = fd;
  104. freeaddrinfo(ai);
  105. return 0;
  106. fail:
  107. if (cur_ai->ai_next) {
  108. /* Retry with the next sockaddr */
  109. cur_ai = cur_ai->ai_next;
  110. if (fd >= 0)
  111. closesocket(fd);
  112. ret = 0;
  113. goto restart;
  114. }
  115. fail1:
  116. if (fd >= 0)
  117. closesocket(fd);
  118. freeaddrinfo(ai);
  119. return ret;
  120. }
  121. static int tcp_read(URLContext *h, uint8_t *buf, int size)
  122. {
  123. TCPContext *s = h->priv_data;
  124. int ret;
  125. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  126. ret = ff_network_wait_fd(s->fd, 0);
  127. if (ret < 0)
  128. return ret;
  129. }
  130. ret = recv(s->fd, buf, size, 0);
  131. return ret < 0 ? ff_neterrno() : ret;
  132. }
  133. static int tcp_write(URLContext *h, const uint8_t *buf, int size)
  134. {
  135. TCPContext *s = h->priv_data;
  136. int ret;
  137. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  138. ret = ff_network_wait_fd(s->fd, 1);
  139. if (ret < 0)
  140. return ret;
  141. }
  142. ret = send(s->fd, buf, size, 0);
  143. return ret < 0 ? ff_neterrno() : ret;
  144. }
  145. static int tcp_shutdown(URLContext *h, int flags)
  146. {
  147. TCPContext *s = h->priv_data;
  148. int how;
  149. if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
  150. how = SHUT_RDWR;
  151. } else if (flags & AVIO_FLAG_WRITE) {
  152. how = SHUT_WR;
  153. } else {
  154. how = SHUT_RD;
  155. }
  156. return shutdown(s->fd, how);
  157. }
  158. static int tcp_close(URLContext *h)
  159. {
  160. TCPContext *s = h->priv_data;
  161. closesocket(s->fd);
  162. return 0;
  163. }
  164. static int tcp_get_file_handle(URLContext *h)
  165. {
  166. TCPContext *s = h->priv_data;
  167. return s->fd;
  168. }
  169. URLProtocol ff_tcp_protocol = {
  170. .name = "tcp",
  171. .url_open = tcp_open,
  172. .url_read = tcp_read,
  173. .url_write = tcp_write,
  174. .url_close = tcp_close,
  175. .url_get_file_handle = tcp_get_file_handle,
  176. .url_shutdown = tcp_shutdown,
  177. .priv_data_size = sizeof(TCPContext),
  178. .flags = URL_PROTOCOL_FLAG_NETWORK,
  179. };