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
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, *ai, *cur_ai;
43 char hostname[1024],proto[1024],path[1024];
46 av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
47 &port, path, sizeof(path), uri);
48 if (strcmp(proto,"tcp") || port <= 0 || port >= 65536)
49 return AVERROR(EINVAL);
51 memset(&hints, 0, sizeof(hints));
52 hints.ai_family = AF_UNSPEC;
53 hints.ai_socktype = SOCK_STREAM;
54 snprintf(portstr, sizeof(portstr), "%d", port);
55 ret = getaddrinfo(hostname, portstr, &hints, &ai);
57 av_log(NULL, AV_LOG_ERROR,
58 "Failed to resolve hostname %s: %s\n",
59 hostname, gai_strerror(ret));
66 fd = socket(cur_ai->ai_family, cur_ai->ai_socktype, cur_ai->ai_protocol);
69 ff_socket_nonblock(fd, 1);
72 ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
74 struct pollfd p = {fd, POLLOUT, 0};
75 if (ff_neterrno() == AVERROR(EINTR)) {
76 if (url_interrupt_cb()) {
82 if (ff_neterrno() != AVERROR(EINPROGRESS) &&
83 ff_neterrno() != AVERROR(EAGAIN))
86 /* wait until we are connected or until abort */
88 if (url_interrupt_cb()) {
92 ret = poll(&p, 1, 100);
99 getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
101 av_log(NULL, AV_LOG_ERROR,
102 "TCP connection to %s:%d failed: %s\n",
103 hostname, port, strerror(ret));
107 s = av_malloc(sizeof(TCPContext));
110 return AVERROR(ENOMEM);
119 if (cur_ai->ai_next) {
120 /* Retry with the next sockaddr */
121 cur_ai = cur_ai->ai_next;
134 static int tcp_wait_fd(int fd, int write)
136 int ev = write ? POLLOUT : POLLIN;
137 struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
140 ret = poll(&p, 1, 100);
141 return ret < 0 ? ff_neterrno() : p.revents & ev ? 0 : AVERROR(EAGAIN);
144 static int tcp_read(URLContext *h, uint8_t *buf, int size)
146 TCPContext *s = h->priv_data;
149 if (!(h->flags & URL_FLAG_NONBLOCK)) {
150 ret = tcp_wait_fd(s->fd, 0);
154 ret = recv(s->fd, buf, size, 0);
155 return ret < 0 ? ff_neterrno() : ret;
158 static int tcp_write(URLContext *h, const uint8_t *buf, int size)
160 TCPContext *s = h->priv_data;
163 if (!(h->flags & URL_FLAG_NONBLOCK)) {
164 ret = tcp_wait_fd(s->fd, 1);
168 ret = send(s->fd, buf, size, 0);
169 return ret < 0 ? ff_neterrno() : ret;
172 static int tcp_close(URLContext *h)
174 TCPContext *s = h->priv_data;
180 static int tcp_get_file_handle(URLContext *h)
182 TCPContext *s = h->priv_data;
186 URLProtocol ff_tcp_protocol = {
193 .url_get_file_handle = tcp_get_file_handle,