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 #endif /* !HAVE_WINSOCK2_H */
50 #define OFFSET(x) offsetof(TCPContext, x)
51 #define D AV_OPT_FLAG_DECODING_PARAM
52 #define E AV_OPT_FLAG_ENCODING_PARAM
53 static const AVOption options[] = {
54 { "listen", "Listen for incoming connections", OFFSET(listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, .flags = D|E },
55 { "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 },
56 { "listen_timeout", "Connection awaiting timeout (in milliseconds)", OFFSET(listen_timeout), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
57 { "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 },
58 { "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 },
59 { "tcp_nodelay", "Use TCP_NODELAY to disable nagle's algorithm", OFFSET(tcp_nodelay), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags = D|E },
61 { "tcp_mss", "Maximum segment size for outgoing TCP packets", OFFSET(tcp_mss), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
62 #endif /* !HAVE_WINSOCK2_H */
66 static const AVClass tcp_class = {
68 .item_name = av_default_item_name,
70 .version = LIBAVUTIL_VERSION_INT,
73 static void customize_fd(void *ctx, int fd)
76 /* Set the socket's send or receive buffer sizes, if specified.
77 If unspecified or setting fails, system default is used. */
78 if (s->recv_buffer_size > 0) {
79 if (setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &s->recv_buffer_size, sizeof (s->recv_buffer_size))) {
80 ff_log_net_error(ctx, AV_LOG_WARNING, "setsockopt(SO_RCVBUF)");
83 if (s->send_buffer_size > 0) {
84 if (setsockopt (fd, SOL_SOCKET, SO_SNDBUF, &s->send_buffer_size, sizeof (s->send_buffer_size))) {
85 ff_log_net_error(ctx, AV_LOG_WARNING, "setsockopt(SO_SNDBUF)");
88 if (s->tcp_nodelay > 0) {
89 if (setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &s->tcp_nodelay, sizeof (s->tcp_nodelay))) {
90 ff_log_net_error(ctx, AV_LOG_WARNING, "setsockopt(TCP_NODELAY)");
95 if (setsockopt (fd, IPPROTO_TCP, TCP_MAXSEG, &s->tcp_mss, sizeof (s->tcp_mss))) {
96 ff_log_net_error(ctx, AV_LOG_WARNING, "setsockopt(TCP_MAXSEG)");
99 #endif /* !HAVE_WINSOCK2_H */
102 /* return non zero if error */
103 static int tcp_open(URLContext *h, const char *uri, int flags)
105 struct addrinfo hints = { 0 }, *ai, *cur_ai;
107 TCPContext *s = h->priv_data;
111 char hostname[1024],proto[1024],path[1024];
113 s->open_timeout = 5000000;
115 av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
116 &port, path, sizeof(path), uri);
117 if (strcmp(proto, "tcp"))
118 return AVERROR(EINVAL);
119 if (port <= 0 || port >= 65536) {
120 av_log(h, AV_LOG_ERROR, "Port missing in uri\n");
121 return AVERROR(EINVAL);
123 p = strchr(uri, '?');
125 if (av_find_info_tag(buf, sizeof(buf), "listen", p)) {
127 s->listen = strtol(buf, &endptr, 10);
128 /* assume if no digits were found it is a request to enable it */
132 if (av_find_info_tag(buf, sizeof(buf), "timeout", p)) {
133 s->rw_timeout = strtol(buf, NULL, 10);
135 if (av_find_info_tag(buf, sizeof(buf), "listen_timeout", p)) {
136 s->listen_timeout = strtol(buf, NULL, 10);
139 if (s->rw_timeout >= 0) {
141 h->rw_timeout = s->rw_timeout;
143 hints.ai_family = AF_UNSPEC;
144 hints.ai_socktype = SOCK_STREAM;
145 snprintf(portstr, sizeof(portstr), "%d", port);
147 hints.ai_flags |= AI_PASSIVE;
149 ret = getaddrinfo(NULL, portstr, &hints, &ai);
151 ret = getaddrinfo(hostname, portstr, &hints, &ai);
153 av_log(h, AV_LOG_ERROR,
154 "Failed to resolve hostname %s: %s\n",
155 hostname, gai_strerror(ret));
161 #if HAVE_STRUCT_SOCKADDR_IN6
162 // workaround for IOS9 getaddrinfo in IPv6 only network use hardcode IPv4 address can not resolve port number.
163 if (cur_ai->ai_family == AF_INET6){
164 struct sockaddr_in6 * sockaddr_v6 = (struct sockaddr_in6 *)cur_ai->ai_addr;
165 if (!sockaddr_v6->sin6_port){
166 sockaddr_v6->sin6_port = htons(port);
172 while (cur_ai && fd < 0) {
173 fd = ff_socket(cur_ai->ai_family,
175 cur_ai->ai_protocol);
178 cur_ai = cur_ai->ai_next;
186 if (s->listen == 2) {
188 if ((ret = ff_listen(fd, cur_ai->ai_addr, cur_ai->ai_addrlen)) < 0)
190 } else if (s->listen == 1) {
192 if ((ret = ff_listen_bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
193 s->listen_timeout, h)) < 0)
195 // Socket descriptor already closed here. Safe to overwrite to client one.
198 ret = ff_connect_parallel(ai, s->open_timeout / 1000, 3, h, &fd, customize_fd, s);
216 static int tcp_accept(URLContext *s, URLContext **c)
218 TCPContext *sc = s->priv_data;
221 av_assert0(sc->listen);
222 if ((ret = ffurl_alloc(c, s->filename, s->flags, &s->interrupt_callback)) < 0)
224 cc = (*c)->priv_data;
225 ret = ff_accept(sc->fd, sc->listen_timeout, s);
234 static int tcp_read(URLContext *h, uint8_t *buf, int size)
236 TCPContext *s = h->priv_data;
239 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
240 ret = ff_network_wait_fd_timeout(s->fd, 0, h->rw_timeout, &h->interrupt_callback);
244 ret = recv(s->fd, buf, size, 0);
247 return ret < 0 ? ff_neterrno() : ret;
250 static int tcp_write(URLContext *h, const uint8_t *buf, int size)
252 TCPContext *s = h->priv_data;
255 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
256 ret = ff_network_wait_fd_timeout(s->fd, 1, h->rw_timeout, &h->interrupt_callback);
260 ret = send(s->fd, buf, size, MSG_NOSIGNAL);
261 return ret < 0 ? ff_neterrno() : ret;
264 static int tcp_shutdown(URLContext *h, int flags)
266 TCPContext *s = h->priv_data;
269 if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
271 } else if (flags & AVIO_FLAG_WRITE) {
277 return shutdown(s->fd, how);
280 static int tcp_close(URLContext *h)
282 TCPContext *s = h->priv_data;
287 static int tcp_get_file_handle(URLContext *h)
289 TCPContext *s = h->priv_data;
293 static int tcp_get_window_size(URLContext *h)
295 TCPContext *s = h->priv_data;
297 socklen_t avail_len = sizeof(avail);
300 /* SO_RCVBUF with winsock only reports the actual TCP window size when
301 auto-tuning has been disabled via setting SO_RCVBUF */
302 if (s->recv_buffer_size < 0) {
303 return AVERROR(ENOSYS);
307 if (getsockopt(s->fd, SOL_SOCKET, SO_RCVBUF, &avail, &avail_len)) {
308 return ff_neterrno();
313 const URLProtocol ff_tcp_protocol = {
315 .url_open = tcp_open,
316 .url_accept = tcp_accept,
317 .url_read = tcp_read,
318 .url_write = tcp_write,
319 .url_close = tcp_close,
320 .url_get_file_handle = tcp_get_file_handle,
321 .url_get_short_seek = tcp_get_window_size,
322 .url_shutdown = tcp_shutdown,
323 .priv_data_size = sizeof(TCPContext),
324 .flags = URL_PROTOCOL_FLAG_NETWORK,
325 .priv_data_class = &tcp_class,