]> git.sesse.net Git - ffmpeg/blob - libavformat/tcp.c
tcp: add initial timeout limit for incoming connections
[ffmpeg] / libavformat / tcp.c
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 "internal.h"
24 #include "network.h"
25 #include "os_support.h"
26 #include "url.h"
27 #if HAVE_POLL_H
28 #include <poll.h>
29 #endif
30
31 typedef struct TCPContext {
32     int fd;
33 } TCPContext;
34
35 /* return non zero if error */
36 static int tcp_open(URLContext *h, const char *uri, int flags)
37 {
38     struct addrinfo hints = { 0 }, *ai, *cur_ai;
39     int port, fd = -1;
40     TCPContext *s = h->priv_data;
41     int listen_socket = 0;
42     const char *p;
43     char buf[256];
44     int ret;
45     socklen_t optlen;
46     int timeout = 100, listen_timeout = -1;
47     char hostname[1024],proto[1024],path[1024];
48     char portstr[10];
49
50     av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
51         &port, path, sizeof(path), uri);
52     if (strcmp(proto,"tcp") || port <= 0 || port >= 65536)
53         return AVERROR(EINVAL);
54
55     p = strchr(uri, '?');
56     if (p) {
57         if (av_find_info_tag(buf, sizeof(buf), "listen", p))
58             listen_socket = 1;
59         if (av_find_info_tag(buf, sizeof(buf), "timeout", p)) {
60             timeout = strtol(buf, NULL, 10);
61         }
62         if (av_find_info_tag(buf, sizeof(buf), "listen_timeout", p)) {
63             listen_timeout = strtol(buf, NULL, 10);
64         }
65     }
66     hints.ai_family = AF_UNSPEC;
67     hints.ai_socktype = SOCK_STREAM;
68     snprintf(portstr, sizeof(portstr), "%d", port);
69     if (listen_socket)
70         hints.ai_flags |= AI_PASSIVE;
71     if (!hostname[0])
72         ret = getaddrinfo(NULL, portstr, &hints, &ai);
73     else
74         ret = getaddrinfo(hostname, portstr, &hints, &ai);
75     if (ret) {
76         av_log(h, AV_LOG_ERROR,
77                "Failed to resolve hostname %s: %s\n",
78                hostname, gai_strerror(ret));
79         return AVERROR(EIO);
80     }
81
82     cur_ai = ai;
83
84  restart:
85     ret = AVERROR(EIO);
86     fd = socket(cur_ai->ai_family, cur_ai->ai_socktype, cur_ai->ai_protocol);
87     if (fd < 0)
88         goto fail;
89
90     if (listen_socket) {
91         int fd1;
92         int reuse = 1;
93         struct pollfd lp = { fd, POLLIN, 0 };
94         setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
95         ret = bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
96         if (ret) {
97             ret = ff_neterrno();
98             goto fail1;
99         }
100         ret = listen(fd, 1);
101         if (ret) {
102             ret = ff_neterrno();
103             goto fail1;
104         }
105         ret = poll(&lp, 1, listen_timeout >= 0 ? listen_timeout : -1);
106         if (ret <= 0) {
107             ret = AVERROR(ETIMEDOUT);
108             goto fail1;
109         }
110         fd1 = accept(fd, NULL, NULL);
111         if (fd1 < 0) {
112             ret = ff_neterrno();
113             goto fail1;
114         }
115         closesocket(fd);
116         fd = fd1;
117         ff_socket_nonblock(fd, 1);
118     } else {
119  redo:
120         ff_socket_nonblock(fd, 1);
121         ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
122     }
123
124     if (ret < 0) {
125         struct pollfd p = {fd, POLLOUT, 0};
126         ret = ff_neterrno();
127         if (ret == AVERROR(EINTR)) {
128             if (ff_check_interrupt(&h->interrupt_callback)) {
129                 ret = AVERROR_EXIT;
130                 goto fail1;
131             }
132             goto redo;
133         }
134         if (ret != AVERROR(EINPROGRESS) &&
135             ret != AVERROR(EAGAIN))
136             goto fail;
137
138         /* wait until we are connected or until abort */
139         while(timeout--) {
140             if (ff_check_interrupt(&h->interrupt_callback)) {
141                 ret = AVERROR_EXIT;
142                 goto fail1;
143             }
144             ret = poll(&p, 1, 100);
145             if (ret > 0)
146                 break;
147         }
148         if (ret <= 0) {
149             ret = AVERROR(ETIMEDOUT);
150             goto fail;
151         }
152         /* test error */
153         optlen = sizeof(ret);
154         if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen))
155             ret = AVUNERROR(ff_neterrno());
156         if (ret != 0) {
157             char errbuf[100];
158             ret = AVERROR(ret);
159             av_strerror(ret, errbuf, sizeof(errbuf));
160             av_log(h, AV_LOG_ERROR,
161                    "TCP connection to %s:%d failed: %s\n",
162                    hostname, port, errbuf);
163             goto fail;
164         }
165     }
166     h->is_streamed = 1;
167     s->fd = fd;
168     freeaddrinfo(ai);
169     return 0;
170
171  fail:
172     if (cur_ai->ai_next) {
173         /* Retry with the next sockaddr */
174         cur_ai = cur_ai->ai_next;
175         if (fd >= 0)
176             closesocket(fd);
177         goto restart;
178     }
179  fail1:
180     if (fd >= 0)
181         closesocket(fd);
182     freeaddrinfo(ai);
183     return ret;
184 }
185
186 static int tcp_read(URLContext *h, uint8_t *buf, int size)
187 {
188     TCPContext *s = h->priv_data;
189     int ret;
190
191     if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
192         ret = ff_network_wait_fd(s->fd, 0);
193         if (ret < 0)
194             return ret;
195     }
196     ret = recv(s->fd, buf, size, 0);
197     return ret < 0 ? ff_neterrno() : ret;
198 }
199
200 static int tcp_write(URLContext *h, const uint8_t *buf, int size)
201 {
202     TCPContext *s = h->priv_data;
203     int ret;
204
205     if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
206         ret = ff_network_wait_fd(s->fd, 1);
207         if (ret < 0)
208             return ret;
209     }
210     ret = send(s->fd, buf, size, 0);
211     return ret < 0 ? ff_neterrno() : ret;
212 }
213
214 static int tcp_shutdown(URLContext *h, int flags)
215 {
216     TCPContext *s = h->priv_data;
217     int how;
218
219     if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
220         how = SHUT_RDWR;
221     } else if (flags & AVIO_FLAG_WRITE) {
222         how = SHUT_WR;
223     } else {
224         how = SHUT_RD;
225     }
226
227     return shutdown(s->fd, how);
228 }
229
230 static int tcp_close(URLContext *h)
231 {
232     TCPContext *s = h->priv_data;
233     closesocket(s->fd);
234     return 0;
235 }
236
237 static int tcp_get_file_handle(URLContext *h)
238 {
239     TCPContext *s = h->priv_data;
240     return s->fd;
241 }
242
243 URLProtocol ff_tcp_protocol = {
244     .name                = "tcp",
245     .url_open            = tcp_open,
246     .url_read            = tcp_read,
247     .url_write           = tcp_write,
248     .url_close           = tcp_close,
249     .url_get_file_handle = tcp_get_file_handle,
250     .url_shutdown        = tcp_shutdown,
251     .priv_data_size      = sizeof(TCPContext),
252     .flags               = URL_PROTOCOL_FLAG_NETWORK,
253 };