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"
26 #include "os_support.h"
32 typedef struct TCPContext {
36 /* return non zero if error */
37 static int tcp_open(URLContext *h, const char *uri, int flags)
39 struct addrinfo hints = { 0 }, *ai, *cur_ai;
41 TCPContext *s = h->priv_data;
42 int listen_socket = 0;
48 char hostname[1024],proto[1024],path[1024];
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);
58 if (av_find_info_tag(buf, sizeof(buf), "listen", p))
60 if (av_find_info_tag(buf, sizeof(buf), "timeout", p)) {
61 timeout = strtol(buf, NULL, 10);
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);
69 av_log(h, AV_LOG_ERROR,
70 "Failed to resolve hostname %s: %s\n",
71 hostname, gai_strerror(ret));
79 fd = socket(cur_ai->ai_family, cur_ai->ai_socktype, cur_ai->ai_protocol);
86 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
87 ret = bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
97 fd1 = accept(fd, NULL, NULL);
104 ff_socket_nonblock(fd, 1);
107 ff_socket_nonblock(fd, 1);
108 ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
112 struct pollfd p = {fd, POLLOUT, 0};
114 if (ret == AVERROR(EINTR)) {
115 if (ff_check_interrupt(&h->interrupt_callback)) {
121 if (ret != AVERROR(EINPROGRESS) &&
122 ret != AVERROR(EAGAIN))
125 /* wait until we are connected or until abort */
127 if (ff_check_interrupt(&h->interrupt_callback)) {
131 ret = poll(&p, 1, 100);
136 ret = AVERROR(ETIMEDOUT);
140 optlen = sizeof(ret);
141 if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen))
142 ret = AVUNERROR(ff_neterrno());
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);
159 if (cur_ai->ai_next) {
160 /* Retry with the next sockaddr */
161 cur_ai = cur_ai->ai_next;
173 static int tcp_read(URLContext *h, uint8_t *buf, int size)
175 TCPContext *s = h->priv_data;
178 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
179 ret = ff_network_wait_fd(s->fd, 0);
183 ret = recv(s->fd, buf, size, 0);
184 return ret < 0 ? ff_neterrno() : ret;
187 static int tcp_write(URLContext *h, const uint8_t *buf, int size)
189 TCPContext *s = h->priv_data;
192 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
193 ret = ff_network_wait_fd(s->fd, 1);
197 ret = send(s->fd, buf, size, 0);
198 return ret < 0 ? ff_neterrno() : ret;
201 static int tcp_shutdown(URLContext *h, int flags)
203 TCPContext *s = h->priv_data;
206 if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
208 } else if (flags & AVIO_FLAG_WRITE) {
214 return shutdown(s->fd, how);
217 static int tcp_close(URLContext *h)
219 TCPContext *s = h->priv_data;
224 static int tcp_get_file_handle(URLContext *h)
226 TCPContext *s = h->priv_data;
230 URLProtocol ff_tcp_protocol = {
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,