tcp.c 6.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
 */
#include "avformat.h"
22
#include "libavutil/parseutils.h"
23
#include "internal.h"
24
#include "network.h"
25
#include "os_support.h"
26
#include "url.h"
27 28
#if HAVE_POLL_H
#include <poll.h>
29
#endif
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 = { 0 }, *ai, *cur_ai;
Fabrice Bellard's avatar
Fabrice Bellard committed
39
    int port, fd = -1;
40
    TCPContext *s = h->priv_data;
41 42 43
    int listen_socket = 0;
    const char *p;
    char buf[256];
44
    int ret;
45
    socklen_t optlen;
46
    int timeout = 50, listen_timeout = -1;
47
    char hostname[1024],proto[1024],path[1024];
48
    char portstr[10];
49

50
    av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
51
        &port, path, sizeof(path), uri);
52
    if (strcmp(proto, "tcp"))
53
        return AVERROR(EINVAL);
54 55 56 57
    if (port <= 0 || port >= 65536) {
        av_log(h, AV_LOG_ERROR, "Port missing in uri\n");
        return AVERROR(EINVAL);
    }
58 59 60 61
    p = strchr(uri, '?');
    if (p) {
        if (av_find_info_tag(buf, sizeof(buf), "listen", p))
            listen_socket = 1;
62 63 64
        if (av_find_info_tag(buf, sizeof(buf), "timeout", p)) {
            timeout = strtol(buf, NULL, 10);
        }
65 66 67
        if (av_find_info_tag(buf, sizeof(buf), "listen_timeout", p)) {
            listen_timeout = strtol(buf, NULL, 10);
        }
68
    }
69
    hints.ai_family = AF_UNSPEC;
70 71
    hints.ai_socktype = SOCK_STREAM;
    snprintf(portstr, sizeof(portstr), "%d", port);
72 73
    if (listen_socket)
        hints.ai_flags |= AI_PASSIVE;
74 75 76 77
    if (!hostname[0])
        ret = getaddrinfo(NULL, portstr, &hints, &ai);
    else
        ret = getaddrinfo(hostname, portstr, &hints, &ai);
78
    if (ret) {
79
        av_log(h, AV_LOG_ERROR,
80 81
               "Failed to resolve hostname %s: %s\n",
               hostname, gai_strerror(ret));
82
        return AVERROR(EIO);
83
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
84

85 86 87
    cur_ai = ai;

 restart:
88
    ret = AVERROR(EIO);
89
    fd = socket(cur_ai->ai_family, cur_ai->ai_socktype, cur_ai->ai_protocol);
Fabrice Bellard's avatar
Fabrice Bellard committed
90
    if (fd < 0)
91
        goto fail;
92

93 94
    if (listen_socket) {
        int fd1;
95
        int reuse = 1;
96
        struct pollfd lp = { fd, POLLIN, 0 };
97
        setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
98
        ret = bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
99 100 101 102
        if (ret) {
            ret = ff_neterrno();
            goto fail1;
        }
Jordi Ortiz's avatar
Jordi Ortiz committed
103 104 105 106 107
        ret = listen(fd, 1);
        if (ret) {
            ret = ff_neterrno();
            goto fail1;
        }
108 109 110 111 112
        ret = poll(&lp, 1, listen_timeout >= 0 ? listen_timeout : -1);
        if (ret <= 0) {
            ret = AVERROR(ETIMEDOUT);
            goto fail1;
        }
113
        fd1 = accept(fd, NULL, NULL);
114 115 116 117
        if (fd1 < 0) {
            ret = ff_neterrno();
            goto fail1;
        }
118 119
        closesocket(fd);
        fd = fd1;
120
        ff_socket_nonblock(fd, 1);
121
    } else {
122
 redo:
123
        ff_socket_nonblock(fd, 1);
124 125 126
        ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
    }

127
    if (ret < 0) {
128
        struct pollfd p = {fd, POLLOUT, 0};
129 130
        ret = ff_neterrno();
        if (ret == AVERROR(EINTR)) {
131
            if (ff_check_interrupt(&h->interrupt_callback)) {
132
                ret = AVERROR_EXIT;
133
                goto fail1;
134
            }
135
            goto redo;
136
        }
137 138
        if (ret != AVERROR(EINPROGRESS) &&
            ret != AVERROR(EAGAIN))
139
            goto fail;
Fabrice Bellard's avatar
Fabrice Bellard committed
140

141
        /* wait until we are connected or until abort */
142
        while(timeout--) {
143
            if (ff_check_interrupt(&h->interrupt_callback)) {
144
                ret = AVERROR_EXIT;
145 146
                goto fail1;
            }
147 148
            ret = poll(&p, 1, 100);
            if (ret > 0)
149 150
                break;
        }
151 152 153 154
        if (ret <= 0) {
            ret = AVERROR(ETIMEDOUT);
            goto fail;
        }
155 156
        /* test error */
        optlen = sizeof(ret);
157 158
        if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen))
            ret = AVUNERROR(ff_neterrno());
159
        if (ret != 0) {
160 161 162
            char errbuf[100];
            ret = AVERROR(ret);
            av_strerror(ret, errbuf, sizeof(errbuf));
163
            av_log(h, AV_LOG_ERROR,
164
                   "TCP connection to %s:%d failed: %s\n",
165
                   hostname, port, errbuf);
166
            goto fail;
167
        }
168
    }
169
    h->is_streamed = 1;
Fabrice Bellard's avatar
Fabrice Bellard committed
170
    s->fd = fd;
171
    freeaddrinfo(ai);
Fabrice Bellard's avatar
Fabrice Bellard committed
172 173 174
    return 0;

 fail:
175 176 177 178 179 180 181
    if (cur_ai->ai_next) {
        /* Retry with the next sockaddr */
        cur_ai = cur_ai->ai_next;
        if (fd >= 0)
            closesocket(fd);
        goto restart;
    }
182
 fail1:
Fabrice Bellard's avatar
Fabrice Bellard committed
183
    if (fd >= 0)
184
        closesocket(fd);
185
    freeaddrinfo(ai);
186
    return ret;
Fabrice Bellard's avatar
Fabrice Bellard committed
187 188
}

189
static int tcp_read(URLContext *h, uint8_t *buf, int size)
Fabrice Bellard's avatar
Fabrice Bellard committed
190 191
{
    TCPContext *s = h->priv_data;
192
    int ret;
Fabrice Bellard's avatar
Fabrice Bellard committed
193

194
    if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
195
        ret = ff_network_wait_fd(s->fd, 0);
196
        if (ret < 0)
197
            return ret;
Fabrice Bellard's avatar
Fabrice Bellard committed
198
    }
199 200
    ret = recv(s->fd, buf, size, 0);
    return ret < 0 ? ff_neterrno() : ret;
Fabrice Bellard's avatar
Fabrice Bellard committed
201 202
}

203
static int tcp_write(URLContext *h, const uint8_t *buf, int size)
Fabrice Bellard's avatar
Fabrice Bellard committed
204 205
{
    TCPContext *s = h->priv_data;
206
    int ret;
Fabrice Bellard's avatar
Fabrice Bellard committed
207

208
    if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
209
        ret = ff_network_wait_fd(s->fd, 1);
210
        if (ret < 0)
211
            return ret;
Fabrice Bellard's avatar
Fabrice Bellard committed
212
    }
213 214
    ret = send(s->fd, buf, size, 0);
    return ret < 0 ? ff_neterrno() : ret;
Fabrice Bellard's avatar
Fabrice Bellard committed
215 216
}

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
static int tcp_shutdown(URLContext *h, int flags)
{
    TCPContext *s = h->priv_data;
    int how;

    if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
        how = SHUT_RDWR;
    } else if (flags & AVIO_FLAG_WRITE) {
        how = SHUT_WR;
    } else {
        how = SHUT_RD;
    }

    return shutdown(s->fd, how);
}

Fabrice Bellard's avatar
Fabrice Bellard committed
233 234 235
static int tcp_close(URLContext *h)
{
    TCPContext *s = h->priv_data;
236
    closesocket(s->fd);
Fabrice Bellard's avatar
Fabrice Bellard committed
237 238 239
    return 0;
}

240 241 242 243 244 245
static int tcp_get_file_handle(URLContext *h)
{
    TCPContext *s = h->priv_data;
    return s->fd;
}

246
URLProtocol ff_tcp_protocol = {
247 248 249 250 251
    .name                = "tcp",
    .url_open            = tcp_open,
    .url_read            = tcp_read,
    .url_write           = tcp_write,
    .url_close           = tcp_close,
252
    .url_get_file_handle = tcp_get_file_handle,
253
    .url_shutdown        = tcp_shutdown,
254
    .priv_data_size      = sizeof(TCPContext),
255
    .flags               = URL_PROTOCOL_FLAG_NETWORK,
Fabrice Bellard's avatar
Fabrice Bellard committed
256
};