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/parseutils.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/time.h"
25 
26 #include "internal.h"
27 #include "network.h"
28 #include "os_support.h"
29 #include "url.h"
30 #if HAVE_POLL_H
31 #include <poll.h>
32 #endif
33 
34 typedef struct TCPContext {
35  const AVClass *class;
36  int fd;
37  int listen;
41 } TCPContext;
42 
43 #define OFFSET(x) offsetof(TCPContext, x)
44 #define D AV_OPT_FLAG_DECODING_PARAM
45 #define E AV_OPT_FLAG_ENCODING_PARAM
46 static const AVOption options[] = {
47  { "listen", "Listen for incoming connections", OFFSET(listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, .flags = D|E },
48  { "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 },
49  { "listen_timeout", "Connection awaiting timeout (in milliseconds)", OFFSET(listen_timeout), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
50  { NULL }
51 };
52 
53 static const AVClass tcp_class = {
54  .class_name = "tcp",
55  .item_name = av_default_item_name,
56  .option = options,
57  .version = LIBAVUTIL_VERSION_INT,
58 };
59 
60 /* return non zero if error */
61 static int tcp_open(URLContext *h, const char *uri, int flags)
62 {
63  struct addrinfo hints = { 0 }, *ai, *cur_ai;
64  int port, fd = -1;
65  TCPContext *s = h->priv_data;
66  const char *p;
67  char buf[256];
68  int ret;
69  char hostname[1024],proto[1024],path[1024];
70  char portstr[10];
71  s->open_timeout = 5000000;
72 
73  av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
74  &port, path, sizeof(path), uri);
75  if (strcmp(proto, "tcp"))
76  return AVERROR(EINVAL);
77  if (port <= 0 || port >= 65536) {
78  av_log(h, AV_LOG_ERROR, "Port missing in uri\n");
79  return AVERROR(EINVAL);
80  }
81  p = strchr(uri, '?');
82  if (p) {
83  if (av_find_info_tag(buf, sizeof(buf), "listen", p)) {
84  char *endptr = NULL;
85  s->listen = strtol(buf, &endptr, 10);
86  /* assume if no digits were found it is a request to enable it */
87  if (buf == endptr)
88  s->listen = 1;
89  }
90  if (av_find_info_tag(buf, sizeof(buf), "timeout", p)) {
91  s->rw_timeout = strtol(buf, NULL, 10);
92  }
93  if (av_find_info_tag(buf, sizeof(buf), "listen_timeout", p)) {
94  s->listen_timeout = strtol(buf, NULL, 10);
95  }
96  }
97  if (s->rw_timeout >= 0) {
98  s->open_timeout =
99  h->rw_timeout = s->rw_timeout;
100  }
101  hints.ai_family = AF_UNSPEC;
102  hints.ai_socktype = SOCK_STREAM;
103  snprintf(portstr, sizeof(portstr), "%d", port);
104  if (s->listen)
105  hints.ai_flags |= AI_PASSIVE;
106  if (!hostname[0])
107  ret = getaddrinfo(NULL, portstr, &hints, &ai);
108  else
109  ret = getaddrinfo(hostname, portstr, &hints, &ai);
110  if (ret) {
111  av_log(h, AV_LOG_ERROR,
112  "Failed to resolve hostname %s: %s\n",
113  hostname, gai_strerror(ret));
114  return AVERROR(EIO);
115  }
116 
117  cur_ai = ai;
118 
119  restart:
120  fd = ff_socket(cur_ai->ai_family,
121  cur_ai->ai_socktype,
122  cur_ai->ai_protocol);
123  if (fd < 0) {
124  ret = ff_neterrno();
125  goto fail;
126  }
127 
128  if (s->listen) {
129  if ((ret = ff_listen_bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
130  s->listen_timeout, h)) < 0) {
131  goto fail1;
132  }
133  fd = ret;
134  } else {
135  if ((ret = ff_listen_connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
136  s->open_timeout / 1000, h, !!cur_ai->ai_next)) < 0) {
137 
138  if (ret == AVERROR_EXIT)
139  goto fail1;
140  else
141  goto fail;
142  }
143  }
144 
145  h->is_streamed = 1;
146  s->fd = fd;
147  freeaddrinfo(ai);
148  return 0;
149 
150  fail:
151  if (cur_ai->ai_next) {
152  /* Retry with the next sockaddr */
153  cur_ai = cur_ai->ai_next;
154  if (fd >= 0)
155  closesocket(fd);
156  ret = 0;
157  goto restart;
158  }
159  fail1:
160  if (fd >= 0)
161  closesocket(fd);
162  freeaddrinfo(ai);
163  return ret;
164 }
165 
166 static int tcp_read(URLContext *h, uint8_t *buf, int size)
167 {
168  TCPContext *s = h->priv_data;
169  int ret;
170 
171  if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
173  if (ret)
174  return ret;
175  }
176  ret = recv(s->fd, buf, size, 0);
177  return ret < 0 ? ff_neterrno() : ret;
178 }
179 
180 static int tcp_write(URLContext *h, const uint8_t *buf, int size)
181 {
182  TCPContext *s = h->priv_data;
183  int ret;
184 
185  if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
187  if (ret)
188  return ret;
189  }
190  ret = send(s->fd, buf, size, MSG_NOSIGNAL);
191  return ret < 0 ? ff_neterrno() : ret;
192 }
193 
194 static int tcp_shutdown(URLContext *h, int flags)
195 {
196  TCPContext *s = h->priv_data;
197  int how;
198 
199  if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
200  how = SHUT_RDWR;
201  } else if (flags & AVIO_FLAG_WRITE) {
202  how = SHUT_WR;
203  } else {
204  how = SHUT_RD;
205  }
206 
207  return shutdown(s->fd, how);
208 }
209 
210 static int tcp_close(URLContext *h)
211 {
212  TCPContext *s = h->priv_data;
213  closesocket(s->fd);
214  return 0;
215 }
216 
218 {
219  TCPContext *s = h->priv_data;
220  return s->fd;
221 }
222 
224  .name = "tcp",
225  .url_open = tcp_open,
226  .url_read = tcp_read,
227  .url_write = tcp_write,
228  .url_close = tcp_close,
229  .url_get_file_handle = tcp_get_file_handle,
230  .url_shutdown = tcp_shutdown,
231  .priv_data_size = sizeof(TCPContext),
233  .priv_data_class = &tcp_class,
234 };
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:3886
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
#define URL_PROTOCOL_FLAG_NETWORK
Definition: url.h:35
static const AVClass tcp_class
Definition: tcp.c:53
static int tcp_open(URLContext *h, const char *uri, int flags)
Definition: tcp.c:61
AVOption.
Definition: opt.h:255
#define LIBAVUTIL_VERSION_INT
Definition: version.h:62
int is_streamed
true if streamed (no seek possible), default = false
Definition: url.h:46
AVIOInterruptCB interrupt_callback
Definition: url.h:48
int rw_timeout
Definition: tcp.c:39
#define AVIO_FLAG_READ
read-only
Definition: avio.h:460
int64_t rw_timeout
maximum time to wait for (network) read/write operation completion, in mcs
Definition: url.h:49
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:461
#define AI_PASSIVE
Definition: network.h:169
int listen_timeout
Definition: tcp.c:40
int flags
Definition: url.h:44
#define freeaddrinfo
Definition: network.h:208
URLProtocol ff_tcp_protocol
Definition: tcp.c:223
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
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:190
uint8_t
AVOptions.
miscellaneous OS support macros and functions.
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:223
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:673
ptrdiff_t size
Definition: opengl_enc.c:101
#define av_log(a,...)
static int tcp_read(URLContext *h, uint8_t *buf, int size)
Definition: tcp.c:166
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define E
Definition: tcp.c:45
av_default_item_name
#define closesocket
Definition: ffserver.c:28
#define AVERROR(e)
Definition: error.h:43
#define D
Definition: tcp.c:44
#define OFFSET(x)
Definition: tcp.c:43
int listen
Definition: tcp.c:37
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
static const AVOption options[]
Definition: tcp.c:46
static int tcp_close(URLContext *h)
Definition: tcp.c:210
static int tcp_shutdown(URLContext *h, int flags)
Definition: tcp.c:194
ret
Definition: avfilter.c:974
#define ff_neterrno()
Definition: network.h:64
Definition: tcp.c:34
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition: error.h:56
#define AVIO_FLAG_NONBLOCK
Use non-blocking mode.
Definition: avio.h:479
void * buf
Definition: avisynth_c.h:553
Definition: url.h:39
Describe the class of an AVClass context structure.
Definition: log.h:67
void * priv_data
Definition: url.h:42
#define gai_strerror
Definition: network.h:215
#define snprintf
Definition: snprintf.h:34
int fd
Definition: tcp.c:36
misc parsing utilities
const char * name
Definition: url.h:53
int ai_socktype
Definition: network.h:130
static int flags
Definition: cpu.c:47
int open_timeout
Definition: tcp.c:38
#define getaddrinfo
Definition: network.h:207
Main libavformat public API header.
static int tcp_write(URLContext *h, const uint8_t *buf, int size)
Definition: tcp.c:180
#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:217
int ai_family
Definition: network.h:129