Statistics
| Branch: | Revision:

ffmpeg / libavformat / tcp.c @ 6e59474b

History | View | Annotate | Download (4.88 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 ? 0 : FF_NETERROR(EAGAIN);
140
}
141

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

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

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

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

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

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

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