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
46
47 #define OFFSET(x) offsetof(TCPContext, x)
48 #define D AV_OPT_FLAG_DECODING_PARAM
49 #define E AV_OPT_FLAG_ENCODING_PARAM
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 },
58 };
59
65 };
66
67 /* return non zero if error */
69 {
70 struct addrinfo hints = { 0 }, *ai, *cur_ai;
71 int port, fd = -1;
73 const char *p;
75 int ret;
76 char hostname[1024],proto[1024],path[1024];
77 char portstr[10];
79
81 &port, path, sizeof(path), uri);
82 if (strcmp(proto, "tcp"))
84 if (port <= 0 || port >= 65536) {
87 }
88 p = strchr(uri, '?');
89 if (p) {
92 s->
listen = strtol(buf, &endptr, 10);
93 /* assume if no digits were found it is a request to enable it */
94 if (buf == endptr)
96 }
99 }
102 }
103 }
107 }
110 snprintf(portstr,
sizeof(portstr),
"%d", port);
113 if (!hostname[0])
115 else
117 if (ret) {
119 "Failed to resolve hostname %s: %s\n",
122 }
123
124 cur_ai = ai;
125
126 restart:
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);
133 }
134 }
135 #endif
136
138 cur_ai->ai_socktype,
139 cur_ai->ai_protocol);
140 if (fd < 0) {
143 }
144
145 /* Set the socket's send or receive buffer sizes, if specified.
146 If unspecified or setting fails, system default is used. */
149 }
152 }
155 }
156
158 // multi-client
159 if ((ret =
ff_listen(fd, cur_ai->ai_addr, cur_ai->ai_addrlen)) < 0)
160 goto fail1;
161 }
else if (s->
listen == 1) {
162 // single client
163 if ((ret =
ff_listen_bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
165 goto fail1;
166 // Socket descriptor already closed here. Safe to overwrite to client one.
167 fd = ret;
168 } else {
171
173 goto fail1;
174 else
176 }
177 }
178
181
183 return 0;
184
186 if (cur_ai->ai_next) {
187 /* Retry with the next sockaddr */
188 cur_ai = cur_ai->ai_next;
189 if (fd >= 0)
190 closesocket(fd);
191 ret = 0;
192 goto restart;
193 }
194 fail1:
195 if (fd >= 0)
196 closesocket(fd);
198 return ret;
199 }
200
202 {
205 int ret;
208 return ret;
209 cc = (*c)->priv_data;
211 if (ret < 0)
212 return ret;
214 return 0;
215 }
216
218 {
220 int ret;
221
224 if (ret)
225 return ret;
226 }
227 ret = recv(s->
fd, buf, size, 0);
228 if (ret == 0)
231 }
232
234 {
236 int ret;
237
240 if (ret)
241 return ret;
242 }
245 }
246
248 {
250 int how;
251
253 how = SHUT_RDWR;
255 how = SHUT_WR;
256 } else {
257 how = SHUT_RD;
258 }
259
260 return shutdown(s->
fd, how);
261 }
262
264 {
267 return 0;
268 }
269
271 {
274 }
275
277 {
279 int avail;
280 socklen_t avail_len = sizeof(avail);
281
282 #if HAVE_WINSOCK2_H
283 /* SO_RCVBUF with winsock only reports the actual TCP window size when
284 auto-tuning has been disabled via setting SO_RCVBUF */
287 }
288 #endif
289
290 if (getsockopt(s->
fd, SOL_SOCKET, SO_RCVBUF, &avail, &avail_len)) {
292 }
293 return avail;
294 }
295
308 .priv_data_class = &tcp_class,
309 };
void av_url_split(char *proto, int proto_size, char *authorization, int authorization_size, char *hostname, int hostname_size, int *port_ptr, char *path, int path_size, const char *url)
Split a URL string into components.
#define URL_PROTOCOL_FLAG_NETWORK
static const AVClass tcp_class
static int tcp_open(URLContext *h, const char *uri, int flags)
#define LIBAVUTIL_VERSION_INT
int is_streamed
true if streamed (no seek possible), default = false
AVIOInterruptCB interrupt_callback
#define AVIO_FLAG_READ
read-only
const char * av_default_item_name(void *ptr)
Return the context name.
int64_t rw_timeout
maximum time to wait for (network) read/write operation completion, in mcs
#define AVIO_FLAG_WRITE
write-only
int ff_socket(int af, int type, int proto)
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int ff_listen_bind(int fd, const struct sockaddr *addr, socklen_t addrlen, int timeout, URLContext *h)
Bind to a file descriptor and poll for a connection.
miscellaneous OS support macros and functions.
int ff_listen(int fd, const struct sockaddr *addr, socklen_t addrlen)
Bind to a file descriptor to an address without accepting connections.
#define AVERROR_EOF
End of file.
int ff_listen_connect(int fd, const struct sockaddr *addr, socklen_t addrlen, int timeout, URLContext *h, int will_try_next)
Connect to a file descriptor and poll for result.
int av_find_info_tag(char *arg, int arg_size, const char *tag1, const char *info)
Attempt to find a specific tag in a URL.
int ffurl_alloc(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb)
Create a URLContext for accessing to the resource indicated by url, but do not initiate the connectio...
static int tcp_read(URLContext *h, uint8_t *buf, int size)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int ff_accept(int fd, int timeout, URLContext *h)
Poll for a single connection on the passed file descriptor.
simple assert() macros that are a bit more flexible than ISO C assert().
int ff_network_wait_fd_timeout(int fd, int write, int64_t timeout, AVIOInterruptCB *int_cb)
This works similarly to ff_network_wait_fd, but waits up to 'timeout' microseconds Uses ff_network_wa...
static const AVOption options[]
static int tcp_close(URLContext *h)
static int tcp_shutdown(URLContext *h, int flags)
static int tcp_get_window_size(URLContext *h)
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
const URLProtocol ff_tcp_protocol
#define AVIO_FLAG_NONBLOCK
Use non-blocking mode.
Describe the class of an AVClass context structure.
char * filename
specified URL
static int tcp_write(URLContext *h, const uint8_t *buf, int size)
unbuffered private I/O API
static int tcp_get_file_handle(URLContext *h)
static int tcp_accept(URLContext *s, URLContext **c)