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