Newer
Older
* Copyright (c) 2002 Fabrice Bellard
Diego Biurrun
committed
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
Diego Biurrun
committed
* version 2.1 of the License, or (at your option) any later version.
Diego Biurrun
committed
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
Diego Biurrun
committed
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "avformat.h"
#include <unistd.h>
#include "internal.h"
typedef struct TCPContext {
int fd;
} TCPContext;
/* return non zero if error */
static int tcp_open(URLContext *h, const char *uri, int flags)
{
struct addrinfo hints, *ai, *cur_ai;
TCPContext *s = NULL;
Ronald S. Bultje
committed
char hostname[1024],proto[1024],path[1024];
char portstr[10];
Petr Doubek
committed
av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
&port, path, sizeof(path), uri);
if (strcmp(proto,"tcp") || port <= 0 || port >= 65536)
Ronald S. Bultje
committed
return AVERROR(EINVAL);
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
snprintf(portstr, sizeof(portstr), "%d", port);
Ronald S. Bultje
committed
ret = getaddrinfo(hostname, portstr, &hints, &ai);
if (ret) {
av_log(NULL, AV_LOG_ERROR,
"Failed to resolve hostname %s: %s\n",
hostname, gai_strerror(ret));
Ronald S. Bultje
committed
return AVERROR(EIO);
Ronald S. Bultje
committed
}
cur_ai = ai;
restart:
fd = socket(cur_ai->ai_family, cur_ai->ai_socktype, cur_ai->ai_protocol);
goto fail;
ff_socket_nonblock(fd, 1);
ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
if (ff_neterrno() == FF_NETERROR(EINTR)) {
if (url_interrupt_cb())
goto fail1;
if (ff_neterrno() != FF_NETERROR(EINPROGRESS) &&
ff_neterrno() != FF_NETERROR(EAGAIN))
/* wait until we are connected or until abort */
for(;;) {
if (url_interrupt_cb()) {
ret = AVERROR(EINTR);
/* test error */
optlen = sizeof(ret);
getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
Ronald S. Bultje
committed
if (ret != 0) {
av_log(NULL, AV_LOG_ERROR,
"TCP connection to %s:%d failed: %s\n",
hostname, port, strerror(ret));
Ronald S. Bultje
committed
}
Ronald S. Bultje
committed
s = av_malloc(sizeof(TCPContext));
if (!s) {
freeaddrinfo(ai);
Ronald S. Bultje
committed
return AVERROR(ENOMEM);
Ronald S. Bultje
committed
h->priv_data = s;
h->is_streamed = 1;
freeaddrinfo(ai);
if (cur_ai->ai_next) {
/* Retry with the next sockaddr */
cur_ai = cur_ai->ai_next;
if (fd >= 0)
closesocket(fd);
goto restart;
}
ret = AVERROR(EIO);
freeaddrinfo(ai);
static int tcp_read(URLContext *h, uint8_t *buf, int size)
struct pollfd p = {s->fd, POLLIN, 0};
int len, ret;
Michael Niedermayer
committed
for (;;) {
return AVERROR(EINTR);
ret = poll(&p, 1, 100);
if (ret == 1 && p.revents & POLLIN) {
Leon van Stuivenberg
committed
len = recv(s->fd, buf, size, 0);
if (len < 0) {
if (ff_neterrno() != FF_NETERROR(EINTR) &&
ff_neterrno() != FF_NETERROR(EAGAIN))
Ronald S. Bultje
committed
return ff_neterrno();
Leon van Stuivenberg
committed
} else return len;
} else if (ret < 0) {
if (ff_neterrno() == FF_NETERROR(EINTR))
continue;
Leon van Stuivenberg
committed
return -1;
}
static int tcp_write(URLContext *h, const uint8_t *buf, int size)
int ret, size1, len;
struct pollfd p = {s->fd, POLLOUT, 0};
return AVERROR(EINTR);
ret = poll(&p, 1, 100);
if (ret == 1 && p.revents & POLLOUT) {
Leon van Stuivenberg
committed
len = send(s->fd, buf, size, 0);
if (len < 0) {
if (ff_neterrno() != FF_NETERROR(EINTR) &&
ff_neterrno() != FF_NETERROR(EAGAIN))
Ronald S. Bultje
committed
return ff_neterrno();
Leon van Stuivenberg
committed
continue;
}
Leon van Stuivenberg
committed
size -= len;
buf += len;
} else if (ret < 0) {
if (ff_neterrno() == FF_NETERROR(EINTR))
continue;
Leon van Stuivenberg
committed
return -1;
}
}
return size1 - size;
}
static int tcp_close(URLContext *h)
{
TCPContext *s = h->priv_data;
closesocket(s->fd);
static int tcp_get_file_handle(URLContext *h)
{
TCPContext *s = h->priv_data;
return s->fd;
}
Diego Elio Pettenò
committed
URLProtocol ff_tcp_protocol = {
"tcp",
tcp_open,
tcp_read,
tcp_write,
NULL, /* seek */
tcp_close,
.url_get_file_handle = tcp_get_file_handle,