tcp.c 6 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"
26
#if HAVE_SYS_SELECT_H
27
#include <sys/select.h>
28
#endif
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;
41 42 43 44
    fd_set wfds;
    int fd_max, ret;
    struct timeval tv;
    socklen_t optlen;
45
    char hostname[1024],proto[1024],path[1024];
46
    char portstr[10];
47

48
    av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
49 50
        &port, path, sizeof(path), uri);
    if (strcmp(proto,"tcp") || port <= 0 || port >= 65536)
51
        return AVERROR(EINVAL);
52

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

65 66 67 68
    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
69
    if (fd < 0)
70
        goto fail;
71
    ff_socket_nonblock(fd, 1);
72

73
 redo:
74
    ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
75
    if (ret < 0) {
76
        if (ff_neterrno() == FF_NETERROR(EINTR))
77
            goto redo;
78 79
        if (ff_neterrno() != FF_NETERROR(EINPROGRESS) &&
            ff_neterrno() != FF_NETERROR(EAGAIN))
80
            goto fail;
Fabrice Bellard's avatar
Fabrice Bellard committed
81

82 83 84
        /* wait until we are connected or until abort */
        for(;;) {
            if (url_interrupt_cb()) {
85
                ret = AVERROR(EINTR);
86 87 88 89 90 91 92 93 94 95 96
                goto fail1;
            }
            fd_max = fd;
            FD_ZERO(&wfds);
            FD_SET(fd, &wfds);
            tv.tv_sec = 0;
            tv.tv_usec = 100 * 1000;
            ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
            if (ret > 0 && FD_ISSET(fd, &wfds))
                break;
        }
97

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

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

135
static int tcp_read(URLContext *h, uint8_t *buf, int size)
Fabrice Bellard's avatar
Fabrice Bellard committed
136 137
{
    TCPContext *s = h->priv_data;
138
    int len, fd_max, ret;
139 140
    fd_set rfds;
    struct timeval tv;
Fabrice Bellard's avatar
Fabrice Bellard committed
141

142
    for (;;) {
143
        if (url_interrupt_cb())
144
            return AVERROR(EINTR);
145 146 147 148 149
        fd_max = s->fd;
        FD_ZERO(&rfds);
        FD_SET(s->fd, &rfds);
        tv.tv_sec = 0;
        tv.tv_usec = 100 * 1000;
150 151 152 153
        ret = select(fd_max + 1, &rfds, NULL, NULL, &tv);
        if (ret > 0 && FD_ISSET(s->fd, &rfds)) {
            len = recv(s->fd, buf, size, 0);
            if (len < 0) {
154 155
                if (ff_neterrno() != FF_NETERROR(EINTR) &&
                    ff_neterrno() != FF_NETERROR(EAGAIN))
156
                    return AVERROR(ff_neterrno());
157 158
            } else return len;
        } else if (ret < 0) {
159 160
            if (ff_neterrno() == FF_NETERROR(EINTR))
                continue;
161 162
            return -1;
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
163 164 165
    }
}

166
static int tcp_write(URLContext *h, const uint8_t *buf, int size)
Fabrice Bellard's avatar
Fabrice Bellard committed
167 168
{
    TCPContext *s = h->priv_data;
169
    int ret, size1, fd_max, len;
170 171
    fd_set wfds;
    struct timeval tv;
Fabrice Bellard's avatar
Fabrice Bellard committed
172 173 174

    size1 = size;
    while (size > 0) {
175
        if (url_interrupt_cb())
176
            return AVERROR(EINTR);
177 178 179 180 181
        fd_max = s->fd;
        FD_ZERO(&wfds);
        FD_SET(s->fd, &wfds);
        tv.tv_sec = 0;
        tv.tv_usec = 100 * 1000;
182 183 184 185
        ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
        if (ret > 0 && FD_ISSET(s->fd, &wfds)) {
            len = send(s->fd, buf, size, 0);
            if (len < 0) {
186 187
                if (ff_neterrno() != FF_NETERROR(EINTR) &&
                    ff_neterrno() != FF_NETERROR(EAGAIN))
188
                    return AVERROR(ff_neterrno());
189
                continue;
190
            }
191 192 193
            size -= len;
            buf += len;
        } else if (ret < 0) {
194 195
            if (ff_neterrno() == FF_NETERROR(EINTR))
                continue;
196
            return -1;
197
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
198 199 200 201 202 203 204
    }
    return size1 - size;
}

static int tcp_close(URLContext *h)
{
    TCPContext *s = h->priv_data;
205
    closesocket(s->fd);
Fabrice Bellard's avatar
Fabrice Bellard committed
206 207 208 209
    av_free(s);
    return 0;
}

210 211 212 213 214 215
static int tcp_get_file_handle(URLContext *h)
{
    TCPContext *s = h->priv_data;
    return s->fd;
}

Fabrice Bellard's avatar
Fabrice Bellard committed
216 217 218 219 220 221 222
URLProtocol tcp_protocol = {
    "tcp",
    tcp_open,
    tcp_read,
    tcp_write,
    NULL, /* seek */
    tcp_close,
223
    .url_get_file_handle = tcp_get_file_handle,
Fabrice Bellard's avatar
Fabrice Bellard committed
224
};