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