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.

227 lines
6.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 "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. restart:
  104. fd = ff_socket(cur_ai->ai_family,
  105. cur_ai->ai_socktype,
  106. cur_ai->ai_protocol);
  107. if (fd < 0) {
  108. ret = ff_neterrno();
  109. goto fail;
  110. }
  111. if (s->listen) {
  112. if ((ret = ff_listen_bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
  113. s->listen_timeout, h)) < 0) {
  114. goto fail1;
  115. }
  116. fd = ret;
  117. } else {
  118. if ((ret = ff_listen_connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
  119. s->timeout, h, !!cur_ai->ai_next)) < 0) {
  120. if (ret == AVERROR_EXIT)
  121. goto fail1;
  122. else
  123. goto fail;
  124. }
  125. }
  126. h->is_streamed = 1;
  127. s->fd = fd;
  128. freeaddrinfo(ai);
  129. return 0;
  130. fail:
  131. if (cur_ai->ai_next) {
  132. /* Retry with the next sockaddr */
  133. cur_ai = cur_ai->ai_next;
  134. if (fd >= 0)
  135. closesocket(fd);
  136. ret = 0;
  137. goto restart;
  138. }
  139. fail1:
  140. if (fd >= 0)
  141. closesocket(fd);
  142. freeaddrinfo(ai);
  143. return ret;
  144. }
  145. static int tcp_read(URLContext *h, uint8_t *buf, int size)
  146. {
  147. TCPContext *s = h->priv_data;
  148. int ret;
  149. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  150. ret = ff_network_wait_fd(s->fd, 0);
  151. if (ret < 0)
  152. return ret;
  153. }
  154. ret = recv(s->fd, buf, size, 0);
  155. return ret < 0 ? ff_neterrno() : ret;
  156. }
  157. static int tcp_write(URLContext *h, const uint8_t *buf, int size)
  158. {
  159. TCPContext *s = h->priv_data;
  160. int ret;
  161. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  162. ret = ff_network_wait_fd(s->fd, 1);
  163. if (ret < 0)
  164. return ret;
  165. }
  166. ret = send(s->fd, buf, size, MSG_NOSIGNAL);
  167. return ret < 0 ? ff_neterrno() : ret;
  168. }
  169. static int tcp_shutdown(URLContext *h, int flags)
  170. {
  171. TCPContext *s = h->priv_data;
  172. int how;
  173. if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
  174. how = SHUT_RDWR;
  175. } else if (flags & AVIO_FLAG_WRITE) {
  176. how = SHUT_WR;
  177. } else {
  178. how = SHUT_RD;
  179. }
  180. return shutdown(s->fd, how);
  181. }
  182. static int tcp_close(URLContext *h)
  183. {
  184. TCPContext *s = h->priv_data;
  185. closesocket(s->fd);
  186. return 0;
  187. }
  188. static int tcp_get_file_handle(URLContext *h)
  189. {
  190. TCPContext *s = h->priv_data;
  191. return s->fd;
  192. }
  193. const URLProtocol ff_tcp_protocol = {
  194. .name = "tcp",
  195. .url_open = tcp_open,
  196. .url_read = tcp_read,
  197. .url_write = tcp_write,
  198. .url_close = tcp_close,
  199. .url_get_file_handle = tcp_get_file_handle,
  200. .url_shutdown = tcp_shutdown,
  201. .priv_data_size = sizeof(TCPContext),
  202. .flags = URL_PROTOCOL_FLAG_NETWORK,
  203. .priv_data_class = &tcp_class,
  204. };