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.

223 lines
6.1KB

  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 "libavutil/opt.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. typedef struct TCPContext {
  32. const AVClass *class;
  33. int fd;
  34. int listen;
  35. int timeout;
  36. int listen_timeout;
  37. } TCPContext;
  38. #define OFFSET(x) offsetof(TCPContext, x)
  39. #define D AV_OPT_FLAG_DECODING_PARAM
  40. #define E AV_OPT_FLAG_ENCODING_PARAM
  41. static const AVOption options[] = {
  42. { "listen", "Listen for incoming connections", OFFSET(listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, .flags = D|E },
  43. { "timeout", "Connection timeout (in milliseconds)", OFFSET(timeout), AV_OPT_TYPE_INT, { .i64 = 10000 }, INT_MIN, INT_MAX, .flags = D|E },
  44. { "listen_timeout", "Bind timeout (in milliseconds)", OFFSET(listen_timeout), AV_OPT_TYPE_INT, { .i64 = -1 }, INT_MIN, INT_MAX, .flags = D|E },
  45. { NULL }
  46. };
  47. static const AVClass tcp_class = {
  48. .class_name = "tcp",
  49. .item_name = av_default_item_name,
  50. .option = options,
  51. .version = LIBAVUTIL_VERSION_INT,
  52. };
  53. /* return non zero if error */
  54. static int tcp_open(URLContext *h, const char *uri, int flags)
  55. {
  56. struct addrinfo hints = { 0 }, *ai, *cur_ai;
  57. int port, fd = -1;
  58. TCPContext *s = h->priv_data;
  59. const char *p;
  60. char buf[256];
  61. int ret;
  62. char hostname[1024],proto[1024],path[1024];
  63. char portstr[10];
  64. av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
  65. &port, path, sizeof(path), uri);
  66. if (strcmp(proto, "tcp"))
  67. return AVERROR(EINVAL);
  68. if (port <= 0 || port >= 65536) {
  69. av_log(h, AV_LOG_ERROR, "Port missing in uri\n");
  70. return AVERROR(EINVAL);
  71. }
  72. p = strchr(uri, '?');
  73. if (p) {
  74. if (av_find_info_tag(buf, sizeof(buf), "listen", p))
  75. s->listen = 1;
  76. if (av_find_info_tag(buf, sizeof(buf), "timeout", p)) {
  77. s->timeout = strtol(buf, NULL, 10) * 100;
  78. }
  79. if (av_find_info_tag(buf, sizeof(buf), "listen_timeout", p)) {
  80. s->listen_timeout = strtol(buf, NULL, 10);
  81. }
  82. }
  83. hints.ai_family = AF_UNSPEC;
  84. hints.ai_socktype = SOCK_STREAM;
  85. snprintf(portstr, sizeof(portstr), "%d", port);
  86. if (s->listen)
  87. hints.ai_flags |= AI_PASSIVE;
  88. if (!hostname[0])
  89. ret = getaddrinfo(NULL, portstr, &hints, &ai);
  90. else
  91. ret = getaddrinfo(hostname, portstr, &hints, &ai);
  92. if (ret) {
  93. av_log(h, AV_LOG_ERROR,
  94. "Failed to resolve hostname %s: %s\n",
  95. hostname, gai_strerror(ret));
  96. return AVERROR(EIO);
  97. }
  98. cur_ai = ai;
  99. restart:
  100. fd = ff_socket(cur_ai->ai_family,
  101. cur_ai->ai_socktype,
  102. cur_ai->ai_protocol);
  103. if (fd < 0) {
  104. ret = ff_neterrno();
  105. goto fail;
  106. }
  107. if (s->listen) {
  108. if ((fd = ff_listen_bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
  109. s->listen_timeout, h)) < 0) {
  110. ret = fd;
  111. goto fail1;
  112. }
  113. } else {
  114. if ((ret = ff_listen_connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
  115. s->timeout, h, !!cur_ai->ai_next)) < 0) {
  116. if (ret == AVERROR_EXIT)
  117. goto fail1;
  118. else
  119. goto fail;
  120. }
  121. }
  122. h->is_streamed = 1;
  123. s->fd = fd;
  124. freeaddrinfo(ai);
  125. return 0;
  126. fail:
  127. if (cur_ai->ai_next) {
  128. /* Retry with the next sockaddr */
  129. cur_ai = cur_ai->ai_next;
  130. if (fd >= 0)
  131. closesocket(fd);
  132. ret = 0;
  133. goto restart;
  134. }
  135. fail1:
  136. if (fd >= 0)
  137. closesocket(fd);
  138. freeaddrinfo(ai);
  139. return ret;
  140. }
  141. static int tcp_read(URLContext *h, uint8_t *buf, int size)
  142. {
  143. TCPContext *s = h->priv_data;
  144. int ret;
  145. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  146. ret = ff_network_wait_fd(s->fd, 0);
  147. if (ret < 0)
  148. return ret;
  149. }
  150. ret = recv(s->fd, buf, size, 0);
  151. return ret < 0 ? ff_neterrno() : ret;
  152. }
  153. static int tcp_write(URLContext *h, const 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, 1);
  159. if (ret < 0)
  160. return ret;
  161. }
  162. ret = send(s->fd, buf, size, MSG_NOSIGNAL);
  163. return ret < 0 ? ff_neterrno() : ret;
  164. }
  165. static int tcp_shutdown(URLContext *h, int flags)
  166. {
  167. TCPContext *s = h->priv_data;
  168. int how;
  169. if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
  170. how = SHUT_RDWR;
  171. } else if (flags & AVIO_FLAG_WRITE) {
  172. how = SHUT_WR;
  173. } else {
  174. how = SHUT_RD;
  175. }
  176. return shutdown(s->fd, how);
  177. }
  178. static int tcp_close(URLContext *h)
  179. {
  180. TCPContext *s = h->priv_data;
  181. closesocket(s->fd);
  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. .url_shutdown = tcp_shutdown,
  197. .priv_data_size = sizeof(TCPContext),
  198. .flags = URL_PROTOCOL_FLAG_NETWORK,
  199. .priv_data_class = &tcp_class,
  200. };