FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
tcp.c
Go to the documentation of this file.
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  */
21 #include "avformat.h"
22 #include "libavutil/avassert.h"
23 #include "libavutil/parseutils.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/time.h"
26 
27 #include "internal.h"
28 #include "network.h"
29 #include "os_support.h"
30 #include "url.h"
31 #if HAVE_POLL_H
32 #include <poll.h>
33 #endif
34 
35 typedef struct TCPContext {
36  const AVClass *class;
37  int fd;
38  int listen;
44 } TCPContext;
45 
46 #define OFFSET(x) offsetof(TCPContext, x)
47 #define D AV_OPT_FLAG_DECODING_PARAM
48 #define E AV_OPT_FLAG_ENCODING_PARAM
49 static const AVOption options[] = {
50  { "listen", "Listen for incoming connections", OFFSET(listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, .flags = D|E },
51  { "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 },
52  { "listen_timeout", "Connection awaiting timeout (in milliseconds)", OFFSET(listen_timeout), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
53  { "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 },
54  { "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 },
55  { NULL }
56 };
57 
58 static const AVClass tcp_class = {
59  .class_name = "tcp",
60  .item_name = av_default_item_name,
61  .option = options,
62  .version = LIBAVUTIL_VERSION_INT,
63 };
64 
65 /* return non zero if error */
66 static int tcp_open(URLContext *h, const char *uri, int flags)
67 {
68  struct addrinfo hints = { 0 }, *ai, *cur_ai;
69  int port, fd = -1;
70  TCPContext *s = h->priv_data;
71  const char *p;
72  char buf[256];
73  int ret;
74  char hostname[1024],proto[1024],path[1024];
75  char portstr[10];
76  s->open_timeout = 5000000;
77 
78  av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
79  &port, path, sizeof(path), uri);
80  if (strcmp(proto, "tcp"))
81  return AVERROR(EINVAL);
82  if (port <= 0 || port >= 65536) {
83  av_log(h, AV_LOG_ERROR, "Port missing in uri\n");
84  return AVERROR(EINVAL);
85  }
86  p = strchr(uri, '?');
87  if (p) {
88  if (av_find_info_tag(buf, sizeof(buf), "listen", p)) {
89  char *endptr = NULL;
90  s->listen = strtol(buf, &endptr, 10);
91  /* assume if no digits were found it is a request to enable it */
92  if (buf == endptr)
93  s->listen = 1;
94  }
95  if (av_find_info_tag(buf, sizeof(buf), "timeout", p)) {
96  s->rw_timeout = strtol(buf, NULL, 10);
97  }
98  if (av_find_info_tag(buf, sizeof(buf), "listen_timeout", p)) {
99  s->listen_timeout = strtol(buf, NULL, 10);
100  }
101  }
102  if (s->rw_timeout >= 0) {
103  s->open_timeout =
104  h->rw_timeout = s->rw_timeout;
105  }
106  hints.ai_family = AF_UNSPEC;
107  hints.ai_socktype = SOCK_STREAM;
108  snprintf(portstr, sizeof(portstr), "%d", port);
109  if (s->listen)
110  hints.ai_flags |= AI_PASSIVE;
111  if (!hostname[0])
112  ret = getaddrinfo(NULL, portstr, &hints, &ai);
113  else
114  ret = getaddrinfo(hostname, portstr, &hints, &ai);
115  if (ret) {
116  av_log(h, AV_LOG_ERROR,
117  "Failed to resolve hostname %s: %s\n",
118  hostname, gai_strerror(ret));
119  return AVERROR(EIO);
120  }
121 
122  cur_ai = ai;
123 
124  restart:
125 #if HAVE_STRUCT_SOCKADDR_IN6
126  // workaround for IOS9 getaddrinfo in IPv6 only network use hardcode IPv4 address can not resolve port number.
127  if (cur_ai->ai_family == AF_INET6){
128  struct sockaddr_in6 * sockaddr_v6 = (struct sockaddr_in6 *)cur_ai->ai_addr;
129  if (!sockaddr_v6->sin6_port){
130  sockaddr_v6->sin6_port = htons(port);
131  }
132  }
133 #endif
134 
135  fd = ff_socket(cur_ai->ai_family,
136  cur_ai->ai_socktype,
137  cur_ai->ai_protocol);
138  if (fd < 0) {
139  ret = ff_neterrno();
140  goto fail;
141  }
142 
143  /* Set the socket's send or receive buffer sizes, if specified.
144  If unspecified or setting fails, system default is used. */
145  if (s->recv_buffer_size > 0) {
146  setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &s->recv_buffer_size, sizeof (s->recv_buffer_size));
147  }
148  if (s->send_buffer_size > 0) {
149  setsockopt (fd, SOL_SOCKET, SO_SNDBUF, &s->send_buffer_size, sizeof (s->send_buffer_size));
150  }
151 
152  if (s->listen == 2) {
153  // multi-client
154  if ((ret = ff_listen(fd, cur_ai->ai_addr, cur_ai->ai_addrlen)) < 0)
155  goto fail1;
156  } else if (s->listen == 1) {
157  // single client
158  if ((ret = ff_listen_bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
159  s->listen_timeout, h)) < 0)
160  goto fail1;
161  // Socket descriptor already closed here. Safe to overwrite to client one.
162  fd = ret;
163  } else {
164  if ((ret = ff_listen_connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
165  s->open_timeout / 1000, h, !!cur_ai->ai_next)) < 0) {
166 
167  if (ret == AVERROR_EXIT)
168  goto fail1;
169  else
170  goto fail;
171  }
172  }
173 
174  h->is_streamed = 1;
175  s->fd = fd;
176 
177  freeaddrinfo(ai);
178  return 0;
179 
180  fail:
181  if (cur_ai->ai_next) {
182  /* Retry with the next sockaddr */
183  cur_ai = cur_ai->ai_next;
184  if (fd >= 0)
185  closesocket(fd);
186  ret = 0;
187  goto restart;
188  }
189  fail1:
190  if (fd >= 0)
191  closesocket(fd);
192  freeaddrinfo(ai);
193  return ret;
194 }
195 
197 {
198  TCPContext *sc = s->priv_data;
199  TCPContext *cc;
200  int ret;
201  av_assert0(sc->listen);
202  if ((ret = ffurl_alloc(c, s->filename, s->flags, &s->interrupt_callback)) < 0)
203  return ret;
204  cc = (*c)->priv_data;
205  ret = ff_accept(sc->fd, sc->listen_timeout, s);
206  if (ret < 0)
207  return ret;
208  cc->fd = ret;
209  return 0;
210 }
211 
212 static int tcp_read(URLContext *h, uint8_t *buf, int size)
213 {
214  TCPContext *s = h->priv_data;
215  int ret;
216 
217  if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
219  if (ret)
220  return ret;
221  }
222  ret = recv(s->fd, buf, size, 0);
223  return ret < 0 ? ff_neterrno() : ret;
224 }
225 
226 static int tcp_write(URLContext *h, const uint8_t *buf, int size)
227 {
228  TCPContext *s = h->priv_data;
229  int ret;
230 
231  if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
233  if (ret)
234  return ret;
235  }
236  ret = send(s->fd, buf, size, MSG_NOSIGNAL);
237  return ret < 0 ? ff_neterrno() : ret;
238 }
239 
240 static int tcp_shutdown(URLContext *h, int flags)
241 {
242  TCPContext *s = h->priv_data;
243  int how;
244 
245  if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
246  how = SHUT_RDWR;
247  } else if (flags & AVIO_FLAG_WRITE) {
248  how = SHUT_WR;
249  } else {
250  how = SHUT_RD;
251  }
252 
253  return shutdown(s->fd, how);
254 }
255 
256 static int tcp_close(URLContext *h)
257 {
258  TCPContext *s = h->priv_data;
259  closesocket(s->fd);
260  return 0;
261 }
262 
264 {
265  TCPContext *s = h->priv_data;
266  return s->fd;
267 }
268 
270 {
271  TCPContext *s = h->priv_data;
272  int avail;
273  int avail_len = sizeof(avail);
274 
275 #if HAVE_WINSOCK2_H
276  /* SO_RCVBUF with winsock only reports the actual TCP window size when
277  auto-tuning has been disabled via setting SO_RCVBUF */
278  if (s->recv_buffer_size < 0) {
279  return AVERROR(ENOSYS);
280  }
281 #endif
282 
283  if (getsockopt(s->fd, SOL_SOCKET, SO_RCVBUF, &avail, &avail_len)) {
284  return ff_neterrno();
285  }
286  return avail;
287 }
288 
290  .name = "tcp",
291  .url_open = tcp_open,
292  .url_accept = tcp_accept,
293  .url_read = tcp_read,
294  .url_write = tcp_write,
295  .url_close = tcp_close,
296  .url_get_file_handle = tcp_get_file_handle,
297  .url_get_short_seek = tcp_get_window_size,
298  .url_shutdown = tcp_shutdown,
299  .priv_data_size = sizeof(TCPContext),
301  .priv_data_class = &tcp_class,
302 };
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.
Definition: utils.c:4619
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
#define URL_PROTOCOL_FLAG_NETWORK
Definition: url.h:34
static const AVClass tcp_class
Definition: tcp.c:58
static int tcp_open(URLContext *h, const char *uri, int flags)
Definition: tcp.c:66
AVOption.
Definition: opt.h:246
int recv_buffer_size
Definition: tcp.c:42
#define LIBAVUTIL_VERSION_INT
Definition: version.h:86
int is_streamed
true if streamed (no seek possible), default = false
Definition: url.h:45
AVIOInterruptCB interrupt_callback
Definition: url.h:47
int rw_timeout
Definition: tcp.c:40
#define AVIO_FLAG_READ
read-only
Definition: avio.h:643
int64_t rw_timeout
maximum time to wait for (network) read/write operation completion, in mcs
Definition: url.h:48
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:644
#define AI_PASSIVE
Definition: network.h:169
int listen_timeout
Definition: tcp.c:41
int flags
Definition: url.h:43
#define freeaddrinfo
Definition: network.h:208
int ff_socket(int af, int type, int proto)
Definition: network.c:166
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
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.
Definition: network.c:226
uint8_t
AVOptions.
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.
Definition: network.c:190
static int flags
Definition: log.c:57
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.
Definition: network.c:238
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.
Definition: parseutils.c:733
ptrdiff_t size
Definition: opengl_enc.c:101
#define av_log(a,...)
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...
Definition: avio.c:292
static int tcp_read(URLContext *h, uint8_t *buf, int size)
Definition: tcp.c:212
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int ff_accept(int fd, int timeout, URLContext *h)
Poll for a single connection on the passed file descriptor.
Definition: network.c:208
#define E
Definition: tcp.c:48
av_default_item_name
#define closesocket
Definition: ffserver.c:28
#define AVERROR(e)
Definition: error.h:43
#define D
Definition: tcp.c:47
simple assert() macros that are a bit more flexible than ISO C assert().
#define OFFSET(x)
Definition: tcp.c:46
int send_buffer_size
Definition: tcp.c:43
int listen
Definition: tcp.c:38
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...
Definition: network.c:82
#define fail()
Definition: checkasm.h:95
static const AVOption options[]
Definition: tcp.c:49
static int tcp_close(URLContext *h)
Definition: tcp.c:256
static int tcp_shutdown(URLContext *h, int flags)
Definition: tcp.c:240
#define ff_neterrno()
Definition: network.h:64
static int tcp_get_window_size(URLContext *h)
Definition: tcp.c:269
Definition: tcp.c:35
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition: error.h:56
const URLProtocol ff_tcp_protocol
Definition: tcp.c:289
#define AVIO_FLAG_NONBLOCK
Use non-blocking mode.
Definition: avio.h:662
void * buf
Definition: avisynth_c.h:690
Definition: url.h:38
Describe the class of an AVClass context structure.
Definition: log.h:67
void * priv_data
Definition: url.h:41
#define gai_strerror
Definition: network.h:215
#define snprintf
Definition: snprintf.h:34
int fd
Definition: tcp.c:37
misc parsing utilities
const char * name
Definition: url.h:55
int ai_socktype
Definition: network.h:130
int open_timeout
Definition: tcp.c:39
#define getaddrinfo
Definition: network.h:207
Main libavformat public API header.
if(ret< 0)
Definition: vf_mcdeint.c:282
static double c[64]
char * filename
specified URL
Definition: url.h:42
static int tcp_write(URLContext *h, const uint8_t *buf, int size)
Definition: tcp.c:226
#define MSG_NOSIGNAL
Definition: network.h:123
int ai_flags
Definition: network.h:128
unbuffered private I/O API
static int tcp_get_file_handle(URLContext *h)
Definition: tcp.c:263
static int tcp_accept(URLContext *s, URLContext **c)
Definition: tcp.c:196
int ai_family
Definition: network.h:129