Statistics
| Branch: | Revision:

ffmpeg / libavformat / udp.c @ 59612534

History | View | Annotate | Download (14.5 KB)

1 de6d9b64 Fabrice Bellard
/*
2
 * UDP prototype streaming system
3 406792e7 Diego Biurrun
 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4 de6d9b64 Fabrice Bellard
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 19720f15 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 de6d9b64 Fabrice Bellard
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 19720f15 Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16 de6d9b64 Fabrice Bellard
 *
17 19720f15 Fabrice Bellard
 * You should have received a copy of the GNU Lesser General Public
18 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 de6d9b64 Fabrice Bellard
 */
21 3adfb377 Luca Barbato
22
/**
23 ba87f080 Diego Biurrun
 * @file
24 3adfb377 Luca Barbato
 * UDP protocol
25
 */
26
27 a9e02947 Luca Abeni
#define _BSD_SOURCE     /* Needed for using struct ip_mreq with recent glibc */
28 59612534 Martin Storsjö
#define _DARWIN_C_SOURCE /* Needed for using IP_MULTICAST_TTL on OS X */
29 8be1c656 Fabrice Bellard
#include "avformat.h"
30 de6d9b64 Fabrice Bellard
#include <unistd.h>
31 e4a9e3cc Aurelien Jacobs
#include "internal.h"
32 42572ef5 Ramiro Polla
#include "network.h"
33 087b3272 Luca Abeni
#include "os_support.h"
34 b250f9c6 Aurelien Jacobs
#if HAVE_SYS_SELECT_H
35 7d084299 Jindřich Makovička
#include <sys/select.h>
36
#endif
37 c0a8f8d4 Dave Yeo
#include <sys/time.h>
38 de6d9b64 Fabrice Bellard
39 9c633e9a Alex Beregszaszi
#ifndef IPV6_ADD_MEMBERSHIP
40
#define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
41
#define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
42
#endif
43 35b74c3d Luca Abeni
#ifndef IN_MULTICAST
44
#define IN_MULTICAST(a) ((((uint32_t)(a)) & 0xf0000000) == 0xe0000000)
45
#endif
46
#ifndef IN6_IS_ADDR_MULTICAST
47
#define IN6_IS_ADDR_MULTICAST(a) (((uint8_t *) (a))[0] == 0xff)
48
#endif
49 9c633e9a Alex Beregszaszi
50 de6d9b64 Fabrice Bellard
typedef struct {
51 2d225591 Fabrice Bellard
    int udp_fd;
52
    int ttl;
53 236bb1ab Jindřich Makovička
    int buffer_size;
54 2d225591 Fabrice Bellard
    int is_multicast;
55
    int local_port;
56 34ecc397 Thijs
    int reuse_socket;
57 7a91333f Hans Zandbelt
    struct sockaddr_storage dest_addr;
58 397db8ac Luca Abeni
    int dest_addr_len;
59 de6d9b64 Fabrice Bellard
} UDPContext;
60
61
#define UDP_TX_BUF_SIZE 32768
62 f18cae4d Ramiro Polla
#define UDP_MAX_PKT_SIZE 65536
63 de6d9b64 Fabrice Bellard
64 64810754 Ronald S. Bultje
static int udp_set_multicast_ttl(int sockfd, int mcastTTL,
65
                                 struct sockaddr *addr)
66
{
67 a8bde059 Luca Abeni
#ifdef IP_MULTICAST_TTL
68 7a91333f Hans Zandbelt
    if (addr->sa_family == AF_INET) {
69
        if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL, sizeof(mcastTTL)) < 0) {
70 086119b3 Luca Abeni
            av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_MULTICAST_TTL): %s\n", strerror(errno));
71 7a91333f Hans Zandbelt
            return -1;
72
        }
73
    }
74 a8bde059 Luca Abeni
#endif
75 b4d68544 Martin Storsjö
#if defined(IPPROTO_IPV6) && defined(IPV6_MULTICAST_HOPS)
76 7a91333f Hans Zandbelt
    if (addr->sa_family == AF_INET6) {
77
        if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL, sizeof(mcastTTL)) < 0) {
78 086119b3 Luca Abeni
            av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_MULTICAST_HOPS): %s\n", strerror(errno));
79 7a91333f Hans Zandbelt
            return -1;
80
        }
81
    }
82 a8bde059 Luca Abeni
#endif
83 7a91333f Hans Zandbelt
    return 0;
84
}
85
86 64810754 Ronald S. Bultje
static int udp_join_multicast_group(int sockfd, struct sockaddr *addr)
87
{
88 a8bde059 Luca Abeni
#ifdef IP_ADD_MEMBERSHIP
89 7a91333f Hans Zandbelt
    if (addr->sa_family == AF_INET) {
90 a8bde059 Luca Abeni
        struct ip_mreq mreq;
91
92 7a91333f Hans Zandbelt
        mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
93
        mreq.imr_interface.s_addr= INADDR_ANY;
94
        if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
95 086119b3 Luca Abeni
            av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_ADD_MEMBERSHIP): %s\n", strerror(errno));
96 7a91333f Hans Zandbelt
            return -1;
97
        }
98
    }
99 a8bde059 Luca Abeni
#endif
100 63206a8f Martin Storsjö
#if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
101 7a91333f Hans Zandbelt
    if (addr->sa_family == AF_INET6) {
102 a8bde059 Luca Abeni
        struct ipv6_mreq mreq6;
103
104 7a91333f Hans Zandbelt
        memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
105
        mreq6.ipv6mr_interface= 0;
106
        if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
107 086119b3 Luca Abeni
            av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_ADD_MEMBERSHIP): %s\n", strerror(errno));
108 7a91333f Hans Zandbelt
            return -1;
109
        }
110
    }
111 a8bde059 Luca Abeni
#endif
112 7a91333f Hans Zandbelt
    return 0;
113
}
114
115 64810754 Ronald S. Bultje
static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr)
116
{
117 a8bde059 Luca Abeni
#ifdef IP_DROP_MEMBERSHIP
118 7a91333f Hans Zandbelt
    if (addr->sa_family == AF_INET) {
119 a8bde059 Luca Abeni
        struct ip_mreq mreq;
120
121 7a91333f Hans Zandbelt
        mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
122
        mreq.imr_interface.s_addr= INADDR_ANY;
123
        if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
124 086119b3 Luca Abeni
            av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_DROP_MEMBERSHIP): %s\n", strerror(errno));
125 7a91333f Hans Zandbelt
            return -1;
126
        }
127
    }
128 a8bde059 Luca Abeni
#endif
129 63206a8f Martin Storsjö
#if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
130 7a91333f Hans Zandbelt
    if (addr->sa_family == AF_INET6) {
131 a8bde059 Luca Abeni
        struct ipv6_mreq mreq6;
132
133 7a91333f Hans Zandbelt
        memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
134
        mreq6.ipv6mr_interface= 0;
135
        if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
136 086119b3 Luca Abeni
            av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_DROP_MEMBERSHIP): %s\n", strerror(errno));
137 7a91333f Hans Zandbelt
            return -1;
138
        }
139
    }
140 a8bde059 Luca Abeni
#endif
141 7a91333f Hans Zandbelt
    return 0;
142
}
143
144 64810754 Ronald S. Bultje
static struct addrinfo* udp_resolve_host(const char *hostname, int port,
145
                                         int type, int family, int flags)
146
{
147 7a91333f Hans Zandbelt
    struct addrinfo hints, *res = 0;
148
    int error;
149
    char sport[16];
150 d607861c Nicolas George
    const char *node = 0, *service = "0";
151 7a91333f Hans Zandbelt
152
    if (port > 0) {
153 2fc8ea24 Michael Niedermayer
        snprintf(sport, sizeof(sport), "%d", port);
154 7a91333f Hans Zandbelt
        service = sport;
155
    }
156
    if ((hostname) && (hostname[0] != '\0') && (hostname[0] != '?')) {
157
        node = hostname;
158
    }
159 7d8576c2 Luca Abeni
    memset(&hints, 0, sizeof(hints));
160
    hints.ai_socktype = type;
161
    hints.ai_family   = family;
162
    hints.ai_flags = flags;
163
    if ((error = getaddrinfo(node, service, &hints, &res))) {
164 11a74f36 Reimar Döffinger
        res = NULL;
165 7c823d8b Martin Storsjö
        av_log(NULL, AV_LOG_ERROR, "udp_resolve_host: %s\n", gai_strerror(error));
166 7d8576c2 Luca Abeni
    }
167
168 7a91333f Hans Zandbelt
    return res;
169
}
170
171 64810754 Ronald S. Bultje
static int udp_set_url(struct sockaddr_storage *addr,
172
                       const char *hostname, int port)
173
{
174 7a91333f Hans Zandbelt
    struct addrinfo *res0;
175 d05cb726 Luca Abeni
    int addr_len;
176
177 7c823d8b Martin Storsjö
    res0 = udp_resolve_host(hostname, port, SOCK_DGRAM, AF_UNSPEC, 0);
178 6f3e0b21 Panagiotis Issaris
    if (res0 == 0) return AVERROR(EIO);
179 d05cb726 Luca Abeni
    memcpy(addr, res0->ai_addr, res0->ai_addrlen);
180
    addr_len = res0->ai_addrlen;
181 7a91333f Hans Zandbelt
    freeaddrinfo(res0);
182 d05cb726 Luca Abeni
183
    return addr_len;
184 7a91333f Hans Zandbelt
}
185
186 35b74c3d Luca Abeni
static int is_multicast_address(struct sockaddr_storage *addr)
187
{
188
    if (addr->ss_family == AF_INET) {
189
        return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
190
    }
191 b4d68544 Martin Storsjö
#if HAVE_STRUCT_SOCKADDR_IN6
192 35b74c3d Luca Abeni
    if (addr->ss_family == AF_INET6) {
193
        return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);
194
    }
195 b4d68544 Martin Storsjö
#endif
196 35b74c3d Luca Abeni
197
    return 0;
198
}
199
200 64810754 Ronald S. Bultje
static int udp_socket_create(UDPContext *s,
201
                             struct sockaddr_storage *addr, int *addr_len)
202 aa519c47 Luca Abeni
{
203 7a91333f Hans Zandbelt
    int udp_fd = -1;
204 276358c1 Hans Zandbelt
    struct addrinfo *res0 = NULL, *res = NULL;
205 51844e6c Ronald S. Bultje
    int family = AF_UNSPEC;
206 115329f1 Diego Biurrun
207 51844e6c Ronald S. Bultje
    if (((struct sockaddr *) &s->dest_addr)->sa_family)
208
        family = ((struct sockaddr *) &s->dest_addr)->sa_family;
209 7c823d8b Martin Storsjö
    res0 = udp_resolve_host(0, s->local_port, SOCK_DGRAM, family, AI_PASSIVE);
210 7d8576c2 Luca Abeni
    if (res0 == 0)
211
        goto fail;
212
    for (res = res0; res; res=res->ai_next) {
213
        udp_fd = socket(res->ai_family, SOCK_DGRAM, 0);
214
        if (udp_fd > 0) break;
215 086119b3 Luca Abeni
        av_log(NULL, AV_LOG_ERROR, "socket: %s\n", strerror(errno));
216 7d8576c2 Luca Abeni
    }
217 276358c1 Hans Zandbelt
218
    if (udp_fd < 0)
219 7a91333f Hans Zandbelt
        goto fail;
220 115329f1 Diego Biurrun
221 aa519c47 Luca Abeni
    memcpy(addr, res->ai_addr, res->ai_addrlen);
222
    *addr_len = res->ai_addrlen;
223 7a91333f Hans Zandbelt
224 aa519c47 Luca Abeni
    freeaddrinfo(res0);
225 115329f1 Diego Biurrun
226 7a91333f Hans Zandbelt
    return udp_fd;
227 115329f1 Diego Biurrun
228 7a91333f Hans Zandbelt
 fail:
229
    if (udp_fd >= 0)
230
        closesocket(udp_fd);
231 88730be6 Måns Rullgård
    if(res0)
232
        freeaddrinfo(res0);
233 7a91333f Hans Zandbelt
    return -1;
234
}
235
236 aa519c47 Luca Abeni
static int udp_port(struct sockaddr_storage *addr, int addr_len)
237
{
238 03c09e43 D Richard Felker III
    char sbuf[sizeof(int)*3+1];
239 aa519c47 Luca Abeni
240 03c09e43 D Richard Felker III
    if (getnameinfo((struct sockaddr *)addr, addr_len, NULL, 0,  sbuf, sizeof(sbuf), NI_NUMERICSERV) != 0) {
241 086119b3 Luca Abeni
        av_log(NULL, AV_LOG_ERROR, "getnameinfo: %s\n", strerror(errno));
242 aa519c47 Luca Abeni
        return -1;
243
    }
244
245
    return strtol(sbuf, NULL, 10);
246
}
247
248 7a91333f Hans Zandbelt
249 2d225591 Fabrice Bellard
/**
250
 * If no filename is given to av_open_input_file because you want to
251
 * get the local port first, then you must call this function to set
252
 * the remote server address.
253
 *
254
 * url syntax: udp://host:port[?option=val...]
255 35b74c3d Luca Abeni
 * option: 'ttl=n'       : set the ttl value (for multicast only)
256 2d225591 Fabrice Bellard
 *         'localport=n' : set the local port
257 9899efb4 Max Krasnyansky
 *         'pkt_size=n'  : set max packet size
258 34ecc397 Thijs
 *         'reuse=1'     : enable reusing the socket
259 2d225591 Fabrice Bellard
 *
260 9a58234f Diego Biurrun
 * @param h media file context
261 2d225591 Fabrice Bellard
 * @param uri of the remote server
262
 * @return zero if no error.
263
 */
264
int udp_set_remote_url(URLContext *h, const char *uri)
265
{
266
    UDPContext *s = h->priv_data;
267
    char hostname[256];
268
    int port;
269 115329f1 Diego Biurrun
270 f3bfe388 Måns Rullgård
    av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
271 de6d9b64 Fabrice Bellard
272 2d225591 Fabrice Bellard
    /* set the destination address */
273 d05cb726 Luca Abeni
    s->dest_addr_len = udp_set_url(&s->dest_addr, hostname, port);
274
    if (s->dest_addr_len < 0) {
275 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
276 d05cb726 Luca Abeni
    }
277 35b74c3d Luca Abeni
    s->is_multicast = is_multicast_address(&s->dest_addr);
278 d05cb726 Luca Abeni
279 2d225591 Fabrice Bellard
    return 0;
280
}
281
282
/**
283 48e77473 Martin Storsjö
 * Return the local port used by the UDP connection
284 9a58234f Diego Biurrun
 * @param h media file context
285 2d225591 Fabrice Bellard
 * @return the local port number
286
 */
287
int udp_get_local_port(URLContext *h)
288
{
289
    UDPContext *s = h->priv_data;
290
    return s->local_port;
291
}
292
293
/**
294
 * Return the udp file handle for select() usage to wait for several RTP
295
 * streams at the same time.
296
 * @param h media file context
297
 */
298 f0a80394 Ronald S. Bultje
#if (LIBAVFORMAT_VERSION_MAJOR >= 53)
299
static
300
#endif
301 2d225591 Fabrice Bellard
int udp_get_file_handle(URLContext *h)
302
{
303
    UDPContext *s = h->priv_data;
304
    return s->udp_fd;
305
}
306
307
/* put it in UDP context */
308 de6d9b64 Fabrice Bellard
/* return non zero if error */
309
static int udp_open(URLContext *h, const char *uri, int flags)
310
{
311
    char hostname[1024];
312 36b53281 Jindřich Makovička
    int port, udp_fd = -1, tmp, bind_ret = -1;
313 2d225591 Fabrice Bellard
    UDPContext *s = NULL;
314 88730be6 Måns Rullgård
    int is_output;
315 2d225591 Fabrice Bellard
    const char *p;
316
    char buf[256];
317 aa519c47 Luca Abeni
    struct sockaddr_storage my_addr;
318
    int len;
319 de6d9b64 Fabrice Bellard
320
    h->is_streamed = 1;
321 9899efb4 Max Krasnyansky
    h->max_packet_size = 1472;
322 de6d9b64 Fabrice Bellard
323 2d225591 Fabrice Bellard
    is_output = (flags & URL_WRONLY);
324 115329f1 Diego Biurrun
325 51844e6c Ronald S. Bultje
    s = av_mallocz(sizeof(UDPContext));
326 2d225591 Fabrice Bellard
    if (!s)
327 8fa36ae0 François Revol
        return AVERROR(ENOMEM);
328 2d225591 Fabrice Bellard
329
    h->priv_data = s;
330
    s->ttl = 16;
331 236bb1ab Jindřich Makovička
    s->buffer_size = is_output ? UDP_TX_BUF_SIZE : UDP_MAX_PKT_SIZE;
332
333 2d225591 Fabrice Bellard
    p = strchr(uri, '?');
334
    if (p) {
335 34ecc397 Thijs
        s->reuse_socket = find_info_tag(buf, sizeof(buf), "reuse", p);
336 2d225591 Fabrice Bellard
        if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
337
            s->ttl = strtol(buf, NULL, 10);
338
        }
339
        if (find_info_tag(buf, sizeof(buf), "localport", p)) {
340
            s->local_port = strtol(buf, NULL, 10);
341
        }
342 9899efb4 Max Krasnyansky
        if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
343
            h->max_packet_size = strtol(buf, NULL, 10);
344
        }
345 236bb1ab Jindřich Makovička
        if (find_info_tag(buf, sizeof(buf), "buffer_size", p)) {
346
            s->buffer_size = strtol(buf, NULL, 10);
347
        }
348 de6d9b64 Fabrice Bellard
    }
349 2d225591 Fabrice Bellard
350
    /* fill the dest addr */
351 f3bfe388 Måns Rullgård
    av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
352 115329f1 Diego Biurrun
353 f3bfe388 Måns Rullgård
    /* XXX: fix av_url_split */
354 2d225591 Fabrice Bellard
    if (hostname[0] == '\0' || hostname[0] == '?') {
355
        /* only accepts null hostname if input */
356 35b74c3d Luca Abeni
        if (flags & URL_WRONLY)
357 2d225591 Fabrice Bellard
            goto fail;
358
    } else {
359 82505611 Reimar Döffinger
        if (udp_set_remote_url(h, uri) < 0)
360
            goto fail;
361 2d225591 Fabrice Bellard
    }
362 de6d9b64 Fabrice Bellard
363 882d00f2 Luca Abeni
    if (s->is_multicast && !(h->flags & URL_WRONLY))
364
        s->local_port = port;
365 aa519c47 Luca Abeni
    udp_fd = udp_socket_create(s, &my_addr, &len);
366 2d225591 Fabrice Bellard
    if (udp_fd < 0)
367
        goto fail;
368
369 34ecc397 Thijs
    if (s->reuse_socket)
370
        if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
371
            goto fail;
372
373 de6d9b64 Fabrice Bellard
    /* the bind is needed to give a port to the socket now */
374 36b53281 Jindřich Makovička
    /* if multicast, try the multicast address bind first */
375
    if (s->is_multicast && !(h->flags & URL_WRONLY)) {
376
        bind_ret = bind(udp_fd,(struct sockaddr *)&s->dest_addr, len);
377
    }
378
    /* bind to the local address if not multicast or if the multicast
379
     * bind failed */
380
    if (bind_ret < 0 && bind(udp_fd,(struct sockaddr *)&my_addr, len) < 0)
381 de6d9b64 Fabrice Bellard
        goto fail;
382
383 d4936869 Luca Abeni
    len = sizeof(my_addr);
384
    getsockname(udp_fd, (struct sockaddr *)&my_addr, &len);
385 aa519c47 Luca Abeni
    s->local_port = udp_port(&my_addr, len);
386
387 7a91333f Hans Zandbelt
    if (s->is_multicast) {
388
        if (h->flags & URL_WRONLY) {
389 a8bde059 Luca Abeni
            /* output */
390 9cdcb04f Luca Abeni
            if (udp_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
391 7a91333f Hans Zandbelt
                goto fail;
392
        } else {
393 a8bde059 Luca Abeni
            /* input */
394 9cdcb04f Luca Abeni
            if (udp_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr) < 0)
395 7a91333f Hans Zandbelt
                goto fail;
396
        }
397
    }
398 de6d9b64 Fabrice Bellard
399 2d225591 Fabrice Bellard
    if (is_output) {
400
        /* limit the tx buf size to limit latency */
401 236bb1ab Jindřich Makovička
        tmp = s->buffer_size;
402 2d225591 Fabrice Bellard
        if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
403 086119b3 Luca Abeni
            av_log(NULL, AV_LOG_ERROR, "setsockopt(SO_SNDBUF): %s\n", strerror(errno));
404 2d225591 Fabrice Bellard
            goto fail;
405
        }
406 f18cae4d Ramiro Polla
    } else {
407
        /* set udp recv buffer size to the largest possible udp packet size to
408
         * avoid losing data on OSes that set this too low by default. */
409 236bb1ab Jindřich Makovička
        tmp = s->buffer_size;
410
        if (setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp)) < 0) {
411
            av_log(NULL, AV_LOG_WARNING, "setsockopt(SO_RECVBUF): %s\n", strerror(errno));
412
        }
413 ce09d560 Jindřich Makovička
        /* make the socket non-blocking */
414
        ff_socket_nonblock(udp_fd, 1);
415 2d225591 Fabrice Bellard
    }
416
417
    s->udp_fd = udp_fd;
418 de6d9b64 Fabrice Bellard
    return 0;
419
 fail:
420 2d225591 Fabrice Bellard
    if (udp_fd >= 0)
421 9ddd71fc François Revol
        closesocket(udp_fd);
422 2d225591 Fabrice Bellard
    av_free(s);
423 6f3e0b21 Panagiotis Issaris
    return AVERROR(EIO);
424 de6d9b64 Fabrice Bellard
}
425
426 0c1a9eda Zdenek Kabelac
static int udp_read(URLContext *h, uint8_t *buf, int size)
427 de6d9b64 Fabrice Bellard
{
428
    UDPContext *s = h->priv_data;
429 191e8ca7 Måns Rullgård
    int len;
430 7d084299 Jindřich Makovička
    fd_set rfds;
431
    int ret;
432
    struct timeval tv;
433 2d225591 Fabrice Bellard
434
    for(;;) {
435 7d084299 Jindřich Makovička
        if (url_interrupt_cb())
436
            return AVERROR(EINTR);
437
        FD_ZERO(&rfds);
438
        FD_SET(s->udp_fd, &rfds);
439
        tv.tv_sec = 0;
440
        tv.tv_usec = 100 * 1000;
441
        ret = select(s->udp_fd + 1, &rfds, NULL, NULL, &tv);
442 cae9a15c Martin Storsjö
        if (ret < 0) {
443
            if (ff_neterrno() == FF_NETERROR(EINTR))
444
                continue;
445 7d084299 Jindřich Makovička
            return AVERROR(EIO);
446 cae9a15c Martin Storsjö
        }
447 7d084299 Jindřich Makovička
        if (!(ret > 0 && FD_ISSET(s->udp_fd, &rfds)))
448
            continue;
449 ce09d560 Jindřich Makovička
        len = recv(s->udp_fd, buf, size, 0);
450 2d225591 Fabrice Bellard
        if (len < 0) {
451 8da4034f Alex Beregszaszi
            if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
452
                ff_neterrno() != FF_NETERROR(EINTR))
453 6f3e0b21 Panagiotis Issaris
                return AVERROR(EIO);
454 2d225591 Fabrice Bellard
        } else {
455
            break;
456
        }
457
    }
458
    return len;
459 de6d9b64 Fabrice Bellard
}
460
461 27241cbf Martin Storsjö
static int udp_write(URLContext *h, const uint8_t *buf, int size)
462 de6d9b64 Fabrice Bellard
{
463
    UDPContext *s = h->priv_data;
464 2d225591 Fabrice Bellard
    int ret;
465
466
    for(;;) {
467 115329f1 Diego Biurrun
        ret = sendto (s->udp_fd, buf, size, 0,
468 2d225591 Fabrice Bellard
                      (struct sockaddr *) &s->dest_addr,
469 7a91333f Hans Zandbelt
                      s->dest_addr_len);
470 2d225591 Fabrice Bellard
        if (ret < 0) {
471 8da4034f Alex Beregszaszi
            if (ff_neterrno() != FF_NETERROR(EINTR) &&
472
                ff_neterrno() != FF_NETERROR(EAGAIN))
473 25a2ebb9 Martin Storsjö
                return ff_neterrno();
474 2d225591 Fabrice Bellard
        } else {
475
            break;
476
        }
477 de6d9b64 Fabrice Bellard
    }
478 2d225591 Fabrice Bellard
    return size;
479
}
480
481
static int udp_close(URLContext *h)
482
{
483
    UDPContext *s = h->priv_data;
484
485 7a91333f Hans Zandbelt
    if (s->is_multicast && !(h->flags & URL_WRONLY))
486 9cdcb04f Luca Abeni
        udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
487 9ddd71fc François Revol
    closesocket(s->udp_fd);
488 2d225591 Fabrice Bellard
    av_free(s);
489
    return 0;
490 de6d9b64 Fabrice Bellard
}
491
492
URLProtocol udp_protocol = {
493
    "udp",
494
    udp_open,
495 2d225591 Fabrice Bellard
    udp_read,
496 de6d9b64 Fabrice Bellard
    udp_write,
497
    NULL, /* seek */
498
    udp_close,
499 f0a80394 Ronald S. Bultje
    .url_get_file_handle = udp_get_file_handle,
500 de6d9b64 Fabrice Bellard
};