Statistics
| Branch: | Revision:

ffmpeg / libavformat / tcp.c @ f87b1b37

History | View | Annotate | Download (5.12 KB)

1
/*
2
 * TCP protocol
3
 * Copyright (c) 2002 Fabrice Bellard
4
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav 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
 * Libav 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 Libav; 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 "libavutil/parseutils.h"
23
#include <unistd.h>
24
#include "internal.h"
25
#include "network.h"
26
#include "os_support.h"
27
#if HAVE_POLL_H
28
#include <poll.h>
29
#endif
30
#include <sys/time.h>
31

    
32
typedef struct TCPContext {
33
    int fd;
34
} TCPContext;
35

    
36
/* return non zero if error */
37
static int tcp_open(URLContext *h, const char *uri, int flags)
38
{
39
    struct addrinfo hints, *ai, *cur_ai;
40
    int port, fd = -1;
41
    TCPContext *s = NULL;
42
    int listen_socket = 0;
43
    const char *p;
44
    char buf[256];
45
    int ret;
46
    socklen_t optlen;
47
    char hostname[1024],proto[1024],path[1024];
48
    char portstr[10];
49

    
50
    av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
51
        &port, path, sizeof(path), uri);
52
    if (strcmp(proto,"tcp") || port <= 0 || port >= 65536)
53
        return AVERROR(EINVAL);
54

    
55
    p = strchr(uri, '?');
56
    if (p) {
57
        if (av_find_info_tag(buf, sizeof(buf), "listen", p))
58
            listen_socket = 1;
59
    }
60
    memset(&hints, 0, sizeof(hints));
61
    hints.ai_family = AF_UNSPEC;
62
    hints.ai_socktype = SOCK_STREAM;
63
    snprintf(portstr, sizeof(portstr), "%d", port);
64
    ret = getaddrinfo(hostname, portstr, &hints, &ai);
65
    if (ret) {
66
        av_log(NULL, AV_LOG_ERROR,
67
               "Failed to resolve hostname %s: %s\n",
68
               hostname, gai_strerror(ret));
69
        return AVERROR(EIO);
70
    }
71

    
72
    cur_ai = ai;
73

    
74
 restart:
75
    fd = socket(cur_ai->ai_family, cur_ai->ai_socktype, cur_ai->ai_protocol);
76
    if (fd < 0)
77
        goto fail;
78

    
79
    if (listen_socket) {
80
        int fd1;
81
        ret = bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
82
        listen(fd, 1);
83
        fd1 = accept(fd, NULL, NULL);
84
        closesocket(fd);
85
        fd = fd1;
86
    } else {
87
 redo:
88
        ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
89
    }
90

    
91
    ff_socket_nonblock(fd, 1);
92

    
93
    if (ret < 0) {
94
        struct pollfd p = {fd, POLLOUT, 0};
95
        if (ff_neterrno() == AVERROR(EINTR)) {
96
            if (url_interrupt_cb()) {
97
                ret = AVERROR_EXIT;
98
                goto fail1;
99
            }
100
            goto redo;
101
        }
102
        if (ff_neterrno() != AVERROR(EINPROGRESS) &&
103
            ff_neterrno() != AVERROR(EAGAIN))
104
            goto fail;
105

    
106
        /* wait until we are connected or until abort */
107
        for(;;) {
108
            if (url_interrupt_cb()) {
109
                ret = AVERROR_EXIT;
110
                goto fail1;
111
            }
112
            ret = poll(&p, 1, 100);
113
            if (ret > 0)
114
                break;
115
        }
116

    
117
        /* test error */
118
        optlen = sizeof(ret);
119
        getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
120
        if (ret != 0) {
121
            av_log(NULL, AV_LOG_ERROR,
122
                   "TCP connection to %s:%d failed: %s\n",
123
                   hostname, port, strerror(ret));
124
            goto fail;
125
        }
126
    }
127
    s = av_malloc(sizeof(TCPContext));
128
    if (!s) {
129
        freeaddrinfo(ai);
130
        return AVERROR(ENOMEM);
131
    }
132
    h->priv_data = s;
133
    h->is_streamed = 1;
134
    s->fd = fd;
135
    freeaddrinfo(ai);
136
    return 0;
137

    
138
 fail:
139
    if (cur_ai->ai_next) {
140
        /* Retry with the next sockaddr */
141
        cur_ai = cur_ai->ai_next;
142
        if (fd >= 0)
143
            closesocket(fd);
144
        goto restart;
145
    }
146
    ret = AVERROR(EIO);
147
 fail1:
148
    if (fd >= 0)
149
        closesocket(fd);
150
    freeaddrinfo(ai);
151
    return ret;
152
}
153

    
154
static int tcp_read(URLContext *h, uint8_t *buf, int size)
155
{
156
    TCPContext *s = h->priv_data;
157
    int ret;
158

    
159
    if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
160
        ret = ff_network_wait_fd(s->fd, 0);
161
        if (ret < 0)
162
            return ret;
163
    }
164
    ret = recv(s->fd, buf, size, 0);
165
    return ret < 0 ? ff_neterrno() : ret;
166
}
167

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

    
173
    if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
174
        ret = ff_network_wait_fd(s->fd, 1);
175
        if (ret < 0)
176
            return ret;
177
    }
178
    ret = send(s->fd, buf, size, 0);
179
    return ret < 0 ? ff_neterrno() : ret;
180
}
181

    
182
static int tcp_close(URLContext *h)
183
{
184
    TCPContext *s = h->priv_data;
185
    closesocket(s->fd);
186
    av_free(s);
187
    return 0;
188
}
189

    
190
static int tcp_get_file_handle(URLContext *h)
191
{
192
    TCPContext *s = h->priv_data;
193
    return s->fd;
194
}
195

    
196
URLProtocol ff_tcp_protocol = {
197
    "tcp",
198
    tcp_open,
199
    tcp_read,
200
    tcp_write,
201
    NULL, /* seek */
202
    tcp_close,
203
    .url_get_file_handle = tcp_get_file_handle,
204
};