1 /*
2 * TCP protocol
3 * Copyright (c) 2002 Fabrice Bellard
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
26
31 #if HAVE_POLL_H
32 #include <poll.h>
33 #endif
34
45 #if !HAVE_WINSOCK2_H
47 #endif /* !HAVE_WINSOCK2_H */
49
50 #define OFFSET(x) offsetof(TCPContext, x)
51 #define D AV_OPT_FLAG_DECODING_PARAM
52 #define E AV_OPT_FLAG_ENCODING_PARAM
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 },
60 #if !HAVE_WINSOCK2_H
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 */
64 };
65
71 };
72
74 {
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))) {
81 }
82 }
83 if (
s->send_buffer_size > 0) {
84 if (setsockopt (fd, SOL_SOCKET, SO_SNDBUF, &
s->send_buffer_size, sizeof (
s->send_buffer_size))) {
86 }
87 }
88 if (
s->tcp_nodelay > 0) {
89 if (setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &
s->tcp_nodelay, sizeof (
s->tcp_nodelay))) {
91 }
92 }
93 #if !HAVE_WINSOCK2_H
95 if (setsockopt (fd, IPPROTO_TCP, TCP_MAXSEG, &
s->tcp_mss, sizeof (
s->tcp_mss))) {
97 }
98 }
99 #endif /* !HAVE_WINSOCK2_H */
100 }
101
102 /* return non zero if error */
104 {
105 struct addrinfo hints = { 0 }, *ai, *cur_ai;
106 int port, fd = -1;
108 const char *p;
109 char buf[256];
111 char hostname[1024],proto[1024],path[1024];
112 char portstr[10];
113 s->open_timeout = 5000000;
114
116 &port, path, sizeof(path), uri);
117 if (strcmp(proto, "tcp"))
119 if (port <= 0 || port >= 65536) {
122 }
123 p = strchr(uri, '?');
124 if (p) {
127 s->listen = strtol(buf, &endptr, 10);
128 /* assume if no digits were found it is a request to enable it */
129 if (buf == endptr)
131 }
133 s->rw_timeout = strtol(buf,
NULL, 10);
134 }
136 s->listen_timeout = strtol(buf,
NULL, 10);
137 }
138 }
139 if (
s->rw_timeout >= 0) {
141 h->rw_timeout =
s->rw_timeout;
142 }
145 snprintf(portstr,
sizeof(portstr),
"%d", port);
148 if (!hostname[0])
150 else
154 "Failed to resolve hostname %s: %s\n",
157 }
158
159 cur_ai = ai;
160
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);
167 }
168 }
169 #endif
170
172 while (cur_ai && fd < 0) {
174 cur_ai->ai_socktype,
175 cur_ai->ai_protocol);
176 if (fd < 0) {
178 cur_ai = cur_ai->ai_next;
179 }
180 }
181 if (fd < 0)
182 goto fail1;
184 }
185
186 if (
s->listen == 2) {
187 // multi-client
188 if ((
ret =
ff_listen(fd, cur_ai->ai_addr, cur_ai->ai_addrlen)) < 0)
189 goto fail1;
190 }
else if (
s->listen == 1) {
191 // single client
193 s->listen_timeout,
h)) < 0)
194 goto fail1;
195 // Socket descriptor already closed here. Safe to overwrite to client one.
197 } else {
200 goto fail1;
201 }
202
205
207 return 0;
208
209 fail1:
210 if (fd >= 0)
211 closesocket(fd);
214 }
215
217 {
224 cc = (*c)->priv_data;
229 }
231 return 0;
232 }
233
235 {
238
243 }
248 }
249
251 {
254
259 }
262 }
263
265 {
267 int how;
268
270 how = SHUT_RDWR;
272 how = SHUT_WR;
273 } else {
274 how = SHUT_RD;
275 }
276
277 return shutdown(
s->fd, how);
278 }
279
281 {
284 return 0;
285 }
286
288 {
291 }
292
294 {
296 int avail;
297 socklen_t avail_len = sizeof(avail);
298
299 #if HAVE_WINSOCK2_H
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) {
304 }
305 #endif
306
307 if (getsockopt(
s->fd, SOL_SOCKET, SO_RCVBUF, &avail, &avail_len)) {
309 }
310 return avail;
311 }
312
326 };