3 * Copyright (c) 2002 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg 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.
12 * FFmpeg 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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/parseutils.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/time.h"
27 #include "os_support.h"
33 typedef struct TCPContext {
41 #define OFFSET(x) offsetof(TCPContext, x)
42 #define D AV_OPT_FLAG_DECODING_PARAM
43 #define E AV_OPT_FLAG_ENCODING_PARAM
44 static const AVOption options[] = {
45 {"listen", "listen on port instead of connecting", OFFSET(listen), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, D|E },
46 {"timeout", "timeout of socket i/o operations", OFFSET(rw_timeout), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, D|E },
47 {"listen_timeout", "connection awaiting timeout", OFFSET(listen_timeout), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D|E },
51 static const AVClass tcp_context_class = {
53 .item_name = av_default_item_name,
55 .version = LIBAVUTIL_VERSION_INT,
58 /* return non zero if error */
59 static int tcp_open(URLContext *h, const char *uri, int flags)
61 struct addrinfo hints = { 0 }, *ai, *cur_ai;
63 TCPContext *s = h->priv_data;
68 char hostname[1024],proto[1024],path[1024];
70 h->rw_timeout = 5000000;
72 av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
73 &port, path, sizeof(path), uri);
74 if (strcmp(proto, "tcp"))
75 return AVERROR(EINVAL);
76 if (port <= 0 || port >= 65536) {
77 av_log(h, AV_LOG_ERROR, "Port missing in uri\n");
78 return AVERROR(EINVAL);
82 if (av_find_info_tag(buf, sizeof(buf), "listen", p))
84 if (av_find_info_tag(buf, sizeof(buf), "timeout", p)) {
85 s->rw_timeout = strtol(buf, NULL, 10);
87 if (av_find_info_tag(buf, sizeof(buf), "listen_timeout", p)) {
88 s->listen_timeout = strtol(buf, NULL, 10);
91 h->rw_timeout = s->rw_timeout;
92 hints.ai_family = AF_UNSPEC;
93 hints.ai_socktype = SOCK_STREAM;
94 snprintf(portstr, sizeof(portstr), "%d", port);
96 hints.ai_flags |= AI_PASSIVE;
98 ret = getaddrinfo(NULL, portstr, &hints, &ai);
100 ret = getaddrinfo(hostname, portstr, &hints, &ai);
102 av_log(h, AV_LOG_ERROR,
103 "Failed to resolve hostname %s: %s\n",
104 hostname, gai_strerror(ret));
112 fd = socket(cur_ai->ai_family, cur_ai->ai_socktype, cur_ai->ai_protocol);
119 struct pollfd lp = { fd, POLLIN, 0 };
120 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
121 ret = bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
131 ret = poll(&lp, 1, s->listen_timeout >= 0 ? s->listen_timeout : -1);
133 ret = AVERROR(ETIMEDOUT);
136 fd1 = accept(fd, NULL, NULL);
143 ff_socket_nonblock(fd, 1);
146 ff_socket_nonblock(fd, 1);
147 ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
151 struct pollfd p = {fd, POLLOUT, 0};
152 int64_t wait_started;
154 if (ret == AVERROR(EINTR)) {
155 if (ff_check_interrupt(&h->interrupt_callback)) {
161 if (ret != AVERROR(EINPROGRESS) &&
162 ret != AVERROR(EAGAIN))
165 /* wait until we are connected or until abort */
166 wait_started = av_gettime();
168 if (ff_check_interrupt(&h->interrupt_callback)) {
172 ret = poll(&p, 1, 100);
175 } while (!h->rw_timeout || (av_gettime() - wait_started < h->rw_timeout));
177 ret = AVERROR(ETIMEDOUT);
181 optlen = sizeof(ret);
182 if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen))
183 ret = AVUNERROR(ff_neterrno());
187 av_strerror(ret, errbuf, sizeof(errbuf));
188 av_log(h, AV_LOG_ERROR,
189 "TCP connection to %s:%d failed: %s\n",
190 hostname, port, errbuf);
200 if (cur_ai->ai_next) {
201 /* Retry with the next sockaddr */
202 cur_ai = cur_ai->ai_next;
214 static int tcp_read(URLContext *h, uint8_t *buf, int size)
216 TCPContext *s = h->priv_data;
219 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
220 ret = ff_network_wait_fd_timeout(s->fd, 0, h->rw_timeout, &h->interrupt_callback);
224 ret = recv(s->fd, buf, size, 0);
225 return ret < 0 ? ff_neterrno() : ret;
228 static int tcp_write(URLContext *h, const uint8_t *buf, int size)
230 TCPContext *s = h->priv_data;
233 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
234 ret = ff_network_wait_fd_timeout(s->fd, 1, h->rw_timeout, &h->interrupt_callback);
238 ret = send(s->fd, buf, size, 0);
239 return ret < 0 ? ff_neterrno() : ret;
242 static int tcp_shutdown(URLContext *h, int flags)
244 TCPContext *s = h->priv_data;
247 if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
249 } else if (flags & AVIO_FLAG_WRITE) {
255 return shutdown(s->fd, how);
258 static int tcp_close(URLContext *h)
260 TCPContext *s = h->priv_data;
265 static int tcp_get_file_handle(URLContext *h)
267 TCPContext *s = h->priv_data;
271 URLProtocol ff_tcp_protocol = {
273 .url_open = tcp_open,
274 .url_read = tcp_read,
275 .url_write = tcp_write,
276 .url_close = tcp_close,
277 .url_get_file_handle = tcp_get_file_handle,
278 .url_shutdown = tcp_shutdown,
279 .priv_data_size = sizeof(TCPContext),
280 .priv_data_class = &tcp_context_class,
281 .flags = URL_PROTOCOL_FLAG_NETWORK,