Statistics
| Branch: | Revision:

ffmpeg / libavformat / tcp.c @ ebba2b3e

History | View | Annotate | Download (4.66 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
#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 a8475bbd Luca Barbato
#if HAVE_POLL_H
27
#include <poll.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 a8475bbd Luca Barbato
    int ret;
42 09787fb8 Fabrice Bellard
    socklen_t optlen;
43 f23a9759 Ronald S. Bultje
    char hostname[1024],proto[1024],path[1024];
44 fdcdd539 Martin Storsjö
    char portstr[10];
45 6ba5cbc6 Petr Doubek
46 f3bfe388 Måns Rullgård
    av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
47 a3303add Ronald S. Bultje
        &port, path, sizeof(path), uri);
48
    if (strcmp(proto,"tcp") || port <= 0 || port >= 65536)
49 8b9af28d Ronald S. Bultje
        return AVERROR(EINVAL);
50 115329f1 Diego Biurrun
51 fdcdd539 Martin Storsjö
    memset(&hints, 0, sizeof(hints));
52 88248b76 Ronald S. Bultje
    hints.ai_family = AF_UNSPEC;
53 fdcdd539 Martin Storsjö
    hints.ai_socktype = SOCK_STREAM;
54
    snprintf(portstr, sizeof(portstr), "%d", port);
55 63638a3c Ronald S. Bultje
    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 8b9af28d Ronald S. Bultje
        return AVERROR(EIO);
61 63638a3c Ronald S. Bultje
    }
62 171bbb03 Fabrice Bellard
63 fdcdd539 Martin Storsjö
    cur_ai = ai;
64
65
 restart:
66
    fd = socket(cur_ai->ai_family, cur_ai->ai_socktype, cur_ai->ai_protocol);
67 171bbb03 Fabrice Bellard
    if (fd < 0)
68 fdcdd539 Martin Storsjö
        goto fail;
69 ba472aaf Alex Beregszaszi
    ff_socket_nonblock(fd, 1);
70 115329f1 Diego Biurrun
71 09787fb8 Fabrice Bellard
 redo:
72 fdcdd539 Martin Storsjö
    ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
73 09787fb8 Fabrice Bellard
    if (ret < 0) {
74 a8475bbd Luca Barbato
        struct pollfd p = {fd, POLLOUT, 0};
75 28c4741a Martin Storsjö
        if (ff_neterrno() == AVERROR(EINTR)) {
76 c76374c6 Nicolas George
            if (url_interrupt_cb()) {
77
                ret = AVERROR_EXIT;
78 1aa58c64 Thomas Guillem
                goto fail1;
79 c76374c6 Nicolas George
            }
80 09787fb8 Fabrice Bellard
            goto redo;
81 1aa58c64 Thomas Guillem
        }
82 28c4741a Martin Storsjö
        if (ff_neterrno() != AVERROR(EINPROGRESS) &&
83
            ff_neterrno() != AVERROR(EAGAIN))
84 09787fb8 Fabrice Bellard
            goto fail;
85 171bbb03 Fabrice Bellard
86 09787fb8 Fabrice Bellard
        /* wait until we are connected or until abort */
87
        for(;;) {
88
            if (url_interrupt_cb()) {
89 c76374c6 Nicolas George
                ret = AVERROR_EXIT;
90 09787fb8 Fabrice Bellard
                goto fail1;
91
            }
92 a8475bbd Luca Barbato
            ret = poll(&p, 1, 100);
93
            if (ret > 0)
94 09787fb8 Fabrice Bellard
                break;
95
        }
96 115329f1 Diego Biurrun
97 09787fb8 Fabrice Bellard
        /* test error */
98
        optlen = sizeof(ret);
99
        getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
100 63638a3c Ronald S. Bultje
        if (ret != 0) {
101
            av_log(NULL, AV_LOG_ERROR,
102
                   "TCP connection to %s:%d failed: %s\n",
103
                   hostname, port, strerror(ret));
104 09787fb8 Fabrice Bellard
            goto fail;
105 63638a3c Ronald S. Bultje
        }
106 09787fb8 Fabrice Bellard
    }
107 47f944a2 Ronald S. Bultje
    s = av_malloc(sizeof(TCPContext));
108 fdcdd539 Martin Storsjö
    if (!s) {
109
        freeaddrinfo(ai);
110 47f944a2 Ronald S. Bultje
        return AVERROR(ENOMEM);
111 fdcdd539 Martin Storsjö
    }
112 47f944a2 Ronald S. Bultje
    h->priv_data = s;
113
    h->is_streamed = 1;
114 171bbb03 Fabrice Bellard
    s->fd = fd;
115 fdcdd539 Martin Storsjö
    freeaddrinfo(ai);
116 171bbb03 Fabrice Bellard
    return 0;
117
118
 fail:
119 fdcdd539 Martin Storsjö
    if (cur_ai->ai_next) {
120
        /* Retry with the next sockaddr */
121
        cur_ai = cur_ai->ai_next;
122
        if (fd >= 0)
123
            closesocket(fd);
124
        goto restart;
125
    }
126 6f3e0b21 Panagiotis Issaris
    ret = AVERROR(EIO);
127 09787fb8 Fabrice Bellard
 fail1:
128 171bbb03 Fabrice Bellard
    if (fd >= 0)
129 e9d511dc François Revol
        closesocket(fd);
130 fdcdd539 Martin Storsjö
    freeaddrinfo(ai);
131 09787fb8 Fabrice Bellard
    return ret;
132 171bbb03 Fabrice Bellard
}
133
134 0c1a9eda Zdenek Kabelac
static int tcp_read(URLContext *h, uint8_t *buf, int size)
135 171bbb03 Fabrice Bellard
{
136
    TCPContext *s = h->priv_data;
137 ad3cffb6 Nicolas George
    int ret;
138 171bbb03 Fabrice Bellard
139 ad3cffb6 Nicolas George
    if (!(h->flags & URL_FLAG_NONBLOCK)) {
140 ebba2b3e Luca Barbato
        ret = ff_network_wait_fd(s->fd, 0);
141 ad3cffb6 Nicolas George
        if (ret < 0)
142 51b317d2 Nicolas George
            return ret;
143 171bbb03 Fabrice Bellard
    }
144 ad3cffb6 Nicolas George
    ret = recv(s->fd, buf, size, 0);
145
    return ret < 0 ? ff_neterrno() : ret;
146 171bbb03 Fabrice Bellard
}
147
148 27241cbf Martin Storsjö
static int tcp_write(URLContext *h, const uint8_t *buf, int size)
149 171bbb03 Fabrice Bellard
{
150
    TCPContext *s = h->priv_data;
151 ad3cffb6 Nicolas George
    int ret;
152 171bbb03 Fabrice Bellard
153 ad3cffb6 Nicolas George
    if (!(h->flags & URL_FLAG_NONBLOCK)) {
154 ebba2b3e Luca Barbato
        ret = ff_network_wait_fd(s->fd, 1);
155 ad3cffb6 Nicolas George
        if (ret < 0)
156 51b317d2 Nicolas George
            return ret;
157 171bbb03 Fabrice Bellard
    }
158 ad3cffb6 Nicolas George
    ret = send(s->fd, buf, size, 0);
159
    return ret < 0 ? ff_neterrno() : ret;
160 171bbb03 Fabrice Bellard
}
161
162
static int tcp_close(URLContext *h)
163
{
164
    TCPContext *s = h->priv_data;
165 9ddd71fc François Revol
    closesocket(s->fd);
166 171bbb03 Fabrice Bellard
    av_free(s);
167
    return 0;
168
}
169
170 f0a80394 Ronald S. Bultje
static int tcp_get_file_handle(URLContext *h)
171
{
172
    TCPContext *s = h->priv_data;
173
    return s->fd;
174
}
175
176 c6610a21 Diego Elio Pettenò
URLProtocol ff_tcp_protocol = {
177 171bbb03 Fabrice Bellard
    "tcp",
178
    tcp_open,
179
    tcp_read,
180
    tcp_write,
181
    NULL, /* seek */
182
    tcp_close,
183 f0a80394 Ronald S. Bultje
    .url_get_file_handle = tcp_get_file_handle,
184 171bbb03 Fabrice Bellard
};