3 * Copyright (c) 2002 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/parseutils.h"
25 #include "os_support.h"
31 typedef struct TCPContext {
35 /* return non zero if error */
36 static int tcp_open(URLContext *h, const char *uri, int flags)
38 struct addrinfo hints = { 0 }, *ai, *cur_ai;
40 TCPContext *s = h->priv_data;
41 int listen_socket = 0;
46 int timeout = 50, listen_timeout = -1;
47 char hostname[1024],proto[1024],path[1024];
50 av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
51 &port, path, sizeof(path), uri);
52 if (strcmp(proto, "tcp"))
53 return AVERROR(EINVAL);
54 if (port <= 0 || port >= 65536) {
55 av_log(h, AV_LOG_ERROR, "Port missing in uri\n");
56 return AVERROR(EINVAL);
60 if (av_find_info_tag(buf, sizeof(buf), "listen", p))
62 if (av_find_info_tag(buf, sizeof(buf), "timeout", p)) {
63 timeout = strtol(buf, NULL, 10);
65 if (av_find_info_tag(buf, sizeof(buf), "listen_timeout", p)) {
66 listen_timeout = strtol(buf, NULL, 10);
69 hints.ai_family = AF_UNSPEC;
70 hints.ai_socktype = SOCK_STREAM;
71 snprintf(portstr, sizeof(portstr), "%d", port);
73 hints.ai_flags |= AI_PASSIVE;
75 ret = getaddrinfo(NULL, portstr, &hints, &ai);
77 ret = getaddrinfo(hostname, portstr, &hints, &ai);
79 av_log(h, AV_LOG_ERROR,
80 "Failed to resolve hostname %s: %s\n",
81 hostname, gai_strerror(ret));
89 fd = socket(cur_ai->ai_family, cur_ai->ai_socktype, cur_ai->ai_protocol);
96 struct pollfd lp = { fd, POLLIN, 0 };
97 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
98 ret = bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
108 ret = poll(&lp, 1, listen_timeout >= 0 ? listen_timeout : -1);
110 ret = AVERROR(ETIMEDOUT);
113 fd1 = accept(fd, NULL, NULL);
120 ff_socket_nonblock(fd, 1);
123 ff_socket_nonblock(fd, 1);
124 ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
128 struct pollfd p = {fd, POLLOUT, 0};
130 if (ret == AVERROR(EINTR)) {
131 if (ff_check_interrupt(&h->interrupt_callback)) {
137 if (ret != AVERROR(EINPROGRESS) &&
138 ret != AVERROR(EAGAIN))
141 /* wait until we are connected or until abort */
143 if (ff_check_interrupt(&h->interrupt_callback)) {
147 ret = poll(&p, 1, 100);
152 ret = AVERROR(ETIMEDOUT);
156 optlen = sizeof(ret);
157 if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen))
158 ret = AVUNERROR(ff_neterrno());
162 av_strerror(ret, errbuf, sizeof(errbuf));
163 av_log(h, AV_LOG_ERROR,
164 "TCP connection to %s:%d failed: %s\n",
165 hostname, port, errbuf);
175 if (cur_ai->ai_next) {
176 /* Retry with the next sockaddr */
177 cur_ai = cur_ai->ai_next;
189 static int tcp_read(URLContext *h, uint8_t *buf, int size)
191 TCPContext *s = h->priv_data;
194 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
195 ret = ff_network_wait_fd(s->fd, 0);
199 ret = recv(s->fd, buf, size, 0);
200 return ret < 0 ? ff_neterrno() : ret;
203 static int tcp_write(URLContext *h, const uint8_t *buf, int size)
205 TCPContext *s = h->priv_data;
208 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
209 ret = ff_network_wait_fd(s->fd, 1);
213 ret = send(s->fd, buf, size, 0);
214 return ret < 0 ? ff_neterrno() : ret;
217 static int tcp_shutdown(URLContext *h, int flags)
219 TCPContext *s = h->priv_data;
222 if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
224 } else if (flags & AVIO_FLAG_WRITE) {
230 return shutdown(s->fd, how);
233 static int tcp_close(URLContext *h)
235 TCPContext *s = h->priv_data;
240 static int tcp_get_file_handle(URLContext *h)
242 TCPContext *s = h->priv_data;
246 URLProtocol ff_tcp_protocol = {
248 .url_open = tcp_open,
249 .url_read = tcp_read,
250 .url_write = tcp_write,
251 .url_close = tcp_close,
252 .url_get_file_handle = tcp_get_file_handle,
253 .url_shutdown = tcp_shutdown,
254 .priv_data_size = sizeof(TCPContext),
255 .flags = URL_PROTOCOL_FLAG_NETWORK,