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/mem.h"
24 #include "libavutil/parseutils.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/time.h"
30 #include "os_support.h"
36 typedef struct TCPContext
{
51 #endif /* !HAVE_WINSOCK2_H */
54 #define OFFSET(x) offsetof(TCPContext, x)
55 #define D AV_OPT_FLAG_DECODING_PARAM
56 #define E AV_OPT_FLAG_ENCODING_PARAM
57 static const AVOption options
[] = {
58 { "listen", "Listen for incoming connections", OFFSET(listen
), AV_OPT_TYPE_INT
, { .i64
= 0 }, 0, 2, .flags
= D
|E
},
59 { "local_port", "Local port", OFFSET(local_port
), AV_OPT_TYPE_STRING
, { .str
= NULL
}, 0, 0, .flags
= D
|E
},
60 { "local_addr", "Local address", OFFSET(local_addr
), AV_OPT_TYPE_STRING
, { .str
= NULL
}, 0, 0, .flags
= D
|E
},
61 { "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
},
62 { "listen_timeout", "Connection awaiting timeout (in milliseconds)", OFFSET(listen_timeout
), AV_OPT_TYPE_INT
, { .i64
= -1 }, -1, INT_MAX
, .flags
= D
|E
},
63 { "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
},
64 { "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
},
65 { "tcp_nodelay", "Use TCP_NODELAY to disable nagle's algorithm", OFFSET(tcp_nodelay
), AV_OPT_TYPE_BOOL
, { .i64
= 0 }, 0, 1, .flags
= D
|E
},
66 { "tcp_keepalive", "Use TCP keepalive to detect dead connections and keep long-lived connections active.", OFFSET(tcp_keepalive
), AV_OPT_TYPE_BOOL
, { .i64
= 0 }, 0, 1, .flags
= D
|E
},
68 { "tcp_mss", "Maximum segment size for outgoing TCP packets", OFFSET(tcp_mss
), AV_OPT_TYPE_INT
, { .i64
= -1 }, -1, INT_MAX
, .flags
= D
|E
},
69 #endif /* !HAVE_WINSOCK2_H */
73 static const AVClass tcp_class
= {
75 .item_name
= av_default_item_name
,
77 .version
= LIBAVUTIL_VERSION_INT
,
80 static int customize_fd(void *ctx
, int fd
, int family
)
84 if (s
->local_addr
|| s
->local_port
) {
85 struct addrinfo hints
= { 0 }, *ai
, *cur_ai
;
88 hints
.ai_family
= family
;
89 hints
.ai_socktype
= SOCK_STREAM
;
91 ret
= getaddrinfo(s
->local_addr
, s
->local_port
, &hints
, &ai
);
93 av_log(ctx
, AV_LOG_ERROR
,
94 "Failed to getaddrinfo local addr: %s port: %s err: %s\n",
95 s
->local_addr
, s
->local_port
, gai_strerror(ret
));
101 ret
= bind(fd
, (struct sockaddr
*)cur_ai
->ai_addr
, (int)cur_ai
->ai_addrlen
);
103 cur_ai
= cur_ai
->ai_next
;
110 ff_log_net_error(ctx
, AV_LOG_ERROR
, "bind local failed");
114 /* Set the socket's send or receive buffer sizes, if specified.
115 If unspecified or setting fails, system default is used. */
116 if (s
->recv_buffer_size
> 0) {
117 if (setsockopt (fd
, SOL_SOCKET
, SO_RCVBUF
, &s
->recv_buffer_size
, sizeof (s
->recv_buffer_size
))) {
118 ff_log_net_error(ctx
, AV_LOG_WARNING
, "setsockopt(SO_RCVBUF)");
121 if (s
->send_buffer_size
> 0) {
122 if (setsockopt (fd
, SOL_SOCKET
, SO_SNDBUF
, &s
->send_buffer_size
, sizeof (s
->send_buffer_size
))) {
123 ff_log_net_error(ctx
, AV_LOG_WARNING
, "setsockopt(SO_SNDBUF)");
126 if (s
->tcp_nodelay
> 0) {
127 if (setsockopt (fd
, IPPROTO_TCP
, TCP_NODELAY
, &s
->tcp_nodelay
, sizeof (s
->tcp_nodelay
))) {
128 ff_log_net_error(ctx
, AV_LOG_WARNING
, "setsockopt(TCP_NODELAY)");
131 if (s
->tcp_keepalive
> 0) {
132 if (setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
, &s
->tcp_keepalive
, sizeof(s
->tcp_keepalive
))) {
133 ff_log_net_error(ctx
, AV_LOG_WARNING
, "setsockopt(SO_KEEPALIVE)");
138 if (s
->tcp_mss
> 0) {
139 if (setsockopt (fd
, IPPROTO_TCP
, TCP_MAXSEG
, &s
->tcp_mss
, sizeof (s
->tcp_mss
))) {
140 ff_log_net_error(ctx
, AV_LOG_WARNING
, "setsockopt(TCP_MAXSEG)");
143 #endif /* !HAVE_WINSOCK2_H */
148 /* return non zero if error */
149 static int tcp_open(URLContext
*h
, const char *uri
, int flags
)
151 struct addrinfo hints
= { 0 }, *ai
, *cur_ai
;
153 TCPContext
*s
= h
->priv_data
;
156 char hostname
[1024],proto
[1024],path
[1024];
158 s
->open_timeout
= 5000000;
160 av_url_split(proto
, sizeof(proto
), NULL
, 0, hostname
, sizeof(hostname
),
161 &port
, path
, sizeof(path
), uri
);
162 if (strcmp(proto
, "tcp"))
163 return AVERROR(EINVAL
);
164 if (port
<= 0 || port
>= 65536) {
165 av_log(h
, AV_LOG_ERROR
, "Port missing in uri\n");
166 return AVERROR(EINVAL
);
168 p
= strchr(uri
, '?');
170 int ret
= ff_parse_opts_from_query_string(s
, p
, 1);
174 if (s
->rw_timeout
>= 0) {
176 h
->rw_timeout
= s
->rw_timeout
;
178 hints
.ai_family
= AF_UNSPEC
;
179 hints
.ai_socktype
= SOCK_STREAM
;
180 snprintf(portstr
, sizeof(portstr
), "%d", port
);
182 hints
.ai_flags
|= AI_PASSIVE
;
184 ret
= getaddrinfo(NULL
, portstr
, &hints
, &ai
);
186 ret
= getaddrinfo(hostname
, portstr
, &hints
, &ai
);
188 av_log(h
, AV_LOG_ERROR
,
189 "Failed to resolve hostname %s: %s\n",
190 hostname
, gai_strerror(ret
));
196 #if HAVE_STRUCT_SOCKADDR_IN6
197 // workaround for IOS9 getaddrinfo in IPv6 only network use hardcode IPv4 address can not resolve port number.
198 if (cur_ai
->ai_family
== AF_INET6
){
199 struct sockaddr_in6
* sockaddr_v6
= (struct sockaddr_in6
*)cur_ai
->ai_addr
;
200 if (!sockaddr_v6
->sin6_port
){
201 sockaddr_v6
->sin6_port
= htons(port
);
207 while (cur_ai
&& fd
< 0) {
208 fd
= ff_socket(cur_ai
->ai_family
,
210 cur_ai
->ai_protocol
, h
);
213 cur_ai
= cur_ai
->ai_next
;
218 customize_fd(s
, fd
, cur_ai
->ai_family
);
221 if (s
->listen
== 2) {
223 if ((ret
= ff_listen(fd
, cur_ai
->ai_addr
, cur_ai
->ai_addrlen
, h
)) < 0)
225 } else if (s
->listen
== 1) {
227 if ((ret
= ff_listen_bind(fd
, cur_ai
->ai_addr
, cur_ai
->ai_addrlen
,
228 s
->listen_timeout
, h
)) < 0)
230 // Socket descriptor already closed here. Safe to overwrite to client one.
233 ret
= ff_connect_parallel(ai
, s
->open_timeout
/ 1000, 3, h
, &fd
, customize_fd
, s
);
251 static int tcp_accept(URLContext
*s
, URLContext
**c
)
253 TCPContext
*sc
= s
->priv_data
;
256 av_assert0(sc
->listen
);
257 if ((ret
= ffurl_alloc(c
, s
->filename
, s
->flags
, &s
->interrupt_callback
)) < 0)
259 cc
= (*c
)->priv_data
;
260 ret
= ff_accept(sc
->fd
, sc
->listen_timeout
, s
);
269 static int tcp_read(URLContext
*h
, uint8_t *buf
, int size
)
271 TCPContext
*s
= h
->priv_data
;
274 if (!(h
->flags
& AVIO_FLAG_NONBLOCK
)) {
275 ret
= ff_network_wait_fd_timeout(s
->fd
, 0, h
->rw_timeout
, &h
->interrupt_callback
);
279 ret
= recv(s
->fd
, buf
, size
, 0);
282 return ret
< 0 ? ff_neterrno() : ret
;
285 static int tcp_write(URLContext
*h
, const uint8_t *buf
, int size
)
287 TCPContext
*s
= h
->priv_data
;
290 if (!(h
->flags
& AVIO_FLAG_NONBLOCK
)) {
291 ret
= ff_network_wait_fd_timeout(s
->fd
, 1, h
->rw_timeout
, &h
->interrupt_callback
);
295 ret
= send(s
->fd
, buf
, size
, MSG_NOSIGNAL
);
296 return ret
< 0 ? ff_neterrno() : ret
;
299 static int tcp_shutdown(URLContext
*h
, int flags
)
301 TCPContext
*s
= h
->priv_data
;
304 if (flags
& AVIO_FLAG_WRITE
&& flags
& AVIO_FLAG_READ
) {
306 } else if (flags
& AVIO_FLAG_WRITE
) {
312 return shutdown(s
->fd
, how
);
315 static int tcp_close(URLContext
*h
)
317 TCPContext
*s
= h
->priv_data
;
322 static int tcp_get_file_handle(URLContext
*h
)
324 TCPContext
*s
= h
->priv_data
;
328 static int tcp_get_window_size(URLContext
*h
)
330 TCPContext
*s
= h
->priv_data
;
332 socklen_t avail_len
= sizeof(avail
);
335 /* SO_RCVBUF with winsock only reports the actual TCP window size when
336 auto-tuning has been disabled via setting SO_RCVBUF */
337 if (s
->recv_buffer_size
< 0) {
338 return AVERROR(ENOSYS
);
342 if (getsockopt(s
->fd
, SOL_SOCKET
, SO_RCVBUF
, &avail
, &avail_len
)) {
343 return ff_neterrno();
348 const URLProtocol ff_tcp_protocol
= {
350 .url_open
= tcp_open
,
351 .url_accept
= tcp_accept
,
352 .url_read
= tcp_read
,
353 .url_write
= tcp_write
,
354 .url_close
= tcp_close
,
355 .url_get_file_handle
= tcp_get_file_handle
,
356 .url_get_short_seek
= tcp_get_window_size
,
357 .url_shutdown
= tcp_shutdown
,
358 .priv_data_size
= sizeof(TCPContext
),
359 .flags
= URL_PROTOCOL_FLAG_NETWORK
,
360 .priv_data_class
= &tcp_class
,