Statistics
| Branch: | Revision:

ffmpeg / libavformat / tcp.c @ 470bce2b

History | View | Annotate | Download (5.04 KB)

1 171bbb03 Fabrice Bellard
/*
2
 * TCP protocol
3
 * Copyright (c) 2002 Fabrice Bellard.
4
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 171bbb03 Fabrice Bellard
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 171bbb03 Fabrice Bellard
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 171bbb03 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 171bbb03 Fabrice Bellard
 */
21
#include "avformat.h"
22
#include <unistd.h>
23 42572ef5 Ramiro Polla
#include "network.h"
24 087b3272 Luca Abeni
#include "os_support.h"
25 6ad1c9c9 Baptiste Coudurier
#ifdef HAVE_SYS_SELECT_H
26 c75a0cce Baptiste Coudurier
#include <sys/select.h>
27 6ad1c9c9 Baptiste Coudurier
#endif
28 09787fb8 Fabrice Bellard
#include <sys/time.h>
29 171bbb03 Fabrice Bellard
30
typedef struct TCPContext {
31
    int fd;
32
} TCPContext;
33
34
/* return non zero if error */
35
static int tcp_open(URLContext *h, const char *uri, int flags)
36
{
37
    struct sockaddr_in dest_addr;
38
    int port, fd = -1;
39 88730be6 Måns Rullgård
    TCPContext *s = NULL;
40 09787fb8 Fabrice Bellard
    fd_set wfds;
41
    int fd_max, ret;
42
    struct timeval tv;
43
    socklen_t optlen;
44 f23a9759 Ronald S. Bultje
    char hostname[1024],proto[1024],path[1024];
45 6ba5cbc6 Petr Doubek
46 e6c13819 Ronald S. Bultje
    if(!ff_network_init())
47
        return AVERROR(EIO);
48
49 6ba5cbc6 Petr Doubek
    url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
50 a3303add Ronald S. Bultje
        &port, path, sizeof(path), uri);
51
    if (strcmp(proto,"tcp") || port <= 0 || port >= 65536)
52 8b9af28d Ronald S. Bultje
        return AVERROR(EINVAL);
53 115329f1 Diego Biurrun
54 171bbb03 Fabrice Bellard
    dest_addr.sin_family = AF_INET;
55
    dest_addr.sin_port = htons(port);
56
    if (resolve_host(&dest_addr.sin_addr, hostname) < 0)
57 8b9af28d Ronald S. Bultje
        return AVERROR(EIO);
58 171bbb03 Fabrice Bellard
59 b5cccea0 Alex Beregszaszi
    fd = socket(AF_INET, SOCK_STREAM, 0);
60 171bbb03 Fabrice Bellard
    if (fd < 0)
61 8b9af28d Ronald S. Bultje
        return AVERROR(EIO);
62 ba472aaf Alex Beregszaszi
    ff_socket_nonblock(fd, 1);
63 115329f1 Diego Biurrun
64 09787fb8 Fabrice Bellard
 redo:
65 115329f1 Diego Biurrun
    ret = connect(fd, (struct sockaddr *)&dest_addr,
66 09787fb8 Fabrice Bellard
                  sizeof(dest_addr));
67
    if (ret < 0) {
68 8da4034f Alex Beregszaszi
        if (ff_neterrno() == FF_NETERROR(EINTR))
69 09787fb8 Fabrice Bellard
            goto redo;
70 85060fe6 Ramiro Polla
        if (ff_neterrno() != FF_NETERROR(EINPROGRESS) &&
71
            ff_neterrno() != FF_NETERROR(EAGAIN))
72 09787fb8 Fabrice Bellard
            goto fail;
73 171bbb03 Fabrice Bellard
74 09787fb8 Fabrice Bellard
        /* wait until we are connected or until abort */
75
        for(;;) {
76
            if (url_interrupt_cb()) {
77 8fa36ae0 François Revol
                ret = AVERROR(EINTR);
78 09787fb8 Fabrice Bellard
                goto fail1;
79
            }
80
            fd_max = fd;
81
            FD_ZERO(&wfds);
82
            FD_SET(fd, &wfds);
83
            tv.tv_sec = 0;
84
            tv.tv_usec = 100 * 1000;
85
            ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
86
            if (ret > 0 && FD_ISSET(fd, &wfds))
87
                break;
88
        }
89 115329f1 Diego Biurrun
90 09787fb8 Fabrice Bellard
        /* test error */
91
        optlen = sizeof(ret);
92
        getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
93
        if (ret != 0)
94
            goto fail;
95
    }
96 47f944a2 Ronald S. Bultje
    s = av_malloc(sizeof(TCPContext));
97
    if (!s)
98
        return AVERROR(ENOMEM);
99
    h->priv_data = s;
100
    h->is_streamed = 1;
101 171bbb03 Fabrice Bellard
    s->fd = fd;
102
    return 0;
103
104
 fail:
105 6f3e0b21 Panagiotis Issaris
    ret = AVERROR(EIO);
106 09787fb8 Fabrice Bellard
 fail1:
107 171bbb03 Fabrice Bellard
    if (fd >= 0)
108 e9d511dc François Revol
        closesocket(fd);
109 09787fb8 Fabrice Bellard
    return ret;
110 171bbb03 Fabrice Bellard
}
111
112 0c1a9eda Zdenek Kabelac
static int tcp_read(URLContext *h, uint8_t *buf, int size)
113 171bbb03 Fabrice Bellard
{
114
    TCPContext *s = h->priv_data;
115 b51469a0 Leon van Stuivenberg
    int len, fd_max, ret;
116 09787fb8 Fabrice Bellard
    fd_set rfds;
117
    struct timeval tv;
118 171bbb03 Fabrice Bellard
119 9eef2b77 Michael Niedermayer
    for (;;) {
120 09787fb8 Fabrice Bellard
        if (url_interrupt_cb())
121 8fa36ae0 François Revol
            return AVERROR(EINTR);
122 09787fb8 Fabrice Bellard
        fd_max = s->fd;
123
        FD_ZERO(&rfds);
124
        FD_SET(s->fd, &rfds);
125
        tv.tv_sec = 0;
126
        tv.tv_usec = 100 * 1000;
127 b51469a0 Leon van Stuivenberg
        ret = select(fd_max + 1, &rfds, NULL, NULL, &tv);
128
        if (ret > 0 && FD_ISSET(s->fd, &rfds)) {
129
            len = recv(s->fd, buf, size, 0);
130
            if (len < 0) {
131 8da4034f Alex Beregszaszi
                if (ff_neterrno() != FF_NETERROR(EINTR) &&
132
                    ff_neterrno() != FF_NETERROR(EAGAIN))
133 8fa36ae0 François Revol
                    return AVERROR(errno);
134 b51469a0 Leon van Stuivenberg
            } else return len;
135
        } else if (ret < 0) {
136
            return -1;
137
        }
138 171bbb03 Fabrice Bellard
    }
139
}
140
141 0c1a9eda Zdenek Kabelac
static int tcp_write(URLContext *h, uint8_t *buf, int size)
142 171bbb03 Fabrice Bellard
{
143
    TCPContext *s = h->priv_data;
144 b51469a0 Leon van Stuivenberg
    int ret, size1, fd_max, len;
145 09787fb8 Fabrice Bellard
    fd_set wfds;
146
    struct timeval tv;
147 171bbb03 Fabrice Bellard
148
    size1 = size;
149
    while (size > 0) {
150 09787fb8 Fabrice Bellard
        if (url_interrupt_cb())
151 8fa36ae0 François Revol
            return AVERROR(EINTR);
152 09787fb8 Fabrice Bellard
        fd_max = s->fd;
153
        FD_ZERO(&wfds);
154
        FD_SET(s->fd, &wfds);
155
        tv.tv_sec = 0;
156
        tv.tv_usec = 100 * 1000;
157 b51469a0 Leon van Stuivenberg
        ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
158
        if (ret > 0 && FD_ISSET(s->fd, &wfds)) {
159
            len = send(s->fd, buf, size, 0);
160
            if (len < 0) {
161 8da4034f Alex Beregszaszi
                if (ff_neterrno() != FF_NETERROR(EINTR) &&
162
                    ff_neterrno() != FF_NETERROR(EAGAIN))
163 8fa36ae0 François Revol
                    return AVERROR(errno);
164 b51469a0 Leon van Stuivenberg
                continue;
165 6c8e0d4d Philip Gladstone
            }
166 b51469a0 Leon van Stuivenberg
            size -= len;
167
            buf += len;
168
        } else if (ret < 0) {
169
            return -1;
170 6c8e0d4d Philip Gladstone
        }
171 171bbb03 Fabrice Bellard
    }
172
    return size1 - size;
173
}
174
175
static int tcp_close(URLContext *h)
176
{
177
    TCPContext *s = h->priv_data;
178 9ddd71fc François Revol
    closesocket(s->fd);
179 1642cb6b Ramiro Polla
    ff_network_close();
180 171bbb03 Fabrice Bellard
    av_free(s);
181
    return 0;
182
}
183
184
URLProtocol tcp_protocol = {
185
    "tcp",
186
    tcp_open,
187
    tcp_read,
188
    tcp_write,
189
    NULL, /* seek */
190
    tcp_close,
191
};