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.

216 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 = 0 }, 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. if (!s->timeout)
  84. s->timeout = h->rw_timeout ? h->rw_timeout / 1000 : 10000;
  85. if (h->rw_timeout && s->listen_timeout < 0)
  86. s->listen_timeout = h->rw_timeout / 1000;
  87. hints.ai_family = AF_UNSPEC;
  88. hints.ai_socktype = SOCK_STREAM;
  89. snprintf(portstr, sizeof(portstr), "%d", port);
  90. if (s->listen)
  91. hints.ai_flags |= AI_PASSIVE;
  92. if (!hostname[0])
  93. ret = getaddrinfo(NULL, portstr, &hints, &ai);
  94. else
  95. ret = getaddrinfo(hostname, portstr, &hints, &ai);
  96. if (ret) {
  97. av_log(h, AV_LOG_ERROR,
  98. "Failed to resolve hostname %s: %s\n",
  99. hostname, gai_strerror(ret));
  100. return AVERROR(EIO);
  101. }
  102. cur_ai = ai;
  103. if (s->listen) {
  104. while (cur_ai && fd < 0) {
  105. fd = ff_socket(cur_ai->ai_family,
  106. cur_ai->ai_socktype,
  107. cur_ai->ai_protocol);
  108. if (fd < 0) {
  109. ret = ff_neterrno();
  110. cur_ai = cur_ai->ai_next;
  111. }
  112. }
  113. if (fd < 0)
  114. goto fail1;
  115. if ((ret = ff_listen_bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
  116. s->listen_timeout, h)) < 0) {
  117. goto fail1;
  118. }
  119. fd = ret;
  120. } else {
  121. ret = ff_connect_parallel(ai, s->timeout, 3, h, &fd, NULL, NULL);
  122. if (ret < 0)
  123. goto fail1;
  124. }
  125. h->is_streamed = 1;
  126. s->fd = fd;
  127. freeaddrinfo(ai);
  128. return 0;
  129. fail1:
  130. if (fd >= 0)
  131. closesocket(fd);
  132. freeaddrinfo(ai);
  133. return ret;
  134. }
  135. static int tcp_read(URLContext *h, 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, 0);
  141. if (ret < 0)
  142. return ret;
  143. }
  144. ret = recv(s->fd, buf, size, 0);
  145. return ret < 0 ? ff_neterrno() : ret;
  146. }
  147. static int tcp_write(URLContext *h, const uint8_t *buf, int size)
  148. {
  149. TCPContext *s = h->priv_data;
  150. int ret;
  151. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  152. ret = ff_network_wait_fd(s->fd, 1);
  153. if (ret < 0)
  154. return ret;
  155. }
  156. ret = send(s->fd, buf, size, MSG_NOSIGNAL);
  157. return ret < 0 ? ff_neterrno() : ret;
  158. }
  159. static int tcp_shutdown(URLContext *h, int flags)
  160. {
  161. TCPContext *s = h->priv_data;
  162. int how;
  163. if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
  164. how = SHUT_RDWR;
  165. } else if (flags & AVIO_FLAG_WRITE) {
  166. how = SHUT_WR;
  167. } else {
  168. how = SHUT_RD;
  169. }
  170. return shutdown(s->fd, how);
  171. }
  172. static int tcp_close(URLContext *h)
  173. {
  174. TCPContext *s = h->priv_data;
  175. closesocket(s->fd);
  176. return 0;
  177. }
  178. static int tcp_get_file_handle(URLContext *h)
  179. {
  180. TCPContext *s = h->priv_data;
  181. return s->fd;
  182. }
  183. const URLProtocol ff_tcp_protocol = {
  184. .name = "tcp",
  185. .url_open = tcp_open,
  186. .url_read = tcp_read,
  187. .url_write = tcp_write,
  188. .url_close = tcp_close,
  189. .url_get_file_handle = tcp_get_file_handle,
  190. .url_shutdown = tcp_shutdown,
  191. .priv_data_size = sizeof(TCPContext),
  192. .flags = URL_PROTOCOL_FLAG_NETWORK,
  193. .priv_data_class = &tcp_class,
  194. };