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 = socket(cur_ai->ai_family, cur_ai->ai_socktype, cur_ai->ai_protocol);
94 if ((fd = ff_listen_bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
95 listen_timeout, h)) < 0) {
100 if ((ret = ff_listen_connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
101 timeout * 100, h)) < 0) {
103 if (ret == AVERROR_EXIT)
116 if (cur_ai->ai_next) {
117 /* Retry with the next sockaddr */
118 cur_ai = cur_ai->ai_next;
131 static int tcp_read(URLContext *h, uint8_t *buf, int size)
133 TCPContext *s = h->priv_data;
136 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
137 ret = ff_network_wait_fd(s->fd, 0);
141 ret = recv(s->fd, buf, size, 0);
142 return ret < 0 ? ff_neterrno() : ret;
145 static int tcp_write(URLContext *h, const uint8_t *buf, int size)
147 TCPContext *s = h->priv_data;
150 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
151 ret = ff_network_wait_fd(s->fd, 1);
155 ret = send(s->fd, buf, size, 0);
156 return ret < 0 ? ff_neterrno() : ret;
159 static int tcp_shutdown(URLContext *h, int flags)
161 TCPContext *s = h->priv_data;
164 if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
166 } else if (flags & AVIO_FLAG_WRITE) {
172 return shutdown(s->fd, how);
175 static int tcp_close(URLContext *h)
177 TCPContext *s = h->priv_data;
182 static int tcp_get_file_handle(URLContext *h)
184 TCPContext *s = h->priv_data;
188 URLProtocol ff_tcp_protocol = {
190 .url_open = tcp_open,
191 .url_read = tcp_read,
192 .url_write = tcp_write,
193 .url_close = tcp_close,
194 .url_get_file_handle = tcp_get_file_handle,
195 .url_shutdown = tcp_shutdown,
196 .priv_data_size = sizeof(TCPContext),
197 .flags = URL_PROTOCOL_FLAG_NETWORK,