3 * Copyright (c) 2002 Fabrice Bellard
5 * This file is part of Libav.
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.
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.
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
22 #include "libavutil/parseutils.h"
26 #include "os_support.h"
33 typedef struct TCPContext {
37 /* return non zero if error */
38 static int tcp_open(URLContext *h, const char *uri, int flags)
40 struct addrinfo hints, *ai, *cur_ai;
43 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))
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);
67 av_log(h, AV_LOG_ERROR,
68 "Failed to resolve hostname %s: %s\n",
69 hostname, gai_strerror(ret));
76 fd = socket(cur_ai->ai_family, cur_ai->ai_socktype, cur_ai->ai_protocol);
82 ret = bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
84 fd1 = accept(fd, NULL, NULL);
89 ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
92 ff_socket_nonblock(fd, 1);
95 struct pollfd p = {fd, POLLOUT, 0};
96 if (ff_neterrno() == AVERROR(EINTR)) {
97 if (url_interrupt_cb()) {
103 if (ff_neterrno() != AVERROR(EINPROGRESS) &&
104 ff_neterrno() != AVERROR(EAGAIN))
107 /* wait until we are connected or until abort */
109 if (url_interrupt_cb()) {
113 ret = poll(&p, 1, 100);
119 optlen = sizeof(ret);
120 getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
122 av_log(h, AV_LOG_ERROR,
123 "TCP connection to %s:%d failed: %s\n",
124 hostname, port, strerror(ret));
128 s = av_malloc(sizeof(TCPContext));
131 return AVERROR(ENOMEM);
140 if (cur_ai->ai_next) {
141 /* Retry with the next sockaddr */
142 cur_ai = cur_ai->ai_next;
155 static int tcp_read(URLContext *h, uint8_t *buf, int size)
157 TCPContext *s = h->priv_data;
160 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
161 ret = ff_network_wait_fd(s->fd, 0);
165 ret = recv(s->fd, buf, size, 0);
166 return ret < 0 ? ff_neterrno() : ret;
169 static int tcp_write(URLContext *h, const uint8_t *buf, int size)
171 TCPContext *s = h->priv_data;
174 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
175 ret = ff_network_wait_fd(s->fd, 1);
179 ret = send(s->fd, buf, size, 0);
180 return ret < 0 ? ff_neterrno() : ret;
183 static int tcp_close(URLContext *h)
185 TCPContext *s = h->priv_data;
191 static int tcp_get_file_handle(URLContext *h)
193 TCPContext *s = h->priv_data;
197 URLProtocol ff_tcp_protocol = {
199 .url_open = tcp_open,
200 .url_read = tcp_read,
201 .url_write = tcp_write,
202 .url_close = tcp_close,
203 .url_get_file_handle = tcp_get_file_handle,