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;
45 } TCPContext;
46 
47 #define OFFSET(x) offsetof(TCPContext, x)
48 #define D AV_OPT_FLAG_DECODING_PARAM
49 #define E AV_OPT_FLAG_ENCODING_PARAM
50 static const AVOption options[] = {
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 },
57  { NULL }
58 };
59 
60 static const AVClass tcp_class = {
61  .class_name = "tcp",
62  .item_name = av_default_item_name,
63  .option = options,
64  .version = LIBAVUTIL_VERSION_INT,
65 };
66 
67 /* return non zero if error */
68 static int tcp_open(URLContext *h, const char *uri, int flags)
69 {
70  struct addrinfo hints = { 0 }, *ai, *cur_ai;
71  int port, fd = -1;
72  TCPContext *s = h->priv_data;
73  const char *p;
74  char buf[256];
75  int ret;
76  char hostname[1024],proto[1024],path[1024];
77  char portstr[10];
78  s->open_timeout = 5000000;
79 
80  av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
81  &port, path, sizeof(path), uri);
82  if (strcmp(proto, "tcp"))
83  return AVERROR(EINVAL);
84  if (port <= 0 || port >= 65536) {
85  av_log(h, AV_LOG_ERROR, "Port missing in uri\n");
86  return AVERROR(EINVAL);
87  }
88  p = strchr(uri, '?');
89  if (p) {
90  if (av_find_info_tag(buf, sizeof(buf), "listen", p)) {
91  char *endptr = NULL;
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)
95  s->listen = 1;
96  }
97  if (av_find_info_tag(buf, sizeof(buf), "timeout", p)) {
98  s->rw_timeout = strtol(buf, NULL, 10);
99  }
100  if (av_find_info_tag(buf, sizeof(buf), "listen_timeout", p)) {
101  s->listen_timeout = strtol(buf, NULL, 10);
102  }
103  }
104  if (s->rw_timeout >= 0) {
105  s->open_timeout =
106  h->rw_timeout = s->rw_timeout;
107  }
108  hints.ai_family = AF_UNSPEC;
109  hints.ai_socktype = SOCK_STREAM;
110  snprintf(portstr, sizeof(portstr), "%d", port);
111  if (s->listen)
112  hints.ai_flags |= AI_PASSIVE;
113  if (!hostname[0])
114  ret = getaddrinfo(NULL, portstr, &hints, &ai);
115  else
116  ret = getaddrinfo(hostname, portstr, &hints, &ai);
117  if (ret) {
118  av_log(h, AV_LOG_ERROR,
119  "Failed to resolve hostname %s: %s\n",
120  hostname, gai_strerror(ret));
121  return AVERROR(EIO);
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 
137  fd = ff_socket(cur_ai->ai_family,
138  cur_ai->ai_socktype,
139  cur_ai->ai_protocol);
140  if (fd < 0) {
141  ret = ff_neterrno();
142  goto fail;
143  }
144 
145  /* Set the socket's send or receive buffer sizes, if specified.
146  If unspecified or setting fails, system default is used. */
147  if (s->recv_buffer_size > 0) {
148  setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &s->recv_buffer_size, sizeof (s->recv_buffer_size));
149  }
150  if (s->send_buffer_size > 0) {
151  setsockopt (fd, SOL_SOCKET, SO_SNDBUF, &s->send_buffer_size, sizeof (s->send_buffer_size));
152  }
153  if (s->tcp_nodelay > 0) {
154  setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &s->tcp_nodelay, sizeof (s->tcp_nodelay));
155  }
156 
157  if (s->listen == 2) {
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,
164  s->listen_timeout, h)) < 0)
165  goto fail1;
166  // Socket descriptor already closed here. Safe to overwrite to client one.
167  fd = ret;
168  } else {
169  if ((ret = ff_listen_connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
170  s->open_timeout / 1000, h, !!cur_ai->ai_next)) < 0) {
171 
172  if (ret == AVERROR_EXIT)
173  goto fail1;
174  else
175  goto fail;
176  }
177  }
178 
179  h->is_streamed = 1;
180  s->fd = fd;
181 
182  freeaddrinfo(ai);
183  return 0;
184 
185  fail:
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);
197  freeaddrinfo(ai);
198  return ret;
199 }
200 
202 {
203  TCPContext *sc = s->priv_data;
204  TCPContext *cc;
205  int ret;
206  av_assert0(sc->listen);
207  if ((ret = ffurl_alloc(c, s->filename, s->flags, &s->interrupt_callback)) < 0)
208  return ret;
209  cc = (*c)->priv_data;
210  ret = ff_accept(sc->fd, sc->listen_timeout, s);
211  if (ret < 0)
212  return ret;
213  cc->fd = ret;
214  return 0;
215 }
216 
217 static int tcp_read(URLContext *h, uint8_t *buf, int size)
218 {
219  TCPContext *s = h->priv_data;
220  int ret;
221 
222  if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
224  if (ret)
225  return ret;
226  }
227  ret = recv(s->fd, buf, size, 0);
228  if (ret == 0)
229  return AVERROR_EOF;
230  return ret < 0 ? ff_neterrno() : ret;
231 }
232 
233 static int tcp_write(URLContext *h, const uint8_t *buf, int size)
234 {
235  TCPContext *s = h->priv_data;
236  int ret;
237 
238  if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
240  if (ret)
241  return ret;
242  }
243  ret = send(s->fd, buf, size, MSG_NOSIGNAL);
244  return ret < 0 ? ff_neterrno() : ret;
245 }
246 
247 static int tcp_shutdown(URLContext *h, int flags)
248 {
249  TCPContext *s = h->priv_data;
250  int how;
251 
252  if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
253  how = SHUT_RDWR;
254  } else if (flags & AVIO_FLAG_WRITE) {
255  how = SHUT_WR;
256  } else {
257  how = SHUT_RD;
258  }
259 
260  return shutdown(s->fd, how);
261 }
262 
263 static int tcp_close(URLContext *h)
264 {
265  TCPContext *s = h->priv_data;
266  closesocket(s->fd);
267  return 0;
268 }
269 
271 {
272  TCPContext *s = h->priv_data;
273  return s->fd;
274 }
275 
277 {
278  TCPContext *s = h->priv_data;
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 */
285  if (s->recv_buffer_size < 0) {
286  return AVERROR(ENOSYS);
287  }
288 #endif
289 
290  if (getsockopt(s->fd, SOL_SOCKET, SO_RCVBUF, &avail, &avail_len)) {
291  return ff_neterrno();
292  }
293  return avail;
294 }
295 
297  .name = "tcp",
298  .url_open = tcp_open,
299  .url_accept = tcp_accept,
300  .url_read = tcp_read,
301  .url_write = tcp_write,
302  .url_close = tcp_close,
303  .url_get_file_handle = tcp_get_file_handle,
304  .url_get_short_seek = tcp_get_window_size,
305  .url_shutdown = tcp_shutdown,
306  .priv_data_size = sizeof(TCPContext),
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.
Definition: utils.c:4700
#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:60
static int tcp_open(URLContext *h, const char *uri, int flags)
Definition: tcp.c:68
AVOption.
Definition: opt.h:246
int recv_buffer_size
Definition: tcp.c:42
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
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:654
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
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:655
#define AI_PASSIVE
Definition: network.h:176
int listen_timeout
Definition: tcp.c:41
int flags
Definition: url.h:43
#define freeaddrinfo
Definition: network.h:215
int ff_socket(int af, int type, int proto)
Definition: network.c:179
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:239
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:203
static int flags
Definition: log.c:55
#define AVERROR_EOF
End of file.
Definition: error.h:55
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:251
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:743
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:290
static int tcp_read(URLContext *h, uint8_t *buf, int size)
Definition: tcp.c:217
#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:221
#define E
Definition: tcp.c:49
#define AVERROR(e)
Definition: error.h:43
#define D
Definition: tcp.c:48
simple assert() macros that are a bit more flexible than ISO C assert().
#define OFFSET(x)
Definition: tcp.c:47
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:77
#define fail()
Definition: checkasm.h:116
static const AVOption options[]
Definition: tcp.c:50
static int tcp_close(URLContext *h)
Definition: tcp.c:263
static int tcp_shutdown(URLContext *h, int flags)
Definition: tcp.c:247
#define ff_neterrno()
Definition: network.h:65
static int tcp_get_window_size(URLContext *h)
Definition: tcp.c:276
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:296
#define AVIO_FLAG_NONBLOCK
Use non-blocking mode.
Definition: avio.h:673
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:222
#define snprintf
Definition: snprintf.h:34
int fd
Definition: tcp.c:37
misc parsing utilities
int tcp_nodelay
Definition: tcp.c:44
const char * name
Definition: url.h:55
int ai_socktype
Definition: network.h:137
int open_timeout
Definition: tcp.c:39
#define getaddrinfo
Definition: network.h:214
Main libavformat public API header.
if(ret< 0)
Definition: vf_mcdeint.c:279
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:233
#define MSG_NOSIGNAL
Definition: network.h:130
int ai_flags
Definition: network.h:135
unbuffered private I/O API
static int tcp_get_file_handle(URLContext *h)
Definition: tcp.c:270
static int tcp_accept(URLContext *s, URLContext **c)
Definition: tcp.c:201
int ai_family
Definition: network.h:136