Statistics
| Branch: | Revision:

ffmpeg / libavformat / tcp.c @ c76374c6

History | View | Annotate | Download (4.91 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() == AVERROR(EINTR)) {
76
            if (url_interrupt_cb()) {
77
                ret = AVERROR_EXIT;
78
                goto fail1;
79
            }
80
            goto redo;
81
        }
82
        if (ff_neterrno() != AVERROR(EINPROGRESS) &&
83
            ff_neterrno() != AVERROR(EAGAIN))
84
            goto fail;
85

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

    
97
        /* test error */
98
        optlen = sizeof(ret);
99
        getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
100
        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
            goto fail;
105
        }
106
    }
107
    s = av_malloc(sizeof(TCPContext));
108
    if (!s) {
109
        freeaddrinfo(ai);
110
        return AVERROR(ENOMEM);
111
    }
112
    h->priv_data = s;
113
    h->is_streamed = 1;
114
    s->fd = fd;
115
    freeaddrinfo(ai);
116
    return 0;
117

    
118
 fail:
119
    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
    ret = AVERROR(EIO);
127
 fail1:
128
    if (fd >= 0)
129
        closesocket(fd);
130
    freeaddrinfo(ai);
131
    return ret;
132
}
133

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

    
140
    ret = poll(&p, 1, 100);
141
    return ret < 0 ? ff_neterrno() : p.revents & ev ? 0 : AVERROR(EAGAIN);
142
}
143

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

    
149
    if (!(h->flags & URL_FLAG_NONBLOCK)) {
150
        ret = tcp_wait_fd(s->fd, 0);
151
        if (ret < 0)
152
            return ret;
153
    }
154
    ret = recv(s->fd, buf, size, 0);
155
    return ret < 0 ? ff_neterrno() : ret;
156
}
157

    
158
static int tcp_write(URLContext *h, const uint8_t *buf, int size)
159
{
160
    TCPContext *s = h->priv_data;
161
    int ret;
162

    
163
    if (!(h->flags & URL_FLAG_NONBLOCK)) {
164
        ret = tcp_wait_fd(s->fd, 1);
165
        if (ret < 0)
166
            return ret;
167
    }
168
    ret = send(s->fd, buf, size, 0);
169
    return ret < 0 ? ff_neterrno() : ret;
170
}
171

    
172
static int tcp_close(URLContext *h)
173
{
174
    TCPContext *s = h->priv_data;
175
    closesocket(s->fd);
176
    av_free(s);
177
    return 0;
178
}
179

    
180
static int tcp_get_file_handle(URLContext *h)
181
{
182
    TCPContext *s = h->priv_data;
183
    return s->fd;
184
}
185

    
186
URLProtocol ff_tcp_protocol = {
187
    "tcp",
188
    tcp_open,
189
    tcp_read,
190
    tcp_write,
191
    NULL, /* seek */
192
    tcp_close,
193
    .url_get_file_handle = tcp_get_file_handle,
194
};