Statistics
| Branch: | Revision:

ffmpeg / libavformat / tcp.c @ ebba2b3e

History | View | Annotate | Download (4.66 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 <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_read(URLContext *h, uint8_t *buf, int size)
135
{
136
    TCPContext *s = h->priv_data;
137
    int ret;
138

    
139
    if (!(h->flags & URL_FLAG_NONBLOCK)) {
140
        ret = ff_network_wait_fd(s->fd, 0);
141
        if (ret < 0)
142
            return ret;
143
    }
144
    ret = recv(s->fd, buf, size, 0);
145
    return ret < 0 ? ff_neterrno() : ret;
146
}
147

    
148
static int tcp_write(URLContext *h, const uint8_t *buf, int size)
149
{
150
    TCPContext *s = h->priv_data;
151
    int ret;
152

    
153
    if (!(h->flags & URL_FLAG_NONBLOCK)) {
154
        ret = ff_network_wait_fd(s->fd, 1);
155
        if (ret < 0)
156
            return ret;
157
    }
158
    ret = send(s->fd, buf, size, 0);
159
    return ret < 0 ? ff_neterrno() : ret;
160
}
161

    
162
static int tcp_close(URLContext *h)
163
{
164
    TCPContext *s = h->priv_data;
165
    closesocket(s->fd);
166
    av_free(s);
167
    return 0;
168
}
169

    
170
static int tcp_get_file_handle(URLContext *h)
171
{
172
    TCPContext *s = h->priv_data;
173
    return s->fd;
174
}
175

    
176
URLProtocol ff_tcp_protocol = {
177
    "tcp",
178
    tcp_open,
179
    tcp_read,
180
    tcp_write,
181
    NULL, /* seek */
182
    tcp_close,
183
    .url_get_file_handle = tcp_get_file_handle,
184
};