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