]> 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, *ai, *cur_ai;
41     int port, fd = -1;
42     TCPContext *s = NULL;
43     int listen_socket = 0;
44     const char *p;
45     char buf[256];
46     int ret;
47     socklen_t optlen;
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     }
61     memset(&hints, 0, sizeof(hints));
62     hints.ai_family = AF_UNSPEC;
63     hints.ai_socktype = SOCK_STREAM;
64     snprintf(portstr, sizeof(portstr), "%d", port);
65     ret = getaddrinfo(hostname, portstr, &hints, &ai);
66     if (ret) {
67         av_log(h, AV_LOG_ERROR,
68                "Failed to resolve hostname %s: %s\n",
69                hostname, gai_strerror(ret));
70         return AVERROR(EIO);
71     }
72
73     cur_ai = ai;
74
75  restart:
76     fd = socket(cur_ai->ai_family, cur_ai->ai_socktype, cur_ai->ai_protocol);
77     if (fd < 0)
78         goto fail;
79
80     if (listen_socket) {
81         int fd1;
82         ret = bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
83         listen(fd, 1);
84         fd1 = accept(fd, NULL, NULL);
85         closesocket(fd);
86         fd = fd1;
87     } else {
88  redo:
89         ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
90     }
91
92     ff_socket_nonblock(fd, 1);
93
94     if (ret < 0) {
95         int timeout=50;
96         struct pollfd p = {fd, POLLOUT, 0};
97         if (ff_neterrno() == AVERROR(EINTR)) {
98             if (url_interrupt_cb()) {
99                 ret = AVERROR_EXIT;
100                 goto fail1;
101             }
102             goto redo;
103         }
104         if (ff_neterrno() != AVERROR(EINPROGRESS) &&
105             ff_neterrno() != AVERROR(EAGAIN))
106             goto fail;
107
108         /* wait until we are connected or until abort */
109         for(;;) {
110             if (url_interrupt_cb()) {
111                 ret = AVERROR_EXIT;
112                 goto fail1;
113             }
114             ret = poll(&p, 1, 100);
115             if (ret > 0)
116                 break;
117             if(!--timeout){
118                 av_log(NULL, AV_LOG_ERROR,
119                     "TCP open %s:%d timeout\n",
120                     hostname, port);
121                 goto fail;
122             }
123         }
124
125         /* test error */
126         optlen = sizeof(ret);
127         getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
128         if (ret != 0) {
129             av_log(h, AV_LOG_ERROR,
130                    "TCP connection to %s:%d failed: %s\n",
131                    hostname, port, strerror(ret));
132             goto fail;
133         }
134     }
135     s = av_malloc(sizeof(TCPContext));
136     if (!s) {
137         freeaddrinfo(ai);
138         return AVERROR(ENOMEM);
139     }
140     h->priv_data = s;
141     h->is_streamed = 1;
142     s->fd = fd;
143     freeaddrinfo(ai);
144     return 0;
145
146  fail:
147     if (cur_ai->ai_next) {
148         /* Retry with the next sockaddr */
149         cur_ai = cur_ai->ai_next;
150         if (fd >= 0)
151             closesocket(fd);
152         goto restart;
153     }
154     ret = AVERROR(EIO);
155  fail1:
156     if (fd >= 0)
157         closesocket(fd);
158     freeaddrinfo(ai);
159     return ret;
160 }
161
162 static int tcp_read(URLContext *h, uint8_t *buf, int size)
163 {
164     TCPContext *s = h->priv_data;
165     int ret;
166
167     if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
168         ret = ff_network_wait_fd(s->fd, 0);
169         if (ret < 0)
170             return ret;
171     }
172     ret = recv(s->fd, buf, size, 0);
173     return ret < 0 ? ff_neterrno() : ret;
174 }
175
176 static int tcp_write(URLContext *h, const uint8_t *buf, int size)
177 {
178     TCPContext *s = h->priv_data;
179     int ret;
180
181     if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
182         ret = ff_network_wait_fd(s->fd, 1);
183         if (ret < 0)
184             return ret;
185     }
186     ret = send(s->fd, buf, size, 0);
187     return ret < 0 ? ff_neterrno() : ret;
188 }
189
190 static int tcp_close(URLContext *h)
191 {
192     TCPContext *s = h->priv_data;
193     closesocket(s->fd);
194     av_free(s);
195     return 0;
196 }
197
198 static int tcp_get_file_handle(URLContext *h)
199 {
200     TCPContext *s = h->priv_data;
201     return s->fd;
202 }
203
204 URLProtocol ff_tcp_protocol = {
205     .name                = "tcp",
206     .url_open            = tcp_open,
207     .url_read            = tcp_read,
208     .url_write           = tcp_write,
209     .url_close           = tcp_close,
210     .url_get_file_handle = tcp_get_file_handle,
211 };