tcp.c 5.18 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 "network.h"
24
#include "os_support.h"
25
#if HAVE_SYS_SELECT_H
26
#include <sys/select.h>
27
#endif
Fabrice Bellard's avatar
Fabrice Bellard committed
28
#include <sys/time.h>
Fabrice Bellard's avatar
Fabrice Bellard committed
29 30 31 32 33 34 35 36 37 38

typedef struct TCPContext {
    int fd;
} TCPContext;

/* return non zero if error */
static int tcp_open(URLContext *h, const char *uri, int flags)
{
    struct sockaddr_in dest_addr;
    int port, fd = -1;
39
    TCPContext *s = NULL;
Fabrice Bellard's avatar
Fabrice Bellard committed
40 41 42 43
    fd_set wfds;
    int fd_max, ret;
    struct timeval tv;
    socklen_t optlen;
44
    char hostname[1024],proto[1024],path[1024];
45

46 47 48
    if(!ff_network_init())
        return AVERROR(EIO);

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

Fabrice Bellard's avatar
Fabrice Bellard committed
54 55 56
    dest_addr.sin_family = AF_INET;
    dest_addr.sin_port = htons(port);
    if (resolve_host(&dest_addr.sin_addr, hostname) < 0)
57
        return AVERROR(EIO);
Fabrice Bellard's avatar
Fabrice Bellard committed
58

59
    fd = socket(AF_INET, SOCK_STREAM, 0);
Fabrice Bellard's avatar
Fabrice Bellard committed
60
    if (fd < 0)
61
        return AVERROR(EIO);
62
    ff_socket_nonblock(fd, 1);
63

Fabrice Bellard's avatar
Fabrice Bellard committed
64
 redo:
65
    ret = connect(fd, (struct sockaddr *)&dest_addr,
Fabrice Bellard's avatar
Fabrice Bellard committed
66 67
                  sizeof(dest_addr));
    if (ret < 0) {
68
        if (ff_neterrno() == FF_NETERROR(EINTR))
Fabrice Bellard's avatar
Fabrice Bellard committed
69
            goto redo;
70 71
        if (ff_neterrno() != FF_NETERROR(EINPROGRESS) &&
            ff_neterrno() != FF_NETERROR(EAGAIN))
Fabrice Bellard's avatar
Fabrice Bellard committed
72
            goto fail;
Fabrice Bellard's avatar
Fabrice Bellard committed
73

Fabrice Bellard's avatar
Fabrice Bellard committed
74 75 76
        /* wait until we are connected or until abort */
        for(;;) {
            if (url_interrupt_cb()) {
77
                ret = AVERROR(EINTR);
Fabrice Bellard's avatar
Fabrice Bellard committed
78 79 80 81 82 83 84 85 86 87 88
                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;
        }
89

Fabrice Bellard's avatar
Fabrice Bellard committed
90 91 92 93 94 95
        /* test error */
        optlen = sizeof(ret);
        getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
        if (ret != 0)
            goto fail;
    }
96 97 98 99 100
    s = av_malloc(sizeof(TCPContext));
    if (!s)
        return AVERROR(ENOMEM);
    h->priv_data = s;
    h->is_streamed = 1;
Fabrice Bellard's avatar
Fabrice Bellard committed
101 102 103 104
    s->fd = fd;
    return 0;

 fail:
105
    ret = AVERROR(EIO);
Fabrice Bellard's avatar
Fabrice Bellard committed
106
 fail1:
Fabrice Bellard's avatar
Fabrice Bellard committed
107
    if (fd >= 0)
108
        closesocket(fd);
Fabrice Bellard's avatar
Fabrice Bellard committed
109
    return ret;
Fabrice Bellard's avatar
Fabrice Bellard committed
110 111
}

112
static int tcp_read(URLContext *h, uint8_t *buf, int size)
Fabrice Bellard's avatar
Fabrice Bellard committed
113 114
{
    TCPContext *s = h->priv_data;
115
    int len, fd_max, ret;
Fabrice Bellard's avatar
Fabrice Bellard committed
116 117
    fd_set rfds;
    struct timeval tv;
Fabrice Bellard's avatar
Fabrice Bellard committed
118

119
    for (;;) {
Fabrice Bellard's avatar
Fabrice Bellard committed
120
        if (url_interrupt_cb())
121
            return AVERROR(EINTR);
Fabrice Bellard's avatar
Fabrice Bellard committed
122 123 124 125 126
        fd_max = s->fd;
        FD_ZERO(&rfds);
        FD_SET(s->fd, &rfds);
        tv.tv_sec = 0;
        tv.tv_usec = 100 * 1000;
127 128 129 130
        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) {
131 132
                if (ff_neterrno() != FF_NETERROR(EINTR) &&
                    ff_neterrno() != FF_NETERROR(EAGAIN))
133
                    return AVERROR(errno);
134 135 136 137
            } else return len;
        } else if (ret < 0) {
            return -1;
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
138 139 140
    }
}

141
static int tcp_write(URLContext *h, uint8_t *buf, int size)
Fabrice Bellard's avatar
Fabrice Bellard committed
142 143
{
    TCPContext *s = h->priv_data;
144
    int ret, size1, fd_max, len;
Fabrice Bellard's avatar
Fabrice Bellard committed
145 146
    fd_set wfds;
    struct timeval tv;
Fabrice Bellard's avatar
Fabrice Bellard committed
147 148 149

    size1 = size;
    while (size > 0) {
Fabrice Bellard's avatar
Fabrice Bellard committed
150
        if (url_interrupt_cb())
151
            return AVERROR(EINTR);
Fabrice Bellard's avatar
Fabrice Bellard committed
152 153 154 155 156
        fd_max = s->fd;
        FD_ZERO(&wfds);
        FD_SET(s->fd, &wfds);
        tv.tv_sec = 0;
        tv.tv_usec = 100 * 1000;
157 158 159 160
        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) {
161 162
                if (ff_neterrno() != FF_NETERROR(EINTR) &&
                    ff_neterrno() != FF_NETERROR(EAGAIN))
163
                    return AVERROR(errno);
164
                continue;
165
            }
166 167 168 169
            size -= len;
            buf += len;
        } else if (ret < 0) {
            return -1;
170
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
171 172 173 174 175 176 177
    }
    return size1 - size;
}

static int tcp_close(URLContext *h)
{
    TCPContext *s = h->priv_data;
178
    closesocket(s->fd);
179
    ff_network_close();
Fabrice Bellard's avatar
Fabrice Bellard committed
180 181 182 183
    av_free(s);
    return 0;
}

184 185 186 187 188 189
static int tcp_get_file_handle(URLContext *h)
{
    TCPContext *s = h->priv_data;
    return s->fd;
}

Fabrice Bellard's avatar
Fabrice Bellard committed
190 191 192 193 194 195 196
URLProtocol tcp_protocol = {
    "tcp",
    tcp_open,
    tcp_read,
    tcp_write,
    NULL, /* seek */
    tcp_close,
197
    .url_get_file_handle = tcp_get_file_handle,
Fabrice Bellard's avatar
Fabrice Bellard committed
198
};