tcp.c 4.91 KB
Newer Older
Fabrice Bellard's avatar
Fabrice Bellard committed
1 2
/*
 * TCP protocol
3
 * Copyright (c) 2002 Fabrice Bellard
Fabrice Bellard's avatar
Fabrice Bellard committed
4
 *
5 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
Fabrice Bellard's avatar
Fabrice Bellard committed
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
Fabrice Bellard's avatar
Fabrice Bellard committed
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
Fabrice Bellard's avatar
Fabrice Bellard committed
13 14 15 16 17
 * 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
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
Fabrice Bellard's avatar
Fabrice Bellard committed
20 21 22
 */
#include "avformat.h"
#include <unistd.h>
23
#include "internal.h"
24
#include "network.h"
25
#include "os_support.h"
Luca Barbato's avatar
Luca Barbato committed
26 27
#if HAVE_POLL_H
#include <poll.h>
28
#endif
Fabrice Bellard's avatar
Fabrice Bellard committed
29
#include <sys/time.h>
Fabrice Bellard's avatar
Fabrice Bellard committed
30 31 32 33 34 35 36 37

typedef struct TCPContext {
    int fd;
} TCPContext;

/* return non zero if error */
static int tcp_open(URLContext *h, const char *uri, int flags)
{
38
    struct addrinfo hints, *ai, *cur_ai;
Fabrice Bellard's avatar
Fabrice Bellard committed
39
    int port, fd = -1;
40
    TCPContext *s = NULL;
Luca Barbato's avatar
Luca Barbato committed
41
    int ret;
Fabrice Bellard's avatar
Fabrice Bellard committed
42
    socklen_t optlen;
43
    char hostname[1024],proto[1024],path[1024];
44
    char portstr[10];
45

Måns Rullgård's avatar
Måns Rullgård committed
46
    av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
47 48
        &port, path, sizeof(path), uri);
    if (strcmp(proto,"tcp") || port <= 0 || port >= 65536)
49
        return AVERROR(EINVAL);
50

51
    memset(&hints, 0, sizeof(hints));
52
    hints.ai_family = AF_UNSPEC;
53 54
    hints.ai_socktype = SOCK_STREAM;
    snprintf(portstr, sizeof(portstr), "%d", port);
55 56 57 58 59
    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));
60
        return AVERROR(EIO);
61
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
62

63 64 65 66
    cur_ai = ai;

 restart:
    fd = socket(cur_ai->ai_family, cur_ai->ai_socktype, cur_ai->ai_protocol);
Fabrice Bellard's avatar
Fabrice Bellard committed
67
    if (fd < 0)
68
        goto fail;
69
    ff_socket_nonblock(fd, 1);
70

Fabrice Bellard's avatar
Fabrice Bellard committed
71
 redo:
72
    ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
Fabrice Bellard's avatar
Fabrice Bellard committed
73
    if (ret < 0) {
Luca Barbato's avatar
Luca Barbato committed
74
        struct pollfd p = {fd, POLLOUT, 0};
75
        if (ff_neterrno() == AVERROR(EINTR)) {
76 77
            if (url_interrupt_cb()) {
                ret = AVERROR_EXIT;
78
                goto fail1;
79
            }
Fabrice Bellard's avatar
Fabrice Bellard committed
80
            goto redo;
81
        }
82 83
        if (ff_neterrno() != AVERROR(EINPROGRESS) &&
            ff_neterrno() != AVERROR(EAGAIN))
Fabrice Bellard's avatar
Fabrice Bellard committed
84
            goto fail;
Fabrice Bellard's avatar
Fabrice Bellard committed
85

Fabrice Bellard's avatar
Fabrice Bellard committed
86 87 88
        /* wait until we are connected or until abort */
        for(;;) {
            if (url_interrupt_cb()) {
89
                ret = AVERROR_EXIT;
Fabrice Bellard's avatar
Fabrice Bellard committed
90 91
                goto fail1;
            }
Luca Barbato's avatar
Luca Barbato committed
92 93
            ret = poll(&p, 1, 100);
            if (ret > 0)
Fabrice Bellard's avatar
Fabrice Bellard committed
94 95
                break;
        }
96

Fabrice Bellard's avatar
Fabrice Bellard committed
97 98 99
        /* test error */
        optlen = sizeof(ret);
        getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
100 101 102 103
        if (ret != 0) {
            av_log(NULL, AV_LOG_ERROR,
                   "TCP connection to %s:%d failed: %s\n",
                   hostname, port, strerror(ret));
Fabrice Bellard's avatar
Fabrice Bellard committed
104
            goto fail;
105
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
106
    }
107
    s = av_malloc(sizeof(TCPContext));
108 109
    if (!s) {
        freeaddrinfo(ai);
110
        return AVERROR(ENOMEM);
111
    }
112 113
    h->priv_data = s;
    h->is_streamed = 1;
Fabrice Bellard's avatar
Fabrice Bellard committed
114
    s->fd = fd;
115
    freeaddrinfo(ai);
Fabrice Bellard's avatar
Fabrice Bellard committed
116 117 118
    return 0;

 fail:
119 120 121 122 123 124 125
    if (cur_ai->ai_next) {
        /* Retry with the next sockaddr */
        cur_ai = cur_ai->ai_next;
        if (fd >= 0)
            closesocket(fd);
        goto restart;
    }
126
    ret = AVERROR(EIO);
Fabrice Bellard's avatar
Fabrice Bellard committed
127
 fail1:
Fabrice Bellard's avatar
Fabrice Bellard committed
128
    if (fd >= 0)
129
        closesocket(fd);
130
    freeaddrinfo(ai);
Fabrice Bellard's avatar
Fabrice Bellard committed
131
    return ret;
Fabrice Bellard's avatar
Fabrice Bellard committed
132 133
}

Nicolas George's avatar
Nicolas George committed
134 135 136 137 138 139 140
static int tcp_wait_fd(int fd, int write)
{
    int ev = write ? POLLOUT : POLLIN;
    struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
    int ret;

    ret = poll(&p, 1, 100);
141
    return ret < 0 ? ff_neterrno() : p.revents & ev ? 0 : AVERROR(EAGAIN);
Nicolas George's avatar
Nicolas George committed
142 143
}

144
static int tcp_read(URLContext *h, uint8_t *buf, int size)
Fabrice Bellard's avatar
Fabrice Bellard committed
145 146
{
    TCPContext *s = h->priv_data;
Nicolas George's avatar
Nicolas George committed
147
    int ret;
Fabrice Bellard's avatar
Fabrice Bellard committed
148

Nicolas George's avatar
Nicolas George committed
149
    if (!(h->flags & URL_FLAG_NONBLOCK)) {
Nicolas George's avatar
Nicolas George committed
150
        ret = tcp_wait_fd(s->fd, 0);
Nicolas George's avatar
Nicolas George committed
151
        if (ret < 0)
Nicolas George's avatar
Nicolas George committed
152
            return ret;
Fabrice Bellard's avatar
Fabrice Bellard committed
153
    }
Nicolas George's avatar
Nicolas George committed
154 155
    ret = recv(s->fd, buf, size, 0);
    return ret < 0 ? ff_neterrno() : ret;
Fabrice Bellard's avatar
Fabrice Bellard committed
156 157
}

158
static int tcp_write(URLContext *h, const uint8_t *buf, int size)
Fabrice Bellard's avatar
Fabrice Bellard committed
159 160
{
    TCPContext *s = h->priv_data;
Nicolas George's avatar
Nicolas George committed
161
    int ret;
Fabrice Bellard's avatar
Fabrice Bellard committed
162

Nicolas George's avatar
Nicolas George committed
163
    if (!(h->flags & URL_FLAG_NONBLOCK)) {
Nicolas George's avatar
Nicolas George committed
164
        ret = tcp_wait_fd(s->fd, 1);
Nicolas George's avatar
Nicolas George committed
165
        if (ret < 0)
Nicolas George's avatar
Nicolas George committed
166
            return ret;
Fabrice Bellard's avatar
Fabrice Bellard committed
167
    }
Nicolas George's avatar
Nicolas George committed
168 169
    ret = send(s->fd, buf, size, 0);
    return ret < 0 ? ff_neterrno() : ret;
Fabrice Bellard's avatar
Fabrice Bellard committed
170 171 172 173 174
}

static int tcp_close(URLContext *h)
{
    TCPContext *s = h->priv_data;
175
    closesocket(s->fd);
Fabrice Bellard's avatar
Fabrice Bellard committed
176 177 178 179
    av_free(s);
    return 0;
}

180 181 182 183 184 185
static int tcp_get_file_handle(URLContext *h)
{
    TCPContext *s = h->priv_data;
    return s->fd;
}

186
URLProtocol ff_tcp_protocol = {
Fabrice Bellard's avatar
Fabrice Bellard committed
187 188 189 190 191 192
    "tcp",
    tcp_open,
    tcp_read,
    tcp_write,
    NULL, /* seek */
    tcp_close,
193
    .url_get_file_handle = tcp_get_file_handle,
Fabrice Bellard's avatar
Fabrice Bellard committed
194
};