Statistics
| Branch: | Revision:

ffmpeg / libavformat / tcp.c @ da9e6c42

History | View | Annotate | Download (5.69 KB)

1 171bbb03 Fabrice Bellard
/*
2
 * TCP protocol
3 406792e7 Diego Biurrun
 * Copyright (c) 2002 Fabrice Bellard
4 171bbb03 Fabrice Bellard
 *
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 e4a9e3cc Aurelien Jacobs
#include "internal.h"
24 42572ef5 Ramiro Polla
#include "network.h"
25 087b3272 Luca Abeni
#include "os_support.h"
26 b250f9c6 Aurelien Jacobs
#if HAVE_SYS_SELECT_H
27 c75a0cce Baptiste Coudurier
#include <sys/select.h>
28 6ad1c9c9 Baptiste Coudurier
#endif
29 09787fb8 Fabrice Bellard
#include <sys/time.h>
30 171bbb03 Fabrice Bellard
31
typedef struct TCPContext {
32
    int fd;
33
} TCPContext;
34
35
/* return non zero if error */
36
static int tcp_open(URLContext *h, const char *uri, int flags)
37
{
38 fdcdd539 Martin Storsjö
    struct addrinfo hints, *ai, *cur_ai;
39 171bbb03 Fabrice Bellard
    int port, fd = -1;
40 88730be6 Måns Rullgård
    TCPContext *s = NULL;
41 09787fb8 Fabrice Bellard
    fd_set wfds;
42
    int fd_max, ret;
43
    struct timeval tv;
44
    socklen_t optlen;
45 f23a9759 Ronald S. Bultje
    char hostname[1024],proto[1024],path[1024];
46 fdcdd539 Martin Storsjö
    char portstr[10];
47 6ba5cbc6 Petr Doubek
48 c5c6e67c Martin Storsjö
    ff_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
49 a3303add Ronald S. Bultje
        &port, path, sizeof(path), uri);
50
    if (strcmp(proto,"tcp") || port <= 0 || port >= 65536)
51 8b9af28d Ronald S. Bultje
        return AVERROR(EINVAL);
52 115329f1 Diego Biurrun
53 fdcdd539 Martin Storsjö
    memset(&hints, 0, sizeof(hints));
54 88248b76 Ronald S. Bultje
    hints.ai_family = AF_UNSPEC;
55 fdcdd539 Martin Storsjö
    hints.ai_socktype = SOCK_STREAM;
56
    snprintf(portstr, sizeof(portstr), "%d", port);
57
    if (getaddrinfo(hostname, portstr, &hints, &ai))
58 8b9af28d Ronald S. Bultje
        return AVERROR(EIO);
59 171bbb03 Fabrice Bellard
60 fdcdd539 Martin Storsjö
    cur_ai = ai;
61
62
 restart:
63
    fd = socket(cur_ai->ai_family, cur_ai->ai_socktype, cur_ai->ai_protocol);
64 171bbb03 Fabrice Bellard
    if (fd < 0)
65 fdcdd539 Martin Storsjö
        goto fail;
66 ba472aaf Alex Beregszaszi
    ff_socket_nonblock(fd, 1);
67 115329f1 Diego Biurrun
68 09787fb8 Fabrice Bellard
 redo:
69 fdcdd539 Martin Storsjö
    ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
70 09787fb8 Fabrice Bellard
    if (ret < 0) {
71 8da4034f Alex Beregszaszi
        if (ff_neterrno() == FF_NETERROR(EINTR))
72 09787fb8 Fabrice Bellard
            goto redo;
73 85060fe6 Ramiro Polla
        if (ff_neterrno() != FF_NETERROR(EINPROGRESS) &&
74
            ff_neterrno() != FF_NETERROR(EAGAIN))
75 09787fb8 Fabrice Bellard
            goto fail;
76 171bbb03 Fabrice Bellard
77 09787fb8 Fabrice Bellard
        /* wait until we are connected or until abort */
78
        for(;;) {
79
            if (url_interrupt_cb()) {
80 8fa36ae0 François Revol
                ret = AVERROR(EINTR);
81 09787fb8 Fabrice Bellard
                goto fail1;
82
            }
83
            fd_max = fd;
84
            FD_ZERO(&wfds);
85
            FD_SET(fd, &wfds);
86
            tv.tv_sec = 0;
87
            tv.tv_usec = 100 * 1000;
88
            ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
89
            if (ret > 0 && FD_ISSET(fd, &wfds))
90
                break;
91
        }
92 115329f1 Diego Biurrun
93 09787fb8 Fabrice Bellard
        /* test error */
94
        optlen = sizeof(ret);
95
        getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
96
        if (ret != 0)
97
            goto fail;
98
    }
99 47f944a2 Ronald S. Bultje
    s = av_malloc(sizeof(TCPContext));
100 fdcdd539 Martin Storsjö
    if (!s) {
101
        freeaddrinfo(ai);
102 47f944a2 Ronald S. Bultje
        return AVERROR(ENOMEM);
103 fdcdd539 Martin Storsjö
    }
104 47f944a2 Ronald S. Bultje
    h->priv_data = s;
105
    h->is_streamed = 1;
106 171bbb03 Fabrice Bellard
    s->fd = fd;
107 fdcdd539 Martin Storsjö
    freeaddrinfo(ai);
108 171bbb03 Fabrice Bellard
    return 0;
109
110
 fail:
111 fdcdd539 Martin Storsjö
    if (cur_ai->ai_next) {
112
        /* Retry with the next sockaddr */
113
        cur_ai = cur_ai->ai_next;
114
        if (fd >= 0)
115
            closesocket(fd);
116
        goto restart;
117
    }
118 6f3e0b21 Panagiotis Issaris
    ret = AVERROR(EIO);
119 09787fb8 Fabrice Bellard
 fail1:
120 171bbb03 Fabrice Bellard
    if (fd >= 0)
121 e9d511dc François Revol
        closesocket(fd);
122 fdcdd539 Martin Storsjö
    freeaddrinfo(ai);
123 09787fb8 Fabrice Bellard
    return ret;
124 171bbb03 Fabrice Bellard
}
125
126 0c1a9eda Zdenek Kabelac
static int tcp_read(URLContext *h, uint8_t *buf, int size)
127 171bbb03 Fabrice Bellard
{
128
    TCPContext *s = h->priv_data;
129 b51469a0 Leon van Stuivenberg
    int len, fd_max, ret;
130 09787fb8 Fabrice Bellard
    fd_set rfds;
131
    struct timeval tv;
132 171bbb03 Fabrice Bellard
133 9eef2b77 Michael Niedermayer
    for (;;) {
134 09787fb8 Fabrice Bellard
        if (url_interrupt_cb())
135 8fa36ae0 François Revol
            return AVERROR(EINTR);
136 09787fb8 Fabrice Bellard
        fd_max = s->fd;
137
        FD_ZERO(&rfds);
138
        FD_SET(s->fd, &rfds);
139
        tv.tv_sec = 0;
140
        tv.tv_usec = 100 * 1000;
141 b51469a0 Leon van Stuivenberg
        ret = select(fd_max + 1, &rfds, NULL, NULL, &tv);
142
        if (ret > 0 && FD_ISSET(s->fd, &rfds)) {
143
            len = recv(s->fd, buf, size, 0);
144
            if (len < 0) {
145 8da4034f Alex Beregszaszi
                if (ff_neterrno() != FF_NETERROR(EINTR) &&
146
                    ff_neterrno() != FF_NETERROR(EAGAIN))
147 73c13268 Martin Storsjö
                    return AVERROR(ff_neterrno());
148 b51469a0 Leon van Stuivenberg
            } else return len;
149
        } else if (ret < 0) {
150 cae9a15c Martin Storsjö
            if (ff_neterrno() == FF_NETERROR(EINTR))
151
                continue;
152 b51469a0 Leon van Stuivenberg
            return -1;
153
        }
154 171bbb03 Fabrice Bellard
    }
155
}
156
157 27241cbf Martin Storsjö
static int tcp_write(URLContext *h, const uint8_t *buf, int size)
158 171bbb03 Fabrice Bellard
{
159
    TCPContext *s = h->priv_data;
160 b51469a0 Leon van Stuivenberg
    int ret, size1, fd_max, len;
161 09787fb8 Fabrice Bellard
    fd_set wfds;
162
    struct timeval tv;
163 171bbb03 Fabrice Bellard
164
    size1 = size;
165
    while (size > 0) {
166 09787fb8 Fabrice Bellard
        if (url_interrupt_cb())
167 8fa36ae0 François Revol
            return AVERROR(EINTR);
168 09787fb8 Fabrice Bellard
        fd_max = s->fd;
169
        FD_ZERO(&wfds);
170
        FD_SET(s->fd, &wfds);
171
        tv.tv_sec = 0;
172
        tv.tv_usec = 100 * 1000;
173 b51469a0 Leon van Stuivenberg
        ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
174
        if (ret > 0 && FD_ISSET(s->fd, &wfds)) {
175
            len = send(s->fd, buf, size, 0);
176
            if (len < 0) {
177 8da4034f Alex Beregszaszi
                if (ff_neterrno() != FF_NETERROR(EINTR) &&
178
                    ff_neterrno() != FF_NETERROR(EAGAIN))
179 73c13268 Martin Storsjö
                    return AVERROR(ff_neterrno());
180 b51469a0 Leon van Stuivenberg
                continue;
181 6c8e0d4d Philip Gladstone
            }
182 b51469a0 Leon van Stuivenberg
            size -= len;
183
            buf += len;
184
        } else if (ret < 0) {
185 cae9a15c Martin Storsjö
            if (ff_neterrno() == FF_NETERROR(EINTR))
186
                continue;
187 b51469a0 Leon van Stuivenberg
            return -1;
188 6c8e0d4d Philip Gladstone
        }
189 171bbb03 Fabrice Bellard
    }
190
    return size1 - size;
191
}
192
193
static int tcp_close(URLContext *h)
194
{
195
    TCPContext *s = h->priv_data;
196 9ddd71fc François Revol
    closesocket(s->fd);
197 171bbb03 Fabrice Bellard
    av_free(s);
198
    return 0;
199
}
200
201 f0a80394 Ronald S. Bultje
static int tcp_get_file_handle(URLContext *h)
202
{
203
    TCPContext *s = h->priv_data;
204
    return s->fd;
205
}
206
207 171bbb03 Fabrice Bellard
URLProtocol tcp_protocol = {
208
    "tcp",
209
    tcp_open,
210
    tcp_read,
211
    tcp_write,
212
    NULL, /* seek */
213
    tcp_close,
214 f0a80394 Ronald S. Bultje
    .url_get_file_handle = tcp_get_file_handle,
215 171bbb03 Fabrice Bellard
};