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"
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;
45 int timeout = 100, listen_timeout = -1;
46 char hostname[1024],proto[1024],path[1024];
49 av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
50 &port, path, sizeof(path), uri);
51 if (strcmp(proto, "tcp"))
52 return AVERROR(EINVAL);
53 if (port <= 0 || port >= 65536) {
54 av_log(h, AV_LOG_ERROR, "Port missing in uri\n");
55 return AVERROR(EINVAL);
59 if (av_find_info_tag(buf, sizeof(buf), "listen", p))
61 if (av_find_info_tag(buf, sizeof(buf), "timeout", p)) {
62 timeout = strtol(buf, NULL, 10);
64 if (av_find_info_tag(buf, sizeof(buf), "listen_timeout", p)) {
65 listen_timeout = strtol(buf, NULL, 10);
68 hints.ai_family = AF_UNSPEC;
69 hints.ai_socktype = SOCK_STREAM;
70 snprintf(portstr, sizeof(portstr), "%d", port);
72 hints.ai_flags |= AI_PASSIVE;
74 ret = getaddrinfo(NULL, portstr, &hints, &ai);
76 ret = getaddrinfo(hostname, portstr, &hints, &ai);
78 av_log(h, AV_LOG_ERROR,
79 "Failed to resolve hostname %s: %s\n",
80 hostname, gai_strerror(ret));
87 fd = ff_socket(cur_ai->ai_family,
96 if ((fd = ff_listen_bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
97 listen_timeout, h)) < 0) {
102 if ((ret = ff_listen_connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
103 timeout * 100, h, cur_ai->ai_next)) < 0) {
105 if (ret == AVERROR_EXIT)
118 if (cur_ai->ai_next) {
119 /* Retry with the next sockaddr */
120 cur_ai = cur_ai->ai_next;
133 static int tcp_read(URLContext *h, uint8_t *buf, int size)
135 TCPContext *s = h->priv_data;
138 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
139 ret = ff_network_wait_fd(s->fd, 0);
143 ret = recv(s->fd, buf, size, 0);
144 return ret < 0 ? ff_neterrno() : ret;
147 static int tcp_write(URLContext *h, const uint8_t *buf, int size)
149 TCPContext *s = h->priv_data;
152 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
153 ret = ff_network_wait_fd(s->fd, 1);
157 ret = send(s->fd, buf, size, 0);
158 return ret < 0 ? ff_neterrno() : ret;
161 static int tcp_shutdown(URLContext *h, int flags)
163 TCPContext *s = h->priv_data;
166 if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
168 } else if (flags & AVIO_FLAG_WRITE) {
174 return shutdown(s->fd, how);
177 static int tcp_close(URLContext *h)
179 TCPContext *s = h->priv_data;
184 static int tcp_get_file_handle(URLContext *h)
186 TCPContext *s = h->priv_data;
190 URLProtocol ff_tcp_protocol = {
192 .url_open = tcp_open,
193 .url_read = tcp_read,
194 .url_write = tcp_write,
195 .url_close = tcp_close,
196 .url_get_file_handle = tcp_get_file_handle,
197 .url_shutdown = tcp_shutdown,
198 .priv_data_size = sizeof(TCPContext),
199 .flags = URL_PROTOCOL_FLAG_NETWORK,