]> git.sesse.net Git - ffmpeg/blob - libavformat/tcp.c
lavfi: rename vsrc_buffer.c to buffersrc.c
[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 <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 = 100;
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         ret = bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
87         listen(fd, 1);
88         fd1 = accept(fd, NULL, NULL);
89         closesocket(fd);
90         fd = fd1;
91         ff_socket_nonblock(fd, 1);
92     } else {
93  redo:
94         ff_socket_nonblock(fd, 1);
95         ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
96     }
97
98     if (ret < 0) {
99         struct pollfd p = {fd, POLLOUT, 0};
100         ret = ff_neterrno();
101         if (ret == AVERROR(EINTR)) {
102             if (ff_check_interrupt(&h->interrupt_callback)) {
103                 ret = AVERROR_EXIT;
104                 goto fail1;
105             }
106             goto redo;
107         }
108         if (ret != AVERROR(EINPROGRESS) &&
109             ret != AVERROR(EAGAIN))
110             goto fail;
111
112         /* wait until we are connected or until abort */
113         while(timeout--) {
114             if (ff_check_interrupt(&h->interrupt_callback)) {
115                 ret = AVERROR_EXIT;
116                 goto fail1;
117             }
118             ret = poll(&p, 1, 100);
119             if (ret > 0)
120                 break;
121         }
122         if (ret <= 0) {
123             ret = AVERROR(ETIMEDOUT);
124             goto fail;
125         }
126         /* test error */
127         optlen = sizeof(ret);
128         getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
129         if (ret != 0) {
130             av_log(h, AV_LOG_ERROR,
131                    "TCP connection to %s:%d failed: %s\n",
132                    hostname, port, strerror(ret));
133             ret = AVERROR(ret);
134             goto fail;
135         }
136     }
137     h->is_streamed = 1;
138     s->fd = fd;
139     freeaddrinfo(ai);
140     return 0;
141
142  fail:
143     if (cur_ai->ai_next) {
144         /* Retry with the next sockaddr */
145         cur_ai = cur_ai->ai_next;
146         if (fd >= 0)
147             closesocket(fd);
148         goto restart;
149     }
150  fail1:
151     if (fd >= 0)
152         closesocket(fd);
153     freeaddrinfo(ai);
154     return ret;
155 }
156
157 static int tcp_read(URLContext *h, uint8_t *buf, int size)
158 {
159     TCPContext *s = h->priv_data;
160     int ret;
161
162     if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
163         ret = ff_network_wait_fd(s->fd, 0);
164         if (ret < 0)
165             return ret;
166     }
167     ret = recv(s->fd, buf, size, 0);
168     return ret < 0 ? ff_neterrno() : ret;
169 }
170
171 static int tcp_write(URLContext *h, const uint8_t *buf, int size)
172 {
173     TCPContext *s = h->priv_data;
174     int ret;
175
176     if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
177         ret = ff_network_wait_fd(s->fd, 1);
178         if (ret < 0)
179             return ret;
180     }
181     ret = send(s->fd, buf, size, 0);
182     return ret < 0 ? ff_neterrno() : ret;
183 }
184
185 static int tcp_close(URLContext *h)
186 {
187     TCPContext *s = h->priv_data;
188     closesocket(s->fd);
189     return 0;
190 }
191
192 static int tcp_get_file_handle(URLContext *h)
193 {
194     TCPContext *s = h->priv_data;
195     return s->fd;
196 }
197
198 URLProtocol ff_tcp_protocol = {
199     .name                = "tcp",
200     .url_open            = tcp_open,
201     .url_read            = tcp_read,
202     .url_write           = tcp_write,
203     .url_close           = tcp_close,
204     .url_get_file_handle = tcp_get_file_handle,
205     .priv_data_size      = sizeof(TCPContext),
206     .flags               = URL_PROTOCOL_FLAG_NETWORK,
207 };