]> git.sesse.net Git - ffmpeg/blob - libavformat/tcp.c
avcodec: Add utils test
[ffmpeg] / libavformat / tcp.c
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;
39     int open_timeout;
40     int rw_timeout;
41     int listen_timeout;
42     int recv_buffer_size;
43     int send_buffer_size;
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     fd = ff_socket(cur_ai->ai_family,
126                    cur_ai->ai_socktype,
127                    cur_ai->ai_protocol);
128     if (fd < 0) {
129         ret = ff_neterrno();
130         goto fail;
131     }
132
133     if (s->listen == 2) {
134         // multi-client
135         if ((ret = ff_listen(fd, cur_ai->ai_addr, cur_ai->ai_addrlen)) < 0)
136             goto fail1;
137     } else if (s->listen == 1) {
138         // single client
139         if ((ret = ff_listen_bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
140                                   s->listen_timeout, h)) < 0)
141             goto fail1;
142         // Socket descriptor already closed here. Safe to overwrite to client one.
143         fd = ret;
144     } else {
145         if ((ret = ff_listen_connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
146                                      s->open_timeout / 1000, h, !!cur_ai->ai_next)) < 0) {
147
148             if (ret == AVERROR_EXIT)
149                 goto fail1;
150             else
151                 goto fail;
152         }
153     }
154
155     h->is_streamed = 1;
156     s->fd = fd;
157     /* Set the socket's send or receive buffer sizes, if specified.
158        If unspecified or setting fails, system default is used. */
159     if (s->recv_buffer_size > 0) {
160         setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &s->recv_buffer_size, sizeof (s->recv_buffer_size));
161     }
162     if (s->send_buffer_size > 0) {
163         setsockopt (fd, SOL_SOCKET, SO_SNDBUF, &s->send_buffer_size, sizeof (s->send_buffer_size));
164     }
165
166     freeaddrinfo(ai);
167     return 0;
168
169  fail:
170     if (cur_ai->ai_next) {
171         /* Retry with the next sockaddr */
172         cur_ai = cur_ai->ai_next;
173         if (fd >= 0)
174             closesocket(fd);
175         ret = 0;
176         goto restart;
177     }
178  fail1:
179     if (fd >= 0)
180         closesocket(fd);
181     freeaddrinfo(ai);
182     return ret;
183 }
184
185 static int tcp_accept(URLContext *s, URLContext **c)
186 {
187     TCPContext *sc = s->priv_data;
188     TCPContext *cc;
189     int ret;
190     av_assert0(sc->listen);
191     if ((ret = ffurl_alloc(c, s->filename, s->flags, &s->interrupt_callback)) < 0)
192         return ret;
193     cc = (*c)->priv_data;
194     ret = ff_accept(sc->fd, sc->listen_timeout, s);
195     if (ret < 0)
196         return ff_neterrno();
197     cc->fd = ret;
198     return 0;
199 }
200
201 static int tcp_read(URLContext *h, uint8_t *buf, int size)
202 {
203     TCPContext *s = h->priv_data;
204     int ret;
205
206     if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
207         ret = ff_network_wait_fd_timeout(s->fd, 0, h->rw_timeout, &h->interrupt_callback);
208         if (ret)
209             return ret;
210     }
211     ret = recv(s->fd, buf, size, 0);
212     return ret < 0 ? ff_neterrno() : ret;
213 }
214
215 static int tcp_write(URLContext *h, const uint8_t *buf, int size)
216 {
217     TCPContext *s = h->priv_data;
218     int ret;
219
220     if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
221         ret = ff_network_wait_fd_timeout(s->fd, 1, h->rw_timeout, &h->interrupt_callback);
222         if (ret)
223             return ret;
224     }
225     ret = send(s->fd, buf, size, MSG_NOSIGNAL);
226     return ret < 0 ? ff_neterrno() : ret;
227 }
228
229 static int tcp_shutdown(URLContext *h, int flags)
230 {
231     TCPContext *s = h->priv_data;
232     int how;
233
234     if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
235         how = SHUT_RDWR;
236     } else if (flags & AVIO_FLAG_WRITE) {
237         how = SHUT_WR;
238     } else {
239         how = SHUT_RD;
240     }
241
242     return shutdown(s->fd, how);
243 }
244
245 static int tcp_close(URLContext *h)
246 {
247     TCPContext *s = h->priv_data;
248     closesocket(s->fd);
249     return 0;
250 }
251
252 static int tcp_get_file_handle(URLContext *h)
253 {
254     TCPContext *s = h->priv_data;
255     return s->fd;
256 }
257
258 const URLProtocol ff_tcp_protocol = {
259     .name                = "tcp",
260     .url_open            = tcp_open,
261     .url_accept          = tcp_accept,
262     .url_read            = tcp_read,
263     .url_write           = tcp_write,
264     .url_close           = tcp_close,
265     .url_get_file_handle = tcp_get_file_handle,
266     .url_shutdown        = tcp_shutdown,
267     .priv_data_size      = sizeof(TCPContext),
268     .flags               = URL_PROTOCOL_FLAG_NETWORK,
269     .priv_data_class     = &tcp_class,
270 };