Statistics
| Branch: | Revision:

ffmpeg / libavformat / udp.c @ 890d2799

History | View | Annotate | Download (13.2 KB)

1 de6d9b64 Fabrice Bellard
/*
2
 * UDP prototype streaming system
3 2d225591 Fabrice Bellard
 * 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 8be1c656 Fabrice Bellard
#include "avformat.h"
22 de6d9b64 Fabrice Bellard
#include <unistd.h>
23 42572ef5 Ramiro Polla
#include "network.h"
24 087b3272 Luca Abeni
#include "os_support.h"
25 de6d9b64 Fabrice Bellard
26 9c633e9a Alex Beregszaszi
#ifndef IPV6_ADD_MEMBERSHIP
27
#define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
28
#define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
29
#endif
30
31 de6d9b64 Fabrice Bellard
typedef struct {
32 2d225591 Fabrice Bellard
    int udp_fd;
33
    int ttl;
34
    int is_multicast;
35
    int local_port;
36 34ecc397 Thijs
    int reuse_socket;
37 7a91333f Hans Zandbelt
#ifndef CONFIG_IPV6
38 2d225591 Fabrice Bellard
    struct sockaddr_in dest_addr;
39 7a91333f Hans Zandbelt
#else
40
    struct sockaddr_storage dest_addr;
41
#endif
42 0ec12335 Luca Abeni
    size_t dest_addr_len;
43 de6d9b64 Fabrice Bellard
} UDPContext;
44
45
#define UDP_TX_BUF_SIZE 32768
46 f18cae4d Ramiro Polla
#define UDP_MAX_PKT_SIZE 65536
47 de6d9b64 Fabrice Bellard
48 9cdcb04f Luca Abeni
static int udp_set_multicast_ttl(int sockfd, int mcastTTL, struct sockaddr *addr) {
49 a8bde059 Luca Abeni
#ifdef IP_MULTICAST_TTL
50 7a91333f Hans Zandbelt
    if (addr->sa_family == AF_INET) {
51
        if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL, sizeof(mcastTTL)) < 0) {
52 086119b3 Luca Abeni
            av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_MULTICAST_TTL): %s\n", strerror(errno));
53 7a91333f Hans Zandbelt
            return -1;
54
        }
55
    }
56 a8bde059 Luca Abeni
#endif
57
#ifdef CONFIG_IPV6
58 7a91333f Hans Zandbelt
    if (addr->sa_family == AF_INET6) {
59
        if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL, sizeof(mcastTTL)) < 0) {
60 086119b3 Luca Abeni
            av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_MULTICAST_HOPS): %s\n", strerror(errno));
61 7a91333f Hans Zandbelt
            return -1;
62
        }
63
    }
64 a8bde059 Luca Abeni
#endif
65 7a91333f Hans Zandbelt
    return 0;
66
}
67
68 9cdcb04f Luca Abeni
static int udp_join_multicast_group(int sockfd, struct sockaddr *addr) {
69 a8bde059 Luca Abeni
#ifdef IP_ADD_MEMBERSHIP
70 7a91333f Hans Zandbelt
    if (addr->sa_family == AF_INET) {
71 a8bde059 Luca Abeni
        struct ip_mreq mreq;
72
73 7a91333f Hans Zandbelt
        mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
74
        mreq.imr_interface.s_addr= INADDR_ANY;
75
        if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
76 086119b3 Luca Abeni
            av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_ADD_MEMBERSHIP): %s\n", strerror(errno));
77 7a91333f Hans Zandbelt
            return -1;
78
        }
79
    }
80 a8bde059 Luca Abeni
#endif
81
#ifdef CONFIG_IPV6
82 7a91333f Hans Zandbelt
    if (addr->sa_family == AF_INET6) {
83 a8bde059 Luca Abeni
        struct ipv6_mreq mreq6;
84
85 7a91333f Hans Zandbelt
        memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
86
        mreq6.ipv6mr_interface= 0;
87
        if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
88 086119b3 Luca Abeni
            av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_ADD_MEMBERSHIP): %s\n", strerror(errno));
89 7a91333f Hans Zandbelt
            return -1;
90
        }
91
    }
92 a8bde059 Luca Abeni
#endif
93 7a91333f Hans Zandbelt
    return 0;
94
}
95
96 9cdcb04f Luca Abeni
static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr) {
97 a8bde059 Luca Abeni
#ifdef IP_DROP_MEMBERSHIP
98 7a91333f Hans Zandbelt
    if (addr->sa_family == AF_INET) {
99 a8bde059 Luca Abeni
        struct ip_mreq mreq;
100
101 7a91333f Hans Zandbelt
        mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
102
        mreq.imr_interface.s_addr= INADDR_ANY;
103
        if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
104 086119b3 Luca Abeni
            av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_DROP_MEMBERSHIP): %s\n", strerror(errno));
105 7a91333f Hans Zandbelt
            return -1;
106
        }
107
    }
108 a8bde059 Luca Abeni
#endif
109
#ifdef CONFIG_IPV6
110 7a91333f Hans Zandbelt
    if (addr->sa_family == AF_INET6) {
111 a8bde059 Luca Abeni
        struct ipv6_mreq mreq6;
112
113 7a91333f Hans Zandbelt
        memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
114
        mreq6.ipv6mr_interface= 0;
115
        if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
116 086119b3 Luca Abeni
            av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_DROP_MEMBERSHIP): %s\n", strerror(errno));
117 7a91333f Hans Zandbelt
            return -1;
118
        }
119
    }
120 a8bde059 Luca Abeni
#endif
121 7a91333f Hans Zandbelt
    return 0;
122
}
123
124 a8bde059 Luca Abeni
#ifdef CONFIG_IPV6
125 7b49ce2e Stefan Huehner
static struct addrinfo* udp_ipv6_resolve_host(const char *hostname, int port, int type, int family, int flags) {
126 7a91333f Hans Zandbelt
    struct addrinfo hints, *res = 0;
127
    int error;
128
    char sport[16];
129 d607861c Nicolas George
    const char *node = 0, *service = "0";
130 7a91333f Hans Zandbelt
131
    if (port > 0) {
132 2fc8ea24 Michael Niedermayer
        snprintf(sport, sizeof(sport), "%d", port);
133 7a91333f Hans Zandbelt
        service = sport;
134
    }
135
    if ((hostname) && (hostname[0] != '\0') && (hostname[0] != '?')) {
136
        node = hostname;
137
    }
138 7d8576c2 Luca Abeni
    memset(&hints, 0, sizeof(hints));
139
    hints.ai_socktype = type;
140
    hints.ai_family   = family;
141
    hints.ai_flags = flags;
142
    if ((error = getaddrinfo(node, service, &hints, &res))) {
143
        av_log(NULL, AV_LOG_ERROR, "udp_ipv6_resolve_host: %s\n", gai_strerror(error));
144
    }
145
146 7a91333f Hans Zandbelt
    return res;
147
}
148
149 d05cb726 Luca Abeni
static int udp_set_url(struct sockaddr_storage *addr, const char *hostname, int port) {
150 7a91333f Hans Zandbelt
    struct addrinfo *res0;
151 d05cb726 Luca Abeni
    int addr_len;
152
153 7a91333f Hans Zandbelt
    res0 = udp_ipv6_resolve_host(hostname, port, SOCK_DGRAM, AF_UNSPEC, 0);
154 6f3e0b21 Panagiotis Issaris
    if (res0 == 0) return AVERROR(EIO);
155 d05cb726 Luca Abeni
    memcpy(addr, res0->ai_addr, res0->ai_addrlen);
156
    addr_len = res0->ai_addrlen;
157 7a91333f Hans Zandbelt
    freeaddrinfo(res0);
158 d05cb726 Luca Abeni
159
    return addr_len;
160 7a91333f Hans Zandbelt
}
161
162 aa519c47 Luca Abeni
static int udp_socket_create(UDPContext *s, struct sockaddr_storage *addr, int *addr_len)
163
{
164 7a91333f Hans Zandbelt
    int udp_fd = -1;
165 276358c1 Hans Zandbelt
    struct addrinfo *res0 = NULL, *res = NULL;
166 51844e6c Ronald S. Bultje
    int family = AF_UNSPEC;
167 115329f1 Diego Biurrun
168 51844e6c Ronald S. Bultje
    if (((struct sockaddr *) &s->dest_addr)->sa_family)
169
        family = ((struct sockaddr *) &s->dest_addr)->sa_family;
170
    res0 = udp_ipv6_resolve_host(0, s->local_port, SOCK_DGRAM, family, AI_PASSIVE);
171 7d8576c2 Luca Abeni
    if (res0 == 0)
172
        goto fail;
173
    for (res = res0; res; res=res->ai_next) {
174
        udp_fd = socket(res->ai_family, SOCK_DGRAM, 0);
175
        if (udp_fd > 0) break;
176 086119b3 Luca Abeni
        av_log(NULL, AV_LOG_ERROR, "socket: %s\n", strerror(errno));
177 7d8576c2 Luca Abeni
    }
178 276358c1 Hans Zandbelt
179
    if (udp_fd < 0)
180 7a91333f Hans Zandbelt
        goto fail;
181 115329f1 Diego Biurrun
182 aa519c47 Luca Abeni
    memcpy(addr, res->ai_addr, res->ai_addrlen);
183
    *addr_len = res->ai_addrlen;
184 7a91333f Hans Zandbelt
185 aa519c47 Luca Abeni
    freeaddrinfo(res0);
186 115329f1 Diego Biurrun
187 7a91333f Hans Zandbelt
    return udp_fd;
188 115329f1 Diego Biurrun
189 7a91333f Hans Zandbelt
 fail:
190
    if (udp_fd >= 0)
191
        closesocket(udp_fd);
192 88730be6 Måns Rullgård
    if(res0)
193
        freeaddrinfo(res0);
194 7a91333f Hans Zandbelt
    return -1;
195
}
196
197 aa519c47 Luca Abeni
static int udp_port(struct sockaddr_storage *addr, int addr_len)
198
{
199
    char sbuf[NI_MAXSERV];
200
    char hbuf[NI_MAXHOST];
201
202
    if (getnameinfo((struct sockaddr *)addr, addr_len, hbuf, sizeof(hbuf),  sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
203 086119b3 Luca Abeni
        av_log(NULL, AV_LOG_ERROR, "getnameinfo: %s\n", strerror(errno));
204 aa519c47 Luca Abeni
        return -1;
205
    }
206
207
    return strtol(sbuf, NULL, 10);
208
}
209
210 d05cb726 Luca Abeni
#else
211
212
static int udp_set_url(struct sockaddr_in *addr, const char *hostname, int port)
213
{
214
    /* set the destination address */
215
    if (resolve_host(&addr->sin_addr, hostname) < 0)
216
        return AVERROR(EIO);
217
    addr->sin_family = AF_INET;
218
    addr->sin_port = htons(port);
219
220
    return sizeof(struct sockaddr_in);
221
}
222
223 aa519c47 Luca Abeni
static int udp_socket_create(UDPContext *s, struct sockaddr_in *addr, int *addr_len)
224
{
225
    int fd;
226
227
    fd = socket(AF_INET, SOCK_DGRAM, 0);
228
    if (fd < 0)
229
        return -1;
230
231
    addr->sin_family = AF_INET;
232
    addr->sin_addr.s_addr = htonl (INADDR_ANY);
233
    addr->sin_port = htons(s->local_port);
234
    *addr_len = sizeof(struct sockaddr_in);
235
236
    return fd;
237
}
238
239
static int udp_port(struct sockaddr_in *addr, int len)
240
{
241
    return ntohs(addr->sin_port);
242
}
243 0a4f20c6 Diego Biurrun
#endif /* CONFIG_IPV6 */
244 7a91333f Hans Zandbelt
245
246 2d225591 Fabrice Bellard
/**
247
 * If no filename is given to av_open_input_file because you want to
248
 * get the local port first, then you must call this function to set
249
 * the remote server address.
250
 *
251
 * url syntax: udp://host:port[?option=val...]
252 115329f1 Diego Biurrun
 * option: 'multicast=1' : enable multicast
253 2d225591 Fabrice Bellard
 *         'ttl=n'       : set the ttl value (for multicast only)
254
 *         'localport=n' : set the local port
255 9899efb4 Max Krasnyansky
 *         'pkt_size=n'  : set max packet size
256 34ecc397 Thijs
 *         'reuse=1'     : enable reusing the socket
257 2d225591 Fabrice Bellard
 *
258
 * @param s1 media file context
259
 * @param uri of the remote server
260
 * @return zero if no error.
261
 */
262
int udp_set_remote_url(URLContext *h, const char *uri)
263
{
264
    UDPContext *s = h->priv_data;
265
    char hostname[256];
266
    int port;
267 115329f1 Diego Biurrun
268 6ba5cbc6 Petr Doubek
    url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
269 de6d9b64 Fabrice Bellard
270 2d225591 Fabrice Bellard
    /* set the destination address */
271 d05cb726 Luca Abeni
    s->dest_addr_len = udp_set_url(&s->dest_addr, hostname, port);
272
    if (s->dest_addr_len < 0) {
273 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
274 d05cb726 Luca Abeni
    }
275
276 2d225591 Fabrice Bellard
    return 0;
277
}
278
279
/**
280
 * Return the local port used by the UDP connexion
281
 * @param s1 media file context
282
 * @return the local port number
283
 */
284
int udp_get_local_port(URLContext *h)
285
{
286
    UDPContext *s = h->priv_data;
287
    return s->local_port;
288
}
289
290
/**
291
 * Return the udp file handle for select() usage to wait for several RTP
292
 * streams at the same time.
293
 * @param h media file context
294
 */
295
int udp_get_file_handle(URLContext *h)
296
{
297
    UDPContext *s = h->priv_data;
298
    return s->udp_fd;
299
}
300
301
/* put it in UDP context */
302 de6d9b64 Fabrice Bellard
/* return non zero if error */
303
static int udp_open(URLContext *h, const char *uri, int flags)
304
{
305
    char hostname[1024];
306 2d225591 Fabrice Bellard
    int port, udp_fd = -1, tmp;
307
    UDPContext *s = NULL;
308 88730be6 Måns Rullgård
    int is_output;
309 2d225591 Fabrice Bellard
    const char *p;
310
    char buf[256];
311 88730be6 Måns Rullgård
#ifndef CONFIG_IPV6
312 d4936869 Luca Abeni
    struct sockaddr_in my_addr;
313 aa519c47 Luca Abeni
#else
314
    struct sockaddr_storage my_addr;
315 88730be6 Måns Rullgård
#endif
316 aa519c47 Luca Abeni
    int len;
317 de6d9b64 Fabrice Bellard
318
    h->is_streamed = 1;
319 9899efb4 Max Krasnyansky
    h->max_packet_size = 1472;
320 de6d9b64 Fabrice Bellard
321 2d225591 Fabrice Bellard
    is_output = (flags & URL_WRONLY);
322 115329f1 Diego Biurrun
323 51844e6c Ronald S. Bultje
    s = av_mallocz(sizeof(UDPContext));
324 2d225591 Fabrice Bellard
    if (!s)
325 8fa36ae0 François Revol
        return AVERROR(ENOMEM);
326 2d225591 Fabrice Bellard
327
    h->priv_data = s;
328
    s->ttl = 16;
329
    p = strchr(uri, '?');
330
    if (p) {
331
        s->is_multicast = find_info_tag(buf, sizeof(buf), "multicast", p);
332 34ecc397 Thijs
        s->reuse_socket = find_info_tag(buf, sizeof(buf), "reuse", p);
333 2d225591 Fabrice Bellard
        if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
334
            s->ttl = strtol(buf, NULL, 10);
335
        }
336
        if (find_info_tag(buf, sizeof(buf), "localport", p)) {
337
            s->local_port = strtol(buf, NULL, 10);
338
        }
339 9899efb4 Max Krasnyansky
        if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
340
            h->max_packet_size = strtol(buf, NULL, 10);
341
        }
342 de6d9b64 Fabrice Bellard
    }
343 2d225591 Fabrice Bellard
344
    /* fill the dest addr */
345 6ba5cbc6 Petr Doubek
    url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
346 115329f1 Diego Biurrun
347 2d225591 Fabrice Bellard
    /* XXX: fix url_split */
348
    if (hostname[0] == '\0' || hostname[0] == '?') {
349
        /* only accepts null hostname if input */
350
        if (s->is_multicast || (flags & URL_WRONLY))
351
            goto fail;
352
    } else {
353
        udp_set_remote_url(h, uri);
354
    }
355 de6d9b64 Fabrice Bellard
356 1642cb6b Ramiro Polla
    if(!ff_network_init())
357
        return AVERROR(EIO);
358
359 882d00f2 Luca Abeni
    if (s->is_multicast && !(h->flags & URL_WRONLY))
360
        s->local_port = port;
361 aa519c47 Luca Abeni
    udp_fd = udp_socket_create(s, &my_addr, &len);
362 2d225591 Fabrice Bellard
    if (udp_fd < 0)
363
        goto fail;
364
365 34ecc397 Thijs
    if (s->reuse_socket)
366
        if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
367
            goto fail;
368
369 de6d9b64 Fabrice Bellard
    /* the bind is needed to give a port to the socket now */
370 aa519c47 Luca Abeni
    if (bind(udp_fd,(struct sockaddr *)&my_addr, len) < 0)
371 de6d9b64 Fabrice Bellard
        goto fail;
372
373 d4936869 Luca Abeni
    len = sizeof(my_addr);
374
    getsockname(udp_fd, (struct sockaddr *)&my_addr, &len);
375 aa519c47 Luca Abeni
    s->local_port = udp_port(&my_addr, len);
376
377 7a91333f Hans Zandbelt
    if (s->is_multicast) {
378
        if (h->flags & URL_WRONLY) {
379 a8bde059 Luca Abeni
            /* output */
380 9cdcb04f Luca Abeni
            if (udp_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
381 7a91333f Hans Zandbelt
                goto fail;
382
        } else {
383 a8bde059 Luca Abeni
            /* input */
384 9cdcb04f Luca Abeni
            if (udp_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr) < 0)
385 7a91333f Hans Zandbelt
                goto fail;
386
        }
387
    }
388 de6d9b64 Fabrice Bellard
389 2d225591 Fabrice Bellard
    if (is_output) {
390
        /* limit the tx buf size to limit latency */
391
        tmp = UDP_TX_BUF_SIZE;
392
        if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
393 086119b3 Luca Abeni
            av_log(NULL, AV_LOG_ERROR, "setsockopt(SO_SNDBUF): %s\n", strerror(errno));
394 2d225591 Fabrice Bellard
            goto fail;
395
        }
396 f18cae4d Ramiro Polla
    } else {
397
        /* set udp recv buffer size to the largest possible udp packet size to
398
         * avoid losing data on OSes that set this too low by default. */
399
        tmp = UDP_MAX_PKT_SIZE;
400
        setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp));
401 2d225591 Fabrice Bellard
    }
402
403
    s->udp_fd = udp_fd;
404 de6d9b64 Fabrice Bellard
    return 0;
405
 fail:
406 2d225591 Fabrice Bellard
    if (udp_fd >= 0)
407 9ddd71fc François Revol
        closesocket(udp_fd);
408 2d225591 Fabrice Bellard
    av_free(s);
409 6f3e0b21 Panagiotis Issaris
    return AVERROR(EIO);
410 de6d9b64 Fabrice Bellard
}
411
412 0c1a9eda Zdenek Kabelac
static int udp_read(URLContext *h, uint8_t *buf, int size)
413 de6d9b64 Fabrice Bellard
{
414
    UDPContext *s = h->priv_data;
415 191e8ca7 Måns Rullgård
    int len;
416 2d225591 Fabrice Bellard
417
    for(;;) {
418 4c7d5764 Luca Abeni
        len = recv(s->udp_fd, buf, size, 0);
419 2d225591 Fabrice Bellard
        if (len < 0) {
420 8da4034f Alex Beregszaszi
            if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
421
                ff_neterrno() != FF_NETERROR(EINTR))
422 6f3e0b21 Panagiotis Issaris
                return AVERROR(EIO);
423 2d225591 Fabrice Bellard
        } else {
424
            break;
425
        }
426
    }
427
    return len;
428 de6d9b64 Fabrice Bellard
}
429
430 0c1a9eda Zdenek Kabelac
static int udp_write(URLContext *h, uint8_t *buf, int size)
431 de6d9b64 Fabrice Bellard
{
432
    UDPContext *s = h->priv_data;
433 2d225591 Fabrice Bellard
    int ret;
434
435
    for(;;) {
436 115329f1 Diego Biurrun
        ret = sendto (s->udp_fd, buf, size, 0,
437 2d225591 Fabrice Bellard
                      (struct sockaddr *) &s->dest_addr,
438 7a91333f Hans Zandbelt
                      s->dest_addr_len);
439 2d225591 Fabrice Bellard
        if (ret < 0) {
440 8da4034f Alex Beregszaszi
            if (ff_neterrno() != FF_NETERROR(EINTR) &&
441
                ff_neterrno() != FF_NETERROR(EAGAIN))
442 6f3e0b21 Panagiotis Issaris
                return AVERROR(EIO);
443 2d225591 Fabrice Bellard
        } else {
444
            break;
445
        }
446 de6d9b64 Fabrice Bellard
    }
447 2d225591 Fabrice Bellard
    return size;
448
}
449
450
static int udp_close(URLContext *h)
451
{
452
    UDPContext *s = h->priv_data;
453
454 7a91333f Hans Zandbelt
    if (s->is_multicast && !(h->flags & URL_WRONLY))
455 9cdcb04f Luca Abeni
        udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
456 9ddd71fc François Revol
    closesocket(s->udp_fd);
457 1642cb6b Ramiro Polla
    ff_network_close();
458 2d225591 Fabrice Bellard
    av_free(s);
459
    return 0;
460 de6d9b64 Fabrice Bellard
}
461
462
URLProtocol udp_protocol = {
463
    "udp",
464
    udp_open,
465 2d225591 Fabrice Bellard
    udp_read,
466 de6d9b64 Fabrice Bellard
    udp_write,
467
    NULL, /* seek */
468
    udp_close,
469
};