Statistics
| Branch: | Revision:

ffmpeg / libavformat / tcp.c @ c60112f2

History | View | Annotate | Download (5.22 KB)

1 171bbb03 Fabrice Bellard
/*
2
 * TCP protocol
3 406792e7 Diego Biurrun
 * Copyright (c) 2002 Fabrice Bellard
4 171bbb03 Fabrice Bellard
 *
5 2912e87a Mans Rullgard
 * This file is part of Libav.
6 b78e7197 Diego Biurrun
 *
7 2912e87a Mans Rullgard
 * Libav 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 2912e87a Mans Rullgard
 * Libav 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 2912e87a Mans Rullgard
 * License along with Libav; 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 3d42d493 Luca Barbato
#include "libavutil/parseutils.h"
23 171bbb03 Fabrice Bellard
#include <unistd.h>
24 e4a9e3cc Aurelien Jacobs
#include "internal.h"
25 42572ef5 Ramiro Polla
#include "network.h"
26 087b3272 Luca Abeni
#include "os_support.h"
27 5cec8971 Anton Khirnov
#include "url.h"
28 a8475bbd Luca Barbato
#if HAVE_POLL_H
29
#include <poll.h>
30 6ad1c9c9 Baptiste Coudurier
#endif
31 09787fb8 Fabrice Bellard
#include <sys/time.h>
32 171bbb03 Fabrice Bellard
33
typedef struct TCPContext {
34
    int fd;
35
} TCPContext;
36
37
/* return non zero if error */
38
static int tcp_open(URLContext *h, const char *uri, int flags)
39
{
40 fdcdd539 Martin Storsjö
    struct addrinfo hints, *ai, *cur_ai;
41 171bbb03 Fabrice Bellard
    int port, fd = -1;
42 88730be6 Måns Rullgård
    TCPContext *s = NULL;
43 3d42d493 Luca Barbato
    int listen_socket = 0;
44
    const char *p;
45
    char buf[256];
46 a8475bbd Luca Barbato
    int ret;
47 09787fb8 Fabrice Bellard
    socklen_t optlen;
48 f23a9759 Ronald S. Bultje
    char hostname[1024],proto[1024],path[1024];
49 fdcdd539 Martin Storsjö
    char portstr[10];
50 6ba5cbc6 Petr Doubek
51 f3bfe388 Måns Rullgård
    av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
52 a3303add Ronald S. Bultje
        &port, path, sizeof(path), uri);
53
    if (strcmp(proto,"tcp") || port <= 0 || port >= 65536)
54 8b9af28d Ronald S. Bultje
        return AVERROR(EINVAL);
55 115329f1 Diego Biurrun
56 3d42d493 Luca Barbato
    p = strchr(uri, '?');
57
    if (p) {
58
        if (av_find_info_tag(buf, sizeof(buf), "listen", p))
59
            listen_socket = 1;
60
    }
61 fdcdd539 Martin Storsjö
    memset(&hints, 0, sizeof(hints));
62 88248b76 Ronald S. Bultje
    hints.ai_family = AF_UNSPEC;
63 fdcdd539 Martin Storsjö
    hints.ai_socktype = SOCK_STREAM;
64
    snprintf(portstr, sizeof(portstr), "%d", port);
65 63638a3c Ronald S. Bultje
    ret = getaddrinfo(hostname, portstr, &hints, &ai);
66
    if (ret) {
67 c60112f2 Martin Storsjö
        av_log(h, AV_LOG_ERROR,
68 63638a3c Ronald S. Bultje
               "Failed to resolve hostname %s: %s\n",
69
               hostname, gai_strerror(ret));
70 8b9af28d Ronald S. Bultje
        return AVERROR(EIO);
71 63638a3c Ronald S. Bultje
    }
72 171bbb03 Fabrice Bellard
73 fdcdd539 Martin Storsjö
    cur_ai = ai;
74
75
 restart:
76
    fd = socket(cur_ai->ai_family, cur_ai->ai_socktype, cur_ai->ai_protocol);
77 171bbb03 Fabrice Bellard
    if (fd < 0)
78 fdcdd539 Martin Storsjö
        goto fail;
79 115329f1 Diego Biurrun
80 3d42d493 Luca Barbato
    if (listen_socket) {
81
        int fd1;
82
        ret = bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
83
        listen(fd, 1);
84
        fd1 = accept(fd, NULL, NULL);
85
        closesocket(fd);
86
        fd = fd1;
87
    } else {
88 09787fb8 Fabrice Bellard
 redo:
89 3d42d493 Luca Barbato
        ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
90
    }
91
92
    ff_socket_nonblock(fd, 1);
93
94 09787fb8 Fabrice Bellard
    if (ret < 0) {
95 a8475bbd Luca Barbato
        struct pollfd p = {fd, POLLOUT, 0};
96 28c4741a Martin Storsjö
        if (ff_neterrno() == AVERROR(EINTR)) {
97 c76374c6 Nicolas George
            if (url_interrupt_cb()) {
98
                ret = AVERROR_EXIT;
99 1aa58c64 Thomas Guillem
                goto fail1;
100 c76374c6 Nicolas George
            }
101 09787fb8 Fabrice Bellard
            goto redo;
102 1aa58c64 Thomas Guillem
        }
103 28c4741a Martin Storsjö
        if (ff_neterrno() != AVERROR(EINPROGRESS) &&
104
            ff_neterrno() != AVERROR(EAGAIN))
105 09787fb8 Fabrice Bellard
            goto fail;
106 171bbb03 Fabrice Bellard
107 09787fb8 Fabrice Bellard
        /* wait until we are connected or until abort */
108
        for(;;) {
109
            if (url_interrupt_cb()) {
110 c76374c6 Nicolas George
                ret = AVERROR_EXIT;
111 09787fb8 Fabrice Bellard
                goto fail1;
112
            }
113 a8475bbd Luca Barbato
            ret = poll(&p, 1, 100);
114
            if (ret > 0)
115 09787fb8 Fabrice Bellard
                break;
116
        }
117 115329f1 Diego Biurrun
118 09787fb8 Fabrice Bellard
        /* test error */
119
        optlen = sizeof(ret);
120
        getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
121 63638a3c Ronald S. Bultje
        if (ret != 0) {
122 c60112f2 Martin Storsjö
            av_log(h, AV_LOG_ERROR,
123 63638a3c Ronald S. Bultje
                   "TCP connection to %s:%d failed: %s\n",
124
                   hostname, port, strerror(ret));
125 09787fb8 Fabrice Bellard
            goto fail;
126 63638a3c Ronald S. Bultje
        }
127 09787fb8 Fabrice Bellard
    }
128 47f944a2 Ronald S. Bultje
    s = av_malloc(sizeof(TCPContext));
129 fdcdd539 Martin Storsjö
    if (!s) {
130
        freeaddrinfo(ai);
131 47f944a2 Ronald S. Bultje
        return AVERROR(ENOMEM);
132 fdcdd539 Martin Storsjö
    }
133 47f944a2 Ronald S. Bultje
    h->priv_data = s;
134
    h->is_streamed = 1;
135 171bbb03 Fabrice Bellard
    s->fd = fd;
136 fdcdd539 Martin Storsjö
    freeaddrinfo(ai);
137 171bbb03 Fabrice Bellard
    return 0;
138
139
 fail:
140 fdcdd539 Martin Storsjö
    if (cur_ai->ai_next) {
141
        /* Retry with the next sockaddr */
142
        cur_ai = cur_ai->ai_next;
143
        if (fd >= 0)
144
            closesocket(fd);
145
        goto restart;
146
    }
147 6f3e0b21 Panagiotis Issaris
    ret = AVERROR(EIO);
148 09787fb8 Fabrice Bellard
 fail1:
149 171bbb03 Fabrice Bellard
    if (fd >= 0)
150 e9d511dc François Revol
        closesocket(fd);
151 fdcdd539 Martin Storsjö
    freeaddrinfo(ai);
152 09787fb8 Fabrice Bellard
    return ret;
153 171bbb03 Fabrice Bellard
}
154
155 0c1a9eda Zdenek Kabelac
static int tcp_read(URLContext *h, uint8_t *buf, int size)
156 171bbb03 Fabrice Bellard
{
157
    TCPContext *s = h->priv_data;
158 ad3cffb6 Nicolas George
    int ret;
159 171bbb03 Fabrice Bellard
160 f87b1b37 Anton Khirnov
    if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
161 ebba2b3e Luca Barbato
        ret = ff_network_wait_fd(s->fd, 0);
162 ad3cffb6 Nicolas George
        if (ret < 0)
163 51b317d2 Nicolas George
            return ret;
164 171bbb03 Fabrice Bellard
    }
165 ad3cffb6 Nicolas George
    ret = recv(s->fd, buf, size, 0);
166
    return ret < 0 ? ff_neterrno() : ret;
167 171bbb03 Fabrice Bellard
}
168
169 27241cbf Martin Storsjö
static int tcp_write(URLContext *h, const uint8_t *buf, int size)
170 171bbb03 Fabrice Bellard
{
171
    TCPContext *s = h->priv_data;
172 ad3cffb6 Nicolas George
    int ret;
173 171bbb03 Fabrice Bellard
174 f87b1b37 Anton Khirnov
    if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
175 ebba2b3e Luca Barbato
        ret = ff_network_wait_fd(s->fd, 1);
176 ad3cffb6 Nicolas George
        if (ret < 0)
177 51b317d2 Nicolas George
            return ret;
178 171bbb03 Fabrice Bellard
    }
179 ad3cffb6 Nicolas George
    ret = send(s->fd, buf, size, 0);
180
    return ret < 0 ? ff_neterrno() : ret;
181 171bbb03 Fabrice Bellard
}
182
183
static int tcp_close(URLContext *h)
184
{
185
    TCPContext *s = h->priv_data;
186 9ddd71fc François Revol
    closesocket(s->fd);
187 171bbb03 Fabrice Bellard
    av_free(s);
188
    return 0;
189
}
190
191 f0a80394 Ronald S. Bultje
static int tcp_get_file_handle(URLContext *h)
192
{
193
    TCPContext *s = h->priv_data;
194
    return s->fd;
195
}
196
197 c6610a21 Diego Elio Pettenò
URLProtocol ff_tcp_protocol = {
198 f35ff97f Anton Khirnov
    .name                = "tcp",
199
    .url_open            = tcp_open,
200
    .url_read            = tcp_read,
201
    .url_write           = tcp_write,
202
    .url_close           = tcp_close,
203 f0a80394 Ronald S. Bultje
    .url_get_file_handle = tcp_get_file_handle,
204 171bbb03 Fabrice Bellard
};