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/avassert.h"
23 #include "libavutil/parseutils.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/time.h"
29 #include "os_support.h"
35 typedef struct TCPContext {
46 #define OFFSET(x) offsetof(TCPContext, x)
47 #define D AV_OPT_FLAG_DECODING_PARAM
48 #define E AV_OPT_FLAG_ENCODING_PARAM
49 static const AVOption options[] = {
50 { "listen", "Listen for incoming connections", OFFSET(listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, .flags = D|E },
51 { "timeout", "set timeout (in microseconds) of socket I/O operations", OFFSET(rw_timeout), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
52 { "listen_timeout", "Connection awaiting timeout (in milliseconds)", OFFSET(listen_timeout), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
53 { "send_buffer_size", "Socket send buffer size (in bytes)", OFFSET(send_buffer_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
54 { "recv_buffer_size", "Socket receive buffer size (in bytes)", OFFSET(recv_buffer_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
58 static const AVClass tcp_class = {
60 .item_name = av_default_item_name,
62 .version = LIBAVUTIL_VERSION_INT,
65 /* return non zero if error */
66 static int tcp_open(URLContext *h, const char *uri, int flags)
68 struct addrinfo hints = { 0 }, *ai, *cur_ai;
70 TCPContext *s = h->priv_data;
74 char hostname[1024],proto[1024],path[1024];
76 s->open_timeout = 5000000;
78 av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
79 &port, path, sizeof(path), uri);
80 if (strcmp(proto, "tcp"))
81 return AVERROR(EINVAL);
82 if (port <= 0 || port >= 65536) {
83 av_log(h, AV_LOG_ERROR, "Port missing in uri\n");
84 return AVERROR(EINVAL);
88 if (av_find_info_tag(buf, sizeof(buf), "listen", p)) {
90 s->listen = strtol(buf, &endptr, 10);
91 /* assume if no digits were found it is a request to enable it */
95 if (av_find_info_tag(buf, sizeof(buf), "timeout", p)) {
96 s->rw_timeout = strtol(buf, NULL, 10);
98 if (av_find_info_tag(buf, sizeof(buf), "listen_timeout", p)) {
99 s->listen_timeout = strtol(buf, NULL, 10);
102 if (s->rw_timeout >= 0) {
104 h->rw_timeout = s->rw_timeout;
106 hints.ai_family = AF_UNSPEC;
107 hints.ai_socktype = SOCK_STREAM;
108 snprintf(portstr, sizeof(portstr), "%d", port);
110 hints.ai_flags |= AI_PASSIVE;
112 ret = getaddrinfo(NULL, portstr, &hints, &ai);
114 ret = getaddrinfo(hostname, portstr, &hints, &ai);
116 av_log(h, AV_LOG_ERROR,
117 "Failed to resolve hostname %s: %s\n",
118 hostname, gai_strerror(ret));
125 #if HAVE_STRUCT_SOCKADDR_IN6
126 // workaround for IOS9 getaddrinfo in IPv6 only network use hardcode IPv4 address can not resolve port number.
127 if (cur_ai->ai_family == AF_INET6){
128 struct sockaddr_in6 * sockaddr_v6 = (struct sockaddr_in6 *)cur_ai->ai_addr;
129 if (!sockaddr_v6->sin6_port){
130 sockaddr_v6->sin6_port = htons(port);
135 fd = ff_socket(cur_ai->ai_family,
137 cur_ai->ai_protocol);
143 /* Set the socket's send or receive buffer sizes, if specified.
144 If unspecified or setting fails, system default is used. */
145 if (s->recv_buffer_size > 0) {
146 setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &s->recv_buffer_size, sizeof (s->recv_buffer_size));
148 if (s->send_buffer_size > 0) {
149 setsockopt (fd, SOL_SOCKET, SO_SNDBUF, &s->send_buffer_size, sizeof (s->send_buffer_size));
152 if (s->listen == 2) {
154 if ((ret = ff_listen(fd, cur_ai->ai_addr, cur_ai->ai_addrlen)) < 0)
156 } else if (s->listen == 1) {
158 if ((ret = ff_listen_bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
159 s->listen_timeout, h)) < 0)
161 // Socket descriptor already closed here. Safe to overwrite to client one.
164 if ((ret = ff_listen_connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
165 s->open_timeout / 1000, h, !!cur_ai->ai_next)) < 0) {
167 if (ret == AVERROR_EXIT)
181 if (cur_ai->ai_next) {
182 /* Retry with the next sockaddr */
183 cur_ai = cur_ai->ai_next;
196 static int tcp_accept(URLContext *s, URLContext **c)
198 TCPContext *sc = s->priv_data;
201 av_assert0(sc->listen);
202 if ((ret = ffurl_alloc(c, s->filename, s->flags, &s->interrupt_callback)) < 0)
204 cc = (*c)->priv_data;
205 ret = ff_accept(sc->fd, sc->listen_timeout, s);
212 static int tcp_read(URLContext *h, uint8_t *buf, int size)
214 TCPContext *s = h->priv_data;
217 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
218 ret = ff_network_wait_fd_timeout(s->fd, 0, h->rw_timeout, &h->interrupt_callback);
222 ret = recv(s->fd, buf, size, 0);
223 return ret < 0 ? ff_neterrno() : ret;
226 static int tcp_write(URLContext *h, const uint8_t *buf, int size)
228 TCPContext *s = h->priv_data;
231 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
232 ret = ff_network_wait_fd_timeout(s->fd, 1, h->rw_timeout, &h->interrupt_callback);
236 ret = send(s->fd, buf, size, MSG_NOSIGNAL);
237 return ret < 0 ? ff_neterrno() : ret;
240 static int tcp_shutdown(URLContext *h, int flags)
242 TCPContext *s = h->priv_data;
245 if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
247 } else if (flags & AVIO_FLAG_WRITE) {
253 return shutdown(s->fd, how);
256 static int tcp_close(URLContext *h)
258 TCPContext *s = h->priv_data;
263 static int tcp_get_file_handle(URLContext *h)
265 TCPContext *s = h->priv_data;
269 static int tcp_get_window_size(URLContext *h)
271 TCPContext *s = h->priv_data;
273 int avail_len = sizeof(avail);
276 /* SO_RCVBUF with winsock only reports the actual TCP window size when
277 auto-tuning has been disabled via setting SO_RCVBUF */
278 if (s->recv_buffer_size < 0) {
279 return AVERROR(ENOSYS);
283 if (getsockopt(s->fd, SOL_SOCKET, SO_RCVBUF, &avail, &avail_len)) {
284 return ff_neterrno();
289 const URLProtocol ff_tcp_protocol = {
291 .url_open = tcp_open,
292 .url_accept = tcp_accept,
293 .url_read = tcp_read,
294 .url_write = tcp_write,
295 .url_close = tcp_close,
296 .url_get_file_handle = tcp_get_file_handle,
297 .url_get_short_seek = tcp_get_window_size,
298 .url_shutdown = tcp_shutdown,
299 .priv_data_size = sizeof(TCPContext),
300 .flags = URL_PROTOCOL_FLAG_NETWORK,
301 .priv_data_class = &tcp_class,