Statistics
| Branch: | Revision:

ffmpeg / libavformat / tcp.c @ 51b317d2

History | View | Annotate | Download (5.63 KB)

1
/*
2
 * TCP protocol
3
 * Copyright (c) 2002 Fabrice Bellard
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * 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
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
#include "avformat.h"
22
#include <unistd.h>
23
#include "internal.h"
24
#include "network.h"
25
#include "os_support.h"
26
#if HAVE_POLL_H
27
#include <poll.h>
28
#endif
29
#include <sys/time.h>
30

    
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
    struct addrinfo hints, *ai, *cur_ai;
39
    int port, fd = -1;
40
    TCPContext *s = NULL;
41
    int ret;
42
    socklen_t optlen;
43
    char hostname[1024],proto[1024],path[1024];
44
    char portstr[10];
45

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

    
51
    memset(&hints, 0, sizeof(hints));
52
    hints.ai_family = AF_UNSPEC;
53
    hints.ai_socktype = SOCK_STREAM;
54
    snprintf(portstr, sizeof(portstr), "%d", port);
55
    ret = getaddrinfo(hostname, portstr, &hints, &ai);
56
    if (ret) {
57
        av_log(NULL, AV_LOG_ERROR,
58
               "Failed to resolve hostname %s: %s\n",
59
               hostname, gai_strerror(ret));
60
        return AVERROR(EIO);
61
    }
62

    
63
    cur_ai = ai;
64

    
65
 restart:
66
    fd = socket(cur_ai->ai_family, cur_ai->ai_socktype, cur_ai->ai_protocol);
67
    if (fd < 0)
68
        goto fail;
69
    ff_socket_nonblock(fd, 1);
70

    
71
 redo:
72
    ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
73
    if (ret < 0) {
74
        struct pollfd p = {fd, POLLOUT, 0};
75
        if (ff_neterrno() == FF_NETERROR(EINTR)) {
76
            if (url_interrupt_cb())
77
                goto fail1;
78
            goto redo;
79
        }
80
        if (ff_neterrno() != FF_NETERROR(EINPROGRESS) &&
81
            ff_neterrno() != FF_NETERROR(EAGAIN))
82
            goto fail;
83

    
84
        /* wait until we are connected or until abort */
85
        for(;;) {
86
            if (url_interrupt_cb()) {
87
                ret = AVERROR(EINTR);
88
                goto fail1;
89
            }
90
            ret = poll(&p, 1, 100);
91
            if (ret > 0)
92
                break;
93
        }
94

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

    
116
 fail:
117
    if (cur_ai->ai_next) {
118
        /* Retry with the next sockaddr */
119
        cur_ai = cur_ai->ai_next;
120
        if (fd >= 0)
121
            closesocket(fd);
122
        goto restart;
123
    }
124
    ret = AVERROR(EIO);
125
 fail1:
126
    if (fd >= 0)
127
        closesocket(fd);
128
    freeaddrinfo(ai);
129
    return ret;
130
}
131

    
132
static int tcp_wait_fd(int fd, int write)
133
{
134
    int ev = write ? POLLOUT : POLLIN;
135
    struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
136
    int ret;
137

    
138
    ret = poll(&p, 1, 100);
139
    return ret < 0 ? ff_neterrno() : !!(p.revents & ev);
140
}
141

    
142
static int tcp_read(URLContext *h, uint8_t *buf, int size)
143
{
144
    TCPContext *s = h->priv_data;
145
    int len, ret;
146

    
147
    for (;;) {
148
        if (url_interrupt_cb())
149
            return AVERROR(EINTR);
150
        ret = tcp_wait_fd(s->fd, 0);
151
        if (ret > 0) {
152
            len = recv(s->fd, buf, size, 0);
153
            if (len < 0) {
154
                if (ff_neterrno() != FF_NETERROR(EINTR) &&
155
                    ff_neterrno() != FF_NETERROR(EAGAIN))
156
                    return ff_neterrno();
157
            } else return len;
158
        } else if (ret < 0) {
159
            if (ret == FF_NETERROR(EINTR))
160
                continue;
161
            return ret;
162
        }
163
    }
164
}
165

    
166
static int tcp_write(URLContext *h, const uint8_t *buf, int size)
167
{
168
    TCPContext *s = h->priv_data;
169
    int ret, size1, len;
170

    
171
    size1 = size;
172
    while (size > 0) {
173
        if (url_interrupt_cb())
174
            return AVERROR(EINTR);
175
        ret = tcp_wait_fd(s->fd, 1);
176
        if (ret > 0) {
177
            len = send(s->fd, buf, size, 0);
178
            if (len < 0) {
179
                if (ff_neterrno() != FF_NETERROR(EINTR) &&
180
                    ff_neterrno() != FF_NETERROR(EAGAIN))
181
                    return ff_neterrno();
182
                continue;
183
            }
184
            size -= len;
185
            buf += len;
186
        } else if (ret < 0) {
187
            if (ret == FF_NETERROR(EINTR))
188
                continue;
189
            return ret;
190
        }
191
    }
192
    return size1 - size;
193
}
194

    
195
static int tcp_close(URLContext *h)
196
{
197
    TCPContext *s = h->priv_data;
198
    closesocket(s->fd);
199
    av_free(s);
200
    return 0;
201
}
202

    
203
static int tcp_get_file_handle(URLContext *h)
204
{
205
    TCPContext *s = h->priv_data;
206
    return s->fd;
207
}
208

    
209
URLProtocol ff_tcp_protocol = {
210
    "tcp",
211
    tcp_open,
212
    tcp_read,
213
    tcp_write,
214
    NULL, /* seek */
215
    tcp_close,
216
    .url_get_file_handle = tcp_get_file_handle,
217
};