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 {
47 #define OFFSET(x) offsetof(TCPContext, x)
48 #define D AV_OPT_FLAG_DECODING_PARAM
49 #define E AV_OPT_FLAG_ENCODING_PARAM
50 static const AVOption options[] = {
51 { "listen", "Listen for incoming connections", OFFSET(listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, .flags = D|E },
52 { "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 },
53 { "listen_timeout", "Connection awaiting timeout (in milliseconds)", OFFSET(listen_timeout), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
54 { "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 },
55 { "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 },
56 { "tcp_nodelay", "Use TCP_NODELAY to disable nagle's algorithm", OFFSET(tcp_nodelay), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags = D|E },
60 static const AVClass tcp_class = {
62 .item_name = av_default_item_name,
64 .version = LIBAVUTIL_VERSION_INT,
67 /* return non zero if error */
68 static int tcp_open(URLContext *h, const char *uri, int flags)
70 struct addrinfo hints = { 0 }, *ai, *cur_ai;
72 TCPContext *s = h->priv_data;
76 char hostname[1024],proto[1024],path[1024];
78 s->open_timeout = 5000000;
80 av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
81 &port, path, sizeof(path), uri);
82 if (strcmp(proto, "tcp"))
83 return AVERROR(EINVAL);
84 if (port <= 0 || port >= 65536) {
85 av_log(h, AV_LOG_ERROR, "Port missing in uri\n");
86 return AVERROR(EINVAL);
90 if (av_find_info_tag(buf, sizeof(buf), "listen", p)) {
92 s->listen = strtol(buf, &endptr, 10);
93 /* assume if no digits were found it is a request to enable it */
97 if (av_find_info_tag(buf, sizeof(buf), "timeout", p)) {
98 s->rw_timeout = strtol(buf, NULL, 10);
100 if (av_find_info_tag(buf, sizeof(buf), "listen_timeout", p)) {
101 s->listen_timeout = strtol(buf, NULL, 10);
104 if (s->rw_timeout >= 0) {
106 h->rw_timeout = s->rw_timeout;
108 hints.ai_family = AF_UNSPEC;
109 hints.ai_socktype = SOCK_STREAM;
110 snprintf(portstr, sizeof(portstr), "%d", port);
112 hints.ai_flags |= AI_PASSIVE;
114 ret = getaddrinfo(NULL, portstr, &hints, &ai);
116 ret = getaddrinfo(hostname, portstr, &hints, &ai);
118 av_log(h, AV_LOG_ERROR,
119 "Failed to resolve hostname %s: %s\n",
120 hostname, gai_strerror(ret));
127 #if HAVE_STRUCT_SOCKADDR_IN6
128 // workaround for IOS9 getaddrinfo in IPv6 only network use hardcode IPv4 address can not resolve port number.
129 if (cur_ai->ai_family == AF_INET6){
130 struct sockaddr_in6 * sockaddr_v6 = (struct sockaddr_in6 *)cur_ai->ai_addr;
131 if (!sockaddr_v6->sin6_port){
132 sockaddr_v6->sin6_port = htons(port);
137 fd = ff_socket(cur_ai->ai_family,
139 cur_ai->ai_protocol);
145 /* Set the socket's send or receive buffer sizes, if specified.
146 If unspecified or setting fails, system default is used. */
147 if (s->recv_buffer_size > 0) {
148 setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &s->recv_buffer_size, sizeof (s->recv_buffer_size));
150 if (s->send_buffer_size > 0) {
151 setsockopt (fd, SOL_SOCKET, SO_SNDBUF, &s->send_buffer_size, sizeof (s->send_buffer_size));
153 if (s->tcp_nodelay > 0) {
154 setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &s->tcp_nodelay, sizeof (s->tcp_nodelay));
157 if (s->listen == 2) {
159 if ((ret = ff_listen(fd, cur_ai->ai_addr, cur_ai->ai_addrlen)) < 0)
161 } else if (s->listen == 1) {
163 if ((ret = ff_listen_bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
164 s->listen_timeout, h)) < 0)
166 // Socket descriptor already closed here. Safe to overwrite to client one.
169 if ((ret = ff_listen_connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
170 s->open_timeout / 1000, h, !!cur_ai->ai_next)) < 0) {
172 if (ret == AVERROR_EXIT)
186 if (cur_ai->ai_next) {
187 /* Retry with the next sockaddr */
188 cur_ai = cur_ai->ai_next;
201 static int tcp_accept(URLContext *s, URLContext **c)
203 TCPContext *sc = s->priv_data;
206 av_assert0(sc->listen);
207 if ((ret = ffurl_alloc(c, s->filename, s->flags, &s->interrupt_callback)) < 0)
209 cc = (*c)->priv_data;
210 ret = ff_accept(sc->fd, sc->listen_timeout, s);
217 static int tcp_read(URLContext *h, uint8_t *buf, int size)
219 TCPContext *s = h->priv_data;
222 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
223 ret = ff_network_wait_fd_timeout(s->fd, 0, h->rw_timeout, &h->interrupt_callback);
227 ret = recv(s->fd, buf, size, 0);
228 return ret < 0 ? ff_neterrno() : ret;
231 static int tcp_write(URLContext *h, const uint8_t *buf, int size)
233 TCPContext *s = h->priv_data;
236 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
237 ret = ff_network_wait_fd_timeout(s->fd, 1, h->rw_timeout, &h->interrupt_callback);
241 ret = send(s->fd, buf, size, MSG_NOSIGNAL);
242 return ret < 0 ? ff_neterrno() : ret;
245 static int tcp_shutdown(URLContext *h, int flags)
247 TCPContext *s = h->priv_data;
250 if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
252 } else if (flags & AVIO_FLAG_WRITE) {
258 return shutdown(s->fd, how);
261 static int tcp_close(URLContext *h)
263 TCPContext *s = h->priv_data;
268 static int tcp_get_file_handle(URLContext *h)
270 TCPContext *s = h->priv_data;
274 static int tcp_get_window_size(URLContext *h)
276 TCPContext *s = h->priv_data;
278 socklen_t avail_len = sizeof(avail);
281 /* SO_RCVBUF with winsock only reports the actual TCP window size when
282 auto-tuning has been disabled via setting SO_RCVBUF */
283 if (s->recv_buffer_size < 0) {
284 return AVERROR(ENOSYS);
288 if (getsockopt(s->fd, SOL_SOCKET, SO_RCVBUF, &avail, &avail_len)) {
289 return ff_neterrno();
294 const URLProtocol ff_tcp_protocol = {
296 .url_open = tcp_open,
297 .url_accept = tcp_accept,
298 .url_read = tcp_read,
299 .url_write = tcp_write,
300 .url_close = tcp_close,
301 .url_get_file_handle = tcp_get_file_handle,
302 .url_get_short_seek = tcp_get_window_size,
303 .url_shutdown = tcp_shutdown,
304 .priv_data_size = sizeof(TCPContext),
305 .flags = URL_PROTOCOL_FLAG_NETWORK,
306 .priv_data_class = &tcp_class,